diff options
Diffstat (limited to 'examples/live/src/main.zig')
| -rw-r--r-- | examples/live/src/main.zig | 36 |
1 files changed, 27 insertions, 9 deletions
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 }, |
