Fixes #6786: Lv2Proc: Fix losing automation on export (#6944)

This PR is a about reloading an `Lv2Proc`, e.g. in case of a sample rate
change.

Prior to this PR, #6419 handled this by first saving the models into
XML, then destroying and re-initializing the whole `Lv2Proc` and finally
reloading the saved XML. However, #6786 shows that the automation is not
properly restored in such a case.

This PR thus attempts to not destroy the automatable models, just
everything else. This is done by moving `Lv2Proc::createPorts` into the
CTOR before calling `Lv2Proc::initPlugin`, which makes `initPlugin()`
and `shutdownPlugin()` proper inverses of each other (note that in jalv,
the ports are also created before the features are). The new class
`Lv2ProcSuspender` adds an RAII interface for reloading the `Lv2Proc`.

Note that another, possibly more clean approach would be to separate the
features and the plugin from the models ("controls"), to then only
destroy the features and the plugin. This could be done by having
`Lv2Effect` contain an `Lv2Proc` and `Lv2FxControls` contain an
`Lv2ProcControls`. Then the effect classes are the usual way round, and
you still maintain the separation between processor and controls in the
core LV2 code. (Similarly for the instrument, except we don't have a
processor/control split for instruments, so an instance of each class
would be contained within the same instrument instance.) - Thanks for
this proposal to @DomClark .
This commit is contained in:
Johannes Lorenz
2023-10-21 17:04:40 +02:00
committed by Johannes Lorenz
parent 379acb970b
commit 5c37aa2e2e
3 changed files with 72 additions and 18 deletions

View File

@@ -45,6 +45,7 @@
#include "Lv2Evbuf.h"
#include "MidiEvent.h"
#include "MidiEventToByteSeq.h"
#include "NoCopyNoMove.h"
namespace lmms
@@ -168,6 +169,27 @@ Plugin::Type Lv2Proc::check(const LilvPlugin *plugin,
class Lv2ProcSuspender : NoCopyNoMove
{
public:
Lv2ProcSuspender(Lv2Proc* proc)
: m_proc(proc)
, m_wasActive(proc->m_instance)
{
if (m_wasActive) { m_proc->shutdownPlugin(); }
}
~Lv2ProcSuspender()
{
if (m_wasActive) { m_proc->initPlugin(); }
}
private:
Lv2Proc* const m_proc;
const bool m_wasActive;
};
Lv2Proc::Lv2Proc(const LilvPlugin *plugin, Model* parent) :
LinkedModelGroup(parent),
m_plugin(plugin),
@@ -175,6 +197,7 @@ Lv2Proc::Lv2Proc(const LilvPlugin *plugin, Model* parent) :
m_midiInputBuf(m_maxMidiInputEvents),
m_midiInputReader(m_midiInputBuf)
{
createPorts();
initPlugin();
}
@@ -186,22 +209,7 @@ Lv2Proc::~Lv2Proc() { shutdownPlugin(); }
void Lv2Proc::reload()
{
// save controls, which we want to keep
QDomDocument doc;
QDomElement controls = doc.createElement("controls");
saveValues(doc, controls);
// backup construction variables
const LilvPlugin* plugin = m_plugin;
Model* parent = Model::parentModel();
// destroy everything using RAII ...
this->~Lv2Proc();
// ... and reuse it ("placement new")
new (this) Lv2Proc(plugin, parent);
// reload the controls
loadValues(controls);
}
void Lv2Proc::reload() { Lv2ProcSuspender(this); }
@@ -434,8 +442,6 @@ void Lv2Proc::initPlugin()
initPluginSpecificFeatures();
m_features.createFeatureVectors();
createPorts();
m_instance = lilv_plugin_instantiate(m_plugin,
Engine::audioEngine()->processingSampleRate(),
m_features.featurePointers());