From 1637cf5efa338fee7797ba83f264dcab5bde4800 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 05:50:47 -0300 Subject: examples/live: the interpreter of a tape, and its probe Demo 1's route: a program becomes a flat tape of at most 128 instructions (src/Lang.zig: the ops, the instruction, the tape and the two uniform blocks that carry it; build.zig writes Lang.Op out as ops.glsl, so the op numbers have one definition), and ONE interpreter, shaders/vm.glsl, runs it per pixel (picture.frag) and per sample (synth.comp, crossing from the tape it replaces over the first `fade` samples). Instruction i writes register i; the tape is pushed as uniforms. The probe measures it before anything else is built: `zig build probe` plays made-up tapes (every instruction reaching the last, a noise in ten) under --profile-gpu. RTX 3050 (driver 620.70.06), Debug host, means of 601 frames, three runs agreeing within 0.07 ms: picture, empty tape 720p 0.14-0.23 1080p 0.19-0.28 (a pass's cost) picture, 60 720p 1.38-1.45 1080p 2.89-2.96 picture, 128 720p 2.71-2.81 1080p 5.86-5.95 picture, 128 at 640x360 0.85-0.94 The budget, under 2 ms at 1280x720: 60 instructions meet it, 128 do not. The cheap fixes, on 128 at 720p: the tape in a storage buffer 2.97-3.03 (worse); the registers as an array of 16 (kept in registers, indexing turned into selects) 5.82-5.93 (twice as slow); the 128-array with only 16 slots used 2.11-2.34 (cache footprint: a register allocator would buy about a sixth); half resolution 0.85-0.94. So the sketch will draw a tape longer than 64 instructions at half resolution, scaled up. The variants are kept outside the tree. The synth's numbers taken here were of a kernel that wrote nothing (its uniform block was past SDL's 4 KiB): the sound's change measures it again. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- examples/live/shaders/synth.comp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 examples/live/shaders/synth.comp (limited to 'examples/live/shaders/synth.comp') diff --git a/examples/live/shaders/synth.comp b/examples/live/shaders/synth.comp new file mode 100644 index 0000000..9a066a6 --- /dev/null +++ b/examples/live/shaders/synth.comp @@ -0,0 +1,26 @@ +#version 450 +#extension GL_GOOGLE_include_directive : require +// One audio block, one thread per sample: the out tape at each sample's +// time, crossing over the first `fade` samples from the tape it replaces, +// so an edit does not click. x and y are 0. +layout(local_size_x = 64) in; + +#include "Synth.glsl" +#define TAPE(i) u.tape[i] +#include "vm.glsl" + +layout(set = 1, binding = 0) buffer Block { float block[]; }; + +// A sample the device can take: no NaN or infinity, -1..1. +float heard(float v) { + return isnan(v) || isinf(v) ? 0.0 : clamp(v, -1.0, 1.0); +} + +void main() { + uint n = gl_GlobalInvocationID.x; + if (n >= u.count) return; + float t = u.start + float(n) / u.rate; + float v = heard(vm(0u, u.len, t, 0.0, 0.0).x); + if (n < u.fade) v = mix(heard(vm(MAX_TAPE, u.old_len, t, 0.0, 0.0).x), v, (float(n) + 0.5) / float(u.fade)); + block[n] = v; +} -- cgit v1.3 From a4d1e3131499c17bd6f73e65154aa0c2b31c62b3 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 06:05:00 -0300 Subject: 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 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- examples/live/build.zig | 1 + examples/live/shaders/synth.comp | 4 +- examples/live/src/Lang.zig | 12 +++-- examples/live/src/main.zig | 108 +++++++++++++++++++++++++++++++++++---- examples/live/src/probe.zig | 12 +++-- examples/live/test/live.script | 10 ++-- 6 files changed, 124 insertions(+), 23 deletions(-) (limited to 'examples/live/shaders/synth.comp') 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 `.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 -- cgit v1.3 From 8dc369f343fda8f54756376728f4690386c20a69 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 06:10:20 -0300 Subject: examples/live: the scripted proof, a session played back and sought test/live.script now ends with the run's own session replayed: `play session 0` from its start, then `seek` to the two moments marked as they happened (the gradient just typed, and the end after an edit, a broken line and its mend). At each, the State equals the mark leaf by leaf (`expect-snapshot`: the text, both tapes, the problem, the time), and at the first the screenshot equals the live one over the text and the picture (0,0 1000x600, without the scope and the status line). `stop` then goes on live from that moment: the typed program, before its edit. What a script could not do: a `record` clip ends at the first mark (Console.zig's `save` stops it), so the clip cannot be marked while it is made: the session is replayed instead. A mark is State before its frame's tick, so its time is the printed time less a frame, and `seek MS` lands on the last frame at or before MS: the script seeks 5 ms past each mark's time (272 and 488). The sound at a jump: `want` counted the queue the clear is about to drop, so a seek starved the device for a frame; now it counts from empty. And the block after a jump crosses from the old tape at where the device was (the clock less what was queued) to the tape at the new time (Synth.old_start). Not measured: a capture records every block made, including the tail the jump drops, so it cannot show what was heard across a seek. zig build test: 68 of 68 steps, 6 of 6 tests. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- examples/live/shaders/synth.comp | 10 +++++----- examples/live/src/Lang.zig | 6 ++++-- examples/live/src/main.zig | 13 +++++++++---- examples/live/src/probe.zig | 1 + examples/live/test/live.script | 22 +++++++++++++++++++++- 5 files changed, 40 insertions(+), 12 deletions(-) (limited to 'examples/live/shaders/synth.comp') diff --git a/examples/live/shaders/synth.comp b/examples/live/shaders/synth.comp index 8ae48bb..0e7ab02 100644 --- a/examples/live/shaders/synth.comp +++ b/examples/live/shaders/synth.comp @@ -1,8 +1,9 @@ #version 450 #extension GL_GOOGLE_include_directive : require // One audio block, one thread per sample: the out tape at each sample's -// time, crossing over the first `fade` samples from the tape it replaces, -// so an edit does not click. x and y are 0. +// time, crossing over the first `fade` samples from the tape it replaces +// (at its own time, which differs after a seek), so an edit or a seek +// does not click. x and y are 0. layout(local_size_x = 64) in; #include "Synth.glsl" @@ -21,8 +22,7 @@ float heard(float v) { void main() { uint n = gl_GlobalInvocationID.x; if (n >= u.count) return; - float t = u.start + float(n) / u.rate; - float v = heard(vm(0u, u.len, t, 0.0, 0.0).x); - if (n < u.fade) v = mix(heard(vm(MAX_TAPE, u.old_len, t, 0.0, 0.0).x), v, (float(n) + 0.5) / float(u.fade)); + float v = heard(vm(0u, u.len, u.start + float(n) / u.rate, 0.0, 0.0).x); + if (n < u.fade) v = mix(heard(vm(MAX_TAPE, u.old_len, u.old_start + float(n) / u.rate, 0.0, 0.0).x), v, (float(n) + 0.5) / float(u.fade)); block[n] = v; } diff --git a/examples/live/src/Lang.zig b/examples/live/src/Lang.zig index 4f3c3bf..484de54 100644 --- a/examples/live/src/Lang.zig +++ b/examples/live/src/Lang.zig @@ -484,10 +484,12 @@ pub const Synth = extern struct { /// Samples in the block. count: u32, fade: u32, - /// The block's first sample, seconds. + /// The block's first sample, seconds; the old tape's, which differs + /// after a jump (a seek: the old tape where the device was). start: f32, + old_start: f32, rate: f32, - pad: [2]f32 = .{ 0, 0 }, + pad: f32 = 0, pub const glsl = .{ .uniform = .{ .set = 2, .instance = "u" } }; }; diff --git a/examples/live/src/main.zig b/examples/live/src/main.zig index 13f89be..965152a 100644 --- a/examples/live/src/main.zig +++ b/examples/live/src/main.zig @@ -166,16 +166,20 @@ fn sound(s: *const State, ss: *Session, f: *sk.Frame) void { 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. + // A jump is heard at once: what was queued from before it is dropped, + // and the block crosses from where the device was to the new time. + const rate: f64 = @floatFromInt(in.sample_rate); + var queued = in.stream(stream).queued; + const was = ss.clock - @as(f64, @floatFromInt(queued)) / rate; if (in.jumped) { ss.clock = s.time; + queued = 0; f.audio(stream, &.{}, .{ .clear = true }); } - const rate: f64 = @floatFromInt(in.sample_rate); - const want: u32 = @min(in.lead -| in.stream(stream).queued, max_block); + const want: u32 = @min(in.lead -| 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 changed = in.jumped or !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)); @@ -190,6 +194,7 @@ fn sound(s: *const State, ss: *Session, f: *sk.Frame) void { .count = want, .fade = if (changed and s.crossfade) want else 0, .start = @floatCast(ss.clock), + .old_start = @floatCast(if (in.jumped) was else ss.clock), .rate = @floatCast(rate), }), .groups = .{ (want + 63) / 64, 1, 1 }, diff --git a/examples/live/src/probe.zig b/examples/live/src/probe.zig index 1e2970c..4f816b2 100644 --- a/examples/live/src/probe.zig +++ b/examples/live/src/probe.zig @@ -123,6 +123,7 @@ pub fn draw(ss: *Session, f: *sk.Frame) void { .count = k.count, .fade = if (k.fade) k.count else 0, .start = ss.time, + .old_start = ss.time, .rate = 48000, }), .groups = .{ (k.count + 63) / 64, 1, 1 }, diff --git a/examples/live/test/live.script b/examples/live/test/live.script index 22085bf..695d16a 100644 --- a/examples/live/test/live.script +++ b/examples/live/test/live.script @@ -1,4 +1,6 @@ -# A program typed, its tapes after each line, a broken line and its mend. +# A program typed, its tapes after each line, a broken line and its mend; +# then this run's session played back, and sought to two moments marked as +# they happened. (A mark ends a clip, `record`, so the session is replayed.) # 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 @@ -16,6 +18,7 @@ expect color.len 4 expect color.code[0].op x expect color.code[3].op hsv frames 2 +console save typed screenshot .zig-cache/live/gradient.png # An edit: y becomes x, and the picture changes. key backspace @@ -39,3 +42,20 @@ expect-same-screenshot .zig-cache/live/edited.png .zig-cache/live/broken.png 0 6 type ) expect problem.len 0 expect color.len 3 +console save end +# Played back from the start, then sought: each moment's text, tapes and +# time as they were live, leaf by leaf (a mark is taken before its frame's +# tick: 0.2667 s and 0.4833 s; a seek lands on the last frame at or before). +console play session 0 +frames 100 +console seek 488 +expect-snapshot end +console seek 272 +expect-snapshot typed +expect color.len 4 +screenshot .zig-cache/live/sought.png +expect-same-screenshot .zig-cache/live/gradient.png .zig-cache/live/sought.png 0 0 0 1000 600 +# Live again from there: the typed program, before its edit. +console stop +expect len 53 +expect color.code[3].op hsv -- cgit v1.3