summaryrefslogtreecommitdiff
path: root/examples/live/src/main.zig
blob: d7a514ff37aa698ecddc65d500b1eda7a52129b9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//! 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. 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");
const pe = @import("pardes-editor");

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;

/// 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 {
    ed: Editor = .{},
    /// 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 = .{},
    /// 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.
    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,
};

/// 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 },
    block: sk.Buffer = .none,
    /// The out tape and the one it replaces, for synth.comp.
    tapes: sk.Buffer = .none,
    /// Seconds: where the next block starts.
    clock: f64 = 0,
    /// The tape the last block played.
    heard: Lang.Tape = .{},
    scope: scope.Scope = .{},
    /// `capture` appends what is heard to `<stem>.capture.f32`.
    capturing: bool = false,
};

/// `capture` starts or stops appending what is heard to the sketch's file
/// `capture.f32` (mono f32 at the device's rate, beside its save).
pub fn command(ss: *Session, c: *sk.Console, line: []const u8) !bool {
    if (!std.mem.eql(u8, std.mem.trim(u8, line, " \t"), "capture")) return false;
    ss.capturing = !ss.capturing;
    c.log(if (ss.capturing) "capturing what is heard to capture.f32" else "capture stopped");
    return true;
}

/// 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;

/// The language's grammar (grammar/, its parser.c compiled in by build.zig),
/// handed to the editor at run time.
pub extern fn tree_sitter_live() *const pe.syntax.ts.Language;
pub fn language() pe.Language {
    return .{ .name = "live", .ext = ".live", .ts = tree_sitter_live(), .highlights = @embedFile("highlights.scm"), .indents = @embedFile("indents.scm") };
}

/// The text's parse, for the editor's node walks (Alt-o, Alt-i, ...):
/// derived, so not in State, and kept between calls, as Alt-i goes back the
/// way Alt-o came only through its history; forgotten at a jump (`draw`).
/// A reload starts it again, with the code its grammar is in.
// ponytail: the tree a reload leaves behind is leaked (tree-sitter's
// malloc, a few KiB a reload).
var syn: pe.Syntax = .{};

pub fn update(s: *State, in: *const sk.Input) void {
    const revision = s.ed.revision;
    syn.lang = language();
    // 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) {
                    chord = false;
                    continue;
                }
                break :text .{ .cp = @intCast(@min(e.value, 0x10ffff)), .text = utf8[0..n] };
            },
            .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,
        };
        // 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);
        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), null)) {
        .program => |p| {
            s.out = p.out;
            s.color = p.color;
            s.problem = .{};
        },
        .problem => |p| s.problem = p,
    }
}

/// 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 },
    .fragment = .{ .code = shaders.picture_frag, .uniform_buffers = 1 },
    .stride = 0,
    .attributes = &.{},
    .blend = .none,
};

pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void {
    if (f.in.jumped) syn.forget();
    f.clear(0x000000);
    if (!f.host.gpu()) {
        f.text(16, 16, 2, "live needs a GPU: the host has none.", 0xff8080);
        return;
    }
    // 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 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);
        ss.picture = f.host.newImage(size[0], size[1]);
        ss.size = size;
    }
    f.gpu(.{
        .label = "picture",
        .image = ss.picture,
        .width = size[0],
        .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, &seen.code),
            .len = seen.len,
            .time = s.time,
            .size = .{ @floatFromInt(size[0]), @floatFromInt(size[1]) },
        }) }},
    });
    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,
    // 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 want: u32 = @min(in.lead -| queued, max_block);
    if (want > 0) {
        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.* = .{ heard.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 = heard.len,
                .old_len = ss.heard.len,
                .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 },
        });
        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 = heard.*;
    }
    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;
/// 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;
const selected: sk.Color = .{ .r = 0x3c, .g = 0x6e, .b = 0xc8, .a = 170 };
const also_selected: sk.Color = .{ .r = 0x3c, .g = 0x6e, .b = 0xc8, .a = 90 };
/// pardes's own theme: its text's ink and a class's.
const theme = &pe.syntax.themes[0];

/// The editor's text over the picture on a dark band: its lines coloured
/// by the grammar in pardes's theme, every
/// range (the primary stronger), a caret at each range's head that says
/// the mode (a block in normal and select, a bar in insert), the mode's
/// name in the corner; the problem as a red mark under its byte and its
/// words under the text.
// ponytail: one face at one size, a column a fixed advance and a byte of
// this ASCII language a column; sketchbook's paragraph call (spans, any
// byte's place) takes the place of the measuring and the textIn here.
fn code(s: *const State, f: *sk.Frame) void {
    const text = s.ed.bytes(f.scratch) catch return;
    const ranges = f.scratch.create([pe.max_selections]pe.Selection) catch return;
    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 problem = s.problem.len > 0;
    var lines: usize = 1;
    var widest: usize = 24;
    var it = std.mem.splitScalar(u8, text, '\n');
    while (it.next()) |line| : (lines += 1) widest = @max(widest, line.len + 1);
    if (problem) widest = @max(widest, s.problem.len);
    f.fill(.{ .x = 0, .y = 0, .w = @as(f32, @floatFromInt(widest)) * cell[0] + 2 * margin, .h = @as(f32, @floatFromInt(lines - 1 + @intFromBool(problem))) * cell[1] + 2 * margin }, sk.Color{ .r = 0, .g = 0, .b = 0, .a = 150 });

    var y: f32 = margin;
    var start: usize = 0;
    it = std.mem.splitScalar(u8, text, '\n');
    while (it.next()) |line| : (y += cell[1]) {
        // a line's bytes and its newline, start..end
        const end = start + line.len + 1;
        for (ranges[0..got.n], 0..) |r, i| {
            const from = @max(@min(r.anchor, r.head), start);
            const to = @min(@max(r.anchor, r.head), end);
            if (from < to) f.fill(.{ .x = margin + @as(f32, @floatFromInt(from - start)) * cell[0], .y = y, .w = @as(f32, @floatFromInt(to - from)) * cell[0], .h = cell[1] }, if (i == got.primary) selected else also_selected);
        }
        // a run of bytes of one class at a time, in the theme's inks
        var a: usize = 0;
        while (a < line.len) {
            const class = if (start + a < colors.len) colors[start + a] else .none;
            var b = a + 1;
            while (b < line.len and (if (start + b < colors.len) colors[start + b] else .none) == class) b += 1;
            const c = pe.syntax.synInk(theme, class) orelse pe.syntax.ink(theme);
            f.textIn(.code, margin + @as(f32, @floatFromInt(a)) * cell[0], y, scale, line[a..b], sk.Color{ .r = c[0], .g = c[1], .b = c[2] });
            a = b;
        }
        for (ranges[0..got.n], 0..) |r, i| {
            // the head's character: the one before it in a range that
            // grows forward
            const at = if (r.head > r.anchor) r.head - 1 else r.head;
            if (at < start or at >= end) continue;
            const x = margin + @as(f32, @floatFromInt(at - start)) * cell[0];
            const caret: sk.Color = .{ .r = 0xe8, .g = 0xe8, .b = 0xe8, .a = if (i == got.primary) 200 else 110 };
            if (s.ed.mode() == .insert) {
                f.fill(.{ .x = x - 1, .y = y, .w = 2, .h = cell[1] }, caret);
            } else f.fill(.{ .x = x, .y = y, .w = cell[0], .h = cell[1] }, caret.alpha(caret.a / 2));
        }
        if (problem and s.problem.at >= start and s.problem.at < end)
            f.fill(.{ .x = margin + @as(f32, @floatFromInt(s.problem.at - start)) * cell[0], .y = y + cell[1] - 2, .w = cell[0], .h = 2 }, red);
        start = end;
    }
    if (problem) f.textIn(.code, margin, y, scale, s.problem.text(), red);

    const mode = switch (f.in.mode) {
        .live => "live",
        .recording => "recording",
        .playing => "replay",
        .paused => "paused",
    };
    const lead = @as(f32, @floatFromInt(f.in.lead)) / @as(f32, @floatFromInt(f.in.sample_rate)) * 1000;
    f.print(width - 300, 12, 1, ink, "{s}  {s} {d:.1} s  lead {d:.1} ms", .{ switch (s.ed.mode()) {
        .normal => "NOR",
        .insert => "INS",
        .select => "SEL",
    }, mode, s.time, lead });
}