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>
32 #include <ardour/buffer_set.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 /* ********** FIXME: TYPE **************
53 * Inserts are still definitely audio only */
54 Insert::Insert(Session& s, string name, Placement p)
55 : Redirect (s, name, p)
59 Insert::Insert(Session& s, string name, Placement p, int imin, int imax, int omin, int omax)
60 : Redirect (s, name, p, imin, imax, omin, omax)
64 /***************************************************************
65 Plugin inserts: send data through a plugin
66 ***************************************************************/
68 const string PluginInsert::port_automation_node_name = "PortAutomation";
70 PluginInsert::PluginInsert (Session& s, boost::shared_ptr<Plugin> plug, Placement placement)
71 : Insert (s, plug->name(), placement)
73 /* the first is the master */
75 _plugins.push_back (plug);
77 _plugins[0]->ParameterChanged.connect (mem_fun (*this, &PluginInsert::parameter_changed));
82 Glib::Mutex::Lock em (_session.engine().process_lock());
83 IO::MoreChannels (max(input_streams(), output_streams()));
86 RedirectCreated (this); /* EMIT SIGNAL */
89 PluginInsert::PluginInsert (Session& s, const XMLNode& node)
90 : Insert (s, "will change", PreFader)
92 if (set_state (node)) {
93 throw failed_constructor();
98 _plugins[0]->ParameterChanged.connect (mem_fun (*this, &PluginInsert::parameter_changed));
101 Glib::Mutex::Lock em (_session.engine().process_lock());
102 IO::MoreChannels (max(input_streams(), output_streams()));
106 PluginInsert::PluginInsert (const PluginInsert& other)
107 : Insert (other._session, other.plugin()->name(), other.placement())
109 uint32_t count = other._plugins.size();
111 /* make as many copies as requested */
112 for (uint32_t n = 0; n < count; ++n) {
113 _plugins.push_back (plugin_factory (other.plugin (n)));
117 _plugins[0]->ParameterChanged.connect (mem_fun (*this, &PluginInsert::parameter_changed));
121 RedirectCreated (this); /* EMIT SIGNAL */
125 PluginInsert::set_count (uint32_t num)
127 bool require_state = !_plugins.empty();
129 /* this is a bad idea.... we shouldn't do this while active.
130 only a route holding their redirect_lock should be calling this
135 } else if (num > _plugins.size()) {
136 uint32_t diff = num - _plugins.size();
138 for (uint32_t n = 0; n < diff; ++n) {
139 _plugins.push_back (plugin_factory (_plugins[0]));
142 /* XXX do something */
146 } else if (num < _plugins.size()) {
147 uint32_t diff = _plugins.size() - num;
148 for (uint32_t n= 0; n < diff; ++n) {
157 PluginInsert::init ()
161 set<uint32_t>::iterator s;
164 PluginInsert::~PluginInsert ()
166 GoingAway (); /* EMIT SIGNAL */
170 PluginInsert::automation_list_creation_callback (uint32_t which, AutomationList& alist)
172 alist.automation_state_changed.connect (sigc::bind (mem_fun (*this, &PluginInsert::auto_state_changed), (which)));
176 PluginInsert::auto_state_changed (uint32_t which)
178 AutomationList& alist (automation_list (which));
180 if (alist.automation_state() != Off) {
181 _plugins[0]->set_parameter (which, alist.eval (_session.transport_frame()));
186 PluginInsert::output_streams() const
189 return ChanCount(DataType::AUDIO, _plugins[0]->get_info()->n_outputs * _plugins.size());
193 PluginInsert::input_streams() const
196 return ChanCount(DataType::AUDIO, _plugins[0]->get_info()->n_inputs * _plugins.size());
200 PluginInsert::natural_output_streams() const
203 return ChanCount(DataType::AUDIO, _plugins[0]->get_info()->n_outputs);
207 PluginInsert::natural_input_streams() const
210 return ChanCount(DataType::AUDIO, _plugins[0]->get_info()->n_inputs);
214 PluginInsert::is_generator() const
216 /* XXX more finesse is possible here. VST plugins have a
217 a specific "instrument" flag, for example.
220 return _plugins[0]->get_info()->n_inputs == 0;
224 PluginInsert::set_automatable ()
228 a = _plugins.front()->automatable ();
230 for (set<uint32_t>::iterator i = a.begin(); i != a.end(); ++i) {
236 PluginInsert::parameter_changed (uint32_t which, float val)
238 vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin();
240 /* don't set the first plugin, just all the slaves */
242 if (i != _plugins.end()) {
244 for (; i != _plugins.end(); ++i) {
245 (*i)->set_parameter (which, val);
251 PluginInsert::set_block_size (nframes_t nframes)
253 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
254 (*i)->set_block_size (nframes);
259 PluginInsert::activate ()
261 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
267 PluginInsert::deactivate ()
269 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
275 PluginInsert::connect_and_run (BufferSet& bufs, nframes_t nframes, nframes_t offset, bool with_auto, nframes_t now)
277 uint32_t in_index = 0;
278 uint32_t out_index = 0;
280 /* Note that we've already required that plugins
281 be able to handle in-place processing.
286 map<uint32_t,AutomationList*>::iterator li;
289 for (n = 0, li = parameter_automation.begin(); li != parameter_automation.end(); ++li, ++n) {
291 AutomationList& alist (*((*li).second));
293 if (alist.automation_playback()) {
296 float val = alist.rt_safe_eval (now, valid);
299 /* set the first plugin, the others will be set via signals */
300 _plugins[0]->set_parameter ((*li).first, val);
307 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
308 (*i)->connect_and_run (bufs, in_index, out_index, nframes, offset);
311 /* leave remaining channel buffers alone */
315 PluginInsert::automation_snapshot (nframes_t now)
317 map<uint32_t,AutomationList*>::iterator li;
319 for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
321 AutomationList *alist = ((*li).second);
322 if (alist != 0 && alist->automation_write ()) {
324 float val = _plugins[0]->get_parameter ((*li).first);
325 alist->rt_add (now, val);
326 last_automation_snapshot = now;
332 PluginInsert::transport_stopped (nframes_t now)
334 map<uint32_t,AutomationList*>::iterator li;
336 for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
337 AutomationList& alist (*(li->second));
338 alist.reposition_for_rt_add (now);
340 if (alist.automation_state() != Off) {
341 _plugins[0]->set_parameter (li->first, alist.eval (now));
347 PluginInsert::silence (nframes_t nframes, nframes_t offset)
349 uint32_t in_index = 0;
350 uint32_t out_index = 0;
355 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
356 n = (*i) -> get_info()->n_inputs;
357 (*i)->connect_and_run (_session.get_silent_buffers (ChanCount(DataType::AUDIO, n)), in_index, out_index, nframes, offset);
363 PluginInsert::run (BufferSet& bufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset)
367 if (_session.transport_rolling()) {
368 automation_run (bufs, nframes, offset);
370 connect_and_run (bufs, nframes, offset, false);
373 uint32_t in = _plugins[0]->get_info()->n_inputs;
374 uint32_t out = _plugins[0]->get_info()->n_outputs;
378 /* not active, but something has make up for any channel count increase */
380 for (uint32_t n = out - in; n < out; ++n) {
381 memcpy (bufs.get_audio(n).data(nframes, offset), bufs.get_audio(in - 1).data(nframes, offset), sizeof (Sample) * nframes);
385 bufs.count().set(DataType::AUDIO, out);
390 PluginInsert::set_parameter (uint32_t port, float val)
392 /* the others will be set from the event triggered by this */
394 _plugins[0]->set_parameter (port, val);
396 if (automation_list (port).automation_write()) {
397 automation_list (port).add (_session.audible_frame(), val);
400 _session.set_dirty();
404 PluginInsert::automation_run (BufferSet& bufs, nframes_t nframes, nframes_t offset)
406 ControlEvent next_event (0, 0.0f);
407 nframes_t now = _session.transport_frame ();
408 nframes_t end = now + nframes;
410 Glib::Mutex::Lock lm (_automation_lock, Glib::TRY_LOCK);
413 connect_and_run (bufs, nframes, offset, false);
417 if (!find_next_event (now, end, next_event)) {
419 /* no events have a time within the relevant range */
421 connect_and_run (bufs, nframes, offset, true, now);
427 nframes_t cnt = min (((nframes_t) ceil (next_event.when) - now), nframes);
429 connect_and_run (bufs, cnt, offset, true, now);
435 if (!find_next_event (now, end, next_event)) {
440 /* cleanup anything that is left to do */
443 connect_and_run (bufs, nframes, offset, true, now);
448 PluginInsert::default_parameter_value (uint32_t port)
450 if (_plugins.empty()) {
451 fatal << _("programming error: ") << X_("PluginInsert::default_parameter_value() called with no plugin")
456 return _plugins[0]->default_value (port);
460 PluginInsert::set_port_automation_state (uint32_t port, AutoState s)
462 if (port < _plugins[0]->parameter_count()) {
464 AutomationList& al = automation_list (port);
466 if (s != al.automation_state()) {
467 al.set_automation_state (s);
468 _session.set_dirty ();
474 PluginInsert::get_port_automation_state (uint32_t port)
476 if (port < _plugins[0]->parameter_count()) {
477 return automation_list (port).automation_state();
484 PluginInsert::protect_automation ()
486 set<uint32_t> automated_params;
488 what_has_automation (automated_params);
490 for (set<uint32_t>::iterator i = automated_params.begin(); i != automated_params.end(); ++i) {
492 AutomationList& al = automation_list (*i);
494 switch (al.automation_state()) {
496 al.set_automation_state (Off);
499 al.set_automation_state (Play);
507 boost::shared_ptr<Plugin>
508 PluginInsert::plugin_factory (boost::shared_ptr<Plugin> other)
510 boost::shared_ptr<LadspaPlugin> lp;
512 boost::shared_ptr<VSTPlugin> vp;
514 #ifdef HAVE_AUDIOUNITS
515 boost::shared_ptr<AUPlugin> ap;
518 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
519 return boost::shared_ptr<Plugin> (new LadspaPlugin (*lp));
521 } else if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (other)) != 0) {
522 return boost::shared_ptr<Plugin> (new VSTPlugin (*vp));
524 #ifdef HAVE_AUDIOUNITS
525 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
526 return boost::shared_ptr<Plugin> (new AUPlugin (*ap));
530 fatal << string_compose (_("programming error: %1"),
531 X_("unknown plugin type in PluginInsert::plugin_factory"))
534 return boost::shared_ptr<Plugin> ((Plugin*) 0);
538 PluginInsert::compute_output_streams (int32_t cnt) const
540 return _plugins[0]->get_info()->n_outputs * cnt;
544 PluginInsert::configure_io (int32_t magic, int32_t in, int32_t out)
546 return set_count (magic);
550 PluginInsert::can_support_input_configuration (int32_t in) const
552 int32_t outputs = _plugins[0]->get_info()->n_outputs;
553 int32_t inputs = _plugins[0]->get_info()->n_inputs;
557 /* instrument plugin, always legal, but it throws
558 away any existing active streams.
564 if (outputs == 1 && inputs == 1) {
565 /* mono plugin, replicate as needed */
574 if ((inputs < in) && (inputs % in == 0)) {
576 /* number of inputs is a factor of the requested input
577 configuration, so we can replicate.
589 PluginInsert::get_state(void)
595 PluginInsert::state (bool full)
598 XMLNode *node = new XMLNode("Insert");
600 node->add_child_nocopy (Redirect::state (full));
602 node->add_property ("type", _plugins[0]->state_node_name());
603 snprintf(buf, sizeof(buf), "%s", _plugins[0]->name());
604 node->add_property("id", string(buf));
605 if (_plugins[0]->state_node_name() == "ladspa") {
607 snprintf (buf, sizeof (buf), "%ld", _plugins[0]->get_info()->unique_id);
608 node->add_property("unique-id", string(buf));
610 node->add_property("count", string_compose("%1", _plugins.size()));
611 node->add_child_nocopy (_plugins[0]->get_state());
613 /* add port automation state */
614 XMLNode *autonode = new XMLNode(port_automation_node_name);
615 set<uint32_t> automatable = _plugins[0]->automatable();
617 for (set<uint32_t>::iterator x = automatable.begin(); x != automatable.end(); ++x) {
619 XMLNode* child = new XMLNode("port");
620 snprintf(buf, sizeof(buf), "%" PRIu32, *x);
621 child->add_property("number", string(buf));
623 child->add_child_nocopy (automation_list (*x).state (full));
624 autonode->add_child_nocopy (*child);
627 node->add_child_nocopy (*autonode);
633 PluginInsert::set_state(const XMLNode& node)
635 XMLNodeList nlist = node.children();
636 XMLNodeIterator niter;
637 XMLPropertyList plist;
638 const XMLProperty *prop;
640 ARDOUR::PluginType type;
642 if ((prop = node.property ("type")) == 0) {
643 error << _("XML node describing insert is missing the `type' field") << endmsg;
647 if (prop->value() == X_("ladspa") || prop->value() == X_("Ladspa")) { /* handle old school sessions */
648 type = ARDOUR::LADSPA;
649 } else if (prop->value() == X_("vst")) {
652 error << string_compose (_("unknown plugin type %1 in plugin insert state"),
658 prop = node.property ("unique-id");
660 unique = atol(prop->value().c_str());
663 if ((prop = node.property ("id")) == 0) {
664 error << _("XML node describing insert is missing the `id' field") << endmsg;
668 boost::shared_ptr<Plugin> plugin;
671 plugin = find_plugin (_session, "", unique, type);
673 plugin = find_plugin (_session, prop->value(), 0, type);
677 error << string_compose(_("Found a reference to a plugin (\"%1\") that is unknown.\n"
678 "Perhaps it was removed or moved since it was last used."), prop->value())
685 if ((prop = node.property ("count")) != 0) {
686 sscanf (prop->value().c_str(), "%u", &count);
689 if (_plugins.size() != count) {
691 _plugins.push_back (plugin);
693 for (uint32_t n=1; n < count; ++n) {
694 _plugins.push_back (plugin_factory (plugin));
698 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
699 if ((*niter)->name() == plugin->state_node_name()) {
700 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
701 (*i)->set_state (**niter);
707 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
708 if ((*niter)->name() == Redirect::state_node_name) {
709 Redirect::set_state (**niter);
714 if (niter == nlist.end()) {
715 error << _("XML node describing insert is missing a Redirect node") << endmsg;
719 if (niter == nlist.end()) {
720 error << string_compose(_("XML node describing a plugin insert is missing the `%1' information"), plugin->state_node_name()) << endmsg;
724 /* look for port automation node */
726 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
728 if ((*niter)->name() != port_automation_node_name) {
734 XMLNodeConstIterator iter;
739 cnodes = (*niter)->children ("port");
741 for(iter = cnodes.begin(); iter != cnodes.end(); ++iter){
745 if ((cprop = child->property("number")) != 0) {
746 port = cprop->value().c_str();
748 warning << _("PluginInsert: Auto: no ladspa port number") << endmsg;
752 sscanf (port, "%" PRIu32, &port_id);
754 if (port_id >= _plugins[0]->parameter_count()) {
755 warning << _("PluginInsert: Auto: port id out of range") << endmsg;
759 if (!child->children().empty()) {
760 automation_list (port_id).set_state (*child->children().front());
762 if ((cprop = child->property("auto")) != 0) {
767 sscanf (cprop->value().c_str(), "0x%x", &x);
768 automation_list (port_id).set_automation_state (AutoState (x));
774 automation_list (port_id).set_automation_state (Off);
785 if (niter == nlist.end()) {
786 warning << string_compose(_("XML node describing a port automation is missing the `%1' information"), port_automation_node_name) << endmsg;
789 // The name of the PluginInsert comes from the plugin, nothing else
790 set_name(plugin->get_info()->name,this);
796 PluginInsert::describe_parameter (uint32_t what)
798 return _plugins[0]->describe_parameter (what);
802 PluginInsert::latency()
804 return _plugins[0]->latency ();
808 PluginInsert::type ()
810 boost::shared_ptr<LadspaPlugin> lp;
812 boost::shared_ptr<VSTPlugin> vp;
814 #ifdef HAVE_AUDIOUNITS
815 boost::shared_ptr<AUPlugin> ap;
818 PluginPtr other = plugin ();
820 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
821 return ARDOUR::LADSPA;
823 } else if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (other)) != 0) {
826 #ifdef HAVE_AUDIOUNITS
827 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
828 return ARDOUR::AudioUnit;
832 return (ARDOUR::PluginType) 0;
836 /***************************************************************
837 Port inserts: send output to a port, pick up input at a port
838 ***************************************************************/
840 PortInsert::PortInsert (Session& s, Placement p)
841 : Insert (s, string_compose (_("insert %1"), (bitslot = s.next_insert_id()) + 1), p, 1, -1, 1, -1)
844 RedirectCreated (this); /* EMIT SIGNAL */
847 PortInsert::PortInsert (const PortInsert& other)
848 : Insert (other._session, string_compose (_("insert %1"), (bitslot = other._session.next_insert_id()) + 1), other.placement(), 1, -1, 1, -1)
851 RedirectCreated (this); /* EMIT SIGNAL */
857 if (add_input_port ("", this)) {
858 error << _("PortInsert: cannot add input port") << endmsg;
859 throw failed_constructor();
862 if (add_output_port ("", this)) {
863 error << _("PortInsert: cannot add output port") << endmsg;
864 throw failed_constructor();
868 PortInsert::PortInsert (Session& s, const XMLNode& node)
869 : Insert (s, "will change", PreFader)
871 if (set_state (node)) {
872 throw failed_constructor();
875 RedirectCreated (this); /* EMIT SIGNAL */
878 PortInsert::~PortInsert ()
884 PortInsert::run (BufferSet& bufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset)
886 if (n_outputs().get(_default_type) == 0) {
891 /* deliver silence */
892 silence (nframes, offset);
896 deliver_output(bufs, start_frame, end_frame, nframes, offset);
898 collect_input(bufs, nframes, offset);
902 PortInsert::get_state(void)
908 PortInsert::state (bool full)
910 XMLNode *node = new XMLNode("Insert");
912 node->add_child_nocopy (Redirect::state(full));
913 node->add_property ("type", "port");
914 snprintf (buf, sizeof (buf), "%" PRIu32, bitslot);
915 node->add_property ("bitslot", buf);
921 PortInsert::set_state(const XMLNode& node)
923 XMLNodeList nlist = node.children();
924 XMLNodeIterator niter;
925 XMLPropertyList plist;
926 const XMLProperty *prop;
928 if ((prop = node.property ("type")) == 0) {
929 error << _("XML node describing insert is missing the `type' field") << endmsg;
933 if (prop->value() != "port") {
934 error << _("non-port insert XML used for port plugin insert") << endmsg;
938 if ((prop = node.property ("bitslot")) == 0) {
939 bitslot = _session.next_insert_id();
941 sscanf (prop->value().c_str(), "%" PRIu32, &bitslot);
942 _session.mark_insert_id (bitslot);
945 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
946 if ((*niter)->name() == Redirect::state_node_name) {
947 Redirect::set_state (**niter);
952 if (niter == nlist.end()) {
953 error << _("XML node describing insert is missing a Redirect node") << endmsg;
961 PortInsert::latency()
963 /* because we deliver and collect within the same cycle,
964 all I/O is necessarily delayed by at least frames_per_cycle().
966 if the return port for insert has its own latency, we
967 need to take that into account too.
970 return _session.engine().frames_per_cycle() + input_latency();
974 PortInsert::can_support_input_configuration (int32_t in) const
976 if (input_maximum() == ChanCount::INFINITE && output_maximum() == ChanCount::INFINITE) {
978 /* not configured yet */
980 return 1; /* we can support anything the first time we're asked */
984 /* the "input" config for a port insert corresponds to how
985 many output ports it will have.
988 if (output_maximum().get(_default_type) == static_cast<uint32_t>(in)) {
997 PortInsert::configure_io (int32_t ignored_magic, int32_t in, int32_t out)
999 /* do not allow configuration to be changed outside the range of
1000 the last request config. or something like that.
1004 /* this is a bit odd:
1006 the number of inputs we are required to handle corresponds
1007 to the number of output ports we need.
1009 the number of outputs we are required to have corresponds
1010 to the number of input ports we need.
1013 set_output_maximum (ChanCount(_default_type, in));
1014 set_output_minimum (ChanCount(_default_type, in));
1015 set_input_maximum (ChanCount(_default_type, out));
1016 set_input_minimum (ChanCount(_default_type, out));
1019 in = n_outputs ().get(_default_type);
1023 out = n_inputs ().get(_default_type);
1026 return ensure_io (ChanCount(_default_type, out), ChanCount(_default_type, in), false, this);
1030 PortInsert::compute_output_streams (int32_t cnt) const
1032 /* puzzling, eh? think about it ... */
1033 return n_inputs ().get(_default_type);
1037 PortInsert::output_streams() const
1043 PortInsert::input_streams() const
1045 return n_outputs ();