summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 00:07:07 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commitcddefdd6e5c7fb632088d504d2ed96368fa44e0f (patch)
tree3ae5bf53be2f5dc5746c9d3ece5e90786c3637b6
parentfe0cee90f9bd4caa60b907e502aec4cc0daa4b60 (diff)
downloadsketchbook-cddefdd6e5c7fb632088d504d2ed96368fa44e0f.tar.gz
sketchbook-cddefdd6e5c7fb632088d504d2ed96368fa44e0f.zip
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 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
-rw-r--r--build.zig9
-rw-r--r--src/abi.zig10
-rw-r--r--src/host/Host.zig4
-rw-r--r--src/host/audio.zig36
-rw-r--r--src/host/script.zig9
-rw-r--r--src/sketch/web.zig11
-rw-r--r--test/voices.script29
-rw-r--r--test/voices.zig28
8 files changed, 132 insertions, 4 deletions
diff --git a/build.zig b/build.zig
index ac819c4..d2176d9 100644
--- a/build.zig
+++ b/build.zig
@@ -92,7 +92,8 @@ pub fn build(b: *Build) void {
b.step("test-sessions", "Test the sessions database: frames, keyframes, marks, clips").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = host, .filters = &.{"session"} })).step);
b.step("test-presentation", "Test presentation feedback, pacing and trace snapshots").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = host, .filters = &.{"presentation"} })).step);
- b.step("test-audio", "Test the voice mixer: reconciling, limiting, pitch and groups").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = host, .filters = &.{ "voice", "mixer" } })).step);
+ const test_audio = b.step("test-audio", "Test the voice mixer (reconciling, limiting, pitch, groups) and a made sound played as a voice");
+ test_audio.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = host, .filters = &.{ "voice", "mixer" } })).step);
const setup_tests = b.step("test-setup", "Test resource setup lifecycle and arguments");
for ([_]*Build.Module{ host, b.createModule(.{ .root_source_file = b.path("src/sketch/root.zig"), .target = target, .optimize = optimize, .imports = sketch_imports }) }) |module| setup_tests.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = module, .filters = &.{"setup"} })).step);
@@ -245,6 +246,12 @@ pub fn build(b: *Build) void {
// `Host.textSize` against the text drawn (test/text.zig).
const text = sb.app(.{ .name = "text", .root_source_file = b.path("test/text.zig") });
test_step.dependOn(&text.addScript(text.addExecutable(), b.path("test/text.script"), .{}).step);
+ // A sound the sketch made, as a voice: once per serial, not again on a
+ // replay (test/voices.zig).
+ const voices = sb.app(.{ .name = "voices", .root_source_file = b.path("test/voices.zig") });
+ const voices_script = voices.addScript(voices.addExecutable(), b.path("test/voices.script"), .{});
+ test_step.dependOn(&voices_script.step);
+ test_audio.dependOn(&voices_script.step);
// The anim module's player on its test scenes: a live one with a
// custom object between vector objects, and a compiled one, which is
// also exported, at half size.
diff --git a/src/abi.zig b/src/abi.zig
index f0abdb3..3c47e69 100644
--- a/src/abi.zig
+++ b/src/abi.zig
@@ -632,6 +632,8 @@ pub const Host = struct {
/// WAV, FLAC or Ogg Vorbis bytes in memory. Not cached. `.none` if
/// they do not decode.
decodeSound: *const fn (*anyopaque, []const u8) Audio,
+ /// Samples a sketch made, copied: see `Host.newSound`.
+ newSound: *const fn (*anyopaque, [*]const f32, usize) callconv(.c) Audio,
/// A loaded sound's samples: mono, at `Input.sample_rate`, alive as
/// long as the host. Empty for `.none`.
samples: *const fn (*anyopaque, Audio) []const f32,
@@ -696,6 +698,14 @@ pub const Host = struct {
pub fn decodeSound(h: Host, wav: []const u8) Audio {
return h.vtable.decodeSound(h.ctx, wav);
}
+ /// Mono samples at `Input.sample_rate`, copied into the host: a sound
+ /// for `Voice.audio` like a loaded one (synthesised one-shots become
+ /// voices, reconciled and replay-safe). Kept for the life of the host,
+ /// like every sound: make each once (in `setup`, or cached in Session).
+ /// `.none` if a sample is not finite.
+ pub fn newSound(h: Host, sound: []const f32) Audio {
+ return h.vtable.newSound(h.ctx, sound.ptr, sound.len);
+ }
pub fn samples(h: Host, audio: Audio) []const f32 {
return h.vtable.samples(h.ctx, audio);
}
diff --git a/src/host/Host.zig b/src/host/Host.zig
index 1a75b2a..000488b 100644
--- a/src/host/Host.zig
+++ b/src/host/Host.zig
@@ -1010,6 +1010,7 @@ const api: abi.Host.VTable = .{
.textSize = apiTextSize,
.loadSound = apiLoadSound,
.decodeSound = apiDecodeSound,
+ .newSound = apiNewSound,
.samples = apiSamples,
.readFile = apiReadFile,
.env = apiEnv,
@@ -1076,6 +1077,9 @@ fn apiLoadSound(ctx: *anyopaque, path: []const u8) abi.Audio {
fn apiDecodeSound(ctx: *anyopaque, wav: []const u8) abi.Audio {
return hostOf(ctx).audio.decode(wav);
}
+fn apiNewSound(ctx: *anyopaque, samples: [*]const f32, len: usize) callconv(.c) abi.Audio {
+ return hostOf(ctx).audio.new(samples[0..len]);
+}
fn apiReadFile(ctx: *anyopaque, path: []const u8) []const u8 {
const self = hostOf(ctx);
if (self.files.get(path)) |bytes| return bytes;
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));
diff --git a/src/host/script.zig b/src/host/script.zig
index 9e1dddf..a559d76 100644
--- a/src/host/script.zig
+++ b/src/host/script.zig
@@ -27,7 +27,9 @@
//! holds VALUE, read in its own layout
//! expect-log TEXT a recent console line contains TEXT
//! expect-no-log TEXT no recent console line does
-//! expect-audio KEY stream KEY has received samples
+//! expect-audio KEY stream KEY has received samples, or voice
+//! KEY sounded since the last check of it
+//! expect-no-audio KEY voice KEY did not sound since the last check
//! screenshot FILE save the next frame as PNG
//! expect-same-screenshot A B [MAX [X Y W H]]
//! two saved PNGs match, every channel within MAX
@@ -236,7 +238,10 @@ fn step(host: *Host, arena: std.mem.Allocator, command: []const u8, rest: []cons
return;
} else if (eq(u8, command, "expect-audio")) {
const key = try std.fmt.parseInt(u32, rest, 10);
- if (host.input.stream(key).pushed == 0) return error.ExpectationFailed;
+ if (host.input.stream(key).pushed == 0 and !host.audio.sounded(key)) return error.ExpectationFailed;
+ return;
+ } else if (eq(u8, command, "expect-no-audio")) {
+ if (host.audio.sounded(try std.fmt.parseInt(u32, rest, 10))) return error.ExpectationFailed;
return;
} else if (eq(u8, command, "screenshot")) {
if (std.fs.path.dirname(rest)) |dir| try std.Io.Dir.cwd().createDirPath(host.io, dir);
diff --git a/src/sketch/web.zig b/src/sketch/web.zig
index 6cf165a..817862c 100644
--- a/src/sketch/web.zig
+++ b/src/sketch/web.zig
@@ -411,6 +411,7 @@ fn Driver(comptime App: type) type {
.textSize = textSize,
.loadSound = loadSound,
.decodeSound = unsupportedSound,
+ .newSound = newSound,
.samples = samples,
.readFile = readFile,
.env = env,
@@ -460,6 +461,16 @@ fn Driver(comptime App: type) type {
fn unsupportedSound(_: *anyopaque, _: []const u8) abi.Audio {
return .none;
}
+ /// Kept, unnamed, for `samples` (voices are silent on the web).
+ fn newSound(_: *anyopaque, ptr: [*]const f32, len: usize) callconv(.c) abi.Audio {
+ for (ptr[0..len]) |v| if (!std.math.isFinite(v)) return .none;
+ const copy = gpa.dupe(f32, ptr[0..len]) catch return .none;
+ sounds.append(gpa, .{ .name = "", .samples = copy }) catch {
+ gpa.free(copy);
+ return .none;
+ };
+ return @fromBackingInt(@intCast(sounds.items.len));
+ }
fn samples(_: *anyopaque, a: abi.Audio) []const f32 {
const i = @backingInt(a);
return if (i == 0 or i > sounds.items.len) &.{} else sounds.items[i - 1].samples;
diff --git a/test/voices.script b/test/voices.script
new file mode 100644
index 0000000..43283f7
--- /dev/null
+++ b/test/voices.script
@@ -0,0 +1,29 @@
+# A sound made with host.newSound is a voice like a loaded one: it sounds
+# once for its serial, a replay that keeps declaring that serial does not
+# sound it again, and a new serial does.
+frames 3
+expect-no-audio 1
+key space
+expect serial 1
+expect-audio 1
+frames 10
+expect-no-audio 1
+# Recorded while the blip is declared but finished, then played back,
+# stepped and seeked over: the same serial all along, so it stays silent.
+console record 0
+frames 20
+console stop
+console play 0
+frames 30
+expect-no-audio 1
+console seek start
+console step 5
+console seek end
+expect-no-audio 1
+console stop
+# A new serial starts it again.
+key space
+expect serial 2
+expect-audio 1
+frames 10
+expect-no-audio 1
diff --git a/test/voices.zig b/test/voices.zig
new file mode 100644
index 0000000..9932642
--- /dev/null
+++ b/test/voices.zig
@@ -0,0 +1,28 @@
+//! A sound the sketch made itself, played as a voice: space bumps the
+//! serial that starts it (test/voices.script).
+const sk = @import("sketch");
+
+pub const name = "voices";
+pub const width = 160;
+pub const height = 120;
+
+pub const State = struct { serial: u32 = 0 };
+pub const Session = struct { blip: sk.Audio = .none };
+
+pub fn setup(ss: *Session, init: *sk.Init) !void {
+ // Shorter than the host's lead (2048 samples), so the first frame that
+ // declares it mixes all of it.
+ var samples: [1000]f32 = undefined;
+ for (&samples, 0..) |*v, i| v.* = 0.5 * @sin(@as(f32, @floatFromInt(i)) * 0.06);
+ ss.blip = init.host.newSound(&samples);
+ if (ss.blip == .none) return error.NewSound;
+}
+
+pub fn update(s: *State, in: *const sk.Input) void {
+ if (in.pressed(sk.Key.space)) s.serial += 1;
+}
+
+pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void {
+ f.clear(0x202020);
+ if (s.serial != 0) f.voice(.{ .key = 1, .audio = ss.blip, .serial = s.serial });
+}