forward-port from 2.X commits 5827-6000 including
[ardour.git] / libs / ardour / ladspa_plugin.cc
index 348542ce1c99825ed9ffc2857f3ba6ef3e21f53d..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
@@ -102,7 +102,7 @@ LadspaPlugin::init (void *mod, uint32_t index, nframes_t rate)
                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) {
@@ -121,7 +121,7 @@ LadspaPlugin::init (void *mod, uint32_t index, nframes_t rate)
        for (i = 0; i < port_cnt; ++i) {
                if (LADSPA_IS_PORT_CONTROL(port_descriptor (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];
@@ -131,7 +131,7 @@ LadspaPlugin::init (void *mod, uint32_t index, nframes_t rate)
                        if (!LADSPA_IS_PORT_INPUT(port_descriptor (i))) {
                                continue;
                        }
-               
+
                        _shadow_data[i] = default_value (i);
                }
        }
@@ -144,8 +144,6 @@ LadspaPlugin::~LadspaPlugin ()
        deactivate ();
        cleanup ();
 
-       GoingAway (); /* EMIT SIGNAL */
-       
        /* XXX who should close a plugin? */
 
         // dlclose (module);
@@ -172,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;
@@ -228,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 {
@@ -242,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 {
@@ -256,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) {
@@ -268,12 +274,12 @@ 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) {
@@ -285,7 +291,7 @@ LadspaPlugin::default_value (uint32_t port)
        }
 
        return ret;
-}      
+}
 
 void
 LadspaPlugin::set_parameter (uint32_t which, float val)
@@ -299,7 +305,7 @@ LadspaPlugin::set_parameter (uint32_t which, float val)
                        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"
@@ -346,7 +352,7 @@ 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");
@@ -368,8 +374,12 @@ 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;
@@ -412,13 +422,58 @@ LadspaPlugin::set_state(const XMLNode& node)
        return 0;
 }
 
+int
+LadspaPlugin::set_state_2X (const XMLNode& node, int /* 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::get_parameter_descriptor (uint32_t which, ParameterDescriptor& desc) const
 {
        LADSPA_PortRangeHint prh;
 
        prh  = port_range_hints()[which];
-       
+
 
        if (LADSPA_IS_HINT_BOUNDED_BELOW(prh.HintDescriptor)) {
                desc.min_unbound = false;
@@ -431,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;
@@ -444,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;
@@ -455,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);
@@ -496,9 +551,9 @@ LadspaPlugin::automatable () const
        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(), Evoral::Parameter(PluginAutomation, 0, i));
                }
        }
@@ -514,18 +569,20 @@ LadspaPlugin::connect_and_run (BufferSet& bufs,
        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, 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, 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));
                        }
                }
        }
-       
+
        run_in_place (nframes);
        now = get_cycles ();
        set_cycles ((uint32_t) (now - then));
@@ -590,9 +647,9 @@ LadspaPlugin::latency_compute_run ()
        /* 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;
@@ -600,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))) {
@@ -619,7 +676,7 @@ LadspaPlugin::latency_compute_run ()
                }
                port_index++;
        }
-       
+
        run_in_place (bufsize);
        deactivate ();
 }
@@ -644,5 +701,10 @@ LadspaPluginInfo::load (Session& session)
 
        catch (failed_constructor &err) {
                return PluginPtr ((Plugin*) 0);
-       }       
+       }
+}
+
+LadspaPluginInfo::LadspaPluginInfo()
+{
+       type = ARDOUR::LADSPA;
 }