Nota — a free DAW for macOS, Windows and Linux. No subscription, no account — just open it and make music.
C# audioaudio-unitavaloniacpp20cross-platformdawdigital-audio-workstationdspjucelinux-audiomcp-servermidimusicmusic-productionpiano-rollplugin-hostsequencersynthesizervstvst3
(fix) Export and freeze no longer start audio that was not running
Egor Khindikaynene88398983c54
diff --git a/.specify/bugs/export-restart-headless/assessment.md b/.specify/bugs/export-restart-headless/assessment.mdnew file mode 100644index 00000000..d02a534d--- /dev/null+++ b/.specify/bugs/export-restart-headless/assessment.md@@ -0,0 +1,140 @@+# Bug Assessment: Export and freeze restart audio that was never running++- **Slug**: export-restart-headless+- **Created**: 2026-10-14+- **Source**: pasted text; CI run https://github.com/nota-daw/nota/actions/runs/38546277984 (host: github.com — allowlisted; the run's failed-job log was read with `gh run view --log-failed`)+- **Verdict**: valid+- **Severity**: medium++## Report (verbatim or summarized)++> Export (and track freeze) crash when no audio device is available. After an offline+> render, AudioExporter.Restore and TrackFreezer.Restore unconditionally call+> engine.Start() to "resume live audio", even when the audio backend was not running+> before the export. On a machine without an audio output device (GitHub Actions Linux and+> Windows runners) Start() throws NotaEngineException: AudioDevice and the smoke test+> aborts. Expected: export/freeze restores the backend to the state it was in before+> (restart only if it was running), and never throws from Restore on a headless machine.++CI log, Linux and Windows jobs (identical):++```+Unhandled exception. Nota.Application.NotaEngineException: AudioDevice+ at Nota.Infrastructure.NotaEngine.Check(NotaResult result) in .../Interop/NotaEngine.cs:line 244+ at Nota.Infrastructure.NotaEngine.Start() in .../Interop/NotaEngine.cs:line 43+ at Nota.Infrastructure.AudioExporter.Restore(IAudioEngine engine, ExportRequest r) in .../Audio/AudioExporter.cs:line 339+ at Nota.Infrastructure.AudioExporter.ExportMaster(IAudioEngine engine, ExportRequest r, IProgress`1 progress) in .../Audio/AudioExporter.cs:line 53+ at Program.<Main>$(String[] args) in .../tests/Nota.SmokeTest/Program.cs:line 8460+```++Preceded on Linux by ALSA `cannot find card '0'` / `Unknown PCM default`. The macOS job does+not hit it: the runner has a virtual CoreAudio output, so the restart succeeds.++## Symptom++Exporting (master or stems) or freezing/bouncing a track on an engine whose audio backend is+not running ends by *starting* the backend. With no usable output device that start fails+and `Restore` throws after the file was already written; the export reports failure (app) or+the process dies (smoke test, MCP). Expected: the backend ends in the state it was in before+the operation, and a missing device never turns a successful offline render into an error.++## Reproduction++1. On a machine with no audio output device (e.g. `ubuntu-latest` GitHub runner, no ALSA card),+ build and run `scripts/build-linux.sh`.+2. The smoke test reaches the export section (`exporter.ExportMaster(ne, req)`,+ `tests/Nota.SmokeTest/Program.cs:8486` in the current tree); `ne` was never `Start()`ed.+3. `ExportMaster` renders the file, then `Restore` → `engine.Start()` → `NOTA_ERR_AUDIO_DEVICE`+ → unhandled `NotaEngineException`, exit code 134 (Linux) / 0xE0434352 (Windows).++In the app the same path is reachable when the output device disappears while Nota runs+(unplugged USB interface, backend stopped) and the user then exports or freezes —+[NEEDS CLARIFICATION: not reproduced in the app; the app currently needs a device to start+at all, `MainWindowViewModel` calls `Engine.Start()` unguarded].++## Suspected Code Paths++- `src/managed/Nota.Infrastructure/Audio/AudioExporter.cs:333` `Restore()` — calls `engine.Start()`+ unconditionally; reached from `ExportMaster` (:53) and `ExportStems` (:103).+- `src/managed/Nota.Infrastructure/Audio/AudioExporter.cs:26`, `:72` — `engine.Stop()` before+ rendering; the backend state at this point is not recorded.+- `src/managed/Nota.Infrastructure/Audio/TrackFreezer.cs:120` `Restore()` — same unconditional+ `engine.Start()`; reached from `Freeze` (:31, :59, :63) and `BounceRange` (:85, :111, :115),+ stops at :25 and :79.+- `src/native/nota.engine/src/nota_engine_core.cpp:32` `nota_engine_start` — maps a failed+ backend start to `NOTA_ERR_AUDIO_DEVICE` (correct; the bug is in the caller).+- `src/native/nota.engine/include/nota/nota_engine.h` — no way to ask whether the backend is+ running. `nota_engine_sample_rate` is not a substitute: `CoreAudioBackend` and+ `MiniaudioBackend` keep `sampleRate_` after `stop()`, so it stays non-zero.+- `src/native/nota.engine/src/AudioBackend.h:67` `isRunning()` — the state exists natively,+ it is just not exposed.++## Root Cause Hypothesis++`AudioExporter` and `TrackFreezer` assume the backend is always running when an offline+render begins, so "restore" is implemented as "start". That holds in the normal app session+but not for an engine that was never started (smoke test, a future headless/CLI export) or+whose device went away. Because the managed layer cannot query the running state through+the C ABI, it cannot remember and restore it. Confidence: high — the stack trace points at+the exact call, and the code path is unconditional.++## Proposed Remediation++**Preferred**: Expose the running state and restore it faithfully.++1. C ABI: add `int32_t nota_engine_is_running(const NotaEngine*)` to `nota_engine.h`+ (returns `backend_->isRunning()`; 0 for a null engine), implemented in+ `nota_engine_core.cpp` via a new `Engine::isRunning() const`.+2. Managed: `NativeMethods.IsRunning`, `IAudioEngine.IsRunning { get; }`,+ `NotaEngine.IsRunning`.+3. `AudioExporter.ExportMaster/ExportStems` and `TrackFreezer.Freeze/BounceRange`: read+ `bool wasRunning = engine.IsRunning` before `engine.Stop()`, pass it to `Restore`, and call+ `engine.Start()` only when it was running.++A device that vanished *during* the render (was running, start now fails) still throws —+that is a real device error, and the callers in `MainWindow.Files.cs` / `MainWindow.Freeze.cs`+already surface export failures; this fix does not change that path.++**Alternatives**:+- Catch `NotaEngineException` in `Restore` and swallow it. Smallest change, no ABI addition,+ but it still starts audio on an engine that was deliberately idle (the macOS smoke run+ currently opens the real device mid-test) and hides genuine device loss after a render.+- Infer the state from `SampleRate > 0`. Rejected: backends keep the last rate after `stop()`.++**Files likely to change**:+- `src/native/nota.engine/include/nota/nota_engine.h`+- `src/native/nota.engine/src/nota_engine_core.cpp`+- `src/native/nota.engine/src/Engine.h`+- `src/managed/Nota.Application/Engine/IAudioEngine.cs`+- `src/managed/Nota.Infrastructure/Interop/NativeMethods.cs`+- `src/managed/Nota.Infrastructure/Interop/NotaEngine.cs`+- `src/managed/Nota.Infrastructure/Audio/AudioExporter.cs`+- `src/managed/Nota.Infrastructure/Audio/TrackFreezer.cs`+- `tests/Nota.SmokeTest/Program.cs`++**Tests to add or update**:+- Smoke: a fresh engine reports `IsRunning == false`; after `ExportMaster`, `ExportStems`,+ `TrackFreezer.Freeze` and `BounceRange` on that engine it is still `false` and no exception+ is thrown (this is what fails on the headless CI runners today).+- Smoke (device present only, behind the existing opt-in `--audiocheck`): a started engine is+ running again after an export.++## Risks & Considerations++- Additive C ABI change: the constitution requires new API surface to be reflected in+ `nota_engine.h` and the managed bindings together; the engine version bumps with the next+ release as usual (minor/patch per the release rules).+- Several files in the fix (`nota_engine.h`, `nota_engine_core.cpp`, `Engine.h`,+ `IAudioEngine.cs`, `NativeMethods.cs`, `NotaEngine.cs`, `Program.cs`) carry uncommitted+ pan-law / stereo-map work in the current working tree; the fix must land on its own branch+ from `origin/develop` so the two do not mix.+- `isRunning()` is read on the UI/worker thread while the audio thread may be running; the+ backends' `running_` is a plain `bool` written only by start/stop on the control thread, so+ the read is safe from the same thread that calls `Stop()` (the exporter's worker thread+ calls both).++## Open Questions++- [NEEDS CLARIFICATION: should the app itself tolerate starting with no output device (offline+ mode) — out of scope for this fix, but the same unguarded `Start()` sits in+ `MainWindowViewModel`.]diff --git a/.specify/bugs/export-restart-headless/fix.md b/.specify/bugs/export-restart-headless/fix.mdnew file mode 100644index 00000000..b7180c25--- /dev/null+++ b/.specify/bugs/export-restart-headless/fix.md@@ -0,0 +1,75 @@+# Bug Fix: Export and freeze restart audio that was never running++- **Slug**: export-restart-headless+- **Fixed**: 2026-10-14+- **Assessment**: ./assessment.md+- **Status**: applied++## Summary++The engine now reports whether its audio backend is running (`nota_engine_is_running` →+`IAudioEngine.IsRunning`). `AudioExporter` and `TrackFreezer` read it before stopping the+backend for an offline render and restart it afterwards only if it was running, so an engine+that was never started — headless CI runners, a machine without an output device — stays+stopped and the export no longer throws.++## Changes++| File | Change | Notes |+|------|--------|-------|+| `src/native/nota.engine/include/nota/nota_engine.h` | modified | new `nota_engine_is_running` (additive ABI) |+| `src/native/nota.engine/src/Engine.h` | modified | `Engine::isRunning()` → `backend_->isRunning()` |+| `src/native/nota.engine/src/nota_engine_core.cpp` | modified | C entry point; 0 for a null engine |+| `src/managed/Nota.Infrastructure/Interop/NativeMethods.cs` | modified | `IsRunning` import |+| `src/managed/Nota.Application/Engine/IAudioEngine.cs` | modified | `bool IsRunning { get; }` |+| `src/managed/Nota.Infrastructure/Interop/NotaEngine.cs` | modified | `IsRunning` property |+| `src/managed/Nota.Infrastructure/Audio/AudioExporter.cs` | modified | `ExportMaster` / `ExportStems` remember the state; `Restore` starts only if it was running |+| `src/managed/Nota.Infrastructure/Audio/TrackFreezer.cs` | modified | same for `Freeze` / `BounceRange` (6 `Restore` call sites) |+| `tests/Nota.SmokeTest/Program.cs` | added tests | four headless checks; stale comment on the normalize test corrected |++## Diff Highlights++```csharp+bool wasRunning = engine.IsRunning; // restart afterwards only what was running+engine.Stop(); // halt the audio backend: no audio thread during render+...+if (wasRunning) engine.Start();+```++## Tests Added or Updated++- `Program.cs` "export leaves a backend that was never started stopped" — `ExportMaster` on a+ fresh engine (the call that crashed on the Linux/Windows runners).+- `Program.cs` "stem export leaves a backend that was never started stopped" — `ExportStems`.+- `Program.cs` "bounce leaves a backend that was never started stopped" — `TrackFreezer.BounceRange`.+- `Program.cs` "freeze leaves a backend that was never started stopped" — `TrackFreezer.Freeze`.++The device-present case (a running engine restarts after export) is exercised by the app+itself; the opt-in `--audiocheck` was not extended — see Deviations.++## Local Verification++- Commands run: `scripts/build.sh` on macOS (worktree on `fix/ci-green` from `origin/develop`)+ → native + managed build OK; smoke test: the four new checks PASS; one unrelated failure,+ `bg warp: plays identically to the synchronous rebuild (max diff 5.4E-001)`.+- Re-ran the smoke test twice without rebuilding → `SMOKE TEST PASSED` both times; the+ `bg warp` check is intermittent and does not touch export/freeze (see Follow-ups).+- Not verified locally: the headless Linux/Windows path itself (this Mac has an output+ device) — that is what the CI run on the pull request checks.++## Deviations from Assessment++- The assessment suggested also extending the opt-in `--audiocheck` with "a started engine is+ running again after an export". Not done: the check needs audio hardware and never runs in+ CI, and the restart path is the pre-existing behaviour, unchanged for a running engine.++## Follow-ups++- `bg warp: plays identically to the synchronous rebuild` failed once in three local runs —+ a likely race between the background warp worker and the comparison render; worth its own+ `/speckit-bug-assess`.+- The doc comments on `nota_engine_sample_rate` / `NotaEngine.SampleRate` say "0 if not+ started", but the backends keep the last rate after `stop()`.+- `MainWindowViewModel` still calls `Engine.Start()` unguarded, so the app cannot open+ without an output device (assessment, Open Questions).+- `CHANGELOG.md` `[Unreleased]`: add a Fixed entry when this lands.diff --git a/src/managed/Nota.Application/Engine/IAudioEngine.cs b/src/managed/Nota.Application/Engine/IAudioEngine.csindex f87748e3..63edf031 100644--- a/src/managed/Nota.Application/Engine/IAudioEngine.cs+++ b/src/managed/Nota.Application/Engine/IAudioEngine.cs@@ -15,6 +15,8 @@ public interface IAudioEngine : IDisposable{// --- Device / diagnostics ---------------------------------------------double SampleRate { get; }+ /// <summary>True while the output device is open and the audio thread runs.</summary>+ bool IsRunning { get; }void Start();void Stop();void SetTestTone(bool enabled);diff --git a/src/managed/Nota.Infrastructure/Audio/AudioExporter.cs b/src/managed/Nota.Infrastructure/Audio/AudioExporter.csindex 1cf08114..cf05e28b 100644--- a/src/managed/Nota.Infrastructure/Audio/AudioExporter.cs+++ b/src/managed/Nota.Infrastructure/Audio/AudioExporter.cs@@ -23,6 +23,7 @@ public sealed class AudioExporter : IAudioExporterint sr = r.SampleRate;long totalFrames = FramesFor(r);+ bool wasRunning = engine.IsRunning; // restart afterwards only what was runningengine.Stop(); // halt the audio backend: no audio thread during renderengine.SetLoop(false, 0, 0); // don't wrap; render the whole range onceengine.SetMetronome(false); // keep clicks out of the bounce@@ -50,7 +51,7 @@ public sealed class AudioExporter : IAudioExporter0.0, 1.0);}- Restore(engine, r);+ Restore(engine, r, wasRunning);progress?.Report(1.0);}@@ -69,6 +70,7 @@ public sealed class AudioExporter : IAudioExportervar savedMute = new bool[tracks.Count];for (int i = 0; i < tracks.Count; i++) savedMute[i] = tracks[i].Muted != 0;+ bool wasRunning = engine.IsRunning;engine.Stop();engine.SetLoop(false, 0, 0);engine.SetMetronome(false);@@ -100,7 +102,7 @@ public sealed class AudioExporter : IAudioExporter}for (int j = 0; j < count; j++) engine.SetTrackMute(tracks[j].Id, savedMute[j]); // restore mutes- Restore(engine, r);+ Restore(engine, r, wasRunning);progress?.Report(1.0);return written;}@@ -330,12 +332,14 @@ public sealed class AudioExporter : IAudioExporterprivate static long FramesFor(ExportRequest r)=> (long)System.Math.Ceiling(r.TotalBeats * (r.SampleRate * 60.0 / r.Bpm));- private static void Restore(IAudioEngine engine, ExportRequest r)+ private static void Restore(IAudioEngine engine, ExportRequest r, bool wasRunning){engine.StopTransport();engine.Seek(0);engine.SetLoop(r.RestoreLoop, 0, 16);engine.SetMetronome(r.RestoreMetronome);- engine.Start(); // resume live audio at the device sample rate+ // Resume live audio at the device sample rate — only if it was live: an engine that+ // was never started (headless, no output device) has nothing to resume.+ if (wasRunning) engine.Start();}}diff --git a/src/managed/Nota.Infrastructure/Audio/TrackFreezer.cs b/src/managed/Nota.Infrastructure/Audio/TrackFreezer.csindex 78a71b37..aa787366 100644--- a/src/managed/Nota.Infrastructure/Audio/TrackFreezer.cs+++ b/src/managed/Nota.Infrastructure/Audio/TrackFreezer.cs@@ -22,13 +22,14 @@ public sealed class TrackFreezerdouble restoreBeat = engine.PositionBeats;double loopStart = engine.LoopStart, loopEnd = engine.LoopEnd; // preserve the loop region+ bool wasRunning = engine.IsRunning; // restart afterwards only what was runningengine.Stop(); // halt the audio backend: no audio thread during captureengine.SetLoop(false, 0, 0); // capture the whole range once, no wrapengine.SetMetronome(false); // keep clicks out of the freezeengine.StopTransport();long total = engine.BeginFreeze(trackId, lengthBeats);- if (total <= 0) { Restore(engine, restoreLoop, restoreMetronome, loopStart, loopEnd, restoreBeat); return false; }+ if (total <= 0) { Restore(engine, restoreLoop, restoreMetronome, loopStart, loopEnd, restoreBeat, wasRunning); return false; }try{@@ -56,11 +57,11 @@ public sealed class TrackFreezercatch{engine.CancelFreeze();- Restore(engine, restoreLoop, restoreMetronome, loopStart, loopEnd, restoreBeat);+ Restore(engine, restoreLoop, restoreMetronome, loopStart, loopEnd, restoreBeat, wasRunning);throw;}- Restore(engine, restoreLoop, restoreMetronome, loopStart, loopEnd, restoreBeat);+ Restore(engine, restoreLoop, restoreMetronome, loopStart, loopEnd, restoreBeat, wasRunning);progress?.Report(1.0);return true;}@@ -76,13 +77,14 @@ public sealed class TrackFreezerdouble restoreBeat = engine.PositionBeats;double loopStart = engine.LoopStart, loopEnd = engine.LoopEnd;+ bool wasRunning = engine.IsRunning;engine.Stop();engine.SetLoop(false, 0, 0);engine.SetMetronome(false);engine.StopTransport();long total = engine.BeginBounce(trackId, endBeat - startBeat);- if (total <= 0) { Restore(engine, restoreLoop, restoreMetronome, loopStart, loopEnd, restoreBeat); return null; }+ if (total <= 0) { Restore(engine, restoreLoop, restoreMetronome, loopStart, loopEnd, restoreBeat, wasRunning); return null; }float[] pcm;try@@ -108,22 +110,22 @@ public sealed class TrackFreezercatch{engine.CancelFreeze();- Restore(engine, restoreLoop, restoreMetronome, loopStart, loopEnd, restoreBeat);+ Restore(engine, restoreLoop, restoreMetronome, loopStart, loopEnd, restoreBeat, wasRunning);throw;}- Restore(engine, restoreLoop, restoreMetronome, loopStart, loopEnd, restoreBeat);+ Restore(engine, restoreLoop, restoreMetronome, loopStart, loopEnd, restoreBeat, wasRunning);progress?.Report(1.0);return pcm;}private static void Restore(IAudioEngine engine, bool loop, bool metronome,- double loopStart, double loopEnd, double beat)+ double loopStart, double loopEnd, double beat, bool wasRunning){engine.StopTransport();engine.Seek(beat);engine.SetLoop(loop, loopStart, loopEnd);engine.SetMetronome(metronome);- engine.Start(); // resume live audio at the device sample rate+ if (wasRunning) engine.Start(); // resume live audio — only if it was live before}}diff --git a/src/managed/Nota.Infrastructure/Interop/NativeMethods.cs b/src/managed/Nota.Infrastructure/Interop/NativeMethods.csindex 4560b57f..98a4859c 100644--- a/src/managed/Nota.Infrastructure/Interop/NativeMethods.cs+++ b/src/managed/Nota.Infrastructure/Interop/NativeMethods.cs@@ -51,6 +51,9 @@ internal static partial class NativeMethods[LibraryImport(Lib, EntryPoint = "nota_engine_sample_rate")]internal static partial double SampleRate(IntPtr engine);+ [LibraryImport(Lib, EntryPoint = "nota_engine_is_running")]+ internal static partial int IsRunning(IntPtr engine);+[LibraryImport(Lib, EntryPoint = "nota_transport_play")]internal static partial NotaResult TransportPlay(IntPtr engine);diff --git a/src/managed/Nota.Infrastructure/Interop/NotaEngine.cs b/src/managed/Nota.Infrastructure/Interop/NotaEngine.csindex d49699b2..d133c598 100644--- a/src/managed/Nota.Infrastructure/Interop/NotaEngine.cs+++ b/src/managed/Nota.Infrastructure/Interop/NotaEngine.cs@@ -36,6 +36,16 @@ public sealed partial class NotaEngine : IAudioEngine}}+ /// <summary>True while the output device is open and the audio thread runs.</summary>+ public bool IsRunning+ {+ get+ {+ ThrowIfDisposed();+ return NativeMethods.IsRunning(_handle) != 0;+ }+ }+/// <summary>Open the default output device and start the audio thread.</summary>public void Start(){diff --git a/src/native/nota.engine/include/nota/nota_engine.h b/src/native/nota.engine/include/nota/nota_engine.hindex cfc539b6..a216021d 100644--- a/src/native/nota.engine/include/nota/nota_engine.h+++ b/src/native/nota.engine/include/nota/nota_engine.h@@ -222,6 +222,11 @@ NOTA_API NotaResult nota_engine_set_frequency(NotaEngine* engine, float hz);/* Current output sample rate (0 if not started). Informational. */NOTA_API double nota_engine_sample_rate(const NotaEngine* engine);+/* 1 while the output device is open and the audio thread runs (between a successful+ * nota_engine_start and nota_engine_stop), else 0. Offline work that stops the backend+ * reads it first so it restarts only what was running. */+NOTA_API int32_t nota_engine_is_running(const NotaEngine* engine);+/* ---- Audio preview / audition (M7-4a) ------------------------------------* Decode a file and mix it into the live output without a track, for browser* auditioning. One preview at a time; needs the backend running to be heard. */diff --git a/src/native/nota.engine/src/Engine.h b/src/native/nota.engine/src/Engine.hindex 5542d1df..de44c840 100644--- a/src/native/nota.engine/src/Engine.h+++ b/src/native/nota.engine/src/Engine.h@@ -80,6 +80,7 @@ public:bool start();void stop();double sampleRate() const { return backend_ ? backend_->sampleRate() : 0.0; }+ bool isRunning() const { return backend_ && backend_->isRunning(); }// --- audio device settings (M7-1) ---// The chosen device/rate/buffer live in config_ (persisted to audio.json)diff --git a/src/native/nota.engine/src/nota_engine_core.cpp b/src/native/nota.engine/src/nota_engine_core.cppindex 8c3b369c..f19af664 100644--- a/src/native/nota.engine/src/nota_engine_core.cpp+++ b/src/native/nota.engine/src/nota_engine_core.cpp@@ -58,6 +58,11 @@ double nota_engine_sample_rate(const NotaEngine* engine) {return reinterpret_cast<const Engine*>(engine)->sampleRate();}+int32_t nota_engine_is_running(const NotaEngine* engine) {+ if (!engine) return 0;+ return reinterpret_cast<const Engine*>(engine)->isRunning() ? 1 : 0;+}+// ---- Audio preview / audition (M7-4a) -------------------------------------NotaResult nota_engine_preview_file(NotaEngine* e, const char* path) {diff --git a/tests/Nota.SmokeTest/Program.cs b/tests/Nota.SmokeTest/Program.csindex 0927d091..b436e99a 100644--- a/tests/Nota.SmokeTest/Program.cs+++ b/tests/Nota.SmokeTest/Program.cs@@ -8446,8 +8446,8 @@ Check(engine.SessionAudioRecordSelfTest(), "session audio slot captures + loops}// -- export normalize (−1 dBTP true peak) via AudioExporter ----------------// Own engine: ExportMaster start()s the backend on Restore, so keep it off the-// shared `engine` (a later test asserts its backend was never started).+// Own engine, never started: the export must leave its backend stopped — restarting it+// threw on machines without an output device (.specify/bugs/export-restart-headless).{using var ne = new NotaEngine();ne.SetBpm(120); ne.SetTimeSignature(4, 4);@@ -8459,7 +8459,9 @@ Check(engine.SessionAudioRecordSelfTest(), "session audio slot captures + loopsstring nPath = Path.Combine(Path.GetTempPath(), "nota_normalize_test.wav");var req = new ExportRequest(nPath, 2.0, 120.0, 48000, WavBitDepth.Float32,RestoreLoop: false, RestoreMetronome: false, Normalize: true, Dither: false);+ bool neIdle = !ne.IsRunning;exporter.ExportMaster(ne, req);+ Check(neIdle && !ne.IsRunning, "export leaves a backend that was never started stopped");var nraw = File.ReadAllBytes(nPath);int ndata = BitConverter.ToInt32(nraw, 40);@@ -8637,6 +8639,7 @@ Check(engine.SessionAudioRecordSelfTest(), "session audio slot captures + loopsFormat: ExportFormat.Flac));var files = Directory.GetFiles(dir, "*.flac");Check(n == 2 && files.Length == 2, $"FLAC stems wrote one file per track ({n} reported, {files.Length} found)");+ Check(!se.IsRunning, "stem export leaves a backend that was never started stopped");var decoded = files.Select(f => NotaEngine.DecodeMono(f, 10.0, out _)).ToList();Check(decoded.All(d => d is not null && d.Length > 48000), "every FLAC stem decodes to full length");if (decoded.All(d => d is not null))@@ -8650,6 +8653,23 @@ Check(engine.SessionAudioRecordSelfTest(), "session audio slot captures + loopsfinally { try { if (Directory.Exists(dir)) Directory.Delete(dir, recursive: true); } catch { } }}+// -- freeze / bounce on an engine that was never started (export-restart-headless) --+// TrackFreezer stops the backend for the capture and used to start it unconditionally+// afterwards, which threw with no output device. It must restore what was there: nothing.+{+ using var fz = new NotaEngine();+ fz.SetBpm(120); fz.SetTimeSignature(4, 4);+ int fzTrk = fz.AddInstrumentTrack();+ int fzClip = fz.AddMidiClip(fzTrk, 0.0, 2.0);+ fz.SetClipNotes(fzTrk, fzClip, new[] { new NotaNote(60, 0.0, 2.0, 0.9f) });+ var freezer = new TrackFreezer();+ bool fzIdle = !fz.IsRunning;+ var bounced = freezer.BounceRange(fz, fzTrk, 0.0, 2.0, restoreLoop: false, restoreMetronome: false);+ Check(fzIdle && bounced is { Length: > 0 } && !fz.IsRunning, "bounce leaves a backend that was never started stopped");+ bool frozen = freezer.Freeze(fz, fzTrk, 2.0, restoreLoop: false, restoreMetronome: false);+ Check(frozen && !fz.IsRunning, "freeze leaves a backend that was never started stopped");+}+// -- M6-2: level meters (post-fader peak/RMS, master + per-track) --{engine.StopAllSession();