Use sys::path and SessionDirectory in Session::create_session_file_from_template...
[ardour.git] / libs / ardour / ladspa_plugin.cc
index 98f098ef11143f1c8cc637287819e72b5f638dc2..5ca4a250cce5211a741b7ff3c7f76668f0ed590a 100644 (file)
@@ -17,6 +17,9 @@
 
 */
 
+#define __STDC_FORMAT_MACROS 1
+#include <inttypes.h>
+
 #include <vector>
 #include <string>
 
@@ -40,6 +43,7 @@
 #include <ardour/session.h>
 #include <ardour/audioengine.h>
 #include <ardour/ladspa_plugin.h>
+#include <ardour/buffer_set.h>
 
 #include <pbd/stl_delete.h>
 
@@ -116,7 +120,6 @@ 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))) {
-                       cerr << "plugin, port nr " << i << ", data = " << control_data[i] << endl;
                        connect_port (i, &control_data[i]);
                        
                        if (LADSPA_IS_PORT_OUTPUT(port_descriptor (i)) &&
@@ -133,8 +136,6 @@ LadspaPlugin::init (void *mod, uint32_t index, nframes_t rate)
                }
        }
 
-       Plugin::setup_controls ();
-
        latency_compute_run ();
 }
 
@@ -158,6 +159,33 @@ LadspaPlugin::~LadspaPlugin ()
        }
 }
 
+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)
+{
+       for (map<uint32_t,float>::iterator i = state.parameters.begin(); i != state.parameters.end(); ++i) {
+               set_parameter (i->first, i->second);
+       }
+}
+
 float
 LadspaPlugin::default_value (uint32_t port)
 {
@@ -288,11 +316,13 @@ LadspaPlugin::set_parameter (uint32_t which, float val)
 {
        if (which < descriptor->PortCount) {
                shadow_data[which] = (LADSPA_Data) val;
-               ParameterChanged (which, val); /* EMIT SIGNAL */
+#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"
@@ -461,20 +491,23 @@ 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 "??";
        }
 }
 
-nframes_t
-LadspaPlugin::latency () const
+ARDOUR::nframes_t
+LadspaPlugin::signal_latency () const
 {
+       if (_user_latency) {
+               return _user_latency;
+       }
+
        if (latency_control_port) {
                return (nframes_t) floor (*latency_control_port);
        } else {
@@ -482,16 +515,16 @@ LadspaPlugin::latency () const
        }
 }
 
-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));
                }
        }
 
@@ -499,26 +532,28 @@ LadspaPlugin::automatable () const
 }
 
 int
-LadspaPlugin::connect_and_run (vector<Sample*>& bufs, uint32_t nbufs, int32_t& in_index, int32_t& out_index, nframes_t nframes, 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;
+       uint32_t port_index = 0;
        cycles_t then, now;
 
-       port_index = 0;
-
        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))) {
-                               connect_port (port_index, bufs[min((uint32_t) in_index,nbufs - 1)] + offset);
+                               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))) {
-                               connect_port (port_index, bufs[min((uint32_t) out_index,nbufs - 1)] + offset);
+                               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++;
@@ -527,7 +562,7 @@ LadspaPlugin::connect_and_run (vector<Sample*>& bufs, uint32_t nbufs, int32_t& i
                port_index++;
        }
        
-       run (nframes);
+       run_in_place (nframes);
        now = get_cycles ();
        set_cycles ((uint32_t) (now - then));
 
@@ -571,7 +606,7 @@ LadspaPlugin::print_parameter (uint32_t param, char *buf, uint32_t len) const
 }
 
 void
-LadspaPlugin::run (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))) {
@@ -621,7 +656,7 @@ LadspaPlugin::latency_compute_run ()
                port_index++;
        }
        
-       run (bufsize);
+       run_in_place (bufsize);
        deactivate ();
 }