From a1ae80096bd69ee7d92ea9e4564e960d8553395e Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 00:38:45 -0300 Subject: micscope off the Store, its takes files written through Host.appendFile The library could read a file and not write one, so micscope kept its takes in the Store: a tape appended every frame while recording, which a save of 64 MiB could not hold (under six minutes of 48 kHz audio, and micscope allows ten-minute takes). Now a sketch has one call that writes: f.host.appendFile(name, bytes) appends to `.`, beside the state file (in .sketchbook/ under `zig build run`), creating it, and returns the file's path for readFile, or "" when nothing was written. It is pointer and length in, a C-callconv entry in the vtable like newSound. Only draw and command have a host, so a write is not replayed, like sound. The web host writes nothing and returns "". readFile still maps a file once, so what is appended after is not in what it returned. A scripted --fresh run works on `.`; the files it appends to are `..`, and the exit now moves every file of the run (found by that prefix in the state's directory) to the given name, or removes them for an export, where it moved the state and its store by two fixed suffixes. micscope: State lists the takes (sk.List(Take, 64): id, source) and the counter, next_id; `update` adds a take when recording starts and removes one on delete. Each take's samples are `take-.f32` (mono f32 at the host's rate), appended to by draw as it records and by `open`, which also puts them straight into Session (the web keeps no files). Session holds the tape (every take's samples, read from their files once a process) and a clip per take (start, len, and heard, the playhead, which a restart no longer keeps); draw makes the tape follow State: a take State lost leaves it, one it lacks is read from its file. A deleted take's file stays, so a seek back finds it again; after a jump (start, seek, load, set, reload) draw moves next_id past take files already written (--fresh keeps them, and a seek back finds the future's) with a `next-id N` command. Gone: the Store, the remove request, the `takes N` command that told State the Store's count, adoptTape. Old micscope.state.store files are not read: their takes stay in them. README: appendFile among the host calls, a paragraph on it, and the private run's appended files. Tests: zig build test, 164/164 steps, 133/133 tests (one new: appendFile appends beside the state and names the file). micscope's zig build test, 67/67 steps, 10/10 tests; transport.script reads `takes.len`, loses the miscounted-takes check with its mechanism, and gains a deleted take read back from its file after `load` and the counter moved past written files. Pictures: the four scripts' 18 screenshots before and after, compared by pixel: the differences are where two runs of one build differ by themselves (playheads, the microphone's take lengths, the live view). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- examples/micscope/src/main.zig | 247 +++++++++++++++++++++-------------------- 1 file changed, 129 insertions(+), 118 deletions(-) (limited to 'examples/micscope/src') diff --git a/examples/micscope/src/main.zig b/examples/micscope/src/main.zig index 3b44ba0..ba7d678 100644 --- a/examples/micscope/src/main.zig +++ b/examples/micscope/src/main.zig @@ -9,11 +9,13 @@ //! speakers. //! //! Memory by lifetime: State is the transport, the selection and the views, -//! changed by `update` from keys and the mouse, and replayable. Store holds -//! the takes, on disk: they are still there after a restart. Session is -//! this process's playback cursor, the microphone's recent past and GPU -//! objects. Transport changes, seeks and deletions are `sk.Request`s, which -//! `draw` carries out once each. +//! the takes (their ids and names), changed by `update` from keys and the +//! mouse, and replayable. A take's samples are a file of its own, written +//! by `draw` as it records and by `open` (see `Host.appendFile`): they are +//! still there after a restart. Session is this process's copy of the +//! takes' samples, the playback cursor, the microphone's recent past and +//! GPU objects. Transport changes and seeks are `sk.Request`s, which `draw` +//! carries out once each. const std = @import("std"); const sk = @import("sketch"); const gpu = sk.abi.gpu; @@ -27,7 +29,7 @@ const tuner = @import("tuner.zig"); pub const name = "micscope"; pub const width = 1280; pub const height = 900; -pub const commands = .{ "open", "takes" }; +pub const commands = .{ "open", "next-id" }; /// The longest take: ten minutes at the host's rate. const max_seconds = 600; @@ -55,11 +57,12 @@ pub const State = struct { seek: sk.Request(f32) = .{}, scrubbing: bool = false, dialog: sk.Request(void) = .{}, - /// Deletes the take at this index. - remove: sk.Request(u32) = .{}, - /// How many takes the Store holds. `update` keeps count as it records - /// and deletes; `draw` corrects it with the `takes` command if not. - takes: u32 = 0, + /// Oldest first. + takes: sk.List(Take, max_takes) = .{}, + /// The next take's id. `draw` moves it past take files already there + /// with the `next-id` command: other runs' (`--fresh` keeps the files), + /// or the future a seek went back from. + next_id: u32 = 1, /// The take the transport and the keys work on, by index (oldest 0). selected: u32 = 0, views: [max_takes]View = @splat(.{}), @@ -76,37 +79,36 @@ pub const State = struct { }; pub const Take = struct { - /// Its samples: `Store.tape[start..][0..len]`. - start: usize = 0, - len: usize = 0, - /// Stays the same when earlier takes are deleted: GPU caches go by it. + /// Names its file, `take-.f32`: its mono samples at + /// `Input.sample_rate`. Stays the same when earlier takes are deleted: + /// Session and the GPU caches go by it. id: u32 = 0, - /// Where the listener is, in samples into the take. - heard: f64 = 0, /// A file's name, or empty for the microphone. source: [64]u8 = @splat(0), source_len: u32 = 0, }; -pub const Store = struct { - /// Every take's mono samples at `Input.sample_rate`, one after another, - /// oldest first. A recording grows the last one. - tape: std.ArrayList(f32) = .empty, - takes: std.ArrayList(Take) = .empty, - next_id: u32 = 1, +/// A take's samples in `Session.tape`. +const Clip = struct { + id: u32 = 0, + start: usize = 0, + len: usize = 0, + /// Where the listener is, in samples into the take. + heard: f64 = 0, }; pub const Session = struct { transport: sk.Handled = .{}, seek: sk.Handled = .{}, dialog: sk.Handled = .{}, - remove: sk.Handled = .{}, + /// State's takes' samples, read from their files once a process, one + /// after another in the order read. A recording grows the last one. + tape: std.ArrayList(f32) = .empty, + clips: sk.List(Clip, max_takes) = .{}, /// The take playback follows, by id, and the next of its samples to /// queue. playing: u32 = 0, cursor: usize = 0, - /// The take being recorded, by id. - recording: u32 = 0, /// The microphone's recent past and the live view. live: scope.Scope = .{}, /// Pitch, by tuner.comp: of the microphone (live, or recording), and @@ -123,7 +125,7 @@ const Tuned = enum(u32) { input, take }; /// GPU objects, made on first use. const Gpu = struct { ready: bool = false, - /// `Store.tape`, and how many of its samples are in it. + /// `Session.tape`, and how many of its samples are in it. tape: sk.Buffer = .none, capacity: usize = 0, uploaded: usize = 0, @@ -281,7 +283,7 @@ const World = struct { live: bool, fn of(s: *const State) World { - return .{ .takes = s.takes, .live = s.transport.value != .recording }; + return .{ .takes = s.takes.count(), .live = s.transport.value != .recording }; } fn slots(w: World) u32 { return w.takes + @intFromBool(w.live); @@ -341,10 +343,10 @@ pub fn update(s: *State, in: *const sk.Input) void { if (in.pressed(sk.Key.equals)) s.gain = @min(s.gain * 1.25, 64); if (in.pressed(sk.Key.minus)) s.gain = @max(s.gain / 1.25, 0.25); if (in.pressed(sk.Key.z) and s.selected < max_takes) s.views[s.selected] = .{}; - if (!recording and s.takes > 0) { + if (!recording and s.takes.count() > 0) { // Up the strip is older. if (in.pressed(sk.Key.up)) select(s, s.selected -| 1, in); - if (in.pressed(sk.Key.down)) select(s, @min(s.selected + 1, s.takes - 1), in); + if (in.pressed(sk.Key.down)) select(s, @min(s.selected + 1, s.takes.count() - 1), in); } const l = Layout.of(in.width, in.height); const page = l.view.h - l.panel_height / 2; @@ -429,7 +431,7 @@ fn act(s: *State, a: Action, in: *const sk.Input) void { .start => seekTo(s, 0), .end => seekTo(s, 1), .open => s.dialog.send({}), - .delete => if (now != .recording and s.takes > 0) remove(s, in), + .delete => if (now != .recording and s.takes.count() > 0) remove(s, in), .overview => { // The whole strip, or back to full size. const l = Layout.of(in.width, in.height); @@ -446,9 +448,9 @@ fn act(s: *State, a: Action, in: *const sk.Input) void { /// Starts a new take, unless the strip is full. It takes the live view's /// place, which the camera follows. fn record(s: *State) void { - if (s.takes >= max_takes) return; - s.selected = s.takes; - s.takes += 1; + s.takes.appendBounded(.{ .id = s.next_id }) catch return; + s.next_id += 1; + s.selected = s.takes.count() - 1; s.views[s.selected] = .{}; s.follow = true; change(s, .recording); @@ -483,12 +485,13 @@ fn panBy(s: *State, pixels: f32, in: *const sk.Input) void { moveCamera(s, cam, in); } -/// Deletes the selected take and selects the one above it. +/// Deletes the selected take and selects the one above it. Its file stays: +/// the host deletes no files, and a seek back finds it again. fn remove(s: *State, in: *const sk.Input) void { - const index = @min(s.selected, s.takes - 1); - s.remove.send(index); - std.mem.copyForwards(View, s.views[index .. s.takes - 1], s.views[index + 1 .. s.takes]); - s.takes -= 1; + const n = s.takes.count(); + const index = @min(s.selected, n - 1); + _ = s.takes.orderedRemove(index); + std.mem.copyForwards(View, s.views[index .. n - 1], s.views[index + 1 .. n]); s.selected = index -| 1; moveCamera(s, cameraOf(s, Layout.of(in.width, in.height)), in); } @@ -506,15 +509,14 @@ fn zoom(v: *View, at: f32, factor: f32) void { v.hi = lo + span; } -/// `open PATH` adds a file as a take; `open` alone shows the dialog. -/// `takes N` tells State how many takes the Store holds (`draw` sends it). -pub fn command(s: *State, store: *Store, c: *sk.Console, line: []const u8) !bool { +/// `open PATH` adds a file as a take, writing its samples to the take's +/// file; `open` alone shows the dialog. `next-id N` moves the counter +/// (`draw` sends it). +pub fn command(s: *State, ss: *Session, c: *sk.Console, line: []const u8) !bool { var words = std.mem.tokenizeAny(u8, line, " \t"); const verb = words.next() orelse return false; - if (std.mem.eql(u8, verb, "takes")) { - const n = try std.fmt.parseInt(u32, words.next() orelse return error.ExpectedCount, 10); - s.takes = @min(n, max_takes); - s.selected = @min(s.selected, s.takes -| 1); + if (std.mem.eql(u8, verb, "next-id")) { + s.next_id = try std.fmt.parseInt(u32, words.next() orelse return error.ExpectedId, 10); return true; } if (!std.mem.eql(u8, verb, "open")) return false; @@ -523,22 +525,24 @@ pub fn command(s: *State, store: *Store, c: *sk.Console, line: []const u8) !bool s.dialog.send({}); return true; } - if (store.takes.items.len >= max_takes) return error.TooManyTakes; + if (s.takes.count() >= max_takes) return error.TooManyTakes; const samples = c.host.samples(c.host.loadSound(path)); if (samples.len == 0) return error.CannotOpen; // Recording grows the last take: stop before this one goes after it. if (s.transport.value == .recording) change(s, .stopped); const kept = samples[0..@min(samples.len, max_seconds * c.in.sample_rate)]; - var take: Take = .{ .start = store.tape.items.len, .len = kept.len, .id = store.next_id }; + var take: Take = .{ .id = s.next_id }; const base = std.fs.path.basename(path); take.source_len = @intCast(@min(base.len, take.source.len)); @memcpy(take.source[0..take.source_len], base[0..take.source_len]); - try store.takes.ensureUnusedCapacity(c.store, 1); - try store.tape.appendSlice(c.store, kept); - store.takes.appendAssumeCapacity(take); - store.next_id += 1; - s.takes = @intCast(store.takes.items.len); - s.selected = s.takes - 1; + // Into Session now, so the web, which keeps no files, has it too. + try ss.tape.appendSlice(c.session, kept); + try ss.clips.appendBounded(.{ .id = take.id, .start = ss.tape.items.len - kept.len, .len = kept.len }); + var file: [32]u8 = undefined; + _ = c.host.appendFile(try std.fmt.bufPrint(&file, "take-{d}.f32", .{take.id}), std.mem.sliceAsBytes(kept)); + try s.takes.appendBounded(take); + s.next_id += 1; + s.selected = s.takes.count() - 1; s.views[s.selected] = .{}; s.follow = true; change(s, .stopped); @@ -549,7 +553,7 @@ pub fn command(s: *State, store: *Store, c: *sk.Console, line: []const u8) !bool // --------------------------------------------------------------------------- // Draw: the takes, the microphone, the speakers and the GPU. -pub fn draw(s: *const State, ss: *Session, store: *Store, f: *sk.Frame) void { +pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void { const in = f.in; const rate: f64 = @floatFromInt(in.sample_rate); const dt: f64 = @min(@as(f64, @floatFromInt(in.dt)) / std.time.ns_per_s, 0.1); @@ -557,24 +561,24 @@ pub fn draw(s: *const State, ss: *Session, store: *Store, f: *sk.Frame) void { f.canvas(@intFromFloat(@max(in.window_width, min_width)), @intFromFloat(@max(in.window_height, min_height))); f.clear(0x0b0c0f); - adoptTape(store, f.store); if (ss.dialog.take(s.dialog) != null) f.openFile("open", .{ .pattern = "wav;flac;ogg" }); - if (ss.remove.take(s.remove)) |index| removeTake(ss, store, index); + // Files are written live, never while a recording of the session plays + // back: like sound, a write is not replayed. + const writing = in.mode == .live or in.mode == .recording; + if (in.jumped and writing) { + // State came from elsewhere: the next id's file may hold samples. + var id = s.next_id; + while (f.host.readFile(f.host.appendFile(f.format("take-{d}.f32", .{id}), &.{})).len > 0) id += 1; + if (id != s.next_id) f.run(f.format("next-id {d}", .{id})); + } + readTakes(s, ss, f); // Requests from State. Any of them, or another take selected, flushes // what is queued, so the cursor restarts from what was heard. const transport = ss.transport.take(s.transport); const seek = ss.seek.take(s.seek); - // Recording goes by the transport's value rather than the request, so a - // fresh session that only caught up with the request still records. - if (s.transport.value != .recording) { - ss.recording = 0; - } else if (store.takes.items.len == 0 or store.takes.items[store.takes.items.len - 1].id != ss.recording) { - startTake(ss, store, f.store); - } - const takes = store.takes.items; - if (s.takes != takes.len) f.run(f.format("takes {d}", .{takes.len})); - const selected: ?*Take = if (takes.len > 0) &takes[@min(s.selected, takes.len - 1)] else null; + const takes = s.takes.constSlice(); + const selected: ?*Clip = if (takes.len > 0) clipOf(ss, takes[@min(s.selected, takes.len - 1)].id) else null; var queued: usize = in.stream(stream).queued; if (selected) |t| { if (transport != null or seek != null or t.id != ss.playing) { @@ -606,15 +610,16 @@ pub fn draw(s: *const State, ss: *Session, store: *Store, f: *sk.Frame) void { f.microphone(); ss.live.push(in.microphone); switch (s.transport.value) { - .recording => if (takes.len > 0 and takes[takes.len - 1].id == ss.recording) { - const t = &takes[takes.len - 1]; + // Into the last take, at the tape's end. + .recording => if (takes.len > 0 and writing) if (clipOf(ss, takes[takes.len - 1].id)) |t| if (t.start + t.len == ss.tape.items.len) { const room = max_seconds * in.sample_rate -| t.len; const heard = in.microphone[0..@min(in.microphone.len, room)]; - store.tape.appendSlice(f.store, heard) catch f.host.log("the store is full: recording stopped"); - t.len = store.tape.items.len - t.start; + ss.tape.appendSlice(f.session, heard) catch f.host.log("out of memory: recording stopped"); + _ = f.host.appendFile(f.format("take-{d}.f32", .{t.id}), std.mem.sliceAsBytes(heard)); + t.len = ss.tape.items.len - t.start; t.heard = @floatFromInt(t.len); }, - .forward, .backward => if (selected) |t| play(ss, t, store.tape.items[t.start..][0..t.len], f, s.transport.value == .forward, queued, rate, dt), + .forward, .backward => if (selected) |t| play(ss, t, ss.tape.items[t.start..][0..t.len], f, s.transport.value == .forward, queued, rate, dt), .stopped => {}, } @@ -632,41 +637,48 @@ pub fn draw(s: *const State, ss: *Session, store: *Store, f: *sk.Frame) void { f.text(16, Layout.header, 2, "micscope needs a GPU: the host has none.", 0xff8080); return; } - render(s, ss, store, f, rate, dt); -} - -/// Stores from before the strip held one tape: it becomes the first take. -fn adoptTape(store: *Store, a: std.mem.Allocator) void { - if (store.takes.items.len > 0 or store.tape.items.len == 0) return; - store.takes.append(a, .{ .len = store.tape.items.len, .id = store.next_id }) catch return; - store.next_id += 1; + render(s, ss, f, rate, dt); } -fn startTake(ss: *Session, store: *Store, a: std.mem.Allocator) void { - store.takes.append(a, .{ .start = store.tape.items.len, .id = store.next_id }) catch { - return; +/// Session's tape follows State's takes: a take State no longer has leaves +/// it, and one it lacks is read from its file (a new one's is empty). +fn readTakes(s: *const State, ss: *Session, f: *sk.Frame) void { + var i: u32 = 0; + while (i < ss.clips.count()) { + const t = ss.clips.buffer[i]; + if (for (s.takes.constSlice()) |take| { + if (take.id == t.id) break true; + } else false) { + i += 1; + continue; + } + _ = ss.clips.orderedRemove(i); + const tape = ss.tape.items; + std.mem.copyForwards(f32, tape[t.start .. tape.len - t.len], tape[t.start + t.len ..]); + ss.tape.shrinkRetainingCapacity(tape.len - t.len); + for (ss.clips.slice()[i..]) |*later| later.start -= t.len; + ss.gpu.uploaded = @min(ss.gpu.uploaded, t.start); + for (&ss.gpu.panels) |*p| if (p.id == t.id) { + p.id = 0; + p.forget(); + }; + } + for (s.takes.constSlice()) |take| if (clipOf(ss, take.id) == null) { + const bytes = f.host.readFile(f.host.appendFile(f.format("take-{d}.f32", .{take.id}), &.{})); + const samples = std.mem.bytesAsSlice(f32, @as([]align(4) const u8, @alignCast(bytes[0 .. bytes.len / 4 * 4]))); + ss.tape.appendSlice(f.session, samples) catch return f.host.log("out of memory: takes not read"); + ss.clips.appendBounded(.{ .id = take.id, .start = ss.tape.items.len - samples.len, .len = samples.len }) catch return; }; - ss.recording = store.next_id; - store.next_id += 1; } -fn removeTake(ss: *Session, store: *Store, index: u32) void { - if (index >= store.takes.items.len) return; - const t = store.takes.orderedRemove(index); - const tape = store.tape.items; - std.mem.copyForwards(f32, tape[t.start .. tape.len - t.len], tape[t.start + t.len ..]); - store.tape.shrinkRetainingCapacity(tape.len - t.len); - for (store.takes.items[index..]) |*later| later.start -= t.len; - ss.gpu.uploaded = @min(ss.gpu.uploaded, t.start); - for (&ss.gpu.panels) |*p| if (p.id == t.id) { - p.id = 0; - p.forget(); - }; +fn clipOf(ss: *Session, id: u32) ?*Clip { + for (ss.clips.slice()) |*t| if (t.id == id) return t; + return null; } /// Keeps the stream `lead_seconds` ahead, reading the take either way, and /// follows the listener. -fn play(ss: *Session, t: *Take, tape: []const f32, f: *sk.Frame, forward: bool, queued: usize, rate: f64, dt: f64) void { +fn play(ss: *Session, t: *Clip, tape: []const f32, f: *sk.Frame, forward: bool, queued: usize, rate: f64, dt: f64) void { const lead: usize = @intFromFloat(rate * lead_seconds); const want = lead -| queued; // What is heard now: the cursor, less what is queued in front of it. @@ -768,17 +780,17 @@ fn sync(g: *Gpu, f: *sk.Frame, tape: []const f32) void { } } -fn render(s: *const State, ss: *Session, store: *Store, f: *sk.Frame, rate: f64, dt: f64) void { +fn render(s: *const State, ss: *Session, f: *sk.Frame, rate: f64, dt: f64) void { const g = &ss.gpu; setup(g, f); - sync(g, f, store.tape.items); + sync(g, f, ss.tape.items); // New code may mean new shaders: make everything again (the live view // sees to its own). if (f.in.reloaded) for (&g.panels) |*p| p.forget(); - const takes = store.takes.items; + const takes = s.takes.constSlice(); const selected = if (takes.len > 0) @min(s.selected, takes.len - 1) else 0; const recording = s.transport.value == .recording; - const world: World = .{ .takes = @intCast(takes.len), .live = !recording }; + const world: World = .of(s); const l = Layout.of(f.in.width, f.in.height); const cam = cameraOf(s, l); const view = l.view; @@ -795,24 +807,23 @@ fn render(s: *const State, ss: *Session, store: *Store, f: *sk.Frame, rate: f64, // The tuner: on the microphone's newest sound, and on the selected take // around its playhead. const reach = tuner_layouts.tuner_window + tuner_layouts.tuner_lags; - if (recording and takes.len > 0) { - const t = takes[takes.len - 1]; + const last = if (recording and takes.len > 0) clipOf(ss, takes[takes.len - 1].id) else null; + if (last) |t| { tune(g, f, .input, g.tape, t.start, t.len, t.len, rate); } else tune(g, f, .input, ss.live.gpu.tape, 0, scope.history, scope.history, rate); - if (!recording and takes.len > 0) { - const t = takes[selected]; + if (!recording and takes.len > 0) if (clipOf(ss, takes[selected].id)) |t| { const end: usize = @intFromFloat(@max(t.heard, 0)); tune(g, f, .take, g.tape, t.start, t.len, end + reach / 2, rate); - } + }; for (0..world.takes) |slot| { const k: u32 = @intCast(slot); const b = l.box(k).toScreen(cam); if (b.whole.y + b.whole.h <= view.y or b.whole.y >= view.y + view.h) continue; const tint: layouts.Tint = if (recording and k + 1 == world.takes) .recording else .take; - drawTake(s, g, f, size, b, &takes[k], s.views[k], k == selected, tint, rate, dt); + drawTake(s, g, f, size, b, clipOf(ss, takes[k].id) orelse continue, s.views[k], k == selected, tint, rate, dt); } f.clip(null); - labels(s, ss, store, f, world, cam, rate); + labels(s, ss, f, world, cam, rate); } /// Finds the pitch of the `tuner_window` samples before `end` (of the @@ -884,7 +895,7 @@ fn spectrum(g: *Gpu, f: *sk.Frame, tape: sk.Buffer, p: *Panel, size: [2]u32, par }); } -fn drawTake(s: *const State, g: *Gpu, f: *sk.Frame, size: Box, b: Box, t: *const Take, view: View, selected: bool, tint: layouts.Tint, rate: f64, dt: f64) void { +fn drawTake(s: *const State, g: *Gpu, f: *sk.Frame, size: Box, b: Box, t: *const Clip, view: View, selected: bool, tint: layouts.Tint, rate: f64, dt: f64) void { const p = panelFor(g, f, t.id); // Waveform columns, when what they show changed. @@ -1041,8 +1052,8 @@ const red = 0xff5a5a; const green = 0x6fdc8c; const yellow = 0xffd84d; -fn labels(s: *const State, ss: *const Session, store: *const Store, f: *sk.Frame, world: World, cam: sk.Camera, rate: f64) void { - const takes = store.takes.items; +fn labels(s: *const State, ss: *Session, f: *sk.Frame, world: World, cam: sk.Camera, rate: f64) void { + const takes = s.takes.constSlice(); const recording = s.transport.value == .recording; const selected = if (takes.len > 0) @min(s.selected, takes.len - 1) else 0; @@ -1055,14 +1066,13 @@ fn labels(s: *const State, ss: *const Session, store: *const Store, f: *sk.Frame }; const l = Layout.of(f.in.width, f.in.height); f.text(16, 10, 2, status[0], status[1]); - if (takes.len > 0) { - const t = takes[selected]; + if (if (takes.len > 0) clipOf(ss, takes[selected].id) else null) |t| { const times = .{ Time{ .seconds = t.heard / rate }, Time{ .seconds = @as(f64, @floatFromInt(t.len)) / rate } }; // As much as fits beside the buttons (or, narrow, the corner). const room = if (l.narrow) l.w - 16 * 7 else l.button(buttons.len - 1).x - 16; if (room >= 480) { f.print(140, 10, 2, bright, "{f} / {f}", times); - if (room >= 740) f.print(500, 14, 1, dim, "take {d} of {d}: {s}", .{ selected + 1, takes.len, source(&t) }); + if (room >= 740) f.print(500, 14, 1, dim, "take {d} of {d}: {s}", .{ selected + 1, takes.len, source(&takes[selected]) }); } else f.print(16 + 16 * 7, 14, 1, bright, "{f} / {f}", times); } for (buttons, 0..) |a, i| button(s, f, a, l.button(i)); @@ -1087,14 +1097,14 @@ fn labels(s: *const State, ss: *const Session, store: *const Store, f: *sk.Frame tunerOverlay(f, b.spectrum, ss.tuners[@intFromEnum(Tuned.input)].reading, s.a4, rate, cam.zoom >= 0.8); continue; } - const t = &takes[k]; + const t = clipOf(ss, takes[k].id) orelse continue; const seconds = Time{ .seconds = @as(f64, @floatFromInt(t.len)) / rate }; if (recording and k + 1 == world.takes) { f.print(b.whole.x, title_y, 1, red, "RECORDING take {d} {f}", .{ k + 1, seconds }); } else { const chosen = k == selected; if (chosen) f.fill(.{ .x = b.whole.x - if (l.narrow) @as(f32, 6) else 12, .y = b.wave.y, .w = 3, .h = b.timeline.h }, yellow); - f.print(b.whole.x, title_y, 1, @as(u24, if (chosen) bright else dim), "#{d} {s} {f}", .{ k + 1, source(t), seconds }); + f.print(b.whole.x, title_y, 1, @as(u24, if (chosen) bright else dim), "#{d} {s} {f}", .{ k + 1, source(&takes[k]), seconds }); } if (cam.zoom >= 0.8) { const span = visible(s.views[k], t.len, @intFromFloat(@min(l.box(0).wave.w, layouts.max_columns))); @@ -1254,7 +1264,8 @@ test "the world stacks takes in order with the live view last" { test "the camera follows the bottom and stays inside the world, wide or narrow" { for ([_]Layout{ .of(width, height), .of(390, 844) }) |l| { - var s: State = .{ .takes = 5 }; + var s: State = .{}; + s.takes.len = 5; const cam = cameraOf(&s, l); // Following: the live view's bottom sits at the viewport's bottom. try std.testing.expectApproxEqAbs(l.view.y + l.view.h - 4, cam.toScreen(l.box(5).whole).y + l.panel_height, 1); -- cgit v1.3