sysaudio: improve backend selection; remove i8 sample support (#767)

* sysaudio: fix compilation errors
* re-order backend selection
* remove i8 samples support from backends and disable signedToSigned convertion for now
* update sine-wave example
This commit is contained in:
Ali Chraghi 2023-04-30 16:36:39 -07:00 committed by GitHub
parent 8fbc36999f
commit ed05166348
Failed to generate hash of commit
8 changed files with 64 additions and 89 deletions

View file

@ -327,7 +327,7 @@ pub const Context = struct {
const sample_rate = device.sample_rate.clamp(options.sample_rate);
const sample_spec = c.pa_sample_spec{
.format = toPAFormat(format) catch unreachable,
.format = toPAFormat(format),
.rate = sample_rate,
.channels = @intCast(u5, device.channels.len),
};
@ -459,33 +459,28 @@ pub const Player = struct {
var self = @ptrCast(*Player, @alignCast(@alignOf(*Player), user_data.?));
var frames_left = nbytes;
while (frames_left > 0) {
var chunk_size = frames_left;
if (lib.pa_stream_begin_write(
self.stream,
@ptrCast(
[*c]?*anyopaque,
@alignCast(@alignOf([*c]?*anyopaque), &self.write_ptr),
),
&chunk_size,
) != 0) {
if (std.debug.runtime_safety) unreachable;
return;
}
if (lib.pa_stream_begin_write(
self.stream,
@ptrCast(
[*c]?*anyopaque,
@alignCast(@alignOf([*c]?*anyopaque), &self.write_ptr),
),
&frames_left,
) != 0) {
if (std.debug.runtime_safety) unreachable;
return;
}
for (self.channels, 0..) |*ch, i| {
ch.*.ptr = self.write_ptr + self.format.frameSize(i);
}
for (self.channels, 0..) |*ch, i| {
ch.*.ptr = self.write_ptr + self.format.frameSize(i);
}
const frames = chunk_size / self.format.frameSize(self.channels.len);
self.writeFn(self.user_data, frames);
const frames = frames_left / self.format.frameSize(self.channels.len);
self.writeFn(self.user_data, frames);
if (lib.pa_stream_write(self.stream, self.write_ptr, chunk_size, null, 0, c.PA_SEEK_RELATIVE) != 0) {
if (std.debug.runtime_safety) unreachable;
return;
}
frames_left -= chunk_size;
if (lib.pa_stream_write(self.stream, self.write_ptr, frames_left, null, 0, c.PA_SEEK_RELATIVE) != 0) {
if (std.debug.runtime_safety) unreachable;
return;
}
}
@ -616,7 +611,7 @@ pub fn fromPAChannelPos(pos: c.pa_channel_position_t) !main.Channel.Id {
c.PA_CHANNEL_POSITION_SIDE_RIGHT => .side_right,
// TODO: .front_center?
c.PA_CHANNEL_POSITION_AUX0...c.PA_CHANNEL_POSITION_AUX31 => error.Invalid,
c.PA_CHANNEL_POSITION_AUX0...c.PA_CHANNEL_POSITION_AUX31 => .front_center,
c.PA_CHANNEL_POSITION_TOP_CENTER => .top_center,
c.PA_CHANNEL_POSITION_TOP_FRONT_LEFT => .top_front_left,
@ -630,7 +625,7 @@ pub fn fromPAChannelPos(pos: c.pa_channel_position_t) !main.Channel.Id {
};
}
pub fn toPAFormat(format: main.Format) !c.pa_sample_format_t {
pub fn toPAFormat(format: main.Format) c.pa_sample_format_t {
return switch (format) {
.u8 => c.PA_SAMPLE_U8,
.i16 => if (is_little) c.PA_SAMPLE_S16LE else c.PA_SAMPLE_S16BE,
@ -638,8 +633,6 @@ pub fn toPAFormat(format: main.Format) !c.pa_sample_format_t {
.i24_4b => if (is_little) c.PA_SAMPLE_S24_32LE else c.PA_SAMPLE_S24_32BE,
.i32 => if (is_little) c.PA_SAMPLE_S32LE else c.PA_SAMPLE_S32BE,
.f32 => if (is_little) c.PA_SAMPLE_FLOAT32LE else c.PA_SAMPLE_FLOAT32BE,
.i8 => error.Invalid,
};
}