Merge with 2.0-ongoing R2885.
[ardour.git] / libs / ardour / ladspa_plugin.cc
index 82cc4e620269e4e5bd7ec9588b0d1131cd615b39..ccc12f8cf8a394dc80fd1a5c3718e2d5f2b52aad 100644 (file)
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id$
 */
 
+#define __STDC_FORMAT_MACROS 1
+#include <inttypes.h>
+
 #include <vector>
 #include <string>
 
@@ -32,7 +34,6 @@
 
 #include <pbd/compose.h>
 #include <pbd/error.h>
-#include <pbd/pathscanner.h>
 #include <pbd/xml++.h>
 
 #include <midi++/manager.h>
@@ -52,7 +53,7 @@ using namespace std;
 using namespace ARDOUR;
 using namespace PBD;
 
-LadspaPlugin::LadspaPlugin (void *mod, AudioEngine& e, Session& session, uint32_t index, jack_nframes_t rate)
+LadspaPlugin::LadspaPlugin (void *mod, AudioEngine& e, Session& session, uint32_t index, nframes_t rate)
        : Plugin (e, session)
 {
        init (mod, index, rate);
@@ -61,81 +62,79 @@ 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];
        }
 }
 
 void
-LadspaPlugin::init (void *mod, uint32_t index, jack_nframes_t rate)
+LadspaPlugin::init (void *mod, uint32_t index, nframes_t rate)
 {
        LADSPA_Descriptor_Function dfunc;
        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;
+       _sample_rate = rate;
 
-       if (descriptor->instantiate == 0) {
+       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);
                }
        }
 
-       Plugin::setup_controls ();
-
        latency_compute_run ();
 }
 
@@ -150,40 +149,21 @@ LadspaPlugin::~LadspaPlugin ()
 
         // dlclose (module);
 
-       if (control_data) {
-               delete [] control_data;
+       if (_control_data) {
+               delete [] _control_data;
        }
 
-       if (shadow_data) {
-               delete [] shadow_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);
-               }
-       }
-}
-
-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
@@ -193,6 +173,7 @@ LadspaPlugin::default_value (uint32_t port)
        float ret = 0.0f;
        bool bounds_given = false;
        bool sr_scaling = false;
+       bool earlier_hint = false;
 
        /* defaults - case 1 */
        
@@ -201,6 +182,7 @@ LadspaPlugin::default_value (uint32_t port)
                        ret = prh[port].LowerBound;
                        bounds_given = true;
                        sr_scaling = true;
+                       earlier_hint = true;
                }
                
                /* FIXME: add support for logarithmic defaults */
@@ -209,33 +191,41 @@ LadspaPlugin::default_value (uint32_t port)
                        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;
                        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;
                        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;
+                       earlier_hint = true;
                }
                else if (LADSPA_IS_HINT_DEFAULT_1(prh[port].HintDescriptor)) {
                        ret = 1.0f;
+                       earlier_hint = true;
                }
                else if (LADSPA_IS_HINT_DEFAULT_100(prh[port].HintDescriptor)) {
                        ret = 100.0f;
+                       earlier_hint = true;
                }
                else if (LADSPA_IS_HINT_DEFAULT_440(prh[port].HintDescriptor)) {
                        ret = 440.0f;
+                       earlier_hint = true;
                }
                else {
                        /* no hint found */
@@ -288,13 +278,13 @@ LadspaPlugin::default_value (uint32_t port)
        
        /* defaults - case 5 */
                
-       if (LADSPA_IS_HINT_SAMPLE_RATE(prh[port].HintDescriptor)) {
+       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;
                }
        }
 
@@ -304,13 +294,15 @@ LadspaPlugin::default_value (uint32_t port)
 void
 LadspaPlugin::set_parameter (uint32_t which, float val)
 {
-       if (which < descriptor->PortCount) {
-               shadow_data[which] = (LADSPA_Data) val;
-               ParameterChanged (which, val); /* EMIT SIGNAL */
+       if (which < _descriptor->PortCount) {
+               _shadow_data[which] = (LADSPA_Data) val;
+#if 0
+               ParameterChanged (Parameter(PluginAutomation, 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"
@@ -324,9 +316,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];
        }
 }
 
@@ -337,7 +329,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;
@@ -364,7 +356,7 @@ LadspaPlugin::get_state()
                        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);
                }
@@ -479,37 +471,40 @@ LadspaPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& des
        return 0;
 }
 
-
 string
-LadspaPlugin::describe_parameter (uint32_t which)
+LadspaPlugin::describe_parameter (Parameter which)
 {
-       if (which < parameter_count()) {
-               return port_names()[which];
+       if (which.type() == PluginAutomation && which.id() < parameter_count()) {
+               return port_names()[which.id()];
        } else {
                return "??";
        }
 }
 
-jack_nframes_t
-LadspaPlugin::latency () const
+ARDOUR::nframes_t
+LadspaPlugin::signal_latency () const
 {
-       if (latency_control_port) {
-               return (jack_nframes_t) floor (*latency_control_port);
+       if (_user_latency) {
+               return _user_latency;
+       }
+
+       if (_latency_control_port) {
+               return (nframes_t) floor (*_latency_control_port);
        } else {
                return 0;
        }
 }
 
-set<uint32_t>
+set<Parameter>
 LadspaPlugin::automatable () const
 {
-       set<uint32_t> ret;
+       set<Parameter> ret;
 
        for (uint32_t i = 0; i < parameter_count(); ++i){
                if (LADSPA_IS_PORT_INPUT(port_descriptor (i)) && 
                    LADSPA_IS_PORT_CONTROL(port_descriptor (i))){
                        
-                       ret.insert (ret.end(), i);
+                       ret.insert (ret.end(), Parameter(PluginAutomation, i));
                }
        }
 
@@ -517,14 +512,14 @@ LadspaPlugin::automatable () const
 }
 
 int
-LadspaPlugin::connect_and_run (BufferSet& bufs, uint32_t& in_index, uint32_t& out_index, jack_nframes_t nframes, jack_nframes_t offset)
+LadspaPlugin::connect_and_run (BufferSet& bufs, uint32_t& in_index, uint32_t& out_index, nframes_t nframes, nframes_t offset)
 {
        uint32_t port_index = 0;
        cycles_t then, now;
 
        then = get_cycles ();
 
-       const uint32_t nbufs = bufs.count().get(DataType::AUDIO);
+       const uint32_t nbufs = bufs.count().n_audio();
 
        while (port_index < parameter_count()) {
                if (LADSPA_IS_PORT_AUDIO (port_descriptor(port_index))) {
@@ -547,7 +542,7 @@ LadspaPlugin::connect_and_run (BufferSet& bufs, uint32_t& in_index, uint32_t& ou
                port_index++;
        }
        
-       run (nframes);
+       run_in_place (nframes);
        now = get_cycles ();
        set_cycles ((uint32_t) (now - then));
 
@@ -591,20 +586,20 @@ LadspaPlugin::print_parameter (uint32_t param, char *buf, uint32_t len) const
 }
 
 void
-LadspaPlugin::run (jack_nframes_t nframes)
+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;
        }
 
@@ -617,7 +612,7 @@ LadspaPlugin::latency_compute_run ()
        uint32_t port_index = 0;
        uint32_t in_index = 0;
        uint32_t out_index = 0;
-       const jack_nframes_t bufsize = 1024;
+       const nframes_t bufsize = 1024;
        LADSPA_Data buffer[bufsize];
 
        memset(buffer,0,sizeof(LADSPA_Data)*bufsize);
@@ -641,7 +636,7 @@ LadspaPlugin::latency_compute_run ()
                port_index++;
        }
        
-       run (bufsize);
+       run_in_place (bufsize);
        deactivate ();
 }