summaryrefslogtreecommitdiff
path: root/examples/live/src
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 06:05:00 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commita4d1e3131499c17bd6f73e65154aa0c2b31c62b3 (patch)
tree087f71b9670da52c2bf92501aff402a2919b9975 /examples/live/src
parentc300a059850115a87a34456dfd60ac97c4a09890 (diff)
downloadsketchbook-a4d1e3131499c17bd6f73e65154aa0c2b31c62b3.tar.gz
sketchbook-a4d1e3131499c17bd6f73e65154aa0c2b31c62b3.zip
examples/live: the sound, the out tape on the GPU per sample
`pub const audio_pull = 256`; each frame `draw` runs synth.comp over as many samples as keep the host's lead queued (`in.lead -| queued`, 8192 at most) and streams the block with f.audioBuffer, as spheres does. Time is a sample clock in Session: set from State's time at every jump (a seek, a step, a replay's start; the queue is dropped then, so the jump is heard at once), and free-running otherwise, so a paused replay keeps sounding its moment. When the out tape differs from the one the last block played, the block crosses from the old tape to the new over its length (`set crossfade false` turns that off, to hear what it saves). The blocks are read back for the scope (an oscilloscope and a spectrum along the bottom) and, after the console's `capture`, appended to the sketch's capture.f32 beside its save. The status line shows the lead. The tapes reach synth.comp in a storage buffer, uploaded per block: two tapes are 4 KiB, and SDL_GPU's Vulkan backend binds a uniform block 4 KiB at a time (MAX_UBO_SECTION_SIZE), so a 4128-byte Synth block read its count as 0 past the cut and the kernel wrote nothing, silently: the probe's first synth numbers timed that. Measured again (RTX 3050, Debug host, two runs): every synth dispatch 0.08-0.14 ms, 60 or 128 instructions, 256 or 1024 samples, fading or not, the empty pass's cost alone; the 4 KiB upload 0.07-0.08. Measured on SDL's dummy driver (nothing audible) from captures: out = 0.3 * sin(t * tau * 220): peak 0.3000, RMS 0.2121, fundamental 220.000 Hz; edited to 330 in one keystroke, 330.000 Hz. The largest sample-to-sample step across the edit: 0.0131 with the crossfade (a 330 Hz sine's own steepest step is 0.0130), 0.3725 without; into the first sound, 0.0011 and 0.2967. The survey's program: peak 0.50, RMS 0.16, its bass at 55.0 Hz. Test: live.script also expects the stream to receive the GPU's blocks, and compares screenshots above the scope. zig build test: 68 of 68 steps. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'examples/live/src')
-rw-r--r--examples/live/src/Lang.zig12
-rw-r--r--examples/live/src/main.zig108
-rw-r--r--examples/live/src/probe.zig12
3 files changed, 113 insertions, 19 deletions
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,