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>
33 #include <ardour/buffer_set.h>
36 #include <ardour/vst_plugin.h>
39 #ifdef HAVE_AUDIOUNITS
40 #include <ardour/audio_unit.h>
43 #include <ardour/audioengine.h>
44 #include <ardour/session.h>
45 #include <ardour/types.h>
50 using namespace ARDOUR;
53 /* ********** FIXME: TYPE **************
54 * Inserts are still definitely audio only */
55 Insert::Insert(Session& s, string name, Placement p)
56 : Redirect (s, name, p)
60 Insert::Insert(Session& s, string name, Placement p, int imin, int imax, int omin, int omax)
61 : Redirect (s, name, p, imin, imax, omin, omax)
65 /***************************************************************
66 Plugin inserts: send data through a plugin
67 ***************************************************************/
69 const string PluginInsert::port_automation_node_name = "PortAutomation";
71 PluginInsert::PluginInsert (Session& s, boost::shared_ptr<Plugin> plug, Placement placement)
72 : Insert (s, plug->name(), placement)
74 /* the first is the master */
76 _plugins.push_back (plug);
78 _plugins[0]->ParameterChanged.connect (mem_fun (*this, &PluginInsert::parameter_changed));
83 Glib::Mutex::Lock em (_session.engine().process_lock());
84 IO::MoreChannels (max(input_streams(), output_streams()));
87 RedirectCreated (this); /* EMIT SIGNAL */
90 PluginInsert::PluginInsert (Session& s, const XMLNode& node)
91 : Insert (s, "will change", PreFader)
93 if (set_state (node)) {
94 throw failed_constructor();
99 _plugins[0]->ParameterChanged.connect (mem_fun (*this, &PluginInsert::parameter_changed));
102 Glib::Mutex::Lock em (_session.engine().process_lock());
103 IO::MoreChannels (max(input_streams(), output_streams()));
107 PluginInsert::PluginInsert (const PluginInsert& other)
108 : Insert (other._session, other.plugin()->name(), other.placement())
110 uint32_t count = other._plugins.size();
112 /* make as many copies as requested */
113 for (uint32_t n = 0; n < count; ++n) {
114 _plugins.push_back (plugin_factory (other.plugin (n)));
118 _plugins[0]->ParameterChanged.connect (mem_fun (*this, &PluginInsert::parameter_changed));
122 RedirectCreated (this); /* EMIT SIGNAL */
126 PluginInsert::set_count (uint32_t num)
128 bool require_state = !_plugins.empty();
130 /* this is a bad idea.... we shouldn't do this while active.
131 only a route holding their redirect_lock should be calling this
136 } else if (num > _plugins.size()) {
137 uint32_t diff = num - _plugins.size();
139 for (uint32_t n = 0; n < diff; ++n) {
140 _plugins.push_back (plugin_factory (_plugins[0]));
143 /* XXX do something */
147 } else if (num < _plugins.size()) {
148 uint32_t diff = _plugins.size() - num;
149 for (uint32_t n= 0; n < diff; ++n) {
158 PluginInsert::init ()
162 set<uint32_t>::iterator s;
165 PluginInsert::~PluginInsert ()
167 GoingAway (); /* EMIT SIGNAL */
171 PluginInsert::automation_list_creation_callback (uint32_t which, AutomationList& alist)
173 alist.automation_state_changed.connect (sigc::bind (mem_fun (*this, &PluginInsert::auto_state_changed), (which)));
177 PluginInsert::auto_state_changed (uint32_t which)
179 AutomationList& alist (automation_list (which));
181 if (alist.automation_state() != Off) {
182 _plugins[0]->set_parameter (which, alist.eval (_session.transport_frame()));
187 PluginInsert::output_streams() const
190 return ChanCount(DataType::AUDIO, _plugins[0]->get_info()->n_outputs * _plugins.size());
194 PluginInsert::input_streams() const
197 return ChanCount(DataType::AUDIO, _plugins[0]->get_info()->n_inputs * _plugins.size());
201 PluginInsert::natural_output_streams() const
204 return ChanCount(DataType::AUDIO, _plugins[0]->get_info()->n_outputs);
208 PluginInsert::natural_input_streams() const
211 return ChanCount(DataType::AUDIO, _plugins[0]->get_info()->n_inputs);
215 PluginInsert::is_generator() const
217 /* XXX more finesse is possible here. VST plugins have a
218 a specific "instrument" flag, for example.
221 return _plugins[0]->get_info()->n_inputs == 0;
225 PluginInsert::set_automatable ()
229 a = _plugins.front()->automatable ();
231 for (set<uint32_t>::iterator i = a.begin(); i != a.end(); ++i) {
237 PluginInsert::parameter_changed (uint32_t which, float val)
239 vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin();
241 /* don't set the first plugin, just all the slaves */
243 if (i != _plugins.end()) {
245 for (; i != _plugins.end(); ++i) {
246 (*i)->set_parameter (which, val);
252 PluginInsert::set_block_size (nframes_t nframes)
254 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
255 (*i)->set_block_size (nframes);
260 PluginInsert::activate ()
262 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
268 PluginInsert::deactivate ()
270 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
276 PluginInsert::connect_and_run (BufferSet& bufs, nframes_t nframes, nframes_t offset, bool with_auto, nframes_t now)
278 uint32_t in_index = 0;
279 uint32_t out_index = 0;
281 /* Note that we've already required that plugins
282 be able to handle in-place processing.
287 map<uint32_t,AutomationList*>::iterator li;
290 for (n = 0, li = parameter_automation.begin(); li != parameter_automation.end(); ++li, ++n) {
292 AutomationList& alist (*((*li).second));
294 if (alist.automation_playback()) {
297 float val = alist.rt_safe_eval (now, valid);
300 /* set the first plugin, the others will be set via signals */
301 _plugins[0]->set_parameter ((*li).first, val);
308 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
309 (*i)->connect_and_run (bufs, in_index, out_index, nframes, offset);
312 /* leave remaining channel buffers alone */
316 PluginInsert::automation_snapshot (nframes_t now)
318 map<uint32_t,AutomationList*>::iterator li;
320 for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
322 AutomationList *alist = ((*li).second);
323 if (alist != 0 && alist->automation_write ()) {
325 float val = _plugins[0]->get_parameter ((*li).first);
326 alist->rt_add (now, val);
327 last_automation_snapshot = now;
333 PluginInsert::transport_stopped (nframes_t now)
335 map<uint32_t,AutomationList*>::iterator li;
337 for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
338 AutomationList& alist (*(li->second));
339 alist.reposition_for_rt_add (now);
341 if (alist.automation_state() != Off) {
342 _plugins[0]->set_parameter (li->first, alist.eval (now));
348 PluginInsert::silence (nframes_t nframes, nframes_t offset)
350 uint32_t in_index = 0;
351 uint32_t out_index = 0;
356 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
357 n = (*i) -> get_info()->n_inputs;
358 (*i)->connect_and_run (_session.get_silent_buffers (ChanCount(DataType::AUDIO, n)), in_index, out_index, nframes, offset);
364 PluginInsert::run (BufferSet& bufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset)
368 if (_session.transport_rolling()) {
369 automation_run (bufs, nframes, offset);
371 connect_and_run (bufs, nframes, offset, false);
374 uint32_t in = _plugins[0]->get_info()->n_inputs;
375 uint32_t out = _plugins[0]->get_info()->n_outputs;
379 /* not active, but something has make up for any channel count increase */
381 for (uint32_t n = out - in; n < out; ++n) {
382 memcpy (bufs.get_audio(n).data(nframes, offset), bufs.get_audio(in - 1).data(nframes, offset), sizeof (Sample) * nframes);
386 bufs.count().set(DataType::AUDIO, out);
391 PluginInsert::set_parameter (uint32_t port, float val)
393 /* the others will be set from the event triggered by this */
395 _plugins[0]->set_parameter (port, val);
397 if (automation_list (port).automation_write()) {
398 automation_list (port).add (_session.audible_frame(), val);
401 _session.set_dirty();
405 PluginInsert::automation_run (BufferSet& bufs, nframes_t nframes, nframes_t offset)
407 ControlEvent next_event (0, 0.0f);
408 nframes_t now = _session.transport_frame ();
409 nframes_t end = now + nframes;
411 Glib::Mutex::Lock lm (_automation_lock, Glib::TRY_LOCK);
414 connect_and_run (bufs, nframes, offset, false);
418 if (!find_next_event (now, end, next_event)) {
420 /* no events have a time within the relevant range */
422 connect_and_run (bufs, nframes, offset, true, now);
428 nframes_t cnt = min (((nframes_t) floor (next_event.when) - now), nframes);
430 connect_and_run (bufs, cnt, offset, true, now);
436 if (!find_next_event (now, end, next_event)) {
441 /* cleanup anything that is left to do */
444 connect_and_run (bufs, nframes, offset, true, now);
449 PluginInsert::default_parameter_value (uint32_t port)
451 if (_plugins.empty()) {
452 fatal << _("programming error: ") << X_("PluginInsert::default_parameter_value() called with no plugin")
457 return _plugins[0]->default_value (port);
461 PluginInsert::set_port_automation_state (uint32_t port, AutoState s)
463 if (port < _plugins[0]->parameter_count()) {
465 AutomationList& al = automation_list (port);
467 if (s != al.automation_state()) {
468 al.set_automation_state (s);
469 _session.set_dirty ();
475 PluginInsert::get_port_automation_state (uint32_t port)
477 if (port < _plugins[0]->parameter_count()) {
478 return automation_list (port).automation_state();
485 PluginInsert::protect_automation ()
487 set<uint32_t> automated_params;
489 what_has_automation (automated_params);
491 for (set<uint32_t>::iterator i = automated_params.begin(); i != automated_params.end(); ++i) {
493 AutomationList& al = automation_list (*i);
495 switch (al.automation_state()) {
498 al.set_automation_state (Off);
506 boost::shared_ptr<Plugin>
507 PluginInsert::plugin_factory (boost::shared_ptr<Plugin> other)
509 boost::shared_ptr<LadspaPlugin> lp;
511 boost::shared_ptr<VSTPlugin> vp;
513 #ifdef HAVE_AUDIOUNITS
514 boost::shared_ptr<AUPlugin> ap;
517 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
518 return boost::shared_ptr<Plugin> (new LadspaPlugin (*lp));
520 } else if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (other)) != 0) {
521 return boost::shared_ptr<Plugin> (new VSTPlugin (*vp));
523 #ifdef HAVE_AUDIOUNITS
524 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
525 return boost::shared_ptr<Plugin> (new AUPlugin (*ap));
529 fatal << string_compose (_("programming error: %1"),
530 X_("unknown plugin type in PluginInsert::plugin_factory"))
533 return boost::shared_ptr<Plugin> ((Plugin*) 0);
537 PluginInsert::compute_output_streams (int32_t cnt) const
539 return _plugins[0]->get_info()->n_outputs * cnt;
543 PluginInsert::configure_io (int32_t magic, int32_t in, int32_t out)
545 return set_count (magic);
549 PluginInsert::can_support_input_configuration (int32_t in) const
551 int32_t outputs = _plugins[0]->get_info()->n_outputs;
552 int32_t inputs = _plugins[0]->get_info()->n_inputs;
556 /* instrument plugin, always legal, but it throws
557 away any existing active streams.
563 if (outputs == 1 && inputs == 1) {
564 /* mono plugin, replicate as needed */
573 if ((inputs < in) && (inputs % in == 0)) {
575 /* number of inputs is a factor of the requested input
576 configuration, so we can replicate.
588 PluginInsert::get_state(void)
594 PluginInsert::state (bool full)
597 XMLNode *node = new XMLNode("Insert");
599 node->add_child_nocopy (Redirect::state (full));
601 node->add_property ("type", _plugins[0]->state_node_name());
602 snprintf(buf, sizeof(buf), "%s", _plugins[0]->name());
603 node->add_property("id", string(buf));
604 if (_plugins[0]->state_node_name() == "ladspa") {
606 snprintf (buf, sizeof (buf), "%ld", _plugins[0]->get_info()->unique_id);
607 node->add_property("unique-id", string(buf));
609 node->add_property("count", string_compose("%1", _plugins.size()));
610 node->add_child_nocopy (_plugins[0]->get_state());
612 /* add port automation state */
613 XMLNode *autonode = new XMLNode(port_automation_node_name);
614 set<uint32_t> automatable = _plugins[0]->automatable();
616 for (set<uint32_t>::iterator x = automatable.begin(); x != automatable.end(); ++x) {
618 XMLNode* child = new XMLNode("port");
619 snprintf(buf, sizeof(buf), "%" PRIu32, *x);
620 child->add_property("number", string(buf));
622 child->add_child_nocopy (automation_list (*x).state (full));
623 autonode->add_child_nocopy (*child);
626 node->add_child_nocopy (*autonode);
632 PluginInsert::set_state(const XMLNode& node)
634 XMLNodeList nlist = node.children();
635 XMLNodeIterator niter;
636 XMLPropertyList plist;
637 const XMLProperty *prop;
639 ARDOUR::PluginType type;
641 if ((prop = node.property ("type")) == 0) {
642 error << _("XML node describing insert is missing the `type' field") << endmsg;
646 if (prop->value() == X_("ladspa") || prop->value() == X_("Ladspa")) { /* handle old school sessions */
647 type = ARDOUR::LADSPA;
648 } else if (prop->value() == X_("vst")) {
651 error << string_compose (_("unknown plugin type %1 in plugin insert state"),
657 prop = node.property ("unique-id");
659 unique = atol(prop->value().c_str());
662 if ((prop = node.property ("id")) == 0) {
663 error << _("XML node describing insert is missing the `id' field") << endmsg;
667 boost::shared_ptr<Plugin> plugin;
670 plugin = find_plugin (_session, "", unique, type);
672 plugin = find_plugin (_session, prop->value(), 0, type);
676 error << string_compose(_("Found a reference to a plugin (\"%1\") that is unknown.\n"
677 "Perhaps it was removed or moved since it was last used."), prop->value())
684 if ((prop = node.property ("count")) != 0) {
685 sscanf (prop->value().c_str(), "%u", &count);
688 if (_plugins.size() != count) {
690 _plugins.push_back (plugin);
692 for (uint32_t n=1; n < count; ++n) {
693 _plugins.push_back (plugin_factory (plugin));
697 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
698 if ((*niter)->name() == plugin->state_node_name()) {
699 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
700 (*i)->set_state (**niter);
706 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
707 if ((*niter)->name() == Redirect::state_node_name) {
708 Redirect::set_state (**niter);
713 if (niter == nlist.end()) {
714 error << _("XML node describing insert is missing a Redirect node") << endmsg;
718 if (niter == nlist.end()) {
719 error << string_compose(_("XML node describing a plugin insert is missing the `%1' information"), plugin->state_node_name()) << endmsg;
723 /* look for port automation node */
725 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
727 if ((*niter)->name() != port_automation_node_name) {
733 XMLNodeConstIterator iter;
738 cnodes = (*niter)->children ("port");
740 for(iter = cnodes.begin(); iter != cnodes.end(); ++iter){
744 if ((cprop = child->property("number")) != 0) {
745 port = cprop->value().c_str();
747 warning << _("PluginInsert: Auto: no ladspa port number") << endmsg;
751 sscanf (port, "%" PRIu32, &port_id);
753 if (port_id >= _plugins[0]->parameter_count()) {
754 warning << _("PluginInsert: Auto: port id out of range") << endmsg;
758 if (!child->children().empty()) {
759 automation_list (port_id).set_state (*child->children().front());
761 if ((cprop = child->property("auto")) != 0) {
766 sscanf (cprop->value().c_str(), "0x%x", &x);
767 automation_list (port_id).set_automation_state (AutoState (x));
773 automation_list (port_id).set_automation_state (Off);
784 if (niter == nlist.end()) {
785 warning << string_compose(_("XML node describing a port automation is missing the `%1' information"), port_automation_node_name) << endmsg;
788 // The name of the PluginInsert comes from the plugin, nothing else
789 set_name(plugin->get_info()->name,this);
795 PluginInsert::describe_parameter (uint32_t what)
797 return _plugins[0]->describe_parameter (what);
801 PluginInsert::latency()
803 return _plugins[0]->latency ();
807 PluginInsert::type ()
809 boost::shared_ptr<LadspaPlugin> lp;
811 boost::shared_ptr<VSTPlugin> vp;
813 #ifdef HAVE_AUDIOUNITS
814 boost::shared_ptr<AUPlugin> ap;
817 PluginPtr other = plugin ();
819 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
820 return ARDOUR::LADSPA;
822 } else if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (other)) != 0) {
825 #ifdef HAVE_AUDIOUNITS
826 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
827 return ARDOUR::AudioUnit;
831 return (ARDOUR::PluginType) 0;
835 /***************************************************************
836 Port inserts: send output to a port, pick up input at a port
837 ***************************************************************/
839 PortInsert::PortInsert (Session& s, Placement p)
840 : Insert (s, string_compose (_("insert %1"), (bitslot = s.next_insert_id()) + 1), p, 1, -1, 1, -1)
843 RedirectCreated (this); /* EMIT SIGNAL */
846 PortInsert::PortInsert (const PortInsert& other)
847 : Insert (other._session, string_compose (_("insert %1"), (bitslot = other._session.next_insert_id()) + 1), other.placement(), 1, -1, 1, -1)
850 RedirectCreated (this); /* EMIT SIGNAL */
856 if (add_input_port ("", this)) {
857 error << _("PortInsert: cannot add input port") << endmsg;
858 throw failed_constructor();
861 if (add_output_port ("", this)) {
862 error << _("PortInsert: cannot add output port") << endmsg;
863 throw failed_constructor();
867 PortInsert::PortInsert (Session& s, const XMLNode& node)
868 : Insert (s, "will change", PreFader)
870 if (set_state (node)) {
871 throw failed_constructor();
874 RedirectCreated (this); /* EMIT SIGNAL */
877 PortInsert::~PortInsert ()
883 PortInsert::run (BufferSet& bufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset)
885 if (n_outputs().get(_default_type) == 0) {
890 /* deliver silence */
891 silence (nframes, offset);
895 deliver_output(bufs, start_frame, end_frame, nframes, offset);
897 collect_input(bufs, nframes, offset);
901 PortInsert::get_state(void)
907 PortInsert::state (bool full)
909 XMLNode *node = new XMLNode("Insert");
911 node->add_child_nocopy (Redirect::state(full));
912 node->add_property ("type", "port");
913 snprintf (buf, sizeof (buf), "%" PRIu32, bitslot);
914 node->add_property ("bitslot", buf);
920 PortInsert::set_state(const XMLNode& node)
922 XMLNodeList nlist = node.children();
923 XMLNodeIterator niter;
924 XMLPropertyList plist;
925 const XMLProperty *prop;
927 if ((prop = node.property ("type")) == 0) {
928 error << _("XML node describing insert is missing the `type' field") << endmsg;
932 if (prop->value() != "port") {
933 error << _("non-port insert XML used for port plugin insert") << endmsg;
937 if ((prop = node.property ("bitslot")) == 0) {
938 bitslot = _session.next_insert_id();
940 sscanf (prop->value().c_str(), "%" PRIu32, &bitslot);
941 _session.mark_insert_id (bitslot);
944 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
945 if ((*niter)->name() == Redirect::state_node_name) {
946 Redirect::set_state (**niter);
951 if (niter == nlist.end()) {
952 error << _("XML node describing insert is missing a Redirect node") << endmsg;
960 PortInsert::latency()
962 /* because we deliver and collect within the same cycle,
963 all I/O is necessarily delayed by at least frames_per_cycle().
965 if the return port for insert has its own latency, we
966 need to take that into account too.
969 return _session.engine().frames_per_cycle() + input_latency();
973 PortInsert::can_support_input_configuration (int32_t in) const
975 if (input_maximum() == ChanCount::INFINITE && output_maximum() == ChanCount::INFINITE) {
977 /* not configured yet */
979 return 1; /* we can support anything the first time we're asked */
983 /* the "input" config for a port insert corresponds to how
984 many output ports it will have.
987 if (output_maximum().get(_default_type) == static_cast<uint32_t>(in)) {
996 PortInsert::configure_io (int32_t ignored_magic, int32_t in, int32_t out)
998 /* do not allow configuration to be changed outside the range of
999 the last request config. or something like that.
1003 /* this is a bit odd:
1005 the number of inputs we are required to handle corresponds
1006 to the number of output ports we need.
1008 the number of outputs we are required to have corresponds
1009 to the number of input ports we need.
1012 set_output_maximum (ChanCount(_default_type, in));
1013 set_output_minimum (ChanCount(_default_type, in));
1014 set_input_maximum (ChanCount(_default_type, out));
1015 set_input_minimum (ChanCount(_default_type, out));
1018 in = n_outputs ().get(_default_type);
1022 out = n_inputs ().get(_default_type);
1025 return ensure_io (ChanCount(_default_type, out), ChanCount(_default_type, in), false, this);
1029 PortInsert::compute_output_streams (int32_t cnt) const
1031 /* puzzling, eh? think about it ... */
1032 return n_inputs ().get(_default_type);
1036 PortInsert::output_streams() const
1042 PortInsert::input_streams() const
1044 return n_outputs ();