From c68a8c9d9efa746e8eaef0392a327888577a05e0 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 00:11:20 -0300 Subject: audio: a voice's rate (its pitch), and groups of voices under one gain Voice.rate is the voice's playback speed and so its pitch: 2 is an octave up and half as long, 0 holds it, and changing it does not restart the voice. The mixer keeps a fractional cursor and resamples by linear interpolation between a sample and the next (the last toward silence, or the first when looping): the least code that sounds right, a little dull far from 1; at rate 1 the samples come out exactly. An infinite or NaN rate ends the voice instead of crashing the conversion. Voice.group (0-7) puts a voice under a group's gain, which draw declares each frame with f.gain(group, value) (Command.gain); a group not given one plays at 1, so it is declared state and replays like the voices. The mixer glides each group toward its gain by 1/480 a sample (10 ms for a full swing), so a gain set in one step does not click. Music ducking under speech is then the sketch lowering one number; nothing ducks by itself. The web host ignores gains as it ignores voices. Test: half and double speed against the interpolated samples, rate 0 holding, an infinite rate ending; a looping voice in group 1 at gain 0.5 gliding there and staying while group 0's is untouched, and going back to 1 when the gain is no longer declared. test-audio also builds the voices sketch for the web, which compiles the web host's newSound and gain. The README lists rate, group and f.gain with what they do. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- src/sketch/root.zig | 4 ++++ src/sketch/web.zig | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) (limited to 'src/sketch') diff --git a/src/sketch/root.zig b/src/sketch/root.zig index 1270b5d..d15046d 100644 --- a/src/sketch/root.zig +++ b/src/sketch/root.zig @@ -212,6 +212,10 @@ pub const Frame = struct { pub fn voice(f: *Frame, v: Voice) void { f.emit(.{ .voice = v }); } + /// Scales every voice of `group` this frame (1 when not declared). + pub fn gain(f: *Frame, group: u3, value: f32) void { + f.emit(.{ .gain = .{ .group = group, .gain = value } }); + } pub fn audio(f: *Frame, key: u32, samples: []const f32, options: struct { clear: bool = false }) void { f.emit(.{ .audio = .{ .key = key, .samples = samples, .clear = options.clear } }); } diff --git a/src/sketch/web.zig b/src/sketch/web.zig index 817862c..62a173a 100644 --- a/src/sketch/web.zig +++ b/src/sketch/web.zig @@ -241,9 +241,9 @@ fn Driver(comptime App: type) type { }, .download => |v| js.download(@backingInt(v.buffer), v.offset, v.size, v.key), .open_file => |v| js.openFile(v.command.ptr, v.command.len, v.pattern.ptr, v.pattern.len), - // Synthesised voices are not on the web yet; quitting a page + // Voices (and so their groups) are not on the web yet; quitting a page // means nothing; `run` lines go after the frame. - .voice, .quit, .run, .backdrop => {}, + .voice, .gain, .quit, .run, .backdrop => {}, } } -- cgit v1.3