summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/micscope/README.md10
-rw-r--r--examples/micscope/src/main.zig247
-rw-r--r--examples/micscope/test/record.script10
-rw-r--r--examples/micscope/test/transport.script22
4 files changed, 156 insertions, 133 deletions
diff --git a/examples/micscope/README.md b/examples/micscope/README.md
index bd55ee0..c02318c 100644
--- a/examples/micscope/README.md
+++ b/examples/micscope/README.md
@@ -148,6 +148,10 @@ with WebGPU (Chrome, Edge, Safari 26; Firefox on Linux with
browser has accepted its self-signed certificate (miniserve prints the
addresses as QR codes, for phones).
-The takes are the sketch's `Store`: they live in `micscope.state.store` and
-are still there after a restart. Takes stop at ten minutes; the strip holds
-64. A store from before the strip has its one tape as the first take.
+Each take is a file of its samples (mono 32-bit floats at the host's
+rate), `micscope.state.take-<id>.f32` beside the state (in `.sketchbook/`
+under `zig build run`), written as it records (`Host.appendFile`). State
+lists the takes, so they are still there after a restart. A deleted take's
+file stays: a seek back finds it again. Takes stop at ten minutes; the
+strip holds 64. A `micscope.state.store` from before is not read: its takes
+stay in it.
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-<id>.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);
diff --git a/examples/micscope/test/record.script b/examples/micscope/test/record.script
index 2750a18..843d802 100644
--- a/examples/micscope/test/record.script
+++ b/examples/micscope/test/record.script
@@ -1,9 +1,9 @@
# Records (from whatever microphone the host gives a headless run) into a
# new take, stops, and plays it back.
-expect takes 0
+expect takes.len 0
key r
expect transport.value recording
-expect takes 1
+expect takes.len 1
frames 60
screenshot .zig-cache/micscope/recording.png
key r
@@ -13,11 +13,11 @@ expect transport.value forward
frames 10
# Each recording is another take; opening a file while recording ends it.
key r
-expect takes 2
+expect takes.len 2
expect selected 1
console open test/sweep.wav
expect transport.value stopped
-expect takes 3
+expect takes.len 3
frames 2
-expect takes 3
+expect takes.len 3
screenshot .zig-cache/micscope/strip.png
diff --git a/examples/micscope/test/transport.script b/examples/micscope/test/transport.script
index f821a03..2015682 100644
--- a/examples/micscope/test/transport.script
+++ b/examples/micscope/test/transport.script
@@ -2,7 +2,7 @@
# 16 kHz) over a quiet 440 Hz tone.
console open test/sweep.wav
frames 2
-expect takes 1
+expect takes.len 1
expect selected 0
expect transport.value stopped
key right
@@ -36,7 +36,7 @@ frames 30
screenshot .zig-cache/micscope/played.png
# A second take goes under the first and is selected; up is older.
console open test/sweep.wav
-expect takes 2
+expect takes.len 2
expect selected 1
key up
expect selected 0
@@ -47,14 +47,22 @@ click 400 150
expect selected 0
frames 2
screenshot .zig-cache/micscope/two-takes.png
-# Deleting it leaves the second.
+# Deleting it leaves the second. Its file stays: going back to before the
+# deletion reads it again.
+console save two
key delete
-expect takes 1
+expect takes.len 1
expect selected 0
-# State that miscounts the takes is told by draw.
-console set takes 0
+console load two
frames 2
-expect takes 1
+expect takes.len 2
+key delete
+expect takes.len 1
+# A counter that names a take file already written (--fresh keeps the
+# files; a seek back finds the future's) is moved past it by draw.
+console set next_id 1
+frames 2
+expect next_id 3
# More takes than fit: the camera follows the bottom until the wheel pans
# it, then 0 shows the whole strip.
console open test/sweep.wav