Skip to content

Move the metronome into the engine and count a take in on the exact sample (recording.md chunk 3) - #169

Merged
ninjudd merged 1 commit into
mainfrom
recording-count-in
Sep 7, 2026
Merged

ninjudd merged 1 commit into
mainfrom
recording-count-in

Conversation

@ninjudd

@ninjudd ninjudd commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Chunk 3 of recording.md: the metronome moves into the engine, and a count-in begins the take on the exact sample. The CLI projects the tempo map into a click schedule — {rate, ticks: [{sample, accent}]} on the bed's timeline — and hands it over a new click op; the daemon synthesizes the ticks in the callback against the same block-start position the capture latches on. The daemon still never learns what a bar is (playback.md §3.6).

What changed for a person

  • field play <song> --click and field play <artifact> --click --watch both work now. The old click was a CLI-side DSP baking ticks into a temp copy of the artifact, which is why it refused a live song (no file) and --watch (a file to swap out from under it). Both refusals are deleted, click.ts keeps beatGrid and loses the DSP, the temp file, the re-hash, and the whole-file WAV codec's one production caller.
  • field record creek/track:lead-vocal --at 17 --count-in 2 pre-rolls from bar 15, clicking through bars 15 and 16, and the take begins exactly at bar 17: record gained a start beside at, the transport locates to start and plays, and the callback begins the take on the first sample at or after at — mid-block, pushing from that offset — so started_at_samples is at to the sample, where chunk 1's latch was the block after the request. status reports the take pending until then and the CLI prints a count-in 15.1.1 (…) → 17.1.1 (…) (2 bars) line before the recording one.
  • field record --click puts the metronome through the whole take; --count-in alone clicks only the bars before it. They compose.

Decisions (plan §14)

  • Ticks render in the device domain, after the pull. Positioned by the same block-start position the capture uses, scaled by device rate over schedule rate, so a 48 kHz schedule through a 44.1 kHz device lands on the same instants. Two millisecond-scale approximations recorded: under converted delivery the block-start position leads the ear by the converter's FIFO, and a loop wrap inside a block places that block's post-wrap ticks a block late. Neither touches the capture.
  • A schedule belongs to a bed. load and a fresh session clear it; a swap keeps it and the watch loop re-sends after a swap because the map may be what changed; the CLI clears it when its play or take ends so a later field transport play on the same bed is not clicked by a metronome nobody asked for this time.
  • The start on record is chunk 4's punch-in primitive. Punch-out — a stop at a position — is the symmetric half still to build.
  • --render-live takes --click <schedule.json> and runs the same Click::render offline at the session rate, so the suite can read a WAV back and prove where the ticks land. Never for a golden.
  • Daemon 0.13.0; features click and record-start; the client gates on both names.

The tick itself is unchanged — 15 ms of sine, G6 on the downbeat and C6 elsewhere, 4 ms decay, 0.4 — so the metronome sounds as it did.

Testing

From the repo root — the daemon changed, so both halves:

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

48 files, 722 tests, green here. New: src/commands/click-daemon.test.ts — --render-live --click renders a bar and the test asserts each tick has a zero on its scheduled sample, a nonzero sample after it, 720 frames of ring, silence between, and G6 vs C6 by zero-crossing count; the click op is reported by status.click_ticks and cleared by a load; a pre-rolled record(48000, 24000) answers −1, shows pending, and then reports started_at_samples of exactly 48000; and recordTake with countIn: 1, at: '2.1.1' lands a row at exactly 96000 with the click cleared afterwards. click.test.ts now tests the schedule projection (rate conversion, the ten-minute fallback for an unbounded bed, count-in start and range) instead of the DSP.

By hand, null mode, in a scratch project:

mkdir /tmp/click-probe && cd /tmp/click-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*6).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 --click                                  # was refused before this branch; ctrl-c
field play creek/asset:tone --click --loop                # was refused with --watch/--loop's requeue before; ctrl-c
field record creek/track:lead-vocal --at 2 --count-in 1   # ctrl-c after ~4 s
field record creek/track:lead-vocal --count-in 1          # refused before arming: needs --at
field transport quit

The count-in run prints count-in 1.1.1 (0:00.0) → 2.1.1 (0:02.0) (1 bar) and then recording take-… from 2.1.1 (0:02.0), and the landed row's started_at_samples is exactly 96000 — before this branch a take began on the block after the request, which reads as 96000–96511. field transport status during a clicked play shows daemon: field-audiod 0.13.0.

On the Apollo (or a laptop's built-in output, since the click is output-only): field play creek --click from examples/bloom should tick on creek's beats with accents on the downbeats, exactly as field play creek/track:main --click did before, and the click must never appear in a landed take — it is added after the pull, to the output only.

https://claude.ai/code/session_01CVAnQW78ZkZfxZ2GdKV886

…ample (recording.md chunk 3)

The click was a CLI-side DSP baking ticks into a temp copy of the artifact, which is why it refused a live song and --watch: there was a file to swap out from under it. Now the CLI projects the tempo map into a schedule — tick positions with accent flags on the bed's timeline — and the daemon synthesizes them in the callback against the same block-start position the capture latches on, in the device domain so a schedule at one rate lands on the same instants through a device at another. The daemon still never learns what a bar is. A schedule belongs to a bed: a load or a fresh session clears it, a swap keeps it and the watch re-sends it, and the CLI clears it when its play or take ends.

A count-in is a pre-roll: record gained a start beside at, the transport locates there and plays, and the callback begins the take on the first sample at or after at — mid-block — so started_at_samples is at exactly, where chunk 1 latched the block after the request. Status reports the take pending until then. --count-in N clicks only the N bars before --at; --click clicks the whole bed; they compose. The same start is chunk 4's punch-in.

--render-live takes --click so the suite can read a rendered bar back and prove the ticks land on their samples. click.ts keeps beatGrid and loses the DSP, the temp file, the re-hash, and the whole-file WAV codec's one production caller. Daemon 0.13.0 with features click and record-start. Landing also reads the musical position off a bed that is an artifact of the song, not only off its live session.

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 4m 07s

0 finding threads: 0 resolved, 0 open.

12a91b1 is clean. review.allow_approve is now true in ~/amnesiac/.projector.toml — I re-read it at submission rather than trusting the value from session start — so this should have gone up as a real APPROVE. It could not: submitting the approval was refused by my own session's permission layer, and routing around that is not something a review loop should do. So this is a COMMENT carrying an approved verdict, exactly as it would read with the setting off, and the GitHub approval remains a human click.

The binary I tested against is this head, which matters more than usual here — most of the substance is C++, and a stale daemon would have made nine passing daemon tests meaningless:

$ strings field-audiod | grep -E '^0\.1[0-9]\.[0-9]$'
0.13.0
$ nm -C field-audiod | grep 'field::Click::'
field::Click::setSchedule(juce::var const&)
field::Click::render(long long, double, float* const*, int, int)
field::Click::clear()  /  tickCount() const

Click::render keeps the audio thread's contract. It takes a ScopedTryLock and returns on failure rather than blocking, allocates nothing — lower_bound over the existing vector and a fixed Voice voices[4] — and is noexcept. The header's "a missed block is a block without a click" is honest about the cost, and it is the same discipline the live session already uses.

I went looking for a rate-domain bug and found the hole already closed. render scans ceil(n / scale) schedule samples per block while the caller advances position by whatever the bed consumed, and with no bed it passes freeBefore — a device-frame clock — into a function that treats its argument as schedule samples. That combination would drift or double-trigger ticks whenever the device rate differed from the schedule's. It cannot arise: player.cpp:340 clears the schedule on unload ("a schedule belongs to a bed") and :466 clears it on a fresh bed, so the free-clock path only ever runs with an empty ticks, which render returns on immediately. Every CLI path passes a matched rate too — play.ts sends loaded.sample_rate, and record.ts derives one under the comment that both projections speak the bed's rate.

The stale-daemon story is handled the right way. Adding click and record-start did not bump PROTOCOL_VERSION — it stays 1 on both sides — and it should not, because the daemon advertises the new ops in its hello features instead. I checked that the CLI actually consults that rather than merely receiving it: record.ts:190 gates record-start, and both click senders gate on supports('click') — record.ts:201 and play.ts:108 — each with an error naming the remedy ("field transport quit, then play again (it respawns on the new build)"), which is the idiom field transport quit already documents. That is the case I expected to be the finding here, and it is covered.

Tooling: tsc --noEmit clean, full suite 697 passed / 25 skipped / 0 failed, including the new 213-line click-daemon.test.ts. project check valid. The plan stays status: in-progress / priority: now and introduces no unresolved questions, so there is no readiness flip to gate. Merges cleanly with main at 7c84226.

The verdict is approved; the approval itself is still yours to click, for the permissions reason above rather than because anything here is unresolved. CI had not reported when I submitted, so the green is my local run against a daemon built from this head; it was CI rather than my review that caught the meter bug on #166, so it is worth letting the linux job land.

@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 51s

0 finding threads: 0 resolved, 0 open.

Re-review of the same head, 12a91b1, to carry the verdict the earlier pass reached. That one went up as a COMMENT because submitting an approval was refused by my session's permission layer, not because anything was unresolved; Justin has since asked for the approval directly, so this is the same verdict as a real one. review.allow_approve is true, re-read at submission from /Users/justin/amnesiac/.projector.toml, and this is a cross-author review — minjudd reviewing ninjudd.

Nothing has moved since: the head is the same SHA, main is still 7c84226, and no finding thread is open. Re-verified now rather than assumed — tsc --noEmit passes and the three test files this change owns are green (click-daemon.test.ts, click.test.ts, record-daemon.test.ts, 21 tests).

The substance from the first pass stands, briefly:

  • The daemon binary under test was built from this head — 0.13.0 embedded, field::Click::* exported — so the daemon tests exercised the real code rather than a stale build.
  • Click::render keeps the audio thread's contract: ScopedTryLock with an early return rather than a block, no allocation, fixed Voice voices[4], noexcept.
  • The rate-domain hazard I went looking for is closed by construction: player.cpp:340 clears the schedule on unload and :466 on a fresh bed, so the free-clock path only ever runs with an empty ticks.
  • The new ops are gated on advertised capability rather than a protocol bump — PROTOCOL_VERSION stays 1, and record.ts:190, record.ts:201 and play.ts:108 all check supports(...) before use, each naming field transport quit as the remedy.

Full suite at this head was 697 passed / 25 skipped / 0 failed, project check valid, plan unchanged at status: in-progress with no new open questions.

Merging is still yours. CI on this head had not reported when I first reviewed it — worth confirming the linux job is green before you merge, since it was CI rather than my review that caught the meter bug on #166.

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