summaryrefslogtreecommitdiff
path: root/test
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 /test
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
Diffstat (limited to 'test')
-rw-r--r--test/voices.script29
-rw-r--r--test/voices.zig28
2 files changed, 57 insertions, 0 deletions
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 });
+}