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 */
56 Insert::Insert(Session& s, Placement p)
57 : Redirect (s, s.next_insert_name(), p)
61 Insert::Insert(Session& s, Placement p, int imin, int imax, int omin, int omax)
62 : Redirect (s, s.next_insert_name(), p, imin, imax, omin, omax)
66 Insert::Insert(Session& s, string name, Placement p)
67 : Redirect (s, name, p)
71 /***************************************************************
72 Plugin inserts: send data through a plugin
73 ***************************************************************/
75 const string PluginInsert::port_automation_node_name = "PortAutomation";
77 PluginInsert::PluginInsert (Session& s, boost::shared_ptr<Plugin> plug, Placement placement)
78 : Insert (s, plug->name(), placement)
80 /* the first is the master */
82 _plugins.push_back (plug);
84 _plugins[0]->ParameterChanged.connect (mem_fun (*this, &PluginInsert::parameter_changed));
89 Glib::Mutex::Lock em (_session.engine().process_lock());
90 IO::MoreChannels (max(input_streams(), output_streams()));
93 RedirectCreated (this); /* EMIT SIGNAL */
96 PluginInsert::PluginInsert (Session& s, const XMLNode& node)
97 : Insert (s, "will change", PreFader)
99 if (set_state (node)) {
100 throw failed_constructor();
105 _plugins[0]->ParameterChanged.connect (mem_fun (*this, &PluginInsert::parameter_changed));
108 Glib::Mutex::Lock em (_session.engine().process_lock());
109 IO::MoreChannels (max(input_streams(), output_streams()));
113 PluginInsert::PluginInsert (const PluginInsert& other)
114 : Insert (other._session, other.plugin()->name(), other.placement())
116 uint32_t count = other._plugins.size();
118 /* make as many copies as requested */
119 for (uint32_t n = 0; n < count; ++n) {
120 _plugins.push_back (plugin_factory (other.plugin (n)));
124 _plugins[0]->ParameterChanged.connect (mem_fun (*this, &PluginInsert::parameter_changed));
128 RedirectCreated (this); /* EMIT SIGNAL */
132 PluginInsert::set_count (uint32_t num)
134 bool require_state = !_plugins.empty();
136 /* this is a bad idea.... we shouldn't do this while active.
137 only a route holding their redirect_lock should be calling this
142 } else if (num > _plugins.size()) {
143 uint32_t diff = num - _plugins.size();
145 for (uint32_t n = 0; n < diff; ++n) {
146 _plugins.push_back (plugin_factory (_plugins[0]));
149 /* XXX do something */
153 } else if (num < _plugins.size()) {
154 uint32_t diff = _plugins.size() - num;
155 for (uint32_t n= 0; n < diff; ++n) {
164 PluginInsert::init ()
168 set<uint32_t>::iterator s;
171 PluginInsert::~PluginInsert ()
173 GoingAway (); /* EMIT SIGNAL */
177 PluginInsert::automation_list_creation_callback (uint32_t which, AutomationList& alist)
179 alist.automation_state_changed.connect (sigc::bind (mem_fun (*this, &PluginInsert::auto_state_changed), (which)));
183 PluginInsert::auto_state_changed (uint32_t which)
185 AutomationList& alist (automation_list (which));
187 if (alist.automation_state() != Off) {
188 _plugins[0]->set_parameter (which, alist.eval (_session.transport_frame()));
193 PluginInsert::output_streams() const
196 return ChanCount(DataType::AUDIO, _plugins[0]->get_info()->n_outputs * _plugins.size());
200 PluginInsert::input_streams() const
203 return ChanCount(DataType::AUDIO, _plugins[0]->get_info()->n_inputs * _plugins.size());
207 PluginInsert::natural_output_streams() const
210 return ChanCount(DataType::AUDIO, _plugins[0]->get_info()->n_outputs);
214 PluginInsert::natural_input_streams() const
217 return ChanCount(DataType::AUDIO, _plugins[0]->get_info()->n_inputs);
221 PluginInsert::is_generator() const
223 /* XXX more finesse is possible here. VST plugins have a
224 a specific "instrument" flag, for example.
227 return _plugins[0]->get_info()->n_inputs == 0;
231 PluginInsert::set_automatable ()
235 a = _plugins.front()->automatable ();
237 for (set<uint32_t>::iterator i = a.begin(); i != a.end(); ++i) {
243 PluginInsert::parameter_changed (uint32_t which, float val)
245 vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin();
247 /* don't set the first plugin, just all the slaves */
249 if (i != _plugins.end()) {
251 for (; i != _plugins.end(); ++i) {
252 (*i)->set_parameter (which, val);
258 PluginInsert::set_block_size (nframes_t nframes)
260 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
261 (*i)->set_block_size (nframes);
266 PluginInsert::activate ()
268 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
274 PluginInsert::deactivate ()
276 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
282 PluginInsert::connect_and_run (BufferSet& bufs, nframes_t nframes, nframes_t offset, bool with_auto, nframes_t now)
284 uint32_t in_index = 0;
285 uint32_t out_index = 0;
287 /* Note that we've already required that plugins
288 be able to handle in-place processing.
293 map<uint32_t,AutomationList*>::iterator li;
296 for (n = 0, li = parameter_automation.begin(); li != parameter_automation.end(); ++li, ++n) {
298 AutomationList& alist (*((*li).second));
300 if (alist.automation_playback()) {
303 float val = alist.rt_safe_eval (now, valid);
306 /* set the first plugin, the others will be set via signals */
307 _plugins[0]->set_parameter ((*li).first, val);
314 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
315 (*i)->connect_and_run (bufs, in_index, out_index, nframes, offset);
318 /* leave remaining channel buffers alone */
322 PluginInsert::automation_snapshot (nframes_t now)
324 map<uint32_t,AutomationList*>::iterator li;
326 for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
328 AutomationList *alist = ((*li).second);
329 if (alist != 0 && alist->automation_write ()) {
331 float val = _plugins[0]->get_parameter ((*li).first);
332 alist->rt_add (now, val);
333 last_automation_snapshot = now;
339 PluginInsert::transport_stopped (nframes_t now)
341 map<uint32_t,AutomationList*>::iterator li;
343 for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
344 AutomationList& alist (*(li->second));
345 alist.reposition_for_rt_add (now);
347 if (alist.automation_state() != Off) {
348 _plugins[0]->set_parameter (li->first, alist.eval (now));
354 PluginInsert::silence (nframes_t nframes, nframes_t offset)
356 uint32_t in_index = 0;
357 uint32_t out_index = 0;
362 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
363 n = (*i) -> get_info()->n_inputs;
364 (*i)->connect_and_run (_session.get_silent_buffers (ChanCount(DataType::AUDIO, n)), in_index, out_index, nframes, offset);
370 PluginInsert::run (BufferSet& bufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset)
374 if (_session.transport_rolling()) {
375 automation_run (bufs, nframes, offset);
377 connect_and_run (bufs, nframes, offset, false);
380 uint32_t in = _plugins[0]->get_info()->n_inputs;
381 uint32_t out = _plugins[0]->get_info()->n_outputs;
385 /* not active, but something has make up for any channel count increase */
387 for (uint32_t n = out - in; n < out; ++n) {
388 memcpy (bufs.get_audio(n).data(nframes, offset), bufs.get_audio(in - 1).data(nframes, offset), sizeof (Sample) * nframes);
392 bufs.count().set(DataType::AUDIO, out);
397 PluginInsert::set_parameter (uint32_t port, float val)
399 /* the others will be set from the event triggered by this */
401 _plugins[0]->set_parameter (port, val);
403 if (automation_list (port).automation_write()) {
404 automation_list (port).add (_session.audible_frame(), val);
407 _session.set_dirty();
411 PluginInsert::automation_run (BufferSet& bufs, nframes_t nframes, nframes_t offset)
413 ControlEvent next_event (0, 0.0f);
414 nframes_t now = _session.transport_frame ();
415 nframes_t end = now + nframes;
417 Glib::Mutex::Lock lm (_automation_lock, Glib::TRY_LOCK);
420 connect_and_run (bufs, nframes, offset, false);
424 if (!find_next_event (now, end, next_event)) {
426 /* no events have a time within the relevant range */
428 connect_and_run (bufs, nframes, offset, true, now);
434 nframes_t cnt = min (((nframes_t) floor (next_event.when) - now), nframes);
436 connect_and_run (bufs, cnt, offset, true, now);
442 if (!find_next_event (now, end, next_event)) {
447 /* cleanup anything that is left to do */
450 connect_and_run (bufs, nframes, offset, true, now);
455 PluginInsert::default_parameter_value (uint32_t port)
457 if (_plugins.empty()) {
458 fatal << _("programming error: ") << X_("PluginInsert::default_parameter_value() called with no plugin")
463 return _plugins[0]->default_value (port);
467 PluginInsert::set_port_automation_state (uint32_t port, AutoState s)
469 if (port < _plugins[0]->parameter_count()) {
471 AutomationList& al = automation_list (port);
473 if (s != al.automation_state()) {
474 al.set_automation_state (s);
475 _session.set_dirty ();
481 PluginInsert::get_port_automation_state (uint32_t port)
483 if (port < _plugins[0]->parameter_count()) {
484 return automation_list (port).automation_state();
491 PluginInsert::protect_automation ()
493 set<uint32_t> automated_params;
495 what_has_automation (automated_params);
497 for (set<uint32_t>::iterator i = automated_params.begin(); i != automated_params.end(); ++i) {
499 AutomationList& al = automation_list (*i);
501 switch (al.automation_state()) {
504 al.set_automation_state (Off);
512 boost::shared_ptr<Plugin>
513 PluginInsert::plugin_factory (boost::shared_ptr<Plugin> other)
515 boost::shared_ptr<LadspaPlugin> lp;
517 boost::shared_ptr<VSTPlugin> vp;
519 #ifdef HAVE_AUDIOUNITS
520 boost::shared_ptr<AUPlugin> ap;
523 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
524 return boost::shared_ptr<Plugin> (new LadspaPlugin (*lp));
526 } else if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (other)) != 0) {
527 return boost::shared_ptr<Plugin> (new VSTPlugin (*vp));
529 #ifdef HAVE_AUDIOUNITS
530 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
531 return boost::shared_ptr<Plugin> (new AUPlugin (*ap));
535 fatal << string_compose (_("programming error: %1"),
536 X_("unknown plugin type in PluginInsert::plugin_factory"))
539 return boost::shared_ptr<Plugin> ((Plugin*) 0);
543 PluginInsert::compute_output_streams (int32_t cnt) const
545 return _plugins[0]->get_info()->n_outputs * cnt;
549 PluginInsert::configure_io (int32_t magic, int32_t in, int32_t out)
551 return set_count (magic);
555 PluginInsert::can_support_input_configuration (int32_t in) const
557 int32_t outputs = _plugins[0]->get_info()->n_outputs;
558 int32_t inputs = _plugins[0]->get_info()->n_inputs;
562 /* instrument plugin, always legal, but it throws
563 away any existing active streams.
569 if (outputs == 1 && inputs == 1) {
570 /* mono plugin, replicate as needed */
579 if ((inputs < in) && (inputs % in == 0)) {
581 /* number of inputs is a factor of the requested input
582 configuration, so we can replicate.
594 PluginInsert::get_state(void)
600 PluginInsert::state (bool full)
603 XMLNode *node = new XMLNode("Insert");
605 node->add_child_nocopy (Redirect::state (full));
607 node->add_property ("type", _plugins[0]->state_node_name());
608 snprintf(buf, sizeof(buf), "%s", _plugins[0]->name());
609 node->add_property("id", string(buf));
610 if (_plugins[0]->state_node_name() == "ladspa") {
612 snprintf (buf, sizeof (buf), "%ld", _plugins[0]->get_info()->unique_id);
613 node->add_property("unique-id", string(buf));
615 node->add_property("count", string_compose("%1", _plugins.size()));
616 node->add_child_nocopy (_plugins[0]->get_state());
618 /* add port automation state */
619 XMLNode *autonode = new XMLNode(port_automation_node_name);
620 set<uint32_t> automatable = _plugins[0]->automatable();
622 for (set<uint32_t>::iterator x = automatable.begin(); x != automatable.end(); ++x) {
624 XMLNode* child = new XMLNode("port");
625 snprintf(buf, sizeof(buf), "%" PRIu32, *x);
626 child->add_property("number", string(buf));
629 snprintf(buf, sizeof(buf), "0x%x", automation_list (*x).automation_state ());
631 snprintf(buf, sizeof(buf), "0x%x", ARDOUR::Off);
633 child->add_property("auto", string(buf));
635 autonode->add_child_nocopy (*child);
638 node->add_child_nocopy (*autonode);
644 PluginInsert::set_state(const XMLNode& node)
646 XMLNodeList nlist = node.children();
647 XMLNodeIterator niter;
648 XMLPropertyList plist;
649 const XMLProperty *prop;
651 ARDOUR::PluginType type;
653 if ((prop = node.property ("type")) == 0) {
654 error << _("XML node describing insert is missing the `type' field") << endmsg;
658 if (prop->value() == X_("ladspa") || prop->value() == X_("Ladspa")) { /* handle old school sessions */
659 type = ARDOUR::LADSPA;
660 } else if (prop->value() == X_("vst")) {
663 error << string_compose (_("unknown plugin type %1 in plugin insert state"),
669 prop = node.property ("unique-id");
671 unique = atol(prop->value().c_str());
674 if ((prop = node.property ("id")) == 0) {
675 error << _("XML node describing insert is missing the `id' field") << endmsg;
679 boost::shared_ptr<Plugin> plugin;
682 plugin = find_plugin (_session, "", unique, type);
684 plugin = find_plugin (_session, prop->value(), 0, type);
688 error << string_compose(_("Found a reference to a plugin (\"%1\") that is unknown.\n"
689 "Perhaps it was removed or moved since it was last used."), prop->value())
696 if ((prop = node.property ("count")) != 0) {
697 sscanf (prop->value().c_str(), "%u", &count);
700 if (_plugins.size() != count) {
702 _plugins.push_back (plugin);
704 for (uint32_t n=1; n < count; ++n) {
705 _plugins.push_back (plugin_factory (plugin));
709 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
710 if ((*niter)->name() == plugin->state_node_name()) {
711 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
712 (*i)->set_state (**niter);
718 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
719 if ((*niter)->name() == Redirect::state_node_name) {
720 Redirect::set_state (**niter);
725 if (niter == nlist.end()) {
726 error << _("XML node describing insert is missing a Redirect node") << endmsg;
730 if (niter == nlist.end()) {
731 error << string_compose(_("XML node describing a plugin insert is missing the `%1' information"), plugin->state_node_name()) << endmsg;
735 /* look for port automation node */
737 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
738 if ((*niter)->name() == port_automation_node_name) {
741 XMLNodeConstIterator iter;
746 cnodes = (*niter)->children ("port");
748 for(iter = cnodes.begin(); iter != cnodes.end(); ++iter){
752 if ((cprop = child->property("number")) != 0) {
753 port = cprop->value().c_str();
755 warning << _("PluginInsert: Auto: no ladspa port number") << endmsg;
759 sscanf (port, "%" PRIu32, &port_id);
761 if (port_id >= _plugins[0]->parameter_count()) {
762 warning << _("PluginInsert: Auto: port id out of range") << endmsg;
766 if ((cprop = child->property("auto")) != 0) {
768 sscanf (cprop->value().c_str(), "0x%x", &x);
769 automation_list (port_id).set_automation_state (AutoState (x));
777 if (niter == nlist.end()) {
778 warning << string_compose(_("XML node describing a port automation is missing the `%1' information"), port_automation_node_name) << endmsg;
781 // The name of the PluginInsert comes from the plugin, nothing else
782 set_name(plugin->get_info()->name,this);
788 PluginInsert::describe_parameter (uint32_t what)
790 return _plugins[0]->describe_parameter (what);
794 PluginInsert::latency()
796 return _plugins[0]->latency ();
800 PluginInsert::type ()
802 boost::shared_ptr<LadspaPlugin> lp;
804 boost::shared_ptr<VSTPlugin> vp;
806 #ifdef HAVE_AUDIOUNITS
807 boost::shared_ptr<AUPlugin> ap;
810 PluginPtr other = plugin ();
812 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
813 return ARDOUR::LADSPA;
815 } else if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (other)) != 0) {
818 #ifdef HAVE_AUDIOUNITS
819 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
820 return ARDOUR::AudioUnit;
824 return (ARDOUR::PluginType) 0;
828 /***************************************************************
829 Port inserts: send output to a port, pick up input at a port
830 ***************************************************************/
832 PortInsert::PortInsert (Session& s, Placement p)
833 : Insert (s, p, 1, -1, 1, -1)
836 RedirectCreated (this); /* EMIT SIGNAL */
839 PortInsert::PortInsert (const PortInsert& other)
840 : Insert (other._session, 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 (BufferSet& bufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset)
878 if (n_outputs().get(_default_type) == 0) {
883 /* deliver silence */
884 silence (nframes, offset);
888 deliver_output(bufs, start_frame, end_frame, nframes, offset);
890 collect_input(bufs, nframes, offset);
894 PortInsert::get_state(void)
900 PortInsert::state (bool full)
902 XMLNode *node = new XMLNode("Insert");
904 node->add_child_nocopy (Redirect::state(full));
905 node->add_property("type", "port");
911 PortInsert::set_state(const XMLNode& node)
913 XMLNodeList nlist = node.children();
914 XMLNodeIterator niter;
915 XMLPropertyList plist;
916 const XMLProperty *prop;
918 if ((prop = node.property ("type")) == 0) {
919 error << _("XML node describing insert is missing the `type' field") << endmsg;
923 if (prop->value() != "port") {
924 error << _("non-port insert XML used for port plugin insert") << endmsg;
928 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
929 if ((*niter)->name() == Redirect::state_node_name) {
930 Redirect::set_state (**niter);
935 if (niter == nlist.end()) {
936 error << _("XML node describing insert is missing a Redirect node") << endmsg;
944 PortInsert::latency()
946 /* because we deliver and collect within the same cycle,
947 all I/O is necessarily delayed by at least frames_per_cycle().
949 if the return port for insert has its own latency, we
950 need to take that into account too.
953 return _session.engine().frames_per_cycle() + input_latency();
957 PortInsert::can_support_input_configuration (int32_t in) const
959 if (input_maximum() == ChanCount::INFINITE && output_maximum() == ChanCount::INFINITE) {
961 /* not configured yet */
963 return 1; /* we can support anything the first time we're asked */
967 /* the "input" config for a port insert corresponds to how
968 many output ports it will have.
971 if (output_maximum().get(_default_type) == static_cast<uint32_t>(in)) {
980 PortInsert::configure_io (int32_t ignored_magic, int32_t in, int32_t out)
982 /* do not allow configuration to be changed outside the range of
983 the last request config. or something like that.
987 /* this is a bit odd:
989 the number of inputs we are required to handle corresponds
990 to the number of output ports we need.
992 the number of outputs we are required to have corresponds
993 to the number of input ports we need.
996 set_output_maximum (ChanCount(_default_type, in));
997 set_output_minimum (ChanCount(_default_type, in));
998 set_input_maximum (ChanCount(_default_type, out));
999 set_input_minimum (ChanCount(_default_type, out));
1002 in = n_outputs ().get(_default_type);
1006 out = n_inputs ().get(_default_type);
1009 return ensure_io (ChanCount(_default_type, out), ChanCount(_default_type, in), false, this);
1013 PortInsert::compute_output_streams (int32_t cnt) const
1015 /* puzzling, eh? think about it ... */
1016 return n_inputs ().get(_default_type);
1020 PortInsert::output_streams() const
1026 PortInsert::input_streams() const
1028 return n_outputs ();