summaryrefslogtreecommitdiff
path: root/src/host/audio.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/host/audio.zig')
-rw-r--r--src/host/audio.zig22
1 files changed, 21 insertions, 1 deletions
diff --git a/src/host/audio.zig b/src/host/audio.zig
index e95e38e..880be72 100644
--- a/src/host/audio.zig
+++ b/src/host/audio.zig
@@ -67,7 +67,11 @@ pub const Audio = struct {
underruns: u32 = 0,
};
- pub fn open(self: *Audio) !void {
+ /// The default playback device, taking `pull` samples at once if asked
+ /// (`abi.Info.audio_pull`): a hint, so the environment's wins.
+ pub fn open(self: *Audio, pull: u32) !void {
+ var hint: [16]u8 = undefined;
+ if (pull > 0) _ = c.SDL_SetHint(c.SDL_HINT_AUDIO_DEVICE_SAMPLE_FRAMES, try std.fmt.bufPrintSentinel(&hint, "{d}", .{pull}, 0));
const device = c.SDL_OpenAudioDevice(c.SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec);
if (device == 0) return error.OpenAudio;
self.device = device;
@@ -539,3 +543,19 @@ test "the lead the mixer gives streams outlasts the slowest recent frame and the
intervals[6] = std.time.ns_per_s;
try expectEqual(@as(u32, max_lead), lead(intervals, 1024, &margin, false));
}
+
+test "the mixer's device takes at once what the sketch asks, SDL's own when it asks nothing, and the environment's over both" {
+ _ = c.SDL_SetHintWithPriority(c.SDL_HINT_AUDIO_DRIVER, "dummy", c.SDL_HINT_OVERRIDE);
+ try std.testing.expect(c.SDL_Init(c.SDL_INIT_AUDIO));
+ defer c.SDL_Quit();
+ defer _ = c.SDL_ResetHint(c.SDL_HINT_AUDIO_DEVICE_SAMPLE_FRAMES);
+ for ([_][3]u32{ .{ 0, 0, 1024 }, .{ 256, 0, 256 }, .{ 256, 512, 512 } }) |case| {
+ // SDL's copy of the environment, not the process's.
+ if (case[1] > 0) try std.testing.expect(c.SDL_SetEnvironmentVariable(c.SDL_GetEnvironment(), c.SDL_HINT_AUDIO_DEVICE_SAMPLE_FRAMES, "512", true));
+ defer _ = c.SDL_UnsetEnvironmentVariable(c.SDL_GetEnvironment(), c.SDL_HINT_AUDIO_DEVICE_SAMPLE_FRAMES);
+ var a: Audio = .{ .gpa = std.testing.allocator };
+ try a.open(case[0]);
+ defer a.deinit();
+ try std.testing.expectEqual(case[2], a.pull);
+ }
+}