Просмотр исходного кода

Keep hosted plugin settings when effect slots, mixer tracks or channels are moved (issue #32)

Hosted plugins are reused by position, so a plugin that ended up in another
place was rebuilt from the Song's saved state, which is only refreshed on
save: its parameters came back reset. editSongStructural() copies the live
states in first and drops position reuse for duplicate plugins so two
instances of one plugin swap correctly.

Not compiled or tested: no JUCE build was available.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
HighTecno 1 месяц назад
Родитель
Сommit
7cf40fb2eb

+ 25 - 0
src/engine/AudioEngine.cpp

@@ -2200,6 +2200,31 @@ void AudioEngine::editSong (const std::function<void (Song&)>& edit, bool undoab
     publishSong();
 }
 
+void AudioEngine::editSongStructural (const std::function<void (Song&)>& edit)
+{
+    capturePluginStates();
+
+    // a plugin is kept when the same plugin sat in the same place; for duplicates that would keep the old order
+    std::map<std::string, int> uses;
+    for (const auto& c : lastPublishedShape.song.channels)
+        if (c.instrument == InstrumentKind::plugin && ! c.plugin.id.empty())
+            ++uses[c.plugin.id];
+    for (const auto& track : lastPublishedShape.song.mixer)
+        for (const auto& slot : track.effects)
+            if (slot.kind == EffectKind::plugin && ! slot.plugin.id.empty())
+                ++uses[slot.plugin.id];
+
+    for (auto& c : lastPublishedShape.song.channels)
+        if (c.instrument == InstrumentKind::plugin && uses[c.plugin.id] > 1)
+            c.plugin.id.clear();
+    for (auto& track : lastPublishedShape.song.mixer)
+        for (auto& slot : track.effects)
+            if (slot.kind == EffectKind::plugin && uses[slot.plugin.id] > 1)
+                slot.plugin.id.clear();
+
+    editSong (edit);
+}
+
 void AudioEngine::undo()
 {
     if (undoStack.empty())

+ 6 - 0
src/engine/AudioEngine.h

@@ -430,6 +430,12 @@ public:
     // (dragging a note, turning a knob) become a single step.
     void editSong (const std::function<void (Song&)>& edit, bool undoable = true);
 
+    // For an edit that moves or removes channels, mixer tracks or effect slots (issue #32): hosted plugins are reused
+    // by position, so the ones that end up elsewhere are built again from the Song's saved state. That state is only
+    // refreshed on save, so the live settings are copied in first (or a moved plugin came back with its parameters
+    // reset), and two instances of one plugin are never kept in each other's place.
+    void editSongStructural (const std::function<void (Song&)>& edit);
+
     // Changes since the last save/open (for the "*" in the title and the "save changes?" prompt).
     bool isModified() const noexcept { return modified; }
     void markSaved() noexcept { modified = false; sendChangeMessage(); }

+ 1 - 1
src/ui/ChannelRackComponent.cpp

@@ -1192,7 +1192,7 @@ bool ChannelRackComponent::moveSelected (int delta)
     bool moved = false;
     std::vector<int> after;
 
-    engine.editSong ([&] (Song& s) {
+    engine.editSongStructural ([&] (Song& s) {
         after = rows;
         moved = moveChannels (s, after, delta);
     });

+ 6 - 6
src/ui/MixerComponent.cpp

@@ -1246,7 +1246,7 @@ bool MixerStrips::moveSelected (int delta)
 {
     auto moved = selection;
     bool ok = false;
-    engine.editSong ([&] (Song& s) { ok = mixerops::moveTracks (s, moved, delta); });
+    engine.editSongStructural ([&] (Song& s) { ok = mixerops::moveTracks (s, moved, delta); });
 
     if (ok)
         setSelection (moved, juce::jlimit (1, (int) engine.getSong().mixer.size() - 1, selected + (delta < 0 ? -1 : 1)));
@@ -4060,7 +4060,7 @@ void EffectsPanel::mouseWheelMove (const juce::MouseEvent& e, const juce::MouseW
     {
         const auto to = slot + ((wheel.deltaY != 0.0f ? wheel.deltaY : -wheel.deltaX) > 0.0f ? -1 : 1);
         bool moved = false;
-        engine.editSong ([t = track, slot, to, &moved] (Song& song) { moved = mixerops::moveEffectSlot (song, t, slot, to); });
+        engine.editSongStructural ([t = track, slot, to, &moved] (Song& song) { moved = mixerops::moveEffectSlot (song, t, slot, to); });
         if (moved)
             selectedSlot = to;
         repaint();
@@ -4310,7 +4310,7 @@ void EffectsPanel::mouseUp (const juce::MouseEvent&)
     if (dragSlot >= 0 && dropSlot >= 0 && dropSlot != dragSlot)
     {
         bool moved = false;
-        engine.editSong ([t = track, from = dragSlot, to = dropSlot, &moved] (Song& song) { moved = mixerops::moveEffectSlot (song, t, from, to); });
+        engine.editSongStructural ([t = track, from = dragSlot, to = dropSlot, &moved] (Song& song) { moved = mixerops::moveEffectSlot (song, t, from, to); });
         if (moved)
             selectedSlot = dropSlot;
     }
@@ -4787,11 +4787,11 @@ void EffectsPanel::showSlotMenu (int slot)
     });
     menu.addSeparator();
     menu.addItem (tr ("Move up"), slot > 0, false, [this, slot] {
-        engine.editSong ([t = track, slot] (Song& song) { song.swapEffectSlots (t, slot, slot - 1); });
+        engine.editSongStructural ([t = track, slot] (Song& song) { song.swapEffectSlots (t, slot, slot - 1); });
         selectedSlot = slot - 1;
     });
     menu.addItem (tr ("Move down"), slot < count - 1, false, [this, slot] {
-        engine.editSong ([t = track, slot] (Song& song) { song.swapEffectSlots (t, slot, slot + 1); });
+        engine.editSongStructural ([t = track, slot] (Song& song) { song.swapEffectSlots (t, slot, slot + 1); });
         selectedSlot = slot + 1;
     });
     if ((*slots())[(std::size_t) slot].kind == EffectKind::plugin)
@@ -4849,7 +4849,7 @@ void EffectsPanel::deleteSlot (int slot)
     const auto* s = slots();
     if (s == nullptr || slot < 0 || slot >= (int) s->size())
         return;
-    engine.editSong ([t = track, slot] (Song& song) { song.removeEffectSlot (t, slot); });
+    engine.editSongStructural ([t = track, slot] (Song& song) { song.removeEffectSlot (t, slot); });
     selectedSlot = juce::jmin (slot, (int) slots()->size() - 1);
     repaint();
 }