summaryrefslogtreecommitdiff
path: root/src/host/audio.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 00:11:20 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commitc68a8c9d9efa746e8eaef0392a327888577a05e0 (patch)
treedb903d320e3f4701845af5f07a4ffc5dc9d80ff8 /src/host/audio.zig
parent896855648c3371ede3c672bbfa75175fa3a1e42e (diff)
downloadsketchbook-c68a8c9d9efa746e8eaef0392a327888577a05e0.tar.gz
sketchbook-c68a8c9d9efa746e8eaef0392a327888577a05e0.zip
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 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'src/host/audio.zig')
-rw-r--r--src/host/audio.zig71
1 files changed, 64 insertions, 7 deletions
diff --git a/src/host/audio.zig b/src/host/audio.zig
index 31ac38a..bf5fdc4 100644
--- a/src/host/audio.zig
+++ b/src/host/audio.zig
@@ -11,6 +11,9 @@ pub const sample_rate = 48_000;
const spec: c.SDL_AudioSpec = .{ .format = c.SDL_AUDIO_F32, .channels = 1, .freq = sample_rate };
/// A device block plus the next frame; 512 samples can underrun at 60 Hz.
const queue_samples = 2048;
+/// How far a group's gain moves a sample toward a new one: a full swing
+/// takes 10 ms, so a gain set in a step does not click.
+const gain_step = 1.0 / 480.0;
pub const Audio = struct {
gpa: std.mem.Allocator,
@@ -20,6 +23,9 @@ pub const Audio = struct {
clips: std.ArrayList([]f32) = .empty,
paths: std.StringHashMapUnmanaged(abi.Audio) = .empty,
voices: std.ArrayList(Playing) = .empty,
+ /// Each group's gain as declared this frame, and as the mix has got to.
+ gains: [8]f32 = @splat(1),
+ heard_gains: [8]f32 = @splat(1),
streams: std.ArrayList(Stream) = .empty,
status: std.ArrayList(abi.Stream) = .empty,
scope: Scope = .{},
@@ -31,7 +37,8 @@ pub const Audio = struct {
const Playing = struct {
voice: abi.Voice,
- cursor: usize = 0,
+ /// Where in the sound, in its samples: fractional when `rate` is.
+ cursor: f64 = 0,
done: bool = false,
seen: bool = true,
/// Mixed since a script last asked (`expect-audio`).
@@ -110,6 +117,7 @@ pub const Audio = struct {
pub fn beginFrame(self: *Audio) void {
for (self.voices.items) |*v| v.seen = false;
+ self.gains = @splat(1);
self.listening = false;
}
@@ -202,27 +210,36 @@ pub const Audio = struct {
}
/// Mixes live voices into `output`; returns how many samples were written,
- /// stopping early once every voice has finished.
+ /// stopping early once every voice has finished. A voice at another
+ /// rate is resampled by linear interpolation between its neighbours
+ /// (at rate 1 it is its samples, exactly).
pub fn mix(self: *Audio, output: []f32) usize {
var count: usize = 0;
for (output) |*sample| {
var sum: f32 = 0;
var alive = false;
+ for (&self.heard_gains, self.gains) |*heard, gain| heard.* += std.math.clamp(gain - heard.*, -gain_step, gain_step);
for (self.voices.items) |*playing| {
if (playing.done) continue;
const samples = self.samplesOf(playing.voice.audio);
- if (playing.cursor >= samples.len) {
+ const len: f64 = @floatFromInt(samples.len);
+ // Also ends a cursor an infinite or NaN rate made.
+ if (!(playing.cursor < len)) {
playing.done = true;
continue;
}
alive = true;
playing.sounded = true;
const v = playing.voice;
- const edge = @min(playing.cursor + 1, samples.len - playing.cursor);
+ const i: usize = @intFromFloat(playing.cursor);
+ const t: f32 = @floatCast(playing.cursor - @floor(playing.cursor));
+ const next = if (i + 1 < samples.len) samples[i + 1] else if (v.loop) samples[0] else 0;
+ const at = if (t == 0) samples[i] else samples[i] + (next - samples[i]) * t;
+ const edge = @min(i + 1, samples.len - i);
const fade: f32 = if (v.fade == 0) 1 else @as(f32, @floatFromInt(@min(edge, v.fade))) / @as(f32, @floatFromInt(v.fade));
- sum += samples[playing.cursor] * v.volume * fade;
- playing.cursor += 1;
- if (playing.cursor == samples.len and v.loop) playing.cursor = 0;
+ sum += at * v.volume * self.heard_gains[v.group] * fade;
+ playing.cursor += @max(v.rate, 0);
+ if (playing.cursor >= len and v.loop) playing.cursor = @mod(playing.cursor, len);
}
if (!alive) break;
sample.* = limit(sum);
@@ -405,3 +422,43 @@ test "the mixer passes the mix through below the knee and limits it to 1 above,
try std.testing.expectApproxEqAbs(@as(f32, 1), (limit(knee + h) - limit(knee)) / h, 0.01);
try std.testing.expectEqual(@as(f32, 0.875), limit(1));
}
+
+test "voices at another rate are resampled linearly, and a group's gain scales its voices smoothly" {
+ var audio: Audio = .{ .gpa = std.testing.allocator };
+ defer audio.deinit();
+ const clip = audio.new(&.{ 0, 0.1, 0.2, 0.3 });
+ var out: [1024]f32 = undefined;
+ // Half speed: twice as long, the halves interpolated (the last toward
+ // silence); double speed: every other sample.
+ audio.declare(.{ .key = 1, .audio = clip, .rate = 0.5 });
+ try std.testing.expectEqual(@as(usize, 8), audio.mix(&out));
+ for ([_]f32{ 0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.15 }, out[0..8]) |want, got| try std.testing.expectApproxEqAbs(want, got, 1e-6);
+ audio.declare(.{ .key = 1, .audio = clip, .rate = 2, .serial = 1 });
+ try std.testing.expectEqual(@as(usize, 2), audio.mix(&out));
+ try std.testing.expectEqualSlices(f32, &.{ 0, 0.2 }, out[0..2]);
+ // Rate 0 holds it; an infinite rate ends it.
+ audio.declare(.{ .key = 1, .audio = clip, .rate = 0, .serial = 2 });
+ try std.testing.expectEqual(@as(usize, 3), audio.mix(out[0..3]));
+ audio.declare(.{ .key = 1, .audio = clip, .rate = std.math.inf(f32), .serial = 3 });
+ try std.testing.expectEqual(@as(usize, 1), audio.mix(&out));
+
+ // A looping voice in group 1 and one in group 0, at 0.25 each: group 1
+ // set to 0.5 glides there in 10 ms and stays; group 0 is untouched.
+ const level = audio.new(&.{0.25});
+ audio.beginFrame();
+ audio.declare(.{ .key = 1, .audio = level, .loop = true, .group = 1 });
+ audio.declare(.{ .key = 2, .audio = level, .loop = true });
+ audio.gains[1] = 0.5;
+ try std.testing.expectEqual(out.len, audio.mix(&out));
+ try std.testing.expect(out[0] < 0.5 and out[0] > 0.49);
+ try std.testing.expect(out[100] > out[200] and out[200] > out[400]);
+ try std.testing.expectEqual(@as(f32, 0.375), out[480]);
+ try std.testing.expectEqual(@as(f32, 0.375), out[1023]);
+ // Undeclared next frame: back to 1, as smoothly.
+ audio.beginFrame();
+ audio.declare(.{ .key = 1, .audio = level, .loop = true, .group = 1 });
+ audio.declare(.{ .key = 2, .audio = level, .loop = true });
+ try std.testing.expectEqual(out.len, audio.mix(&out));
+ try std.testing.expect(out[0] > 0.375 and out[0] < 0.38);
+ try std.testing.expectEqual(@as(f32, 0.5), out[1023]);
+}