summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md17
-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
-rw-r--r--src/abi.zig14
-rw-r--r--src/host/Host.zig60
-rw-r--r--src/host/root.zig24
-rw-r--r--src/sketch/web.zig5
9 files changed, 262 insertions, 147 deletions
diff --git a/README.md b/README.md
index 8cf2998..5b262d3 100644
--- a/README.md
+++ b/README.md
@@ -128,7 +128,7 @@ State.
`imageAt`, `target`, `blend`, `text`, `print`, `clip`, `canvas`, `cursor`, `voice`, `gain`, `audio`, `microphone`, `pixels`, `gpu`,
`compute`, `upload`, `download`, `openFile`, `run`, `quit`, plus the three allocators and
`f.host` (`loadImage`, `decodeImage`, `newImage`, `newBuffer`, `loadSound`,
-`decodeSound`, `newSound`, `samples`, `readFile`, `env`, `log`, `textSize`). Commands may be built
+`decodeSound`, `newSound`, `samples`, `readFile`, `appendFile`, `env`, `log`, `textSize`). Commands may be built
from locals: the methods copy what they keep (text, GPU descriptions,
uniforms) into scratch. Bulk data (vertices, samples, pixels, uploads) is
not copied and must outlive the frame: scratch, session or store memory.
@@ -171,6 +171,14 @@ does the same with any line, after the frame: `draw` cannot write State,
so this is how it tells State something only it learns (how many takes a
Store holds, say).
+`f.host.appendFile(name, bytes)` appends to a file of the sketch's own,
+`<state>.<name>` beside its state file (in `.sketchbook/` under `zig build
+run`), creating it, and returns its path for `readFile` ("" when nothing
+was written, and always on the web). Only `draw` and `command` can call it,
+and a write is not replayed, like sound. `readFile` maps a file once, so
+what is appended afterwards is not in what it returned. micscope writes
+each take to `take-<id>.f32` as it records.
+
`f.download(buffer, .{ .size = n, .key = k })` reads a GPU buffer back
without stalling: the bytes arrive in a later frame's `in.downloads`, a
frame or two behind. For measurements a shader made (a pitch, a level),
@@ -892,9 +900,10 @@ a talk with a slider, a loop, a choice, two slides and `beside`.
`expect`, or a console line that errors (a sketch command returning an
error included), fails the script. Headless always means offscreen, even
with `SDL_VIDEO_DRIVER` set, and GPU work still runs, into a texture. A
- scripted `--fresh` run works on files of its own (`<state>.<pid>`),
- renamed to the given `--state` at exit, so two builds running the same
- script at once do not share them; its sessions are in memory.
+ scripted `--fresh` run works on files of its own (`<state>.<pid>`, and
+ `<state>.<pid>.<name>` for those it appends to), renamed to the given
+ `--state` at exit, so two builds running the same script at once do not
+ share them; its sessions are in memory.
- **Export.** `--export DIR` plays the sketch headless, from fresh state,
and saves every frame as `DIR/000000.png`, `000001.png`, ...;
`--export -` writes them to stdout instead, as raw RGBA:
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
diff --git a/src/abi.zig b/src/abi.zig
index 35c399b..f88fca5 100644
--- a/src/abi.zig
+++ b/src/abi.zig
@@ -647,7 +647,9 @@ pub const Host = struct {
samples: *const fn (*anyopaque, Audio) []const f32,
/// The whole file, mapped for the life of the host; empty if missing.
readFile: *const fn (*anyopaque, []const u8) []const u8,
- env: *const fn (*anyopaque, []const u8) ?[]const u8,
+ /// See `Host.appendFile`: name, then bytes.
+ appendFile: *const fn (*anyopaque, [*]const u8, usize, [*]const u8, usize) callconv(.c) [*:0]const u8,
+ env:*const fn (*anyopaque, []const u8) ?[]const u8,
/// A monotonic clock in nanoseconds, for measuring, not for state:
/// `update` must use `Input` time to stay replayable.
clock: *const fn (*anyopaque) u64,
@@ -720,6 +722,16 @@ pub const Host = struct {
pub fn readFile(h: Host, path: []const u8) []const u8 {
return h.vtable.readFile(h.ctx, path);
}
+ /// Appends `bytes` to the sketch's file `name` (no `/` in it),
+ /// creating it: `<state>.<name>`, beside the state file (in
+ /// `.sketchbook/` under `zig build run`). Returns the file's path, for
+ /// `readFile`, or "" when nothing was written, which on the web is
+ /// always. Only `draw` and `command` can write, and a write is not
+ /// replayed, like sound. `readFile` maps a file once: what is appended
+ /// to it afterwards is not in what it returned.
+ pub fn appendFile(h: Host, name: []const u8, bytes: []const u8) []const u8 {
+ return std.mem.span(h.vtable.appendFile(h.ctx, name.ptr, name.len, bytes.ptr, bytes.len));
+ }
pub fn env(h: Host, name: []const u8) ?[]const u8 {
return h.vtable.env(h.ctx, name);
}
diff --git a/src/host/Host.zig b/src/host/Host.zig
index 4f1214a..05d08f7 100644
--- a/src/host/Host.zig
+++ b/src/host/Host.zig
@@ -107,6 +107,9 @@ commands: std.ArrayList(abi.Command) = .empty,
pending: std.ArrayList(abi.Event) = .empty,
input: abi.Input = .{},
files: std.StringHashMapUnmanaged([]align(memory.page) const u8) = .empty,
+/// The paths `appendFile` wrote, each once, 0-terminated: the sketch keeps
+/// them.
+written: std.StringHashMapUnmanaged(void) = .empty,
canvas: [2]u32 = .{ 0, 0 },
cursor: abi.Cursor = .arrow,
quit: bool = false,
@@ -259,6 +262,9 @@ pub fn deinit(self: *Host) void {
std.posix.munmap(entry.value_ptr.*);
}
self.files.deinit(self.gpa);
+ var written = self.written.keyIterator();
+ while (written.next()) |path| self.gpa.free(path.ptr[0 .. path.len + 1]);
+ self.written.deinit(self.gpa);
self.timeline.deinit();
self.session.deinit(self.gpa);
self.store.close();
@@ -1014,6 +1020,7 @@ const api: abi.Host.VTable = .{
.newSound = apiNewSound,
.samples = apiSamples,
.readFile = apiReadFile,
+ .appendFile = apiAppendFile,
.env = apiEnv,
.clock = apiClock,
};
@@ -1090,6 +1097,35 @@ fn apiReadFile(ctx: *anyopaque, path: []const u8) []const u8 {
self.files.put(self.gpa, key, bytes) catch self.gpa.free(key);
return bytes;
}
+fn apiAppendFile(ctx: *anyopaque, name_ptr: [*]const u8, name_len: usize, bytes: [*]const u8, len: usize) callconv(.c) [*:0]const u8 {
+ const self = hostOf(ctx);
+ const name = name_ptr[0..name_len];
+ if (name.len == 0 or std.mem.indexOfScalar(u8, name, '/') != null) {
+ self.print("appendFile: {s}: a name, not a path", .{name});
+ return "";
+ }
+ var buf: [std.fs.max_path_bytes]u8 = undefined;
+ const path = std.mem.print(&buf, "{s}.{s}", .{ self.options.state, name }) catch return "";
+ const file = std.Io.Dir.cwd().createFile(self.io, path, .{ .truncate = false }) catch |err| {
+ self.print("appendFile: {s}: {t}", .{ path, err });
+ return "";
+ };
+ defer file.close(self.io);
+ const end = file.length(self.io) catch return "";
+ file.writePositionalAll(self.io, bytes[0..len], end) catch |err| {
+ self.print("appendFile: {s}: {t}", .{ path, err });
+ return "";
+ };
+ const kept = self.written.getOrPut(self.gpa, path) catch return "";
+ if (!kept.found_existing) {
+ const copy = self.gpa.dupeSentinel(u8, path, 0) catch {
+ self.written.removeByPtr(kept.key_ptr);
+ return "";
+ };
+ kept.key_ptr.* = copy;
+ }
+ return @ptrCast(kept.key_ptr.*.ptr);
+}
fn apiClock(_: *anyopaque) u64 {
return c.SDL_GetTicksNS();
}
@@ -1295,3 +1331,27 @@ test "a state file's snapshots from before sessions become marks, and the file o
try std.testing.expectEqual(@as(usize, 1), again.slots);
try std.testing.expectEqual(@as(u32, 5), std.mem.readInt(u32, again.slot(0)[0..4], .little));
}
+
+test "appendFile appends to a file beside the state and names it for readFile" {
+ const io = std.testing.io;
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var buf: [std.fs.max_path_bytes]u8 = undefined;
+ var host: Host = undefined;
+ host.gpa = std.testing.allocator;
+ host.io = io;
+ host.written = .empty;
+ host.options = .{ .state = try std.mem.print(&buf, ".zig-cache/tmp/{s}/t.state", .{&tmp.sub_path}) };
+ defer {
+ var written = host.written.keyIterator();
+ while (written.next()) |path| host.gpa.free(path.ptr[0 .. path.len + 1]);
+ host.written.deinit(host.gpa);
+ }
+ const name = "take-1.f32";
+ const path = std.mem.span(apiAppendFile(&host, name, name.len, "ab", 2));
+ try std.testing.expect(std.mem.endsWith(u8, path, "/t.state.take-1.f32"));
+ try std.testing.expectEqual(path.ptr, apiAppendFile(&host, name, name.len, "cd", 2));
+ const bytes = try tmp.dir.readFileAlloc(io, "t.state.take-1.f32", host.gpa, .unlimited);
+ defer host.gpa.free(bytes);
+ try std.testing.expectEqualStrings("abcd", bytes);
+}
diff --git a/src/host/root.zig b/src/host/root.zig
index 5889b42..c64af92 100644
--- a/src/host/root.zig
+++ b/src/host/root.zig
@@ -96,21 +96,27 @@ pub fn main(init: std.process.Init, linked: ?abi.Sketch) !void {
// A scripted run from fresh state works on files of its own, moved to
// the given path at exit: two runs of one build step (two `zig build`s
// at once share its output path) never share a state or store file,
- // nor fail on the store's lock (its sessions are in memory). An export's are
- // removed instead: nothing reads them, and a player may have the given
- // ones open.
+ // nor the files the sketch appends to, nor fail on the store's lock
+ // (its sessions are in memory). An export's are removed instead:
+ // nothing reads them, and a player may have the given ones open.
var private_buf: [std.fs.max_path_bytes]u8 = undefined;
if (options.state.len == 0 and (script_path != null or options.export_to != null)) options.state = "sketch.state";
const given = options.state;
const private = (script_path != null or options.export_to != null) and options.fresh;
if (!private) sketchbookDir(init.io, given);
if (private) options.state = std.mem.print(&private_buf, "{s}.{d}", .{ given, std.os.linux.getpid() }) catch fatal("--state path too long");
- defer if (private) for ([_][]const u8{ "", ".store" }) |suffix| {
- var from: [std.fs.max_path_bytes + 16]u8 = undefined;
- var to: [std.fs.max_path_bytes + 16]u8 = undefined;
- const a = std.mem.printSentinel(&from, "{s}{s}", .{ options.state, suffix }, 0) catch continue;
- const b = std.mem.printSentinel(&to, "{s}{s}", .{ given, suffix }, 0) catch continue;
- _ = if (options.export_to != null) std.os.linux.unlink(a.ptr) else std.os.linux.rename(a.ptr, b.ptr);
+ // Its files are `<private>` and `<private>.<name>`, beside the given one.
+ defer if (private) moved: {
+ const dir = std.Io.Dir.cwd().openDir(init.io, std.fs.path.dirname(given) orelse ".", .{ .iterate = true }) catch break :moved;
+ defer dir.close(init.io);
+ const from = std.fs.path.basename(options.state);
+ var it = dir.iterate();
+ while (it.next(init.io) catch null) |entry| {
+ if (!std.mem.startsWith(u8, entry.name, from) or (entry.name.len > from.len and entry.name[from.len] != '.')) continue;
+ var to: [std.fs.max_path_bytes]u8 = undefined;
+ const name = std.mem.print(&to, "{s}{s}", .{ std.fs.path.basename(given), entry.name[from.len..] }) catch continue;
+ if (options.export_to != null) dir.deleteFile(init.io, entry.name) catch {} else dir.rename(entry.name, dir, name, init.io) catch {};
+ }
};
sdl_heap.install();
diff --git a/src/sketch/web.zig b/src/sketch/web.zig
index 62a173a..fd8d7bb 100644
--- a/src/sketch/web.zig
+++ b/src/sketch/web.zig
@@ -414,6 +414,7 @@ fn Driver(comptime App: type) type {
.newSound = newSound,
.samples = samples,
.readFile = readFile,
+ .appendFile = appendFile,
.env = env,
.clock = clock,
};
@@ -478,6 +479,10 @@ fn Driver(comptime App: type) type {
fn readFile(_: *anyopaque, _: []const u8) []const u8 {
return &.{};
}
+ /// No files on the web: nothing is written, and the path is "".
+ fn appendFile(_: *anyopaque, _: [*]const u8, _: usize, _: [*]const u8, _: usize) callconv(.c) [*:0]const u8 {
+ return "";
+ }
fn env(_: *anyopaque, _: []const u8) ?[]const u8 {
return null;
}