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) Disarming a track releases the notes held from live input (#368)
Chris Laupama68439425986e
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 1b80bc00..328607d4 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -192,6 +192,9 @@ Entry categories: `Added`, `Changed`, `Fixed`, `Removed`.at full detail instead of in blocks.### Fixed+- **Disarming a track no longer leaves a held note sounding** (nota-daw/nota#366): turning record arm off+ (or the arm moving to another selected track) while a key is down releases the notes that+ came in through live input, instead of leaving them on until panic or stop.- **Session clips no longer wait forever to start** (nota-daw/nota#343): a clip launched on amuted track, or one silenced by another track's solo, starts on the next launch point andkeeps time while silent; a transport loop with no bar line inside it now launches at thediff --git a/src/native/nota.engine/src/engine/Engine.cpp b/src/native/nota.engine/src/engine/Engine.cppindex 04acc62e..f4f8d662 100644--- a/src/native/nota.engine/src/engine/Engine.cpp+++ b/src/native/nota.engine/src/engine/Engine.cpp@@ -571,6 +571,7 @@ std::shared_ptr<Track> Engine::cloneTrack(const Track& t) const {nt->ccRoutesDirty = true; // Arp CC routes (specs/044): rebuilt when publishednt->nextModId = t.nextModId;nt->sessionPlayer = t.sessionPlayer; // shared — playback state persists (M5-2)+ nt->liveHeld = t.liveHeld; // shared — held live notes persist (#366)nt->setFrozen(t.frozen()); // M7: freeze state + buffer (shared by pointer)nt->frozenBuf = t.frozenBuf;nt->frozenSpb = t.frozenSpb;diff --git a/src/native/nota.engine/src/engine/Engine.h b/src/native/nota.engine/src/engine/Engine.hindex 867741fe..d52e8b4e 100644--- a/src/native/nota.engine/src/engine/Engine.h+++ b/src/native/nota.engine/src/engine/Engine.h@@ -1991,6 +1991,17 @@ class Engine {static constexpr int kMaxLiveExpr = 256;MidiEvent liveExpr_[kMaxLiveExpr];int liveExprCount_ = 0;+ // Tracks disarmed with keys still down (#366), from the live queue's release requests+ // (expr kLiveReleaseExpr); one past capacity waits for the next block. Marked on their+ // tracks once the block's graph is in hand.+ static constexpr int32_t kLiveReleaseExpr = -2;+ static constexpr int kMaxLiveRelease = 64;+ int32_t liveRelease_[kMaxLiveRelease] = {};+ int liveReleaseCount_ = 0;+ // Track t's share of the block's live notes, appended to evs: the untargeted ones and/or+ // the ones addressed to it, then the release of what it was sounding if it was disarmed+ // and no longer takes live input. Returns the new count.+ int liveNotesFor(const Track& t, bool untargeted, bool targeted, bool stillTakes, MidiEv* evs, int n);// Whether a live event reaches track t (armed / audition, or addressed to it).bool takesLiveEvent(const Track& t, const MidiEvent& e) const;// Apply the live expression that reaches `t` (and its MIDI source track) to `inst`.diff --git a/src/native/nota.engine/src/engine/Engine_Recording.cpp b/src/native/nota.engine/src/engine/Engine_Recording.cppindex ba551b5d..930e6a7a 100644--- a/src/native/nota.engine/src/engine/Engine_Recording.cpp+++ b/src/native/nota.engine/src/engine/Engine_Recording.cpp@@ -14,8 +14,17 @@ namespace nota {// --- live MIDI, arming, recording ------------------------------------------void Engine::setTrackArmed(int32_t trackId, bool armed) {- if (auto t = findTrackAuthoring(trackId)) t->setArmed(armed);+ auto t = findTrackAuthoring(trackId);+ const bool wasArmed = t && t->armed();+ if (t) t->setArmed(armed);if (armed) return;+ // A key still held from live input loses its note-off once the track stops taking live+ // notes (#366): the audio thread releases the live notes this track is sounding. The key+ // itself stays held, for other armed tracks and for a take started while it's down.+ if (wasArmed) {+ std::lock_guard<std::mutex> lk(remoteMidiMx_);+ remoteMidi_.push(MidiEvent{false, -1, 0.0f, trackId, kLiveReleaseExpr});+ }// Disarming a track that's currently recording finalizes its take (the audio clip is// materialised / MIDI capture stops), so toggling its REC button stops the recording.if (audioRecording_ && trackId == audioRecordTrackId_) stopAudioRecording();diff --git a/src/native/nota.engine/src/engine/Engine_Render.cpp b/src/native/nota.engine/src/engine/Engine_Render.cppindex 613607ea..c4d905bc 100644--- a/src/native/nota.engine/src/engine/Engine_Render.cpp+++ b/src/native/nota.engine/src/engine/Engine_Render.cpp@@ -14,6 +14,7 @@#include "media/WarpStream.h"#include <algorithm>+#include <bit>#include <chrono>#include <cmath>#include <cstdint>@@ -915,6 +916,7 @@ void Engine::publishHeldSnapshot(double nowBeat) {void Engine::drainLiveMidi(double blockStartBeat, bool playing) {liveCount_ = 0;liveExprCount_ = 0;+ liveReleaseCount_ = 0;const bool rec = recording_.load(std::memory_order_relaxed);// When recording into a Session slot, time notes against the slot's own loop@@ -971,6 +973,15 @@ void Engine::drainLiveMidi(double blockStartBeat, bool playing) {liveExpr_[liveExprCount_++] = e;continue;}+ if (e.expr == kLiveReleaseExpr) { // a track disarmed: release, the keys stay down+ if (liveReleaseCount_ >= kMaxLiveRelease) {+ liveCarry_ = e;+ liveCarried_ = true;+ break;+ }+ liveRelease_[liveReleaseCount_++] = e.trackId;+ continue;+ }const bool valid = e.pitch >= 0 && e.pitch < 128;const uint32_t bit = valid ? 1u << (e.pitch & 31) : 0u;if (liveCount_ >= kMaxLive || (valid && (seen[e.pitch >> 5] & bit))) {@@ -1172,9 +1183,35 @@ int Engine::gatherInstrumentNotes(// Live notes reach a track when it's armed, or when it's the audition target// (so clicking a rack/drum pad plays it without arming/recording).// A track-addressed event (Nota Remote) plays only its own track.+ const bool takesUntargeted = t.armed() || t.id() == auditionTrackId_.load(std::memory_order_relaxed);+ return liveNotesFor(t, takesUntargeted, true, takesUntargeted, evs, n);+}++int Engine::liveNotesFor(const Track& t, bool untargeted, bool targeted, bool stillTakes, MidiEv* evs, int n) {+ Track::LiveHeld* held = t.liveHeld.get();for (int i = 0; i < liveCount_ && n < 1024; ++i) {const MidiEvent& le = liveEvents_[i];- if (takesLiveEvent(t, le)) evs[n++] = {0, le.on, le.pitch, le.velocity};+ if (le.trackId >= 0 ? !targeted || le.trackId != t.id() : !untargeted) continue;+ evs[n++] = {0, le.on, le.pitch, le.velocity};+ if (!held || le.trackId >= 0 || le.pitch < 0 || le.pitch >= 128) continue;+ const uint64_t bit = 1ull << (le.pitch & 63);+ if (le.on) held->bits[le.pitch >> 6] |= bit;+ else held->bits[le.pitch >> 6] &= ~bit;+ }+ // Disarmed with keys down (#366): release the live notes it was given, unless it takes+ // live input again (re-armed, or the audition target).+ if (held && held->release) {+ held->release = false;+ if (!stillTakes) {+ for (int h = 0; h < 2; ++h) {+ uint64_t bits = held->bits[h];+ held->bits[h] = 0;+ while (bits && n < 1024) {+ evs[n++] = {0, false, h * 64 + std::countr_zero(bits), 0.0f};+ bits &= bits - 1;+ }+ }+ }}return n;}@@ -1447,10 +1484,8 @@ void Engine::renderSessionSlotRaw(Track& t, float* dst, int32_t frames, double s// Live monitoring while overdub-recording into this slot (M5-4).const bool monitoring = recordSlotPlayer_.load(std::memory_order_acquire) == &sp;- if (monitoring)- for (int i = 0; i < liveCount_ && n < 1024; ++i)- if (liveEvents_[i].trackId < 0 || liveEvents_[i].trackId == t.id()) // a phone plays only its own track- evs[n++] = {0, liveEvents_[i].on, liveEvents_[i].pitch, liveEvents_[i].velocity};+ // A phone plays only its own track.+ n = liveNotesFor(t, monitoring, monitoring, monitoring && t.armed(), evs, n);std::sort(evs, evs + n, midiEvLess);@@ -1750,6 +1785,11 @@ void Engine::processBlock(float* out, int32_t numFrames) {if (md) md->reset();}notesOffCount_ = 0;+ // Tracks disarmed with keys down (#366): they release on this render.+ if (g)+ for (int k = 0; k < liveReleaseCount_; ++k)+ for (auto& tptr : g->tracks)+ if (tptr->id() == liveRelease_[k] && tptr->liveHeld) tptr->liveHeld->release = true;// Note chase: when playback starts, the playhead jumps (seek) or the loop wraps, clip// notes that began before the playhead but are still held get re-triggered at the// jump point — otherwise a note you start in the middle of stays silent.diff --git a/src/native/nota.engine/src/engine/Track.h b/src/native/nota.engine/src/engine/Track.hindex ef0284f4..a2801620 100644--- a/src/native/nota.engine/src/engine/Track.h+++ b/src/native/nota.engine/src/engine/Track.h@@ -184,6 +184,16 @@ class Track {// isn't sent again every block. Audio thread only.bool sessionSilentReleased = false;+ // The untargeted live notes (keyboard, MIDI) the track was given and not yet the note-off+ // of, and whether it was disarmed with them down (#366): released on its next render.+ // Shared by every version of the track (edits, undo) and with no other track, so it+ // follows the track whatever happens to its instrument. Audio thread only.+ struct LiveHeld {+ uint64_t bits[2] = {0, 0};+ bool release = false;+ };+ std::shared_ptr<LiveHeld> liveHeld = std::make_shared<LiveHeld>();+// Metering (M6-2): post-fader block peak/RMS per channel. Written by the// audio thread each block, read by the UI. Transient — not copied across// snapshots (a freshly cloned track just re-populates on the next block).diff --git a/tests/Nota.SmokeTest/SessionInputTests.cs b/tests/Nota.SmokeTest/SessionInputTests.csindex 6c5523a8..5080b787 100644--- a/tests/Nota.SmokeTest/SessionInputTests.cs+++ b/tests/Nota.SmokeTest/SessionInputTests.cs@@ -6,7 +6,7 @@// in a Session slot replaces the clip its track was playing (#337), and an audio track can// take one channel of a stereo input as a mono source in the middle (#340), and a plain click// on an arm button arms that track alone, ⌘-click more than one, and selecting an instrument-// track arms it (#332).+// track arms it (#332), and disarming a track while a key is held releases its note (#366).using Nota.Application;using Nota.Infrastructure;@@ -27,6 +27,10 @@ internal static class SessionInputTestsforeach (var r in AudioTakeReplacesPlayingSlot()) yield return r;foreach (var r in MonoInput()) yield return r;foreach (var r in ExclusiveArm()) yield return r;+ foreach (var r in DisarmReleasesHeldNotes()) yield return r;+ foreach (var r in DisarmKeepsKeyForOtherTrack()) yield return r;+ foreach (var r in DisarmSparesClipNotes()) yield return r;+ foreach (var r in DisarmAfterEditAndInSession()) yield return r;}private static void Beats(NotaEngine e, double beats, float[]? keep = null)@@ -237,6 +241,182 @@ internal static class SessionInputTestsyield return (!TrackArm.FollowSelection(e, synth) && Armed(aud), "an armed audio track keeps its arm");}+ // A key held on an armed track, then the track disarmed (by its button, or by the arm moving+ // to another selected track) before the key comes up: disarming releases the note (#366).+ private static IEnumerable<(bool, string)> DisarmReleasesHeldNotes()+ {+ foreach (bool bySelection in new[] { false, true })+ {+ using var e = new NotaEngine();+ int keys = e.AddInstrumentTrack(), other = e.AddInstrumentTrack();+ e.SetTrackBuiltinInstrument(keys, 0);+ e.SetTrackBuiltinInstrument(other, 0);+ TrackArm.Set(e, keys, true, additive: false);+ e.NoteOn(60, 0.9f);+ float held = Peak(e, 0.5);+ if (bySelection) TrackArm.FollowSelection(e, other);+ else TrackArm.Set(e, keys, false, additive: false);+ Beats(e, 2.0); // past the release, the key still down+ float after = Peak(e, 0.5);+ e.NoteOff(60);+ float released = Peak(e, 0.5);+ string how = bySelection ? "the arm moving to another track" : "its arm button";+ yield return (held > 0.01f && after < 1e-4f && released < 1e-4f,+ $"disarming by {how} while a key is held releases the note (peak {held:F3} → {after:G2})");+ }+ }++ // A track edited while a key is held (its object replaced), a Session overdub, a routed+ // track whose source's instrument was replaced, and many tracks disarmed at once still+ // release the held note (#366).+ private static IEnumerable<(bool, string)> DisarmAfterEditAndInSession()+ {+ using (var e = new NotaEngine())+ {+ int keys = e.AddInstrumentTrack();+ e.SetTrackBuiltinInstrument(keys, 0);+ int clip = e.AddMidiClip(keys, 16.0, 4.0);+ TrackArm.Set(e, keys, true, additive: false);+ e.NoteOn(60, 0.9f);+ float held = Peak(e, 0.5);+ e.SetClipNotes(keys, clip, new[] { new NotaNote(67, 0.0, 1.0, 0.8f) });+ Beats(e, 0.5);+ TrackArm.Set(e, keys, false, additive: false);+ Beats(e, 2.0);+ float after = Peak(e, 0.5);+ e.NoteOff(60);+ yield return (held > 0.01f && after < 1e-4f,+ $"a track edited while a key is held still releases it on disarm (peak {held:F3} → {after:G2})");+ }+ using (var e = new NotaEngine())+ {+ e.SetBpm(120);+ e.SetMetronome(false);+ e.SetLaunchQuant(0.0);+ int keys = e.AddInstrumentTrack();+ e.SetTrackBuiltinInstrument(keys, 0);+ TrackArm.Set(e, keys, true, additive: false);+ e.Seek(0);+ Beats(e, 0.01);+ e.RecordSessionSlot(keys, 0);+ Beats(e, 1.0);+ e.NoteOn(64, 0.9f);+ float held = Peak(e, 0.5);+ TrackArm.Set(e, keys, false, additive: false);+ Beats(e, 2.0);+ float after = Peak(e, 0.5);+ e.NoteOff(64);+ Beats(e, 0.2);+ e.StopSessionRecord();+ e.StopAllSession();+ e.StopTransport();+ yield return (held > 0.01f && after < 1e-4f,+ $"disarming during a Session overdub releases the held note (peak {held:F3} → {after:G2})");+ }+ using (var e = new NotaEngine())+ {+ int src = e.AddInstrumentTrack(), dst = e.AddInstrumentTrack();+ e.SetTrackBuiltinInstrument(src, 0);+ e.SetTrackBuiltinInstrument(dst, 0);+ e.SetTrackMidiSource(dst, src);+ TrackArm.Set(e, src, true, additive: false);+ e.NoteOn(60, 0.9f);+ float held = Peak(e, 0.5);+ e.SetTrackBuiltinInstrument(src, 0); // the source's instrument replaced+ Beats(e, 0.5);+ TrackArm.Set(e, src, false, additive: false);+ Beats(e, 2.0);+ float after = Peak(e, 0.5);+ e.NoteOff(60);+ yield return (held > 0.01f && after < 1e-4f,+ $"a routed track still releases after its source's instrument is replaced (peak {held:F3} → {after:G2})");+ }+ using (var e = new NotaEngine())+ {+ var all = new List<int>();+ for (int k = 0; k < 70; k++)+ {+ int t = e.AddInstrumentTrack();+ e.SetTrackBuiltinInstrument(t, 0);+ TrackArm.Set(e, t, true, additive: k > 0);+ all.Add(t);+ }+ int last = e.AddInstrumentTrack();+ e.NoteOn(60, 0.5f);+ float held = Peak(e, 0.5);+ TrackArm.Set(e, last, true, additive: false); // disarms all seventy at once (past one block's 64 releases)+ Beats(e, 1.0);+ float after = Peak(e, 0.5);+ e.NoteOff(60);+ yield return (held > 0.01f && after < 1e-4f,+ $"seventy tracks disarmed at once all release the held note (peak {held:F3} → {after:G2})");+ }+ }++ // The key stays down for the other armed track: a take started there while it's still held+ // records it (#366).+ private static IEnumerable<(bool, string)> DisarmKeepsKeyForOtherTrack()+ {+ using var e = new NotaEngine();+ e.SetBpm(120);+ e.SetMetronome(false);+ e.SetCountIn(0);+ int a = e.AddInstrumentTrack(), b = e.AddInstrumentTrack();+ e.SetTrackBuiltinInstrument(a, 0);+ e.SetTrackBuiltinInstrument(b, 0);+ TrackArm.Set(e, a, true, additive: false);+ TrackArm.Set(e, b, true, additive: true);+ e.Play();+ Beats(e, 1.0);+ e.NoteOn(62, 0.8f);+ Beats(e, 0.5);+ TrackArm.Set(e, a, false, additive: true);+ Beats(e, 0.5);+ e.SetRecording(true); // the take goes to b, the key still down+ Beats(e, 1.0);+ e.NoteOff(62);+ Beats(e, 0.2);+ e.Poll();+ e.SetRecording(false);+ e.Poll();+ bool got = false;+ for (int c = 0; c < 8 && e.TryGetClipInfo(b, c, out _); c++)+ got |= e.GetClipNotes(b, c).Any(n => n.Pitch == 62);+ yield return (got, "disarming one track leaves the key held for a take started on another armed track");+ e.StopTransport();+ }++ // Disarming releases only the live notes that track was sounding: its clip's note on a+ // pitch another track holds from Nota Remote keeps playing (#366).+ private static IEnumerable<(bool, string)> DisarmSparesClipNotes()+ {+ using var e = new NotaEngine();+ e.SetBpm(120);+ e.SetMetronome(false);+ int a = e.AddInstrumentTrack(), b = e.AddInstrumentTrack();+ e.SetTrackBuiltinInstrument(a, 0);+ e.SetTrackBuiltinInstrument(b, 0);+ int clip = e.AddMidiClip(a, 0.0, 8.0);+ e.SetClipNotes(a, clip, new[] { new NotaNote(64, 0.0, 8.0, 0.8f) });+ TrackArm.Set(e, a, true, additive: false);+ e.Seek(0.0);+ e.Play();+ Beats(e, 0.5);+ e.TrackNoteOn(b, 64, 0.8f);+ Beats(e, 0.5);+ TrackArm.Set(e, a, false, additive: false);+ float peak = 0;+ var buf = new float[Chunk * 2];+ for (int k = 0; k < 64; k++)+ {+ e.RenderOffline(buf, Chunk, Sr);+ if (k >= 8 && e.TryGetTrackMeter(a, out var m)) peak = Math.Max(peak, m.Peak);+ }+ e.TrackNoteOff(b, 64);+ yield return (peak > 0.01f, $"disarming leaves the track's clip note sounding (peak {peak:F3})");+ e.StopTransport();+ }+private static int FirstAudio(NotaEngine e){for (int i = 0; i < e.TrackCount; i++)