2 Copyright (C) 2000 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <sigc++/bind.h>
25 #include <pbd/failed_constructor.h>
26 #include <pbd/xml++.h>
28 #include <ardour/insert.h>
29 #include <ardour/plugin.h>
30 #include <ardour/port.h>
31 #include <ardour/route.h>
32 #include <ardour/ladspa_plugin.h>
35 #include <ardour/vst_plugin.h>
38 #ifdef HAVE_AUDIOUNITS
39 #include <ardour/audio_unit.h>
42 #include <ardour/audioengine.h>
43 #include <ardour/session.h>
44 #include <ardour/types.h>
49 using namespace ARDOUR;
52 Insert::Insert(Session& s, Placement p)
53 : Redirect (s, s.next_insert_name(), p)
57 Insert::Insert(Session& s, Placement p, int imin, int imax, int omin, int omax)
58 : Redirect (s, s.next_insert_name(), p, imin, imax, omin, omax)
62 Insert::Insert(Session& s, string name, Placement p)
63 : Redirect (s, name, p)
67 /***************************************************************
68 Plugin inserts: send data through a plugin
69 ***************************************************************/
71 const string PluginInsert::port_automation_node_name = "PortAutomation";
73 PluginInsert::PluginInsert (Session& s, boost::shared_ptr<Plugin> plug, Placement placement)
74 : Insert (s, plug->name(), placement)
76 /* the first is the master */
78 _plugins.push_back (plug);
80 _plugins[0]->ParameterChanged.connect (mem_fun (*this, &PluginInsert::parameter_changed));
85 Glib::Mutex::Lock em (_session.engine().process_lock());
86 IO::MoreOutputs (output_streams ());
89 RedirectCreated (this); /* EMIT SIGNAL */
92 PluginInsert::PluginInsert (Session& s, const XMLNode& node)
93 : Insert (s, "will change", PreFader)
95 if (set_state (node)) {
96 throw failed_constructor();
101 _plugins[0]->ParameterChanged.connect (mem_fun (*this, &PluginInsert::parameter_changed));
104 Glib::Mutex::Lock em (_session.engine().process_lock());
105 IO::MoreOutputs (output_streams());
109 PluginInsert::PluginInsert (const PluginInsert& other)
110 : Insert (other._session, other.plugin()->name(), other.placement())
112 uint32_t count = other._plugins.size();
114 /* make as many copies as requested */
115 for (uint32_t n = 0; n < count; ++n) {
116 _plugins.push_back (plugin_factory (other.plugin (n)));
120 _plugins[0]->ParameterChanged.connect (mem_fun (*this, &PluginInsert::parameter_changed));
124 RedirectCreated (this); /* EMIT SIGNAL */
128 PluginInsert::set_count (uint32_t num)
130 bool require_state = !_plugins.empty();
132 /* this is a bad idea.... we shouldn't do this while active.
133 only a route holding their redirect_lock should be calling this
138 } else if (num > _plugins.size()) {
139 uint32_t diff = num - _plugins.size();
141 for (uint32_t n = 0; n < diff; ++n) {
142 _plugins.push_back (plugin_factory (_plugins[0]));
145 /* XXX do something */
149 } else if (num < _plugins.size()) {
150 uint32_t diff = _plugins.size() - num;
151 for (uint32_t n= 0; n < diff; ++n) {
160 PluginInsert::init ()
164 set<uint32_t>::iterator s;
167 PluginInsert::~PluginInsert ()
169 GoingAway (); /* EMIT SIGNAL */
173 PluginInsert::automation_list_creation_callback (uint32_t which, AutomationList& alist)
175 alist.automation_state_changed.connect (sigc::bind (mem_fun (*this, &PluginInsert::auto_state_changed), (which)));
179 PluginInsert::auto_state_changed (uint32_t which)
181 AutomationList& alist (automation_list (which));
183 if (alist.automation_state() != Off) {
184 _plugins[0]->set_parameter (which, alist.eval (_session.transport_frame()));
189 PluginInsert::output_streams() const
191 return _plugins[0]->get_info()->n_outputs * _plugins.size();
195 PluginInsert::input_streams() const
197 return _plugins[0]->get_info()->n_inputs * _plugins.size();
201 PluginInsert::natural_output_streams() const
203 return _plugins[0]->get_info()->n_outputs;
207 PluginInsert::natural_input_streams() const
209 return _plugins[0]->get_info()->n_inputs;
213 PluginInsert::is_generator() const
215 /* XXX more finesse is possible here. VST plugins have a
216 a specific "instrument" flag, for example.
219 return _plugins[0]->get_info()->n_inputs == 0;
223 PluginInsert::set_automatable ()
227 a = _plugins.front()->automatable ();
229 for (set<uint32_t>::iterator i = a.begin(); i != a.end(); ++i) {
235 PluginInsert::parameter_changed (uint32_t which, float val)
237 vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin();
239 /* don't set the first plugin, just all the slaves */
241 if (i != _plugins.end()) {
243 for (; i != _plugins.end(); ++i) {
244 (*i)->set_parameter (which, val);
250 PluginInsert::set_block_size (nframes_t nframes)
252 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
253 (*i)->set_block_size (nframes);
258 PluginInsert::activate ()
260 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
266 PluginInsert::deactivate ()
268 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
274 PluginInsert::connect_and_run (vector<Sample*>& bufs, uint32_t nbufs, nframes_t nframes, nframes_t offset, bool with_auto, nframes_t now)
276 int32_t in_index = 0;
277 int32_t out_index = 0;
279 /* Note that we've already required that plugins
280 be able to handle in-place processing.
285 map<uint32_t,AutomationList*>::iterator li;
288 for (n = 0, li = parameter_automation.begin(); li != parameter_automation.end(); ++li, ++n) {
290 AutomationList& alist (*((*li).second));
292 if (alist.automation_playback()) {
295 float val = alist.rt_safe_eval (now, valid);
298 /* set the first plugin, the others will be set via signals */
299 _plugins[0]->set_parameter ((*li).first, val);
306 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
307 (*i)->connect_and_run (bufs, nbufs, in_index, out_index, nframes, offset);
310 /* leave remaining channel buffers alone */
314 PluginInsert::automation_snapshot (nframes_t now)
316 map<uint32_t,AutomationList*>::iterator li;
318 for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
320 AutomationList *alist = ((*li).second);
321 if (alist != 0 && alist->automation_write ()) {
323 float val = _plugins[0]->get_parameter ((*li).first);
324 alist->rt_add (now, val);
325 last_automation_snapshot = now;
331 PluginInsert::transport_stopped (nframes_t now)
333 map<uint32_t,AutomationList*>::iterator li;
335 for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
336 AutomationList& alist (*(li->second));
337 alist.reposition_for_rt_add (now);
339 if (alist.automation_state() != Off) {
340 _plugins[0]->set_parameter (li->first, alist.eval (now));
346 PluginInsert::silence (nframes_t nframes, nframes_t offset)
348 int32_t in_index = 0;
349 int32_t out_index = 0;
354 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
355 n = (*i) -> get_info()->n_inputs;
356 (*i)->connect_and_run (_session.get_silent_buffers (n), n, in_index, out_index, nframes, offset);
362 PluginInsert::run (vector<Sample *>& bufs, uint32_t nbufs, nframes_t nframes, nframes_t offset)
366 if (_session.transport_rolling()) {
367 automation_run (bufs, nbufs, nframes, offset);
369 connect_and_run (bufs, nbufs, nframes, offset, false);
372 uint32_t in = _plugins[0]->get_info()->n_inputs;
373 uint32_t out = _plugins[0]->get_info()->n_outputs;
377 /* not active, but something has make up for any channel count increase */
379 for (uint32_t n = out - in; n < out; ++n) {
380 memcpy (bufs[n], bufs[in - 1], sizeof (Sample) * nframes);
387 PluginInsert::set_parameter (uint32_t port, float val)
389 /* the others will be set from the event triggered by this */
391 _plugins[0]->set_parameter (port, val);
393 if (automation_list (port).automation_write()) {
394 automation_list (port).add (_session.audible_frame(), val);
397 _session.set_dirty();
401 PluginInsert::automation_run (vector<Sample *>& bufs, uint32_t nbufs, nframes_t nframes, nframes_t offset)
403 ControlEvent next_event (0, 0.0f);
404 nframes_t now = _session.transport_frame ();
405 nframes_t end = now + nframes;
407 Glib::Mutex::Lock lm (_automation_lock, Glib::TRY_LOCK);
410 connect_and_run (bufs, nbufs, nframes, offset, false);
414 if (!find_next_event (now, end, next_event)) {
416 /* no events have a time within the relevant range */
418 connect_and_run (bufs, nbufs, nframes, offset, true, now);
424 nframes_t cnt = min (((nframes_t) floor (next_event.when) - now), nframes);
426 connect_and_run (bufs, nbufs, cnt, offset, true, now);
432 if (!find_next_event (now, end, next_event)) {
437 /* cleanup anything that is left to do */
440 connect_and_run (bufs, nbufs, nframes, offset, true, now);
445 PluginInsert::default_parameter_value (uint32_t port)
447 if (_plugins.empty()) {
448 fatal << _("programming error: ") << X_("PluginInsert::default_parameter_value() called with no plugin")
453 return _plugins[0]->default_value (port);
457 PluginInsert::set_port_automation_state (uint32_t port, AutoState s)
459 if (port < _plugins[0]->parameter_count()) {
461 AutomationList& al = automation_list (port);
463 if (s != al.automation_state()) {
464 al.set_automation_state (s);
465 _session.set_dirty ();
471 PluginInsert::get_port_automation_state (uint32_t port)
473 if (port < _plugins[0]->parameter_count()) {
474 return automation_list (port).automation_state();
481 PluginInsert::protect_automation ()
483 set<uint32_t> automated_params;
485 what_has_automation (automated_params);
487 for (set<uint32_t>::iterator i = automated_params.begin(); i != automated_params.end(); ++i) {
489 AutomationList& al = automation_list (*i);
491 switch (al.automation_state()) {
494 al.set_automation_state (Off);
502 boost::shared_ptr<Plugin>
503 PluginInsert::plugin_factory (boost::shared_ptr<Plugin> other)
505 boost::shared_ptr<LadspaPlugin> lp;
507 boost::shared_ptr<VSTPlugin> vp;
509 #ifdef HAVE_AUDIOUNITS
510 boost::shared_ptr<AUPlugin> ap;
513 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
514 return boost::shared_ptr<Plugin> (new LadspaPlugin (*lp));
516 } else if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (other)) != 0) {
517 return boost::shared_ptr<Plugin> (new VSTPlugin (*vp));
519 #ifdef HAVE_AUDIOUNITS
520 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
521 return boost::shared_ptr<Plugin> (new AUPlugin (*ap));
525 fatal << string_compose (_("programming error: %1"),
526 X_("unknown plugin type in PluginInsert::plugin_factory"))
529 return boost::shared_ptr<Plugin> ((Plugin*) 0);
533 PluginInsert::compute_output_streams (int32_t cnt) const
535 return _plugins[0]->get_info()->n_outputs * cnt;
539 PluginInsert::configure_io (int32_t magic, int32_t in, int32_t out)
541 return set_count (magic);
545 PluginInsert::can_support_input_configuration (int32_t in) const
547 int32_t outputs = _plugins[0]->get_info()->n_outputs;
548 int32_t inputs = _plugins[0]->get_info()->n_inputs;
552 /* instrument plugin, always legal, but it throws
553 away any existing active streams.
559 if (outputs == 1 && inputs == 1) {
560 /* mono plugin, replicate as needed */
569 if ((inputs < in) && (inputs % in == 0)) {
571 /* number of inputs is a factor of the requested input
572 configuration, so we can replicate.
584 PluginInsert::get_state(void)
590 PluginInsert::state (bool full)
593 XMLNode *node = new XMLNode("Insert");
595 node->add_child_nocopy (Redirect::state (full));
597 node->add_property ("type", _plugins[0]->state_node_name());
598 snprintf(buf, sizeof(buf), "%s", _plugins[0]->name());
599 node->add_property("id", string(buf));
600 if (_plugins[0]->state_node_name() == "ladspa") {
602 snprintf (buf, sizeof (buf), "%ld", _plugins[0]->get_info()->unique_id);
603 node->add_property("unique-id", string(buf));
605 node->add_property("count", string_compose("%1", _plugins.size()));
606 node->add_child_nocopy (_plugins[0]->get_state());
608 /* add port automation state */
609 XMLNode *autonode = new XMLNode(port_automation_node_name);
610 set<uint32_t> automatable = _plugins[0]->automatable();
612 for (set<uint32_t>::iterator x = automatable.begin(); x != automatable.end(); ++x) {
614 XMLNode* child = new XMLNode("port");
615 snprintf(buf, sizeof(buf), "%" PRIu32, *x);
616 child->add_property("number", string(buf));
619 snprintf(buf, sizeof(buf), "0x%x", automation_list (*x).automation_state ());
621 snprintf(buf, sizeof(buf), "0x%x", ARDOUR::Off);
623 child->add_property("auto", string(buf));
625 autonode->add_child_nocopy (*child);
628 node->add_child_nocopy (*autonode);
634 PluginInsert::set_state(const XMLNode& node)
636 XMLNodeList nlist = node.children();
637 XMLNodeIterator niter;
638 XMLPropertyList plist;
639 const XMLProperty *prop;
641 ARDOUR::PluginType type;
643 if ((prop = node.property ("type")) == 0) {
644 error << _("XML node describing insert is missing the `type' field") << endmsg;
648 if (prop->value() == X_("ladspa") || prop->value() == X_("Ladspa")) { /* handle old school sessions */
649 type = ARDOUR::LADSPA;
650 } else if (prop->value() == X_("vst")) {
653 error << string_compose (_("unknown plugin type %1 in plugin insert state"),
659 prop = node.property ("unique-id");
661 unique = atol(prop->value().c_str());
664 if ((prop = node.property ("id")) == 0) {
665 error << _("XML node describing insert is missing the `id' field") << endmsg;
669 boost::shared_ptr<Plugin> plugin;
672 plugin = find_plugin (_session, "", unique, type);
674 plugin = find_plugin (_session, prop->value(), 0, type);
678 error << string_compose(_("Found a reference to a plugin (\"%1\") that is unknown.\n"
679 "Perhaps it was removed or moved since it was last used."), prop->value())
686 if ((prop = node.property ("count")) != 0) {
687 sscanf (prop->value().c_str(), "%u", &count);
690 if (_plugins.size() != count) {
692 _plugins.push_back (plugin);
694 for (uint32_t n=1; n < count; ++n) {
695 _plugins.push_back (plugin_factory (plugin));
699 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
700 if ((*niter)->name() == plugin->state_node_name()) {
701 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
702 (*i)->set_state (**niter);
708 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
709 if ((*niter)->name() == Redirect::state_node_name) {
710 Redirect::set_state (**niter);
715 if (niter == nlist.end()) {
716 error << _("XML node describing insert is missing a Redirect node") << endmsg;
720 if (niter == nlist.end()) {
721 error << string_compose(_("XML node describing a plugin insert is missing the `%1' information"), plugin->state_node_name()) << endmsg;
725 /* look for port automation node */
727 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
728 if ((*niter)->name() == port_automation_node_name) {
731 XMLNodeConstIterator iter;
736 cnodes = (*niter)->children ("port");
738 for(iter = cnodes.begin(); iter != cnodes.end(); ++iter){
742 if ((cprop = child->property("number")) != 0) {
743 port = cprop->value().c_str();
745 warning << _("PluginInsert: Auto: no ladspa port number") << endmsg;
749 sscanf (port, "%" PRIu32, &port_id);
751 if (port_id >= _plugins[0]->parameter_count()) {
752 warning << _("PluginInsert: Auto: port id out of range") << endmsg;
756 if ((cprop = child->property("auto")) != 0) {
758 sscanf (cprop->value().c_str(), "0x%x", &x);
759 automation_list (port_id).set_automation_state (AutoState (x));
767 if (niter == nlist.end()) {
768 warning << string_compose(_("XML node describing a port automation is missing the `%1' information"), port_automation_node_name) << endmsg;
771 // The name of the PluginInsert comes from the plugin, nothing else
772 set_name(plugin->get_info()->name,this);
778 PluginInsert::describe_parameter (uint32_t what)
780 return _plugins[0]->describe_parameter (what);
784 PluginInsert::latency()
786 return _plugins[0]->latency ();
790 PluginInsert::type ()
792 boost::shared_ptr<LadspaPlugin> lp;
794 boost::shared_ptr<VSTPlugin> vp;
796 #ifdef HAVE_AUDIOUNITS
797 boost::shared_ptr<AUPlugin> ap;
800 PluginPtr other = plugin ();
802 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
803 return ARDOUR::LADSPA;
805 } else if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (other)) != 0) {
808 #ifdef HAVE_AUDIOUNITS
809 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
810 return ARDOUR::AudioUnit;
814 return (ARDOUR::PluginType) 0;
818 /***************************************************************
819 Port inserts: send output to a port, pick up input at a port
820 ***************************************************************/
822 PortInsert::PortInsert (Session& s, Placement p)
823 : Insert (s, p, 1, -1, 1, -1)
826 RedirectCreated (this); /* EMIT SIGNAL */
830 PortInsert::PortInsert (const PortInsert& other)
831 : Insert (other._session, other.placement(), 1, -1, 1, -1)
834 RedirectCreated (this); /* EMIT SIGNAL */
840 if (add_input_port ("", this)) {
841 error << _("PortInsert: cannot add input port") << endmsg;
842 throw failed_constructor();
845 if (add_output_port ("", this)) {
846 error << _("PortInsert: cannot add output port") << endmsg;
847 throw failed_constructor();
851 PortInsert::PortInsert (Session& s, const XMLNode& node)
852 : Insert (s, "will change", PreFader)
854 if (set_state (node)) {
855 throw failed_constructor();
858 RedirectCreated (this); /* EMIT SIGNAL */
861 PortInsert::~PortInsert ()
867 PortInsert::run (vector<Sample *>& bufs, uint32_t nbufs, nframes_t nframes, nframes_t offset)
869 if (n_outputs() == 0) {
874 /* deliver silence */
875 silence (nframes, offset);
880 vector<Port*>::iterator o;
881 vector<Port*>::iterator i;
885 for (o = _outputs.begin(), n = 0; o != _outputs.end(); ++o, ++n) {
886 memcpy ((*o)->get_buffer (nframes) + offset, bufs[min(nbufs,n)], sizeof (Sample) * nframes);
887 (*o)->mark_silence (false);
892 for (i = _inputs.begin(), n = 0; i != _inputs.end(); ++i, ++n) {
893 memcpy (bufs[min(nbufs,n)], (*i)->get_buffer (nframes) + offset, sizeof (Sample) * nframes);
898 PortInsert::get_state(void)
904 PortInsert::state (bool full)
906 XMLNode *node = new XMLNode("Insert");
908 node->add_child_nocopy (Redirect::state(full));
909 node->add_property("type", "port");
915 PortInsert::set_state(const XMLNode& node)
917 XMLNodeList nlist = node.children();
918 XMLNodeIterator niter;
919 XMLPropertyList plist;
920 const XMLProperty *prop;
922 if ((prop = node.property ("type")) == 0) {
923 error << _("XML node describing insert is missing the `type' field") << endmsg;
927 if (prop->value() != "port") {
928 error << _("non-port insert XML used for port plugin insert") << endmsg;
932 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
933 if ((*niter)->name() == Redirect::state_node_name) {
934 Redirect::set_state (**niter);
939 if (niter == nlist.end()) {
940 error << _("XML node describing insert is missing a Redirect node") << endmsg;
948 PortInsert::latency()
950 /* because we deliver and collect within the same cycle,
951 all I/O is necessarily delayed by at least frames_per_cycle().
953 if the return port for insert has its own latency, we
954 need to take that into account too.
957 return _session.engine().frames_per_cycle() + input_latency();
961 PortInsert::can_support_input_configuration (int32_t in) const
963 if (input_maximum() == -1 && output_maximum() == -1) {
965 /* not configured yet */
967 return 1; /* we can support anything the first time we're asked */
971 /* the "input" config for a port insert corresponds to how
972 many output ports it will have.
975 if (output_maximum() == in) {
984 PortInsert::configure_io (int32_t ignored_magic, int32_t in, int32_t out)
986 /* do not allow configuration to be changed outside the range of
987 the last request config. or something like that.
991 /* this is a bit odd:
993 the number of inputs we are required to handle corresponds
994 to the number of output ports we need.
996 the number of outputs we are required to have corresponds
997 to the number of input ports we need.
1000 set_output_maximum (in);
1001 set_output_minimum (in);
1002 set_input_maximum (out);
1003 set_input_minimum (out);
1013 return ensure_io (out, in, false, this);
1017 PortInsert::compute_output_streams (int32_t cnt) const
1019 /* puzzling, eh? think about it ... */
1024 PortInsert::output_streams() const
1030 PortInsert::input_streams() const
1032 return n_outputs ();