summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/host/audio.zig50
1 files changed, 48 insertions, 2 deletions
diff --git a/src/host/audio.zig b/src/host/audio.zig
index aebb909..d9f258a 100644
--- a/src/host/audio.zig
+++ b/src/host/audio.zig
@@ -211,8 +211,7 @@ pub const Audio = struct {
if (playing.cursor == samples.len and v.loop) playing.cursor = 0;
}
if (!alive) break;
- // Bounded however many voices sound at once.
- sample.* = sum / (1 + @abs(sum));
+ sample.* = limit(sum);
count += 1;
}
return count;
@@ -228,6 +227,24 @@ pub const Audio = struct {
}
};
+/// 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 +342,32 @@ 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 "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));
+}