summaryrefslogtreecommitdiff
path: root/examples/live/src/main.zig
blob: 965152aa53172679fa3ba656393d593fdcb53d52 (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
//! 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");

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
    // deleting the last; part 2 puts pardes's editor here.
    text: [4096]u8 = @splat(0),
    len: u32 = 0,
    /// The last program that parsed.
    out: Lang.Tape = .{},
    color: 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.
    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 `<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;
}

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;
            },
            .@"return", .keypad_enter => if (s.len < s.text.len) {
                s.text[s.len] = '\n';
                s.len += 1;
                edited = true;
            },
            else => {},
        },
        else => {},
    };
    if (!edited) return;
    switch (Lang.compile(s.text[0..s.len])) {
        .program => |p| {
            s.out = p.out;
            s.color = p.color;
            s.problem = .{};
        },
        .problem => |p| s.problem = p,
    }
}

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 {
    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 half: u32 = if (s.color.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, &s.color.code),
            .len = s.color.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 new = std.mem.sliceAsBytes(s.out.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));
        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),
                .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 = 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;
const red = 0xff5a50;

/// The text over the picture on a dark band, the caret at its end, and the
/// problem: a red mark under its byte and its words under the text.
// 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 cell = f.host.textSizeIn(.code, scale, "M");
    const lines = std.mem.count(u8, text, "\n") + 1;
    var widest: f32 = 0;
    var it = std.mem.splitScalar(u8, text, '\n');
    while (it.next()) |line| widest = @max(widest, f.host.textSizeIn(.code, scale, line)[0]);
    const problem = s.problem.len > 0;
    if (problem) widest = @max(widest, f.host.textSizeIn(.code, scale, s.problem.text())[0]);
    const band_h = @as(f32, @floatFromInt(lines + @intFromBool(problem))) * cell[1] + 2 * margin;
    f.fill(.{ .x = 0, .y = 0, .w = @max(widest, 24 * cell[0]) + 2 * margin + cell[0], .h = band_h }, 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]) {
        f.textIn(.code, margin, y, scale, line, ink);
        const end = start + line.len;
        if (problem and s.problem.at >= start and s.problem.at <= end) {
            const x = margin + f.host.textSizeIn(.code, scale, text[start..s.problem.at])[0];
            f.fill(.{ .x = x, .y = y + cell[1] - 2, .w = cell[0], .h = 2 }, red);
        }
        // The caret, after the last byte.
        if (end == text.len) f.fill(.{ .x = margin + f.host.textSizeIn(.code, scale, line)[0], .y = y, .w = 2, .h = cell[1] }, ink);
        start = end + 1;
    }
    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 - 260, 12, 1, ink, "{s} {d:.1} s  lead {d:.1} ms", .{ mode, s.time, lead });
}