summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/abi.zig10
-rw-r--r--src/host/Host.zig4
-rw-r--r--src/host/audio.zig36
-rw-r--r--src/host/script.zig9
-rw-r--r--src/sketch/web.zig11
5 files changed, 67 insertions, 3 deletions
diff --git a/src/abi.zig b/src/abi.zig
index f0abdb3..3c47e69 100644
--- a/src/abi.zig
+++ b/src/abi.zig
@@ -632,6 +632,8 @@ pub const Host = struct {
/// WAV, FLAC or Ogg Vorbis bytes in memory. Not cached. `.none` if
/// they do not decode.
decodeSound: *const fn (*anyopaque, []const u8) Audio,
+ /// Samples a sketch made, copied: see `Host.newSound`.
+ newSound: *const fn (*anyopaque, [*]const f32, usize) callconv(.c) Audio,
/// A loaded sound's samples: mono, at `Input.sample_rate`, alive as
/// long as the host. Empty for `.none`.
samples: *const fn (*anyopaque, Audio) []const f32,
@@ -696,6 +698,14 @@ pub const Host = struct {
pub fn decodeSound(h: Host, wav: []const u8) Audio {
return h.vtable.decodeSound(h.ctx, wav);
}
+ /// Mono samples at `Input.sample_rate`, copied into the host: a sound
+ /// for `Voice.audio` like a loaded one (synthesised one-shots become
+ /// voices, reconciled and replay-safe). Kept for the life of the host,
+ /// like every sound: make each once (in `setup`, or cached in Session).
+ /// `.none` if a sample is not finite.
+ pub fn newSound(h: Host, sound: []const f32) Audio {
+ return h.vtable.newSound(h.ctx, sound.ptr, sound.len);
+ }
pub fn samples(h: Host, audio: Audio) []const f32 {
return h.vtable.samples(h.ctx, audio);
}
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);
diff --git a/src/sketch/web.zig b/src/sketch/web.zig
index 6cf165a..817862c 100644
--- a/src/sketch/web.zig
+++ b/src/sketch/web.zig
@@ -411,6 +411,7 @@ fn Driver(comptime App: type) type {
.textSize = textSize,
.loadSound = loadSound,
.decodeSound = unsupportedSound,
+ .newSound = newSound,
.samples = samples,
.readFile = readFile,
.env = env,
@@ -460,6 +461,16 @@ fn Driver(comptime App: type) type {
fn unsupportedSound(_: *anyopaque, _: []const u8) abi.Audio {
return .none;
}
+ /// Kept, unnamed, for `samples` (voices are silent on the web).
+ fn newSound(_: *anyopaque, ptr: [*]const f32, len: usize) callconv(.c) abi.Audio {
+ for (ptr[0..len]) |v| if (!std.math.isFinite(v)) return .none;
+ const copy = gpa.dupe(f32, ptr[0..len]) catch return .none;
+ sounds.append(gpa, .{ .name = "", .samples = copy }) catch {
+ gpa.free(copy);
+ return .none;
+ };
+ return @fromBackingInt(@intCast(sounds.items.len));
+ }
fn samples(_: *anyopaque, a: abi.Audio) []const f32 {
const i = @backingInt(a);
return if (i == 0 or i > sounds.items.len) &.{} else sounds.items[i - 1].samples;