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) Fixed problem with automatization when changing device order

Egor Khindikaynen4821f72afc2b
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9bfe4b4e..9b4c4e4c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -17,6 +17,8 @@ created, and the build-and-publish run starts.
Entry categories: `Added`, `Changed`, `Fixed`, `Removed`.
### Fixed
+- Reordering or removing effects in Devices no longer scrambles their automation: each lane
+ stays on its own device and parameter. Removing a device also removes its automation.
- In the piano roll you can now drag a note's left edge to change where it starts; its end
stays put.
- Stacked notes (a chord) no longer hide each other's velocity. Grab a stem by its cap to
diff --git a/src/managed/Nota.App/ArrangementView.Automation.cs b/src/managed/Nota.App/ArrangementView.Automation.cs
index 44f5fc8b..7166702b 100644
--- a/src/managed/Nota.App/ArrangementView.Automation.cs
+++ b/src/managed/Nota.App/ArrangementView.Automation.cs
@@ -133,6 +133,21 @@ public sealed partial class ArrangementView
Redraw();
}
+ /// <summary>The device chain reordered / removed effects (midi=false) or MIDI effects on a
+ /// track: keep its shown automation target on the same device (the engine moves the lanes
+ /// the same way). map: old chain index → new, -1 when the device is gone (→ Volume).</summary>
+ internal void RemapAutoTargets(int trackId, bool midi, Func<int, int> map)
+ {
+ if (!_autoTargets.TryGetValue(trackId, out var sel)) return;
+ bool onChain = midi ? sel.target == AutomationTarget.MidiDeviceParam
+ : sel.target == AutomationTarget.DeviceParam
+ || (sel.target == AutomationTarget.PluginParam && sel.dev >= 0);
+ if (!onChain) return;
+ int ni = map(sel.dev);
+ _autoTargets[trackId] = ni >= 0 ? sel with { dev = ni } : (AutomationTarget.Volume, -1, -1, "");
+ // Refresh() (raised by the chain's Changed) rebuilds the lanes from _autoTargets.
+ }
+
/// <summary>Select a hosted-plugin parameter (by stable id) as the lane target (M9-B3).</summary>
internal void SetAutoPluginTarget(TrackVM t, int deviceIndex, string paramId)
{
diff --git a/src/managed/Nota.App/DeviceChainView.Shell.cs b/src/managed/Nota.App/DeviceChainView.Shell.cs
index e490fb08..17e383a2 100644
--- a/src/managed/Nota.App/DeviceChainView.Shell.cs
+++ b/src/managed/Nota.App/DeviceChainView.Shell.cs
@@ -67,6 +67,7 @@ public sealed partial class DeviceChainView
// map: old index → new index, or -1 when the device is gone.
private void RemapExtras(ChainKind k, Func<int, int> map)
{
+ if (k != ChainKind.Instrument) DevicesRemapped?.Invoke(_trackId, k == ChainKind.Midi, map);
if (k == ChainKind.Instrument || !_cardExtra.TryGetValue(_trackId, out var d)) return;
var domain = d.Where(kv => KeyKind(kv.Key) == k).ToList();
foreach (var kv in domain) d.Remove(kv.Key);
diff --git a/src/managed/Nota.App/DeviceChainView.cs b/src/managed/Nota.App/DeviceChainView.cs
index 25e6b141..913679c3 100644
--- a/src/managed/Nota.App/DeviceChainView.cs
+++ b/src/managed/Nota.App/DeviceChainView.cs
@@ -53,6 +53,11 @@ public sealed partial class DeviceChainView : UserControl
/// <summary>Raised after a change that affects track headers (remove/reorder).</summary>
public event Action? Changed;
+ /// <summary>Raised when this view reorders / removes effects (midi=false) or MIDI effects
+ /// (midi=true) on a track: map is old chain index → new index, or -1 when the device is gone.
+ /// Lets index-addressed state elsewhere (the arrangement's automation target) follow it.</summary>
+ public event Action<int, bool, Func<int, int>>? DevicesRemapped;
+
/// <summary>Raised to save a device/instrument as a preset (M7-4c); arg is the
/// device index, or -1 for the track's instrument. MainWindow does the prompt+save.</summary>
public event Action<int>? PresetSaveRequested;
diff --git a/src/managed/Nota.App/MainWindow.axaml.cs b/src/managed/Nota.App/MainWindow.axaml.cs
index ea79bb90..313dc81a 100644
--- a/src/managed/Nota.App/MainWindow.axaml.cs
+++ b/src/managed/Nota.App/MainWindow.axaml.cs
@@ -289,6 +289,7 @@ public partial class MainWindow : Window
_deviceChain = new DeviceChainView(vm.Engine, _factory, App.Services.GetService<IPluginCatalog>(), _kits);
// A pad added / removed / renamed in the Drum Rack card changes the pattern grid's rows.
_deviceChain.Changed += () => { Timeline.Refresh(); _patternView?.Reload(); if (_modular?.IsVisible == true) _modular.Refresh(); };
+ _deviceChain.DevicesRemapped += Timeline.RemapAutoTargets;
_deviceChain.PresetSaveRequested += OnSavePreset;
_deviceChain.RackPresetSaveRequested += OnSaveRackChainPreset;
_deviceChain.ItemDropped += OnDevicePanelDrop; // browser drag onto the device panel
diff --git a/src/managed/Nota.Application/Engine/IAudioEngine.cs b/src/managed/Nota.Application/Engine/IAudioEngine.cs
index 630d1064..713fa6db 100644
--- a/src/managed/Nota.Application/Engine/IAudioEngine.cs
+++ b/src/managed/Nota.Application/Engine/IAudioEngine.cs
@@ -784,6 +784,7 @@ public interface IAudioEngine : IDisposable
string AutomationLaneParamId(int trackId, int laneIndex);
int PluginLastTouchedParam(int trackId, int deviceIndex); // "Learn" (M9-B3)
bool AutomationWriteSelfTest(); // M9-C: device-free write-path check
+ bool AutomationDeviceRemapSelfTest(); // lanes follow their device on reorder / remove
// Automation record (M9-C). There are no record modes: lanes always play back, and
// a control gesture records while automation record is on (the transport record
// button drives it). deviceIndex < 0 = instrument; paramId for PluginParam.
diff --git a/src/managed/Nota.Infrastructure/Interop/NativeMethods.Automation.cs b/src/managed/Nota.Infrastructure/Interop/NativeMethods.Automation.cs
index 4edf5b5a..bbec418b 100644
--- a/src/managed/Nota.Infrastructure/Interop/NativeMethods.Automation.cs
+++ b/src/managed/Nota.Infrastructure/Interop/NativeMethods.Automation.cs
@@ -30,6 +30,9 @@ internal static partial class NativeMethods
[LibraryImport(Lib, EntryPoint = "nota_engine_automation_write_selftest")]
internal static partial int AutomationWriteSelfTest(IntPtr engine);
+ [LibraryImport(Lib, EntryPoint = "nota_engine_automation_device_remap_selftest")]
+ internal static partial int AutomationDeviceRemapSelfTest(IntPtr engine);
+
[LibraryImport(Lib, EntryPoint = "nota_engine_set_automation_record")]
internal static partial void SetAutomationRecord(IntPtr engine, int on);
diff --git a/src/managed/Nota.Infrastructure/Interop/NotaEngine.Automation.cs b/src/managed/Nota.Infrastructure/Interop/NotaEngine.Automation.cs
index 2835bc61..e2a6c543 100644
--- a/src/managed/Nota.Infrastructure/Interop/NotaEngine.Automation.cs
+++ b/src/managed/Nota.Infrastructure/Interop/NotaEngine.Automation.cs
@@ -26,6 +26,9 @@ public sealed partial class NotaEngine
public bool AutomationWriteSelfTest()
{ ThrowIfDisposed(); return NativeMethods.AutomationWriteSelfTest(_handle) != 0; }
+ public bool AutomationDeviceRemapSelfTest()
+ { ThrowIfDisposed(); return NativeMethods.AutomationDeviceRemapSelfTest(_handle) != 0; }
+
// --- automation write / record (M9-C). deviceIndex < 0 = instrument. ---
/// <summary>While on, a control gesture records into that parameter's lane. The
/// transport record button drives this — <see cref="SetRecording"/> sets it too.</summary>
diff --git a/src/native/nota.engine/include/nota/nota_engine.h b/src/native/nota.engine/include/nota/nota_engine.h
index f84fd484..2f3807b5 100644
--- a/src/native/nota.engine/include/nota/nota_engine.h
+++ b/src/native/nota.engine/include/nota/nota_engine.h
@@ -266,6 +266,8 @@ NOTA_API int32_t nota_plugin_last_touched_param(NotaEngine* engine, int32_t trac
/* ---- automation write / record (M9-C) -----------------------------------
* Device-free self-test of the write path (touch, latch, override/re-enable). */
NOTA_API int32_t nota_engine_automation_write_selftest(NotaEngine* engine);
+/* Self-test: reordering / removing effects keeps each lane on its own device. */
+NOTA_API int32_t nota_engine_automation_device_remap_selftest(NotaEngine* engine);
/* There are no record modes: lanes always play back, and gestures record while
* automation record is on (the transport record button drives it — engaging it
* via nota_engine_set_recording sets this too). begin/end bracket a control
diff --git a/src/native/nota.engine/src/Engine.h b/src/native/nota.engine/src/Engine.h
index 8b66bca0..fda64fc7 100644
--- a/src/native/nota.engine/src/Engine.h
+++ b/src/native/nota.engine/src/Engine.h
@@ -235,6 +235,7 @@ public:
void reenableAutomation(); // hand the overridden lanes back to playback
bool automationOverridden() const { return !overrides_.empty(); }
bool automationWriteSelfTest(); // device-free: touch, latch, override/re-enable
+ bool automationDeviceRemapSelfTest(); // lanes follow their device on move/remove
// --- debug oscillator (M0) ---
void setToneEnabled(bool enabled);
@@ -915,6 +916,11 @@ private:
// Keep CV-link targetDevice indices valid after a device on `track` was removed
// (removedIndex>=0) or moved (from/to). Clones the affected owner tracks + republishes.
void remapCvLinksAfterDeviceChange(int32_t track, int32_t removedIndex, int32_t from, int32_t to);
+ // Keep automation-lane deviceIndex valid after an effect (midi=false) or MIDI effect
+ // (midi=true) on `nt` was removed (removedIndex>=0; its lanes are dropped) or moved
+ // (from/to). Edits `nt` in place before it is published, so it shares the device
+ // edit's undo step; also fixes in-flight write / override lane references.
+ void remapAutomationAfterDeviceChange(Track& nt, bool midi, int32_t removedIndex, int32_t from, int32_t to);
// automation write (M9-C, message thread)
void sampleAutomationWritesAt(double beat); // grow active lanes to (beat, control-value)
diff --git a/src/native/nota.engine/src/Engine_Automation.cpp b/src/native/nota.engine/src/Engine_Automation.cpp
index 6a5ace08..ae348379 100644
--- a/src/native/nota.engine/src/Engine_Automation.cpp
+++ b/src/native/nota.engine/src/Engine_Automation.cpp
@@ -452,6 +452,90 @@ int32_t Engine::addAutomationLane(int32_t trackId, int32_t target, int32_t devic
return index;
}
+// --- device edit → lane index fix-up (message thread) -----------------------
+
+void Engine::remapAutomationAfterDeviceChange(Track& nt, bool midi, int32_t removedIndex, int32_t from, int32_t to) {
+ // Lanes on the effect chain (DeviceParam / PluginParam >= 0) or the MIDI-FX chain.
+ auto onChain = [midi](const AutomationLane& l) {
+ if (midi) return l.target == AutomationTarget::MidiDeviceParam;
+ return l.target == AutomationTarget::DeviceParam ||
+ (l.target == AutomationTarget::PluginParam && l.deviceIndex >= 0);
+ };
+ // New chain position of `idx` (-1 = its device was removed).
+ auto remap = [&](int32_t idx) -> int32_t {
+ if (removedIndex >= 0) return idx == removedIndex ? -1 : idx > removedIndex ? idx - 1 : idx;
+ if (idx == from) return to;
+ if (from < to) { if (idx > from && idx <= to) return idx - 1; }
+ else { if (idx >= to && idx < from) return idx + 1; }
+ return idx;
+ };
+
+ // laneMap: old lane index → new lane index (-1 = dropped with its device).
+ std::vector<int32_t> laneMap(nt.automation.size());
+ std::vector<AutomationLane> kept;
+ kept.reserve(nt.automation.size());
+ for (size_t i = 0; i < nt.automation.size(); ++i) {
+ AutomationLane& l = nt.automation[i];
+ if (onChain(l)) {
+ const int32_t ni = remap(l.deviceIndex);
+ if (ni < 0) { laneMap[i] = -1; continue; }
+ l.deviceIndex = ni;
+ }
+ laneMap[i] = static_cast<int32_t>(kept.size());
+ kept.push_back(std::move(l));
+ }
+ nt.automation = std::move(kept);
+
+ // In-flight gestures on this track address lanes by index and targets by device.
+ const int32_t trackId = nt.id();
+ auto laneGone = [&](int32_t& laneIndex) {
+ if (laneIndex < 0 || laneIndex >= static_cast<int32_t>(laneMap.size())) return true;
+ laneIndex = laneMap[laneIndex];
+ return laneIndex < 0;
+ };
+ std::erase_if(activeWrites_, [&](ActiveWrite& aw) {
+ if (aw.trackId != trackId) return false;
+ if (laneGone(aw.laneIndex)) return true;
+ const auto tgt = static_cast<AutomationTarget>(aw.target);
+ const bool chain = midi ? tgt == AutomationTarget::MidiDeviceParam
+ : tgt == AutomationTarget::DeviceParam ||
+ (tgt == AutomationTarget::PluginParam && aw.deviceIndex >= 0);
+ if (chain) aw.deviceIndex = remap(aw.deviceIndex);
+ return false;
+ });
+ std::erase_if(overrides_, [&](OverrideTarget& o) { return o.trackId == trackId && laneGone(o.laneIndex); });
+}
+
+bool Engine::automationDeviceRemapSelfTest() {
+ // Two effects, a lane on each: reordering the chain keeps every lane on its own
+ // device; removing a device drops its lanes and shifts the rest.
+ const int32_t tid = addInstrumentTrack();
+ const int32_t d0 = addTrackBuiltinDevice(tid, 3); // Delay @0
+ const int32_t d1 = addTrackBuiltinDevice(tid, 4); // Utility @1
+ const int32_t d2 = addTrackBuiltinDevice(tid, 2); // Reverb @2
+ if (d0 != 0 || d1 != 1 || d2 != 2) { removeTrack(tid); return false; }
+ const int32_t dp = static_cast<int32_t>(AutomationTarget::DeviceParam);
+ addAutomationLane(tid, dp, 0, 1); // lane 0 → Delay
+ addAutomationLane(tid, dp, 1, 2); // lane 1 → Utility
+ addAutomationLane(tid, dp, 2, 3); // lane 2 → Reverb
+ addAutomationLane(tid, static_cast<int32_t>(AutomationTarget::Volume), -1, -1); // lane 3
+ auto devOf = [&](int32_t lane) {
+ int32_t dev = -9;
+ automationLaneInfo(tid, lane, nullptr, &dev, nullptr, nullptr);
+ return dev;
+ };
+ auto kindOf = [&](int32_t lane) { return trackDeviceBuiltinKind(tid, devOf(lane)); };
+
+ moveDevice(tid, 0, 2); // Utility, Reverb, Delay
+ bool ok = kindOf(0) == 3 && kindOf(1) == 4 && kindOf(2) == 2 && devOf(3) == -1;
+ moveDevice(tid, 2, 0); // Delay, Utility, Reverb
+ ok = ok && devOf(0) == 0 && devOf(1) == 1 && devOf(2) == 2;
+ removeDevice(tid, 1); // Delay, Reverb — Utility's lane goes
+ ok = ok && automationLaneCount(tid) == 3 && kindOf(0) == 3 && kindOf(1) == 2 && devOf(2) == -1;
+ removeTrack(tid);
+ return ok;
+}
+
int32_t Engine::automationLaneCount(int32_t trackId) const {
auto t = findTrackAuthoring(trackId);
return t ? static_cast<int32_t>(t->automation.size()) : 0;
diff --git a/src/native/nota.engine/src/Engine_Devices.cpp b/src/native/nota.engine/src/Engine_Devices.cpp
index 92bfe8fe..440c3a51 100644
--- a/src/native/nota.engine/src/Engine_Devices.cpp
+++ b/src/native/nota.engine/src/Engine_Devices.cpp
@@ -547,6 +547,7 @@ bool Engine::moveDevice(int32_t trackId, int32_t fromIndex, int32_t toIndex) {
auto dev = nt->devices[fromIndex];
nt->devices.erase(nt->devices.begin() + fromIndex);
nt->devices.insert(nt->devices.begin() + toIndex, dev);
+ remapAutomationAfterDeviceChange(*nt, false, -1, fromIndex, toIndex); // lanes follow their device
republishWithTrack(trackId, nt);
remapCvLinksAfterDeviceChange(trackId, -1, fromIndex, toIndex); // keep CV-link targets valid
return true; // order change doesn't affect total chain latency (PDC unchanged)
@@ -560,6 +561,7 @@ bool Engine::removeDevice(int32_t trackId, int32_t deviceIndex) {
if (old->devices[deviceIndex]) old->devices[deviceIndex]->closeEditor();
auto nt = cloneTrack(*old);
nt->devices.erase(nt->devices.begin() + deviceIndex);
+ remapAutomationAfterDeviceChange(*nt, false, deviceIndex, -1, -1); // drop/shift its lanes
republishWithTrack(trackId, nt);
remapCvLinksAfterDeviceChange(trackId, deviceIndex, -1, -1); // drop/shift CV-link targets
recomputePdc();
@@ -689,6 +691,7 @@ bool Engine::moveMidiEffect(int32_t trackId, int32_t fromIndex, int32_t toIndex)
auto md = nt->midiEffects[fromIndex];
nt->midiEffects.erase(nt->midiEffects.begin() + fromIndex);
nt->midiEffects.insert(nt->midiEffects.begin() + toIndex, md);
+ remapAutomationAfterDeviceChange(*nt, true, -1, fromIndex, toIndex);
republishWithTrack(trackId, nt);
return true;
}
@@ -698,6 +701,7 @@ bool Engine::removeMidiEffect(int32_t trackId, int32_t index) {
if (!old || index < 0 || index >= static_cast<int32_t>(old->midiEffects.size())) return false;
auto nt = cloneTrack(*old);
nt->midiEffects.erase(nt->midiEffects.begin() + index);
+ remapAutomationAfterDeviceChange(*nt, true, index, -1, -1);
republishWithTrack(trackId, nt);
if (nt->instrument) nt->instrument->allNotesOff(); // drop any arp-driven voices
return true;
diff --git a/src/native/nota.engine/src/nota_engine_automation.cpp b/src/native/nota.engine/src/nota_engine_automation.cpp
index 8c89677f..27f8b22d 100644
--- a/src/native/nota.engine/src/nota_engine_automation.cpp
+++ b/src/native/nota.engine/src/nota_engine_automation.cpp
@@ -88,6 +88,9 @@ const char* nota_track_automation_lane_param_id(const NotaEngine* e, int32_t tra
int32_t nota_engine_automation_write_selftest(NotaEngine* e) {
return (e && ENG(e)->automationWriteSelfTest()) ? 1 : 0;
}
+int32_t nota_engine_automation_device_remap_selftest(NotaEngine* e) {
+ return (e && ENG(e)->automationDeviceRemapSelfTest()) ? 1 : 0;
+}
void nota_engine_set_automation_record(NotaEngine* e, int32_t on) {
if (e) ENG(e)->setAutomationRecord(on != 0);
}
diff --git a/tests/Nota.SmokeTest/Program.cs b/tests/Nota.SmokeTest/Program.cs
index 06eb3118..36bdef9c 100644
--- a/tests/Nota.SmokeTest/Program.cs
+++ b/tests/Nota.SmokeTest/Program.cs
@@ -8801,6 +8801,7 @@ Console.WriteLine("-- M9-B1: plugin-param automation core --");
Console.WriteLine("-- M9-C W1: automation write/record --");
{
Check(engine.AutomationWriteSelfTest(), "automation write path: touch, latch, override (device-free)");
+ Check(engine.AutomationDeviceRemapSelfTest(), "automation lanes follow their device on reorder / remove");
}
// ===================== M9-C W2: record-switch C ABI + C# ===================