summaryrefslogtreecommitdiff
path: root/src/host
diff options
context:
space:
mode:
Diffstat (limited to 'src/host')
-rw-r--r--src/host/Host.zig4
-rw-r--r--src/host/audio.zig36
-rw-r--r--src/host/script.zig9
3 files changed, 46 insertions, 3 deletions
diff --git a/src/host/Host.zig b/src/host/Host.zig
index 1a75b2a..000488b 100644
--- a/src/host/Host.zig
+++ b/src/host/Host.zig
@@ -1010,6 +1010,7 @@ const api: abi.Host.VTable = .{
.textSize = apiTextSize,
.loadSound = apiLoadSound,
.decodeSound = apiDecodeSound,
+ .newSound = apiNewSound,
.samples = apiSamples,
.readFile = apiReadFile,
.env = apiEnv,
@@ -1076,6 +1077,9 @@ fn apiLoadSound(ctx: *anyopaque, path: []const u8) abi.Audio {
fn apiDecodeSound(ctx: *anyopaque, wav: []const u8) abi.Audio {
return hostOf(ctx).audio.decode(wav);
}
+fn apiNewSound(ctx: *anyopaque, samples: [*]const f32, len: usize) callconv(.c) abi.Audio {
+ return hostOf(ctx).audio.new(samples[0..len]);
+}
fn apiReadFile(ctx: *anyopaque, path: []const u8) []const u8 {
const self = hostOf(ctx);
if (self.files.get(path)) |bytes| return bytes;
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));
diff --git a/src/host/script.zig b/src/host/script.zig
index 9e1dddf..a559d76 100644
--- a/src/host/script.zig
+++ b/src/host/script.zig
@@ -27,7 +27,9 @@
//! holds VALUE, read in its own layout
//! expect-log TEXT a recent console line contains TEXT
//! expect-no-log TEXT no recent console line does
-//! expect-audio KEY stream KEY has received samples
+//! expect-audio KEY stream KEY has received samples, or voice
+//! KEY sounded since the last check of it
+//! expect-no-audio KEY voice KEY did not sound since the last check
//! screenshot FILE save the next frame as PNG
//! expect-same-screenshot A B [MAX [X Y W H]]
//! two saved PNGs match, every channel within MAX
@@ -236,7 +238,10 @@ fn step(host: *Host, arena: std.mem.Allocator, command: []const u8, rest: []cons
return;
} else if (eq(u8, command, "expect-audio")) {
const key = try std.fmt.parseInt(u32, rest, 10);
- if (host.input.stream(key).pushed == 0) return error.ExpectationFailed;
+ if (host.input.stream(key).pushed == 0 and !host.audio.sounded(key)) return error.ExpectationFailed;
+ return;
+ } else if (eq(u8, command, "expect-no-audio")) {
+ if (host.audio.sounded(try std.fmt.parseInt(u32, rest, 10))) return error.ExpectationFailed;
return;
} else if (eq(u8, command, "screenshot")) {
if (std.fs.path.dirname(rest)) |dir| try std.Io.Dir.cwd().createDirPath(host.io, dir);