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.
22 #include <sigc++/bind.h>
24 #include <pbd/failed_constructor.h>
25 #include <pbd/xml++.h>
27 #include <ardour/insert.h>
28 #include <ardour/plugin.h>
29 #include <ardour/port.h>
30 #include <ardour/route.h>
31 #include <ardour/ladspa_plugin.h>
34 #include <ardour/vst_plugin.h>
37 #ifdef HAVE_AUDIOUNITS
38 #include <ardour/audio_unit.h>
41 #include <ardour/audioengine.h>
42 #include <ardour/session.h>
43 #include <ardour/types.h>
48 using namespace ARDOUR;
51 Insert::Insert(Session& s, string name, Placement p)
52 : Redirect (s, name, p)
56 Insert::Insert(Session& s, string name, Placement p, int imin, int imax, int omin, int omax)
57 : Redirect (s, name, p, imin, imax, omin, omax)
61 /***************************************************************
62 Plugin inserts: send data through a plugin
63 ***************************************************************/
65 const string PluginInsert::port_automation_node_name = "PortAutomation";
67 PluginInsert::PluginInsert (Session& s, boost::shared_ptr<Plugin> plug, Placement placement)
68 : Insert (s, plug->name(), placement)
70 /* the first is the master */
72 _plugins.push_back (plug);
74 _plugins[0]->ParameterChanged.connect (mem_fun (*this, &PluginInsert::parameter_changed));
79 Glib::Mutex::Lock em (_session.engine().process_lock());
80 IO::MoreOutputs (output_streams ());
83 RedirectCreated (this); /* EMIT SIGNAL */
86 PluginInsert::PluginInsert (Session& s, const XMLNode& node)
87 : Insert (s, "will change", PreFader)
89 if (set_state (node)) {
90 throw failed_constructor();
95 _plugins[0]->ParameterChanged.connect (mem_fun (*this, &PluginInsert::parameter_changed));
98 Glib::Mutex::Lock em (_session.engine().process_lock());
99 IO::MoreOutputs (output_streams());
103 PluginInsert::PluginInsert (const PluginInsert& other)
104 : Insert (other._session, other.plugin()->name(), other.placement())
106 uint32_t count = other._plugins.size();
108 /* make as many copies as requested */
109 for (uint32_t n = 0; n < count; ++n) {
110 _plugins.push_back (plugin_factory (other.plugin (n)));
114 _plugins[0]->ParameterChanged.connect (mem_fun (*this, &PluginInsert::parameter_changed));
118 RedirectCreated (this); /* EMIT SIGNAL */
122 PluginInsert::set_count (uint32_t num)
124 bool require_state = !_plugins.empty();
126 /* this is a bad idea.... we shouldn't do this while active.
127 only a route holding their redirect_lock should be calling this
132 } else if (num > _plugins.size()) {
133 uint32_t diff = num - _plugins.size();
135 for (uint32_t n = 0; n < diff; ++n) {
136 _plugins.push_back (plugin_factory (_plugins[0]));
139 /* XXX do something */
143 } else if (num < _plugins.size()) {
144 uint32_t diff = _plugins.size() - num;
145 for (uint32_t n= 0; n < diff; ++n) {
154 PluginInsert::init ()
158 set<uint32_t>::iterator s;
161 PluginInsert::~PluginInsert ()
163 GoingAway (); /* EMIT SIGNAL */
167 PluginInsert::automation_list_creation_callback (uint32_t which, AutomationList& alist)
169 alist.automation_state_changed.connect (sigc::bind (mem_fun (*this, &PluginInsert::auto_state_changed), (which)));
173 PluginInsert::auto_state_changed (uint32_t which)
175 AutomationList& alist (automation_list (which));
177 if (alist.automation_state() != Off) {
178 _plugins[0]->set_parameter (which, alist.eval (_session.transport_frame()));
183 PluginInsert::output_streams() const
185 return _plugins[0]->get_info()->n_outputs * _plugins.size();
189 PluginInsert::input_streams() const
191 return _plugins[0]->get_info()->n_inputs * _plugins.size();
195 PluginInsert::natural_output_streams() const
197 return _plugins[0]->get_info()->n_outputs;
201 PluginInsert::natural_input_streams() const
203 return _plugins[0]->get_info()->n_inputs;
207 PluginInsert::is_generator() const
209 /* XXX more finesse is possible here. VST plugins have a
210 a specific "instrument" flag, for example.
213 return _plugins[0]->get_info()->n_inputs == 0;
217 PluginInsert::set_automatable ()
221 a = _plugins.front()->automatable ();
223 for (set<uint32_t>::iterator i = a.begin(); i != a.end(); ++i) {
229 PluginInsert::parameter_changed (uint32_t which, float val)
231 vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin();
233 /* don't set the first plugin, just all the slaves */
235 if (i != _plugins.end()) {
237 for (; i != _plugins.end(); ++i) {
238 (*i)->set_parameter (which, val);
244 PluginInsert::set_block_size (nframes_t nframes)
246 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
247 (*i)->set_block_size (nframes);
252 PluginInsert::activate ()
254 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
260 PluginInsert::deactivate ()
262 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
268 PluginInsert::connect_and_run (vector<Sample*>& bufs, uint32_t nbufs, nframes_t nframes, nframes_t offset, bool with_auto, nframes_t now)
270 int32_t in_index = 0;
271 int32_t out_index = 0;
273 /* Note that we've already required that plugins
274 be able to handle in-place processing.
279 map<uint32_t,AutomationList*>::iterator li;
282 for (n = 0, li = parameter_automation.begin(); li != parameter_automation.end(); ++li, ++n) {
284 AutomationList& alist (*((*li).second));
286 if (alist.automation_playback()) {
289 float val = alist.rt_safe_eval (now, valid);
292 /* set the first plugin, the others will be set via signals */
293 _plugins[0]->set_parameter ((*li).first, val);
300 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
301 (*i)->connect_and_run (bufs, nbufs, in_index, out_index, nframes, offset);
304 /* leave remaining channel buffers alone */
308 PluginInsert::automation_snapshot (nframes_t now)
310 map<uint32_t,AutomationList*>::iterator li;
312 for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
314 AutomationList *alist = ((*li).second);
315 if (alist != 0 && alist->automation_write ()) {
317 float val = _plugins[0]->get_parameter ((*li).first);
318 alist->rt_add (now, val);
319 last_automation_snapshot = now;
325 PluginInsert::transport_stopped (nframes_t now)
327 map<uint32_t,AutomationList*>::iterator li;
329 for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
330 AutomationList& alist (*(li->second));
331 alist.reposition_for_rt_add (now);
333 if (alist.automation_state() != Off) {
334 _plugins[0]->set_parameter (li->first, alist.eval (now));
340 PluginInsert::silence (nframes_t nframes, nframes_t offset)
342 int32_t in_index = 0;
343 int32_t out_index = 0;
348 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
349 n = (*i) -> get_info()->n_inputs;
350 (*i)->connect_and_run (_session.get_silent_buffers (n), n, in_index, out_index, nframes, offset);
356 PluginInsert::run (vector<Sample *>& bufs, uint32_t nbufs, nframes_t nframes, nframes_t offset)
360 if (_session.transport_rolling()) {
361 automation_run (bufs, nbufs, nframes, offset);
363 connect_and_run (bufs, nbufs, nframes, offset, false);
366 uint32_t in = _plugins[0]->get_info()->n_inputs;
367 uint32_t out = _plugins[0]->get_info()->n_outputs;
371 /* not active, but something has make up for any channel count increase */
373 for (uint32_t n = out - in; n < out; ++n) {
374 memcpy (bufs[n], bufs[in - 1], sizeof (Sample) * nframes);
381 PluginInsert::set_parameter (uint32_t port, float val)
383 /* the others will be set from the event triggered by this */
385 _plugins[0]->set_parameter (port, val);
387 if (automation_list (port).automation_write()) {
388 automation_list (port).add (_session.audible_frame(), val);
391 _session.set_dirty();
395 PluginInsert::automation_run (vector<Sample *>& bufs, uint32_t nbufs, nframes_t nframes, nframes_t offset)
397 ControlEvent next_event (0, 0.0f);
398 nframes_t now = _session.transport_frame ();
399 nframes_t end = now + nframes;
401 Glib::Mutex::Lock lm (_automation_lock, Glib::TRY_LOCK);
404 connect_and_run (bufs, nbufs, nframes, offset, false);
408 if (!find_next_event (now, end, next_event)) {
410 /* no events have a time within the relevant range */
412 connect_and_run (bufs, nbufs, nframes, offset, true, now);
418 nframes_t cnt = min (((nframes_t) floor (next_event.when) - now), nframes);
420 connect_and_run (bufs, nbufs, cnt, offset, true, now);
426 if (!find_next_event (now, end, next_event)) {
431 /* cleanup anything that is left to do */
434 connect_and_run (bufs, nbufs, nframes, offset, true, now);
439 PluginInsert::default_parameter_value (uint32_t port)
441 if (_plugins.empty()) {
442 fatal << _("programming error: ") << X_("PluginInsert::default_parameter_value() called with no plugin")
447 return _plugins[0]->default_value (port);
451 PluginInsert::set_port_automation_state (uint32_t port, AutoState s)
453 if (port < _plugins[0]->parameter_count()) {
455 AutomationList& al = automation_list (port);
457 if (s != al.automation_state()) {
458 al.set_automation_state (s);
459 _session.set_dirty ();
465 PluginInsert::get_port_automation_state (uint32_t port)
467 if (port < _plugins[0]->parameter_count()) {
468 return automation_list (port).automation_state();
475 PluginInsert::protect_automation ()
477 set<uint32_t> automated_params;
479 what_has_automation (automated_params);
481 for (set<uint32_t>::iterator i = automated_params.begin(); i != automated_params.end(); ++i) {
483 AutomationList& al = automation_list (*i);
485 switch (al.automation_state()) {
487 al.set_automation_state (Off);
490 al.set_automation_state (Play);
498 boost::shared_ptr<Plugin>
499 PluginInsert::plugin_factory (boost::shared_ptr<Plugin> other)
501 boost::shared_ptr<LadspaPlugin> lp;
503 boost::shared_ptr<VSTPlugin> vp;
505 #ifdef HAVE_AUDIOUNITS
506 boost::shared_ptr<AUPlugin> ap;
509 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
510 return boost::shared_ptr<Plugin> (new LadspaPlugin (*lp));
512 } else if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (other)) != 0) {
513 return boost::shared_ptr<Plugin> (new VSTPlugin (*vp));
515 #ifdef HAVE_AUDIOUNITS
516 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
517 return boost::shared_ptr<Plugin> (new AUPlugin (*ap));
521 fatal << string_compose (_("programming error: %1"),
522 X_("unknown plugin type in PluginInsert::plugin_factory"))
525 return boost::shared_ptr<Plugin> ((Plugin*) 0);
529 PluginInsert::compute_output_streams (int32_t cnt) const
531 return _plugins[0]->get_info()->n_outputs * cnt;
535 PluginInsert::configure_io (int32_t magic, int32_t in, int32_t out)
537 return set_count (magic);
541 PluginInsert::can_support_input_configuration (int32_t in) const
543 int32_t outputs = _plugins[0]->get_info()->n_outputs;
544 int32_t inputs = _plugins[0]->get_info()->n_inputs;
548 /* instrument plugin, always legal, but it throws
549 away any existing active streams.
555 if (outputs == 1 && inputs == 1) {
556 /* mono plugin, replicate as needed */
565 if ((inputs < in) && (inputs % in == 0)) {
567 /* number of inputs is a factor of the requested input
568 configuration, so we can replicate.
580 PluginInsert::get_state(void)
586 PluginInsert::state (bool full)
589 XMLNode *node = new XMLNode("Insert");
591 node->add_child_nocopy (Redirect::state (full));
593 node->add_property ("type", _plugins[0]->state_node_name());
594 snprintf(buf, sizeof(buf), "%s", _plugins[0]->name());
595 node->add_property("id", string(buf));
596 if (_plugins[0]->state_node_name() == "ladspa") {
598 snprintf (buf, sizeof (buf), "%ld", _plugins[0]->get_info()->unique_id);
599 node->add_property("unique-id", string(buf));
601 node->add_property("count", string_compose("%1", _plugins.size()));
602 node->add_child_nocopy (_plugins[0]->get_state());
604 /* add port automation state */
605 XMLNode *autonode = new XMLNode(port_automation_node_name);
606 set<uint32_t> automatable = _plugins[0]->automatable();
608 for (set<uint32_t>::iterator x = automatable.begin(); x != automatable.end(); ++x) {
610 XMLNode* child = new XMLNode("port");
611 snprintf(buf, sizeof(buf), "%" PRIu32, *x);
612 child->add_property("number", string(buf));
614 child->add_child_nocopy (automation_list (*x).state (full));
615 autonode->add_child_nocopy (*child);
618 node->add_child_nocopy (*autonode);
624 PluginInsert::set_state(const XMLNode& node)
626 XMLNodeList nlist = node.children();
627 XMLNodeIterator niter;
628 XMLPropertyList plist;
629 const XMLProperty *prop;
631 ARDOUR::PluginType type;
633 if ((prop = node.property ("type")) == 0) {
634 error << _("XML node describing insert is missing the `type' field") << endmsg;
638 if (prop->value() == X_("ladspa") || prop->value() == X_("Ladspa")) { /* handle old school sessions */
639 type = ARDOUR::LADSPA;
640 } else if (prop->value() == X_("vst")) {
643 error << string_compose (_("unknown plugin type %1 in plugin insert state"),
649 prop = node.property ("unique-id");
651 unique = atol(prop->value().c_str());
654 if ((prop = node.property ("id")) == 0) {
655 error << _("XML node describing insert is missing the `id' field") << endmsg;
659 boost::shared_ptr<Plugin> plugin;
662 plugin = find_plugin (_session, "", unique, type);
664 plugin = find_plugin (_session, prop->value(), 0, type);
668 error << string_compose(_("Found a reference to a plugin (\"%1\") that is unknown.\n"
669 "Perhaps it was removed or moved since it was last used."), prop->value())
676 if ((prop = node.property ("count")) != 0) {
677 sscanf (prop->value().c_str(), "%u", &count);
680 if (_plugins.size() != count) {
682 _plugins.push_back (plugin);
684 for (uint32_t n=1; n < count; ++n) {
685 _plugins.push_back (plugin_factory (plugin));
689 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
690 if ((*niter)->name() == plugin->state_node_name()) {
691 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
692 (*i)->set_state (**niter);
698 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
699 if ((*niter)->name() == Redirect::state_node_name) {
700 Redirect::set_state (**niter);
705 if (niter == nlist.end()) {
706 error << _("XML node describing insert is missing a Redirect node") << endmsg;
710 if (niter == nlist.end()) {
711 error << string_compose(_("XML node describing a plugin insert is missing the `%1' information"), plugin->state_node_name()) << endmsg;
715 /* look for port automation node */
717 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
719 if ((*niter)->name() != port_automation_node_name) {
725 XMLNodeConstIterator iter;
730 cnodes = (*niter)->children ("port");
732 for(iter = cnodes.begin(); iter != cnodes.end(); ++iter){
736 if ((cprop = child->property("number")) != 0) {
737 port = cprop->value().c_str();
739 warning << _("PluginInsert: Auto: no ladspa port number") << endmsg;
743 sscanf (port, "%" PRIu32, &port_id);
745 if (port_id >= _plugins[0]->parameter_count()) {
746 warning << _("PluginInsert: Auto: port id out of range") << endmsg;
750 if (!child->children().empty()) {
751 automation_list (port_id).set_state (*child->children().front());
753 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));
765 automation_list (port_id).set_automation_state (Off);
776 if (niter == nlist.end()) {
777 warning << string_compose(_("XML node describing a port automation is missing the `%1' information"), port_automation_node_name) << endmsg;
780 // The name of the PluginInsert comes from the plugin, nothing else
781 set_name(plugin->get_info()->name,this);
787 PluginInsert::describe_parameter (uint32_t what)
789 return _plugins[0]->describe_parameter (what);
793 PluginInsert::latency()
795 return _plugins[0]->latency ();
799 PluginInsert::type ()
801 boost::shared_ptr<LadspaPlugin> lp;
803 boost::shared_ptr<VSTPlugin> vp;
805 #ifdef HAVE_AUDIOUNITS
806 boost::shared_ptr<AUPlugin> ap;
809 PluginPtr other = plugin ();
811 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
812 return ARDOUR::LADSPA;
814 } else if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (other)) != 0) {
817 #ifdef HAVE_AUDIOUNITS
818 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
819 return ARDOUR::AudioUnit;
823 return (ARDOUR::PluginType) 0;
827 /***************************************************************
828 Port inserts: send output to a port, pick up input at a port
829 ***************************************************************/
831 PortInsert::PortInsert (Session& s, Placement p)
832 : Insert (s, string_compose (_("insert %1"), (bitslot = s.next_insert_id()) + 1), p, 1, -1, 1, -1)
835 RedirectCreated (this); /* EMIT SIGNAL */
839 PortInsert::PortInsert (const PortInsert& other)
840 : Insert (other._session, string_compose (_("insert %1"), (bitslot = other._session.next_insert_id()) + 1), other.placement(), 1, -1, 1, -1)
843 RedirectCreated (this); /* EMIT SIGNAL */
849 if (add_input_port ("", this)) {
850 error << _("PortInsert: cannot add input port") << endmsg;
851 throw failed_constructor();
854 if (add_output_port ("", this)) {
855 error << _("PortInsert: cannot add output port") << endmsg;
856 throw failed_constructor();
860 PortInsert::PortInsert (Session& s, const XMLNode& node)
861 : Insert (s, "will change", PreFader)
863 if (set_state (node)) {
864 throw failed_constructor();
867 RedirectCreated (this); /* EMIT SIGNAL */
870 PortInsert::~PortInsert ()
876 PortInsert::run (vector<Sample *>& bufs, uint32_t nbufs, nframes_t nframes, nframes_t offset)
878 if (n_outputs() == 0) {
883 /* deliver silence */
884 silence (nframes, offset);
889 vector<Port*>::iterator o;
890 vector<Port*>::iterator i;
894 for (o = _outputs.begin(), n = 0; o != _outputs.end(); ++o, ++n) {
895 memcpy ((*o)->get_buffer (nframes) + offset, bufs[min(nbufs,n)], sizeof (Sample) * nframes);
896 (*o)->mark_silence (false);
901 for (i = _inputs.begin(), n = 0; i != _inputs.end(); ++i, ++n) {
902 memcpy (bufs[min(nbufs,n)], (*i)->get_buffer (nframes) + offset, sizeof (Sample) * nframes);
907 PortInsert::get_state(void)
913 PortInsert::state (bool full)
915 XMLNode *node = new XMLNode("Insert");
917 node->add_child_nocopy (Redirect::state(full));
918 node->add_property ("type", "port");
919 snprintf (buf, sizeof (buf), "%" PRIu32, bitslot);
920 node->add_property ("bitslot", buf);
926 PortInsert::set_state(const XMLNode& node)
928 XMLNodeList nlist = node.children();
929 XMLNodeIterator niter;
930 XMLPropertyList plist;
931 const XMLProperty *prop;
933 if ((prop = node.property ("type")) == 0) {
934 error << _("XML node describing insert is missing the `type' field") << endmsg;
938 if (prop->value() != "port") {
939 error << _("non-port insert XML used for port plugin insert") << endmsg;
943 if ((prop = node.property ("bitslot")) == 0) {
944 bitslot = _session.next_insert_id();
946 sscanf (prop->value().c_str(), "%" PRIu32, &bitslot);
947 _session.mark_insert_id (bitslot);
950 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
951 if ((*niter)->name() == Redirect::state_node_name) {
952 Redirect::set_state (**niter);
957 if (niter == nlist.end()) {
958 error << _("XML node describing insert is missing a Redirect node") << endmsg;
966 PortInsert::latency()
968 /* because we deliver and collect within the same cycle,
969 all I/O is necessarily delayed by at least frames_per_cycle().
971 if the return port for insert has its own latency, we
972 need to take that into account too.
975 return _session.engine().frames_per_cycle() + input_latency();
979 PortInsert::can_support_input_configuration (int32_t in) const
981 if (input_maximum() == -1 && output_maximum() == -1) {
983 /* not configured yet */
985 return 1; /* we can support anything the first time we're asked */
989 /* the "input" config for a port insert corresponds to how
990 many output ports it will have.
993 if (output_maximum() == in) {
1002 PortInsert::configure_io (int32_t ignored_magic, int32_t in, int32_t out)
1004 /* do not allow configuration to be changed outside the range of
1005 the last request config. or something like that.
1009 /* this is a bit odd:
1011 the number of inputs we are required to handle corresponds
1012 to the number of output ports we need.
1014 the number of outputs we are required to have corresponds
1015 to the number of input ports we need.
1018 set_output_maximum (in);
1019 set_output_minimum (in);
1020 set_input_maximum (out);
1021 set_input_minimum (out);
1031 return ensure_io (out, in, false, this);
1035 PortInsert::compute_output_streams (int32_t cnt) const
1037 /* puzzling, eh? think about it ... */
1042 PortInsert::output_streams() const
1048 PortInsert::input_streams() const
1050 return n_outputs ();