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/src/probe.zig | 130 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 examples/live/src/probe.zig (limited to 'examples/live/src/probe.zig') diff --git a/examples/live/src/probe.zig b/examples/live/src/probe.zig new file mode 100644 index 0000000..663bed3 --- /dev/null +++ b/examples/live/src/probe.zig @@ -0,0 +1,130 @@ +//! The probe: the interpreter alone over made-up tapes of 60 and 128 +//! instructions, each pass at 1280x720 and 1920x1080 (and 128 at half of +//! 1280x720, as the sketch draws a long tape), and the synth kernel over +//! blocks of 256 and 1024 samples. `zig build probe` plays +//! test/probe.script with `--profile-gpu`, which prints each label's time +//! at exit; the empty tape's is the cost of a pass doing nothing. +const std = @import("std"); +const sk = @import("sketch"); +const gpu = sk.abi.gpu; +const Lang = @import("Lang"); +const shaders = @import("shaders"); + +pub const name = "live-probe"; +pub const width = 640; +pub const height = 360; + +pub const Session = struct { + images: [3]sk.Image = .{ .none, .none, .none }, + block: sk.Buffer = .none, + time: f32 = 0, +}; + +const sizes = [3][2]u32{ .{ 1280, 720 }, .{ 1920, 1080 }, .{ 640, 360 } }; + +const Picture = struct { label: []const u8, len: u32, size: u2 }; +const pictures = [_]Picture{ + .{ .label = "picture 0 720p", .len = 0, .size = 0 }, + .{ .label = "picture 60 720p", .len = 60, .size = 0 }, + .{ .label = "picture 128 720p", .len = 128, .size = 0 }, + .{ .label = "picture 0 1080p", .len = 0, .size = 1 }, + .{ .label = "picture 60 1080p", .len = 60, .size = 1 }, + .{ .label = "picture 128 1080p", .len = 128, .size = 1 }, + .{ .label = "picture 128 360p", .len = 128, .size = 2 }, +}; + +const Block = struct { label: []const u8, len: u32, count: u32, fade: bool = false }; +const blocks = [_]Block{ + .{ .label = "synth 60 x256", .len = 60, .count = 256 }, + .{ .label = "synth 128 x256", .len = 128, .count = 256 }, + .{ .label = "synth 60 x1024", .len = 60, .count = 1024 }, + .{ .label = "synth 128 x1024", .len = 128, .count = 1024 }, + .{ .label = "synth 128 x1024 fading", .len = 128, .count = 1024, .fade = true }, +}; + +/// `len` instructions, every one reaching the last: x, y, t and a +/// constant, then a cycle of ops over the three before, a noise every +/// tenth, and hsv. +fn madeUp(comptime len: u32) [Lang.max_tape][4]u32 { + @setEvalBranchQuota(10_000); + var code: [Lang.max_tape][4]u32 = @splat(.{ 0, 0, 0, 0 }); + const cycle = [_]Lang.Op{ .mul, .add, .sin, .fract, .sub, .smoothstep, .abs, .mix, .cos }; + for (0..len) |i| { + const op: Lang.Op = switch (i) { + 0 => .x, + 1 => .y, + 2 => .t, + 3 => .constant, + else => if (i == len - 1) .hsv else if (i % 10 == 9) .noise else cycle[i % cycle.len], + }; + const a: u32 = if (op == .constant) @bitCast(@as(f32, 6)) else @intCast(i -| 1); + code[i] = .{ @intFromEnum(op), a, @intCast(i -| 2), @intCast(i -| 3) }; + } + return code; +} + +const tapes = .{ madeUp(0), madeUp(60), madeUp(128) }; + +fn tape(len: u32) [Lang.max_tape][4]u32 { + return switch (len) { + 0 => tapes[0], + 60 => tapes[1], + else => tapes[2], + }; +} + +const pipeline: gpu.Pipeline = .{ + .vertex = .{ .code = shaders.fullscreen_vert }, + .fragment = .{ .code = shaders.picture_frag, .uniform_buffers = 1 }, + .stride = 0, + .attributes = &.{}, + .blend = .none, +}; +const kernel: gpu.Kernel = .{ .code = shaders.synth_comp, .readwrite_buffers = 1, .uniform_buffers = 1 }; + +pub fn draw(ss: *Session, f: *sk.Frame) void { + f.clear(0x000000); + if (!f.host.gpu()) return f.text(8, 8, 2, "no GPU here", 0xffffff); + 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.time += @floatCast(@as(f64, @floatFromInt(f.in.dt)) / std.time.ns_per_s); + for (pictures) |p| { + const size = sizes[p.size]; + f.gpu(.{ + .label = p.label, + .image = ss.images[p.size], + .width = size[0], + .height = size[1], + .pipelines = &.{pipeline}, + .draws = &.{.{ .pipeline = 0, .vertex_count = 3, .fragment_uniforms = gpu.uniform(f.scratch, Lang.Picture{ + .tape = tape(p.len), + .len = p.len, + .time = ss.time, + .size = .{ @floatFromInt(size[0]), @floatFromInt(size[1]) }, + }) }}, + }); + } + 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); + f.compute(.{ + .label = k.label, + .kernel = kernel, + .readwrite = &.{ss.block}, + .uniforms = gpu.uniform(f.scratch, Lang.Synth{ + .tape = both, + .len = k.len, + .old_len = k.len, + .count = k.count, + .fade = if (k.fade) k.count else 0, + .start = ss.time, + .rate = 48000, + }), + .groups = .{ (k.count + 63) / 64, 1, 1 }, + }); + } + f.image(ss.images[0], .{ .x = 0, .y = 0, .w = f.in.width, .h = f.in.height }, .{}); +} -- cgit v1.3 From e7679f416daeee551e81a592c7fe06b8d8f7b705 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 06:00:47 -0300 Subject: examples/live: the language, text in, a tape and a problem out Lang.compile reads lines of `name = expression` over t, x and y (+ - * /, unary minus, parentheses, numbers, # comments) and returns either a Program, the out and color tapes, or a Problem: a byte of the text and words. The builtins are one table, `builtins` (call, op, arity, value, doc): the survey's sin, cos, exp, log, pow, sqrt, abs, floor, fract, min, max, clamp, mix, step, smoothstep, atan2, noise, saw, sqr, tri, seq, hsv, rgb, tau and pi, and the inputs t, x, y. A name is defined once, before it is used; builtins cannot be renamed; hsv and rgb are colours, so only the whole color line can be one. Each tape is emitted from its line's root, depth first, each term once, so it holds only what the line reaches; t, x and y are a term each however often named, and a negative number is a number. seq's values go in registers side by side: as emitted when they are fresh numbers, else copied. noise's missing arguments are 0. Nothing is allocated: the compiler is a struct on the stack, 512 terms, 128 names, 32 levels of nesting, 17 arguments (seq's 16 values), 128 instructions a tape, about 28 KiB in all; past any of them is a problem in words ("the out line needs more than 128 instructions", at the line). vm.glsl's smoothstep is written out: GLSL leaves lo > hi undefined, and the survey's ring is smoothstep(0.03, 0, ...). Tests: a tone instruction by instruction; the survey's 20-line program (out 54 instructions, color 46, neither holding the other's terms, every operand an earlier register); every builtin; precedence, comments and numbers; 24 problems, each with its byte and words; each capacity, met and passed. zig build test: 6 of 6. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- examples/live/build.zig | 5 +- examples/live/shaders/vm.glsl | 3 +- examples/live/src/Lang.zig | 617 +++++++++++++++++++++++++++++++++++++++++- examples/live/src/probe.zig | 2 +- 4 files changed, 617 insertions(+), 10 deletions(-) (limited to 'examples/live/src/probe.zig') diff --git a/examples/live/build.zig b/examples/live/build.zig index cc93612..2ad85fb 100644 --- a/examples/live/build.zig +++ b/examples/live/build.zig @@ -11,7 +11,7 @@ pub fn build(b: *std.Build) void { const ops = b.addWriteFiles(); _ = ops.add("ops.glsl", comptime blk: { var glsl: []const u8 = "// Generated from Lang.Op by build.zig; do not edit.\n"; - for (std.enums.values(Lang.Op)) |op| glsl = glsl ++ std.fmt.comptimePrint("const uint OP_{t} = {d}u;\n", .{ op, @intFromEnum(op) }); + for (std.enums.values(Lang.Op)) |op| glsl = glsl ++ std.fmt.comptimePrint("const uint OP_{t} = {d}u;\n", .{ op, @backingInt(op) }); break :blk glsl; }); const lang = b.createModule(.{ .root_source_file = b.path("src/Lang.zig") }); @@ -29,6 +29,9 @@ pub fn build(b: *std.Build) void { .{ .name = "shaders", .module = shaders.module }, }; + const lang_test = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("src/Lang.zig"), .target = target, .optimize = optimize }) }); + b.step("test", "Run the language's tests").dependOn(&b.addRunArtifact(lang_test).step); + // The interpreter's GPU time over made-up tapes (`--profile-gpu`). const probe = sb.app(.{ .name = "live-probe", .root_source_file = b.path("src/probe.zig"), .imports = imports }); const run_probe = b.addRunArtifact(probe.addExecutable()); diff --git a/examples/live/shaders/vm.glsl b/examples/live/shaders/vm.glsl index 185d23a..06319ec 100644 --- a/examples/live/shaders/vm.glsl +++ b/examples/live/shaders/vm.glsl @@ -75,7 +75,8 @@ vec3 vm(uint base, uint len, float t, float x, float y) { case OP_clamp: v = clamp(a, b, c); break; case OP_mix: v = mix(a, b, c); break; case OP_step: v = step(a, b); break; - case OP_smoothstep: v = smoothstep(a, b, c); break; + // GLSL leaves smoothstep undefined for lo > hi; this one is not. + case OP_smoothstep: v = clamp((c - a) / (b - a), 0.0, 1.0); v = v * v * (3.0 - 2.0 * v); break; case OP_atan2: v = atan(a, b); break; case OP_noise: v = noise(vec3(a, b, c)); break; case OP_saw: v = 2.0 * fract(a + 0.5) - 1.0; break; diff --git a/examples/live/src/Lang.zig b/examples/live/src/Lang.zig index 644ff61..a6db8b9 100644 --- a/examples/live/src/Lang.zig +++ b/examples/live/src/Lang.zig @@ -1,14 +1,35 @@ //! The language: lines of `name = expression` over `t`, `x` and `y`, -//! compiled into two tapes, `out` (heard) and `color` (seen), that one -//! interpreter (shaders/vm.glsl) runs per sample and per pixel. +//! compiled into two tapes, `out` (heard, -1..1) and `color` (seen), that +//! one interpreter (shaders/vm.glsl) runs per sample and per pixel. Names +//! are shared by both lines; a name is defined once, before it is used, so +//! a program is a graph without cycles and every program ends. //! -//! A tape is at most `max_tape` instructions; instruction i writes register -//! i from earlier ones. Its GPU form is the uniform blocks at the end of -//! this file, and build.zig writes `Op` out as ops.glsl for the shaders: -//! this file is the one definition of both. +//! beat = t * 2 +//! kick = exp(-7 * fract(beat)) +//! out = sin(tau * 48 * t) * kick # heard +//! color = hsv(x, 0.5, y * kick) # seen +//! +//! `+ - * /`, unary `-` and parentheses; numbers like 0.5, 120, 1e-3; +//! `#` to the end of the line is a comment; the builtins are `builtins`. +//! +//! A tape is at most `max_tape` instructions, holding only what its line +//! reaches; instruction i writes register i from earlier ones. Its GPU +//! form is the uniform blocks at the end of this file, and build.zig +//! writes `Op` out as ops.glsl for the shaders: this file is the one +//! definition of both. `compile` allocates nothing: its work is a few +//! fixed arrays on the stack (`max_terms`, `max_names`, `max_depth`), and a +//! program past any of them is a problem like any other, in words. const std = @import("std"); pub const max_tape = 128; +/// Numbers, names used and operations in the whole text. +pub const max_terms = 512; +/// Lines that define a name. +pub const max_names = 128; +/// Parentheses and unary minuses inside each other. +pub const max_depth = 32; +/// seq's index and values. +pub const max_args = 17; /// By how many registers an instruction reads: none, one, two, three. pub const Op = enum(u32) { @@ -32,7 +53,7 @@ pub const Op = enum(u32) { /// Register `a`, so that seq's values sit side by side. copy, /// Register `b + floor(a) mod c`: `b` and `c` are numbers, not - /// registers it reads, and registers b.. hold copies. + /// registers it reads, and registers b.. hold the values. seq, add, sub, @@ -52,6 +73,59 @@ pub const Op = enum(u32) { rgb, }; +pub const Builtin = struct { + /// As it is called, for the manual. + call: []const u8, + op: Op, + /// Arguments; 0 for an input or a constant, used without parentheses. + min: u8 = 0, + max: u8 = 0, + value: f32 = 0, + doc: []const u8, + + pub fn name(b: Builtin) []const u8 { + return b.call[0 .. std.mem.indexOfScalar(u8, b.call, '(') orelse b.call.len]; + } +}; + +/// The builtins, the one table: a name is added here and in vm.glsl's +/// 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 = "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" }, + .{ .call = "cos(a)", .op = .cos, .min = 1, .max = 1, .doc = "cosine, of radians" }, + .{ .call = "exp(a)", .op = .exp, .min = 1, .max = 1, .doc = "e to the a" }, + .{ .call = "log(a)", .op = .log, .min = 1, .max = 1, .doc = "natural logarithm" }, + .{ .call = "pow(a, b)", .op = .pow, .min = 2, .max = 2, .doc = "a to the b (a not negative)" }, + .{ .call = "sqrt(a)", .op = .sqrt, .min = 1, .max = 1, .doc = "square root" }, + .{ .call = "abs(a)", .op = .abs, .min = 1, .max = 1, .doc = "a without its sign" }, + .{ .call = "floor(a)", .op = .floor, .min = 1, .max = 1, .doc = "the whole number at or below a" }, + .{ .call = "fract(a)", .op = .fract, .min = 1, .max = 1, .doc = "a - floor(a): 0..1, again every 1" }, + .{ .call = "min(a, b)", .op = .min, .min = 2, .max = 2, .doc = "the smaller" }, + .{ .call = "max(a, b)", .op = .max, .min = 2, .max = 2, .doc = "the larger" }, + .{ .call = "clamp(v, lo, hi)", .op = .clamp, .min = 3, .max = 3, .doc = "v kept between lo and hi" }, + .{ .call = "mix(a, b, w)", .op = .mix, .min = 3, .max = 3, .doc = "a + (b - a) * w" }, + .{ .call = "step(edge, v)", .op = .step, .min = 2, .max = 2, .doc = "0 below edge, 1 from it on" }, + .{ .call = "smoothstep(lo, hi, v)", .op = .smoothstep, .min = 3, .max = 3, .doc = "0 to 1, smoothly, as v goes from lo to hi (either way round)" }, + .{ .call = "atan2(y, x)", .op = .atan2, .min = 2, .max = 2, .doc = "the angle of the point (x, y), radians" }, + .{ .call = "noise(x, y, z)", .op = .noise, .min = 1, .max = 3, .doc = "smooth noise, -1..1, a new value about every 1; y and z are 0 when left out" }, + .{ .call = "saw(p)", .op = .saw, .min = 1, .max = 1, .doc = "a sawtooth, -1..1, of period 1 (cycles, not radians), rising through 0 at 0" }, + .{ .call = "sqr(p)", .op = .sqr, .min = 1, .max = 1, .doc = "a square wave, 1 then -1, of period 1" }, + .{ .call = "tri(p)", .op = .tri, .min = 1, .max = 1, .doc = "a triangle wave, -1..1, of period 1, rising through 0 at 0" }, + .{ .call = "seq(i, v0, v1, ...)", .op = .seq, .min = 2, .max = max_args, .doc = "the value at floor(i), round the list: a step pattern (16 values at most)" }, + .{ .call = "hsv(h, s, v)", .op = .hsv, .min = 3, .max = 3, .doc = "a colour: hue round the wheel 0..1, saturation, value; only as the whole color line" }, + .{ .call = "rgb(r, g, b)", .op = .rgb, .min = 3, .max = 3, .doc = "a colour, red, green and blue 0..1; only as the whole color line" }, +}; + +fn builtin(name: []const u8) ?Builtin { + for (builtins) |b| if (std.mem.eql(u8, b.name(), name)) return b; + return null; +} + pub const Instruction = extern struct { op: Op = .constant, a: u32 = 0, @@ -62,6 +136,328 @@ pub const Instruction = extern struct { pub const Tape = struct { len: u32 = 0, code: [max_tape]Instruction = @splat(.{}), + + pub fn slice(tape: *const Tape) []const Instruction { + return tape.code[0..@min(tape.len, max_tape)]; + } +}; + +/// What is wrong with a program, and where: a byte of its text. +pub const Problem = struct { + at: u32 = 0, + len: u32 = 0, + words: [96]u8 = @splat(0), + + pub fn text(p: *const Problem) []const u8 { + return p.words[0..@min(p.len, p.words.len)]; + } +}; + +pub const Program = struct { out: Tape = .{}, color: Tape = .{} }; + +pub const Result = union(enum) { program: Program, problem: Problem }; + +pub fn compile(text: []const u8) Result { + var c: Compiler = .{ .text = text }; + 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 }; + return .{ .program = program }; +} + +const none = std.math.maxInt(u16); + +const Term = struct { + op: Op, + value: f32 = 0, + /// Where it was written. + at: u32, + len: u8 = 0, + args: [max_args]u16 = undefined, +}; + +const Name = struct { text: []const u8, term: u16, at: u32 }; + +const Token = struct { + kind: enum { name, number, symbol, newline, end }, + at: u32, + end: u32, + symbol: u8 = 0, + + fn is(tok: Token, symbol: u8) bool { + return tok.kind == .symbol and tok.symbol == symbol; + } +}; + +const Compiler = struct { + text: []const u8, + pos: u32 = 0, + terms: [max_terms]Term = undefined, + term_count: u16 = 0, + names: [max_names]Name = undefined, + name_count: u16 = 0, + /// The term of t, x and y, made once. + inputs: [3]u16 = .{ none, none, none }, + depth: u8 = 0, + /// The line's hsv or rgb, if it called one. + colour: u16 = none, + problem: Problem = .{}, + + fn fail(c: *Compiler, at: u32, comptime fmt: []const u8, args: anytype) error{Problem} { + c.problem = .{ .at = at }; + var w: std.Io.Writer = .fixed(&c.problem.words); + w.print(fmt, args) catch {}; + c.problem.len = @intCast(w.end); + return error.Problem; + } + + fn span(c: *const Compiler, tok: Token) []const u8 { + return c.text[tok.at..tok.end]; + } + + fn next(c: *Compiler) Token { + const s = c.text; + var i = c.pos; + while (i < s.len and (s[i] == ' ' or s[i] == '\t' or s[i] == '\r')) i += 1; + if (i < s.len and s[i] == '#') while (i < s.len and s[i] != '\n') : (i += 1) {}; + const at = i; + defer c.pos = i; + if (i == s.len) return .{ .kind = .end, .at = at, .end = at }; + switch (s[i]) { + '\n' => { + i += 1; + return .{ .kind = .newline, .at = at, .end = i }; + }, + 'a'...'z', 'A'...'Z', '_' => { + while (i < s.len and (std.ascii.isAlphanumeric(s[i]) or s[i] == '_')) i += 1; + return .{ .kind = .name, .at = at, .end = i }; + }, + '0'...'9', '.' => { + while (i < s.len and (std.ascii.isDigit(s[i]) or s[i] == '.' or s[i] == 'e' or + ((s[i] == '-' or s[i] == '+') and s[i - 1] == 'e'))) i += 1; + return .{ .kind = .number, .at = at, .end = i }; + }, + else => { + i += 1; + return .{ .kind = .symbol, .at = at, .end = i, .symbol = s[at] }; + }, + } + } + + fn peek(c: *Compiler) Token { + const pos = c.pos; + defer c.pos = pos; + return c.next(); + } + + fn describe(c: *const Compiler, tok: Token) []const u8 { + return switch (tok.kind) { + .newline, .end => "the end of the line", + else => c.span(tok), + }; + } + + fn term(c: *Compiler, t: Term) !u16 { + if (c.term_count == max_terms) return c.fail(t.at, "the program is longer than {d} terms", .{max_terms}); + c.terms[c.term_count] = t; + c.term_count += 1; + return c.term_count - 1; + } + + fn call(c: *Compiler, op: Op, at: u32, args: []const u16) !u16 { + var t: Term = .{ .op = op, .at = at, .len = @intCast(args.len) }; + @memcpy(t.args[0..args.len], args); + return c.term(t); + } + + fn lookup(c: *const Compiler, name: []const u8) ?Name { + for (c.names[0..c.name_count]) |n| if (std.mem.eql(u8, n.text, name)) return n; + return null; + } + + fn lines(c: *Compiler) !void { + while (true) { + const first = c.next(); + switch (first.kind) { + .end => return, + .newline => continue, + .name => {}, + else => return c.fail(first.at, "a line is a name, =, and an expression", .{}), + } + const name = c.span(first); + const eq = c.next(); + if (!eq.is('=')) return c.fail(eq.at, "expected = after {s}, found {s}", .{ name, c.describe(eq) }); + 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 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"))) + return c.fail(c.terms[c.colour].at, "a colour (hsv, rgb) can only be the whole color line", .{}); + if (c.name_count == max_names) return c.fail(first.at, "more than {d} names", .{max_names}); + c.names[c.name_count] = .{ .text = name, .term = root, .at = first.at }; + c.name_count += 1; + } + } + + fn enter(c: *Compiler, at: u32) !void { + if (c.depth == max_depth) return c.fail(at, "nested more than {d} deep", .{max_depth}); + c.depth += 1; + } + + fn expr(c: *Compiler) error{Problem}!u16 { + try c.enter(c.peek().at); + 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() }); + } + } + + fn product(c: *Compiler) !u16 { + 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() }); + } + } + + fn unary(c: *Compiler) error{Problem}!u16 { + const tok = c.peek(); + if (!tok.is('-')) return c.primary(); + _ = c.next(); + try c.enter(tok.at); + defer c.depth -= 1; + const a = try c.unary(); + // 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.call(.neg, tok.at, &.{a}); + } + + fn primary(c: *Compiler) !u16 { + const tok = c.next(); + 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 }); + }, + .name => { + const name = c.span(tok); + if (c.peek().is('(')) return 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; + } + 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 }); + const input: ?usize = switch (b.op) { + .t => 0, + .x => 1, + .y => 2, + else => null, + }; + const i = input orelse return 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]; + }, + .newline, .end => return c.fail(tok.at, "expected an expression before the end of the line", .{}), + .symbol => { + if (!tok.is('(')) { + if (c.text[tok.at] < 0x20 or c.text[tok.at] > 0x7e) return c.fail(tok.at, "byte 0x{x:0>2} is not part of the language", .{c.text[tok.at]}); + return c.fail(tok.at, "expected an expression, found {s}", .{c.span(tok)}); + } + 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; + }, + } + } + + /// `name(args)`, `name` being `tok`. + fn callOf(c: *Compiler, tok: Token) !u16 { + const name = c.span(tok); + _ = c.next(); + const b = builtin(name) orelse { + if (c.lookup(name) != null) return c.fail(tok.at, "{s} is a name, not a builtin: it takes no ( )", .{name}); + return c.fail(tok.at, "{s} is not a builtin", .{name}); + }; + if (b.max == 0) return c.fail(tok.at, "{s} takes no arguments", .{name}); + var args: [max_args]u16 = undefined; + var n: u8 = 0; + if (c.peek().is(')')) { + _ = c.next(); + } else while (true) { + const at = c.peek().at; + if (n == b.max) return c.fail(at, "{s} takes at most {d} arguments: {s}", .{ name, b.max, b.call }); + args[n] = try c.expr(); + n += 1; + const sep = c.next(); + if (sep.is(')')) break; + if (!sep.is(',')) return c.fail(sep.at, "expected , or ) in {s}(...), found {s}", .{ name, c.describe(sep) }); + } + if (n < b.min) return c.fail(tok.at, "{s} takes {d} arguments, not {d}: {s}", .{ name, b.min, n, b.call }); + if (b.op == .noise) while (n < 3) : (n += 1) { + args[n] = try c.term(.{ .op = .constant, .at = tok.at }); + }; + const t = try c.call(b.op, tok.at, args[0..n]); + if (b.op == .hsv or b.op == .rgb) c.colour = t; + 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; + into.len += 1; + return into.len - 1; + } + + fn emit(c: *const Compiler, into: *Tape, at: u16, registers: *[max_terms]u16) error{Full}!u32 { + if (registers[at] != none) return registers[at]; + const t = c.terms[at]; + var r: [max_args]u32 = @splat(0); + for (t.args[0..t.len], 0..) |arg, i| r[i] = try c.emit(into, arg, registers); + const i: Instruction = switch (t.op) { + .constant => .{ .op = .constant, .a = @bitCast(t.value) }, + .seq => seq: { + // The values in registers side by side: as they are when + // each was emitted just now, else copied. + const values = r[1..t.len]; + var first = values[0]; + for (values, 0..) |v, k| { + if (v != values[0] + k) { + first = @intCast(into.len); + for (values) |each| _ = try push(into, .{ .op = .copy, .a = each }); + break; + } + } + break :seq .{ .op = .seq, .a = r[0], .b = first, .c = @intCast(values.len) }; + }, + else => .{ .op = t.op, .a = r[0], .b = r[1], .c = r[2] }, + }; + const register = try push(into, i); + registers[at] = @intCast(register); + return register; + } }; /// picture.frag's uniforms (SDL_GPU's fragment set). @@ -93,3 +489,210 @@ pub const Synth = extern struct { pub const glsl = .{ .uniform = .{ .set = 2, .instance = "u" } }; }; + +// --------------------------------------------------------------------------- +// Tests. + +const testing = std.testing; + +fn compiled(text: []const u8) !Program { + return switch (compile(text)) { + .program => |p| p, + .problem => |p| { + std.debug.print("unexpected problem at {d}: {s}\n", .{ p.at, p.text() }); + return error.TestUnexpectedResult; + }, + }; +} + +fn ops(tape: Tape) [max_tape]Op { + var out: [max_tape]Op = @splat(.constant); + for (tape.slice(), 0..) |i, k| out[k] = i.op; + return out; +} + +fn expectOps(tape: Tape, want: []const Op) !void { + try testing.expectEqualSlices(Op, want, ops(tape)[0..tape.len]); +} + +/// The survey's program (reports/demo1.md, ยง1 A). +const survey = + \\# the beat, shared by sound and picture + \\bpm = 120 + \\beat = t * bpm / 60 + \\kick = exp(-7 * fract(beat)) + \\bar = beat / 4 + \\# sound: out is what you hear, -1..1 + \\root = seq(bar, 55, 55, 65.4, 49) + \\bass = saw(root * t) * (1 - kick) * 0.25 + \\thump = sin(tau * 48 * t * (1 + 2 * kick)) * kick * 0.5 + \\hat = noise(t * 9000) * exp(-30 * fract(beat * 2 + 0.5)) * 0.08 + \\out = bass + thump + hat + \\# picture: color is what you see; x and y run 0..1 + \\dx = x - 0.5 + \\dy = y - 0.5 + \\r = sqrt(dx * dx + dy * dy) + \\ring = smoothstep(0.03, 0, abs(r - 0.2 - 0.1 * kick)) + \\glow = 0.15 * noise(x * 6, y * 6, t * 0.5) + \\color = hsv(fract(bar / 4 + r), 0.6, ring + glow) + \\ +; + +test "a tone, instruction by instruction" { + const p = try compiled("out = sin(tau * 220 * t) * 0.3"); + try expectOps(p.out, &.{ .constant, .constant, .mul, .t, .mul, .sin, .constant, .mul }); + const code = p.out.slice(); + try testing.expectEqual(@as(u32, @bitCast(@as(f32, std.math.tau))), code[0].a); + try testing.expectEqual(@as(u32, @bitCast(@as(f32, 220))), code[1].a); + try testing.expectEqual(Instruction{ .op = .mul, .a = 0, .b = 1 }, code[2]); + try testing.expectEqual(Instruction{ .op = .mul, .a = 2, .b = 3 }, code[4]); + try testing.expectEqual(Instruction{ .op = .sin, .a = 4 }, code[5]); + try testing.expectEqual(Instruction{ .op = .mul, .a = 5, .b = 6 }, code[7]); + try testing.expectEqual(@as(u32, 0), p.color.len); +} + +test "the survey's program: each tape holds only what its line reaches" { + const p = try compiled(survey); + const heard = ops(p.out)[0..p.out.len]; + const seen = ops(p.color)[0..p.color.len]; + // The sound never reads x or y, nor is coloured; the picture has no + // bass line or hat. + for (heard) |op| try testing.expect(op != .x and op != .y and op != .hsv); + for (seen) |op| try testing.expect(op != .saw and op != .seq and op != .sin); + try testing.expectEqual(Op.add, heard[heard.len - 1]); + try testing.expectEqual(Op.hsv, seen[seen.len - 1]); + // t is one instruction however often it is named; seq's four numbers + // were emitted side by side, so there are no copies. + try testing.expectEqual(@as(usize, 1), std.mem.count(Op, heard, &.{.t})); + try testing.expectEqual(@as(usize, 0), std.mem.count(Op, heard, &.{.copy})); + try testing.expectEqual(@as(u32, 54), p.out.len); + try testing.expectEqual(@as(u32, 46), p.color.len); + // Every operand is an earlier register. + for ([_]Tape{ p.out, p.color }) |tape| for (tape.slice(), 0..) |i, k| { + if (@backingInt(i.op) < @backingInt(Op.neg)) continue; + try testing.expect(i.a < k); + if (i.op == .seq) { + try testing.expect(i.b + i.c <= k); + } else if (@backingInt(i.op) >= @backingInt(Op.add)) try testing.expect(i.b < k); + }; +} + +test "every builtin" { + for (builtins) |b| { + var buf: [128]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + try w.print("{s} = {s}", .{ if (b.op == .hsv or b.op == .rgb) "color" else "out", b.name() }); + if (b.max > 0) { + try w.writeAll("(t"); + for (1..b.min) |_| try w.writeAll(", t * 2"); + try w.writeAll(")"); + } + const p = try compiled(w.buffered()); + const tape = if (b.op == .hsv or b.op == .rgb) p.color else p.out; + const last = tape.slice()[tape.len - 1]; + try testing.expectEqual(b.op, last.op); + if (b.op == .constant) try testing.expectEqual(@as(u32, @bitCast(b.value)), last.a); + } + // noise's missing arguments are 0; seq copies values that were not + // emitted side by side. + const p = try compiled("a = t * 3\nout = noise(t) + seq(t, 1, a, 2)"); + try expectOps(p.out, &.{ .t, .constant, .constant, .noise, .constant, .constant, .mul, .constant, .copy, .copy, .copy, .seq, .add }); + try testing.expectEqual(Instruction{ .op = .seq, .a = 0, .b = 8, .c = 3 }, p.out.slice()[11]); + try testing.expectEqual(Instruction{ .op = .copy, .a = 6 }, p.out.slice()[9]); +} + +test "precedence, unary minus, numbers, comments, blank lines" { + const p = try compiled("\n # nothing\n a = 1 + 2 * -t / 4 - (5 - -6) # trailing\n\nout = a + 1e-3\n"); + // 1 + ((2 * -t) / 4) - (5 - -6) + try expectOps(p.out, &.{ .constant, .constant, .t, .neg, .mul, .constant, .div, .add, .constant, .constant, .sub, .sub, .constant, .add }); + try testing.expectEqual(@as(u32, @bitCast(@as(f32, -6))), p.out.slice()[9].a); + try testing.expectEqual(@as(u32, @bitCast(@as(f32, 1e-3))), p.out.slice()[12].a); +} + +test "every problem, with its byte" { + const cases = [_]struct { text: []const u8, at: u32, words: []const u8 }{ + .{ .text = "= 1", .at = 0, .words = "a line is a name, =, and an expression" }, + .{ .text = "out 1", .at = 4, .words = "expected = after out, found 1" }, + .{ .text = "out", .at = 3, .words = "expected = after out, found the end of the line" }, + .{ .text = "sin = 1", .at = 0, .words = "sin is a builtin: call yours something else" }, + .{ .text = "a = 1\na = 2", .at = 6, .words = "a is defined twice: a name is given once" }, + .{ .text = "out = 1 2", .at = 8, .words = "expected the end of the line, found 2" }, + .{ .text = "out = rgb(1, 1, 1)", .at = 6, .words = "a colour (hsv, rgb) can only be the whole color line" }, + .{ .text = "color = hsv(1, 1, 1) * 2", .at = 8, .words = "a colour (hsv, rgb) can only be the whole color line" }, + .{ .text = "color = hsv(1, 1, 1)\nout = color", .at = 27, .words = "color is a colour: only the color line can be one" }, + .{ .text = "out = 1.2.3", .at = 6, .words = "1.2.3 is not a number" }, + .{ .text = "out = kick\nkick = 1", .at = 6, .words = "kick is not defined above" }, + .{ .text = "out = sin", .at = 6, .words = "sin takes arguments: sin(a)" }, + .{ .text = "out = 1 +", .at = 9, .words = "expected an expression before the end of the line" }, + .{ .text = "out = * 2", .at = 6, .words = "expected an expression, found *" }, + .{ .text = "out = 1 $ 2", .at = 8, .words = "expected the end of the line, found $" }, + .{ .text = "out = \x01", .at = 6, .words = "byte 0x01 is not part of the language" }, + .{ .text = "out = (1 + 2", .at = 12, .words = "expected ) to close the ( at byte 6, found the end of the line" }, + .{ .text = "a = 1\nout = a(2)", .at = 12, .words = "a is a name, not a builtin: it takes no ( )" }, + .{ .text = "out = foo(2)", .at = 6, .words = "foo is not a builtin" }, + .{ .text = "out = t(2)", .at = 6, .words = "t takes no arguments" }, + .{ .text = "out = sin(1, 2)", .at = 13, .words = "sin takes at most 1 arguments: sin(a)" }, + .{ .text = "out = sin(1 2)", .at = 12, .words = "expected , or ) in sin(...), found 2" }, + .{ .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)) { + .program => { + std.debug.print("{s}: no problem\n", .{case.text}); + return error.TestUnexpectedResult; + }, + .problem => |p| { + testing.expectEqualStrings(case.words, p.text()) catch |e| { + std.debug.print("in {s}\n", .{case.text}); + return e; + }; + try testing.expectEqual(case.at, p.at); + }, + }; +} + +test "past the capacities: problems in words" { + var buf: [8192]u8 = undefined; + // A tape of 128 fits; one more instruction does not. + var w: std.Io.Writer = .fixed(&buf); + try w.writeAll("out = t"); + for (0..63) |_| try w.writeAll(" + 1"); + const fits = try compiled(w.buffered()); + try testing.expectEqual(@as(u32, 127), fits.out.len); + try w.writeAll(" + 1"); + switch (compile(w.buffered())) { + .program => return error.TestUnexpectedResult, + .problem => |p| { + try testing.expectEqualStrings("the out line needs more than 128 instructions", p.text()); + try testing.expectEqual(@as(u32, 0), p.at); + }, + } + // More terms than the compiler keeps, though no tape reaches them. + w = .fixed(&buf); + for (0..300) |_| try w.writeAll("1+"); + try w.writeAll("1"); + const long = w.buffered(); + var text: [8192]u8 = undefined; + const unused = try std.fmt.bufPrint(&text, "a = {s}\n", .{long}); + switch (compile(unused)) { + .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())) { + .program => return error.TestUnexpectedResult, + .problem => |p| try testing.expectEqualStrings("more than 128 names", p.text()), + } + // Depth. + w = .fixed(&buf); + try w.writeAll("out = "); + for (0..40) |_| try w.writeAll("("); + try w.writeAll("1"); + for (0..40) |_| try w.writeAll(")"); + switch (compile(w.buffered())) { + .program => return error.TestUnexpectedResult, + .problem => |p| try testing.expectEqualStrings("nested more than 32 deep", p.text()), + } +} diff --git a/examples/live/src/probe.zig b/examples/live/src/probe.zig index 663bed3..5f6e8ee 100644 --- a/examples/live/src/probe.zig +++ b/examples/live/src/probe.zig @@ -58,7 +58,7 @@ fn madeUp(comptime len: u32) [Lang.max_tape][4]u32 { else => if (i == len - 1) .hsv else if (i % 10 == 9) .noise else cycle[i % cycle.len], }; const a: u32 = if (op == .constant) @bitCast(@as(f32, 6)) else @intCast(i -| 1); - code[i] = .{ @intFromEnum(op), a, @intCast(i -| 2), @intCast(i -| 3) }; + code[i] = .{ @backingInt(op), a, @intCast(i -| 2), @intCast(i -| 3) }; } return code; } -- 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/src/probe.zig') 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/src/probe.zig') 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