summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/live/build.zig1
-rw-r--r--examples/live/shaders/synth.comp4
-rw-r--r--examples/live/src/Lang.zig12
-rw-r--r--examples/live/src/main.zig108
-rw-r--r--examples/live/src/probe.zig12
-rw-r--r--examples/live/test/live.script10
6 files changed, 124 insertions, 23 deletions
diff --git a/examples/live/build.zig b/examples/live/build.zig
index 2f76306..9d0983d 100644
--- a/examples/live/build.zig
+++ b/examples/live/build.zig
@@ -27,6 +27,7 @@ pub fn build(b: *std.Build) void {
const imports: []const std.Build.Module.Import = &.{
.{ .name = "Lang", .module = lang },
.{ .name = "shaders", .module = shaders.module },
+ .{ .name = "scope", .module = sb.modules.scope },
};
const app = sb.app(.{ .name = "live", .root_source_file = b.path("src/main.zig"), .imports = imports });
diff --git a/examples/live/shaders/synth.comp b/examples/live/shaders/synth.comp
index 9a066a6..8ae48bb 100644
--- a/examples/live/shaders/synth.comp
+++ b/examples/live/shaders/synth.comp
@@ -6,7 +6,9 @@
layout(local_size_x = 64) in;
#include "Synth.glsl"
-#define TAPE(i) u.tape[i]
+// The new tape, then the old one.
+layout(set = 0, binding = 0) readonly buffer Tapes { uvec4 tape[]; };
+#define TAPE(i) tape[i]
#include "vm.glsl"
layout(set = 1, binding = 0) buffer Block { float block[]; };
diff --git a/examples/live/src/Lang.zig b/examples/live/src/Lang.zig
index a6db8b9..4f3c3bf 100644
--- a/examples/live/src/Lang.zig
+++ b/examples/live/src/Lang.zig
@@ -135,7 +135,9 @@ pub const Instruction = extern struct {
pub const Tape = struct {
len: u32 = 0,
- code: [max_tape]Instruction = @splat(.{}),
+ code: Code = @splat(.{}),
+
+ pub const Code = [max_tape]Instruction;
pub fn slice(tape: *const Tape) []const Instruction {
return tape.code[0..@min(tape.len, max_tape)];
@@ -472,11 +474,11 @@ pub const Picture = extern struct {
pub const glsl = .{ .uniform = .{ .set = 3, .instance = "u" } };
};
-/// synth.comp's uniforms (SDL_GPU's compute set): the tape, and the one
-/// it replaces, which the first `fade` samples cross from.
+/// synth.comp's uniforms (SDL_GPU's compute set). Its tapes, the new one
+/// and the one it replaces, which the first `fade` samples cross from, are
+/// a storage buffer: SDL_GPU's Vulkan backend binds a uniform block 4 KiB
+/// at a time, and two tapes are 4 KiB alone.
pub const Synth = extern struct {
- /// The new tape, then the old one.
- tape: [2 * max_tape][4]u32,
len: u32,
old_len: u32,
/// Samples in the block.
diff --git a/examples/live/src/main.zig b/examples/live/src/main.zig
index 02a9c6d..13f89be 100644
--- a/examples/live/src/main.zig
+++ b/examples/live/src/main.zig
@@ -1,23 +1,35 @@
-//! live: a program typed over its own picture. Each line is `name =
-//! expression` over t, x and y (src/Lang.zig); the `color` line is what is
-//! seen. At every keystroke that leaves a program that parses, `update`
-//! compiles it into a tape kept in State, which one interpreter on the GPU
-//! (shaders/vm.glsl) runs per pixel. While a line is broken the last good
-//! tape keeps playing and the problem is marked at its byte.
+//! live: a program typed over its own picture and heard. Each line is
+//! `name = expression` over t, x and y (src/Lang.zig); the `out` line is
+//! what is heard, the `color` line what is seen. At every keystroke that
+//! leaves a program that parses, `update` compiles it into two tapes kept
+//! in State, which one interpreter on the GPU (shaders/vm.glsl) runs per
+//! pixel and per sample. While a line is broken the last good tapes keep
+//! playing and the problem is marked at its byte.
//!
//! State is the text, the tapes, the problem and the time, all changed by
//! `update` from recorded keys and ticks, so a replay or a seek brings back
-//! the text, the tape and the picture of that moment. Session holds the
-//! GPU objects.
+//! the text, the tape and the picture of that moment. The sound runs on a
+//! sample clock in Session, set from State's time at every jump (a seek,
+//! a replay's start) and free-running otherwise, so a paused replay keeps
+//! sounding its moment. Session holds it, the GPU objects and the scope.
const std = @import("std");
const sk = @import("sketch");
const gpu = sk.abi.gpu;
const Lang = @import("Lang");
const shaders = @import("shaders");
+const scope = @import("scope");
pub const name = "live";
pub const width = 1280;
pub const height = 720;
+/// A change is heard about 6.5 ms after `draw`, not 19-27 (README, sound).
+pub const audio_pull = 256;
+pub const commands = .{"capture"};
+
+/// The sound's stream, and its readback's key.
+const stream = 1;
+/// The most samples synthesised in one frame.
+const max_block = 8192;
pub const State = struct {
// ponytail: a plain buffer, typed characters appended and backspace
@@ -31,13 +43,35 @@ pub const State = struct {
problem: Lang.Problem = .{},
/// Seconds, from recorded ticks: a replay draws the same frames.
time: f32 = 0,
+ /// A new out tape crosses from the old one over a block; `set
+ /// crossfade false` hears (and measures) the click it saves.
+ crossfade: bool = true,
};
pub const Session = struct {
picture: sk.Image = .none,
size: [2]u32 = .{ 0, 0 },
+ block: sk.Buffer = .none,
+ /// The out tape and the one it replaces, for synth.comp.
+ tapes: sk.Buffer = .none,
+ /// Seconds: where the next block starts.
+ clock: f64 = 0,
+ /// The tape the last block played.
+ heard: Lang.Tape = .{},
+ scope: scope.Scope = .{},
+ /// `capture` appends what is heard to `<stem>.capture.f32`.
+ capturing: bool = false,
};
+/// `capture` starts or stops appending what is heard to the sketch's file
+/// `capture.f32` (mono f32 at the device's rate, beside its save).
+pub fn command(ss: *Session, c: *sk.Console, line: []const u8) !bool {
+ if (!std.mem.eql(u8, std.mem.trim(u8, line, " \t"), "capture")) return false;
+ ss.capturing = !ss.capturing;
+ c.log(if (ss.capturing) "capturing what is heard to capture.f32" else "capture stopped");
+ return true;
+}
+
pub fn update(s: *State, in: *const sk.Input) void {
var edited = false;
for (in.events) |e| switch (e.kind) {
@@ -76,6 +110,7 @@ pub fn update(s: *State, in: *const sk.Input) void {
}
}
+const synth: gpu.Kernel = .{ .code = shaders.synth_comp, .readonly_buffers = 1, .readwrite_buffers = 1, .uniform_buffers = 1 };
const picture_pipeline: gpu.Pipeline = .{
.vertex = .{ .code = shaders.fullscreen_vert },
.fragment = .{ .code = shaders.picture_frag, .uniform_buffers = 1 },
@@ -113,9 +148,63 @@ pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void {
}) }},
});
f.image(ss.picture, .{ .x = 0, .y = 0, .w = width, .h = height }, .{});
+ sound(s, ss, f);
code(s, f);
}
+/// The out tape over the sample clock, as much as keeps the host's lead
+/// queued, streamed from the GPU's block; and read back for the scope.
+fn sound(s: *const State, ss: *Session, f: *sk.Frame) void {
+ const in = f.in;
+ for (in.downloads) |d| if (d.key == stream) {
+ const samples = f.scratch.alloc(f32, d.bytes.len / 4) catch continue;
+ @memcpy(std.mem.sliceAsBytes(samples), d.bytes[0 .. samples.len * 4]);
+ ss.scope.push(samples);
+ if (ss.capturing) _ = f.host.appendFile("capture.f32", d.bytes);
+ };
+ if (ss.block == .none) {
+ ss.block = f.host.newBuffer(max_block * @sizeOf(f32));
+ ss.tapes = f.host.newBuffer(2 * @sizeOf(Lang.Tape.Code));
+ }
+ // A jump is heard at once: what was queued from before it is dropped.
+ if (in.jumped) {
+ ss.clock = s.time;
+ f.audio(stream, &.{}, .{ .clear = true });
+ }
+ const rate: f64 = @floatFromInt(in.sample_rate);
+ const want: u32 = @min(in.lead -| in.stream(stream).queued, max_block);
+ if (want > 0) {
+ const new = std.mem.sliceAsBytes(s.out.slice());
+ const changed = !std.mem.eql(u8, new, std.mem.sliceAsBytes(ss.heard.slice()));
+ const tapes = f.scratch.create([2]Lang.Tape.Code) catch return;
+ tapes.* = .{ s.out.code, ss.heard.code };
+ f.upload(ss.tapes, 0, std.mem.asBytes(tapes));
+ f.compute(.{
+ .label = "synth",
+ .kernel = synth,
+ .readonly = &.{ss.tapes},
+ .readwrite = &.{ss.block},
+ .uniforms = gpu.uniform(f.scratch, Lang.Synth{
+ .len = s.out.len,
+ .old_len = ss.heard.len,
+ .count = want,
+ .fade = if (changed and s.crossfade) want else 0,
+ .start = @floatCast(ss.clock),
+ .rate = @floatCast(rate),
+ }),
+ .groups = .{ (want + 63) / 64, 1, 1 },
+ });
+ f.audioBuffer(stream, ss.block, .{ .count = want });
+ f.download(ss.block, .{ .size = want * @sizeOf(f32), .key = stream });
+ ss.clock += @as(f64, @floatFromInt(want)) / rate;
+ ss.heard = s.out;
+ }
+ ss.scope.draw(f, .{
+ .oscilloscope = .{ .rect = .{ .x = 16, .y = height - 104, .w = 480, .h = 88 }, .samples = 1024 },
+ .spectrum = .{ .x = 512, .y = height - 104, .w = 480, .h = 88 },
+ });
+}
+
const scale = 1.25;
const margin = 16;
const ink = 0xe8e8e8;
@@ -159,5 +248,6 @@ fn code(s: *const State, f: *sk.Frame) void {
.playing => "replay",
.paused => "paused",
};
- f.print(width - 200, height - 24, 1, ink, "{s} {d:.1} s", .{ mode, s.time });
+ const lead = @as(f32, @floatFromInt(f.in.lead)) / @as(f32, @floatFromInt(f.in.sample_rate)) * 1000;
+ f.print(width - 260, 12, 1, ink, "{s} {d:.1} s lead {d:.1} ms", .{ mode, s.time, lead });
}
diff --git a/examples/live/src/probe.zig b/examples/live/src/probe.zig
index 5f6e8ee..1e2970c 100644
--- a/examples/live/src/probe.zig
+++ b/examples/live/src/probe.zig
@@ -17,6 +17,7 @@ pub const height = 360;
pub const Session = struct {
images: [3]sk.Image = .{ .none, .none, .none },
block: sk.Buffer = .none,
+ tapes: sk.Buffer = .none,
time: f32 = 0,
};
@@ -80,7 +81,7 @@ const pipeline: gpu.Pipeline = .{
.attributes = &.{},
.blend = .none,
};
-const kernel: gpu.Kernel = .{ .code = shaders.synth_comp, .readwrite_buffers = 1, .uniform_buffers = 1 };
+const kernel: gpu.Kernel = .{ .code = shaders.synth_comp, .readonly_buffers = 1, .readwrite_buffers = 1, .uniform_buffers = 1 };
pub fn draw(ss: *Session, f: *sk.Frame) void {
f.clear(0x000000);
@@ -88,6 +89,7 @@ pub fn draw(ss: *Session, f: *sk.Frame) void {
if (ss.block == .none) {
for (&ss.images, sizes) |*image, size| image.* = f.host.newImage(size[0], size[1]);
ss.block = f.host.newBuffer(1024 * @sizeOf(f32));
+ ss.tapes = f.host.newBuffer(2 * Lang.max_tape * 16);
}
ss.time += @floatCast(@as(f64, @floatFromInt(f.in.dt)) / std.time.ns_per_s);
for (pictures) |p| {
@@ -107,15 +109,15 @@ pub fn draw(ss: *Session, f: *sk.Frame) void {
});
}
for (blocks) |k| {
- var both: [2 * Lang.max_tape][4]u32 = undefined;
- both[0..Lang.max_tape].* = tape(k.len);
- both[Lang.max_tape..].* = tape(k.len);
+ const both = f.scratch.create([2][Lang.max_tape][4]u32) catch return;
+ both.* = .{ tape(k.len), tape(k.len) };
+ f.upload(ss.tapes, 0, std.mem.asBytes(both));
f.compute(.{
.label = k.label,
.kernel = kernel,
+ .readonly = &.{ss.tapes},
.readwrite = &.{ss.block},
.uniforms = gpu.uniform(f.scratch, Lang.Synth{
- .tape = both,
.len = k.len,
.old_len = k.len,
.count = k.count,
diff --git a/examples/live/test/live.script b/examples/live/test/live.script
index 386ab33..22085bf 100644
--- a/examples/live/test/live.script
+++ b/examples/live/test/live.script
@@ -1,11 +1,15 @@
# A program typed, its tapes after each line, a broken line and its mend.
-# Screenshots land in .zig-cache/live/ for review.
+# Screenshots land in .zig-cache/live/ for review; they are compared over
+# the picture alone (640,300 600x300: not the text, the scope or the time).
frames 2
type out = sin(tau * 220 * t) * 0.3
expect problem.len 0
expect out.len 8
expect out.code[5].op sin
expect out.code[7].op mul
+# The GPU's blocks reach the speakers' stream (SDL's dummy driver here).
+frames 10
+expect-audio 1
key return
type color = hsv(x, 0.5, y)
expect color.len 4
@@ -22,7 +26,7 @@ expect color.code[2].op hsv
expect color.code[2].c 0
frames 2
screenshot .zig-cache/live/edited.png
-expect-different-screenshot .zig-cache/live/gradient.png .zig-cache/live/edited.png 640 300 600 380
+expect-different-screenshot .zig-cache/live/gradient.png .zig-cache/live/edited.png 640 300 600 300
# Broken: the tape and the picture stay, the problem is set at its byte.
key backspace
expect problem.at 52
@@ -30,7 +34,7 @@ expect color.len 3
expect color.code[2].c 0
frames 2
screenshot .zig-cache/live/broken.png
-expect-same-screenshot .zig-cache/live/edited.png .zig-cache/live/broken.png 0 640 300 600 380
+expect-same-screenshot .zig-cache/live/edited.png .zig-cache/live/broken.png 0 640 300 600 300
# Mended.
type )
expect problem.len 0