From c300a059850115a87a34456dfd60ac97c4a09890 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 06:03:15 -0300 Subject: examples/live: the picture, typed over itself The sketch: State holds the text (a plain 4 KiB buffer, typed characters appended, backspace deleting the last, return a newline: marked ponytail, the place pardes's editor takes in part 2), the last good out and color tapes, the problem and the time, summed from recorded ticks. `update` compiles the text at every keystroke; a program that parses replaces the tapes and clears the problem, one that does not sets the problem and changes nothing else, so the last good picture stays. `draw` runs picture.frag over the color tape into an image the size of the canvas, 1280x720, or half of it when the tape is longer than 64 instructions (the probe: 128 took 2.7 ms at full size, 0.85 at half), and scales it up. The text is drawn over it on a dark band with the library's text calls (the code face at 1.25, a column a fixed advance), the caret at the end, the problem as a red mark under its byte and its words under the text, and live/recording/replay/paused with the time in a corner. Test: test/live.script, the start of the scripted proof: a tone typed (its eight instructions asserted), a gradient (hsv over x and y, four), an edit (y to x: three, the picture different), a broken line (the problem at byte 52, the tape and the picture the same) and its mend. zig build test: 56 of 56 steps, 6 of 6 tests. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- examples/live/test/live.script | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 examples/live/test/live.script (limited to 'examples/live/test/live.script') diff --git a/examples/live/test/live.script b/examples/live/test/live.script new file mode 100644 index 0000000..386ab33 --- /dev/null +++ b/examples/live/test/live.script @@ -0,0 +1,37 @@ +# A program typed, its tapes after each line, a broken line and its mend. +# Screenshots land in .zig-cache/live/ for review. +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 +key return +type color = hsv(x, 0.5, y) +expect color.len 4 +expect color.code[0].op x +expect color.code[3].op hsv +frames 2 +screenshot .zig-cache/live/gradient.png +# An edit: y becomes x, and the picture changes. +key backspace +key backspace +type x) +expect color.len 3 +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 +# Broken: the tape and the picture stay, the problem is set at its byte. +key backspace +expect problem.at 52 +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 +# Mended. +type ) +expect problem.len 0 +expect color.len 3 -- 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/test/live.script') 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/test/live.script') 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 From ed185f305731309cbddb981166678a552f2fda84 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 07:42:48 -0300 Subject: examples/live: pardes's editor in State, typed with helix's keys The plain buffer of part 1 gives way to pardes's text pane as a library (`pardes-editor`, src/lib_editor.zig there): `Editor(.{ .text = 4096, .undo = 16 KiB, .registers = 1024, .selections = 64 })`, a value of plain data, 39,264 bytes of a State of 43,480. The package takes pardes by path (`../../../02-pardes-code`, the checkout beside sketchbook's) with `.editor = true` and its smallest tree-sitter set; sketchbook is spelled `../../../sketchbook` as pardes spells it, since a package is told by its path's spelling and sketchbook's build.zig cannot be two modules. `update` turns events into keys: printables from `text` events only, specials (Escape, Return, Backspace, Tab, arrows, Home, End, pages, Delete) and ctrl or alt chords (helix's `A-I` an upper-case letter) from `key_down`, a chord's letter dropped if it also comes as text. Each call into the editor thaws the value into a static 128 KiB buffer reset for the call (`update` may not allocate from the host; a key at 4 KiB of text took 29 KiB of it). The text is compiled when the editor's `revision` moves. Measured (a scratch bench through `update`, the survey's program, 646 bytes, a key typed into its middle, 400 times): a typed key, edit and compile, 59.5 us best and 78.8 mean in Debug, 7.4 and 7.8 in ReleaseFast; a motion 6.4 and 0.3. The package's cold `zig build lib` went from 3.1 s to 6.6 s, its first build under `--watch -fincremental` from 4.0 to 5.9 s, and an edit of main.zig rebuilt under it from 0.062-0.067 s to 0.067-0.092 s. Test: test/live.script types through helix now (`i`, the text, Escape, `gl`, `h`, `rx` for the edit, `l` `d` to break the line, `u` to mend it) and still replays to equal text, tapes and screenshots, its seeks moved to the marks' new times. zig build test: 81 of 81 steps, 6 of 6 tests. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- examples/live/build.zig | 11 +++- examples/live/build.zig.zon | 8 ++- examples/live/src/main.zig | 122 ++++++++++++++++++++++++++++++----------- examples/live/test/live.script | 39 ++++++++----- 4 files changed, 132 insertions(+), 48 deletions(-) (limited to 'examples/live/test/live.script') diff --git a/examples/live/build.zig b/examples/live/build.zig index 9d0983d..ccfd32d 100644 --- a/examples/live/build.zig +++ b/examples/live/build.zig @@ -30,7 +30,16 @@ pub fn build(b: *std.Build) void { .{ .name = "scope", .module = sb.modules.scope }, }; - const app = sb.app(.{ .name = "live", .root_source_file = b.path("src/main.zig"), .imports = imports }); + // The editor: helix's keys over a value kept in State. Its tree-sitter + // set is the smallest there is (Zig's grammar), as the language's own + // grammar is handed in at run time. + const pardes = b.dependency("pardes", .{ .target = target, .optimize = optimize, .@"tree-sitter" = .zig, .editor = true }); + const app = sb.app(.{ .name = "live", .root_source_file = b.path("src/main.zig"), .imports = &.{ + .{ .name = "Lang", .module = lang }, + .{ .name = "shaders", .module = shaders.module }, + .{ .name = "scope", .module = sb.modules.scope }, + .{ .name = "pardes-editor", .module = pardes.module("pardes-editor") }, + } }); const steps = app.addSteps(.{ .scripts = &.{b.path("test/live.script")} }); const lang_test = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("src/Lang.zig"), .target = target, .optimize = optimize }) }); steps.test_step.dependOn(&b.addRunArtifact(lang_test).step); diff --git a/examples/live/build.zig.zon b/examples/live/build.zig.zon index 27bd831..d5d265a 100644 --- a/examples/live/build.zig.zon +++ b/examples/live/build.zig.zon @@ -2,7 +2,13 @@ .name = .live, .version = "0.0.1", .dependencies = .{ - .sketchbook = .{ .path = "../.." }, + // `../..` spelled as pardes spells it (its `../sketchbook`): a + // package is told by its path's spelling, and one file cannot be + // in two. + .sketchbook = .{ .path = "../../../sketchbook" }, + // pardes's editor as a library (`pardes-editor`), from the checkout + // beside sketchbook's. + .pardes = .{ .path = "../../../02-pardes-code" }, }, .minimum_zig_version = "0.17.0", .paths = .{""}, diff --git a/examples/live/src/main.zig b/examples/live/src/main.zig index 965152a..e62522f 100644 --- a/examples/live/src/main.zig +++ b/examples/live/src/main.zig @@ -18,6 +18,7 @@ const gpu = sk.abi.gpu; const Lang = @import("Lang"); const shaders = @import("shaders"); const scope = @import("scope"); +const pe = @import("pardes-editor"); pub const name = "live"; pub const width = 1280; @@ -31,11 +32,15 @@ const stream = 1; /// The most samples synthesised in one frame. const max_block = 8192; +/// The text: pardes's editor, helix's keys over a value of plain data +/// (src/lib_editor.zig in pardes), its capacities sized to the language's +/// 4 KiB of text. The undo ring keeps whole texts, the oldest dropped: four +/// at the most text, hundreds of a short program. 39,264 bytes in all, of a +/// State of 43,480. +pub const Editor = pe.Editor(.{ .text = 4096, .undo = 16 * 1024, .registers = 1024, .selections = 64 }); + pub const State = struct { - // ponytail: a plain buffer, typed characters appended and backspace - // deleting the last; part 2 puts pardes's editor here. - text: [4096]u8 = @splat(0), - len: u32 = 0, + ed: Editor = .{}, /// The last program that parsed. out: Lang.Tape = .{}, color: Lang.Tape = .{}, @@ -72,35 +77,45 @@ pub fn command(ss: *Session, c: *sk.Console, line: []const u8) !bool { return true; } +/// What the editor thaws its value into for each call, reset after it: +/// `update` may not allocate from the host, and a fixed buffer keeps a +/// call deterministic and bounded. A key at the most text took 29 KiB of it. +var scratch: [128 * 1024]u8 = undefined; + pub fn update(s: *State, in: *const sk.Input) void { - var edited = false; - for (in.events) |e| switch (e.kind) { - .tick => s.time += e.x, - .text => { - var utf8: [4]u8 = undefined; - const n = std.unicode.utf8Encode(@intCast(@min(e.value, 0x10ffff)), &utf8) catch continue; - if (s.len + n > s.text.len) continue; - @memcpy(s.text[s.len..][0..n], utf8[0..n]); - s.len += n; - edited = true; - }, - .key_down => switch (e.key()) { - .backspace => if (s.len > 0) { - s.len -= 1; - while (s.len > 0 and s.text[s.len] & 0xc0 == 0x80) s.len -= 1; - edited = true; + const revision = s.ed.revision; + // a chord's key_down may come with its letter as text too: not typed + var chord = false; + for (in.events) |e| { + var utf8: [4]u8 = undefined; + const k: pe.Key = switch (e.kind) { + .tick => { + s.time += e.x; + continue; }, - .@"return", .keypad_enter => if (s.len < s.text.len) { - s.text[s.len] = '\n'; - s.len += 1; - edited = true; + .text => text: { + const n = std.unicode.utf8Encode(@intCast(@min(e.value, 0x10ffff)), &utf8) catch continue; + if (chord) { + chord = false; + continue; + } + break :text .{ .cp = @intCast(@min(e.value, 0x10ffff)), .text = utf8[0..n] }; }, - else => {}, - }, - else => {}, - }; - if (!edited) return; - switch (Lang.compile(s.text[0..s.len])) { + .key_down => chord: { + const k = key(e) orelse continue; + chord = (k.ctrl or k.alt) and k.cp >= ' ' and k.cp < 0x7f; + break :chord k; + }, + else => continue, + }; + var fba: std.heap.FixedBufferAllocator = .init(&scratch); + // ponytail: a key the text has no room for is dropped, as part 1's + // buffer dropped it; the editor leaves the value as it was. + _ = s.ed.key(fba.allocator(), null, k); + } + if (s.ed.revision == revision) return; + var text: [Editor.capacity.text]u8 = undefined; + switch (Lang.compile(s.ed.read(&text))) { .program => |p| { s.out = p.out; s.color = p.color; @@ -110,6 +125,51 @@ pub fn update(s: *State, in: *const sk.Input) void { } } +/// A key_down as the editor takes it: the specials, and chords (ctrl or alt +/// with a letter, digit or sign), helix's `A-I` an upper-case letter with +/// alt. A printable without a chord comes as `text`, so it is not one here. +fn key(e: sk.Event) ?pe.Key { + const mods = e.value; + const ctrl = mods & sk.Key.Mod.ctrl != 0; + const alt = mods & sk.Key.Mod.alt != 0; + const shift = mods & sk.Key.Mod.shift != 0; + const special: u21 = switch (e.key()) { + .escape => pe.Key.escape, + .@"return", .keypad_enter, .return2 => pe.Key.enter, + .backspace => pe.Key.backspace, + .tab => pe.Key.tab, + .delete => pe.Key.delete, + .left => pe.Key.left, + .right => pe.Key.right, + .up => pe.Key.up, + .down => pe.Key.down, + .home => pe.Key.home, + .end => pe.Key.end, + .page_up => pe.Key.page_up, + .page_down => pe.Key.page_down, + else => 0, + }; + if (special != 0) return .{ .cp = special, .ctrl = ctrl, .alt = alt, .shift = shift }; + if (!ctrl and !alt) return null; + const scan = @backingInt(e.key()); + const cp: u21 = switch (e.key()) { + .a, .b, .c, .d, .e, .f, .g, .h, .i, .j, .k, .l, .m, .n, .o, .p, .q, .r, .s, .t, .u, .v, .w, .x, .y, .z => @as(u21, if (shift) 'A' else 'a') + scan - @backingInt(sk.Key.a), + .@"1", .@"2", .@"3", .@"4", .@"5", .@"6", .@"7", .@"8", .@"9" => '1' + @as(u21, scan - @backingInt(sk.Key.@"1")), + .@"0" => '0', + .space => ' ', + .minus => '-', + .equals => '=', + .left_bracket => '[', + .right_bracket => ']', + .semicolon => ';', + .comma => ',', + .period => '.', + .slash => '/', + else => return null, + }; + return .{ .cp = cp, .ctrl = ctrl, .alt = alt }; +} + 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 }, @@ -220,7 +280,7 @@ const red = 0xff5a50; // ponytail: one face at one size, so a column is a fixed advance; part 2 // draws the editor's text with sketchbook's new text instead. fn code(s: *const State, f: *sk.Frame) void { - const text = s.text[0..s.len]; + const text = s.ed.bytes(f.scratch) catch return; const cell = f.host.textSizeIn(.code, scale, "M"); const lines = std.mem.count(u8, text, "\n") + 1; var widest: f32 = 0; diff --git a/examples/live/test/live.script b/examples/live/test/live.script index 695d16a..ecf3d77 100644 --- a/examples/live/test/live.script +++ b/examples/live/test/live.script @@ -1,9 +1,12 @@ -# 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.) +# A program typed through the editor (helix's keys: `i`, text, Escape, +# motions), its tapes after each line, a broken line and its mend (an +# undo); 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 +type i type out = sin(tau * 220 * t) * 0.3 expect problem.len 0 expect out.len 8 @@ -17,45 +20,51 @@ type color = hsv(x, 0.5, y) expect color.len 4 expect color.code[0].op x expect color.code[3].op hsv +key escape +expect ed.cursor.mode normal frames 2 console save typed screenshot .zig-cache/live/gradient.png -# An edit: y becomes x, and the picture changes. -key backspace -key backspace -type x) +# An edit: y becomes x (to the line's end, a character left, replaced), +# and the picture changes. +type gl +type h +type rx expect color.len 3 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 300 -# Broken: the tape and the picture stay, the problem is set at its byte. -key backspace +# Broken: the ) deleted; the tape and the picture stay, the problem is set +# at its byte. +type l +type d expect problem.at 52 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 300 -# Mended. -type ) +# Mended: undone. +type u 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). +# tick: 0.3 s and 0.5333 s; a seek lands on the last frame at or before). console play session 0 frames 100 -console seek 488 +console seek 538 expect-snapshot end -console seek 272 +console seek 305 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 ed.front 53 +expect ed.back 0 expect color.code[3].op hsv -- cgit v1.3 From 610ffe373f0f5dd5ccb59bf575f5479ad1ecdbaf Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 07:57:28 -0300 Subject: examples/live: hear this, the expression a node walk leaves the selection on After Alt-o, Alt-i, Alt-n or Alt-p (helix's node walks, run in the editor on the module's parse) the primary range is read back and the text compiled again with it as a probe: Lang.compile takes the bytes of a selection and compiles the smallest expression holding them into a third tape, `Program.probe`. The compiler learns each expression's bytes as it parses (`spanned`: a number, a name used, a call, parentheses, a unary minus, each binary operation, a whole line), keeping the smallest that holds the probe: no table of spans. A name used is the line that defines it, so a walk onto `kick` hears kick. State keeps the probe's tape (`probe`, empty when there is none). While it is set, `draw` hears it as `out` (through the crossfade, as any change) and sees it as the picture, grey unless it is a colour. Any other key empties it, so Escape or an edit goes back to the program. It is in State, so a replay probes where the session did. Test: Lang.zig, a probe on an argument, a number, a cursor on one of its digits, a name used, a line's name, a selection Alt-o grew to, and between two lines (none); test/live.script, Alt-o twice from the 2 of 220 (`220`, then `tau * 220`), then Escape. Lang.compile takes the probe as a second argument; the other calls pass null. zig build test: 85 of 85 steps, 8 of 8 tests. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- examples/live/src/Lang.zig | 115 +++++++++++++++++++++++++++++------------ examples/live/src/main.zig | 36 +++++++++---- examples/live/test/grammar.zig | 2 +- examples/live/test/live.script | 13 +++++ 4 files changed, 124 insertions(+), 42 deletions(-) (limited to 'examples/live/test/live.script') diff --git a/examples/live/src/Lang.zig b/examples/live/src/Lang.zig index cc6280f..6ad1f40 100644 --- a/examples/live/src/Lang.zig +++ b/examples/live/src/Lang.zig @@ -155,16 +155,31 @@ pub const Problem = struct { } }; -pub const Program = struct { out: Tape = .{}, color: Tape = .{} }; +/// `probe` is what is heard and seen instead of the program while a +/// selection is on an expression (main.zig): empty when none is asked for, +/// none holds it, or it is longer than a tape. +pub const Program = struct { out: Tape = .{}, color: Tape = .{}, probe: Tape = .{} }; pub const Result = union(enum) { program: Program, problem: Problem }; -pub fn compile(text: []const u8) Result { - var c: Compiler = .{ .text = text }; +/// `probe`: bytes from..to of the text, a selection; the smallest +/// expression holding them is compiled as `Program.probe`. +pub fn compile(text: []const u8, probe: ?[2]u32) Result { + var c: Compiler = .{ .text = text, .probe = probe }; var program: Program = .{}; c.lines() catch return .{ .problem = c.problem }; - c.tape(&program.out, "out") catch return .{ .problem = c.problem }; - c.tape(&program.color, "color") catch return .{ .problem = c.problem }; + for ([_]struct { *Tape, []const u8 }{ .{ &program.out, "out" }, .{ &program.color, "color" } }) |line| { + const name = c.lookup(line[1]) orelse continue; + var registers: [max_terms]u16 = @splat(none); + _ = c.emit(line[0], name.term, ®isters) catch + return .{ .problem = (c.fail(name.at, "the {s} line needs more than {d} instructions", .{ line[1], max_tape }) catch c.problem) }; + } + if (c.heard != none) { + var registers: [max_terms]u16 = @splat(none); + _ = c.emit(&program.probe, c.heard, ®isters) catch { + program.probe = .{}; + }; + } return .{ .program = program }; } @@ -205,6 +220,21 @@ const Compiler = struct { /// The line's hsv or rgb, if it called one. colour: u16 = none, problem: Problem = .{}, + /// The bytes probed, and the smallest expression found holding them. + probe: ?[2]u32 = null, + heard: u16 = none, + heard_len: u32 = std.math.maxInt(u32), + + /// The expression from byte `from` to where the parse is, its term `t`: + /// the probe's, if it is the smallest yet that holds the probed bytes. + fn spanned(c: *Compiler, from: u32, t: u16) u16 { + const p = c.probe orelse return t; + if (from <= p[0] and p[1] <= c.pos and c.pos - from < c.heard_len) { + c.heard = t; + c.heard_len = c.pos - from; + } + return t; + } fn fail(c: *Compiler, at: u32, comptime fmt: []const u8, args: anytype) error{Problem} { c.problem = .{ .at = at }; @@ -293,7 +323,7 @@ const Compiler = struct { if (builtin(name) != null) return c.fail(first.at, "{s} is a builtin: call yours something else", .{name}); if (c.lookup(name) != null) return c.fail(first.at, "{s} is defined twice: a name is given once", .{name}); c.colour = none; - const root = try c.expr(); + const root = c.spanned(first.at, try c.expr()); const after = c.next(); if (after.kind != .newline and after.kind != .end) return c.fail(after.at, "expected the end of the line, found {s}", .{c.describe(after)}); if (c.colour != none and (c.colour != root or !std.mem.eql(u8, name, "color"))) @@ -310,24 +340,26 @@ const Compiler = struct { } fn expr(c: *Compiler) error{Problem}!u16 { - try c.enter(c.peek().at); + const from = c.peek().at; + try c.enter(from); defer c.depth -= 1; var left = try c.product(); while (true) { const tok = c.peek(); const op: Op = if (tok.is('+')) .add else if (tok.is('-')) .sub else return left; _ = c.next(); - left = try c.call(op, tok.at, &.{ left, try c.product() }); + left = c.spanned(from, try c.call(op, tok.at, &.{ left, try c.product() })); } } fn product(c: *Compiler) !u16 { + const from = c.peek().at; var left = try c.unary(); while (true) { const tok = c.peek(); const op: Op = if (tok.is('*')) .mul else if (tok.is('/')) .div else return left; _ = c.next(); - left = try c.call(op, tok.at, &.{ left, try c.unary() }); + left = c.spanned(from, try c.call(op, tok.at, &.{ left, try c.unary() })); } } @@ -341,9 +373,9 @@ const Compiler = struct { // A negative number is a number (each literal is a term of its own). if (c.terms[a].op == .constant) { c.terms[a].value = -c.terms[a].value; - return a; + return c.spanned(tok.at, a); } - return c.call(.neg, tok.at, &.{a}); + return c.spanned(tok.at, try c.call(.neg, tok.at, &.{a})); } fn primary(c: *Compiler) !u16 { @@ -351,14 +383,14 @@ const Compiler = struct { switch (tok.kind) { .number => { const value = std.fmt.parseFloat(f32, c.span(tok)) catch return c.fail(tok.at, "{s} is not a number", .{c.span(tok)}); - return c.term(.{ .op = .constant, .value = value, .at = tok.at }); + return c.spanned(tok.at, try c.term(.{ .op = .constant, .value = value, .at = tok.at })); }, .name => { const name = c.span(tok); - if (c.peek().is('(')) return c.callOf(tok); + if (c.peek().is('(')) return c.spanned(tok.at, try c.callOf(tok)); if (c.lookup(name)) |n| { if (c.terms[n.term].op == .hsv or c.terms[n.term].op == .rgb) return c.fail(tok.at, "{s} is a colour: only the color line can be one", .{name}); - return n.term; + return c.spanned(tok.at, n.term); } const b = builtin(name) orelse return c.fail(tok.at, "{s} is not defined above", .{name}); if (b.max > 0) return c.fail(tok.at, "{s} takes arguments: {s}", .{ name, b.call }); @@ -368,9 +400,9 @@ const Compiler = struct { .y => 2, else => null, }; - const i = input orelse return c.term(.{ .op = b.op, .value = b.value, .at = tok.at }); + const i = input orelse return c.spanned(tok.at, try c.term(.{ .op = b.op, .value = b.value, .at = tok.at })); if (c.inputs[i] == none) c.inputs[i] = try c.term(.{ .op = b.op, .at = tok.at }); - return c.inputs[i]; + return c.spanned(tok.at, c.inputs[i]); }, .newline, .end => return c.fail(tok.at, "expected an expression before the end of the line", .{}), .symbol => { @@ -381,7 +413,7 @@ const Compiler = struct { const inner = try c.expr(); const close = c.next(); if (!close.is(')')) return c.fail(close.at, "expected ) to close the ( at byte {d}, found {s}", .{ tok.at, c.describe(close) }); - return inner; + return c.spanned(tok.at, inner); }, } } @@ -417,15 +449,6 @@ const Compiler = struct { return t; } - /// The tape of the line named `name` (empty without one). - fn tape(c: *Compiler, into: *Tape, name: []const u8) !void { - into.* = .{}; - const line = c.lookup(name) orelse return; - var registers: [max_terms]u16 = @splat(none); - _ = c.emit(into, line.term, ®isters) catch - return c.fail(line.at, "the {s} line needs more than {d} instructions", .{ name, max_tape }); - } - fn push(into: *Tape, i: Instruction) !u32 { if (into.len == max_tape) return error.Full; into.code[into.len] = i; @@ -500,7 +523,7 @@ pub const Synth = extern struct { const testing = std.testing; fn compiled(text: []const u8) !Program { - return switch (compile(text)) { + return switch (compile(text, null)) { .program => |p| p, .problem => |p| { std.debug.print("unexpected problem at {d}: {s}\n", .{ p.at, p.text() }); @@ -641,7 +664,7 @@ test "every problem, with its byte" { .{ .text = "out = mix(1, 2)", .at = 6, .words = "mix takes 3 arguments, not 2: mix(a, b, w)" }, .{ .text = "out = sin()", .at = 6, .words = "sin takes 1 arguments, not 0: sin(a)" }, }; - for (cases) |case| switch (compile(case.text)) { + for (cases) |case| switch (compile(case.text, null)) { .program => { std.debug.print("{s}: no problem\n", .{case.text}); return error.TestUnexpectedResult; @@ -665,7 +688,7 @@ test "past the capacities: problems in words" { const fits = try compiled(w.buffered()); try testing.expectEqual(@as(u32, 127), fits.out.len); try w.writeAll(" + 1"); - switch (compile(w.buffered())) { + switch (compile(w.buffered(), null)) { .program => return error.TestUnexpectedResult, .problem => |p| { try testing.expectEqualStrings("the out line needs more than 128 instructions", p.text()); @@ -679,14 +702,14 @@ test "past the capacities: problems in words" { const long = w.buffered(); var text: [8192]u8 = undefined; const unused = try std.fmt.bufPrint(&text, "a = {s}\n", .{long}); - switch (compile(unused)) { + switch (compile(unused, null)) { .program => return error.TestUnexpectedResult, .problem => |p| try testing.expectEqualStrings("the program is longer than 512 terms", p.text()), } // Names. w = .fixed(&buf); for (0..max_names + 1) |i| try w.print("n{d} = {d}\n", .{ i, i }); - switch (compile(w.buffered())) { + switch (compile(w.buffered(), null)) { .program => return error.TestUnexpectedResult, .problem => |p| try testing.expectEqualStrings("more than 128 names", p.text()), } @@ -696,8 +719,36 @@ test "past the capacities: problems in words" { for (0..40) |_| try w.writeAll("("); try w.writeAll("1"); for (0..40) |_| try w.writeAll(")"); - switch (compile(w.buffered())) { + switch (compile(w.buffered(), null)) { .program => return error.TestUnexpectedResult, .problem => |p| try testing.expectEqualStrings("nested more than 32 deep", p.text()), } } + +test "a probe: the smallest expression holding the bytes, its own tape" { + const text = "kick = exp(-7 * fract(t * 2))\nout = sin(tau * 220 * t) * kick"; + for ([_]struct { [2]u32, []const Op }{ + // `tau * 220 * t`, a selection Alt-o grew to + .{ .{ 40, 53 }, &.{ .constant, .constant, .mul, .t, .mul } }, + // `220` alone, and a cursor on its `2` + .{ .{ 46, 49 }, &.{.constant} }, + .{ .{ 46, 47 }, &.{.constant} }, + // `kick` where it is used: the line that defines it + .{ .{ 57, 61 }, &.{ .constant, .t, .constant, .mul, .fract, .mul, .exp } }, + // an argument + .{ .{ 22, 27 }, &.{ .t, .constant, .mul } }, + // the out line from its name, and the whole of it + .{ .{ 30, 33 }, &.{ .constant, .constant, .mul, .t, .mul, .sin, .constant, .constant, .mul, .fract, .mul, .exp, .mul } }, + }) |case| { + const p = try switch (compile(text, case[0])) { + .program => |p| p, + .problem => error.TestUnexpectedResult, + }; + expectOps(p.probe, case[1]) catch |e| { + std.debug.print("probe {any}\n", .{case[0]}); + return e; + }; + } + // nothing holds the newline between the lines: no probe + try testing.expectEqual(@as(u32, 0), compile(text, .{ 29, 30 }).program.probe.len); +} diff --git a/examples/live/src/main.zig b/examples/live/src/main.zig index d6bc8d6..cc11dbe 100644 --- a/examples/live/src/main.zig +++ b/examples/live/src/main.zig @@ -44,6 +44,10 @@ pub const State = struct { /// The last program that parsed. out: Lang.Tape = .{}, color: Lang.Tape = .{}, + /// Hear this: after a node walk (Alt-o, Alt-i, Alt-n, Alt-p), the + /// expression the primary range is on, heard and seen (grey) instead of + /// out and color until the next key; empty otherwise. + probe: Lang.Tape = .{}, /// What is wrong with the text, if it does not parse (`len` 0: nothing). problem: Lang.Problem = .{}, /// Seconds, from recorded ticks: a replay draws the same frames. @@ -127,11 +131,23 @@ pub fn update(s: *State, in: *const sk.Input) void { var fba: std.heap.FixedBufferAllocator = .init(&scratch); // ponytail: a key the text has no room for is dropped, as part 1's // buffer dropped it; the editor leaves the value as it was. - _ = s.ed.key(fba.allocator(), &syn, k); + const outcome = s.ed.key(fba.allocator(), &syn, k); + s.probe = .{}; + if (k.alt and outcome == .done and (k.cp == 'o' or k.cp == 'i' or k.cp == 'n' or k.cp == 'p')) probe: { + fba.reset(); + const ranges = fba.allocator().create([pe.max_selections]pe.Selection) catch break :probe; + const got = s.ed.ranges(fba.allocator(), ranges) catch break :probe; + const r = ranges[got.primary]; + var text: [Editor.capacity.text]u8 = undefined; + switch (Lang.compile(s.ed.read(&text), .{ @intCast(@min(r.anchor, r.head)), @intCast(@max(r.anchor, r.head)) })) { + .program => |p| s.probe = p.probe, + .problem => {}, + } + } } if (s.ed.revision == revision) return; var text: [Editor.capacity.text]u8 = undefined; - switch (Lang.compile(s.ed.read(&text))) { + switch (Lang.compile(s.ed.read(&text), null)) { .program => |p| { s.out = p.out; s.color = p.color; @@ -204,7 +220,8 @@ pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void { } // A tape past 64 instructions is drawn at half resolution: at 1280x720 // 128 instructions took 2.7 ms of the GPU, at half 0.85 (the probe). - const half: u32 = if (s.color.len > 64) 2 else 1; + const seen = if (s.probe.len > 0) &s.probe else &s.color; + const half: u32 = if (seen.len > 64) 2 else 1; const size: [2]u32 = .{ width / half, height / half }; if (!std.meta.eql(ss.size, size)) { if (ss.picture != .none) f.host.freeImage(ss.picture); @@ -218,8 +235,8 @@ pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void { .height = size[1], .pipelines = &.{picture_pipeline}, .draws = &.{.{ .pipeline = 0, .vertex_count = 3, .fragment_uniforms = gpu.uniform(f.scratch, Lang.Picture{ - .tape = std.mem.bytesToValue([Lang.max_tape][4]u32, &s.color.code), - .len = s.color.len, + .tape = std.mem.bytesToValue([Lang.max_tape][4]u32, &seen.code), + .len = seen.len, .time = s.time, .size = .{ @floatFromInt(size[0]), @floatFromInt(size[1]) }, }) }}, @@ -255,10 +272,11 @@ fn sound(s: *const State, ss: *Session, f: *sk.Frame) void { } const want: u32 = @min(in.lead -| queued, max_block); if (want > 0) { - const new = std.mem.sliceAsBytes(s.out.slice()); + const heard = if (s.probe.len > 0) &s.probe else &s.out; + const new = std.mem.sliceAsBytes(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 }; + tapes.* = .{ heard.code, ss.heard.code }; f.upload(ss.tapes, 0, std.mem.asBytes(tapes)); f.compute(.{ .label = "synth", @@ -266,7 +284,7 @@ fn sound(s: *const State, ss: *Session, f: *sk.Frame) void { .readonly = &.{ss.tapes}, .readwrite = &.{ss.block}, .uniforms = gpu.uniform(f.scratch, Lang.Synth{ - .len = s.out.len, + .len = heard.len, .old_len = ss.heard.len, .count = want, .fade = if (changed and s.crossfade) want else 0, @@ -279,7 +297,7 @@ fn sound(s: *const State, ss: *Session, f: *sk.Frame) void { 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.heard = heard.*; } ss.scope.draw(f, .{ .oscilloscope = .{ .rect = .{ .x = 16, .y = height - 104, .w = 480, .h = 88 }, .samples = 1024 }, diff --git a/examples/live/test/grammar.zig b/examples/live/test/grammar.zig index 497c05d..2fa982f 100644 --- a/examples/live/test/grammar.zig +++ b/examples/live/test/grammar.zig @@ -41,7 +41,7 @@ test "the grammar and Lang.zig agree: both take a program, both refuse a broken }) |case| { const tree = parser.parseString(case.text, null) orelse return error.NoTree; defer tree.destroy(); - const lang = Lang.compile(case.text) == .program; + const lang = Lang.compile(case.text, null) == .program; std.testing.expectEqual(case.grammar, !tree.rootNode().hasError()) catch |e| { std.debug.print("the grammar on {s}\n", .{case.text}); return e; diff --git a/examples/live/test/live.script b/examples/live/test/live.script index ecf3d77..a06ed97 100644 --- a/examples/live/test/live.script +++ b/examples/live/test/live.script @@ -68,3 +68,16 @@ console stop expect ed.front 53 expect ed.back 0 expect color.code[3].op hsv +# Hear this: Alt-o from the 2 of 220 to the number, then to `tau * 220`, +# which alone is heard and seen until the next key. +type gg +type 16l +hold left_alt +key o +expect probe.len 1 +key o +release left_alt +expect probe.len 3 +expect probe.code[2].op mul +key escape +expect probe.len 0 -- cgit v1.3 From 4350e6840940da0b7af800faf5a1455a1de1908e Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 07:58:27 -0300 Subject: examples/live: drag a number, the program following as it moves The left button pressed on a number token (the grammar's `number` node at the byte under the pointer, found with the module's parse) starts a drag kept in State: the literal's bytes, the value, its precision. Each pointer move rewrites the literal through the editor's `replace`: a step for every 4 pixels right of where it was pressed (left, down), the step from the literal's own precision (`220` by ones, `0.25` by hundredths), printed with as many decimals. The first rewrite is a step to undo and the rest join it, so one `u` takes the whole drag back. The text changes, so the program is compiled and heard as it moves. A literal with an exponent is not dragged (marked ponytail). `update` finds the byte under the pointer as `code` lays the text out: a character is 10 by 22 pixels, measured once (the code face at 1.25, natively: ten M are 100) and now a constant both use, where `code` asked the host each frame. Test: test/live.script presses 220, drags 40 pixels right (230), back to 12 left of the press (217), lets go and undoes the drag in one step (220). zig build test: 85 of 85 steps. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- examples/live/src/main.zig | 71 ++++++++++++++++++++++++++++++++++++++++-- examples/live/test/live.script | 13 ++++++++ 2 files changed, 82 insertions(+), 2 deletions(-) (limited to 'examples/live/test/live.script') diff --git a/examples/live/src/main.zig b/examples/live/src/main.zig index cc11dbe..d7a514f 100644 --- a/examples/live/src/main.zig +++ b/examples/live/src/main.zig @@ -48,6 +48,8 @@ pub const State = struct { /// expression the primary range is on, heard and seen (grey) instead of /// out and color until the next key; empty otherwise. probe: Lang.Tape = .{}, + /// A number the pointer was pressed on and drags. + drag: Drag = .{}, /// What is wrong with the text, if it does not parse (`len` 0: nothing). problem: Lang.Problem = .{}, /// Seconds, from recorded ticks: a replay draws the same frames. @@ -57,6 +59,21 @@ pub const State = struct { crossfade: bool = true, }; +/// Drag a number: its literal's bytes, at..at + len, rewritten as the +/// pointer moves, a step for every 4 pixels to the right (left, down) from +/// where it was pressed: its own precision, `220` by ones, `0.25` by +/// hundredths. The drag is one step to undo. +pub const Drag = struct { + active: bool = false, + /// Rewritten once: the next frames join its step to undo. + moved: bool = false, + at: u32 = 0, + len: u32 = 0, + x: f32 = 0, + value: f64 = 0, + decimals: u8 = 0, +}; + pub const Session = struct { picture: sk.Image = .none, size: [2]u32 = .{ 0, 0 }, @@ -107,12 +124,59 @@ pub fn update(s: *State, in: *const sk.Input) void { // a chord's key_down may come with its letter as text too: not typed var chord = false; for (in.events) |e| { + var fba: std.heap.FixedBufferAllocator = .init(&scratch); var utf8: [4]u8 = undefined; const k: pe.Key = switch (e.kind) { .tick => { s.time += e.x; continue; }, + .mouse_down => { + // the number token under the pointer, by the grammar's parse + if (e.code != 1 or e.x < margin or e.y < margin) continue; + var text: [Editor.capacity.text]u8 = undefined; + const t = s.ed.read(&text); + const row: usize = @intFromFloat((e.y - margin) / cell[1]); + const col: usize = @intFromFloat((e.x - margin) / cell[0]); + var start: usize = 0; + for (0..row) |_| start = (std.mem.indexOfScalarPos(u8, t, start, '\n') orelse t.len) + 1; + if (start > t.len) continue; + const end = std.mem.indexOfScalarPos(u8, t, start, '\n') orelse t.len; + if (start + col >= end) continue; + const tree = syn.parse(fba.allocator(), &s.ed, language()) orelse continue; + const n = tree.rootNode().descendantForByteRange(@intCast(start + col), @intCast(start + col + 1)) orelse continue; + if (!std.mem.eql(u8, n.kind(), "number")) continue; + const literal = t[n.startByte()..n.endByte()]; + // ponytail: a number with an exponent is not dragged + if (std.mem.indexOfScalar(u8, literal, 'e') != null) continue; + const point = std.mem.indexOfScalar(u8, literal, '.'); + s.drag = .{ + .active = true, + .at = n.startByte(), + .len = @intCast(literal.len), + .x = e.x, + .value = std.fmt.parseFloat(f64, literal) catch continue, + .decimals = @intCast(if (point) |p| literal.len - p - 1 else 0), + }; + continue; + }, + .mouse_move => { + if (!s.drag.active) continue; + const d = &s.drag; + const steps = @round((e.x - d.x) / 4); + var buf: [32]u8 = undefined; + const num = std.fmt.bufPrint(&buf, "{[0]d:.[1]}", .{ d.value + steps * std.math.pow(f64, 10, -@as(f64, @floatFromInt(d.decimals))), d.decimals }) catch continue; + var text: [Editor.capacity.text]u8 = undefined; + if (std.mem.eql(u8, s.ed.read(&text)[d.at..][0..d.len], num)) continue; + if (s.ed.replace(fba.allocator(), d.at, d.at + d.len, num, d.moved) != .done) continue; + d.len = @intCast(num.len); + d.moved = true; + continue; + }, + .mouse_up, .cancel => { + s.drag.active = false; + continue; + }, .text => text: { const n = std.unicode.utf8Encode(@intCast(@min(e.value, 0x10ffff)), &utf8) catch continue; if (chord) { @@ -128,7 +192,6 @@ pub fn update(s: *State, in: *const sk.Input) void { }, else => continue, }; - var fba: std.heap.FixedBufferAllocator = .init(&scratch); // ponytail: a key the text has no room for is dropped, as part 1's // buffer dropped it; the editor leaves the value as it was. const outcome = s.ed.key(fba.allocator(), &syn, k); @@ -306,6 +369,11 @@ fn sound(s: *const State, ss: *Session, f: *sk.Frame) void { } const scale = 1.25; +/// A character of the code face at `scale`, wide and high, in pixels: +/// measured (`f.host.textSizeIn(.code, 1.25, "M")` natively, and ten of +/// them are 100), so `update` finds the byte under the pointer as `code` +/// lays it out. +const cell: [2]f32 = .{ 10, 22 }; const margin = 16; const ink = 0xe8e8e8; const red = 0xff5a50; @@ -329,7 +397,6 @@ fn code(s: *const State, f: *sk.Frame) void { const got = s.ed.ranges(f.scratch, ranges) catch return; // a class a byte, from the grammar's highlights (none if it fails) const colors = pe.Syntax.colors(f.scratch, &s.ed, language(), 0, text.len) catch &.{}; - const cell = f.host.textSizeIn(.code, scale, "M"); const problem = s.problem.len > 0; var lines: usize = 1; var widest: usize = 24; diff --git a/examples/live/test/live.script b/examples/live/test/live.script index a06ed97..bedcf83 100644 --- a/examples/live/test/live.script +++ b/examples/live/test/live.script @@ -81,3 +81,16 @@ expect probe.len 3 expect probe.code[2].op mul key escape expect probe.len 0 +# Drag a number: 220 (line 1, columns 16..18; a column 10 pixels from 16, +# a line 22 from 16) pressed and dragged 40 pixels right is 230, then 12 +# left of where it was pressed is 217; the program follows; the drag is one +# step to undo. +down 181 27 +move 201 27 +move 221 27 +expect out.code[1].a 1130758144 +move 169 27 +expect out.code[1].a 1129906176 +up 169 27 +type u +expect out.code[1].a 1130102784 -- cgit v1.3 From fb237529724ae1c01b15f99e3b25bdce7c77cf82 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 07:59:59 -0300 Subject: examples/live: square units, x and y from the centre in half the height picture.frag gives the tape x and y in half the target's height, 0 at its centre and y up (`y` -1 to 1, `x` -1.78 to 1.78 on 16:9), so a circle is round on any window, half resolution included. Lang.builtins and the README's table say so; the README's program draws its ring about the centre (`r = sqrt(x * x + y * y)`, the ring's radius and width doubled into the new unit). Test: test/live.script replaces the text (`%`, `c`) with a disc of radius 0.5, `step(x * x + y * y, 0.25)`, and reads its edge 180 pixels from the centre across and up: white at 170, black at 190, each way. zig build test: 85 of 85 steps. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- examples/live/README.md | 6 +++--- examples/live/shaders/picture.frag | 5 +++-- examples/live/src/Lang.zig | 4 ++-- examples/live/test/live.script | 15 +++++++++++++++ 4 files changed, 23 insertions(+), 7 deletions(-) (limited to 'examples/live/test/live.script') diff --git a/examples/live/README.md b/examples/live/README.md index 3c0c885..e69b583 100644 --- a/examples/live/README.md +++ b/examples/live/README.md @@ -14,8 +14,8 @@ beat = t * bpm / 60 kick = exp(-7 * fract(beat)) thump = sin(tau * 48 * t * (1 + 2 * kick)) * kick * 0.5 out = thump -r = sqrt((x - 0.5) * (x - 0.5) + (y - 0.5) * (y - 0.5)) -color = hsv(fract(beat / 16 + r), 0.6, smoothstep(0.03, 0, abs(r - 0.2 - 0.1 * kick))) +r = sqrt(x * x + y * y) +color = hsv(fract(beat / 16 + r), 0.6, smoothstep(0.06, 0, abs(r - 0.4 - 0.2 * kick))) ``` The picture pulses with the kick because both lines say `kick`. @@ -43,7 +43,7 @@ calls; `#` starts a comment. The builtins (src/Lang.zig's one table): | | | | --- | --- | | `t` | seconds | -| `x`, `y` | 0..1, left to right and bottom to top; 0 in sound | +| `x`, `y` | across and up from the centre, in half the window's height (`y` -1 to 1, `x` -1.78 to 1.78 on 16:9: a circle is round); 0 in sound | | `tau`, `pi` | a turn and half a turn, in radians | | `sin(a)`, `cos(a)` | of radians | | `exp(a)`, `log(a)`, `pow(a, b)`, `sqrt(a)` | e to the a, its inverse, a to the b (a not negative), square root | diff --git a/examples/live/shaders/picture.frag b/examples/live/shaders/picture.frag index ec1293e..4621b31 100644 --- a/examples/live/shaders/picture.frag +++ b/examples/live/shaders/picture.frag @@ -1,6 +1,7 @@ #version 450 #extension GL_GOOGLE_include_directive : require -// The color tape at every pixel; x and y run 0..1, y up. +// The color tape at every pixel; x and y in half the target's height, 0 at +// its centre, y up: a circle is round on any window. #include "Picture.glsl" #define TAPE(i) u.tape[i] #include "vm.glsl" @@ -8,6 +9,6 @@ layout(location = 0) out vec4 out_color; void main() { - vec2 p = vec2(gl_FragCoord.x, u.size.y - gl_FragCoord.y) / u.size; + vec2 p = vec2(gl_FragCoord.x - 0.5 * u.size.x, 0.5 * u.size.y - gl_FragCoord.y) / (0.5 * u.size.y); out_color = vec4(clamp(vm(0u, u.len, u.time, p.x, p.y), 0.0, 1.0), 1.0); } diff --git a/examples/live/src/Lang.zig b/examples/live/src/Lang.zig index 6ad1f40..4ce504e 100644 --- a/examples/live/src/Lang.zig +++ b/examples/live/src/Lang.zig @@ -92,8 +92,8 @@ pub const Builtin = struct { /// switch, and only for a program that needs it. pub const builtins = [_]Builtin{ .{ .call = "t", .op = .t, .doc = "seconds" }, - .{ .call = "x", .op = .x, .doc = "0 at the left to 1 at the right; 0 in sound" }, - .{ .call = "y", .op = .y, .doc = "0 at the bottom to 1 at the top; 0 in sound" }, + .{ .call = "x", .op = .x, .doc = "across, in half the window's height: 0 at the centre, -1.78 to 1.78 on 16:9; 0 in sound" }, + .{ .call = "y", .op = .y, .doc = "up, in half the window's height: -1 at the bottom, 0 at the centre, 1 at the top; 0 in sound" }, .{ .call = "tau", .op = .constant, .value = std.math.tau, .doc = "a turn in radians, 6.283..." }, .{ .call = "pi", .op = .constant, .value = std.math.pi, .doc = "half a turn, 3.141..." }, .{ .call = "sin(a)", .op = .sin, .min = 1, .max = 1, .doc = "sine, of radians" }, diff --git a/examples/live/test/live.script b/examples/live/test/live.script index bedcf83..1ddcec2 100644 --- a/examples/live/test/live.script +++ b/examples/live/test/live.script @@ -94,3 +94,18 @@ expect out.code[1].a 1129906176 up 169 27 type u expect out.code[1].a 1130102784 +# Square units: a disc of radius 0.5 (half the window's height is 1) is +# 180 pixels across each way from the centre, round on 16:9. +type % +type c +type color = step(x * x + y * y, 0.25) +key escape +expect color.len 7 +frames 2 +screenshot .zig-cache/live/disc.png +expect-pixel .zig-cache/live/disc.png 810 360 255 255 255 255 2 +expect-pixel .zig-cache/live/disc.png 640 190 255 255 255 255 2 +expect-pixel .zig-cache/live/disc.png 470 360 255 255 255 255 2 +expect-pixel .zig-cache/live/disc.png 830 360 0 0 0 255 2 +expect-pixel .zig-cache/live/disc.png 640 170 0 0 0 255 2 +expect-pixel .zig-cache/live/disc.png 640 550 0 0 0 255 2 -- cgit v1.3