diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-16 23:31:18 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | b2f7c605c279b2dea0c25180730c3d2e7b4b7d45 (patch) | |
| tree | 142f0258cefd71ed210c64ae73f852e386f71991 /src/host/Sessions.zig | |
| parent | 57e254470e11ab40b6bae31b2cbf62aebca27ce4 (diff) | |
| download | sketchbook-b2f7c605c279b2dea0c25180730c3d2e7b4b7d45.tar.gz sketchbook-b2f7c605c279b2dea0c25180730c3d2e7b4b7d45.zip | |
Make snapshots named marks and recordings named clips of sessions
The state file keeps live state only (SKETCH03, one slot). save NAME is a
keyframe and a mark in the sessions database, synced to disk; load, diff,
snapshot and expect-snapshot take mark names, through the keyframe's own
layout migrated to today's, compared over the leaves migration kept. A
file from before (SKETCH01/02) has its used snapshot slots imported as
marks "1".."7" at open, then drops them; a run that records nothing
leaves them alone.
record NAME, stop and play NAME make and play clips: a frame range of a
session and its start keyframe, nothing overwritten, nothing invalidated
by a later save, no capacity. The 64 MiB .inputs file, its --fresh memset
and Timeline's Bank are gone; numbers still work as names, so every
existing script runs unchanged. Event times in a clip are the session's,
not restarted at record. reload.script also checks that a field a reload
drops is still in the keyframe taken before it (expect-key).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'src/host/Sessions.zig')
| -rw-r--r-- | src/host/Sessions.zig | 76 |
1 files changed, 45 insertions, 31 deletions
diff --git a/src/host/Sessions.zig b/src/host/Sessions.zig index 809f160..6f10bba 100644 --- a/src/host/Sessions.zig +++ b/src/host/Sessions.zig @@ -161,6 +161,7 @@ pub fn deinit(s: *Sessions) void { pub fn close(s: *Sessions, state: []const u8, ended: u64) void { defer s.deinit(); if (s.db == .off) return; + s.stopClip() catch {}; _ = s.keyframe(.exit, state) catch {}; s.commitSynced(.{ .end = ended }) catch |err| std.log.warn("sessions: last commit failed: {t}", .{err}); } @@ -227,10 +228,12 @@ pub fn stretch(s: *Sessions, st: Stretch) !void { s.stretches += 1; } -/// Mark `name`: a keyframe of `state`, synced to disk. -pub fn mark(s: *Sessions, name: []const u8, state: []const u8) !void { - const k = try s.keyframe(.mark, state); +/// Mark `name`: a keyframe of `state` (`why`: `.mark`, or `.import`), +/// synced to disk. Returns the keyframe's index. +pub fn mark(s: *Sessions, name: []const u8, why: Why, state: []const u8) !u64 { + const k = try s.keyframe(why, state); try s.commitSynced(.{ .mark = .{ .name = name, .key = k } }); + return k; } /// Starts clip `name` at the next frame, played from keyframe `k`. @@ -366,8 +369,19 @@ fn Commit(comptime DB: type) type { // --------------------------------------------------------------------------- // Reading -pub const Found = struct { session: u64, key: u64 }; -pub const Clip = struct { session: u64, key: u64, start: u64, end: u64 }; +/// A mark (`start` and `end` 0) or a clip. +pub const Entry = struct { + session: u64, + key: u64, + start: u64 = 0, + end: u64 = 0, + bytes: [max_name]u8 = undefined, + len: usize = 0, + + pub fn name(e: *const Entry) []const u8 { + return e.bytes[0..e.len]; + } +}; /// A keyframe, its bytes `gpa`'s (`free`). pub const Loaded = struct { frame: u64, @@ -387,44 +401,44 @@ pub const Loaded = struct { } }; +pub fn markAt(s: *Sessions, i: u64) !Entry { + return s.entryAt(.marks, i); +} +pub fn clipAt(s: *Sessions, i: u64) !Entry { + return s.entryAt(.clips, i); +} /// The newest mark named `name`. -pub fn findMark(s: *Sessions, name: []const u8) !?Found { - const n = try s.count(.marks); - var i = n; - while (i > 0) { - i -= 1; - const e = try s.entry(.marks, i, name) orelse continue; - return .{ .session = e.session, .key = e.key }; - } - return null; +pub fn findMark(s: *Sessions, name: []const u8) !?Entry { + return s.findNamed(.marks, name); } - /// The newest clip named `name`. -pub fn findClip(s: *Sessions, name: []const u8) !?Clip { - var i = try s.count(.clips); +pub fn findClip(s: *Sessions, name: []const u8) !?Entry { + return s.findNamed(.clips, name); +} + +fn findNamed(s: *Sessions, which: K, name: []const u8) !?Entry { + var i = try s.count(which); while (i > 0) { i -= 1; - return try s.entry(.clips, i, name) orelse continue; + const e = try s.entryAt(which, i); + if (std.mem.eql(u8, e.name(), name)) return e; } return null; } -/// Entry `i` of `marks` or `clips` when it is named `name`. -fn entry(s: *Sessions, which: K, i: u64, name: []const u8) !?Clip { +fn entryAt(s: *Sessions, which: K, i: u64) !Entry { switch (s.db) { .off => return error.NotRecording, inline .memory, .file => |*db| { const R = Read(@TypeOf(db.*)); - const e = try R.at(try R.list(try R.root(db), which) orelse return null, i); - var buf: [max_name]u8 = undefined; - const got = (try R.field(e, .name)).readBytes(&buf) catch return null; - if (!std.mem.eql(u8, got, name)) return null; - return .{ - .session = try R.uint(e, .session), - .key = try R.uint(e, .key), - .start = if (which == .clips) try R.uint(e, .start) else 0, - .end = if (which == .clips) try R.uint(e, .end) else 0, - }; + const e = try R.at(try R.list(try R.root(db), which) orelse return error.NoSuchEntry, i); + var out: Entry = .{ .session = try R.uint(e, .session), .key = try R.uint(e, .key) }; + out.len = ((try R.field(e, .name)).readBytes(&out.bytes) catch return error.CorruptSession).len; + if (which == .clips) { + out.start = try R.uint(e, .start); + out.end = try R.uint(e, .end); + } + return out; }, } } @@ -432,7 +446,7 @@ fn entry(s: *Sessions, which: K, i: u64, name: []const u8) !?Clip { /// Entries in the root list `which`, or sessions. pub fn count(s: *Sessions, which: K) !u64 { switch (s.db) { - .off => return 0, + .off => return error.NotRecording, inline .memory, .file => |*db| { const R = Read(@TypeOf(db.*)); return if (try R.list(try R.root(db), which)) |l| try l.count() else 0; |
