forward-port from 2.X commits 5827-6000 including
[ardour.git] / libs / ardour / ladspa_plugin.cc
index 5ca4a250cce5211a741b7ff3c7f76668f0ed590a..81cdff9618567740ef011cfab35fb7817fd52245 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2000-2006 Paul Davis 
+    Copyright (C) 2000-2006 Paul Davis
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
 
 #include <lrdf.h>
 
-#include <pbd/compose.h>
-#include <pbd/error.h>
-#include <pbd/pathscanner.h>
-#include <pbd/xml++.h>
+#include "pbd/compose.h"
+#include "pbd/error.h"
+#include "pbd/xml++.h"
 
-#include <midi++/manager.h>
+#include "midi++/manager.h"
 
-#include <ardour/ardour.h>
-#include <ardour/session.h>
-#include <ardour/audioengine.h>
-#include <ardour/ladspa_plugin.h>
-#include <ardour/buffer_set.h>
+#include "ardour/ardour.h"
+#include "ardour/session.h"
+#include "ardour/audioengine.h"
+#include "ardour/ladspa_plugin.h"
+#include "ardour/buffer_set.h"
+#include "ardour/audio_buffer.h"
 
-#include <pbd/stl_delete.h>
+#include "pbd/stl_delete.h"
 
 #include "i18n.h"
 #include <locale.h>
@@ -63,11 +63,11 @@ LadspaPlugin::LadspaPlugin (void *mod, AudioEngine& e, Session& session, uint32_
 LadspaPlugin::LadspaPlugin (const LadspaPlugin &other)
        : Plugin (other)
 {
-       init (other.module, other._index, other.sample_rate);
+       init (other._module, other._index, other._sample_rate);
 
        for (uint32_t i = 0; i < parameter_count(); ++i) {
-               control_data[i] = other.shadow_data[i];
-               shadow_data[i] = other.shadow_data[i];
+               _control_data[i] = other._shadow_data[i];
+               _shadow_data[i] = other._shadow_data[i];
        }
 }
 
@@ -78,61 +78,61 @@ LadspaPlugin::init (void *mod, uint32_t index, nframes_t rate)
        uint32_t i, port_cnt;
        const char *errstr;
 
-       module = mod;
-       control_data = 0;
-       shadow_data = 0;
-       latency_control_port = 0;
-       was_activated = false;
+       _module = mod;
+       _control_data = 0;
+       _shadow_data = 0;
+       _latency_control_port = 0;
+       _was_activated = false;
 
-       dfunc = (LADSPA_Descriptor_Function) dlsym (module, "ladspa_descriptor");
+       dfunc = (LADSPA_Descriptor_Function) dlsym (_module, "ladspa_descriptor");
 
        if ((errstr = dlerror()) != NULL) {
                error << _("LADSPA: module has no descriptor function.") << endmsg;
                throw failed_constructor();
        }
 
-       if ((descriptor = dfunc (index)) == 0) {
+       if ((_descriptor = dfunc (index)) == 0) {
                error << _("LADSPA: plugin has gone away since discovery!") << endmsg;
                throw failed_constructor();
        }
 
        _index = index;
 
-       if (LADSPA_IS_INPLACE_BROKEN(descriptor->Properties)) {
-               error << string_compose(_("LADSPA: \"%1\" cannot be used, since it cannot do inplace processing"), descriptor->Name) << endmsg;
+       if (LADSPA_IS_INPLACE_BROKEN(_descriptor->Properties)) {
+               error << string_compose(_("LADSPA: \"%1\" cannot be used, since it cannot do inplace processing"), _descriptor->Name) << endmsg;
                throw failed_constructor();
        }
-       
-       sample_rate = rate;
 
-       if (descriptor->instantiate == 0) {
+       _sample_rate = rate;
+
+       if (_descriptor->instantiate == 0) {
                throw failed_constructor();
        }
 
-       if ((handle = descriptor->instantiate (descriptor, rate)) == 0) {
+       if ((_handle = _descriptor->instantiate (_descriptor, rate)) == 0) {
                throw failed_constructor();
        }
 
        port_cnt = parameter_count();
 
-       control_data = new LADSPA_Data[port_cnt];
-       shadow_data = new LADSPA_Data[port_cnt];
+       _control_data = new LADSPA_Data[port_cnt];
+       _shadow_data = new LADSPA_Data[port_cnt];
 
        for (i = 0; i < port_cnt; ++i) {
                if (LADSPA_IS_PORT_CONTROL(port_descriptor (i))) {
-                       connect_port (i, &control_data[i]);
-                       
+                       connect_port (i, &_control_data[i]);
+
                        if (LADSPA_IS_PORT_OUTPUT(port_descriptor (i)) &&
                            strcmp (port_names()[i], X_("latency")) == 0) {
-                               latency_control_port = &control_data[i];
-                               *latency_control_port = 0;
+                               _latency_control_port = &_control_data[i];
+                               *_latency_control_port = 0;
                        }
 
                        if (!LADSPA_IS_PORT_INPUT(port_descriptor (i))) {
                                continue;
                        }
-               
-                       shadow_data[i] = default_value (i);
+
+                       _shadow_data[i] = default_value (i);
                }
        }
 
@@ -144,46 +144,20 @@ LadspaPlugin::~LadspaPlugin ()
        deactivate ();
        cleanup ();
 
-       GoingAway (); /* EMIT SIGNAL */
-       
        /* XXX who should close a plugin? */
 
         // dlclose (module);
 
-       if (control_data) {
-               delete [] control_data;
-       }
-
-       if (shadow_data) {
-               delete [] shadow_data;
-       }
-}
-
-void
-LadspaPlugin::store_state (PluginState& state)
-{
-       state.parameters.clear ();
-       
-       for (uint32_t i = 0; i < parameter_count(); ++i){
-
-               if (LADSPA_IS_PORT_INPUT(port_descriptor (i)) && 
-                   LADSPA_IS_PORT_CONTROL(port_descriptor (i))){
-                       pair<uint32_t,float> datum;
-
-                       datum.first = i;
-                       datum.second = shadow_data[i];
-
-                       state.parameters.insert (datum);
-               }
-       }
+       delete [] _control_data;
+       delete [] _shadow_data;
 }
 
-void
-LadspaPlugin::restore_state (PluginState& state)
+string
+LadspaPlugin::unique_id() const
 {
-       for (map<uint32_t,float>::iterator i = state.parameters.begin(); i != state.parameters.end(); ++i) {
-               set_parameter (i->first, i->second);
-       }
+       char buf[32];
+       snprintf (buf, sizeof (buf), "%lu", _descriptor->UniqueID);
+       return string (buf);
 }
 
 float
@@ -196,40 +170,48 @@ LadspaPlugin::default_value (uint32_t port)
        bool earlier_hint = false;
 
        /* defaults - case 1 */
-       
+
        if (LADSPA_IS_HINT_HAS_DEFAULT(prh[port].HintDescriptor)) {
                if (LADSPA_IS_HINT_DEFAULT_MINIMUM(prh[port].HintDescriptor)) {
                        ret = prh[port].LowerBound;
                        bounds_given = true;
                        sr_scaling = true;
-                       earlier_hint = true;
                }
-               
-               /* FIXME: add support for logarithmic defaults */
-               
+
                else if (LADSPA_IS_HINT_DEFAULT_LOW(prh[port].HintDescriptor)) {
-                       ret = prh[port].LowerBound * 0.75f + prh[port].UpperBound * 0.25f;
+                       if (LADSPA_IS_HINT_LOGARITHMIC(prh[port].HintDescriptor)) {
+                               ret = exp(log(prh[port].LowerBound) * 0.75f + log(prh[port].UpperBound) * 0.25f);
+                       }
+                       else {
+                               ret = prh[port].LowerBound * 0.75f + prh[port].UpperBound * 0.25f;
+                       }
                        bounds_given = true;
                        sr_scaling = true;
-                       earlier_hint = true;
                }
                else if (LADSPA_IS_HINT_DEFAULT_MIDDLE(prh[port].HintDescriptor)) {
-                       ret = prh[port].LowerBound * 0.50f + prh[port].UpperBound * 0.50f;
+                       if (LADSPA_IS_HINT_LOGARITHMIC(prh[port].HintDescriptor)) {
+                               ret = exp(log(prh[port].LowerBound) * 0.5f + log(prh[port].UpperBound) * 0.5f);
+                       }
+                       else {
+                               ret = prh[port].LowerBound * 0.5f + prh[port].UpperBound * 0.5f;
+                       }
                        bounds_given = true;
                        sr_scaling = true;
-                       earlier_hint = true;
                }
                else if (LADSPA_IS_HINT_DEFAULT_HIGH(prh[port].HintDescriptor)) {
-                       ret = prh[port].LowerBound * 0.25f + prh[port].UpperBound * 0.75f;
+                       if (LADSPA_IS_HINT_LOGARITHMIC(prh[port].HintDescriptor)) {
+                               ret = exp(log(prh[port].LowerBound) * 0.25f + log(prh[port].UpperBound) * 0.75f);
+                       }
+                       else {
+                               ret = prh[port].LowerBound * 0.25f + prh[port].UpperBound * 0.75f;
+                       }
                        bounds_given = true;
                        sr_scaling = true;
-                       earlier_hint = true;
                }
                else if (LADSPA_IS_HINT_DEFAULT_MAXIMUM(prh[port].HintDescriptor)) {
                        ret = prh[port].UpperBound;
                        bounds_given = true;
                        sr_scaling = true;
-                       earlier_hint = true;
                }
                else if (LADSPA_IS_HINT_DEFAULT_0(prh[port].HintDescriptor)) {
                        ret = 0.0f;
@@ -252,11 +234,11 @@ LadspaPlugin::default_value (uint32_t port)
                        ret = 0.0f;
                }
        }
-       
+
        /* defaults - case 2 */
        else if (LADSPA_IS_HINT_BOUNDED_BELOW(prh[port].HintDescriptor) &&
                 !LADSPA_IS_HINT_BOUNDED_ABOVE(prh[port].HintDescriptor)) {
-               
+
                if (prh[port].LowerBound < 0) {
                        ret = 0.0f;
                } else {
@@ -266,11 +248,11 @@ LadspaPlugin::default_value (uint32_t port)
                bounds_given = true;
                sr_scaling = true;
        }
-       
+
        /* defaults - case 3 */
        else if (!LADSPA_IS_HINT_BOUNDED_BELOW(prh[port].HintDescriptor) &&
                 LADSPA_IS_HINT_BOUNDED_ABOVE(prh[port].HintDescriptor)) {
-               
+
                if (prh[port].UpperBound > 0) {
                        ret = 0.0f;
                } else {
@@ -280,11 +262,11 @@ LadspaPlugin::default_value (uint32_t port)
                bounds_given = true;
                sr_scaling = true;
        }
-       
+
        /* defaults - case 4 */
        else if (LADSPA_IS_HINT_BOUNDED_BELOW(prh[port].HintDescriptor) &&
                 LADSPA_IS_HINT_BOUNDED_ABOVE(prh[port].HintDescriptor)) {
-               
+
                if (prh[port].LowerBound < 0 && prh[port].UpperBound > 0) {
                        ret = 0.0f;
                } else if (prh[port].LowerBound < 0 && prh[port].UpperBound < 0) {
@@ -292,38 +274,38 @@ LadspaPlugin::default_value (uint32_t port)
                } else {
                        ret = prh[port].LowerBound;
                }
-               bounds_given = true;    
+               bounds_given = true;
                sr_scaling = true;
        }
-       
+
        /* defaults - case 5 */
-               
+
        if (LADSPA_IS_HINT_SAMPLE_RATE(prh[port].HintDescriptor) && !earlier_hint) {
                if (bounds_given) {
                        if (sr_scaling) {
-                               ret *= sample_rate;
+                               ret *= _sample_rate;
                        }
                } else {
-                       ret = sample_rate;
+                       ret = _sample_rate;
                }
        }
 
        return ret;
-}      
+}
 
 void
 LadspaPlugin::set_parameter (uint32_t which, float val)
 {
-       if (which < descriptor->PortCount) {
-               shadow_data[which] = (LADSPA_Data) val;
+       if (which < _descriptor->PortCount) {
+               _shadow_data[which] = (LADSPA_Data) val;
 #if 0
-               ParameterChanged (Parameter(PluginAutomation, which), val); /* EMIT SIGNAL */
+               ParameterChanged (Parameter(PluginAutomation, 0, which), val); /* EMIT SIGNAL */
 
                if (which < parameter_count() && controls[which]) {
                        controls[which]->Changed ();
                }
 #endif
-               
+
        } else {
                warning << string_compose (_("illegal parameter number used with plugin \"%1\". This may"
                                             "indicate a change in the plugin design, and presets may be"
@@ -336,9 +318,9 @@ float
 LadspaPlugin::get_parameter (uint32_t which) const
 {
        if (LADSPA_IS_PORT_INPUT(port_descriptor (which))) {
-               return (float) shadow_data[which];
+               return (float) _shadow_data[which];
        } else {
-               return (float) control_data[which];
+               return (float) _control_data[which];
        }
 }
 
@@ -349,7 +331,7 @@ LadspaPlugin::nth_parameter (uint32_t n, bool& ok) const
 
        ok = false;
 
-       for (c = 0, x = 0; x < descriptor->PortCount; ++x) {
+       for (c = 0, x = 0; x < _descriptor->PortCount; ++x) {
                if (LADSPA_IS_PORT_CONTROL (port_descriptor (x))) {
                        if (c++ == n) {
                                ok = true;
@@ -370,13 +352,13 @@ LadspaPlugin::get_state()
 
        for (uint32_t i = 0; i < parameter_count(); ++i){
 
-               if (LADSPA_IS_PORT_INPUT(port_descriptor (i)) && 
+               if (LADSPA_IS_PORT_INPUT(port_descriptor (i)) &&
                    LADSPA_IS_PORT_CONTROL(port_descriptor (i))){
 
-                       child = new XMLNode("port");
+                       child = new XMLNode("Port");
                        snprintf(buf, sizeof(buf), "%u", i);
                        child->add_property("number", string(buf));
-                       snprintf(buf, sizeof(buf), "%+f", shadow_data[i]);
+                       snprintf(buf, sizeof(buf), "%+f", _shadow_data[i]);
                        child->add_property("value", string(buf));
                        root->add_child_nocopy (*child);
                }
@@ -392,7 +374,56 @@ LadspaPlugin::save_preset (string name)
 }
 
 int
-LadspaPlugin::set_state(const XMLNode& node)
+LadspaPlugin::set_state (const XMLNode& node, int version)
+{
+       if (version < 3000) {
+               return set_state_2X (node, version);
+       }
+       
+       XMLNodeList nodes;
+       XMLProperty *prop;
+       XMLNodeConstIterator iter;
+       XMLNode *child;
+       const char *port;
+       const char *data;
+       uint32_t port_id;
+       LocaleGuard lg (X_("POSIX"));
+
+       if (node.name() != state_node_name()) {
+               error << _("Bad node sent to LadspaPlugin::set_state") << endmsg;
+               return -1;
+       }
+
+       nodes = node.children ("Port");
+
+       for(iter = nodes.begin(); iter != nodes.end(); ++iter){
+
+               child = *iter;
+
+               if ((prop = child->property("number")) != 0) {
+                       port = prop->value().c_str();
+               } else {
+                       warning << _("LADSPA: no ladspa port number") << endmsg;
+                       continue;
+               }
+               if ((prop = child->property("value")) != 0) {
+                       data = prop->value().c_str();
+               } else {
+                       warning << _("LADSPA: no ladspa port data") << endmsg;
+                       continue;
+               }
+
+               sscanf (port, "%" PRIu32, &port_id);
+               set_parameter (port_id, atof(data));
+       }
+
+       latency_compute_run ();
+
+       return 0;
+}
+
+int
+LadspaPlugin::set_state_2X (const XMLNode& node, int /* version */)
 {
        XMLNodeList nodes;
        XMLProperty *prop;
@@ -442,7 +473,7 @@ LadspaPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& des
        LADSPA_PortRangeHint prh;
 
        prh  = port_range_hints()[which];
-       
+
 
        if (LADSPA_IS_HINT_BOUNDED_BELOW(prh.HintDescriptor)) {
                desc.min_unbound = false;
@@ -455,7 +486,7 @@ LadspaPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& des
                desc.min_unbound = true;
                desc.lower = 0;
        }
-       
+
 
        if (LADSPA_IS_HINT_BOUNDED_ABOVE(prh.HintDescriptor)) {
                desc.max_unbound = false;
@@ -468,7 +499,7 @@ LadspaPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& des
                desc.max_unbound = true;
                desc.upper = 4; /* completely arbitrary */
        }
-       
+
        if (LADSPA_IS_HINT_INTEGER (prh.HintDescriptor)) {
                desc.step = 1.0;
                desc.smallstep = 0.1;
@@ -479,7 +510,7 @@ LadspaPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& des
                desc.smallstep = delta / 10000.0f;
                desc.largestep = delta/10.0f;
        }
-       
+
        desc.toggled = LADSPA_IS_HINT_TOGGLED (prh.HintDescriptor);
        desc.logarithmic = LADSPA_IS_HINT_LOGARITHMIC (prh.HintDescriptor);
        desc.sr_dependent = LADSPA_IS_HINT_SAMPLE_RATE (prh.HintDescriptor);
@@ -487,12 +518,11 @@ LadspaPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& des
 
        desc.label = port_names()[which];
 
-
        return 0;
 }
 
 string
-LadspaPlugin::describe_parameter (Parameter which)
+LadspaPlugin::describe_parameter (Evoral::Parameter which)
 {
        if (which.type() == PluginAutomation && which.id() < parameter_count()) {
                return port_names()[which.id()];
@@ -508,23 +538,23 @@ LadspaPlugin::signal_latency () const
                return _user_latency;
        }
 
-       if (latency_control_port) {
-               return (nframes_t) floor (*latency_control_port);
+       if (_latency_control_port) {
+               return (nframes_t) floor (*_latency_control_port);
        } else {
                return 0;
        }
 }
 
-set<Parameter>
+set<Evoral::Parameter>
 LadspaPlugin::automatable () const
 {
-       set<Parameter> ret;
+       set<Evoral::Parameter> ret;
 
        for (uint32_t i = 0; i < parameter_count(); ++i){
-               if (LADSPA_IS_PORT_INPUT(port_descriptor (i)) && 
+               if (LADSPA_IS_PORT_INPUT(port_descriptor (i)) &&
                    LADSPA_IS_PORT_CONTROL(port_descriptor (i))){
-                       
-                       ret.insert (ret.end(), Parameter(PluginAutomation, i));
+
+                       ret.insert (ret.end(), Evoral::Parameter(PluginAutomation, 0, i));
                }
        }
 
@@ -532,36 +562,27 @@ LadspaPlugin::automatable () const
 }
 
 int
-LadspaPlugin::connect_and_run (BufferSet& bufs, uint32_t& in_index, uint32_t& out_index, nframes_t nframes, nframes_t offset)
+LadspaPlugin::connect_and_run (BufferSet& bufs,
+               ChanMapping in_map, ChanMapping out_map,
+               nframes_t nframes, nframes_t offset)
 {
-       uint32_t port_index = 0;
-       cycles_t then, now;
-
-       then = get_cycles ();
-
-       const uint32_t nbufs = bufs.count().n_audio();
-
-       while (port_index < parameter_count()) {
-               if (LADSPA_IS_PORT_AUDIO (port_descriptor(port_index))) {
-                       if (LADSPA_IS_PORT_INPUT (port_descriptor(port_index))) {
-                               const size_t index = min(in_index, nbufs - 1);
-                               connect_port (port_index, bufs.get_audio(index).data(nframes, offset));
-                               //cerr << this << ' ' << name() << " @ " << offset << " inport " << in_index << " = buf " 
-                               //     << min((uint32_t)in_index,nbufs) << " = " << &bufs[min((uint32_t)in_index,nbufs)][offset] << endl;
-                               in_index++;
-
-
-                       } else if (LADSPA_IS_PORT_OUTPUT (port_descriptor (port_index))) {
-                               const size_t index = min(out_index,nbufs - 1);
-                               connect_port (port_index, bufs.get_audio(index).data(nframes, offset));
-                               // cerr << this << ' ' << name() << " @ " << offset << " outport " << out_index << " = buf " 
-                               //     << min((uint32_t)out_index,nbufs) << " = " << &bufs[min((uint32_t)out_index,nbufs)][offset] << endl;
-                               out_index++;
+       cycles_t now;
+       cycles_t then = get_cycles ();
+
+       uint32_t audio_in_index  = 0;
+       uint32_t audio_out_index = 0;
+       for (uint32_t port_index = 0; port_index < parameter_count(); ++port_index) {
+               if (LADSPA_IS_PORT_AUDIO(port_descriptor(port_index))) {
+                       if (LADSPA_IS_PORT_INPUT(port_descriptor(port_index))) {
+                               const uint32_t buf_index = in_map.get(DataType::AUDIO, audio_in_index++);
+                               connect_port(port_index, bufs.get_audio(buf_index).data(offset));
+                       } else if (LADSPA_IS_PORT_OUTPUT(port_descriptor(port_index))) {
+                               const uint32_t buf_index = out_map.get(DataType::AUDIO, audio_out_index++);
+                               connect_port(port_index, bufs.get_audio(buf_index).data(offset));
                        }
                }
-               port_index++;
        }
-       
+
        run_in_place (nframes);
        now = get_cycles ();
        set_cycles ((uint32_t) (now - then));
@@ -610,25 +631,25 @@ LadspaPlugin::run_in_place (nframes_t nframes)
 {
        for (uint32_t i = 0; i < parameter_count(); ++i) {
                if (LADSPA_IS_PORT_INPUT(port_descriptor (i)) && LADSPA_IS_PORT_CONTROL(port_descriptor (i))) {
-                       control_data[i] = shadow_data[i];
+                       _control_data[i] = _shadow_data[i];
                }
        }
-       descriptor->run (handle, nframes);
+       _descriptor->run (_handle, nframes);
 }
 
 void
 LadspaPlugin::latency_compute_run ()
 {
-       if (!latency_control_port) {
+       if (!_latency_control_port) {
                return;
        }
 
        /* we need to run the plugin so that it can set its latency
           parameter.
        */
-       
+
        activate ();
-       
+
        uint32_t port_index = 0;
        uint32_t in_index = 0;
        uint32_t out_index = 0;
@@ -636,13 +657,13 @@ LadspaPlugin::latency_compute_run ()
        LADSPA_Data buffer[bufsize];
 
        memset(buffer,0,sizeof(LADSPA_Data)*bufsize);
-               
+
        /* Note that we've already required that plugins
           be able to handle in-place processing.
        */
-       
+
        port_index = 0;
-       
+
        while (port_index < parameter_count()) {
                if (LADSPA_IS_PORT_AUDIO (port_descriptor (port_index))) {
                        if (LADSPA_IS_PORT_INPUT (port_descriptor (port_index))) {
@@ -655,7 +676,7 @@ LadspaPlugin::latency_compute_run ()
                }
                port_index++;
        }
-       
+
        run_in_place (bufsize);
        deactivate ();
 }
@@ -680,5 +701,10 @@ LadspaPluginInfo::load (Session& session)
 
        catch (failed_constructor &err) {
                return PluginPtr ((Plugin*) 0);
-       }       
+       }
+}
+
+LadspaPluginInfo::LadspaPluginInfo()
+{
+       type = ARDOUR::LADSPA;
 }