From b72c2c978fc256f19df318599e5b9ba7f183c15d Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Fri, 9 Sep 2022 22:34:38 -0700 Subject: [PATCH] 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 --- libs/sysaudio/src/soundio.zig | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/libs/sysaudio/src/soundio.zig b/libs/sysaudio/src/soundio.zig index bf8938c6..2b917e5d 100644 --- a/libs/sysaudio/src/soundio.zig +++ b/libs/sysaudio/src/soundio.zig @@ -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));