sysaudio: target a desired buffer size / latency instead of assuming maximum

On most platforms the maximum amount of frames we can write is reasonable and
provides rarely noticable latency, but on some platforms (e.g. Windows prior
to Mason's patch) this is not true. It's a good idea therefor to target a
desired buffer size / number of frames to write per update, which is also exactly
what we must decide on the WebAudio backend. For now this is hard-coded in sysaudio
but we will expose this as a config option soon.

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2022-09-09 22:34:38 -07:00
parent c009ff3f90
commit b72c2c978f

View file

@ -23,6 +23,8 @@ pub const DataCallback = if (@import("builtin").zig_backend == .stage1)
else
*const fn (device: *Device, user_data: ?*anyopaque, buffer: []u8) void;
const default_buffer_size_per_channel = 1024; // 21.33ms
pub const Device = struct {
properties: Properties,
@ -75,7 +77,16 @@ pub const Device = struct {
// Invoke our data callback with a temporary buffer, this involves one copy later
// but it's such a small amount of memory it is entirely negligible.
const layout = outstream.layout();
const total_frame_count = @intCast(usize, frame_count_max);
const desired_frame_count = default_buffer_size_per_channel * layout.channelCount();
const total_frame_count = if (frame_count_max > desired_frame_count)
if (frame_count_min <= desired_frame_count)
@intCast(usize, desired_frame_count)
else
@intCast(usize, frame_count_min)
else
@intCast(usize, frame_count_max);
const buffer_size: usize = @sizeOf(f32) * total_frame_count * @intCast(usize, layout.channelCount());
const addr = @ptrToInt(&device.planar_buffer);
const aligned_addr = std.mem.alignForward(addr, @alignOf(f32));