Skip to content

armgit / nota

Public Imported from GitHub
0

Nota — a free DAW for macOS, Windows and Linux. No subscription, no account — just open it and make music.

(fix) Retry: keep the count-in out of a retried audio take (#521) (#562)

Egor Khindikaynen5e5e1ce5c55e
diff --git a/.specify/bugs/retry-take-count-in/assessment.md b/.specify/bugs/retry-take-count-in/assessment.md
new file mode 100644
index 00000000..ea46af9a
--- /dev/null
+++ b/.specify/bugs/retry-take-count-in/assessment.md
@@ -0,0 +1,96 @@
+# Bug Assessment: Discard and retry ignores count-in
+
+- **Slug**: retry-take-count-in
+- **Created**: 2026-11-10
+- **Source**: https://github.com/nota-daw/nota/issues/521 (host: github.com — allowlisted)
+- **Verdict**: valid
+- **Severity**: medium
+
+## Report (verbatim or summarized)
+
+> **Discard and retry ignores count-in** (#521, Nota 0.59.1, Arch Linux x86_64)
+>
+> When using Discard and retry take, the recording starts immediately instead of respecting the
+> configured count-in. The count-in appears to be respected visually by the playhead, but the
+> actual recording starts immediately. This causes the playhead to lag behind the recording by
+> the duration of the count-in.
+
+Related: PR #486 (Discard and retry the take being recorded, specs/078).
+
+## Symptom
+
+An Arrangement take started from stop with a count-in, then retried (⌘. / Ctrl+.), counts in
+again on the transport, but the audio input is captured from the moment of the retry: the
+retried audio clip begins with the count-in bar(s), so everything in it sits late by the
+count-in length. Expected: nothing is captured until the count-in runs out, as on a fresh take.
+
+## Reproduction
+
+1. Set a count-in (1 bar or more); arm an audio track on a hardware input.
+2. From stop, press Record: the count-in clicks, then the take records (correct).
+3. While recording, Discard and retry take.
+4. The count-in clicks again, but the new clip holds the count-in audio at its start.
+
+MIDI takes are not affected (see below). The reporter did not say which track type;
+the "recording ahead of playhead" description matches an audio take.
+
+## Suspected Code Paths
+
+- `src/native/nota.engine/src/engine/Engine_TakeRetry.cpp:retryTake()` — for a counted-in
+ Arrangement take it queues `TransportStop`, `Seek`, `TakeRestart` (gate stays shut), then
+ `startArrangementTake(..., retryCountIn=true)` → `startCountIn()`.
+- `src/native/nota.engine/src/engine/Engine.cpp:startCountIn()` — shuts `captureGate_` on the
+ **message thread** at once, then queues `TransportPlay(bars)`.
+- `src/native/nota.engine/src/engine/Engine_Render.cpp` `case CommandType::TransportStop` →
+ `endCountIn()` — **re-opens** `captureGate_` on the audio thread when the queued Stop lands,
+ i.e. after `startCountIn()` already shut it.
+- `Engine_Render.cpp` `case CommandType::TransportPlay` (count-in branch) — starts the count-in
+ but leaves the gate as it found it: open.
+- `src/native/nota.engine/src/engine/Engine_Recording.cpp:onInputBlock()` — the hardware input
+ thread captures whenever `captureGate_` is open; it has no notion of `countInActive_`.
+
+## Root Cause Hypothesis
+
+Ordering race between threads, confidence **high**. On a fresh take nothing precedes the
+count-in, so the gate `startCountIn()` shut stays shut until the count-in's end opens it. On a
+retry, the stop queued just before is applied by the audio thread *after* the message thread
+shut the gate, and `endCountIn()` opens it again; the following `TransportPlay(bars)` starts
+the count-in without shutting it. The hardware input thread therefore pushes the count-in into
+the take, which starts at the take's start beat — so the take's audio is late by the count-in.
+MIDI capture and the internal (resampling) tap gate on `countInActive_` on the audio thread as
+well, which is why they stay correct.
+
+## Proposed Remediation
+
+**Preferred**: make the audio thread's count-in start shut the capture gate itself — in
+`case CommandType::TransportPlay`, when it starts a count-in, `captureGate_.store(false)`. The
+gate is then shut in the same order as the commands that open it (Stop/`endCountIn`, the
+count-in end), whatever was queued before. `startCountIn()` keeps its immediate store for the
+window before the command lands.
+
+**Alternatives**:
+- Don't queue `TransportStop` in the retry when the count-in follows — would need a separate
+ "rewind and count in" command; more surface for the same effect.
+- Make `onInputBlock` also check a published `countInActive_` — a second flag for the same
+ state.
+
+**Files likely to change**:
+- `src/native/nota.engine/src/engine/Engine_Render.cpp`
+- a test hook to drive the hardware input path between offline renders
+ (`include/nota/nota_engine.h`, `src/abi/…`, `Nota.Infrastructure` interop)
+- `tests/Nota.SmokeTest/RetryTakeTests.cs`
+
+**Tests to add or update**:
+- Retry of a counted-in audio take on a (test) hardware input, with input frames pushed through
+ the real input-thread path every render block: the retried clip starts at the take start and
+ is about as long as the time recorded after the count-in, not count-in + that.
+
+## Risks & Considerations
+
+- The Session count-in also uses `TransportPlay(bars)`; it already shuts the gate in
+ `startCountIn()`, so the extra audio-thread store changes nothing there.
+- A retry with `countInBars() == 0` takes the `!countedIn` path and is untouched.
+
+## Open Questions
+
+- None blocking.
diff --git a/.specify/bugs/retry-take-count-in/fix.md b/.specify/bugs/retry-take-count-in/fix.md
new file mode 100644
index 00000000..fbe2b633
--- /dev/null
+++ b/.specify/bugs/retry-take-count-in/fix.md
@@ -0,0 +1,55 @@
+# Bug Fix: Discard and retry ignores count-in
+
+- **Slug**: retry-take-count-in
+- **Fixed**: 2026-11-10
+- **Assessment**: ./assessment.md
+- **Status**: applied
+
+## Summary
+
+The audio thread now shuts the capture gate itself when it starts a count-in. Before, a
+retried take queued a Stop ahead of the count-in, and the Stop's `endCountIn()` reopened the
+gate that `startCountIn()` had already shut on the message thread. With the gate open, the
+hardware input recorded the count-in into the take.
+
+## Changes
+
+| File | Change | Notes |
+|------|--------|-------|
+| `src/native/nota.engine/src/engine/Engine_Render.cpp` | modified | `TransportPlay` count-in branch: `captureGate_.store(false)` |
+| `src/native/nota.engine/src/engine/Engine.h` | added | `inputBlockForTest()`: one block through `onInputBlock` (the real input-thread path) |
+| `src/native/nota.engine/include/nota/nota_engine.h`, `src/abi/nota_engine_track.cpp` | added | `nota_engine_input_block_test` |
+| `src/managed/Nota.Infrastructure/Interop/NativeMethods.cs`, `NotaEngine.cs` | added | `InputBlockTest` wrapper |
+| `tests/Nota.SmokeTest/RetryTakeTests.cs` | added test | `AudioFromStopWithCountIn` + `LiveBeats` helper |
+| `CHANGELOG.md` | modified | `[Unreleased]` → Fixed entry |
+
+## Diff Highlights
+
+```cpp
+case CommandType::TransportPlay:
+ if (c.i0 > 0 && !transport_.isPlaying()) { // count-in first (i0 = bars)
+ ...
+ captureGate_.store(false, std::memory_order_release);
+```
+
+## Tests Added or Updated
+
+- `RetryTakeTests.AudioFromStopWithCountIn`: an audio track on a 2-channel test input, a
+ 1-bar count-in from beat 8, retried two beats into the take. Input blocks go through the
+ real `onInputBlock` gate before every render block. The kept clip must start at beat 8 and
+ be about 2 beats long.
+
+## Local Verification
+
+- `Nota.SmokeTest --retry-take` with the engine change reverted: the new check **fails**
+ (`at 8,00, 6,01 beats, want 2`), which reproduces the bug.
+- `Nota.SmokeTest --retry-take` with the fix: `RETRY TAKE PASSED` (`at 8,00, 2,02 beats`).
+- `scripts/build.sh` (full build + smoke test): see test.md.
+
+## Deviations from Assessment
+
+None.
+
+## Follow-ups
+
+- None.
diff --git a/.specify/bugs/retry-take-count-in/test.md b/.specify/bugs/retry-take-count-in/test.md
new file mode 100644
index 00000000..ef21ecf0
--- /dev/null
+++ b/.specify/bugs/retry-take-count-in/test.md
@@ -0,0 +1,59 @@
+# Bug Verification: Discard and retry ignores count-in
+
+- **Slug**: retry-take-count-in
+- **Tested**: 2026-11-10
+- **Assessment**: ./assessment.md
+- **Fix**: ./fix.md
+- **Result**: partial
+
+## Summary
+
+The automated reproduction fails without the fix and passes with it. Three real-time
+render-pool timing checks fail in the full smoke run, on a machine under heavy unrelated load
+(load average ~9, Rider.Backend at ~420 % CPU). These code paths are not touched by the fix,
+but they have not been checked on an idle machine. The bug has not yet been re-checked in the
+app on a real audio interface.
+
+## Checks Performed
+
+| Check | Command / Action | Result | Notes |
+|-------|------------------|--------|-------|
+| Reproduction (pre-fix) | `Nota.SmokeTest --retry-take`, engine change reverted | fail (as expected) | retried clip 6,01 beats = count-in + take |
+| Reproduction (post-fix) | `Nota.SmokeTest --retry-take` | pass | retried clip at 8,00, 2,02 beats |
+| New / updated tests | `RetryTakeTests.AudioFromStopWithCountIn` | pass | input fed through the real `onInputBlock` gate every block |
+| Retry suite | `Nota.SmokeTest --retry-take` | pass | `RETRY TAKE PASSED` (MIDI count-in, punch-in, Session count-in included) |
+| Full build + smoke | `scripts/build.sh` | fail (3) | render-pool deadline checks only; same 3 on a rerun |
+| Full smoke, 3rd run | `Nota.SmokeTest` | fail (2) | only the 2 "overloaded" checks; load average 24, the VST3 deadline check passed this time |
+| Manual, real interface | app, armed audio track, count-in, ⌘. | not-run | no hands-on run in this session |
+
+## Output Excerpts
+
+```
+[FAIL] four indivisible VST3 jobs: 12/100 device deadlines missed after warm-up (<= 1)
+[FAIL] isolated real VST3 render-pool regressions
+[FAIL] overloaded: 261 of 4125 blocks late with render threads after warm-up (<= 1 %)
+[FAIL] overloaded: at most 12 late blocks within 50 ms (<= 4: …)
+SMOKE TEST FAILED (3)
+```
+
+Every other check passes.
+
+The failing checks (`RenderPoolTests` 3b, `HostedRenderPoolTests`) are wall-clock deadline
+budgets. They never start a count-in, so the `TransportPlay` count-in branch the fix touches
+does not run in them. They go through `TimingBudget.Check`, which is not enforced under CI
+because it is load-sensitive. How many fail tracks the machine load (3 → 2 as load went
+9 → 24 with Rider indexing), not the change.
+
+## Residual Risks
+
+- The render-pool timing checks have not had a clean local pass on an idle machine. CI does
+ not enforce them (`TimingBudget`).
+- The test uses the pretend input device, not a real driver callback thread. The race it pins
+ (the Stop command reopening the gate) is in engine command order and does not depend on the
+ driver.
+
+## Recommendation
+
+The fix holds in the automated reproduction. Every non-timing check passes. Before closing,
+hand-check ⌘. on an audio take with a count-in in the app on a real interface. Then open the
+PR with `Closes #521`.
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0b643dad..528a51ec 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -457,6 +457,10 @@ Entry categories: `Added`, `Changed`, `Fixed`, `Removed`.
MCP: `list_audio_inputs`, `set_track_input`, `get_track_input`.
### Fixed
+- **Discard and retry keeps the count-in out of an audio take** (#521): retrying an audio take
+ that counted in from stop counted in again, but the input was recorded from the moment of the
+ retry, so the new clip began with the count-in and ran late against the playhead by its
+ length. The input is now kept out until the count-in runs out, as on a fresh take.
- **Compact Mixer: the MIDI-in selector shows its whole name** (#550): the drop-down on an
instrument strip left 20 px for the chosen name, so "Aucune" read "ucun" and a longer track
name lost both ends. It now leaves about 29 px, writes the name from the left and ends a longer
diff --git a/src/managed/Nota.Infrastructure/Interop/NativeMethods.cs b/src/managed/Nota.Infrastructure/Interop/NativeMethods.cs
index e28c7624..1c4e773d 100644
--- a/src/managed/Nota.Infrastructure/Interop/NativeMethods.cs
+++ b/src/managed/Nota.Infrastructure/Interop/NativeMethods.cs
@@ -210,6 +210,9 @@ internal static partial class NativeMethods
[LibraryImport(Lib, EntryPoint = "nota_take_record_test_armed_disarm")]
internal static partial int TakeRecordTestArmedDisarm(IntPtr engine, float[] interleaved, int channels, int frames, double startBeat, int disarmTrackId, int disarmAtFrame);
+ [LibraryImport(Lib, EntryPoint = "nota_engine_input_block_test")]
+ internal static partial void InputBlockTest(IntPtr engine, float[] interleaved, int frames, int channels);
+
[LibraryImport(Lib, EntryPoint = "nota_engine_record_held_notes")]
internal static partial int RecordHeldNotes(IntPtr engine, [Out] NotaRecordHeldNote[] outNotes, int maxN,
out int trackId, out int sessionScene, out double nowBeat);
diff --git a/src/managed/Nota.Infrastructure/Interop/NotaEngine.cs b/src/managed/Nota.Infrastructure/Interop/NotaEngine.cs
index d7acf677..d32d0bde 100644
--- a/src/managed/Nota.Infrastructure/Interop/NotaEngine.cs
+++ b/src/managed/Nota.Infrastructure/Interop/NotaEngine.cs
@@ -198,6 +198,10 @@ public sealed partial class NotaEngine : IAudioEngine
/// <summary>Tests: as <see cref="TakeRecordTestArmed"/>, with one track disarmed partway (specs/086).</summary>
public int TakeRecordTestArmedDisarm(float[] interleaved, int channels, int frames, double startBeat, int disarmTrackId, int disarmAtFrame)
{ ThrowIfDisposed(); return NativeMethods.TakeRecordTestArmedDisarm(_handle, interleaved, channels, frames, startBeat, disarmTrackId, disarmAtFrame); }
+ /// <summary>Tests (#521): one device block through the input thread's path — the capture gate,
+ /// then the take ring (see <see cref="SetTestInputDevice"/>); the next poll drains it.</summary>
+ public void InputBlockTest(float[] interleaved, int frames, int channels)
+ { ThrowIfDisposed(); NativeMethods.InputBlockTest(_handle, interleaved, frames, channels); }
public int RecordHeldNotes(NotaRecordHeldNote[] buffer, out int trackId, out int sessionScene, out double nowBeat)
{ ThrowIfDisposed(); return NativeMethods.RecordHeldNotes(_handle, buffer, buffer.Length, out trackId, out sessionScene, out nowBeat); }
diff --git a/src/native/nota.engine/include/nota/nota_engine.h b/src/native/nota.engine/include/nota/nota_engine.h
index 0cc5adcb..5f8071d5 100644
--- a/src/native/nota.engine/include/nota/nota_engine.h
+++ b/src/native/nota.engine/include/nota/nota_engine.h
@@ -1315,6 +1315,12 @@ NOTA_API int32_t nota_take_record_test_armed_disarm(NotaEngine* engine, const
int32_t frames, double start_beat, int32_t disarm_track_id,
int32_t disarm_at_frame);
+/* Smoke-test hook (#521): one block of `channels`-wide device frames handed over as the input
+ * thread does — through the capture gate (shut while a count-in clicks), into the take ring.
+ * Pair with nota_engine_set_test_input_device; the next poll drains it into the take. */
+NOTA_API void nota_engine_input_block_test(NotaEngine* engine, const float* interleaved, int32_t frames,
+ int32_t channels);
+
/* Decoded-sample metadata by id; returns 1 if found, else 0. */
NOTA_API int32_t nota_sample_get_info(const NotaEngine* engine, int64_t sample_id, NotaSampleInfo* out);
/* Copies up to `cap` interleaved floats into `out`; returns the total count
diff --git a/src/native/nota.engine/src/abi/nota_engine_track.cpp b/src/native/nota.engine/src/abi/nota_engine_track.cpp
index 1c685af3..99912b2d 100644
--- a/src/native/nota.engine/src/abi/nota_engine_track.cpp
+++ b/src/native/nota.engine/src/abi/nota_engine_track.cpp
@@ -999,6 +999,9 @@ int32_t nota_take_record_test_n(NotaEngine* e, int32_t track_id, const float* in
int32_t frames, double start_beat) {
return (e && ENG(e)->takeRecordTestN(track_id, interleaved, channels, frames, start_beat)) ? 1 : 0;
}
+void nota_engine_input_block_test(NotaEngine* e, const float* interleaved, int32_t frames, int32_t channels) {
+ if (e) ENG(e)->inputBlockForTest(interleaved, frames, channels);
+}
int32_t nota_sample_get_info(const NotaEngine* e, int64_t sample_id, NotaSampleInfo* out) {
return (e && CENG(e)->sampleInfo(sample_id, out)) ? 1 : 0;
}
diff --git a/src/native/nota.engine/src/engine/Engine.h b/src/native/nota.engine/src/engine/Engine.h
index 8cde4da2..cc687cc6 100644
--- a/src/native/nota.engine/src/engine/Engine.h
+++ b/src/native/nota.engine/src/engine/Engine.h
@@ -1428,6 +1428,10 @@ class Engine {
bool takeRecordTestN(int32_t trackId, const float* interleaved, int32_t channels, int32_t frames, double startBeat);
int32_t takeRecordTestArmed(const float* interleaved, int32_t channels, int32_t frames, double startBeat,
int32_t disarmTrackId = 0, int32_t disarmAtFrame = -1);
+ // One device block as the input thread hands it over (#521): the gate, the epoch marker, the ring.
+ void inputBlockForTest(const float* interleaved, int32_t frames, int32_t channels) {
+ if (interleaved && frames > 0 && channels > 0) onInputBlock(interleaved, frames, channels);
+ }
bool takeRecordTestImpl(int32_t trackId, const float* interleaved, int32_t channels, int32_t frames, double startBeat,
int32_t first, int32_t width);
diff --git a/src/native/nota.engine/src/engine/Engine_Render.cpp b/src/native/nota.engine/src/engine/Engine_Render.cpp
index ef9bf335..68894f17 100644
--- a/src/native/nota.engine/src/engine/Engine_Render.cpp
+++ b/src/native/nota.engine/src/engine/Engine_Render.cpp
@@ -1021,6 +1021,9 @@ void Engine::drainCommands() {
countInTotal_ = static_cast<double>(c.i0) * std::max(1, transport_.beatsPerBar());
lastCountInBeat_ = -1;
uiCountIn_.store(countInTotal_, std::memory_order_relaxed);
+ // Shut the capture here too, in command order: a Stop queued just before (a
+ // retried take, #521) reopened the gate startCountIn() had shut.
+ captureGate_.store(false, std::memory_order_release);
} else {
endCountIn();
transport_.play();
diff --git a/tests/Nota.SmokeTest/RetryTakeTests.cs b/tests/Nota.SmokeTest/RetryTakeTests.cs
index 842b088e..583896e0 100644
--- a/tests/Nota.SmokeTest/RetryTakeTests.cs
+++ b/tests/Nota.SmokeTest/RetryTakeTests.cs
@@ -22,6 +22,7 @@ internal static class RetryTakeTests
foreach (var r in Catalog()) yield return r;
foreach (var r in NothingRecording()) yield return r;
foreach (var r in MidiFromStopWithCountIn()) yield return r;
+ foreach (var r in AudioFromStopWithCountIn()) yield return r;
foreach (var r in OverdubIntoClip()) yield return r;
foreach (var r in PunchIn()) yield return r;
foreach (var r in HeldAcrossRetry()) yield return r;
@@ -620,6 +621,58 @@ internal static class RetryTakeTests
$"retry: nothing recording changes nothing ({r})");
}
+ // #521: an audio take on a hardware input counted in from stop, then retried. The retry
+ // counts in again, and the input stays out of the take until the count-in runs out — the new
+ // clip starts at the take start and holds only what came after it, not the count-in too.
+ private static IEnumerable<(bool, string)> AudioFromStopWithCountIn()
+ {
+ using var e = new NotaEngine();
+ e.SetBpm(120);
+ e.SetMetronome(false);
+ e.SetTestInputDevice(2);
+ e.RenderOffline(new float[Chunk * 2], Chunk, Sr);
+ int rec = e.AddAudioTrack();
+ e.SetTrackInput(rec, InputPick.Pair(0));
+ e.SetTrackArmed(rec, true);
+ e.StopTransport();
+ e.Seek(8.0);
+ LiveBeats(e, 0.01);
+ e.SetCountIn(1);
+ e.SetRecording(true);
+ LiveBeats(e, 6.0); // the bar of count-in, then two beats of take
+
+ var r = e.RetryTake();
+ LiveBeats(e, 0.01);
+ yield return (r == RetryTakeResult.Arrangement && e.IsRecording && e.CountInBeats > 0 && !e.IsPlaying,
+ $"retry audio count-in: counting in again ({r}, count-in {e.CountInBeats:F2})");
+ LiveBeats(e, 6.0); // the bar of count-in again, then two beats
+ e.SetRecording(false);
+ LiveBeats(e, 0.01);
+
+ NotaAudioClipInfo clip = default;
+ double beats = -1.0;
+ bool got = AudioClips(e, rec) == 1 && e.TryGetAudioClipInfo(rec, 0, out clip) && clip.SampleId != 0;
+ if (got && e.TryGetSampleInfo(clip.SampleId, out var info)) beats = info.Frames / Spb;
+ yield return (got && Near(clip.StartBeat, 8.0) && Math.Abs(beats - 2.0) < 0.25,
+ $"retry audio count-in: the take holds what came after the count-in, from the take start (at {clip.StartBeat:F2}, {beats:F2} beats, want 2)");
+ e.StopTransport();
+ }
+
+ // Render and poll as the device would, the input thread handing over a block of input
+ // (both channels at 0.5) before each one — through the capture gate, as a real device does.
+ private static void LiveBeats(NotaEngine e, double beats)
+ {
+ var b = new float[Chunk * 2];
+ var input = Enumerable.Repeat(0.5f, Chunk * 2).ToArray();
+ int frames = (int)Math.Round(beats * Spb);
+ for (int done = 0; done < frames; done += Chunk)
+ {
+ e.InputBlockTest(input, Chunk, 2);
+ e.RenderOffline(b, Chunk, Sr);
+ e.Poll();
+ }
+ }
+
// The issue's case: a take from stop with a count-in. Retry drops it (and the clip it made),
// returns to the take start, counts in again and records; the kept take is one undo step.
private static IEnumerable<(bool, string)> MidiFromStopWithCountIn()