summaryrefslogtreecommitdiff
path: root/src/host/audio.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/host/audio.zig')
-rw-r--r--src/host/audio.zig36
1 files changed, 35 insertions, 1 deletions
diff --git a/src/host/audio.zig b/src/host/audio.zig
index d9f258a..31ac38a 100644
--- a/src/host/audio.zig
+++ b/src/host/audio.zig
@@ -29,7 +29,14 @@ 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 Playing = struct {
+ voice: abi.Voice,
+ cursor: usize = 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 };
pub fn open(self: *Audio) !void {
@@ -81,6 +88,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);
@@ -203,6 +216,7 @@ pub const Audio = struct {
continue;
}
alive = true;
+ playing.sounded = true;
const v = playing.voice;
const edge = @min(playing.cursor + 1, samples.len - playing.cursor);
const fade: f32 = if (v.fade == 0) 1 else @as(f32, @floatFromInt(@min(edge, v.fade))) / @as(f32, @floatFromInt(v.fade));
@@ -217,6 +231,15 @@ pub const Audio = struct {
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;
+ }
+
pub fn streamStatus(self: *Audio) []const abi.Stream {
self.status.clearRetainingCapacity();
for (self.streams.items) |s| {
@@ -343,6 +366,17 @@ test "voices reconcile: one-shots finish and stay silent, new serials restart, u
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));