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) Take retry: find devices by lineage, not by kind
Egor Khindikaynene3af0774b416
diff --git a/src/native/nota.engine/src/devices/Device.h b/src/native/nota.engine/src/devices/Device.hindex 7c450036..84df7b35 100644--- a/src/native/nota.engine/src/devices/Device.h+++ b/src/native/nota.engine/src/devices/Device.h@@ -25,6 +25,12 @@ struct MidiEv;class RackCore;+// A new device's Device::lineage().+inline uint64_t nextDeviceLineage() {+ static std::atomic<uint64_t> next{0};+ return next.fetch_add(1, std::memory_order_relaxed) + 1;+}+class Device {public:// The rack behind this device, if it is one (specs/044): a virtual call where the audio@@ -32,6 +38,12 @@ class Device {virtual const RackCore* asRack() const { return nullptr; }virtual ~Device() = default;+ // Which device this is, across the copies an edit swaps into its slot (a rack fork, a+ // loaded state): a new object gets a new id, a copy is given its source's. Take retry+ // finds a device's automation by it. Message thread only.+ uint64_t lineage() const { return lineage_; }+ void inheritLineage(uint64_t id) { lineage_ = id; }+// Called on the message thread before the device is used (and on sample-rate// changes). maxBlock is the largest frame count a process() call may pass.virtual void setSampleRate(double sr, int32_t maxBlock) = 0;@@ -276,6 +288,7 @@ class Device {std::atomic<float> scMix_{1.0f};std::atomic<int32_t> scTapPre_{0};std::atomic<uint64_t> scPart_{0};+ uint64_t lineage_ = nextDeviceLineage();};} // namespace notadiff --git a/src/native/nota.engine/src/devices/MidiDevice.h b/src/native/nota.engine/src/devices/MidiDevice.hindex 63dd32a1..361811fd 100644--- a/src/native/nota.engine/src/devices/MidiDevice.h+++ b/src/native/nota.engine/src/devices/MidiDevice.h@@ -34,6 +34,12 @@ struct MidiEv {class RackCore;+// A new MIDI device's MidiDevice::lineage().+inline uint64_t nextMidiDeviceLineage() {+ static std::atomic<uint64_t> next{0};+ return next.fetch_add(1, std::memory_order_relaxed) + 1;+}+class MidiDevice {public:// The rack behind this device, if it is one (specs/044): a virtual call where the audio@@ -41,6 +47,12 @@ class MidiDevice {virtual const RackCore* asRack() const { return nullptr; }virtual ~MidiDevice() = default;+ // Which device this is, across the copies an edit swaps into its slot (a rack fork, a+ // loaded state): a new object gets a new id, a copy is given its source's. Take retry+ // finds a device's automation by it. Message thread only.+ uint64_t lineage() const { return lineage_; }+ void inheritLineage(uint64_t id) { lineage_ = id; }+virtual void setSampleRate(double sr, int32_t /*maxBlock*/) {}// Transform `in` (nIn sorted events) into `out` (writing at most maxOut events,@@ -146,6 +158,8 @@ class MidiDevice {std::atomic<bool> bypassed_{false};bool folded_ = false;int32_t presetTag_ = 0;+ uint64_t lineage_ = nextMidiDeviceLineage();};+} // namespace notadiff --git a/src/native/nota.engine/src/engine/Engine_Devices.cpp b/src/native/nota.engine/src/engine/Engine_Devices.cppindex cecc4f7e..030352cd 100644--- a/src/native/nota.engine/src/engine/Engine_Devices.cpp+++ b/src/native/nota.engine/src/engine/Engine_Devices.cpp@@ -1269,6 +1269,7 @@ bool Engine::midiEffectSetState(int32_t t, int32_t i, const uint8_t* data, int32auto fresh = cloneMidiDevice(*old->midiEffects[i]);if (!fresh) return false;fresh->setState(data, size);+ fresh->inheritLineage(old->midiEffects[i]->lineage()); // the same device to take retry// A MIDI Effect Rack's copy plays on with the held-note tables, so it ends what the old one// started instead of leaving it hanging.if (auto* to = dynamic_cast<MidiRack*>(fresh.get()))diff --git a/src/native/nota.engine/src/engine/Engine_Rack.cpp b/src/native/nota.engine/src/engine/Engine_Rack.cppindex 457f84a5..4b6e54f1 100644--- a/src/native/nota.engine/src/engine/Engine_Rack.cpp+++ b/src/native/nota.engine/src/engine/Engine_Rack.cpp@@ -193,6 +193,7 @@ RackCore* Engine::rackForEdit(int32_t trackId, int32_t deviceIndex) {r->setBypassed(src->bypassed());r->setFolded(src->folded());r->setPresetTag(src->presetTag());+ r->inheritLineage(src->lineage()); // the same device to take retryfresh = r.get();nt->midiEffects[mi] = std::move(r);republishWithTrack(trackId, nt);@@ -217,6 +218,7 @@ RackCore* Engine::rackForEdit(int32_t trackId, int32_t deviceIndex) {r->setSidechainTapPre(src->sidechainTapPre());r->setSidechainGainDb(src->sidechainGainDb());r->setSidechainMix(src->sidechainMix());+ r->inheritLineage(src->lineage()); // the same device to take retryfresh = r.get();nt->devices[deviceIndex] = std::move(r);}diff --git a/src/native/nota.engine/src/engine/Engine_TakeRetry.cpp b/src/native/nota.engine/src/engine/Engine_TakeRetry.cppindex 98e5d2d2..e5e1a32c 100644--- a/src/native/nota.engine/src/engine/Engine_TakeRetry.cpp+++ b/src/native/nota.engine/src/engine/Engine_TakeRetry.cpp@@ -13,8 +13,6 @@#include <algorithm>#include <cmath>-#include <cstring>-#include <typeinfo>namespace nota {@@ -64,38 +62,18 @@ void Engine::closeTakeAttempt() {retryStartBeat_ = std::numeric_limits<double>::quiet_NaN();}-// Where each device of `from` sits in `to` (-1: removed). The same object first — devices are-// shared between track copies, so a moved one is found in its new slot; then a device an edit-// replaced with a copy (a rack fork, a MIDI effect's loaded state) pairs with the unmatched-// device of the same type and name next in chain order.+// Where each device of `from` sits in `to` (-1: removed), by lineage: a moved device is the+// same object in a new slot, and a copy an edit swapped in (a rack fork, a MIDI effect's loaded+// state) carries its source's lineage. A fresh device of the same kind is a different device.template <class Chain>static std::vector<int32_t> deviceMap(const Chain& from, const Chain& to) {std::vector<int32_t> map(from.size(), -1);- std::vector<bool> taken(to.size(), false);for (size_t i = 0; i < from.size(); ++i)for (size_t j = 0; j < to.size(); ++j)- if (!taken[j] && from[i] && from[i] == to[j]) {+ if (from[i] && to[j] && from[i]->lineage() == to[j]->lineage()) {map[i] = static_cast<int32_t>(j);- taken[j] = true;break;}- auto same = [](const auto& x, const auto& y) {- return x && y && typeid(*x) == typeid(*y) && std::strcmp(x->displayName(), y->displayName()) == 0;- };- size_t next = 0;- for (size_t i = 0; i < from.size(); ++i) {- if (map[i] >= 0) {- next = std::max(next, static_cast<size_t>(map[i]) + 1);- continue;- }- for (size_t j = next; j < to.size(); ++j)- if (!taken[j] && same(from[i], to[j])) {- map[i] = static_cast<int32_t>(j);- taken[j] = true;- next = j + 1;- break;- }- }return map;}diff --git a/tests/Nota.SmokeTest/RetryTakeTests.cs b/tests/Nota.SmokeTest/RetryTakeTests.csindex 04781ca5..fb541968 100644--- a/tests/Nota.SmokeTest/RetryTakeTests.cs+++ b/tests/Nota.SmokeTest/RetryTakeTests.cs@@ -211,7 +211,8 @@ internal static class RetryTakeTests}// Moving an automated effect mid-take: the pre-take lane comes back aimed at the effect- // where it now sits; removing one takes its lane with it.+ // where it now sits; removing one takes its lane with it, even when a fresh effect of the+ // same kind is added.private static IEnumerable<(bool, string)> DeviceMovedMeanwhile(){const int Utility = 4, Delay = 3;@@ -236,11 +237,12 @@ internal static class RetryTakeTests.Where(i => i.Target == AutomationTarget.DeviceParam).ToArray();yield return (e.AutomationLaneCount(t) == lanes && moved.Length == 1 && moved[0].DeviceIndex == 1 && moved[0].PointCount == 2,$"retry after moving an automated effect: its lane follows it (lanes {lanes} → {e.AutomationLaneCount(t)}, device {(moved.Length == 1 ? moved[0].DeviceIndex : -9)})");- e.RemoveDevice(t, 1); // the Utility goes, mid-take+ e.RemoveDevice(t, 1); // the Utility goes, mid-take,+ e.AddBuiltinDevice(t, Utility); // and a fresh one of the same kind comes ine.RetryTake();Beats(e, 0.01);bool gone = Enumerable.Range(0, e.AutomationLaneCount(t)).All(i => e.AutomationLaneInfo(t, i).Target != AutomationTarget.DeviceParam);- yield return (gone, "retry after removing an automated effect: its lane goes with it");+ yield return (gone, "retry after removing an automated effect and adding another of its kind: the lane goes, the new effect gets none");e.SetRecording(false);e.StopTransport();}