add display name to processors (so that it can differ from the "real name"); pay...
[ardour.git] / libs / ardour / plugin_insert.cc
index e199dbcec5be2b5a9dadec915bb190bc19585059..461a3f0685986a819ac5dbd0ce2ba8c4c23bbcc5 100644 (file)
 
 */
 
+#ifdef WAF_BUILD
+#include "libardour-config.h"
+#endif
+
 #include <string>
 
 #include <sigc++/bind.h>
 
-#include <pbd/failed_constructor.h>
-#include <pbd/xml++.h>
+#include "pbd/failed_constructor.h"
+#include "pbd/xml++.h"
 
-#include <ardour/plugin_insert.h>
-#include <ardour/plugin.h>
-#include <ardour/port.h>
-#include <ardour/route.h>
-#include <ardour/ladspa_plugin.h>
-#include <ardour/buffer_set.h>
-#include <ardour/automation_list.h>
+#include "ardour/audio_buffer.h"
+#include "ardour/automation_list.h"
+#include "ardour/buffer_set.h"
+#include "ardour/event_type_map.h"
+#include "ardour/ladspa_plugin.h"
+#include "ardour/plugin.h"
+#include "ardour/plugin_insert.h"
+#include "ardour/port.h"
+#include "ardour/route.h"
 
 #ifdef HAVE_SLV2
-#include <ardour/lv2_plugin.h>
+#include "ardour/lv2_plugin.h"
 #endif
 
 #ifdef VST_SUPPORT
-#include <ardour/vst_plugin.h>
+#include "ardour/vst_plugin.h"
 #endif
 
 #ifdef HAVE_AUDIOUNITS
-#include <ardour/audio_unit.h>
+#include "ardour/audio_unit.h"
 #endif
 
-#include <ardour/audioengine.h>
-#include <ardour/session.h>
-#include <ardour/types.h>
+#include "ardour/audioengine.h"
+#include "ardour/session.h"
+#include "ardour/types.h"
 
 #include "i18n.h"
 
@@ -56,10 +62,10 @@ using namespace PBD;
 
 const string PluginInsert::port_automation_node_name = "PortAutomation";
 
-PluginInsert::PluginInsert (Session& s, boost::shared_ptr<Plugin> plug, Placement placement)
-       : Processor (s, plug->name(), placement),
-          _signal_analysis_collected_nframes(0),
-          _signal_analysis_collect_nframes_max(0)
+PluginInsert::PluginInsert (Session& s, boost::shared_ptr<Plugin> plug)
+       : Processor (s, plug->name())
+       , _signal_analysis_collected_nframes(0)
+       , _signal_analysis_collect_nframes_max(0)
 {
        /* the first is the master */
 
@@ -76,7 +82,7 @@ PluginInsert::PluginInsert (Session& s, boost::shared_ptr<Plugin> plug, Placemen
 }
 
 PluginInsert::PluginInsert (Session& s, const XMLNode& node)
-       : Processor (s, "unnamed plugin insert", PreFader),
+       : Processor (s, "unnamed plugin insert"),
           _signal_analysis_collected_nframes(0),
           _signal_analysis_collect_nframes_max(0)
 {
@@ -95,23 +101,6 @@ PluginInsert::PluginInsert (Session& s, const XMLNode& node)
        }
 }
 
-PluginInsert::PluginInsert (const PluginInsert& other)
-       : Processor (other._session, other._name, other.placement()),
-          _signal_analysis_collected_nframes(0),
-          _signal_analysis_collect_nframes_max(0)
-{
-       uint32_t count = other._plugins.size();
-
-       /* make as many copies as requested */
-       for (uint32_t n = 0; n < count; ++n) {
-               _plugins.push_back (plugin_factory (other.plugin (n)));
-       }
-
-       init ();
-
-       ProcessorCreated (this); /* EMIT SIGNAL */
-}
-
 bool
 PluginInsert::set_count (uint32_t num)
 {
@@ -175,14 +164,10 @@ PluginInsert::output_streams() const
        ChanCount out = _plugins.front()->get_info()->n_outputs;
 
        if (out == ChanCount::INFINITE) {
-
                return _plugins.front()->output_streams ();
-
        } else {
-
                out.set_audio (out.n_audio() * _plugins.size());
                out.set_midi (out.n_midi() * _plugins.size());
-
                return out;
        }
 }
@@ -197,7 +182,6 @@ PluginInsert::input_streams() const
        } else {
                in.set_audio (in.n_audio() * _plugins.size());
                in.set_midi (in.n_midi() * _plugins.size());
-
                return in;
        }
 }
@@ -249,7 +233,7 @@ PluginInsert::parameter_changed (Evoral::Parameter which, float val)
        if (which.type() != PluginAutomation)
                return;
 
-       vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin();
+       Plugins::iterator i = _plugins.begin();
 
        /* don't set the first plugin, just all the slaves */
 
@@ -264,7 +248,7 @@ PluginInsert::parameter_changed (Evoral::Parameter which, float val)
 void
 PluginInsert::set_block_size (nframes_t nframes)
 {
-       for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
+       for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
                (*i)->set_block_size (nframes);
        }
 }
@@ -272,7 +256,7 @@ PluginInsert::set_block_size (nframes_t nframes)
 void
 PluginInsert::activate ()
 {
-       for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
+       for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
                (*i)->activate ();
        }
 }
@@ -280,7 +264,7 @@ PluginInsert::activate ()
 void
 PluginInsert::deactivate ()
 {
-       for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
+       for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
                (*i)->deactivate ();
        }
 }
@@ -295,8 +279,8 @@ PluginInsert::connect_and_run (BufferSet& bufs, nframes_t nframes, nframes_t off
                collect_signal_nframes = nframes;
        }
 
-       uint32_t in_index = 0;
-       uint32_t out_index = 0;
+       ChanMapping in_map(input_streams());
+       ChanMapping out_map(output_streams());
 
        /* Note that we've already required that plugins
           be able to handle in-place processing.
@@ -329,24 +313,35 @@ PluginInsert::connect_and_run (BufferSet& bufs, nframes_t nframes, nframes_t off
                //std::cerr << "collect input, bufs " << bufs.count().n_audio() << " count,  " << bufs.available().n_audio() << " available" << std::endl;
                //std::cerr << "               streams " << input_streams().n_audio() << std::endl;
                //std::cerr << "filling buffer with " << collect_signal_nframes << " frames at " << _signal_analysis_collected_nframes << std::endl;
+
+               _signal_analysis_inputs.set_count(input_streams());
+
                for (uint32_t i = 0; i < input_streams().n_audio(); ++i) {
-                       _signal_analysis_input_bufferset.get_audio(i).read_from(
+                       _signal_analysis_inputs.get_audio(i).read_from(
                                bufs.get_audio(i),
                                collect_signal_nframes,
                                _signal_analysis_collected_nframes); // offset is for target buffer
                }
+               
        }
 
-       for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
-               (*i)->connect_and_run (bufs, in_index, out_index, nframes, offset);
+       for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
+               (*i)->connect_and_run(bufs, in_map, out_map, nframes, offset);
+               for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
+                       in_map.offset_to(*t, natural_input_streams().get(*t));
+                       out_map.offset_to(*t, natural_output_streams().get(*t));
+               }
        }
 
        if (collect_signal_nframes > 0) {
                // collect output
                //std::cerr << "       output, bufs " << bufs.count().n_audio() << " count,  " << bufs.available().n_audio() << " available" << std::endl;
                //std::cerr << "               streams " << output_streams().n_audio() << std::endl;
+
+               _signal_analysis_outputs.set_count(output_streams());
+
                for (uint32_t i = 0; i < output_streams().n_audio(); ++i) {
-                       _signal_analysis_output_bufferset.get_audio(i).read_from(
+                       _signal_analysis_outputs.get_audio(i).read_from(
                                bufs.get_audio(i), 
                                collect_signal_nframes, 
                                _signal_analysis_collected_nframes); // offset is for target buffer
@@ -359,35 +354,35 @@ PluginInsert::connect_and_run (BufferSet& bufs, nframes_t nframes, nframes_t off
                        _signal_analysis_collect_nframes_max = 0;
                        _signal_analysis_collected_nframes   = 0;
 
-                       AnalysisDataGathered(&_signal_analysis_input_bufferset
-                                            &_signal_analysis_output_bufferset);
+                       AnalysisDataGathered(&_signal_analysis_inputs
+                                            &_signal_analysis_outputs);
                }
        }
        /* leave remaining channel buffers alone */
 }
 
 void
-PluginInsert::silence (nframes_t nframes, nframes_t offset)
+PluginInsert::silence (nframes_t nframes)
 {
-       uint32_t in_index = 0;
-       uint32_t out_index = 0;
+       ChanMapping in_map(input_streams());
+       ChanMapping out_map(output_streams());
 
        if (active()) {
-               for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
-                       (*i)->connect_and_run (_session.get_silent_buffers ((*i)->get_info()->n_inputs), in_index, out_index, nframes, offset);
+               for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
+                       (*i)->connect_and_run (_session.get_silent_buffers ((*i)->get_info()->n_inputs), in_map, out_map, nframes, 0);
                }
        }
 }
        
 void
-PluginInsert::run_in_place (BufferSet& bufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset)
+PluginInsert::run (BufferSet& bufs, sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
 {
        if (active()) {
 
                if (_session.transport_rolling()) {
-                       automation_run (bufs, nframes, offset);
+                       automation_run (bufs, nframes);
                } else {
-                       connect_and_run (bufs, nframes, offset, false);
+                       connect_and_run (bufs, nframes, 0, false);
                }
        } else {
 
@@ -401,7 +396,7 @@ PluginInsert::run_in_place (BufferSet& bufs, nframes_t start_frame, nframes_t en
                        /* not active, but something has make up for any channel count increase */
                        
                        for (uint32_t n = out - in; n < out; ++n) {
-                               memcpy (bufs.get_audio(n).data(nframes, offset), bufs.get_audio(in - 1).data(nframes, offset), sizeof (Sample) * nframes);
+                               memcpy (bufs.get_audio(n).data(), bufs.get_audio(in - 1).data(), sizeof (Sample) * nframes);
                        }
                }
 
@@ -443,11 +438,12 @@ PluginInsert::get_parameter (Evoral::Parameter param)
 }
 
 void
-PluginInsert::automation_run (BufferSet& bufs, nframes_t nframes, nframes_t offset)
+PluginInsert::automation_run (BufferSet& bufs, nframes_t nframes)
 {
        Evoral::ControlEvent next_event (0, 0.0f);
        nframes_t now = _session.transport_frame ();
        nframes_t end = now + nframes;
+       nframes_t offset = 0;
 
        Glib::Mutex::Lock lm (data().control_lock(), Glib::TRY_LOCK);
 
@@ -560,12 +556,13 @@ PluginInsert::configure_io (ChanCount in, ChanCount out)
        // current buffer size here. each request for data fills in these
        // buffers and the analyser makes sure it gets enough data for the 
        // analysis window
-       _signal_analysis_input_bufferset.ensure_buffers (in,  session().engine().frames_per_cycle());
-       _signal_analysis_input_bufferset.set_count(in);
-
-       _signal_analysis_output_bufferset.ensure_buffers(out, session().engine().frames_per_cycle());
-       _signal_analysis_output_bufferset.set_count(out);
+       session().ensure_buffer_set (_signal_analysis_inputs, in);
+       //_signal_analysis_inputs.set_count (in);
+       
+       session().ensure_buffer_set (_signal_analysis_outputs, out);
+       //_signal_analysis_outputs.set_count (out);
 
+       // std::cerr << "set counts to i" << in.n_audio() << "/o" << out.n_audio() << std::endl;
 
        return Processor::configure_io (in, out);
 }
@@ -573,61 +570,60 @@ PluginInsert::configure_io (ChanCount in, ChanCount out)
 bool
 PluginInsert::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
 {
+       // Plugin has flexible I/O, so delegate to it
        if (_plugins.front()->reconfigurable_io()) {
-               /* plugin has flexible I/O, so delegate to it */
                return _plugins.front()->can_support_io_configuration (in, out);
        }
 
+       ChanCount inputs  = _plugins[0]->get_info()->n_inputs;
        ChanCount outputs = _plugins[0]->get_info()->n_outputs;
-       ChanCount inputs = _plugins[0]->get_info()->n_inputs;
 
-       if ((inputs.n_total() == 0)
-                       || (inputs.n_total() == 1 && outputs == inputs)
-                       || (inputs.n_total() == 1 && outputs == inputs
-                               && ((inputs.n_audio() == 0 && in.n_audio() == 0)
-                                       || (inputs.n_midi() == 0 && in.n_midi() == 0)))
-                       || (inputs == in)) {
+       // Plugin inputs match requested inputs exactly
+       if (inputs == in) {
                out = outputs;
                return true;
        }
 
-       bool can_replicate = true;
-
-       /* if number of inputs is a factor of the requested input
-          configuration for every type, we can replicate.
-       */
+       // See if replication is possible
+       // We can replicate if there exists a single factor f such that, for every type,
+       // the number of plugin inputs * f = the requested number of inputs
+       uint32_t f             = 0;
+       bool     can_replicate = true;
        for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
-               if (inputs.get(*t) >= in.get(*t) || (inputs.get(*t) % in.get(*t) != 0)) {
+               // No inputs of this type
+               if (inputs.get(*t) == 0 && in.get(*t) == 0) {
+                       continue;
+
+               // Plugin has more inputs than requested, can not replicate
+               } else if (inputs.get(*t) >= in.get(*t)) {
+                       can_replicate = false;
+                       break;
+               
+               // Plugin inputs is not a factor of requested inputs, can not replicate
+               } else if (inputs.get(*t) == 0 || in.get(*t) % inputs.get(*t) != 0) {
                        can_replicate = false;
                        break;
+               
+               // Potential factor not set yet
+               } else if (f == 0) {
+                       f = in.get(*t) / inputs.get(*t);;
                }
-       }
 
-       if (!can_replicate || (in.n_total() % inputs.n_total() != 0)) {
-               return false;
+               // Factor for this type does not match another type, can not replicate
+               if (f != (in.get(*t) / inputs.get(*t))) {
+                       can_replicate = false;
+                       break;
+               }
        }
 
-       if (inputs.n_total() == 0) {
-               /* instrument plugin, always legal, but throws away any existing streams */
-               out = outputs;
-       } else if (inputs.n_total() == 1 && outputs == inputs
-                       && ((inputs.n_audio() == 0 && in.n_audio() == 0)
-                           || (inputs.n_midi() == 0 && in.n_midi() == 0))) {
-               /* mono, single-typed plugin, replicate as needed to match in */
-               out = in;
-       } else if (inputs == in) {
-               /* exact match */
-               out = outputs;
-       } else {
-               /* replicate - note that we've already verified that
-                  the replication count is constant across all data types.
-               */
+       if (can_replicate) {
                for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
-                       out.set (*t, outputs.get(*t) * (in.get(*t) / inputs.get(*t)));
+                       out.set (*t, outputs.get(*t) * f);
                }
+               return true;
+       } else {
+               return false;
        }
-               
-       return true;
 }
 
 /* Number of plugin instances required to support a given channel configuration.
@@ -686,7 +682,7 @@ PluginInsert::state (bool full)
 {
        XMLNode& node = Processor::state (full);
 
-       node.add_property ("type", _plugins[0]->state_node_name());
+       node.add_property("type", _plugins[0]->state_node_name());
        node.add_property("unique-id", _plugins[0]->unique_id());
        node.add_property("count", string_compose("%1", _plugins.size()));
        node.add_child_nocopy (_plugins[0]->get_state());
@@ -773,7 +769,7 @@ PluginInsert::set_state(const XMLNode& node)
        
        for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
                if ((*niter)->name() == plugin->state_node_name()) {
-                       for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
+                       for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
                                (*i)->set_state (**niter);
                        }
                        break;
@@ -807,9 +803,9 @@ PluginInsert::set_state(const XMLNode& node)
                const char *port;
                uint32_t port_id;
                
-               cnodes = (*niter)->children ("port");
+               cnodes = (*niter)->children ("Port");
                
-               for(iter = cnodes.begin(); iter != cnodes.end(); ++iter){
+               for (iter = cnodes.begin(); iter != cnodes.end(); ++iter) {
                        
                        child = *iter;
                        
@@ -856,10 +852,6 @@ PluginInsert::set_state(const XMLNode& node)
                break;
        } 
 
-       if (niter == nlist.end()) {
-               warning << string_compose(_("XML node describing a port automation is missing the `%1' information"), port_automation_node_name) << endmsg;
-       }
-       
        // The name of the PluginInsert comes from the plugin, nothing else
        _name = plugin->get_info()->name;
        
@@ -953,7 +945,7 @@ PluginInsert::PluginControl::set_value (float val)
 
        }
 
-       for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugin->_plugins.begin();
+       for (Plugins::iterator i = _plugin->_plugins.begin();
                        i != _plugin->_plugins.end(); ++i) {
                (*i)->set_parameter (_list->parameter().id(), val);
        }
@@ -1003,3 +995,15 @@ PluginInsert::get_impulse_analysis_plugin()
        return ret;
 }
 
+void
+PluginInsert::collect_signal_for_analysis(nframes_t nframes)
+{
+       // called from outside the audio thread, so this should be safe
+       // only do audio as analysis is (currently) only for audio plugins
+       _signal_analysis_inputs.ensure_buffers(  DataType::AUDIO, input_streams().n_audio(),  nframes); 
+       _signal_analysis_outputs.ensure_buffers( DataType::AUDIO, output_streams().n_audio(), nframes); 
+
+       _signal_analysis_collected_nframes   = 0;
+       _signal_analysis_collect_nframes_max = nframes; 
+}
+