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) Greptile review of #293: grouping, rack fork race, wheel stepping (#294)
Chris Laupama2dc0f7088f0d
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 24b99764..2a0a52c2 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -30,6 +30,12 @@ Entry categories: `Added`, `Changed`, `Fixed`, `Removed`.and the status line says why. ⌘G on tracks still groups tracks.### Fixed+- **⌘G on a device, follow-ups** (nota-daw/nota#257): grouping a playing effect no longer+ resets it (a Reverb kept ringing cut off), an effect an Arp's CC lane drives stays out of a+ rack instead of losing that link, and ⌘⇧G or undo gives the device back its A/B snapshots+ and preset label. Rack edits during playback no longer race the audio thread over the+ sustain pedal and pad decay state, and the wheel on one stepper box no longer carries+ travel left over from another.- **Copy, paste and duplicate keep every setting** (nota-daw/nota#238): a pasted device cameback with only part of its settings (a pasted Compressor at its default threshold and ratio).Copy now takes the whole device — every parameter, its state, bypass and sidechain — anddiff --git a/src/managed/Nota.App/Controls/RhythmViews.cs b/src/managed/Nota.App/Controls/RhythmViews.csindex d329fae5..e497ea70 100644--- a/src/managed/Nota.App/Controls/RhythmViews.cs+++ b/src/managed/Nota.App/Controls/RhythmViews.cs@@ -392,7 +392,7 @@ internal sealed class RhythmStepStrip : Controlprotected override void OnPointerWheelChanged(PointerWheelEventArgs e){int s = StepAt(e.GetPosition(this).X);- if (!On[s] || WheelInput.Step(e) is not { } dir) return;+ if (!On[s] || WheelInput.Step(e, this, s) is not { } dir) return;e.Handled = true;if (dir != 0) Put(s, true, (float)Math.Clamp(Vel[s] + dir * 0.05, 0.05, 1.0), Acc[s]);}diff --git a/src/managed/Nota.App/DeviceCards/Bodies/ValveDeviceBody.cs b/src/managed/Nota.App/DeviceCards/Bodies/ValveDeviceBody.csindex 20246e56..4e2d73dd 100644--- a/src/managed/Nota.App/DeviceCards/Bodies/ValveDeviceBody.cs+++ b/src/managed/Nota.App/DeviceCards/Bodies/ValveDeviceBody.cs@@ -249,9 +249,9 @@ internal sealed class ValveDeviceBody : IDeviceBodyfly.ShowAt(box);e.Handled = true;};- box.PointerWheelChanged += (_, e) =>+ box.PointerWheelChanged += (wt, e) =>{- if (WheelInput.Step(e) is not { } s) return;+ if (WheelInput.Step(e, wt) is not { } s) return;e.Handled = true; if (s == 0) return;int m = Math.Clamp(Idx(p) - s, 0, names.Length - 1);SetP(p, m); RefreshAll();diff --git a/src/managed/Nota.App/DeviceCards/Bodies/VintageDeviceBody.cs b/src/managed/Nota.App/DeviceCards/Bodies/VintageDeviceBody.csindex 7f2eb2da..f246cbf8 100644--- a/src/managed/Nota.App/DeviceCards/Bodies/VintageDeviceBody.cs+++ b/src/managed/Nota.App/DeviceCards/Bodies/VintageDeviceBody.cs@@ -237,9 +237,9 @@ internal sealed class VintageDeviceBody : IDeviceBodyfly.ShowAt(box);e.Handled = true;};- box.PointerWheelChanged += (_, e) =>+ box.PointerWheelChanged += (wt, e) =>{- if (WheelInput.Step(e) is not { } s) return;+ if (WheelInput.Step(e, wt) is not { } s) return;e.Handled = true; if (s == 0) return;int m = Math.Clamp(Sel(Mode, 6) - s, 0, 5);SetP(Mode, m / 5f); RefreshAll();diff --git a/src/managed/Nota.App/DeviceCards/Instruments/GrainInstrumentCard.cs b/src/managed/Nota.App/DeviceCards/Instruments/GrainInstrumentCard.csindex ef64c9bf..fa457af3 100644--- a/src/managed/Nota.App/DeviceCards/Instruments/GrainInstrumentCard.cs+++ b/src/managed/Nota.App/DeviceCards/Instruments/GrainInstrumentCard.cs@@ -310,7 +310,7 @@ internal sealed class GrainInstrumentCard : IInstrumentCardHeight = 17, MinWidth = 40, CornerRadius = NotaRadius.Badge, BorderThickness = new Thickness(1), BorderBrush = BorderDef,Background = NotaPalette.BgSunken, Child = text,};- box.PointerWheelChanged += (_, e) => { if (WheelInput.Step(e) is not { } s) return; e.Handled = true; if (s != 0) SetRoot(rootNote + s); };+ box.PointerWheelChanged += (wt, e) => { if (WheelInput.Step(e, wt) is not { } s) return; e.Handled = true; if (s != 0) SetRoot(rootNote + s); };// Double-click: the note the file name names, else C4.box.DoubleTapped += (_, e) => { SetRoot(SamplerModel.DetectRoot(sampleName) is var d and >= 0 ? d : 60); e.Handled = true; };ToolTip.SetTip(box, "The root — the key that plays the sample as recorded. Scroll to step it · double-click takes it from the file name (C4 if none)");diff --git a/src/managed/Nota.App/DeviceCards/Instruments/MosaicInstrumentCard.cs b/src/managed/Nota.App/DeviceCards/Instruments/MosaicInstrumentCard.csindex 985924ae..a2c54d92 100644--- a/src/managed/Nota.App/DeviceCards/Instruments/MosaicInstrumentCard.cs+++ b/src/managed/Nota.App/DeviceCards/Instruments/MosaicInstrumentCard.cs@@ -300,9 +300,9 @@ internal sealed class MosaicInstrumentCard : IInstrumentCard};b.PointerReleased += (_, e) => { if (!drag) return; drag = false; e.Pointer.Capture(null); done?.Invoke(); };b.PointerCaptureLost += (_, _) => { if (!drag) return; drag = false; done?.Invoke(); };- b.PointerWheelChanged += (_, e) =>+ b.PointerWheelChanged += (wt, e) =>{- if (WheelInput.Step(e) is not { } s) return;+ if (WheelInput.Step(e, wt) is not { } s) return;e.Handled = true; if (s == 0) return;step(s); tb.Text = text(); done?.Invoke(); Refresh();};diff --git a/src/managed/Nota.App/DeviceCards/Instruments/PendulumInstrumentCard.cs b/src/managed/Nota.App/DeviceCards/Instruments/PendulumInstrumentCard.csindex 25f8787a..0a848dc7 100644--- a/src/managed/Nota.App/DeviceCards/Instruments/PendulumInstrumentCard.cs+++ b/src/managed/Nota.App/DeviceCards/Instruments/PendulumInstrumentCard.cs@@ -474,9 +474,9 @@ internal sealed class PendulumInstrumentCard : IInstrumentCardfly.ShowAt(scaleBox);e.Handled = true;};- scaleBox.PointerWheelChanged += (_, e) =>+ scaleBox.PointerWheelChanged += (wt, e) =>{- if (WheelInput.Step(e) is not { } s) return;+ if (WheelInput.Step(e, wt) is not { } s) return;e.Handled = true; if (s == 0) return;int n = PendulumModel.ModeNames.Length;int m = Math.Clamp(Sel("scalemode", n) - s, 0, n - 1);diff --git a/src/managed/Nota.App/DeviceCards/Instruments/SamplerInstrumentCard.cs b/src/managed/Nota.App/DeviceCards/Instruments/SamplerInstrumentCard.csindex 662cbe42..347ea9dc 100644--- a/src/managed/Nota.App/DeviceCards/Instruments/SamplerInstrumentCard.cs+++ b/src/managed/Nota.App/DeviceCards/Instruments/SamplerInstrumentCard.cs@@ -294,9 +294,9 @@ internal sealed class SamplerInstrumentCard : IInstrumentCardHeight = 17, MinWidth = 40, CornerRadius = NotaRadius.Badge, BorderThickness = new Thickness(1), BorderBrush = BorderDef,Background = NotaPalette.BgSunken, Child = rootText,};- rootBox.PointerWheelChanged += (_, e) =>+ rootBox.PointerWheelChanged += (wt, e) =>{- if (WheelInput.Step(e) is not { } s) return;+ if (WheelInput.Step(e, wt) is not { } s) return;e.Handled = true; if (s == 0) return;rootv = Math.Clamp(rootv + s, 0, 127); acc.SetRoot(rootv); Refresh();};diff --git a/src/managed/Nota.App/DeviceCards/Midi/ChordMidiBody.cs b/src/managed/Nota.App/DeviceCards/Midi/ChordMidiBody.csindex a07a3a44..83fb10fe 100644--- a/src/managed/Nota.App/DeviceCards/Midi/ChordMidiBody.cs+++ b/src/managed/Nota.App/DeviceCards/Midi/ChordMidiBody.cs@@ -407,7 +407,7 @@ internal sealed class ChordMidiBody : IMidiDeviceBodyif (!ev.GetCurrentPoint(keyTb).Properties.IsLeftButtonPressed) return;StepKey(ev.KeyModifiers.HasFlag(KeyModifiers.Shift) ? -1 : 1); ev.Handled = true;};- keyTb.PointerWheelChanged += (_, ev) => { if (WheelInput.Step(ev) is not { } s) return; ev.Handled = true; if (s != 0) StepKey(s); };+ keyTb.PointerWheelChanged += (wt, ev) => { if (WheelInput.Step(ev, wt) is not { } s) return; ev.Handled = true; if (s != 0) StepKey(s); };modeTb.PointerPressed += (_, ev) =>{if (!ev.GetCurrentPoint(modeTb).Properties.IsLeftButtonPressed) return;diff --git a/src/managed/Nota.App/DeviceCards/Midi/ScaleMidiBody.cs b/src/managed/Nota.App/DeviceCards/Midi/ScaleMidiBody.csindex 9cb10499..f4b64c00 100644--- a/src/managed/Nota.App/DeviceCards/Midi/ScaleMidiBody.cs+++ b/src/managed/Nota.App/DeviceCards/Midi/ScaleMidiBody.cs@@ -351,7 +351,7 @@ internal sealed class ScaleMidiBody : IMidiDeviceBodyreadouts.Add(() => name.Text = Notes[Root()]);var row = new StackPanel { Orientation = Orientation.Horizontal, Spacing = 2, VerticalAlignment = VerticalAlignment.Center, Background = Brushes.Transparent,Children = { Arrow(GlyphKind.ChevronLeft, -1), name, Arrow(GlyphKind.ChevronRight, 1) } };- row.PointerWheelChanged += (_, ev) => { if (WheelInput.Step(ev) is not { } s) return; ev.Handled = true; if (s != 0) StepRoot(s); };+ row.PointerWheelChanged += (wt, ev) => { if (WheelInput.Step(ev, wt) is not { } s) return; ev.Handled = true; if (s != 0) StepRoot(s); };ToolTip.SetTip(row, "Root — click the arrows or scroll");Learn(row, PRoot, "Root");return row;@@ -393,9 +393,9 @@ internal sealed class ScaleMidiBody : IMidiDeviceBodyfly.ShowAt(box);ev.Handled = true;};- box.PointerWheelChanged += (_, ev) =>+ box.PointerWheelChanged += (wt, ev) =>{- if (WheelInput.Step(ev) is not { } s) return;+ if (WheelInput.Step(ev, wt) is not { } s) return;ev.Handled = true; if (s == 0) return;int at = Array.IndexOf(TypeRows, Sc()); if (at < 0) at = TypeRows.Length - 1;PickType(TypeRows[Math.Clamp(at - s, 0, TypeRows.Length - 1)]);@@ -433,7 +433,7 @@ internal sealed class ScaleMidiBody : IMidiDeviceBodyvoid Stop() { if (!drag) return; drag = false; End(p); }valHit.PointerReleased += (_, ev) => { if (drag) ev.Pointer.Capture(null); Stop(); };valHit.PointerCaptureLost += (_, _) => Stop();- valHit.PointerWheelChanged += (_, ev) => { if (WheelInput.Step(ev) is not { } s) return; ev.Handled = true; if (s != 0) { Begin(p); Put(Get() + s); End(p); } };+ valHit.PointerWheelChanged += (wt, ev) => { if (WheelInput.Step(ev, wt) is not { } s) return; ev.Handled = true; if (s != 0) { Begin(p); Put(Get() + s); End(p); } };readouts.Add(() => val.Text = DeviceCardKit.NoteName(Get()));var box = new Border{diff --git a/src/managed/Nota.App/DeviceCards/PluginCard.cs b/src/managed/Nota.App/DeviceCards/PluginCard.csindex 42554ace..3ca94bc7 100644--- a/src/managed/Nota.App/DeviceCards/PluginCard.cs+++ b/src/managed/Nota.App/DeviceCards/PluginCard.cs@@ -279,7 +279,7 @@ internal static class PluginCard// The page holds as many knobs as the section fits; wheel over it turns the page.var body = new Border { Background = Brushes.Transparent, Padding = new Thickness(0, 4, 0, 0), Child = host };- body.PointerWheelChanged += (_, ev) => { if (WheelInput.Step(ev) is not { } s) return; ev.Handled = true; if (s != 0) Step(-s); };+ body.PointerWheelChanged += (wt, ev) => { if (WheelInput.Step(ev, wt) is not { } s) return; ev.Handled = true; if (s != 0) Step(-s); };body.SizeChanged += (_, ev) =>{int cols = Math.Max(1, (int)(ev.NewSize.Width / CellW));diff --git a/src/managed/Nota.App/DeviceChainView.Clipboard.cs b/src/managed/Nota.App/DeviceChainView.Clipboard.csindex b80005b9..60a47aa9 100644--- a/src/managed/Nota.App/DeviceChainView.Clipboard.cs+++ b/src/managed/Nota.App/DeviceChainView.Clipboard.cs@@ -115,10 +115,13 @@ public sealed partial class DeviceChainViewstring name = _engine.DeviceName(_trackId, di);if (_engine.WrapDeviceInRack(_trackId, di) < 0){- StatusMessage?.Invoke($"{name} can't go in a rack: it has a sidechain key, automation or a modulation link");+ StatusMessage?.Invoke($"{name} can't go in a rack: it has a sidechain key, automation, a modulation link or an Arp's CC lane");return;}- TrackExtras().Remove(ExtraKey(ChainKind.Effect, di)); // its preset label stays with the device, now in the chain+ // The device's A/B snapshots and preset label live only here: the rack keeps them for+ // when ⌘⇧G (or undo) brings the device back out.+ TrackExtras().Remove(ExtraKey(ChainKind.Effect, di), out var own);+ if (own != null) Extra(ChainKind.Effect, di).Wrapped = own;StatusMessage?.Invoke($"Put {name} in an Audio Effect Rack");Rebuild(); Changed?.Invoke();}@@ -128,10 +131,11 @@ public sealed partial class DeviceChainViewif (!SelectedIsRack) { StatusMessage?.Invoke("Select an Audio Effect Rack to take its devices out"); return; }int di = _selDeviceIndex;int end = _engine.TrackDeviceCount(_trackId);+ var wrapped = Extra(ChainKind.Effect, di).Wrapped;int n = _engine.UnwrapRack(_trackId, di);if (n < 0){- StatusMessage?.Invoke("Only a rack with one chain can be ungrouped, and not while it is automated or modulated");+ StatusMessage?.Invoke("Only a rack with one chain can be ungrouped, and not while it is automated, modulated or driven by an Arp's CC lane");return;}// The rack's slot becomes its n devices; the cards after it shift with them.@@ -140,6 +144,8 @@ public sealed partial class DeviceChainView{TrackExtras().Remove(ExtraKey(ChainKind.Effect, di));for (int i = 1; i < n; i++) ExtrasMoved(ChainKind.Effect, end - 1 + i, di + i);+ if (wrapped != null && wrapped.Sig == CardSig(_trackId, ChainKind.Effect, di))+ TrackExtras()[ExtraKey(ChainKind.Effect, di)] = wrapped;}_selDeviceIndex = n > 0 ? di : -1;StatusMessage?.Invoke(n == 1 ? "Took the device out of the rack" : $"Took {n} devices out of the rack");diff --git a/src/managed/Nota.App/DeviceChainView.Shell.cs b/src/managed/Nota.App/DeviceChainView.Shell.csindex 45306b5a..dab42e1b 100644--- a/src/managed/Nota.App/DeviceChainView.Shell.cs+++ b/src/managed/Nota.App/DeviceChainView.Shell.cs@@ -38,7 +38,8 @@ public sealed partial class DeviceChainView// back restores them, and keyed so a MIDI effect and an audio effect at the same index// don't collide. Sig ties an entry to the device it was made for: when that slot now// holds a different device (swapped, removed behind our back, undo), the card starts fresh.- private sealed class CardExtra { public string Sig = ""; public float[]? A, B; public int Active; public string Preset = "", PresetId = ""; public PluginCard.ViewState Plugin = new(); }+ private sealed class CardExtra { public string Sig = ""; public float[]? A, B; public int Active; public string Preset = "", PresetId = ""; public PluginCard.ViewState Plugin = new();+ public CardExtra? Wrapped; } // a rack's: the extra of the device ⌘G put in it, back when it comes outprivate readonly System.Collections.Generic.Dictionary<int, System.Collections.Generic.Dictionary<int, CardExtra>> _cardExtra = new(); // trackId → key → extraprivate static int ExtraKey(ChainKind k, int di) => k switch { ChainKind.Instrument => -1, ChainKind.Midi => -1000 - di, _ => di };private static ChainKind KeyKind(int key) => key >= 0 ? ChainKind.Effect : key == -1 ? ChainKind.Instrument : ChainKind.Midi;@@ -58,7 +59,15 @@ public sealed partial class DeviceChainViewprivate CardExtra ExtraFor(int t, ChainKind k, int di){var d = TrackExtras(t); int key = ExtraKey(k, di); string sig = CardSig(t, k, di);- if (!d.TryGetValue(key, out var e) || e.Sig != sig) { e = new CardExtra { Sig = sig }; d[key] = e; }+ if (!d.TryGetValue(key, out var e) || e.Sig != sig)+ {+ // Undo / redo of ⌘G swap the device and its rack in one slot: each keeps the other's+ // extra, so the A/B snapshots and preset label survive any number of swaps.+ var prev = e;+ e = prev?.Wrapped is { } w && w.Sig == sig ? w : new CardExtra { Sig = sig };+ if (prev != null) { prev.Wrapped = null; e.Wrapped = prev; }+ d[key] = e;+ }return e;}private CardExtra Extra(ChainKind k, int di) => ExtraFor(_trackId, k, di);diff --git a/src/managed/Nota.App/WheelInput.cs b/src/managed/Nota.App/WheelInput.csindex cb912aa6..8c0e20a0 100644--- a/src/managed/Nota.App/WheelInput.cs+++ b/src/managed/Nota.App/WheelInput.cs@@ -51,17 +51,24 @@ internal static class WheelInputprivate static double _stepAcc;private static ulong _stepAt;+ private static readonly WeakReference<object?> _stepTarget = new(null); // weak: a removed card isn't kept alive+ private static int _stepPart;/// <summary>One step of a stepper under the wheel: +1 up, −1 down, 0 for a trackpad event/// that hasn't travelled a full notch yet; null when the wheel is mostly sideways (leave the/// event unhandled). A mouse notch is one step; a trackpad steps once per notch of travel- /// instead of once per event, which ran a box to its end on a single swipe.</summary>- public static int? Step(PointerWheelEventArgs e)+ /// instead of once per event, which ran a box to its end on a single swipe. <paramref name="target"/>+ /// (with <paramref name="part"/>, for a control holding several) names the value being stepped:+ /// travel left over on one value never carries into the next.</summary>+ public static int? Step(PointerWheelEventArgs e, object target, int part = 0){if (!IsVertical(e)) return null;double n = Notches(e.Delta.Y);- if (e.Timestamp - _stepAt > 300 || Math.Sign(n) != Math.Sign(_stepAcc)) _stepAcc = 0;+ bool same = _stepTarget.TryGetTarget(out var last) && ReferenceEquals(last, target) && part == _stepPart;+ if (e.Timestamp - _stepAt > 300 || Math.Sign(n) != Math.Sign(_stepAcc) || !same) _stepAcc = 0;_stepAt = e.Timestamp;+ _stepTarget.SetTarget(target);+ _stepPart = part;_stepAcc += n;if (Math.Abs(_stepAcc) < 0.999) return 0;int s = Math.Sign(_stepAcc);diff --git a/src/native/nota.engine/src/devices/Instrument.h b/src/native/nota.engine/src/devices/Instrument.hindex bf232651..fba6b077 100644--- a/src/native/nota.engine/src/devices/Instrument.h+++ b/src/native/nota.engine/src/devices/Instrument.h@@ -66,9 +66,10 @@ class Instrument {// playEvents / deliverExpr). An instrument that models the pedal itself (Keys, Mosaic, a// hosted plug-in) or ignores it on purpose (drums) returns true and is left alone.virtual bool handlesSustain() const { return false; }- // The engine's damper state for this instrument. Audio thread only.- bool pedalDown = false;- uint64_t pedalHeld[2] = {0, 0}; // released keys the pedal is holding, one bit per pitch+ // The engine's damper state for this instrument. Written on the audio thread only; relaxed+ // atomics because a rack edit forks the rack on the message thread and carries them over.+ std::atomic<bool> pedalDown{false};+ std::atomic<uint64_t> pedalHeld[2] = {0, 0}; // released keys the pedal is holding, one bit per pitch// Add `frames` of interleaved stereo into `out`. Continues current voices.virtual void render(float* out, int32_t frames) = 0;@@ -80,7 +81,7 @@ class Instrument {// fade-out has run.bool bypassed() const { return bypassed_.load(std::memory_order_relaxed); }void setBypassed(bool b) { bypassed_.store(b, std::memory_order_relaxed); }- bool offSettled = false;+ std::atomic<bool> offSettled{false}; // relaxed, like pedalDown// Musical transport for generative instruments (e.g. Nota Pendulum): the engine// calls this once per block before render() with the block's start beat,diff --git a/src/native/nota.engine/src/devices/racks/DrumRack.cpp b/src/native/nota.engine/src/devices/racks/DrumRack.cppindex 831b7390..e97809ec 100644--- a/src/native/nota.engine/src/devices/racks/DrumRack.cpp+++ b/src/native/nota.engine/src/devices/racks/DrumRack.cpp@@ -77,10 +77,10 @@ void DrumRack::render(float* out, int32_t frames) {const float dec = c.ctl->decay.load(std::memory_order_relaxed);const bool shaped = dec < 0.999f;if (c.ctl->retrig) {- c.ctl->ampEnv = 1.0f;+ c.ctl->ampEnv.store(1.0f, std::memory_order_relaxed);c.ctl->retrig = false;}- float env = c.ctl->ampEnv;+ float env = c.ctl->ampEnv.load(std::memory_order_relaxed);float coef = 1.0f;if (shaped) {const double tauMs = 20.0 * std::pow(100.0, (double)dec); // 20 ms .. 2000 ms@@ -100,7 +100,7 @@ void DrumRack::render(float* out, int32_t frames) {const float a = std::max(std::fabs(l), std::fabs(r));if (a > peak) peak = a;}- if (shaped) c.ctl->ampEnv = env;+ if (shaped) c.ctl->ampEnv.store(env, std::memory_order_relaxed);c.ctl->meter.store(peak * g, std::memory_order_relaxed);}}diff --git a/src/native/nota.engine/src/devices/racks/RackCore.cpp b/src/native/nota.engine/src/devices/racks/RackCore.cppindex 55b343a1..465bc128 100644--- a/src/native/nota.engine/src/devices/racks/RackCore.cpp+++ b/src/native/nota.engine/src/devices/racks/RackCore.cpp@@ -89,7 +89,7 @@ void RackCore::forkFrom(const RackCore& o) {ctl->chokeGroup.store(s.chokeGroup.load(r), r);ctl->tune.store(s.tune.load(r), r);ctl->decay.store(s.decay.load(r), r);- ctl->ampEnv = s.ampEnv; // a ringing pad keeps its level+ ctl->ampEnv.store(s.ampEnv.load(r), r); // a ringing pad keeps its level}c.ctl = std::move(ctl);}@@ -202,11 +202,11 @@ int32_t RackCore::addChainDevice(int32_t chain, int32_t deviceKind) {return addChainDeviceInstance(chain, dev);}-int32_t RackCore::addChainDeviceInstance(int32_t chain, std::shared_ptr<Device> dev) {+int32_t RackCore::addChainDeviceInstance(int32_t chain, std::shared_ptr<Device> dev, bool prepare) {if (!dev) return -1;auto ns = copyState();if (chain < 0 || chain >= static_cast<int32_t>(ns->chains.size())) return -1;- dev->setSampleRate(sampleRate_, maxBlock_);+ if (prepare) dev->setSampleRate(sampleRate_, maxBlock_);ns->chains[chain].devices.push_back(std::move(dev));const int32_t idx = static_cast<int32_t>(ns->chains[chain].devices.size()) - 1;commit(ns);diff --git a/src/native/nota.engine/src/devices/racks/RackCore.h b/src/native/nota.engine/src/devices/racks/RackCore.hindex 3fc7eb08..64a8a915 100644--- a/src/native/nota.engine/src/devices/racks/RackCore.h+++ b/src/native/nota.engine/src/devices/racks/RackCore.h@@ -56,7 +56,8 @@ class RackCore {std::atomic<int32_t> chokeGroup{0}; // 0 = none, 1..8 choke groupstd::atomic<int32_t> tune{0}; // pad transpose, semitones (-48..+48)std::atomic<float> decay{1.0f}; // pad decay 0..1 (1 = sustain / no shaping)- float ampEnv = 0.0f; // audio-thread only: pad decay VCA state+ std::atomic<float> ampEnv{0.0f}; // pad decay VCA state: written on the audio thread only;+ // relaxed so a rack fork can carry it overbool retrig = false; // audio-thread only: noteOn asks for an env kick};@@ -165,7 +166,9 @@ class RackCore {int32_t chainDeviceCount(int32_t chain) const;int32_t addChainDevice(int32_t chain, int32_t deviceKind);// Adds a pre-built device (e.g. a hosted plugin the engine instantiated) to a chain.- int32_t addChainDeviceInstance(int32_t chain, std::shared_ptr<Device> dev);+ // prepare = false: the device is already prepared at this rack's rate and may be playing+ // (moved in by wrapDeviceInRack), so it isn't reset.+ int32_t addChainDeviceInstance(int32_t chain, std::shared_ptr<Device> dev, bool prepare = true);bool removeChainDevice(int32_t chain, int32_t dev);bool moveChainDevice(int32_t chain, int32_t from, int32_t to);Device* chainDeviceAt(int32_t chain, int32_t dev) const;diff --git a/src/native/nota.engine/src/engine/Engine_Rack.cpp b/src/native/nota.engine/src/engine/Engine_Rack.cppindex b7c2d7a6..30ab5dbd 100644--- a/src/native/nota.engine/src/engine/Engine_Rack.cpp+++ b/src/native/nota.engine/src/engine/Engine_Rack.cpp@@ -153,10 +153,13 @@ RackCore* Engine::rackForEdit(int32_t trackId, int32_t deviceIndex) {r->forkFrom(*src);r->setBypassed(src->bypassed());r->folded = src->folded;- r->offSettled = src->offSettled;- r->pedalDown = src->pedalDown;- r->pedalHeld[0] = src->pedalHeld[0];- r->pedalHeld[1] = src->pedalHeld[1];+ // Playback state the audio thread owns: relaxed atomics, carried over so a held pedal+ // still releases its notes after the edit.+ constexpr auto rl = std::memory_order_relaxed;+ r->offSettled.store(src->offSettled.load(rl), rl);+ r->pedalDown.store(src->pedalDown.load(rl), rl);+ r->pedalHeld[0].store(src->pedalHeld[0].load(rl), rl);+ r->pedalHeld[1].store(src->pedalHeld[1].load(rl), rl);for (int i = 0; i < kCtlCount; ++i)r->deliveredCtl[i].store(src->deliveredCtl[i].load(std::memory_order_relaxed), std::memory_order_relaxed);fresh = r.get();@@ -181,11 +184,14 @@ RackCore* Engine::rackForEdit(int32_t trackId, int32_t deviceIndex) {// ---- wrap a device in a rack / unwrap it (#257) ----------------------------// Whether anything outside the device points at it by its chain index: automation lanes-// or CV links. Wrapping moves the device into a rack chain, where those can't follow it.+// CV links, or an Arp's CC target. Wrapping moves the device into a rack chain, where those can't follow it.bool Engine::deviceIsReferenced(const Track& t, int32_t deviceIndex) const {for (auto& l : t.automation)if ((l.target == AutomationTarget::DeviceParam || l.target == AutomationTarget::PluginParam) && l.deviceIndex == deviceIndex)return true;+ // An Arp's CC lane drives a device param by chain index (applyMidiCcRouting).+ for (auto& md : t.midiEffects)+ if (md && md->ccDestDevice() == deviceIndex) return true;for (auto& l : t.cvLinks)if (l.sourceKind == static_cast<int32_t>(CvSourceKind::Param) && l.sourceDevice == deviceIndex) return true;for (auto& ot : authoring_->tracks)@@ -206,7 +212,9 @@ int32_t Engine::wrapDeviceInRack(int32_t trackId, int32_t deviceIndex) {auto rack = std::make_shared<RackDevice>();rack->setSampleRate(sr, kMaxBlock);const int32_t chain = rack->addChain(-1);- if (rack->addChainDeviceInstance(chain, dev) < 0) return -1; // the device itself: its state moves in+ // The device itself moves in, already prepared at this rate: preparing it again would reset+ // it (a Reverb clears its tail) while the published graph may still be playing it.+ if (rack->addChainDeviceInstance(chain, dev, /*prepare=*/false) < 0) return -1;auto nt = cloneTrack(*old);nt->devices[deviceIndex] = rack;republishWithTrack(trackId, nt);@@ -221,6 +229,12 @@ int32_t Engine::unwrapRack(int32_t trackId, int32_t deviceIndex) {RackCore::RackState* st = rack ? rack->live() : nullptr;if (!st || st->chains.size() != 1 || deviceIsReferenced(*old, deviceIndex)) return -1;auto devs = st->chains[0].devices;+ // The devices after the rack shift by (n - 1). Lanes and CV links are remapped below; an+ // Arp's CC target lives on the shared MIDI device, outside the undo snapshot, so rather+ // than move it, don't unwrap under one.+ if (devs.size() != 1)+ for (auto& md : old->midiEffects)+ if (md && md->ccDestDevice() > deviceIndex) return -1;auto nt = cloneTrack(*old);nt->devices.erase(nt->devices.begin() + deviceIndex);nt->devices.insert(nt->devices.begin() + deviceIndex, devs.begin(), devs.end());diff --git a/src/native/nota.engine/src/engine/Engine_Render.cpp b/src/native/nota.engine/src/engine/Engine_Render.cppindex 061c5a06..fe5ae97f 100644--- a/src/native/nota.engine/src/engine/Engine_Render.cpp+++ b/src/native/nota.engine/src/engine/Engine_Render.cpp@@ -96,29 +96,33 @@ inline bool midiEvLess(const MidiEv& a, const MidiEv& b) {// pedal is down a released key's note-off waits in inst.pedalHeld; the pedal coming up sends// them all, and striking a held key again releases it right before the new note-on.inline bool pedalHolds(const Instrument& inst, int32_t pitch) {- return pitch >= 0 && pitch < 128 && ((inst.pedalHeld[pitch >> 6] >> (pitch & 63)) & 1u);+ return pitch >= 0 && pitch < 128 && ((inst.pedalHeld[pitch >> 6].load(std::memory_order_relaxed) >> (pitch & 63)) & 1u);}inline void sendNoteOn(Instrument& inst, int32_t pitch, float vel) {if (pedalHolds(inst, pitch)) {- inst.pedalHeld[pitch >> 6] &= ~(uint64_t{1} << (pitch & 63));+ auto& h = inst.pedalHeld[pitch >> 6];+ h.store(h.load(std::memory_order_relaxed) & ~(uint64_t{1} << (pitch & 63)), std::memory_order_relaxed);inst.noteOff(pitch);}inst.noteOn(pitch, vel);}inline void sendNoteOff(Instrument& inst, int32_t pitch) {- if (inst.pedalDown && pitch >= 0 && pitch < 128 && !inst.handlesSustain()) {- inst.pedalHeld[pitch >> 6] |= uint64_t{1} << (pitch & 63);+ if (inst.pedalDown.load(std::memory_order_relaxed) && pitch >= 0 && pitch < 128 && !inst.handlesSustain()) {+ auto& h = inst.pedalHeld[pitch >> 6];+ h.store(h.load(std::memory_order_relaxed) | (uint64_t{1} << (pitch & 63)), std::memory_order_relaxed);return;}inst.noteOff(pitch);}inline void sendController(Instrument& inst, int32_t ctl, float value) {if (ctl == kCtlSustain && !inst.handlesSustain()) {- inst.pedalDown = value >= 0.5f;- if (!inst.pedalDown) {+ const bool down = value >= 0.5f;+ inst.pedalDown.store(down, std::memory_order_relaxed);+ if (!down) {for (int32_t p = 0; p < 128; ++p)if (pedalHolds(inst, p)) inst.noteOff(p);- inst.pedalHeld[0] = inst.pedalHeld[1] = 0;+ inst.pedalHeld[0].store(0, std::memory_order_relaxed);+ inst.pedalHeld[1].store(0, std::memory_order_relaxed);}}inst.controller(ctl, value);@@ -161,8 +165,8 @@ template <class Live>void playEvents(Instrument& inst, float* dst, int32_t frames, const MidiEv* evs, int n, const ExprEv* xev, int xn, Live&& live) {const bool off = inst.bypassed();- if (!off) inst.offSettled = false;- else if (inst.offSettled)+ if (!off) inst.offSettled.store(false, std::memory_order_relaxed);+ else if (inst.offSettled.load(std::memory_order_relaxed))return;if (off) n = xn = 0; // nothing new starts on the way outint32_t cursor = 0;@@ -206,7 +210,7 @@ void playEvents(}inst.allNotesOff();releaseHeldControllers(inst);- inst.offSettled = true;+ inst.offSettled.store(true, std::memory_order_relaxed);}}} // namespacediff --git a/tests/Nota.SmokeTest/DeviceCopyTests.cs b/tests/Nota.SmokeTest/DeviceCopyTests.csindex 25fb1a76..121975df 100644--- a/tests/Nota.SmokeTest/DeviceCopyTests.cs+++ b/tests/Nota.SmokeTest/DeviceCopyTests.cs@@ -68,6 +68,55 @@ internal static class DeviceCopyTests// A rack with two chains isn't unwrapped (which chain would win?).e.RackDevAddChain(t, r, -1);yield return (e.UnwrapRack(t, r) < 0 && Chain(e, t) == "0,5,2,1", $"wrap in rack: a rack with two chains isn't unwrapped ({Chain(e, t)})");++ // A device an Arp's CC lane drives stays out: the Arp addresses it by chain index.+ int it = e.AddInstrumentTrack();+ e.SetTrackBuiltinInstrument(it, 0);+ int arp = e.AddMidiEffect(it, Arp);+ e.AddBuiltinDevice(it, 0);+ int target = e.AddBuiltinDevice(it, 1);+ e.SetMidiEffectCcDest(it, arp, target, 0);+ yield return (e.WrapDeviceInRack(it, target) < 0 && Chain(e, it) == "0,1", $"wrap in rack: a device an Arp's CC targets isn't wrapped ({Chain(e, it)})");+ e.SetMidiEffectCcDest(it, arp, -1, -1);+ yield return (e.WrapDeviceInRack(it, target) == target, "wrap in rack: it wraps once the Arp lets go");+ // Unwrapping a rack the Arp drives, or one whose devices would shift the Arp's target, is refused.+ e.SetMidiEffectCcDest(it, arp, target, 0);+ yield return (e.UnwrapRack(it, target) < 0 && Chain(e, it) == "0,5", "wrap in rack: a rack an Arp's CC lane drives isn't unwrapped");+ int after = e.AddBuiltinDevice(it, 3);+ e.RackDevAddChainDevice(it, target, 0, 3);+ e.SetMidiEffectCcDest(it, arp, after, 0);+ yield return (e.UnwrapRack(it, target) < 0 && Chain(e, it) == "0,5,3", "wrap in rack: an unwrap that would shift the Arp's target is refused");+ e.SetMidiEffectCcDest(it, arp, -1, -1);+ yield return (e.UnwrapRack(it, target) == 2 && Chain(e, it) == "0,1,3,3", $"wrap in rack: with the Arp unhooked it comes out ({Chain(e, it)})");++ foreach (var x in WrapKeepsTail()) yield return x;+ }++ // Wrapping moves the device in as it is: it isn't prepared again, which reset a Reverb's tail+ // (and reallocated its delay lines under a playing graph).+ private static IEnumerable<(bool, string)> WrapKeepsTail()+ {+ using var e = new NotaEngine();+ int t = e.AddInstrumentTrack();+ e.SetTrackBuiltinInstrument(t, 0);+ int rv = e.AddBuiltinDevice(t, 2);+ e.DeviceSetParam(t, rv, 12, 1f); // Dry/Wet+ e.DeviceSetParam(t, rv, 19, 1f); // Wet Only+ e.SetBpm(120); e.SetMetronome(false);+ int clip = e.AddMidiClip(t, 0.0, 4.0);+ e.SetClipNotes(t, clip, new[] { new NotaNote(60, 0.0, 0.25, 1.0f) });+ var buf = new float[512 * 2];+ static float Rms(float[] b) { double a = 0; foreach (var v in b) a += v * v; return (float)Math.Sqrt(a / b.Length); }+ e.Seek(0); e.Play();+ for (int i = 0; i < 30; i++) e.RenderOffline(buf, 512, 44100);+ e.DeviceSetParam(t, rv, 11, 1f); // Freeze: the tail holds while the synth has released+ for (int i = 0; i < 30; i++) e.RenderOffline(buf, 512, 44100);+ float before = Rms(buf);+ e.WrapDeviceInRack(t, rv);+ e.RenderOffline(buf, 512, 44100);+ float after = Rms(buf);+ e.StopTransport();+ yield return (before > 1e-4f && after > before * 0.5f, $"wrap in rack: a ringing Reverb keeps its tail (rms {before:F4} → {after:F4})");}// A value well inside the param's range and away from its default.diff --git a/tests/perf/nota_perf.cpp b/tests/perf/nota_perf.cppindex 76959e0f..b40a45de 100644--- a/tests/perf/nota_perf.cpp+++ b/tests/perf/nota_perf.cpp@@ -143,6 +143,8 @@ std::vector<Scenario> scenarios() {std::snprintf(id, sizeof id, "instrument-%02d", k);s.push_back({id, instrumentName(k) + ", one track"});}+ s.push_back({"drum-rack", "Drum Rack, 4 Synth pads with decay"});+ s.push_back({"instrument-rack", "Instrument Rack, 2 Synth chains"});s.push_back({"effect-base", "Synth alone (effect baseline)"});s.push_back({"aurora-supersaw-2x", "Aurora Super Saw x2"});for (int k = 0; k < kMaxKind; ++k) {@@ -175,6 +177,31 @@ bool build(NotaEngine* e, const std::string& id) {if (nota_track_add_builtin_device(e, t, kind) < 0) return false;return true;}+ // The racks render their chains themselves (pad decay VCA, key zones), so they get their own rows.+ if (id == "drum-rack") {+ const int t = nota_engine_add_drum_rack_track(e);+ if (t < 0) return false;+ static const int pads[4] = {36, 38, 42, 46};+ for (int p : pads) {+ const int c = nota_rack_add_chain(e, t, 0);+ if (c < 0) return false;+ nota_rack_set_chain_trigger_note(e, t, c, p);+ nota_rack_set_chain_decay(e, t, c, 0.5f);+ }+ const int clip = nota_track_add_midi_clip(e, t, 0.0, 16.0);+ std::vector<NotaNoteData> notes;+ for (int s16 = 0; s16 < 64; ++s16) notes.push_back({pads[s16 % 4], s16 * 0.25, 0.2, 0.9f, 0});+ nota_clip_set_notes(e, t, clip, notes.data(), (int)notes.size());+ return true;+ }+ if (id == "instrument-rack") {+ const int t = nota_engine_add_instrument_rack_track(e);+ if (t < 0) return false;+ for (int i = 0; i < 2; ++i)+ if (nota_rack_add_chain(e, t, 0) < 0) return false;+ fillClip(e, t, 0, 0, 16);+ return true;+ }if (id == "aurora-supersaw-2x") {for (int t = 0; t < 2; ++t) {const int tr = instrumentTrack(e, 5);