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/sketch/web.zig | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/sketch/web.zig') 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; -- cgit v1.3