From cddefdd6e5c7fb632088d504d2ed96368fa44e0f Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 00:07:07 -0300 Subject: host.newSound: samples a sketch made become a sound, played by voices A sketch that synthesised a one-shot had to stream it (and keep a replay from re-firing it by hand) or encode a WAV for decodeSound. host.newSound (samples) copies mono samples at in.sample_rate into the host and gives an Audio like a loaded one, so the one-shot is a voice: reconciled by key and serial, mixed, faded and limited by the host. It is kept for the life of the host like every sound, and .none if a sample is not finite. The vtable entry is C callconv (pointer and length), as anything new in abi.zig is, for a second host; the web host keeps the copy for samples() (its voices are silent). Tests: a unit test that the sound is a copy and that NaN and infinity are refused; test/voices.zig makes a 1000-sample blip in setup and declares it under a serial space bumps, and test/voices.script checks it sounds once, stays silent through a recording of it played, stepped and seeked, and sounds again for a new serial. For that, expect-audio KEY also passes when voice KEY was mixed since it was last asked, and expect-no-audio KEY says it was not. zig build test-audio runs the script too. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- src/host/audio.zig | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'src/host/audio.zig') diff --git a/src/host/audio.zig b/src/host/audio.zig index d9f258a..31ac38a 100644 --- a/src/host/audio.zig +++ b/src/host/audio.zig @@ -29,7 +29,14 @@ pub const Audio = struct { /// Set when opening failed, so it is tried once per declaration. deaf: bool = false, - const Playing = struct { voice: abi.Voice, cursor: usize = 0, done: bool = false, seen: bool = true }; + const Playing = struct { + voice: abi.Voice, + cursor: usize = 0, + done: bool = false, + seen: bool = true, + /// Mixed since a script last asked (`expect-audio`). + sounded: bool = false, + }; const Stream = struct { key: u32, out: *c.SDL_AudioStream, pushed: u64 = 0 }; pub fn open(self: *Audio) !void { @@ -81,6 +88,12 @@ pub const Audio = struct { return self.add(decodeBytes(self.gpa, wav) catch return .none); } + /// A copy of `samples`; `.none` if one is not finite. + pub fn new(self: *Audio, samples: []const f32) abi.Audio { + for (samples) |v| if (!std.math.isFinite(v)) return .none; + return self.add(self.gpa.dupe(f32, samples) catch return .none); + } + fn add(self: *Audio, samples: []f32) abi.Audio { self.clips.append(self.gpa, samples) catch { self.gpa.free(samples); @@ -203,6 +216,7 @@ pub const Audio = struct { continue; } alive = true; + playing.sounded = true; const v = playing.voice; const edge = @min(playing.cursor + 1, samples.len - playing.cursor); const fade: f32 = if (v.fade == 0) 1 else @as(f32, @floatFromInt(@min(edge, v.fade))) / @as(f32, @floatFromInt(v.fade)); @@ -217,6 +231,15 @@ pub const Audio = struct { return count; } + /// Whether voice `key` was mixed since the last call: for scripts. + pub fn sounded(self: *Audio, key: u32) bool { + for (self.voices.items) |*playing| if (playing.voice.key == key) { + defer playing.sounded = false; + return playing.sounded; + }; + return false; + } + pub fn streamStatus(self: *Audio) []const abi.Stream { self.status.clearRetainingCapacity(); for (self.streams.items) |s| { @@ -343,6 +366,17 @@ test "voices reconcile: one-shots finish and stay silent, new serials restart, u try std.testing.expectEqual(@as(usize, 8), audio.mix(&out)); } +test "voices play made sounds: copied, and refused when not finite" { + var audio: Audio = .{ .gpa = std.testing.allocator }; + defer audio.deinit(); + var made = [_]f32{ 0.25, -0.25 }; + const sound = audio.new(&made); + made[0] = 1; + try std.testing.expectEqualSlices(f32, &.{ 0.25, -0.25 }, audio.samplesOf(sound)); + try std.testing.expectEqual(abi.Audio.none, audio.new(&.{ 0, std.math.nan(f32) })); + try std.testing.expectEqual(abi.Audio.none, audio.new(&.{std.math.inf(f32)})); +} + test "the mixer passes the mix through below the knee and limits it to 1 above, rising" { // Identity to the bit: every 997th float from 0 to the knee, both signs. const knee_bits: u32 = @bitCast(@as(f32, knee)); -- cgit v1.3