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) Long audio files no longer stall the app

Egor Khindikaynen77a2620bd1cf
diff --git a/.specify/bugs/long-audio-clip-editor/assessment.md b/.specify/bugs/long-audio-clip-editor/assessment.md
new file mode 100644
index 00000000..35659137
--- /dev/null
+++ b/.specify/bugs/long-audio-clip-editor/assessment.md
@@ -0,0 +1,171 @@
+# Bug Assessment: Long audio files — import stutter, BPM-edit freeze, unreadable clip-editor ruler and waveform
+
+- **Slug**: long-audio-clip-editor
+- **Created**: 2026-10-13
+- **Source**: pasted text (+ two screenshots)
+- **Verdict**: valid (items 2–4 confirmed in code; item 1 likely valid, needs a timing measurement to pin down)
+- **Severity**: high
+
+## Report (verbatim or summarized)
+
+> А теперь давай поработаем с длинными файлами аудио:
+> * когда я перемещаю большой flac файл на аранжировку через драг дроп, есть значительное подтормаживание
+> * клип по умолчанию варпится (это нормально), но при попытке поменять BPM через clip editor, когда я кликаю на текстбокс редактирования BPM, происходит зависание
+> * на длинных файлах clip editor выглядит страшно (приложил скриншот), текст накладывается друг на друга, нужно по-умному скрывать текст и перерисовывать сетку
+> * второй скриншот: при сильном приближении в редакторе клипов временная диаграмма не перестраивается, не читабельно (нужно исправить, оптимизировано)
+
+Screenshot 1: clip editor, long warped clip at fit-to-width — the ruler draws a label on every bar
+(`1 2 3 … 10 11 12 …`), the digits overlap into an unreadable strip, and the beat grid through the
+wave is a dense comb. Screenshot 2: clip editor zoomed in to ~1.2–6.4 bars — the waveform is a row of
+wide flat blocks (one per peak bucket), with no detail inside them.
+
+## Symptom
+
+With a long (multi-minute) FLAC: (1) dropping it on the arrangement makes the UI stutter noticeably;
+(2) clicking the segment BPM chip in the clip editor (to type a BPM) freezes the app; (3) the clip
+editor's ruler labels collide and the grid is a solid comb; (4) zooming in the clip editor only
+magnifies a fixed 1024-bucket overview, so the waveform turns into blocks. Expected: a smooth drop,
+an instant BPM field, a ruler that thins labels/grid by zoom, and a waveform that re-resolves to
+pixel detail as you zoom in.
+
+## Reproduction
+
+1. Drag a long FLAC (e.g. 5–10 min, 44.1/48 kHz stereo) from Finder or the browser onto an arrangement lane.
+ → UI stutters during "Warping … %" (and possibly during decode) [NEEDS CLARIFICATION: which phase stutters — the "Processing… %" decode or the "Warping… %" stretch? Status bar text identifies it].
+2. Open the clip in the clip editor (it is warped by default). Fit-to-width → ruler labels overlap (item 3).
+3. Single- or double-click the BPM chip in the marker strip → app hangs for seconds (item 2).
+4. Zoom in strongly (Ctrl+wheel) → waveform drawn as wide flat blocks (item 4).
+
+## Suspected Code Paths
+
+### (2) BPM click freeze — confirmed
+- `src/managed/Nota.App/AudioClipEditorView.cs:810-823` — a press on a BPM chip starts a scrub
+ (`_bpmScrubSeg = b.seg`), or on the 2nd click opens the text box.
+- `src/managed/Nota.App/AudioClipEditorView.cs:1029` — on release, `if (_bpmScrubSeg >= 0) { …; Commit(); }`
+ commits **unconditionally**, even when the pointer never moved. A double-click therefore commits
+ twice (press 1 release + press 2 release) before the text box even appears, and Enter commits a third time.
+- `AudioClipEditorView.cs:72-77` — `MarkersCommitted` → `_engine.SetClipWarpMarkers` → `Reload()` → `_onChanged()`.
+- `src/native/nota.engine/src/Engine_Tracks.cpp:522-541` `Engine::setClipWarpMarkers` → `configureClipWarp`
+ → `buildWarpCache` (`Engine_Tracks.cpp:162-191`) — runs the stretcher **synchronously over the whole
+ played window** on the calling (UI) thread. For a multi-minute file that is seconds of work per
+ commit. The deferred/sliced path (`SetDeferWarpBuild` + `WarpBuildStep`) is only used by import and
+ project load, never by editor edits.
+- Same synchronous rebuild hits every other warp edit on a long clip: marker drag release
+ (`Commit()`), `setClipWarpTrim` (`Engine_Tracks.cpp:546-563`), pitch, warp mode, project-tempo change.
+
+### (1) Drag-drop stutter — likely
+- `src/managed/Nota.App/MainWindow.AudioImport.cs:100-113` — after a background decode, the clip is
+ auto-warped with `SetDeferWarpBuild(true)`, then `BuildImportWarpAsync` (`:220-230`) builds the
+ cache **on the UI thread** in `WarpBuildStep(48000)` slices, yielding at `Background` priority
+ between slices. Each slice stretches ~1 s of device audio (3 × `kWarpBuildChunk` = 16384 frames);
+ if a slice costs tens of ms, every frame during "Warping… %" is late → a visible stutter that lasts
+ as long as the file (hundreds of slices for a long file).
+- `MainWindow.AudioImport.cs:63-74,186-193` — decode ticks every 50 ms allocate a 2048-bucket peak array
+ and repaint the timeline; cheap, but adds to the load during the decode phase.
+- `src/managed/Nota.App/ArrangementView.cs:770-777` — every `Timeline.Refresh()` recomputes 2048 peaks
+ for changed audio clips (fast via `SampleBuffer::peakTable`; unlikely to be the culprit).
+
+### (3) Ruler label overlap / dense grid — confirmed
+- `src/managed/Nota.App/AudioClipEditorView.cs:1328-1349` `DrawRuler` — `step` is either 1 beat
+ (`beatW >= 30`) or 4 beats (one bar). There is no coarser step, so with ~1–10 px per bar every bar
+ gets a label and a `FormattedText` allocation per bar per frame.
+- `AudioClipEditorView.cs:1133-1144` — the beat grid through the wave draws **every beat** (no
+ thinning by pixel density), producing the comb in screenshot 1 and thousands of lines per render.
+- `AudioClipEditorView.cs:1351-1365` (unwarped seconds ruler) — has a step ladder but caps at 60 s;
+ a very long unwarped file at fit-to-width can still collide.
+
+### (4) Waveform doesn't re-resolve on zoom — confirmed
+- `src/managed/Nota.App/AudioClipEditorView.cs:293-311` `Reload()` — fetches a fixed **1024**
+ buckets for the whole clip (`GetClipWarpFullPeaks` / `GetClipSourcePeaks`) once, on reload only.
+- `AudioClipEditorView.cs:1153-1175` — zoom just widens each bucket (`bw = _zoom * w / _count`);
+ at `MaxZoom = 60` (`:666`) a 1024-bucket overview shows ~17 buckets per screen → blocks.
+ One `FillRectangle` per bucket.
+- Engine already has the building block: `warpPeaksRange(clip, out, n, beat0, beat1)`
+ (`Engine_Tracks.cpp:808-836`) peak-picks any beat range; `getClipWarpFullPeaks` just calls it with
+ `[0, warpBeats]`. There is no range variant for the unwarped source view (`getClipSourcePeaks`,
+ `:889-906`, always whole file). `SampleBuffer::peakRange` (`SampleBuffer.h:83-94`) is
+ block-table accelerated, so range queries are cheap.
+
+## Root Cause Hypothesis
+
+(2) **High confidence.** A plain click on the BPM chip is treated as a zero-length scrub and committed
+on release; each commit synchronously re-stretches the entire long file on the UI thread
+(`buildWarpCache`), and a double-click does this twice before the text field shows — hence a
+multi-second "freeze when clicking the BPM box". (3) and (4) **High confidence**: the ruler/grid have
+no density-based thinning beyond one bar, and the editor waveform is a fixed 1024-bucket overview
+that is never re-queried for the visible window. (1) **Medium confidence**: the import's warp-cache
+build runs on the UI thread in ~1 s-of-audio slices; on a long file that is hundreds of slices, each
+long enough to drop frames.
+
+## Proposed Remediation
+
+**Preferred**:
+
+1. *BPM chip* — track movement: only commit a scrub if the BPM actually changed (or pointer moved > ~2 px);
+ a click without movement is a no-op; double-click opens the text box without any prior commit.
+ Commit on Enter/LostFocus only if the typed value differs from the current one.
+2. *Warp edits off the UI thread* — make editor-driven warp edits (`SetClipWarpMarkers`, `SetClipWarpTrim`,
+ pitch/mode toggles in the clip editor) use the deferred path: set `SetDeferWarpBuild(true)` around the
+ edit and then build the cache with the same sliced builder used on import (factor
+ `BuildImportWarpAsync` into a shared `WarpBuildScheduler` on `MainWindow`, coalescing concurrent
+ requests). Better still (fixes item 1 too): move the stretch itself onto a worker thread — the
+ builder only reads the immutable `SampleBuffer` and a copy of the markers, so the engine can expose
+ "start building cache for clip X on a worker" + "install finished cache (republish) on the authoring
+ thread", with a generation counter so a newer edit discards a stale build. Until the new cache is
+ installed the clip keeps playing its previous cache (reporter's choice).
+3. *Ruler/grid thinning* — pick the labelled step from a ladder of bars `{1,2,4,8,16,32,64,…}` (and beats
+ only when a beat ≥ ~30 px), choosing the smallest step whose pixel width ≥ widest label + padding.
+ Draw grid lines with a separate, finer ladder (bar lines when bar ≥ ~6 px, beat lines when beat ≥ ~6 px,
+ sub-beat 16ths when very zoomed like screenshot 2). Extend the seconds ladder (120, 300, 600 s) for the
+ unwarped ruler. Only iterate the visible range (compute first/last visible beat from scroll/zoom)
+ instead of looping over all beats and `continue`-ing.
+4. *Zoom-aware waveform* — keep the 1024-bucket overview as an instant fallback; add a "detail" peak
+ buffer for the visible window: on zoom/scroll change (debounced ~30 ms, or on the next idle
+ dispatch), request `~pixelWidth` buckets for `[visibleStart, visibleEnd]` (slightly padded) via new
+ range APIs — `nota_clip_warp_peaks_range(track, clip, beat0, beat1, out, n)` (thin wrapper over
+ `warpPeaksRange`) and `nota_clip_source_peaks_range(track, clip, frame0, frame1, out, n)`. Draw the
+ detail buffer when it covers the view, else the overview. Draw peaks as one `StreamGeometry`
+ (or per-pixel columns) instead of a `FillRectangle` per bucket. When buckets get narrower than a
+ source frame (extreme zoom), fall back to drawing the sample line.
+
+**Alternatives**:
+- Simply raise the fixed overview to e.g. 65 536 buckets: trivial, but still blocky at high zoom for
+ very long files, costs ~0.5 MB and a slower `Render` loop per reload; doesn't scale.
+- Keep warp builds on the UI thread but shrink slices (e.g. 8192 frames) — smoother but slower overall and
+ still blocks for one slice; a worker build is the real fix.
+
+**Files likely to change**:
+- `src/managed/Nota.App/AudioClipEditorView.cs` (BPM click, ruler/grid, detail peaks, render)
+- `src/managed/Nota.App/MainWindow.AudioImport.cs` (shared warp-build scheduler / worker)
+- `src/managed/Nota.App/MainWindow.DetailWindow.cs` / `MainWindow.Detail.cs` (host wiring of the editor's change callbacks, if the async build is owned by MainWindow)
+- `src/managed/Nota.Application/Engine/IAudioEngine.cs`, `src/managed/Nota.Infrastructure/Interop/NotaEngine.Tracks.cs`, `NativeMethods` (range-peak APIs; async warp build if chosen)
+- `src/native/nota.engine/include/nota/nota_engine.h`, `src/native/nota.engine/src/nota_engine_track.cpp`, `Engine.h`, `Engine_Tracks.cpp` (range peaks; off-thread warp build + install)
+- tests: engine smoke / unit tests under `tests/`
+
+**Tests to add or update**:
+- Engine: range-peak APIs return the same min/max as the full query over the same span (warped and unwarped), and handle reversed clips, out-of-range spans and `n` larger than frames.
+- Engine: async/deferred warp build after `setClipWarpMarkers` produces a cache identical to the synchronous build; a superseding edit discards the stale build.
+- Managed (headless Avalonia): BPM chip click without movement does not call `SetClipWarpMarkers`; double-click opens the editor with zero commits.
+- Managed: ruler label positions for a 700-bar clip at fit-to-width never overlap (label rects disjoint) and grid line count is bounded by ~w/6.
+- Manual/perf: drop a 10-min FLAC — UI frame time stays < ~20 ms during "Warping…" (log the per-slice duration).
+
+## Risks & Considerations
+
+- Async warp build: playback of the edited clip during the rebuild (silence vs. stale cache vs. realtime stretch) must be decided; the snapshot/republish model (`republishWithTrack`) must install the cache only onto the matching clip generation; undo must not race an in-flight build.
+- Thread safety: the worker may only read the immutable `SampleBuffer` and its own copies of markers/settings; no authoring-state access off the authoring thread (constitution: audio-thread restrictions + authoring-thread ownership).
+- Reversed clips: detail peaks must go through the same mirror logic as the overview (the editor mirrors file-order peaks itself; keep the range API in file order).
+- New C ABI functions: additive only (no ABI break), AGPL headers on any new file.
+- The arrangement lane has the same fixed-2048-bucket limitation for very long clips at deep timeline zoom — out of scope here, but worth a follow-up.
+
+## Clarifications (2026-10-13, from the reporter)
+
+- Item 1 stutters during **"Processing… %"** (the decode phase), not "Warping… %". The decode itself
+ already runs on a worker (`DecodeImport` via `Task.Run`), so the UI-thread cost has to be found by
+ measurement: candidates are the 50 ms progress ticks (`Progress<ImportTick>` → lane repaint of the
+ growing placeholder, `PendingClipsFor` VM churn), the `AddPendingImport → Refresh()` path, the final
+ hand-off (`AddImportedAudioClip` + `Timeline.Refresh`, still labelled "Processing…"), and GC /
+ memory pressure from the multi-hundred-MB sample buffer. `/speckit-bug-fix` must time the UI-thread
+ calls during a long-FLAC import first and fix what the numbers show; the off-thread warp build stays
+ in scope because it is needed for item 2 anyway.
+- During an async warp rebuild the clip **keeps playing the old warp** until the new cache is installed.
+- **Yes** — `MaxZoom` scales with clip length so a long file can zoom down to sample level.
diff --git a/.specify/bugs/long-audio-clip-editor/fix.md b/.specify/bugs/long-audio-clip-editor/fix.md
new file mode 100644
index 00000000..f3461cbb
--- /dev/null
+++ b/.specify/bugs/long-audio-clip-editor/fix.md
@@ -0,0 +1,115 @@
+# Bug Fix: Long audio files — import stutter, BPM-edit freeze, unreadable clip-editor ruler and waveform
+
+- **Slug**: long-audio-clip-editor
+- **Fixed**: 2026-10-13
+- **Assessment**: ./assessment.md
+- **Status**: applied
+
+## Summary
+
+Warp-cache stretching moved off the UI thread: a new non-blocking `warpBuildPoll` stretches one clip
+at a time on an engine worker thread and installs the result. This now drives imports, project load
+and every warp edit in the clip editor. Until the new cache is ready the clip keeps playing its
+previous (stale) cache, clipped to its new length. A plain click on the BPM tag no longer re-warps.
+The clip-editor ruler and grid thin out by zoom level, and the waveform re-fetches peaks for the
+visible slice. The zoom limit grows with the file's length so you can zoom down to single samples.
+
+## Changes
+
+| File | Change | Notes |
+|------|--------|-------|
+| `src/native/nota.engine/src/Warp.h` | modified | `WarpInputs` (everything a cache depends on) stored in `WarpCache`; `WarpMarker ==` |
+| `src/native/nota.engine/src/Engine.h` | modified | `warpBuildPoll`, `WarpJob` (worker state), `warpNeedsBuild`, `nextWarpBuildTarget`, `cancelWarpJob`, range-peak getters |
+| `src/native/nota.engine/src/Engine_Tracks.cpp` | modified | `warpInputsOf` + shared `renderWarpWindow` (cancellable); staleness = inputs differ; deferred edits keep the old cache; `warpBuildPoll` worker build + install-if-unchanged; sync `warpBuildStep` reuses the scan and cancels a running job; `getClipWarpPeaksRange` / `getClipSourcePeaksRange` (≤ 8192 buckets) |
+| `src/native/nota.engine/src/Engine_Render.cpp` | modified | a stale cache plays clipped to the clip's current played length |
+| `src/native/nota.engine/src/Engine_Session.cpp` | modified | slot-from-clip rebuilds a stale cache too |
+| `src/native/nota.engine/src/Engine.cpp` | modified | the destructor cancels and joins the worker |
+| `src/native/nota.engine/include/nota/nota_engine.h`, `src/nota_engine_track.cpp` | added API | `nota_engine_warp_build_poll`, `nota_clip_get_warp_peaks_range`, `nota_clip_get_source_peaks_range` (additive, no ABI break) |
+| `src/managed/Nota.Application/Engine/IAudioEngine.cs`, `Nota.Infrastructure/Interop/{NativeMethods,NotaEngine}.Tracks.cs` | added | `WarpBuildPoll`, `GetClipWarpPeaksRange`, `GetClipSourcePeaksRange` |
+| `src/managed/Nota.App/MainWindow.AudioImport.cs` | modified | `PollWarpBuildAsync` (poll every 15 ms; several loops may share the work); import warp uses it |
+| `src/managed/Nota.App/MainWindow.Files.cs` | modified | project-load warp build uses the poll loop |
+| `src/managed/Nota.App/MainWindow.Detail.cs` | modified | wires the editor's `WarpBuildRequested` |
+| `src/managed/Nota.App/AudioClipEditorView.cs` | modified | `WarpEdit` (defer + background build) around markers / trim / length / warp toggle & mode / auto- & beat-warp / pitch; BPM chip commits only after a real drag; BPM field commits only a changed value; zoom-aware detail peaks; density-based ruler (bars ×2ⁿ → beats → 16ths, ticks to 1/4096 beat) and wave grid; seconds ruler 0.5 ms … 1 h with `m:ss`; ruler clipped to its bounds; dynamic `MaxZoom` (~8 px per source frame) |
+| `tests/Nota.SmokeTest/Program.cs` | added tests | range peaks + background warp build |
+| `CHANGELOG.md` | modified | four `Fixed` entries under [Unreleased] |
+
+## Diff Highlights
+
+```cpp
+// configureClipWarp: a deferred edit keeps the (now stale) cache playing
+if (!c.warpEnabled) { c.warpCache = nullptr; return; }
+if (deferWarpBuild_) return; // stale (or no) cache until warpBuildStep / warpBuildPoll
+```
+
+```csharp
+// BPM chip release: a click (no drag) must not re-warp the clip
+if (_bpmScrubMoved) Commit();
+```
+
+## Tests Added or Updated
+
+- `tests/Nota.SmokeTest/Program.cs` (import section):
+ - `range peaks: whole warp range == full-material peaks`
+ - `range peaks: source sub-range matches a full scan`
+ - `range peaks: sub-frame buckets read single frames`
+ - `bg warp: the deferred edit leaves a stale cache to rebuild`
+ - `bg warp: the clip keeps playing its old cache meanwhile`
+ - `bg warp: worker build installed`
+ - `bg warp: plays identically to the synchronous rebuild` (max diff 0)
+ - `bg warp: a superseded build is dropped, the latest edit wins`
+
+## Local Verification
+
+- Commands run: `scripts/build.sh` → native + managed build OK, **SMOKE TEST PASSED**.
+- Headless harness (`/tmp/longaudioharness`, real `MainWindow`, 8-min 48 kHz FLAC dropped through
+ `ImportAudioInBackgroundAsync`):
+ - Before: one **3.2–3.4 s** UI-thread block at the end of "Processing…". Measured as ~480 sliced
+ `WarpBuildStep(48000)` calls of 5–18 ms each; decode itself cost ~1 ms per UI tick.
+ - After: longest UI-thread block **~21–27 ms** during the whole import; cache fully built.
+ - Clip editor: double-click on the BPM chip **4.8 ms**, warp untouched, field opens. Typing 125 + Enter
+ **5.3 ms**; the background rebuild takes ~3.1 s with the longest UI block **4.7 ms**; warp beats
+ 960 → 1000.
+ - Zoom 80× fetches 3727 detail buckets for the visible slice. Max zoom is 148 406× on this file.
+ Unwarped zoom fetches detail too.
+- Manual checks (screenshots): at fit, bar labels every 8 bars with no overlap. At 80×, the waveform
+ is sharp and labelled `73 · 73.2 · 73.3 …`. At 2000×, single cycles of the 110 Hz tone are
+ visible. The unwarped ruler reads `0 s · 30 s …` at fit and `192.0005 s …` at maximum zoom. A
+ half-off-screen ruler label no longer bleeds into the inspector.
+
+## Deviations from Assessment
+
+- Item 1: after the reporter clarified that the stutter happens during "Processing…", the
+ measurement showed decode is fine. The "Processing…" phase on the track includes the warp build,
+ and that build was the stall. So the off-thread warp build fixes item 1 directly; no decode-side
+ change was needed.
+- The synchronous `warpBuildStep` API is kept (the smoke tests and possible external callers rely on
+ it). The worker path is a new additive `warp_build_poll` instead of a change to `warpBuildStep`.
+- Also wrapped in `WarpEdit`: warp toggle/mode, auto-warp, beat-warp and pitch changes in the clip
+ editor. They hit the same synchronous rebuild.
+- An unrelated pre-existing style-lint failure ("plain space before a unit" in the warp status text)
+ was fixed in passing, because the moved line was flagged.
+
+## Follow-ups
+
+- A project **tempo** change still rebuilds every warp cache synchronously
+ (`reconfigureAllWarpStreams`), so a long warped file freezes there too. It should go through
+ defer + `warpBuildPoll` as well.
+- `AutoWarpClip` / `BeatWarpClip` still run tempo or transient detection on the UI thread. That is
+ quick for loops but can be slow on very long files.
+- The arrangement lane still draws long clips from a fixed 2048-bucket overview. Deep timeline zoom
+ on a long clip is blocky there; the same range-peaks API would fix it.
+- Arrangement edits outside the clip editor (lane trims, split, tempo via the transport) still
+ build caches synchronously.
+
+## Follow-up (2026-10-13, reporter feedback)
+
+- **Zoom flashed the old wide blocks:** each zoom step drew the coarse overview for one frame
+ until the background detail fetch landed. Now the previous detail keeps drawing wherever it
+ reaches, and the overview fills only outside it, until the fresh fetch arrives. The detail is
+ used whenever it's finer than the overview.
+- **Blocks and the playhead spilled outside the waveform:** at zoom, buckets, markers and the
+ playhead could have x < 0 or > width and were drawn over the inspector and the neighbouring
+ panel. `Render` now clips everything to the editor's bounds.
+- Verified headless: frames captured right after zoom steps 80→96× and 96→140×, before the
+ background fetch ran, show the detailed waveform (no blocks). A playhead scrolled past the left
+ edge doesn't appear over the inspector. The harness's other checks still pass.
diff --git a/.specify/bugs/long-audio-clip-editor/test.md b/.specify/bugs/long-audio-clip-editor/test.md
new file mode 100644
index 00000000..6e8a45cc
--- /dev/null
+++ b/.specify/bugs/long-audio-clip-editor/test.md
@@ -0,0 +1,67 @@
+# Bug Verification: Long audio files — import stutter, BPM-edit freeze, unreadable clip-editor ruler and waveform
+
+- **Slug**: long-audio-clip-editor
+- **Tested**: 2026-10-13
+- **Assessment**: ./assessment.md
+- **Fix**: ./fix.md
+- **Result**: verified
+
+## Summary
+
+None of the four symptoms reproduces any more, judging by the automated equivalent of the
+assessment's reproduction (real `MainWindow`, headless, an 8-minute FLAC dropped through the real
+import path):
+- the import no longer blocks the UI for seconds;
+- a click on the BPM tag is instant;
+- the ruler labels don't collide;
+- the waveform re-resolves on zoom, with no flash of old blocks and nothing drawn past the
+ editor's edges.
+
+The full build and smoke test pass, including the 8 new engine checks. No regressions were found.
+The reporter confirmed the first fix in the real app ("Все отлично"); the follow-up (zoom flash,
+clipping) is verified headless only.
+
+## Checks Performed
+
+| Check | Command / Action | Result | Notes |
+|-------|------------------|--------|-------|
+| Reproduction 1 — drop a long FLAC | `/tmp/longaudioharness` (`NOTA_DATA_DIR=… dotnet run`): `ImportAudioInBackgroundAsync` on an 8-min 48 kHz FLAC, UI thread timed every iteration | pass | longest UI-thread block 22.6 ms (before the fix: one 3.2–3.4 s block) |
+| Reproduction 2 — BPM click | synthetic double-click on the BPM chip; type 125 + Enter | pass | click 5.2 ms, warp untouched, field opens; commit 5.7 ms, background rebuild 3.1 s with a 6.7 ms max UI block; clip 960 → 1000 beats |
+| Reproduction 3 — ruler on a long clip | screenshots at fit, 12×, 80×, 2000×, max zoom; warped + unwarped | pass | labels every 8 bars at fit, no overlap; beats / 16ths up close; seconds ruler `0 s…450 s` → `192.0005 s` |
+| Reproduction 4 — waveform at zoom | detail fetch at 80× (3727 buckets) and unwarped; frames captured right after zoom steps 80→96× and 96→140×, before the fetch ran | pass | detailed waveform, no block flash; sample-level at 2000×; max zoom 148 406× |
+| Follow-up — drawing past the edges | playhead scrolled past the left edge at 96× | pass | nothing drawn over the inspector |
+| New engine tests | `scripts/build.sh` (smoke test) | pass | 3 range-peaks checks + 5 background-warp checks, all PASS (bg build 126 ms; identical to the sync build, max diff 0) |
+| Regression suite | `scripts/build.sh` — native universal build, `dotnet build Nota.sln -c Release`, full smoke test | pass | `SMOKE TEST PASSED`, 3112 checks PASS, 0 errors |
+| Lint / style | the smoke test's source-style checks | pass | the "plain space before a unit" check passes after the fix |
+| Real app with a display / the reporter's 96 kHz file | — | not-run | headless only from this session; the reporter checked the main fix in the real app |
+
+## Output Excerpts
+
+```
+import done in 3483 ms … max RunJobs 22.6 ms, max render 23.6 ms
+[PASS] double-click on the BPM chip returns in 5.2 ms
+[PASS] background rebuild finished in 3107 ms, max UI block 6.7 ms
+[PASS] bg warp: plays identically to the synchronous rebuild (max diff 0,0E+000)
+[PASS] bg warp: a superseded build is dropped, the latest edit wins (max diff 0,0E+000)
+SMOKE TEST PASSED
+```
+
+## Residual Risks
+
+- Measured on a generated 48 kHz file headless. A 96 kHz / 24-bit FLAC (like the reporter's)
+ stretches about twice the source data per clip. That work is now on the worker thread, so the
+ UI cost shouldn't scale with it, but this exact case wasn't run.
+- While a rebuild runs, the clip plays its previous warp, clipped to its new length. That is the
+ reporter's choice, so the first second or so after an edit sounds like the old timing.
+- Not covered by this fix (listed in fix.md follow-ups):
+ - a project tempo change still rebuilds every warp cache synchronously;
+ - auto- and beat-warp detection runs on the UI thread;
+ - arrangement-lane trims, splits and tempo edits outside the clip editor still build caches synchronously;
+ - the arrangement lane still draws long clips from a fixed 2048-bucket overview.
+
+## Recommendation
+
+Close the bug: verified through the automated end-to-end reproduction and the full smoke test,
+with the reporter's confirmation of the main fix in the real app. Worth a final in-app look at the
+zoom-flash follow-up with the original 96 kHz FLAC. Open separate issues for the follow-ups,
+starting with the synchronous rebuild on a project tempo change.
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 509eaddb..460fa4ce 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -43,6 +43,22 @@ Entry categories: `Added`, `Changed`, `Fixed`, `Removed`.
The browser also opens wider by default.
### Fixed
+- **Long audio files no longer stall the app:** after dropping a long file (a multi-minute
+ FLAC, say) onto the arrangement, the app stuttered for as long as it took to time-stretch
+ the clip to the project tempo. Warping now runs in the background, so the app stays smooth
+ while the track says "Processing…".
+- **Editing a long clip's tempo no longer freezes:** clicking the BPM tag in the clip editor
+ froze the app for seconds on a long warped clip, because even a plain click re-warped the
+ whole file. A click now does nothing, and a double-click opens the BPM field at once. A real
+ tempo, marker, trim, pitch or warp-mode change re-warps in the background, and the clip
+ keeps playing its previous version until the new one is ready.
+- **Readable clip-editor ruler on long clips:** bar numbers no longer pile on top of each
+ other. The ruler labels every 2nd, 4th, 8th… bar as needed and adds beats and 16ths as
+ you zoom in. The grid through the waveform thins out the same way. The seconds ruler for
+ unwarped clips counts in minutes on long files and in milliseconds up close.
+- **The clip editor's waveform sharpens as you zoom:** zooming in used to magnify a coarse
+ overview into wide blocks. The visible part is now redrawn at screen resolution, and long
+ clips zoom all the way in to individual samples.
- **Nota Reverb no longer fires a loud, looping metallic burst:** after a few minutes of
playback the reverb could suddenly ring at full level and keep ringing. The cause was a
rare off-by-one read just past the end of a reverb delay line, which pulled a garbage
diff --git a/src/managed/Nota.App/AudioClipEditorView.cs b/src/managed/Nota.App/AudioClipEditorView.cs
index ea83b6cd..45bbb229 100644
--- a/src/managed/Nota.App/AudioClipEditorView.cs
+++ b/src/managed/Nota.App/AudioClipEditorView.cs
@@ -18,6 +18,7 @@ using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Layout;
using Avalonia.Media;
+using Avalonia.Threading;
using Nota.Application;
using static Nota.App.ClipEditorKit;
@@ -57,6 +58,7 @@ public sealed class AudioClipEditorView : UserControl
private int _warpMode = 3; // Complex
private bool _snapToGrid = true; // trim brackets snap to whole beats (default on)
private double _detectedBpm;
+ private long _srcFrames; // the whole sample's frames (unwarped detail peaks)
private bool _envMode;
private int _envTarget; // 0 = Volume (0..1), 1 = Pan (-1..1)
private static readonly string[] WarpModes = { "Beats", "Tones", "Texture", "Complex", "Complex Pro", "Re-Pitch" };
@@ -71,10 +73,24 @@ public sealed class AudioClipEditorView : UserControl
_wave.SetColor(clipColor);
_wave.MarkersCommitted = (src, beat) =>
{
- _engine.SetClipWarpMarkers(TrackId, ClipIndex, src, beat);
+ WarpEdit(() => _engine.SetClipWarpMarkers(TrackId, ClipIndex, src, beat));
Reload();
_onChanged();
};
+ // Zoomed in: the visible slice's own peaks (file order, like the overview).
+ _wave.DetailPeaks = (f0, f1, n) =>
+ {
+ var pk = new float[n * 2];
+ int got = 0;
+ if (_warpEnabled)
+ {
+ if (_engine.TryGetAudioClipInfo(TrackId, ClipIndex, out var wi) && wi.WarpBeats > 0)
+ got = _engine.GetClipWarpPeaksRange(TrackId, ClipIndex, f0 * wi.WarpBeats, f1 * wi.WarpBeats, pk, n);
+ }
+ else if (_srcFrames > 0)
+ got = _engine.GetClipSourcePeaksRange(TrackId, ClipIndex, f0 * _srcFrames, f1 * _srcFrames, pk, n);
+ return (pk, got);
+ };
_wave.SourceRegionCommitted = (off, len) =>
{
_engine.SetClipSourceRegion(TrackId, ClipIndex, off, len);
@@ -83,7 +99,7 @@ public sealed class AudioClipEditorView : UserControl
};
_wave.WarpTrimCommitted = (ps, pe) =>
{
- _engine.SetClipWarpTrim(TrackId, ClipIndex, ps, pe);
+ WarpEdit(() => _engine.SetClipWarpTrim(TrackId, ClipIndex, ps, pe));
Reload();
_onChanged();
};
@@ -205,11 +221,13 @@ public sealed class AudioClipEditorView : UserControl
private readonly Canvas _overlay;
private readonly ScrollBar _hScroll;
private int _bpmSeg = -1;
+ private double _bpmEditStart; // the segment's BPM when the field opened
// Inline BPM editor over the warp strip: position + focus a small field.
private void ShowBpmEditor(int seg, double bpm, double xCenter, double yTop)
{
_bpmSeg = seg;
+ _bpmEditStart = bpm;
_bpmBox.Text = bpm.ToString("0.##", NotaNum.Culture);
Canvas.SetLeft(_bpmBox, Math.Max(0, xCenter - _bpmBox.Width / 2));
Canvas.SetTop(_bpmBox, Math.Max(0, yTop - 2));
@@ -229,7 +247,8 @@ public sealed class AudioClipEditorView : UserControl
if (!_bpmBox.IsVisible) return;
int seg = _bpmSeg;
HideBpm();
- if (seg >= 0 && double.TryParse(_bpmBox.Text, NumberStyles.Any, NotaNum.Culture, out var v) && v > 0)
+ if (seg >= 0 && double.TryParse(_bpmBox.Text, NumberStyles.Any, NotaNum.Culture, out var v) && v > 0
+ && Math.Abs(v - _bpmEditStart) >= 0.005) // unchanged → no re-warp
_wave.SetSegmentBpm(seg, v); // → MarkersCommitted → engine + Reload
}
@@ -287,6 +306,8 @@ public sealed class AudioClipEditorView : UserControl
if (gi.SampleId != 0 && _engine.TryGetSampleInfo(gi.SampleId, out var gs) && gs.Frames > 0)
{ haveSample = true; srcSR = gs.SampleRate; srcFrames = gs.Frames; }
_wave.SetSourceRate(srcSR);
+ _srcFrames = srcFrames;
+ _wave.SetSourceFrames(srcFrames);
// Warp markers (only meaningful while warping). Unwarped clips instead show the
// whole sample with draggable Start/End brackets (source-region editing).
@@ -343,11 +364,24 @@ public sealed class AudioClipEditorView : UserControl
_wave.SetPlayhead(frac);
}
+ /// <summary>Set by the host: runs the engine's background warp build after a warp edit.</summary>
+ public Action? WarpBuildRequested;
+
+ // A warp-affecting edit skips the synchronous stretch (seconds of UI freeze on a long
+ // file): the clip keeps playing its previous cache until the background build lands.
+ private void WarpEdit(Action edit)
+ {
+ _engine.SetDeferWarpBuild(true);
+ try { edit(); }
+ finally { _engine.SetDeferWarpBuild(false); }
+ WarpBuildRequested?.Invoke();
+ }
+
private void Resize(int deltaBeats)
{
double len = Math.Max(0.25, _lengthBeats + deltaBeats);
// Warped clips own a musical target length; unwarped clips trim the source.
- if (_warpEnabled) _engine.SetClipWarpLength(TrackId, ClipIndex, len);
+ if (_warpEnabled) WarpEdit(() => _engine.SetClipWarpLength(TrackId, ClipIndex, len));
else _engine.TrimClip(TrackId, ClipIndex, _startBeat, len);
Reload();
_onChanged();
@@ -379,7 +413,7 @@ public sealed class AudioClipEditorView : UserControl
private void ToggleWarp()
{
_warpEnabled = !_warpEnabled;
- _engine.SetClipWarp(TrackId, ClipIndex, _warpEnabled, _warpMode);
+ WarpEdit(() => _engine.SetClipWarp(TrackId, ClipIndex, _warpEnabled, _warpMode));
Reload();
_onChanged();
}
@@ -387,7 +421,7 @@ public sealed class AudioClipEditorView : UserControl
private void SetWarpMode(int mode)
{
_warpMode = Math.Clamp(mode, 0, WarpModes.Length - 1);
- _engine.SetClipWarp(TrackId, ClipIndex, _warpEnabled, _warpMode);
+ WarpEdit(() => _engine.SetClipWarp(TrackId, ClipIndex, _warpEnabled, _warpMode));
Reload();
_onChanged();
}
@@ -397,7 +431,7 @@ public sealed class AudioClipEditorView : UserControl
// material, in which case the clip is left as-is.
private void AutoWarp()
{
- _detectedBpm = _engine.AutoWarpClip(TrackId, ClipIndex);
+ WarpEdit(() => _detectedBpm = _engine.AutoWarpClip(TrackId, ClipIndex));
ShowDetected();
Reload();
_onChanged();
@@ -407,7 +441,7 @@ public sealed class AudioClipEditorView : UserControl
// percussion locks tightly to the grid ("Beats"). Fails on flat material.
private void TransientWarp()
{
- _detectedBpm = _engine.BeatWarpClip(TrackId, ClipIndex);
+ WarpEdit(() => _detectedBpm = _engine.BeatWarpClip(TrackId, ClipIndex));
ShowDetected();
Reload();
_onChanged();
@@ -488,7 +522,7 @@ public sealed class AudioClipEditorView : UserControl
private void Pitch(int d)
{
_pitch = Math.Clamp(_pitch + d, -48, 48);
- _engine.SetClipPitch(TrackId, ClipIndex, _pitch);
+ WarpEdit(() => _engine.SetClipPitch(TrackId, ClipIndex, _pitch));
Reload(); // pitch changes the clip's beat-length (varispeed)
_onChanged();
}
@@ -560,6 +594,7 @@ public sealed class AudioClipEditorView : UserControl
// BPM chip and drag up/down; the warp re-times live (committed on release).
private int _bpmScrubSeg = -1, _bpmHoverSeg = -1;
private double _bpmScrubStartY, _bpmScrubStartBpm;
+ private bool _bpmScrubMoved; // a plain click (no drag) must not re-warp the clip
// Volume envelope (M9 follow-up): points (beat, value 0..1) in clip-local beats.
private static readonly IPen EnvPen = new Pen(NotaPalette.Accent, 1.8);
@@ -663,7 +698,10 @@ public sealed class AudioClipEditorView : UserControl
// this same axis, so one zoom+scroll drives everything. _zoom=1 is fit-to-width.
private double _zoom = 1.0;
private double _scrollFrac;
- private const double MaxZoom = 60.0;
+ // Deep enough to reach single source frames (~8 px each) on any clip length.
+ private double _srcFramesAll; // the whole sample's frames (sets the zoom limit)
+ public void SetSourceFrames(double frames) => _srcFramesAll = Math.Max(0, frames);
+ private double MaxZoom => Math.Max(60.0, _srcFramesAll * 8 / Math.Max(1, Bounds.Width));
/// <summary>Raised when zoom/scroll changes so the host can resync its scrollbar.</summary>
public Action? ViewChanged;
public double Zoom => _zoom;
@@ -744,7 +782,7 @@ public sealed class AudioClipEditorView : UserControl
}
public void SetColor(IBrush b) { _wave = b; InvalidateVisual(); }
- public void SetPeaks(float[] peaks, int count) { _peaks = peaks; _count = count; InvalidateVisual(); }
+ public void SetPeaks(float[] peaks, int count) { _peaks = peaks; _count = count; ClearDetail(); InvalidateVisual(); }
public void SetPlayhead(double frac)
{
if (Math.Abs(frac - _frac) < 1e-4) return;
@@ -815,7 +853,7 @@ public sealed class AudioClipEditorView : UserControl
BpmEditRequested?.Invoke(b.seg, b.bpm, (b.rect.Left + b.rect.Right) / 2, b.rect.Top);
else
{
- _bpmScrubSeg = b.seg; _bpmScrubStartY = pp.Y; _bpmScrubStartBpm = b.bpm;
+ _bpmScrubSeg = b.seg; _bpmScrubStartY = pp.Y; _bpmScrubStartBpm = b.bpm; _bpmScrubMoved = false;
e.Pointer.Capture(this);
}
e.Handled = true;
@@ -948,6 +986,8 @@ public sealed class AudioClipEditorView : UserControl
{
double dy = _bpmScrubStartY - e.GetPosition(this).Y;
double bpm = Math.Clamp(_bpmScrubStartBpm + dy * 0.5, 20, 999);
+ if (!_bpmScrubMoved && Math.Abs(dy) < 2) return; // a click, not a drag (yet)
+ _bpmScrubMoved = true;
ApplySegmentBpm(_bpmScrubSeg, bpm, commit: false); // live warp + redraw, no engine round-trip
return;
}
@@ -1026,7 +1066,14 @@ public sealed class AudioClipEditorView : UserControl
InvalidateVisual();
return;
}
- if (_bpmScrubSeg >= 0) { _bpmScrubSeg = -1; e.Pointer.Capture(null); Commit(); return; } // push the scrubbed warp (one undo step)
+ if (_bpmScrubSeg >= 0) // push the scrubbed warp (one undo step) — only if it was dragged
+ {
+ _bpmScrubSeg = -1; e.Pointer.Capture(null);
+ if (_bpmScrubMoved) Commit();
+ _bpmScrubMoved = false;
+ InvalidateVisual();
+ return;
+ }
if (_envDrag >= 0 || _envBend is not null) { _envDrag = -1; _envBend = null; e.Pointer.Capture(null); CommitEnv(); return; }
if (_srcDrag >= 0)
{
@@ -1117,9 +1164,140 @@ public sealed class AudioClipEditorView : UserControl
MarkersCommitted?.Invoke(src, beat);
}
+ // ---- zoom-aware waveform detail ----------------------------------------
+ // The overview (_peaks) spans the whole clip at a fixed resolution; zoomed in, the
+ // host fetches peaks for just the visible slice (padded a screen either side so a
+ // scroll doesn't refetch at once). Content (file-order) fracs, like the overview.
+ /// <summary>Set by the host: peaks over content fracs [f0, f1] at n buckets → (peaks, count).</summary>
+ public Func<double, double, int, (float[] peaks, int count)>? DetailPeaks;
+ private float[]? _dPeaks;
+ private int _dCount;
+ private double _dF0, _dF1;
+ private bool _detailQueued;
+ private const double DetailPx = 1.5; // overview buckets wider than this → fetch detail
+
+ private void ClearDetail() { _dPeaks = null; _dCount = 0; }
+
+ // Visible content-frac range (mirrored back when the clip is drawn reversed).
+ private (double f0, double f1) VisibleContent(double w)
+ {
+ double v0 = Math.Clamp(XToFracView(0, w), 0, 1), v1 = Math.Clamp(XToFracView(w, w), 0, 1);
+ return _reversed ? (1 - v1, 1 - v0) : (v0, v1);
+ }
+
+ private bool DetailCovers(double f0, double f1, double w)
+ {
+ if (_dPeaks is null || _dCount <= 0 || _dF0 > f0 + 1e-12 || _dF1 < f1 - 1e-12) return false;
+ double px = (_dF1 - _dF0) * _zoom * w / _dCount; // detail bucket width at this zoom
+ return px <= DetailPx * 1.01 && px >= 0.25; // fine enough, and not zoomed far back out
+ }
+
+ private void RequestDetail()
+ {
+ if (_detailQueued || DetailPeaks is null) return;
+ _detailQueued = true;
+ Dispatcher.UIThread.Post(() =>
+ {
+ _detailQueued = false;
+ if (DetailPeaks is null) return;
+ double ww = Bounds.Width;
+ var (a, b) = VisibleContent(ww);
+ double span = b - a;
+ if (span <= 0) return;
+ double p0 = Math.Max(0, a - span), p1 = Math.Min(1, b + span);
+ int n = (int)Math.Clamp(Math.Ceiling((p1 - p0) * _zoom * ww), 1, MaxDetail);
+ // Too wide a slice for one fetch → just the visible part.
+ if ((p1 - p0) * _zoom * ww / n > DetailPx) { p0 = a; p1 = b; n = (int)Math.Clamp(Math.Ceiling(span * _zoom * ww), 1, MaxDetail); }
+ var (pk, cnt) = DetailPeaks(p0, p1, n);
+ if (cnt <= 0) return;
+ _dPeaks = pk; _dCount = cnt; _dF0 = p0; _dF1 = p1;
+ InvalidateVisual();
+ }, DispatcherPriority.Background);
+ }
+ private const int MaxDetail = 8192; // the engine's range-peaks cap
+
+ // Peaks whose buckets evenly span content fracs [f0, f1], drawn through the zoom/
+ // scroll (and mirror) axis; only the visible buckets are visited.
+ private void DrawPeaks(DrawingContext ctx, float[] peaks, int count, double f0, double f1, double w,
+ double mid, double wh, AdsrFrame adsrF, ClipAdsr adsrFit, bool shaped)
+ {
+ double amp = wh / 2 - 6;
+ double span = f1 - f0;
+ if (span <= 0) return;
+ double bwRaw = span * _zoom * w / count;
+ double bw = Math.Max(1, bwRaw);
+ var (c0, c1) = VisibleContent(w);
+ int i0 = Math.Max(0, (int)Math.Floor((c0 - f0) / span * count) - 1);
+ int i1 = Math.Min(count - 1, (int)Math.Ceiling((c1 - f0) / span * count) + 1);
+ for (int i = i0; i <= i1; i++)
+ {
+ // FracToX marks the bucket's leading edge — its RIGHT edge once mirrored.
+ double fx = FracToX(f0 + span * i / count, w);
+ if (_reversed) fx -= bwRaw;
+ if (fx + bw < 0 || fx > w) continue;
+ // The ADSR shapes the played region the way it sounds (outside it is darkened anyway).
+ double g = _gain;
+ if (shaped)
+ {
+ double bt = adsrF.Beat(fx + bw / 2);
+ if (bt >= 0 && bt <= _adsrLen) g *= adsrFit.GainAt(bt, _adsrLen);
+ }
+ float mn = (float)Math.Clamp(peaks[i * 2] * g, -1.0, 1.0);
+ float mx = (float)Math.Clamp(peaks[i * 2 + 1] * g, -1.0, 1.0);
+ ctx.FillRectangle(_wave, new Rect(fx, mid - mx * amp, bw, Math.Max(1, (mx - mn) * amp)));
+ }
+ }
+
+ // ---- ruler / grid density ----------------------------------------------
+ private const double GridMinPx = 8; // closest two grid lines may sit
+ private const double TickMinPx = 6; // closest two ruler ticks may sit
+ private const double LabelPadPx = 12; // gap after a ruler label
+ // Beat steps the ruler and grid choose from: sub-beats (power-of-two), beats, then bars ×2ⁿ.
+ private const double GridFinest = 1.0 / 16; // the wave grid stops at 64ths (ruler ticks go finer)
+ private static readonly double[] BeatSteps = BuildBeatSteps();
+ private static double[] BuildBeatSteps()
+ {
+ var l = new System.Collections.Generic.List<double>();
+ for (double f = 1.0 / 4096; f < 4; f *= 2) l.Add(f); // ruler ticks reach sample-level zoom
+ for (double b = BeatsPerBar; b <= BeatsPerBar * 65536.0; b *= 2) l.Add(b);
+ return l.ToArray();
+ }
+ // The smallest step (beats) at least minPx wide.
+ private static double BeatStep(double pxPerBeat, double minPx)
+ {
+ foreach (var s in BeatSteps) if (s * pxPerBeat >= minPx) return s;
+ return BeatSteps[^1];
+ }
+ private static bool IsMultiple(double v, double step) => Math.Abs(v / step - Math.Round(v / step)) < 1e-6;
+ // Visible beat range, clamped to the warp.
+ private (double b0, double b1) VisibleBeats(double w)
+ {
+ var (f0, f1) = VisibleContent(w);
+ return (f0 * TotalBeats, f1 * TotalBeats);
+ }
+ private double _monoCharW;
+ private double MonoCharW => _monoCharW > 0 ? _monoCharW
+ : _monoCharW = new FormattedText("0000000000", NotaNum.Culture, FlowDirection.LeftToRight, GridFace, 9, RulerBar).Width / 10;
+
+ // bar / bar.beat / bar.beat.16th[.sub] label for a beat (1-based), as fine as the step needs.
+ private static string BeatLabel(double beat, double step)
+ {
+ int bar = (int)Math.Floor(beat / BeatsPerBar + 1e-9) + 1;
+ double inBar = beat - (bar - 1) * BeatsPerBar;
+ int bt = (int)Math.Floor(inBar + 1e-9) + 1;
+ double sub = inBar - (bt - 1);
+ if (IsMultiple(beat, BeatsPerBar)) return bar.ToString(NotaNum.Culture);
+ if (step >= 1 || sub < 1e-6) return $"{bar}.{bt}";
+ int six = (int)Math.Floor(sub * 4 + 1e-6) + 1; // 16ths within the beat
+ return $"{bar}.{bt}.{six}";
+ }
+
public override void Render(DrawingContext ctx)
{
double w = Bounds.Width, h = Bounds.Height;
+ // Zoomed in, buckets, markers and the playhead can sit past either edge: keep every
+ // stroke inside the editor instead of over the inspector or the panel beside it.
+ using var bounds = ctx.PushClip(new Rect(0, 0, w, h));
double top = WaveTop, wh = Math.Max(0, h - top);
ctx.FillRectangle(Bg, new Rect(0, 0, w, h));
_bpmHot.Clear();
@@ -1130,16 +1308,18 @@ public sealed class AudioClipEditorView : UserControl
ctx.DrawLine(StripEdge, new Point(0, top - 0.5), new Point(w, top - 0.5));
DrawRuler(ctx, w);
- // Beat grid through the wave (warped: the beats are known).
+ // Beat grid through the wave (warped: the beats are known), thinned to the zoom so
+ // a long clip doesn't turn into a solid comb; only the visible slice is walked.
if (_mk.Count > 1)
{
double tb = TotalBeats;
- int last = (int)Math.Floor(tb + 1e-6);
- for (int b = 1; b <= last; b++)
+ double step = Math.Max(GridFinest, BeatStep(_zoom * w / tb, GridMinPx));
+ var (b0, b1) = VisibleBeats(w);
+ for (double b = Math.Max(step, Math.Floor(b0 / step) * step); b <= Math.Min(b1, tb - 1e-6); b += step)
{
double x = BeatToX(b, w);
- if (x < -0.5 || x > w + 0.5) continue;
- ctx.DrawLine(b % BeatsPerBar == 0 ? GridBarPen : GridBeatPen, new Point(x + 0.5, top), new Point(x + 0.5, h));
+ bool bar = IsMultiple(b, BeatsPerBar);
+ ctx.DrawLine(bar ? GridBarPen : GridBeatPen, new Point(x + 0.5, top), new Point(x + 0.5, h));
}
}
@@ -1152,25 +1332,30 @@ public sealed class AudioClipEditorView : UserControl
bool shaped = !adsrFit.IsIdentity;
if (_peaks is not null && _count > 0 && wh > 4)
{
- double amp = wh / 2 - 6;
- double bw = Math.Max(1, _zoom * w / _count);
- for (int i = 0; i < _count; i++)
+ // Zoomed in past the overview's resolution → the visible slice's own peaks
+ // (re-fetched once the view settles). Until a fresh fetch lands, the previous
+ // detail keeps drawing wherever it reaches (it's still far finer than the
+ // overview), so zooming never flashes back to the overview's wide blocks.
+ var (c0, c1) = VisibleContent(w);
+ bool coarse = _zoom * w / _count > DetailPx;
+ if (coarse && !DetailCovers(c0, c1, w)) RequestDetail();
+ bool useDetail = coarse && _dPeaks is not null && _dCount > 0
+ && (_dF1 - _dF0) / _dCount < 1.0 / _count && _dF1 > c0 && _dF0 < c1;
+ if (!useDetail)
+ DrawPeaks(ctx, _peaks, _count, 0, 1, w, mid, wh, adsrF, adsrFit, shaped);
+ else
{
- // FracToX marks the bucket's leading edge — its RIGHT edge once mirrored.
- double fx = FracToX((double)i / _count, w);
- if (_reversed) fx -= bw;
- if (fx + bw < 0) { if (_reversed) break; continue; }
- if (fx > w) { if (_reversed) continue; break; }
- // The ADSR shapes the played region the way it sounds (outside it is darkened anyway).
- double g = _gain;
- if (shaped)
- {
- double bt = adsrF.Beat(fx + bw / 2);
- if (bt >= 0 && bt <= _adsrLen) g *= adsrFit.GainAt(bt, _adsrLen);
- }
- float mn = (float)Math.Clamp(_peaks[i * 2] * g, -1.0, 1.0);
- float mx = (float)Math.Clamp(_peaks[i * 2 + 1] * g, -1.0, 1.0);
- ctx.FillRectangle(_wave, new Rect(fx, mid - mx * amp, bw, Math.Max(1, (mx - mn) * amp)));
+ // The overview only outside the detail's span, the detail inside it.
+ double xa = FracToX(_dF0, w), xb = FracToX(_dF1, w);
+ if (xa > xb) (xa, xb) = (xb, xa);
+ if (xa > 0)
+ using (ctx.PushClip(new Rect(0, top, xa, wh)))
+ DrawPeaks(ctx, _peaks, _count, 0, 1, w, mid, wh, adsrF, adsrFit, shaped);
+ if (xb < w)
+ using (ctx.PushClip(new Rect(xb, top, w - xb, wh)))
+ DrawPeaks(ctx, _peaks, _count, 0, 1, w, mid, wh, adsrF, adsrFit, shaped);
+ using (ctx.PushClip(new Rect(Math.Max(0, xa), top, Math.Max(0, Math.Min(w, xb) - Math.Max(0, xa)), wh)))
+ DrawPeaks(ctx, _dPeaks!, _dCount, _dF0, _dF1, w, mid, wh, adsrF, adsrFit, shaped);
}
}
@@ -1327,22 +1512,37 @@ public sealed class AudioClipEditorView : UserControl
// Bars in Ink 2, beats in Ink 6, mono 9 on the baseline right of each tick.
private void DrawRuler(DrawingContext ctx, double w)
{
+ using var clip = ctx.PushClip(new Rect(0, 0, w, RulerH)); // a label half off-screen stays inside
+ double cw = MonoCharW;
if (_mk.Count > 1)
{
+ // Labels at the smallest step wide enough for its own label text (bars thin
+ // to every 2nd/4th/8th… on a long clip); unlabelled ticks at a finer step.
double tb = TotalBeats;
- double beatW = _zoom * w / tb;
- int step = beatW >= 30 ? 1 : BeatsPerBar;
- int last = (int)Math.Floor(tb + 1e-6);
- for (int b = 0; b <= last; b += step)
+ double ppb = _zoom * w / tb;
+ int barDigits = ((int)Math.Floor(tb / BeatsPerBar) + 1).ToString(NotaNum.Culture).Length;
+ double labelStep = BeatSteps[^1];
+ foreach (var st in BeatSteps)
+ {
+ if (st < 0.25 || st == 2) continue; // labels: 16ths, half-beats, beats, then bars
+ int chars = st >= BeatsPerBar ? barDigits : st >= 1 ? barDigits + 2 : barDigits + 4;
+ if (st * ppb >= chars * cw + LabelPadPx + 4) { labelStep = st; break; }
+ }
+ double tickStep = Math.Min(labelStep, BeatStep(ppb, TickMinPx));
+ var (b0, b1) = VisibleBeats(w);
+ double end = Math.Min(b1 + tickStep, tb + 1e-6);
+ for (double b = Math.Max(0, Math.Floor(b0 / tickStep) * tickStep - labelStep); b <= end; b += tickStep)
{
double x = BeatToX(b, w);
- if (x < -40 || x > w + 0.5) continue;
- bool bar = b % BeatsPerBar == 0;
- ctx.DrawLine(bar ? GridBarPen : GridBeatPen, new Point(x + 0.5, 0), new Point(x + 0.5, RulerH));
- if (b >= last) continue;
- int barNo = b / BeatsPerBar + 1, beatNo = b % BeatsPerBar + 1;
- string label = bar ? barNo.ToString(NotaNum.Culture) : $"{barNo}.{beatNo}";
- var ft = new FormattedText(label, NotaNum.Culture, FlowDirection.LeftToRight, GridFace, 9, bar ? RulerBar : RulerBeat);
+ if (x < -200 || x > w + 0.5) continue;
+ bool labelled = IsMultiple(b, labelStep);
+ bool bar = IsMultiple(b, BeatsPerBar);
+ if (labelled)
+ ctx.DrawLine(bar ? GridBarPen : GridBeatPen, new Point(x + 0.5, 0), new Point(x + 0.5, RulerH));
+ else
+ ctx.DrawLine(GridBeatPen, new Point(x + 0.5, RulerH - 6), new Point(x + 0.5, RulerH));
+ if (!labelled || b >= tb - 1e-6) continue;
+ var ft = new FormattedText(BeatLabel(b, labelStep), NotaNum.Culture, FlowDirection.LeftToRight, GridFace, 9, bar ? RulerBar : RulerBeat);
ctx.DrawText(ft, new Point(_reversed ? x - 4 - ft.Width : x + 4, RulerH - 5 - ft.Height));
}
return;
@@ -1350,21 +1550,32 @@ public sealed class AudioClipEditorView : UserControl
if (!(_srcMode && _srcTotal > 0 && _srcSR > 0)) return;
double secs = _srcTotal / _srcSR;
double pxPerSec = _zoom * w / Math.Max(1e-6, secs);
- double[] steps = { 0.1, 0.25, 0.5, 1, 2, 5, 10, 15, 30, 60 };
- double stepS = 60;
- foreach (var s in steps) if (s * pxPerSec >= 56) { stepS = s; break; }
- for (int k = 0; k * stepS <= secs + 1e-9; k++)
+ double[] steps = { 0.0005, 0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.25, 0.5, 1, 2, 5, 10, 15, 30, 60, 120, 300, 600, 1200, 1800, 3600 };
+ double stepS = steps[^1];
+ foreach (var st in steps)
+ if (st * pxPerSec >= SecondsLabel(secs, st).Length * cw + LabelPadPx + 4) { stepS = st; break; }
+ var (f0, f1) = VisibleContent(w);
+ long k0 = Math.Max(0, (long)Math.Floor(f0 * secs / stepS) - 1), k1 = (long)Math.Ceiling(f1 * secs / stepS) + 1;
+ for (long k = k0; k <= k1 && k * stepS <= secs + 1e-9; k++)
{
double t = k * stepS;
double x = SrcToX(t * _srcSR, w);
- if (x < -40 || x > w + 0.5) continue;
+ if (x < -200 || x > w + 0.5) continue;
ctx.DrawLine(GridBeatPen, new Point(x + 0.5, 0), new Point(x + 0.5, RulerH));
- var ft = new FormattedText(string.Format(NotaNum.Culture, stepS < 1 ? "{0:0.0#} s" : "{0:0} s", t),
- NotaNum.Culture, FlowDirection.LeftToRight, GridFace, 9, k == 0 ? RulerBar : RulerBeat);
+ var ft = new FormattedText(SecondsLabel(t, stepS), NotaNum.Culture, FlowDirection.LeftToRight, GridFace, 9, k == 0 ? RulerBar : RulerBeat);
ctx.DrawText(ft, new Point(_reversed ? x - 4 - ft.Width : x + 4, RulerH - 5 - ft.Height));
}
}
+ // Seconds-ruler label: m:ss once a step reaches a minute, else seconds with as many
+ // decimals as the step needs.
+ private static string SecondsLabel(double t, double step)
+ {
+ if (step >= 60) { long r = (long)Math.Round(t); return $"{r / 60}:{r % 60:00}"; }
+ string fmt = step >= 1 ? "0" : step >= 0.1 ? "0.0#" : step >= 0.01 ? "0.00" : "0.000#";
+ return t.ToString(fmt, NotaNum.Culture) + " s";
+ }
+
// bar.beat.16th of a warp beat (1-based) for the selected marker's chip.
private static string Position(double beat)
{
diff --git a/src/managed/Nota.App/MainWindow.AudioImport.cs b/src/managed/Nota.App/MainWindow.AudioImport.cs
index 774f6472..f0472ade 100644
--- a/src/managed/Nota.App/MainWindow.AudioImport.cs
+++ b/src/managed/Nota.App/MainWindow.AudioImport.cs
@@ -8,8 +8,8 @@
// goes (instantly when the analysis cache already knows the file) — then detects tempo;
// 3. back on the UI thread the decoded buffer is placed (no disk I/O) and — if the sample
// index calls it a loop (or a long take with a tempo) — warped to the project tempo, and
-// optionally transposed to the project key; the warp cache builds in small steps so the
-// UI keeps drawing. A one-shot is never warped;
+// optionally transposed to the project key; the warp cache builds on the engine's
+// worker thread so the UI keeps drawing. A one-shot is never warped;
// 4. the track turns opaque and is selected.
using System;
@@ -100,7 +100,7 @@ public partial class MainWindow
if (warpBpm > 0 || semis != 0)
{
// Warp with the tempo the index / worker measured, deferring the (heavy) stretch
- // so it can be built in slices below instead of in one UI-thread block. The
+ // to the engine's background build below instead of one UI-thread block. The
// transpose rides the same deferred build (a warped clip keeps its length).
Engine.SetDeferWarpBuild(true);
try
@@ -215,21 +215,33 @@ public partial class MainWindow
}
}
- // Build the deferred warp cache in slices on the UI thread, yielding at Background
- // priority between them so input and rendering always go first.
+ // Build the deferred warp cache on the engine's worker thread, polling for progress. The
+ // UI thread only installs the finished cache, so a long file no longer stutters the app.
private async Task BuildImportWarpAsync(string name, Func<bool> alive)
{
long total = Engine.WarpBuildRemaining;
- long remaining = total;
- while (remaining > 0 && alive())
+ await PollWarpBuildAsync(alive, remaining =>
{
- remaining = Engine.WarpBuildStep(48000);
double frac = total > 0 ? Math.Clamp(1.0 - (double)remaining / total, 0.0, 1.0) : 1.0;
- if (_vm is not null) _vm.StatusText = $"Warping {name}… {frac * 100:0} %";
- await Dispatcher.UIThread.InvokeAsync(() => { }, DispatcherPriority.Background);
+ if (_vm is not null) _vm.StatusText = $"Warping {name}… {frac * 100:0}\u2009%";
+ });
+ }
+
+ // Drive the background warp build until every stale cache is rebuilt (or alive() turns
+ // false). Several loops may run at once (an import + an editor edit): each poll is cheap
+ // and idempotent, so they simply share the work.
+ private async Task PollWarpBuildAsync(Func<bool>? alive = null, Action<long>? progress = null)
+ {
+ long remaining;
+ while ((remaining = Engine.WarpBuildPoll()) > 0 && (alive?.Invoke() ?? true))
+ {
+ progress?.Invoke(remaining);
+ await Task.Delay(WarpPollMs);
}
}
+ private const int WarpPollMs = 15;
+
private bool TrackExists(int trackId)
{
int n = Engine.TrackCount;
diff --git a/src/managed/Nota.App/MainWindow.Detail.cs b/src/managed/Nota.App/MainWindow.Detail.cs
index 026000da..3372e864 100644
--- a/src/managed/Nota.App/MainWindow.Detail.cs
+++ b/src/managed/Nota.App/MainWindow.Detail.cs
@@ -369,6 +369,7 @@ public partial class MainWindow
bool slot = SessionClip.SceneOf(clipIndex) >= 0;
_audioEditor = new AudioClipEditorView(Engine, trackId, clipIndex,
TrackBrush(trackId), slot ? () => _session?.Refresh() : () => Timeline.Refresh());
+ _audioEditor.WarpBuildRequested = () => _ = PollWarpBuildAsync();
_audioEditorTrackId = trackId;
_audioEditorClipIndex = clipIndex;
}
diff --git a/src/managed/Nota.App/MainWindow.Files.cs b/src/managed/Nota.App/MainWindow.Files.cs
index 12326c7b..3aedfaf0 100644
--- a/src/managed/Nota.App/MainWindow.Files.cs
+++ b/src/managed/Nota.App/MainWindow.Files.cs
@@ -263,7 +263,7 @@ public partial class MainWindow
}
}
- // Fill the warp-clip caches deferred during load, incrementally on the UI thread so the
+ // Fill the warp-clip caches deferred during load, on the engine's worker thread, so the
// status-bar strip advances and the app stays responsive. Warped clips are silent until
// their cache lands (a few seconds for a big project); everything else plays immediately.
private async Task BuildWarpCachesAsync()
@@ -274,16 +274,12 @@ public partial class MainWindow
if (total <= 0) return;
await RunBackgroundAsync("Preparing warped clips…", async prog =>
{
- // ~a few ms of stretching per step keeps each UI frame short; yield so it paints.
- const int chunk = 48000; // device frames per step
- long remaining = total;
- while (remaining > 0 && gen == _warpBuildGen) // bail if another project opened
+ // The stretch runs on the engine's worker thread; the UI only polls + installs.
+ await PollWarpBuildAsync(() => gen == _warpBuildGen, remaining => // bail if another project opened
{
- remaining = Engine.WarpBuildStep(chunk);
double frac = Math.Clamp(1.0 - (double)remaining / total, 0.0, 1.0);
prog.Report(ProgressReport.At(frac, $"Preparing warped clips… {frac * 100:0}\u2009%"));
- await Task.Yield();
- }
+ });
});
}
diff --git a/src/managed/Nota.Application/Engine/IAudioEngine.cs b/src/managed/Nota.Application/Engine/IAudioEngine.cs
index 95c676d5..993cd87f 100644
--- a/src/managed/Nota.Application/Engine/IAudioEngine.cs
+++ b/src/managed/Nota.Application/Engine/IAudioEngine.cs
@@ -219,6 +219,10 @@ public interface IAudioEngine : IDisposable
long WarpBuildStep(int maxFrames);
/// <summary>Frames of warped audio still needing a cache (progress denominator).</summary>
long WarpBuildRemaining { get; }
+ /// <summary>Background warp build: installs a cache a worker finished (if its clip is
+ /// unchanged), then starts the next clip needing one on a worker. Never blocks — poll it
+ /// from a timer until it returns 0. Clips keep playing their previous cache meanwhile.</summary>
+ long WarpBuildPoll();
/// <summary>Replaces a warped clip's source↔beat markers (≥2).</summary>
void SetClipWarpMarkers(int trackId, int clipIndex, double[] srcFrames, double[] beats);
/// <summary>Reads a warped clip's markers into the buffers; returns the total count.</summary>
@@ -347,6 +351,10 @@ public interface IAudioEngine : IDisposable
int GetClipSourcePeaks(int trackId, int clipIndex, float[] outMinMax, int maxPoints);
/// <summary>Peaks over the full warped material (ignores the trim window) for the warped-clip Start/End brackets.</summary>
int GetClipWarpFullPeaks(int trackId, int clipIndex, float[] outMinMax, int maxPoints);
+ /// <summary>Peaks over warped beats [beat0, beat1] of the full material, file order (≤ 8192 points) — the zoomed clip editor's detail.</summary>
+ int GetClipWarpPeaksRange(int trackId, int clipIndex, double beat0, double beat1, float[] outMinMax, int maxPoints);
+ /// <summary>Peaks over source frames [frame0, frame1) of the whole sample (≤ 8192 points) — the zoomed clip editor's detail.</summary>
+ int GetClipSourcePeaksRange(int trackId, int clipIndex, double frame0, double frame1, float[] outMinMax, int maxPoints);
// --- Instrument tracks, MIDI clips & notes -----------------------------
int AddInstrumentTrack();
diff --git a/src/managed/Nota.Infrastructure/Interop/NativeMethods.Tracks.cs b/src/managed/Nota.Infrastructure/Interop/NativeMethods.Tracks.cs
index 863776c7..08801bc7 100644
--- a/src/managed/Nota.Infrastructure/Interop/NativeMethods.Tracks.cs
+++ b/src/managed/Nota.Infrastructure/Interop/NativeMethods.Tracks.cs
@@ -138,6 +138,9 @@ internal static partial class NativeMethods
[LibraryImport(Lib, EntryPoint = "nota_engine_warp_build_remaining")]
internal static partial long WarpBuildRemaining(IntPtr engine);
+ [LibraryImport(Lib, EntryPoint = "nota_engine_warp_build_poll")]
+ internal static partial long WarpBuildPoll(IntPtr engine);
+
[LibraryImport(Lib, EntryPoint = "nota_clip_volume_env_get")]
internal static partial int ClipVolumeEnvGet(IntPtr engine, int trackId, int clipIndex, [Out] AutomationPoint[]? outPoints, int cap);
@@ -323,6 +326,14 @@ internal static partial class NativeMethods
internal static partial int ClipGetWarpFullPeaks(IntPtr engine, int trackId, int clipIndex,
[Out] float[] outMinMax, int maxPoints);
+ [LibraryImport(Lib, EntryPoint = "nota_clip_get_warp_peaks_range")]
+ internal static partial int ClipGetWarpPeaksRange(IntPtr engine, int trackId, int clipIndex, double beat0, double beat1,
+ [Out] float[] outMinMax, int maxPoints);
+
+ [LibraryImport(Lib, EntryPoint = "nota_clip_get_source_peaks_range")]
+ internal static partial int ClipGetSourcePeaksRange(IntPtr engine, int trackId, int clipIndex, double frame0, double frame1,
+ [Out] float[] outMinMax, int maxPoints);
+
[LibraryImport(Lib, EntryPoint = "nota_engine_add_instrument_track")]
internal static partial int AddInstrumentTrack(IntPtr engine);
diff --git a/src/managed/Nota.Infrastructure/Interop/NotaEngine.Tracks.cs b/src/managed/Nota.Infrastructure/Interop/NotaEngine.Tracks.cs
index ce797be2..6a091179 100644
--- a/src/managed/Nota.Infrastructure/Interop/NotaEngine.Tracks.cs
+++ b/src/managed/Nota.Infrastructure/Interop/NotaEngine.Tracks.cs
@@ -189,6 +189,11 @@ public sealed partial class NotaEngine
public long WarpBuildRemaining
{ get { ThrowIfDisposed(); return NativeMethods.WarpBuildRemaining(_handle); } }
+ /// <summary>Background warp build: installs a finished cache, starts the next stale clip on
+ /// a worker thread. Never blocks; returns frames still needing a cache (0 = done).</summary>
+ public long WarpBuildPoll()
+ { ThrowIfDisposed(); return NativeMethods.WarpBuildPoll(_handle); }
+
/// <summary>Per-clip volume envelope (0..1, clip-local beats).</summary>
public AutomationPoint[] GetClipVolumeEnvelope(int trackId, int clipIndex)
{
@@ -544,6 +549,20 @@ public sealed partial class NotaEngine
return NativeMethods.ClipGetWarpFullPeaks(_handle, trackId, clipIndex, outMinMax, maxPoints);
}
+ /// <summary>Peaks over warped beats [beat0, beat1] of the full material (zoomed clip editor).</summary>
+ public int GetClipWarpPeaksRange(int trackId, int clipIndex, double beat0, double beat1, float[] outMinMax, int maxPoints)
+ {
+ ThrowIfDisposed();
+ return NativeMethods.ClipGetWarpPeaksRange(_handle, trackId, clipIndex, beat0, beat1, outMinMax, maxPoints);
+ }
+
+ /// <summary>Peaks over source frames [frame0, frame1) of the whole sample (zoomed clip editor).</summary>
+ public int GetClipSourcePeaksRange(int trackId, int clipIndex, double frame0, double frame1, float[] outMinMax, int maxPoints)
+ {
+ ThrowIfDisposed();
+ return NativeMethods.ClipGetSourcePeaksRange(_handle, trackId, clipIndex, frame0, frame1, outMinMax, maxPoints);
+ }
+
// --- Instrument tracks, MIDI clips & notes -----------------------------
/// <summary>Adds an instrument track with the built-in Nota Synth. Returns its id.</summary>
diff --git a/src/native/nota.engine/include/nota/nota_engine.h b/src/native/nota.engine/include/nota/nota_engine.h
index a4712128..725cf4dc 100644
--- a/src/native/nota.engine/include/nota/nota_engine.h
+++ b/src/native/nota.engine/include/nota/nota_engine.h
@@ -532,6 +532,16 @@ NOTA_API int32_t nota_clip_get_source_peaks(const NotaEngine* engine, int32_t tr
* clip editor's warped-clip Start/End brackets. Same output layout. */
NOTA_API int32_t nota_clip_get_warp_full_peaks(const NotaEngine* engine, int32_t track_id,
int32_t clip_index, float* out_min_max, int32_t max_points);
+/* Zoom-aware clip-editor peaks over a sub-range, file order, up to 8192 points:
+ * warp_peaks_range spans warped beats [beat0, beat1] of the full material (via the
+ * markers); source_peaks_range spans source frames [frame0, frame1) of the whole sample.
+ * Buckets narrower than one frame read that single frame (min == max). */
+NOTA_API int32_t nota_clip_get_warp_peaks_range(const NotaEngine* engine, int32_t track_id,
+ int32_t clip_index, double beat0, double beat1,
+ float* out_min_max, int32_t max_points);
+NOTA_API int32_t nota_clip_get_source_peaks_range(const NotaEngine* engine, int32_t track_id,
+ int32_t clip_index, double frame0, double frame1,
+ float* out_min_max, int32_t max_points);
/* ---- Background audio import -------------------------------------------- */
/* Decodes a WAV/FLAC/MP3 block by block on ANY thread (it touches no engine state), so
@@ -1016,6 +1026,12 @@ NOTA_API double nota_clip_auto_warp(NotaEngine* engine, int32_t track_id, in
NOTA_API void nota_engine_set_defer_warp_build(NotaEngine* engine, int32_t defer);
NOTA_API int64_t nota_engine_warp_build_step(NotaEngine* engine, int32_t max_frames);
NOTA_API int64_t nota_engine_warp_build_remaining(const NotaEngine* engine);
+/* Background warp-cache build: never blocks. Installs a cache a worker thread finished
+ * (if its clip is unchanged since), then starts the next clip needing one on a worker.
+ * Drive it from a timer until it returns 0. Pair with set_defer_warp_build around edits so
+ * they skip the synchronous stretch; a clip keeps playing its previous (stale) cache —
+ * clipped to its new length — until the new one lands. Returns frames still remaining. */
+NOTA_API int64_t nota_engine_warp_build_poll(NotaEngine* engine);
/* Beats-mode warp: detect transients and pin a grid-snapped warp marker at each hit
* so percussion locks tightly to the grid. Sets Beats mode. Returns the detected BPM,
* or 0 when detection fails (clip left unchanged). */
diff --git a/src/native/nota.engine/src/Engine.cpp b/src/native/nota.engine/src/Engine.cpp
index 37f6c316..e8ab1caa 100644
--- a/src/native/nota.engine/src/Engine.cpp
+++ b/src/native/nota.engine/src/Engine.cpp
@@ -46,6 +46,7 @@ Engine::Engine()
}
Engine::~Engine() {
+ cancelWarpJob(); // a background warp build must not outlive the engine
stop();
if (input_) input_->stop(); // monitoring can keep the capture device open until teardown
}
diff --git a/src/native/nota.engine/src/Engine.h b/src/native/nota.engine/src/Engine.h
index 61a527bd..4141240c 100644
--- a/src/native/nota.engine/src/Engine.h
+++ b/src/native/nota.engine/src/Engine.h
@@ -34,6 +34,7 @@
#include <mutex>
#include <unordered_map>
#include <string>
+#include <thread>
#include <vector>
namespace nota {
@@ -383,11 +384,16 @@ public:
int32_t getClipPeaks(int32_t trackId, int32_t clipIndex, float* outMinMax, int32_t maxPoints) const;
int32_t getClipSourcePeaks(int32_t trackId, int32_t clipIndex, float* outMinMax, int32_t maxPoints) const;
int32_t getClipWarpFullPeaks(int32_t trackId, int32_t clipIndex, float* outMinMax, int32_t maxPoints) const;
+ int32_t getClipWarpPeaksRange(int32_t trackId, int32_t clipIndex, double beat0, double beat1, float* outMinMax, int32_t maxPoints) const;
+ int32_t getClipSourcePeaksRange(int32_t trackId, int32_t clipIndex, double frame0, double frame1, float* outMinMax, int32_t maxPoints) const;
// --- incremental warp-cache build (project load, with a progress bar) ---
void setDeferWarpBuild(bool defer); // load path: skip cache builds, fill them via warpBuildStep
int64_t warpBuildStep(int32_t maxFrames); // build ~maxFrames of warped audio; returns frames remaining
int64_t warpBuildRemainingFrames() const; // frames of warped audio still needing a cache (progress)
+ // Background build (editor edits, imports, project load): installs a finished cache and
+ // starts the next stale clip on a worker thread. Never blocks; returns frames remaining.
+ int64_t warpBuildPoll();
// --- arrangement geometry (M4) ---
bool trackInfo(int32_t index, NotaTrackInfo* out) const;
@@ -1490,6 +1496,25 @@ private:
std::unique_ptr<ClipWarpStream> stream;
int64_t offset = 0;
} wb_;
+ // The ONE clip being stretched on a worker thread (warpBuildPoll). The worker only reads
+ // its own copies (inputs) and the immutable sample; the authoring thread installs the
+ // result if the clip still has those inputs, else drops it. Cancelled + joined on teardown.
+ struct WarpJob {
+ int32_t trackId = -1;
+ int32_t clipIndex = -1;
+ std::shared_ptr<SampleBuffer> sample;
+ std::shared_ptr<WarpCache> cache;
+ std::atomic<int64_t> done{0};
+ std::atomic<bool> cancel{false};
+ std::atomic<bool> finished{false};
+ std::thread thread;
+ };
+ std::unique_ptr<WarpJob> wj_;
+ void cancelWarpJob();
+ // Warped clip whose cache is missing or built from other inputs (needs a (re)build).
+ bool warpNeedsBuild(const AudioClip& c) const;
+ // The next clip needing a build (arrangement clips, then session slots), or null.
+ const AudioClip* nextWarpBuildTarget(int32_t& trackId, int32_t& clipIndex) const;
// Per-clip scratch for a warped clip carrying a volume envelope (render there,
// then fold into the mix with the per-sample envelope gain). M9 follow-up.
std::vector<float> clipEnvScratch_;
diff --git a/src/native/nota.engine/src/Engine_Render.cpp b/src/native/nota.engine/src/Engine_Render.cpp
index cafbc2b0..29eb5ebe 100644
--- a/src/native/nota.engine/src/Engine_Render.cpp
+++ b/src/native/nota.engine/src/Engine_Render.cpp
@@ -796,7 +796,10 @@ void Engine::mixAudioClip(const AudioClip& clip, float* dst, int32_t frames, dou
// cache's frame count (both are warpPlayLen×spb once rebuilt for the tempo).
if (clip.warpEnabled && clip.warpCache && clip.warpCache->frames > 0) {
const WarpCache& wc = *clip.warpCache;
- const double clipDeviceLen = static_cast<double>(wc.frames);
+ // A stale cache (an edit's rebuild still running in the background) keeps playing,
+ // clipped to the clip's new played length so it never spills past the clip's end.
+ const double clipDeviceLen = static_cast<double>(
+ std::min<int64_t>(wc.frames, std::llround(clip.warpPlayLen() * spb)));
lenBeats = clipDeviceLen / spb;
const double ovStart = std::max(blockStart, startSamples);
const double ovEnd = std::min(blockStart + frames, startSamples + clipDeviceLen);
diff --git a/src/native/nota.engine/src/Engine_Session.cpp b/src/native/nota.engine/src/Engine_Session.cpp
index 2fe27b86..ddce7b27 100644
--- a/src/native/nota.engine/src/Engine_Session.cpp
+++ b/src/native/nota.engine/src/Engine_Session.cpp
@@ -438,7 +438,7 @@ SessionSlot Engine::slotFromAudioClip(const AudioClip& a) {
s.audio = a;
s.audio.startBeat = 0.0;
s.audio.active = true;
- if (s.audio.warpEnabled && !s.audio.warpCache) // a clipboard copy dropped its cache
+ if (warpNeedsBuild(s.audio)) // a clipboard copy dropped its cache (or it is stale)
configureClipWarp(s.audio, transport_.samplesPerBeat(), transport_.sampleRate());
s.name = a.name;
const double beats = audioClipBeats(s.audio);
diff --git a/src/native/nota.engine/src/Engine_Tracks.cpp b/src/native/nota.engine/src/Engine_Tracks.cpp
index abf50185..9420aa06 100644
--- a/src/native/nota.engine/src/Engine_Tracks.cpp
+++ b/src/native/nota.engine/src/Engine_Tracks.cpp
@@ -151,6 +151,40 @@ int64_t warpCacheFrames(const AudioClip& c, double spb, double devSR) {
return std::llround(c.warpPlayLen() * spb);
}
+// What a cache built for c right now would be stretched from (see WarpInputs).
+WarpInputs warpInputsOf(const AudioClip& c, double spb, double devSR) {
+ WarpInputs in;
+ in.sample = c.sample.get();
+ in.markers = c.warpMarkers;
+ in.warpBeats = c.warpMarkers.size() >= 2 ? c.warpMarkers.back().beat : c.warpBeats;
+ in.playStart = c.warpPlayStart;
+ in.playEnd = c.warpPlayEndEff();
+ in.mode = c.warpMode;
+ in.pitch = c.pitchSemitones;
+ in.spb = spb;
+ in.devSR = devSR;
+ return in;
+}
+
+// Stretch `frames` device frames of the played window described by `in` into `out`
+// (interleaved stereo). The stream primes once on the first chunk, then runs seamlessly
+// (drift-free feed) to the end. Gain is 1 (applied at read time). Stops early — returning
+// false — when `cancel` is raised; `done` (optional) tracks progress for a worker build.
+bool renderWarpWindow(const WarpInputs& in, const SampleBuffer& sample, float* out, int64_t frames,
+ const std::atomic<bool>* cancel = nullptr, std::atomic<int64_t>* done = nullptr) {
+ ClipWarpStream st;
+ st.configure(sample.channels, in.mode, in.pitch, sample.sourceSampleRate, in.devSR);
+ const double warpOffset = in.playStart * in.spb;
+ for (int64_t off = 0; off < frames; off += kWarpBuildChunk) {
+ if (cancel && cancel->load(std::memory_order_relaxed)) return false;
+ const int32_t n = static_cast<int32_t>(std::min<int64_t>(kWarpBuildChunk, frames - off));
+ st.render(out + off * 2, n, warpOffset + static_cast<double>(off),
+ in.markers, in.warpBeats, sample, in.spb, in.devSR, 1.0f);
+ if (done) done->store(off + n, std::memory_order_relaxed);
+ }
+ return true;
+}
+
// Build a warped clip's offline stretch cache, or clear it when off.
// Runs the streaming stretcher ONCE over the whole played window on the authoring
// thread and captures the device-rate output, so the audio thread just copies from
@@ -168,20 +202,11 @@ void buildWarpCache(AudioClip& c, double spb, double devSR) {
cache->frames = frames;
cache->spb = spb;
cache->devSR = devSR;
+ cache->inputs = warpInputsOf(c, spb, devSR);
cache->samples.assign(static_cast<size_t>(frames) * 2, 0.0f);
-
- ClipWarpStream st;
- st.configure(c.sample->channels, c.warpMode, c.pitchSemitones, c.sample->sourceSampleRate, devSR);
- // Render the played window [warpPlayStart, warpPlayEndEff] in contiguous chunks: the
- // stream primes once on the first chunk, then runs seamlessly (drift-free feed) to the
- // end — exactly the audio the old realtime path produced, minus the RT cost. Gain is 1
- // here (applied at read time), so gain changes don't invalidate the cache.
- const double warpOffset = c.warpPlayStart * spb;
- for (int64_t off = 0; off < frames; off += kWarpBuildChunk) {
- const int32_t n = static_cast<int32_t>(std::min<int64_t>(kWarpBuildChunk, frames - off));
- st.render(cache->samples.data() + off * 2, n, warpOffset + static_cast<double>(off),
- c.warpMarkers, c.warpBeats, *c.sample, spb, devSR, 1.0f);
- }
+ // The played window [warpPlayStart, warpPlayEndEff] — exactly the audio the old realtime
+ // path produced, minus the RT cost. Gain changes don't invalidate the cache.
+ renderWarpWindow(cache->inputs, *c.sample, cache->samples.data(), frames);
c.warpCache = std::move(cache);
}
@@ -224,15 +249,44 @@ void clampWarpPlay(AudioClip& c) {
}
} // namespace
-// Build (or clear) a clip's warp cache — unless the load path has asked to defer it.
+// Build (or clear) a clip's warp cache — unless the caller has asked to defer it.
// During project load `deferWarpBuild_` is set so Apply's many warp edits stay cheap;
-// the caches are then filled incrementally by warpBuildStep with a progress bar, so a
+// the caches are then filled by warpBuildStep / warpBuildPoll with a progress bar, so a
// project with hundreds of warped clips opens at once instead of freezing for ~a minute.
+// Editor edits defer too: the clip keeps its now-stale cache (played clipped to the new
+// length) until the background build replaces it, instead of the UI freezing on a long file.
void Engine::configureClipWarp(AudioClip& c, double spb, double devSR) {
- if (deferWarpBuild_) { c.warpCache = nullptr; return; } // filled later by warpBuildStep
+ if (!c.warpEnabled) { c.warpCache = nullptr; return; }
+ if (deferWarpBuild_) return; // stale (or no) cache until warpBuildStep / warpBuildPoll
buildWarpCache(c, spb, devSR);
}
+bool Engine::warpNeedsBuild(const AudioClip& c) const {
+ const double spb = transport_.samplesPerBeat(), devSR = transport_.sampleRate();
+ if (warpCacheFrames(c, spb, devSR) <= 0) return false;
+ return !c.warpCache || !(c.warpCache->inputs == warpInputsOf(c, spb, devSR));
+}
+
+const AudioClip* Engine::nextWarpBuildTarget(int32_t& trackId, int32_t& clipIndex) const {
+ if (!authoring_) return nullptr;
+ for (auto& t : authoring_->tracks) {
+ if (t->type() != TrackType::Audio) continue;
+ for (size_t i = 0; i < t->clips.size(); ++i)
+ if (warpNeedsBuild(t->clips[i])) {
+ trackId = t->id(); clipIndex = static_cast<int32_t>(i);
+ return &t->clips[i];
+ }
+ for (size_t sc = 0; sc < t->sessionSlots.size(); ++sc) {
+ const SessionSlot& sl = t->sessionSlots[sc];
+ if (sl.hasClip && warpNeedsBuild(sl.audio)) {
+ trackId = t->id(); clipIndex = sessionClipIndex(static_cast<int32_t>(sc));
+ return &sl.audio;
+ }
+ }
+ }
+ return nullptr;
+}
+
// --- incremental warp-cache build (managed-driven, with a progress bar) --------------
// The load path defers cache building (above); these drive it afterwards in bounded
// chunks on the authoring thread so the UI stays responsive and a progress bar advances
@@ -249,9 +303,8 @@ const AudioClip* Engine::warpBuildTargetClip() const {
const AudioClip* cp = audioClipRef(*t, wb_.clipIndex); // an arrangement clip or a session slot's
if (!cp) return nullptr;
const AudioClip& c = *cp;
- if (!c.warpEnabled || c.warpCache) return nullptr;
- if (!wb_.cache || warpCacheFrames(c, transport_.samplesPerBeat(), transport_.sampleRate()) != wb_.cache->frames)
- return nullptr;
+ if (!wb_.cache || !warpNeedsBuild(c)) return nullptr;
+ if (!(warpInputsOf(c, transport_.samplesPerBeat(), transport_.sampleRate()) == wb_.cache->inputs)) return nullptr;
return &c;
}
@@ -264,13 +317,14 @@ int64_t Engine::warpBuildRemainingFrames() const {
for (auto& t : authoring_->tracks) {
if (t->type() != TrackType::Audio) continue;
for (auto& c : t->clips)
- if (c.warpEnabled && !c.warpCache) sum += warpCacheFrames(c, spb, devSR);
+ if (warpNeedsBuild(c)) sum += warpCacheFrames(c, spb, devSR);
for (auto& sl : t->sessionSlots)
- if (sl.hasClip && sl.audio.warpEnabled && !sl.audio.warpCache) sum += warpCacheFrames(sl.audio, spb, devSR);
+ if (sl.hasClip && warpNeedsBuild(sl.audio)) sum += warpCacheFrames(sl.audio, spb, devSR);
}
// The in-progress clip is still un-cached above, so its full length was counted —
// subtract what's already rendered so the bar reflects real progress.
if (warpBuildTargetClip()) sum -= wb_.offset;
+ if (wj_ && !wj_->cancel.load(std::memory_order_relaxed)) sum -= wj_->done.load(std::memory_order_relaxed);
return std::max<int64_t>(0, sum);
}
@@ -282,32 +336,19 @@ int64_t Engine::warpBuildStep(int32_t maxFrames) {
const double devSR = transport_.sampleRate();
if (spb <= 0.0 || devSR <= 0.0) return 0;
if (maxFrames < kWarpBuildChunk) maxFrames = kWarpBuildChunk;
+ cancelWarpJob(); // the synchronous path owns the build from here
int32_t budget = maxFrames;
while (budget > 0) {
- // Idle: find the next warp clip without a cache and start building it.
+ // Idle: find the next warp clip needing a cache and start building it.
if (!wb_.building) {
- int32_t tid = -1, ci = -1; const AudioClip* target = nullptr;
- for (auto& t : authoring_->tracks) {
- if (t->type() != TrackType::Audio) continue;
- for (size_t i = 0; i < t->clips.size(); ++i) {
- const AudioClip& c = t->clips[i];
- if (c.warpEnabled && !c.warpCache && warpCacheFrames(c, spb, devSR) > 0) {
- target = &c; tid = t->id(); ci = static_cast<int32_t>(i); break;
- }
- }
- for (size_t sc = 0; !target && sc < t->sessionSlots.size(); ++sc) {
- const SessionSlot& sl = t->sessionSlots[sc];
- if (sl.hasClip && sl.audio.warpEnabled && !sl.audio.warpCache && warpCacheFrames(sl.audio, spb, devSR) > 0) {
- target = &sl.audio; tid = t->id(); ci = sessionClipIndex(static_cast<int32_t>(sc));
- }
- }
- if (target) break;
- }
+ int32_t tid = -1, ci = -1;
+ const AudioClip* target = nextWarpBuildTarget(tid, ci);
if (!target) break; // nothing left to build
const int64_t frames = warpCacheFrames(*target, spb, devSR);
wb_.cache = std::make_shared<WarpCache>();
wb_.cache->frames = frames; wb_.cache->spb = spb; wb_.cache->devSR = devSR;
+ wb_.cache->inputs = warpInputsOf(*target, spb, devSR);
wb_.cache->samples.assign(static_cast<size_t>(frames) * 2, 0.0f);
wb_.stream = std::make_unique<ClipWarpStream>();
wb_.stream->configure(target->sample->channels, target->warpMode, target->pitchSemitones,
@@ -349,6 +390,61 @@ int64_t Engine::warpBuildStep(int32_t maxFrames) {
void Engine::setDeferWarpBuild(bool defer) { deferWarpBuild_ = defer; }
+void Engine::cancelWarpJob() {
+ if (!wj_) return;
+ wj_->cancel.store(true, std::memory_order_relaxed);
+ if (wj_->thread.joinable()) wj_->thread.join();
+ wj_.reset();
+}
+
+// Background warp build. Each call (from the UI's poll loop): if the worker finished,
+// install its cache — only if the clip still has the inputs it was built from, else drop
+// it — then start the next clip needing a build on a fresh worker. A clip edited while its
+// build runs cancels that build early. Returns frames still remaining (0 = all built).
+int64_t Engine::warpBuildPoll() {
+ if (!authoring_) return 0;
+ const double spb = transport_.samplesPerBeat();
+ const double devSR = transport_.sampleRate();
+ if (spb <= 0.0 || devSR <= 0.0) return 0;
+
+ if (wj_) {
+ const AudioClip* c = nullptr;
+ if (auto t = findTrackAuthoring(wj_->trackId); t && t->type() == TrackType::Audio)
+ c = audioClipRef(*t, wj_->clipIndex);
+ const bool valid = c && warpNeedsBuild(*c)
+ && warpInputsOf(*c, spb, devSR) == wj_->cache->inputs;
+ if (!valid) wj_->cancel.store(true, std::memory_order_relaxed);
+ if (valid && !wj_->finished.load(std::memory_order_acquire)) return warpBuildRemainingFrames();
+ if (wj_->thread.joinable()) wj_->thread.join();
+ if (valid && !wj_->cancel.load(std::memory_order_relaxed)) {
+ auto nt = cloneTrack(*findTrackAuthoring(wj_->trackId));
+ audioClipRef(*nt, wj_->clipIndex)->warpCache = std::const_pointer_cast<const WarpCache>(wj_->cache);
+ republishWithTrackRaw(wj_->trackId, nt); // no undo entry — a cache isn't an edit
+ }
+ wj_.reset();
+ }
+
+ int32_t tid = -1, ci = -1;
+ const AudioClip* target = nextWarpBuildTarget(tid, ci);
+ if (!target) return 0;
+ auto job = std::make_unique<WarpJob>();
+ job->trackId = tid; job->clipIndex = ci;
+ job->sample = target->sample; // keeps the buffer alive for the worker
+ job->cache = std::make_shared<WarpCache>();
+ job->cache->frames = warpCacheFrames(*target, spb, devSR);
+ job->cache->spb = spb; job->cache->devSR = devSR;
+ job->cache->inputs = warpInputsOf(*target, spb, devSR);
+ WarpJob* j = job.get();
+ job->thread = std::thread([j] {
+ WarpCache& wc = *j->cache;
+ wc.samples.assign(static_cast<size_t>(wc.frames) * 2, 0.0f);
+ renderWarpWindow(wc.inputs, *j->sample, wc.samples.data(), wc.frames, &j->cancel, &j->done);
+ j->finished.store(true, std::memory_order_release);
+ });
+ wj_ = std::move(job);
+ return warpBuildRemainingFrames();
+}
+
// Rebuild every warped clip's offline stretch cache for the given tempo + device rate.
// The stretched audio depends on both (spb sets the played length; the device rate sets
// the window sizes + src≠device pitch compensation), so a tempo or SR change must re-run
@@ -804,12 +900,12 @@ int32_t Engine::trackReturnIndex(int32_t id) const {
// marker map (time-stretch preserves the amplitude envelope). Shared by the played
// window (getClipPeaks) and the full-material view (getClipWarpFullPeaks).
static int32_t warpPeaksRange(const AudioClip& clip, float* out, int32_t maxPoints,
- double beat0, double beat1) {
+ double beat0, double beat1, int32_t cap = 2048) {
if (!out || maxPoints <= 0 || !clip.sample) return 0;
const auto& m = clip.warpMarkers;
if (m.size() < 2) return 0;
const SampleBuffer& sb = *clip.sample;
- const int32_t buckets = std::min<int32_t>(maxPoints, 2048);
+ const int32_t buckets = std::min<int32_t>(maxPoints, cap);
const double span = beat1 - beat0;
if (!(span > 0.0)) return 0;
auto srcAtBeat = [&](double beat) -> double {
@@ -919,6 +1015,45 @@ int32_t Engine::getClipWarpFullPeaks(int32_t trackId, int32_t clipIndex,
return warpPeaksRange(clip, outMinMax, maxPoints, 0.0, clip.warpBeats);
}
+// The clip editor's zoomed-in detail: peaks over just the visible slice, so zooming
+// re-resolves the waveform instead of magnifying the whole-clip overview into blocks.
+constexpr int32_t kRangePeaksCap = 8192;
+
+int32_t Engine::getClipWarpPeaksRange(int32_t trackId, int32_t clipIndex, double beat0, double beat1,
+ float* outMinMax, int32_t maxPoints) const {
+ auto t = findTrackAuthoring(trackId);
+ if (!t || !audioClipRef(*t, clipIndex)) return 0;
+ const AudioClip& clip = (*audioClipRef(*t, clipIndex));
+ if (!(clip.warpEnabled && clip.warpBeats > 0.0)) return 0;
+ beat0 = std::clamp(beat0, 0.0, clip.warpBeats);
+ beat1 = std::clamp(beat1, 0.0, clip.warpBeats);
+ return warpPeaksRange(clip, outMinMax, maxPoints, beat0, beat1, kRangePeaksCap);
+}
+
+int32_t Engine::getClipSourcePeaksRange(int32_t trackId, int32_t clipIndex, double frame0, double frame1,
+ float* outMinMax, int32_t maxPoints) const {
+ if (!outMinMax || maxPoints <= 0) return 0;
+ auto t = findTrackAuthoring(trackId);
+ if (!t || !audioClipRef(*t, clipIndex)) return 0;
+ const AudioClip& clip = (*audioClipRef(*t, clipIndex));
+ if (!clip.sample) return 0;
+ const SampleBuffer& sb = *clip.sample;
+ const double total = static_cast<double>(sb.frames);
+ frame0 = std::clamp(frame0, 0.0, total);
+ frame1 = std::clamp(frame1, 0.0, total);
+ if (!(frame1 > frame0)) return 0;
+ const int32_t buckets = std::min(maxPoints, kRangePeaksCap);
+ const double per = (frame1 - frame0) / buckets;
+ for (int32_t b = 0; b < buckets; ++b) {
+ const int64_t f0 = static_cast<int64_t>(std::floor(frame0 + per * b));
+ const int64_t f1 = std::max<int64_t>(f0 + 1, static_cast<int64_t>(std::floor(frame0 + per * (b + 1))));
+ float mn, mx;
+ sb.peakRange(f0, f1, mn, mx);
+ outMinMax[b * 2] = mn; outMinMax[b * 2 + 1] = mx;
+ }
+ return buckets;
+}
+
// --- arrangement geometry (M4) ---------------------------------------------
bool Engine::trackInfo(int32_t index, NotaTrackInfo* out) const {
@@ -1720,7 +1855,7 @@ AudioClip Engine::consolidateAudioClips(const std::vector<AudioClip>& clips, dou
if (ce <= s + eps || cs >= e - eps) continue;
src.push_back(c);
// A deferred (project-load) warp cache isn't built yet; the renderer would skip it.
- if (src.back().warpEnabled && !src.back().warpCache) buildWarpCache(src.back(), spb, devSR);
+ if (warpNeedsBuild(src.back())) buildWarpCache(src.back(), spb, devSR);
}
std::sort(src.begin(), src.end(), [](const AudioClip& x, const AudioClip& y) { return x.startBeat < y.startBeat; });
diff --git a/src/native/nota.engine/src/Warp.h b/src/native/nota.engine/src/Warp.h
index 8ba50400..a25f7805 100644
--- a/src/native/nota.engine/src/Warp.h
+++ b/src/native/nota.engine/src/Warp.h
@@ -28,7 +28,26 @@ enum class WarpMode : int32_t {
// A warp marker ties a source position (frames) to a musical position (beats).
// Consecutive markers bound a segment stretched independently to its beat span.
-struct WarpMarker { double srcFrame; double beat; };
+struct WarpMarker {
+ double srcFrame; double beat;
+ bool operator==(const WarpMarker&) const = default;
+};
+
+// Everything a warp cache's audio depends on, captured when it is built. A clip whose
+// current inputs differ from its cache's holds a STALE cache: it keeps playing it (clipped
+// to the new played length) until the rebuild lands, rather than going silent.
+struct WarpInputs {
+ const SampleBuffer* sample = nullptr;
+ std::vector<WarpMarker> markers;
+ double warpBeats = 0.0;
+ double playStart = 0.0;
+ double playEnd = 0.0; // effective end (beats)
+ WarpMode mode = WarpMode::Complex;
+ float pitch = 0.0f;
+ double spb = 0.0;
+ double devSR = 0.0;
+ bool operator==(const WarpInputs&) const = default;
+};
// Offline stretch cache: the clip's played window rendered ONCE to
// a device-rate interleaved-stereo buffer on the authoring thread, so the audio
@@ -42,6 +61,7 @@ struct WarpCache {
int64_t frames = 0; // played-window length in device frames
double spb = 0.0; // device samples-per-beat this was built for
double devSR = 0.0; // device sample rate this was built for
+ WarpInputs inputs; // what it was stretched from (staleness check)
};
} // namespace nota
diff --git a/src/native/nota.engine/src/nota_engine_track.cpp b/src/native/nota.engine/src/nota_engine_track.cpp
index 55068349..3113143a 100644
--- a/src/native/nota.engine/src/nota_engine_track.cpp
+++ b/src/native/nota.engine/src/nota_engine_track.cpp
@@ -104,6 +104,16 @@ int32_t nota_clip_get_warp_full_peaks(const NotaEngine* e, int32_t track_id, int
if (!e) return 0;
return CENG(e)->getClipWarpFullPeaks(track_id, clip_index, out_min_max, max_points);
}
+int32_t nota_clip_get_warp_peaks_range(const NotaEngine* e, int32_t track_id, int32_t clip_index,
+ double beat0, double beat1, float* out_min_max, int32_t max_points) {
+ if (!e) return 0;
+ return CENG(e)->getClipWarpPeaksRange(track_id, clip_index, beat0, beat1, out_min_max, max_points);
+}
+int32_t nota_clip_get_source_peaks_range(const NotaEngine* e, int32_t track_id, int32_t clip_index,
+ double frame0, double frame1, float* out_min_max, int32_t max_points) {
+ if (!e) return 0;
+ return CENG(e)->getClipSourcePeaksRange(track_id, clip_index, frame0, frame1, out_min_max, max_points);
+}
// ---- Instrument tracks, MIDI clips & notes (M2) ---------------------------
@@ -386,6 +396,9 @@ void nota_engine_set_defer_warp_build(NotaEngine* e, int32_t defer) {
int64_t nota_engine_warp_build_step(NotaEngine* e, int32_t max_frames) {
return e ? ENG(e)->warpBuildStep(max_frames) : 0;
}
+int64_t nota_engine_warp_build_poll(NotaEngine* e) {
+ return e ? ENG(e)->warpBuildPoll() : 0;
+}
int64_t nota_engine_warp_build_remaining(const NotaEngine* e) {
return e ? CENG(e)->warpBuildRemainingFrames() : 0;
}
diff --git a/tests/Nota.SmokeTest/Program.cs b/tests/Nota.SmokeTest/Program.cs
index 21176248..06fcb254 100644
--- a/tests/Nota.SmokeTest/Program.cs
+++ b/tests/Nota.SmokeTest/Program.cs
@@ -14348,6 +14348,73 @@ Console.WriteLine("-- background audio import --");
double rd = 0; for (int i = 0; i < ra.Length; i++) rd = Math.Max(rd, Math.Abs(ra[i] - rb[i]));
Check(Rms(ra, 8192) > 0.001f && rd < 1e-5, $"import: plays identically to the synchronous load (max diff {rd:E1})");
+ // Zoomed clip-editor peaks: the full warp range matches the full-material query, and a
+ // source sub-range matches a brute-force scan of the samples.
+ {
+ ie.TryGetAudioClipInfo(ct, cc, out var pi);
+ var full = new float[256 * 2]; var rng = new float[256 * 2];
+ int fn = ie.GetClipWarpFullPeaks(ct, cc, full, 256);
+ int rn = ie.GetClipWarpPeaksRange(ct, cc, 0, pi.WarpBeats, rng, 256);
+ Check(fn == 256 && rn == 256 && full.SequenceEqual(rng), "range peaks: whole warp range == full-material peaks");
+ var raw = ie.ReadSample(pi.SampleId);
+ ie.TryGetSampleInfo(pi.SampleId, out var psi);
+ int chn = psi.Channels;
+ float Mid(long f) => chn == 1 ? raw[f] : 0.5f * (raw[f * chn] + raw[f * chn + 1]);
+ var sub = new float[64 * 2];
+ int sn = ie.GetClipSourcePeaksRange(ct, cc, 1000, 1000 + 64 * 64, sub, 64);
+ double err = 0;
+ for (int b = 0; b < sn; b++)
+ {
+ float mn = 1, mx = -1;
+ for (long f = 1000 + b * 64; f < 1000 + (b + 1) * 64; f++) { float m = Mid(f); mn = Math.Min(mn, m); mx = Math.Max(mx, m); }
+ err = Math.Max(err, Math.Max(Math.Abs(mn - sub[b * 2]), Math.Abs(mx - sub[b * 2 + 1])));
+ }
+ Check(sn == 64 && err < 1e-6, $"range peaks: source sub-range matches a full scan (max err {err:E1})");
+ var tiny = new float[100 * 2];
+ Check(ie.GetClipSourcePeaksRange(ct, cc, 5000, 5010, tiny, 100) == 100, "range peaks: sub-frame buckets read single frames");
+ }
+
+ // Background warp build: a deferred marker edit keeps the old cache playing (not
+ // silent), then warp_build_poll installs — off the calling thread — a cache that plays
+ // exactly like the synchronous build of the same edit.
+ {
+ var ms = new double[64]; var mb = new double[64];
+ int mk = ie.GetClipWarpMarkers(ct, cc, ms, mb);
+ var src2 = ms[..mk]; var beats2 = mb[..mk].Select(b => b * 1.25).ToArray();
+ ie.SetDeferWarpBuild(true);
+ ie.SetClipWarpMarkers(ct, cc, src2, beats2);
+ ie.SetDeferWarpBuild(false);
+ Check(ie.WarpBuildRemaining > 0, "bg warp: the deferred edit leaves a stale cache to rebuild");
+ ie.Stop(); ie.Seek(0); ie.Play();
+ var stale = new float[8192 * 2];
+ ie.RenderOffline(stale, 8192);
+ Check(Rms(stale, 8192) > 0.001f, "bg warp: the clip keeps playing its old cache meanwhile");
+ long guard3 = 0;
+ var bgw = System.Diagnostics.Stopwatch.StartNew();
+ while (ie.WarpBuildPoll() > 0 && ++guard3 < 100000) Thread.Sleep(1);
+ Check(ie.WarpBuildRemaining == 0, $"bg warp: worker build installed ({bgw.ElapsedMilliseconds} ms, {guard3} polls)");
+ se.SetClipWarpMarkers(st2, sc2, src2, beats2); // synchronous reference
+ ie.Stop(); ie.Seek(0); ie.Play(); se.Stop(); se.Seek(0); se.Play();
+ var ba = new float[8192 * 2]; var bb = new float[8192 * 2];
+ ie.RenderOffline(ba, 8192); se.RenderOffline(bb, 8192);
+ double bd = 0; for (int i = 0; i < ba.Length; i++) bd = Math.Max(bd, Math.Abs(ba[i] - bb[i]));
+ Check(Rms(ba, 8192) > 0.001f && bd < 1e-5, $"bg warp: plays identically to the synchronous rebuild (max diff {bd:E1})");
+ // An edit while a build runs supersedes it: the stale result is dropped, not installed.
+ ie.SetDeferWarpBuild(true);
+ ie.SetClipWarpMarkers(ct, cc, src2, mb[..mk]);
+ ie.SetDeferWarpBuild(false);
+ ie.WarpBuildPoll(); // starts the build for the original markers…
+ ie.SetDeferWarpBuild(true);
+ ie.SetClipWarpMarkers(ct, cc, src2, beats2); // …then the user edits again
+ ie.SetDeferWarpBuild(false);
+ guard3 = 0;
+ while (ie.WarpBuildPoll() > 0 && ++guard3 < 100000) Thread.Sleep(1);
+ ie.Stop(); ie.Seek(0); ie.Play();
+ ie.RenderOffline(ba, 8192);
+ bd = 0; for (int i = 0; i < ba.Length; i++) bd = Math.Max(bd, Math.Abs(ba[i] - bb[i]));
+ Check(ie.WarpBuildRemaining == 0 && bd < 1e-5, $"bg warp: a superseded build is dropped, the latest edit wins (max diff {bd:E1})");
+ }
+
// Analysis cache: content fingerprint + round trip in a project's analysis/ folder.
string? k1 = Nota.Infrastructure.AudioAnalysisCache.Fingerprint(clicks);
string copy = Path.Combine(dir, "renamed copy.wav");