Skip to content

Punch-out, the region loop, and loop takes (recording.md chunk 4) - #170

Merged
ninjudd merged 2 commits into
mainfrom
recording-punch-loop
Sep 8, 2026
Merged

ninjudd merged 2 commits into
mainfrom
recording-punch-loop

Conversation

@ninjudd

@ninjudd ninjudd commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

The punch-and-loop half of recording.md chunk 4: a take can end where it was asked to, begin exactly where it was asked to, and repeat over a region with a take per pass. Comping is what remains of the chunk, deliberately unplanned until real takes have accumulated.

What changed for a person

  • field record creek/track:lead-vocal --at 17 --until 21 lands exactly bars 17–20 and returns on its own, stopping the transport the way ctrl-c would. record gained until: the callback ends the take on that sample mid-block and the heartbeat finalizes it while the mix rolls on.
  • --at is now exact in the other direction too. The capture request is armed before the transport starts, so the first rolling block latches on at itself; a bed already rolling toward at waits for it — a punch-in. Chunk 1's "that block or the one after" survives only for a take with no --at.
  • field record creek/track:lead-vocal --at 17 --until 21 --loop loops the region and lands a take every pass until ctrl-c, each printed as it lands, the last chosen; the partial pass ctrl-c ends lands last. --loop needs --until, --until needs --at, and loop takes need a live song as the bed.
  • field play <song> --from 5 --loop is a region loop now, not a refusal pointing at the artifact spelling.

What changed in the engine

  • The region loop (LiveSession::setLoopRegion, loop {from, to} op, to defaulting to the content's extent): the clock wraps from to back to from in the same wrapped-time mixing the declared-extent wrap uses, chains ringing across it — the loop-start realtime.md chunk 4's amendment owed, now noted there as delivered. status carries loop_from/loop_to, looping means either loop, and a load or a fresh session clears it. A file bed still loops only whole.
  • Loop takes are the region plus a standby transaction. record {at, until, loop} sets the region and opens the next pass's transaction ahead of the wrap; at until the callback pushes the block's remaining frames — which the wrap put at at — into the standby and raises a done flag; the heartbeat (serviceCapture) finalizes the finished pass, fires its take event, and opens the next standby. Consecutive takes meet on consecutive input samples, which the suite proves on the null clock's sawtooth. A pass that begins with no standby ready (a region shorter than a heartbeat tick) is counted in record.missed and skipped rather than half-recorded, and field record shows that count on its live line and in the closing tally whenever it is not zero. Stop finalizes the partial pass, discards the unused standby (its directory goes; recordings never lists it), and clears the region.
  • A stopped bed reports no position (pullAudio returns −1 while the transport is not rolling), so nothing latches or ticks against a bed that is not playing — the change that makes at exact.

Known limit recorded (plan §15.6)

A file bed's transport position leads its output by the resampler's prefetch — AudioTransportSource reads a few samples ahead on the first block — so a punch-out against a file bed ends about three frames short. Every record flow's bed is the song's session, whose positions are exact, so this is recorded rather than fixed; the exact fix is counting delivered frames since the last locate instead of asking the transport.

Daemon 0.14.0; features loop-region and record-until. recordTake returns { takes } now rather than one take, since a loop lands many.

Testing

From the repo root — the daemon changed:

pnpm build:native && pnpm build && pnpm test

49 files, 728 tests, green here. New: src/commands/punch-daemon.test.ts — the region loop wraps between its bounds over the socket (positions stay inside, come back around, never finish) and rejects bad regions; a punch-out lands exactly until − at frames with the transport still playing; a loop take lands three full passes of exactly the region's length plus the stop's partial one, each gapless and each continuing the previous on the input clock, with no standby left on disk; recordTake with --until returns on its own and stops, --loop lands one per pass until interrupted with the last chosen; and playLiveSession with from + loop plays the region.

By hand, null mode, in a scratch project:

mkdir /tmp/loop-probe && cd /tmp/loop-probe && export FIELD_AUDIOD_NULL=1
field init --name probe && field song create creek && field track create creek lead-vocal
node -e 'import("/Users/justin/amnesiac/field/dist/audio/wav.js").then(({ writeWavFloat32 }) => writeWavFloat32("tone.wav", 48000, [new Float32Array(48000*8).fill(0.1)]))'
field import tone.wav --song creek
printf 'name: main\ninputs:\n  - audio: tone\n    anchor: absolute\n    at: 0\n' > songs/creek/tracks/main.yaml
printf 'name: lead-vocal\ninputs:\n  - record:\n' > songs/creek/tracks/lead-vocal.yaml
field play creek --from 2 --loop                                 # was refused; ctrl-c
field record creek/track:lead-vocal --at 2 --until 2.3           # returns by itself: one 1.0 s take
field record creek/track:lead-vocal --at 2 --until 2.3 --loop    # ctrl-c after ~3 s: a take per second, then the partial
field check
field transport quit

The punch-out prints recording take-… from 2.1.1 (0:02.0) to 2.3.1 (0:03.0) and one take …, chosen (…, 1.0 s) line — before this branch there was no --until, and a take ended only at ctrl-c. The loop run prints a take line per pass, each 1.0 s, then a shorter one and N takes landed; take-… is chosen — with , M passes missed in that tally and take N, M passes missed on the live line whenever the daemon skipped a pass, which a bar-long region never does; takes.yaml holds every row and check is clean. field play creek --from 2 --loop shows (looping) and positions that never go below bar 2.

On hardware, the thing to listen for is the loop wrap: with --loop, the bed's wrap and the take handoff are the same sample, so a take played back in place should sit exactly on the beat with no click at the seam.

https://claude.ai/code/session_01CVAnQW78ZkZfxZ2GdKV886

A take can now end where it was asked to and begin exactly where it was asked to. `record` gains `until`: the callback ends the take on that sample mid-block and the heartbeat finalizes it while the transport rolls on, and an `at` is exact in the other direction too — the request is armed before play, so the first rolling block latches on `at` itself, and a bed already rolling toward `at` waits for it as a punch-in. The chunk-1 shape, the block after the request, survives only for a take with no `at`.

The live session grows the loop-start realtime.md chunk 4 owed: a region loop between arbitrary bounds, wrapping the way the declared-extent wrap already does, with the chains ringing across it. `field play <song> --from --loop` rides it instead of being refused. A loop take is that region plus a standby transaction opened ahead of each wrap, so the frames after the wrap point go to the next take inside the same block and consecutive takes meet on consecutive input samples — proved on the null clock's sawtooth. `field record --loop` lands every pass as it arrives, the last chosen, and ctrl-c's partial pass lands last.

A file bed's transport position leads its output by the resampler's prefetch, a few samples, so a punch-out against a file bed ends that short; recorded as a known limit of the artifact path, since every record flow's bed is the song's session. Comping is what remains of chunk 4. Daemon 0.14.0 with features loop-region and record-until.

Claude-Session: https://claude.ai/code/session_01CVAnQW78ZkZfxZ2GdKV886

@minjudd minjudd left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔭 Projector review · model claude-opus-5[1m] · effort xhigh · CHANGES REQUESTED · took 3m 04s

1 finding thread: 0 resolved, 1 open — this review opens it.

The cross-thread design is the part I went at hardest, and it holds. The loop handoff passes a raw Capture* from the message thread to the callback through standbyCapture, while a unique_ptr still owns it, which is exactly the shape that goes wrong. It does not here: the callback's standbyCapture.exchange(nullptr) is what serviceCapture reads as "the callback took it", and it responds with capture = std::move(standby) before opening a new standby — so ownership follows the pointer and nothing is destroyed under a thread that is pushing into it.

Finalizing the ended take is safe for a subtler reason worth writing down: serviceCapture calls finished->finalize() with no epoch wait, unlike the stop path. It does not need one. The device callback is single-threaded, and within one invocation every push into the old target happens before captureDone = true; a later callback loads the new activeCapture. With seq_cst atomics that ordering carries to the message thread, so there is no block still writing into the object being finalized.

The sample-exact punch-out reads correctly too. cut is clamped with std::max(from, untilAt - here) so the first push can never go negative, the block's remainder n - cut goes into the standby in the same callback, and the captureDone.load() guard refuses to overwrite a take the heartbeat has not serviced yet, counting the loss instead of half-recording a pass.

The new ops follow the capability pattern #169 established rather than bumping the protocol: PROTOCOL_VERSION stays 1, the daemon advertises loop-region and record-until, and both are gated before use — record.ts:209 and play.ts:456.

Tooling: tsc --noEmit clean, full suite 702 passed / 25 skipped / 0 failed including the new 306-line punch-daemon.test.ts, project check valid, and the daemon binary under test is built from this head (0.14.0 embedded), so the daemon tests exercised this code rather than main's. The plan stays status: in-progress / priority: now and adds no unresolved questions, so no readiness gate applies. Merges cleanly with main at 24b7499.

The one finding is below: the counter this design carefully maintains never reaches the person recording.

Comment thread src/commands/record.ts Resolved
The daemon counted a pass that began with no standby ready, reported it in status, the protocol declared it, and a test asserted it was zero — and nothing printed it, so a performer looping a short region could read "6 takes landed" with no way to tell that from six passes (minjudd's finding on #170). The live line now names the pass and any passes missed, the closing tally says how many were lost, and the formatter that builds the line is tested for the case the daemon cannot be made to hit on demand.

Claude-Session: https://claude.ai/code/session_01CVAnQW78ZkZfxZ2GdKV886

@minjudd minjudd left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔭 Projector review · model claude-opus-5[1m] · effort xhigh · APPROVED · took 1m 31s

1 finding thread: 1 resolved, 0 open.

e4adcb6 is clean. The missed-pass count now reaches the person recording, on both surfaces and with the reason attached.

The fix goes past what I asked for in two ways worth naming. The tally condition is takes.length > 1 || missed > 0, so a single take with lost passes still reports — which is the case my finding was actually about, since one take from three passes is the shape that misleads. And the tally says why rather than only how many: "the region is shorter than the daemon's tick" is the sentence that turns a number into something a performer can act on by lengthening the loop.

passesNote being extracted is what makes it testable, and the new unit test pins the cases that rot quietly — zero missed prints nothing extra, one prints "1 pass", two print "2 passes", and a non-loop record prints nothing at all.

I checked one thing that looked like an off-by-one and it is not. missed = s.record.missed ?? missed sits after the state !== 'recording' break, so the poll that observes the end discards its own reading — which would matter if that reading carried anything. It cannot: player.cpp:729 only sets missed inside if (capture != nullptr) and if (captureLoop), so the field is already absent by the time the state flips. Last-seen is genuinely the best available value, and the comment calling it "the daemon's count, as last seen while the loop ran" is exactly accurate rather than hedging.

The consequence of that, worth recording rather than fixing: a pass lost inside the final 250 ms cannot be counted by anyone, because the counter disappears from status when the capture does. Closing that would mean carrying the final tally out through stop's response or the manifest, which is a different change from this one and not obviously worth it.

Tooling: tsc --noEmit clean, full suite 703 passed / 25 skipped / 0 failed, project check valid. The plan stays status: in-progress / priority: now with no new open questions. Merges cleanly with main.

The concurrency work from the previous head stands unchanged and is what most of the earlier review covered: ownership follows the pointer through standbyCapture, and serviceCapture finalizes without an epoch wait safely, because the single audio thread orders every push before captureDone.

Merging is still yours — CI had not reported when I submitted, so the green is my local run against a 0.14.0 daemon built from this branch.

@ninjudd
ninjudd merged commit ca065ec into main Sep 8, 2026
1 check passed
@ninjudd
ninjudd deleted the recording-punch-loop branch September 8, 2026 05:35
ninjudd added a commit that referenced this pull request Oct 10, 2026
The merge of main brought in recording (#166 to #170) and the remote
(#171, #172), and the manual predated both. It now has a "Record a take"
section and a "Share a project through a remote" section, rows for
record, login, logout, clone, push and pull, `fetched` in the build
vocabulary, `choose --take`, the input device list, and FIELD_REMOTE and
FIELD_TOKEN.

The play section said a live song refuses `--click` and `--from` with
`--loop`. Both work: the engine synthesizes the click, and `--from` with
`--loop` is a region loop on the live session. The section now says
what each does.

The `--version`, `devices`, `transport status`, `export` and `import`
transcripts are rerun at this head, and the install section says every
stem renders through field-audiod, not only effect inserts.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants