diff options
Diffstat (limited to 'src/host/audio.zig')
| -rw-r--r-- | src/host/audio.zig | 266 |
1 files changed, 250 insertions, 16 deletions
diff --git a/src/host/audio.zig b/src/host/audio.zig index aebb909..880be72 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,8 +23,24 @@ 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, + /// Samples the device takes from each stream at once. + pull: u32 = 1024, + /// The wall-clock time (ns) between the ends of the last frames' + /// commands, where pushes are done; and when the last ended. + intervals: [64]u64 = @splat(0), + frame: u64 = 0, + last_frame: u64 = 0, + /// Grown by underruns, shrinking back while there are none. + margin: u32 = 0, + /// A stream ran dry since the lead was last worked out. + dry: bool = false, + /// `Input.lead`: see `lead`. + lead: u32 = 4800, scope: Scope = .{}, /// The default recording device, bound to this stream while declared. microphone: ?*c.SDL_AudioStream = null, @@ -29,13 +48,38 @@ 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 Stream = struct { key: u32, out: *c.SDL_AudioStream, pushed: u64 = 0 }; + const Playing = struct { + voice: abi.Voice, + /// 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`). + sounded: bool = false, + }; + const Stream = struct { + key: u32, + out: *c.SDL_AudioStream, + pushed: u64 = 0, + /// Pushed to this frame, and the last. + fed: bool = false, + was_fed: bool = false, + underruns: u32 = 0, + }; - pub fn open(self: *Audio) !void { + /// The default playback device, taking `pull` samples at once if asked + /// (`abi.Info.audio_pull`): a hint, so the environment's wins. + pub fn open(self: *Audio, pull: u32) !void { + var hint: [16]u8 = undefined; + if (pull > 0) _ = c.SDL_SetHint(c.SDL_HINT_AUDIO_DEVICE_SAMPLE_FRAMES, try std.fmt.bufPrintSentinel(&hint, "{d}", .{pull}, 0)); const device = c.SDL_OpenAudioDevice(c.SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec); if (device == 0) return error.OpenAudio; self.device = device; + var format: c.SDL_AudioSpec = undefined; + var frames: c_int = 0; + if (c.SDL_GetAudioDeviceFormat(device, &format, &frames) and frames > 0 and format.freq > 0) { + self.pull = @intCast(@divFloor(@as(i64, frames) * sample_rate + format.freq - 1, format.freq)); + } self.voices_out = try self.bind(); self.scope.attach(device) catch {}; } @@ -81,6 +125,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); @@ -97,6 +147,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; } @@ -155,9 +206,18 @@ pub const Audio = struct { if (clear) { _ = c.SDL_ClearAudioStream(s.out); s.pushed = 0; + s.was_fed = false; } if (samples.len == 0) return; + // Fed last frame and empty now: the device came up short since + // (counted at the push, as a pull before it would hide it by the + // next frame). + if (s.was_fed and !s.fed and c.SDL_GetAudioStreamQueued(s.out) == 0) { + s.underruns += 1; + self.dry = true; + } if (c.SDL_PutAudioStreamData(s.out, samples.ptr, @intCast(samples.len * @sizeOf(f32)))) s.pushed += samples.len; + s.fed = true; } fn streamFor(self: *Audio, key: u32) ?*Stream { @@ -171,14 +231,22 @@ pub const Audio = struct { return &self.streams.items[self.streams.items.len - 1]; } - /// Drops undeclared voices, keeps the voice mix queued ahead, and opens - /// or closes the microphone. + /// Drops undeclared voices, works out the lead for the next frame, keeps + /// the voice mix queued ahead, and opens or closes the microphone. pub fn endFrame(self: *Audio) void { self.reconcileMicrophone(); var i: usize = 0; while (i < self.voices.items.len) { if (self.voices.items[i].seen) i += 1 else _ = self.voices.swapRemove(i); } + const now = c.SDL_GetTicksNS(); + if (self.last_frame != 0) { + self.intervals[self.frame % self.intervals.len] = now -| self.last_frame; + self.frame += 1; + } + self.last_frame = now; + self.lead = lead(self.intervals, self.pull, &self.margin, self.dry); + self.dry = false; const out = self.voices_out orelse return; const queued = c.SDL_GetAudioStreamQueued(out); if (queued < 0) return; @@ -189,45 +257,98 @@ 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; - // Bounded however many voices sound at once. - sample.* = sum / (1 + @abs(sum)); + sample.* = limit(sum); count += 1; } 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; + } + + /// The streams as a frame starts. pub fn streamStatus(self: *Audio) []const abi.Stream { self.status.clearRetainingCapacity(); - for (self.streams.items) |s| { - const queued = c.SDL_GetAudioStreamQueued(s.out); - self.status.append(self.gpa, .{ .key = s.key, .queued = @intCast(@divFloor(@max(queued, 0), @sizeOf(f32))), .pushed = s.pushed }) catch {}; + for (self.streams.items) |*s| { + const queued: u32 = @intCast(@divFloor(@max(c.SDL_GetAudioStreamQueued(s.out), 0), @sizeOf(f32))); + s.was_fed = s.fed; + s.fed = false; + self.status.append(self.gpa, .{ .key = s.key, .queued = queued, .pushed = s.pushed, .underruns = s.underruns }) catch {}; } return self.status.items; } }; +/// How many samples to keep queued: the slowest of the last frames' worth +/// (pushes come once a frame, `intervals` apart), plus the device's pull +/// (it takes that much at once), plus `margin`: half a pull more at each +/// underrun (`dry`), back down by a sample a frame. +fn lead(intervals: [64]u64, pull: u32, margin: *u32, dry: bool) u32 { + var slowest: u64 = 0; + for (intervals) |ns| slowest = @max(slowest, ns); + margin.* = if (dry) @min(margin.* + pull / 2, max_lead) else margin.* -| 1; + const frame: u32 = @intCast(@min(slowest * sample_rate / std.time.ns_per_s, 1 << 20)); + return @min(frame + pull + margin.*, max_lead); +} +/// 170 ms: a frame slower than that is a stall, not a pace. +const max_lead = 8192; + +/// Where the voice mix stops being passed through: -2.5 dBFS. Quieter +/// mixes keep their true volume to the bit; a clip normalised to full scale +/// only has its peaks bent (1 comes out at 0.875). +pub const knee = 0.75; + +/// The voice mix, bounded however many voices sound at once: unity gain up +/// to the knee, then `knee + (1 - knee)·u/(1 + u)` with `u` how far past the +/// knee in units of the room left, which meets the line at the knee with +/// its slope (1) and tends to 1. (With a knee of 0 it is the old +/// `x/(1 + |x|)`.) Written `1 - 1/(1 + u)` so that every step rounds +/// monotonically and infinity comes out at 1. No state, no lookahead. +pub fn limit(x: f32) f32 { + const a = @abs(x); + if (a <= knee) return x; + const u = (a - knee) / (1 - knee); + return std.math.copysign(knee + (1 - knee) * (1 - 1 / (1 + u)), x); +} + /// WAV, FLAC or Ogg Vorbis, by content (see `vendor/codecs.h`). fn decodeBytes(gpa: std.mem.Allocator, bytes: []const u8) ![]f32 { var channels: u32 = 0; @@ -325,3 +446,116 @@ test "voices reconcile: one-shots finish and stay silent, new serials restart, u try std.testing.expectEqual(@as(usize, 1), audio.voices.items.len); 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)); + var bits: u32 = 0; + while (bits <= knee_bits) : (bits += 997) { + const x: f32 = @bitCast(bits); + try std.testing.expectEqual(@as(u32, @bitCast(x)), @as(u32, @bitCast(limit(x)))); + try std.testing.expectEqual(@as(u32, @bitCast(-x)), @as(u32, @bitCast(limit(-x)))); + } + try std.testing.expectEqual(@as(f32, knee), limit(knee)); + // Rising and at most 1, from the knee's neighbourhood (every float) to + // infinity (every 101st), and odd. + var last = limit(@bitCast(knee_bits - 100_000)); + bits = knee_bits - 100_000; + const inf_bits: u32 = @bitCast(std.math.inf(f32)); + while (bits <= inf_bits) : (bits += if (bits < knee_bits + 100_000) 1 else 101) { + const x: f32 = @bitCast(bits); + const y = limit(x); + try std.testing.expect(y >= last and y <= 1); + try std.testing.expectEqual(-y, limit(-x)); + last = y; + } + try std.testing.expectEqual(@as(f32, 1), limit(std.math.inf(f32))); + // Slope 1 on both sides of the knee, and what a full-scale peak becomes. + const h = 1e-3; + 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]); +} + +test "the lead the mixer gives streams outlasts the slowest recent frame and the device's pull, more after an underrun" { + const expectEqual = std.testing.expectEqual; + var intervals: [64]u64 = @splat(20 * std.time.ns_per_ms); + var margin: u32 = 0; + // 50 Hz and a pull of 1024: a frame's 960 samples and the pull. + try expectEqual(@as(u32, 960 + 1024), lead(intervals, 1024, &margin, false)); + // One 50 ms frame in the window sets it. + intervals[5] = 50 * std.time.ns_per_ms; + try expectEqual(@as(u32, 2400 + 1024), lead(intervals, 1024, &margin, false)); + // An underrun adds half a pull, which goes a sample a frame. + try expectEqual(@as(u32, 2400 + 1024 + 512), lead(intervals, 1024, &margin, true)); + try expectEqual(@as(u32, 2400 + 1024 + 511), lead(intervals, 1024, &margin, false)); + // A stall is not a pace: the lead stops at 170 ms. + intervals[6] = std.time.ns_per_s; + try expectEqual(@as(u32, max_lead), lead(intervals, 1024, &margin, false)); +} + +test "the mixer's device takes at once what the sketch asks, SDL's own when it asks nothing, and the environment's over both" { + _ = c.SDL_SetHintWithPriority(c.SDL_HINT_AUDIO_DRIVER, "dummy", c.SDL_HINT_OVERRIDE); + try std.testing.expect(c.SDL_Init(c.SDL_INIT_AUDIO)); + defer c.SDL_Quit(); + defer _ = c.SDL_ResetHint(c.SDL_HINT_AUDIO_DEVICE_SAMPLE_FRAMES); + for ([_][3]u32{ .{ 0, 0, 1024 }, .{ 256, 0, 256 }, .{ 256, 512, 512 } }) |case| { + // SDL's copy of the environment, not the process's. + if (case[1] > 0) try std.testing.expect(c.SDL_SetEnvironmentVariable(c.SDL_GetEnvironment(), c.SDL_HINT_AUDIO_DEVICE_SAMPLE_FRAMES, "512", true)); + defer _ = c.SDL_UnsetEnvironmentVariable(c.SDL_GetEnvironment(), c.SDL_HINT_AUDIO_DEVICE_SAMPLE_FRAMES); + var a: Audio = .{ .gpa = std.testing.allocator }; + try a.open(case[0]); + defer a.deinit(); + try std.testing.expectEqual(case[2], a.pull); + } +} |
