summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/abi.zig4
-rw-r--r--src/host/Host.zig4
-rw-r--r--src/host/audio.zig22
-rw-r--r--src/host/overlay.zig4
-rw-r--r--src/sketch/root.zig1
5 files changed, 30 insertions, 5 deletions
diff --git a/src/abi.zig b/src/abi.zig
index 2e80a35..6411b0d 100644
--- a/src/abi.zig
+++ b/src/abi.zig
@@ -829,6 +829,10 @@ pub const Info = struct {
/// Whether the save keeps history, or only the present and the marks
/// (`App.Options.record`; Debug builds keep it unless told otherwise).
record: bool = false,
+ /// Samples the sound device takes at once, asked of SDL when the host
+ /// opens it (`SDL_AUDIO_DEVICE_SAMPLE_FRAMES` in the environment wins);
+ /// 0 is SDL's own, 1024 at 48 kHz. The app's `audio_pull`.
+ audio_pull: u32 = 0,
};
/// Which memory `sketch_defaults` fills.
diff --git a/src/host/Host.zig b/src/host/Host.zig
index 135f5c4..0a8eeee 100644
--- a/src/host/Host.zig
+++ b/src/host/Host.zig
@@ -218,7 +218,7 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std
if (options.profile_gpu) if (self.passes) |*g| {
g.profile = .init(gpa);
};
- self.audio.open() catch |err| std.log.warn("audio unavailable: {t}", .{err});
+ self.audio.open(info.audio_pull) catch |err| std.log.warn("audio unavailable: {t}", .{err});
if (options.library) |path| if (linked == null) {
self.watch = Watch.init(path) catch null;
};
@@ -546,7 +546,7 @@ fn draw(self: *Host, now: u64, started: u64) !u64 {
if (self.console.overlay_memory) try overlay.drawMemory(&extra, arena, self.state.layout, self.live(), self.input.width);
if (self.console.overlay_sound) {
const samples = self.audio.scope.snapshot();
- try overlay.drawScope(&extra, arena, &samples, self.audio.scope.rate, self.canvas);
+ try overlay.drawScope(&extra, arena, &samples, self.audio.scope.rate, self.audio.pull, self.canvas);
}
if (self.timing.visible) {
if (self.timing_atlas.image == .none) self.timing_atlas = try overlay.TimingAtlas.init(&self.images, &self.painter);
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);
+ }
+}
diff --git a/src/host/overlay.zig b/src/host/overlay.zig
index e1fc45d..ec040e7 100644
--- a/src/host/overlay.zig
+++ b/src/host/overlay.zig
@@ -111,7 +111,7 @@ pub fn drawMemory(out: *List, arena: std.mem.Allocator, layout: memory.Layout, s
}
}
-pub fn drawScope(out: *List, arena: std.mem.Allocator, samples: []const f32, rate: u32, canvas: [2]u32) !void {
+pub fn drawScope(out: *List, arena: std.mem.Allocator, samples: []const f32, rate: u32, pull: u32, canvas: [2]u32) !void {
const green: abi.Color = .{ .r = 105, .g = 245, .b = 160 };
const x = @as(f32, @floatFromInt(canvas[0])) - 526;
const top = @as(f32, @floatFromInt(canvas[1])) - 130;
@@ -119,7 +119,7 @@ pub fn drawScope(out: *List, arena: std.mem.Allocator, samples: []const f32, rat
try out.append(arena, .{ .rect = .{ .dst = .{ .x = x, .y = top, .w = 514, .h = 118 }, .color = .{ .r = 24, .g = 24, .b = 24 } } });
try out.append(arena, .{ .rect = .{ .dst = .{ .x = x + 1, .y = mid, .w = 512, .h = 1 }, .color = .{ .r = 60, .g = 85, .b = 65 } } });
const ms = @as(f32, @floatFromInt(samples.len * 1000)) / @as(f32, @floatFromInt(@max(rate, 1)));
- try out.append(arena, .{ .text = .{ .x = x + 8, .y = top + 10, .scale = 1, .color = green, .bytes = try std.fmt.allocPrint(arena, "OUTPUT ch1 / {d:.1}ms / overlay sound to hide", .{ms}) } });
+ try out.append(arena, .{ .text = .{ .x = x + 8, .y = top + 10, .scale = 1, .color = green, .bytes = try std.fmt.allocPrint(arena, "OUTPUT ch1 / {d:.1}ms / pull {d} / overlay sound to hide", .{ ms, pull }) } });
for (samples, 0..) |sample, i| {
const a = std.math.clamp(if (std.math.isFinite(sample)) sample else 0, -1, 1);
const previous = samples[i -| 1];
diff --git a/src/sketch/root.zig b/src/sketch/root.zig
index 2a7c64a..755e446 100644
--- a/src/sketch/root.zig
+++ b/src/sketch/root.zig
@@ -354,6 +354,7 @@ pub fn Glue(comptime App: type, comptime made: Build) type {
.commands = if (@hasDecl(App, "commands")) &App.commands else &.{},
.source = made.source ++ " " ++ abi.build,
.record = made.record,
+ .audio_pull = if (@hasDecl(App, "audio_pull")) App.audio_pull else 0,
};
/// What a call can offer, by type.