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) floor (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()) {
497 al.set_automation_state (Off);
505 boost::shared_ptr<Plugin>
506 PluginInsert::plugin_factory (boost::shared_ptr<Plugin> other)
508 boost::shared_ptr<LadspaPlugin> lp;
510 boost::shared_ptr<VSTPlugin> vp;
512 #ifdef HAVE_AUDIOUNITS
513 boost::shared_ptr<AUPlugin> ap;
516 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
517 return boost::shared_ptr<Plugin> (new LadspaPlugin (*lp));
519 } else if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (other)) != 0) {
520 return boost::shared_ptr<Plugin> (new VSTPlugin (*vp));
522 #ifdef HAVE_AUDIOUNITS
523 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
524 return boost::shared_ptr<Plugin> (new AUPlugin (*ap));
528 fatal << string_compose (_("programming error: %1"),
529 X_("unknown plugin type in PluginInsert::plugin_factory"))
532 return boost::shared_ptr<Plugin> ((Plugin*) 0);
536 PluginInsert::compute_output_streams (int32_t cnt) const
538 return _plugins[0]->get_info()->n_outputs * cnt;
542 PluginInsert::configure_io (int32_t magic, int32_t in, int32_t out)
544 return set_count (magic);
548 PluginInsert::can_support_input_configuration (int32_t in) const
550 int32_t outputs = _plugins[0]->get_info()->n_outputs;
551 int32_t inputs = _plugins[0]->get_info()->n_inputs;
555 /* instrument plugin, always legal, but it throws
556 away any existing active streams.
562 if (outputs == 1 && inputs == 1) {
563 /* mono plugin, replicate as needed */
572 if ((inputs < in) && (inputs % in == 0)) {
574 /* number of inputs is a factor of the requested input
575 configuration, so we can replicate.
587 PluginInsert::get_state(void)
593 PluginInsert::state (bool full)
596 XMLNode *node = new XMLNode("Insert");
598 node->add_child_nocopy (Redirect::state (full));
600 node->add_property ("type", _plugins[0]->state_node_name());
601 snprintf(buf, sizeof(buf), "%s", _plugins[0]->name());
602 node->add_property("id", string(buf));
603 if (_plugins[0]->state_node_name() == "ladspa") {
605 snprintf (buf, sizeof (buf), "%ld", _plugins[0]->get_info()->unique_id);
606 node->add_property("unique-id", string(buf));
608 node->add_property("count", string_compose("%1", _plugins.size()));
609 node->add_child_nocopy (_plugins[0]->get_state());
611 /* add port automation state */
612 XMLNode *autonode = new XMLNode(port_automation_node_name);
613 set<uint32_t> automatable = _plugins[0]->automatable();
615 for (set<uint32_t>::iterator x = automatable.begin(); x != automatable.end(); ++x) {
617 XMLNode* child = new XMLNode("port");
618 snprintf(buf, sizeof(buf), "%" PRIu32, *x);
619 child->add_property("number", string(buf));
621 child->add_child_nocopy (automation_list (*x).state (full));
622 autonode->add_child_nocopy (*child);
625 node->add_child_nocopy (*autonode);
631 PluginInsert::set_state(const XMLNode& node)
633 XMLNodeList nlist = node.children();
634 XMLNodeIterator niter;
635 XMLPropertyList plist;
636 const XMLProperty *prop;
638 ARDOUR::PluginType type;
640 if ((prop = node.property ("type")) == 0) {
641 error << _("XML node describing insert is missing the `type' field") << endmsg;
645 if (prop->value() == X_("ladspa") || prop->value() == X_("Ladspa")) { /* handle old school sessions */
646 type = ARDOUR::LADSPA;
647 } else if (prop->value() == X_("vst")) {
650 error << string_compose (_("unknown plugin type %1 in plugin insert state"),
656 prop = node.property ("unique-id");
658 unique = atol(prop->value().c_str());
661 if ((prop = node.property ("id")) == 0) {
662 error << _("XML node describing insert is missing the `id' field") << endmsg;
666 boost::shared_ptr<Plugin> plugin;
669 plugin = find_plugin (_session, "", unique, type);
671 plugin = find_plugin (_session, prop->value(), 0, type);
675 error << string_compose(_("Found a reference to a plugin (\"%1\") that is unknown.\n"
676 "Perhaps it was removed or moved since it was last used."), prop->value())
683 if ((prop = node.property ("count")) != 0) {
684 sscanf (prop->value().c_str(), "%u", &count);
687 if (_plugins.size() != count) {
689 _plugins.push_back (plugin);
691 for (uint32_t n=1; n < count; ++n) {
692 _plugins.push_back (plugin_factory (plugin));
696 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
697 if ((*niter)->name() == plugin->state_node_name()) {
698 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
699 (*i)->set_state (**niter);
705 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
706 if ((*niter)->name() == Redirect::state_node_name) {
707 Redirect::set_state (**niter);
712 if (niter == nlist.end()) {
713 error << _("XML node describing insert is missing a Redirect node") << endmsg;
717 if (niter == nlist.end()) {
718 error << string_compose(_("XML node describing a plugin insert is missing the `%1' information"), plugin->state_node_name()) << endmsg;
722 /* look for port automation node */
724 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
726 if ((*niter)->name() != port_automation_node_name) {
732 XMLNodeConstIterator iter;
737 cnodes = (*niter)->children ("port");
739 for(iter = cnodes.begin(); iter != cnodes.end(); ++iter){
743 if ((cprop = child->property("number")) != 0) {
744 port = cprop->value().c_str();
746 warning << _("PluginInsert: Auto: no ladspa port number") << endmsg;
750 sscanf (port, "%" PRIu32, &port_id);
752 if (port_id >= _plugins[0]->parameter_count()) {
753 warning << _("PluginInsert: Auto: port id out of range") << endmsg;
757 if (!child->children().empty()) {
758 automation_list (port_id).set_state (*child->children().front());
760 if ((cprop = child->property("auto")) != 0) {
765 sscanf (cprop->value().c_str(), "0x%x", &x);
766 automation_list (port_id).set_automation_state (AutoState (x));
772 automation_list (port_id).set_automation_state (Off);
783 if (niter == nlist.end()) {
784 warning << string_compose(_("XML node describing a port automation is missing the `%1' information"), port_automation_node_name) << endmsg;
787 // The name of the PluginInsert comes from the plugin, nothing else
788 set_name(plugin->get_info()->name,this);
794 PluginInsert::describe_parameter (uint32_t what)
796 return _plugins[0]->describe_parameter (what);
800 PluginInsert::latency()
802 return _plugins[0]->latency ();
806 PluginInsert::type ()
808 boost::shared_ptr<LadspaPlugin> lp;
810 boost::shared_ptr<VSTPlugin> vp;
812 #ifdef HAVE_AUDIOUNITS
813 boost::shared_ptr<AUPlugin> ap;
816 PluginPtr other = plugin ();
818 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
819 return ARDOUR::LADSPA;
821 } else if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (other)) != 0) {
824 #ifdef HAVE_AUDIOUNITS
825 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
826 return ARDOUR::AudioUnit;
830 return (ARDOUR::PluginType) 0;
834 /***************************************************************
835 Port inserts: send output to a port, pick up input at a port
836 ***************************************************************/
838 PortInsert::PortInsert (Session& s, Placement p)
839 : Insert (s, string_compose (_("insert %1"), (bitslot = s.next_insert_id()) + 1), p, 1, -1, 1, -1)
842 RedirectCreated (this); /* EMIT SIGNAL */
845 PortInsert::PortInsert (const PortInsert& other)
846 : Insert (other._session, string_compose (_("insert %1"), (bitslot = other._session.next_insert_id()) + 1), other.placement(), 1, -1, 1, -1)
849 RedirectCreated (this); /* EMIT SIGNAL */
855 if (add_input_port ("", this)) {
856 error << _("PortInsert: cannot add input port") << endmsg;
857 throw failed_constructor();
860 if (add_output_port ("", this)) {
861 error << _("PortInsert: cannot add output port") << endmsg;
862 throw failed_constructor();
866 PortInsert::PortInsert (Session& s, const XMLNode& node)
867 : Insert (s, "will change", PreFader)
869 if (set_state (node)) {
870 throw failed_constructor();
873 RedirectCreated (this); /* EMIT SIGNAL */
876 PortInsert::~PortInsert ()
882 PortInsert::run (BufferSet& bufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset)
884 if (n_outputs().get(_default_type) == 0) {
889 /* deliver silence */
890 silence (nframes, offset);
894 deliver_output(bufs, start_frame, end_frame, nframes, offset);
896 collect_input(bufs, nframes, offset);
900 PortInsert::get_state(void)
906 PortInsert::state (bool full)
908 XMLNode *node = new XMLNode("Insert");
910 node->add_child_nocopy (Redirect::state(full));
911 node->add_property ("type", "port");
912 snprintf (buf, sizeof (buf), "%" PRIu32, bitslot);
913 node->add_property ("bitslot", buf);
919 PortInsert::set_state(const XMLNode& node)
921 XMLNodeList nlist = node.children();
922 XMLNodeIterator niter;
923 XMLPropertyList plist;
924 const XMLProperty *prop;
926 if ((prop = node.property ("type")) == 0) {
927 error << _("XML node describing insert is missing the `type' field") << endmsg;
931 if (prop->value() != "port") {
932 error << _("non-port insert XML used for port plugin insert") << endmsg;
936 if ((prop = node.property ("bitslot")) == 0) {
937 bitslot = _session.next_insert_id();
939 sscanf (prop->value().c_str(), "%" PRIu32, &bitslot);
940 _session.mark_insert_id (bitslot);
943 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
944 if ((*niter)->name() == Redirect::state_node_name) {
945 Redirect::set_state (**niter);
950 if (niter == nlist.end()) {
951 error << _("XML node describing insert is missing a Redirect node") << endmsg;
959 PortInsert::latency()
961 /* because we deliver and collect within the same cycle,
962 all I/O is necessarily delayed by at least frames_per_cycle().
964 if the return port for insert has its own latency, we
965 need to take that into account too.
968 return _session.engine().frames_per_cycle() + input_latency();
972 PortInsert::can_support_input_configuration (int32_t in) const
974 if (input_maximum() == ChanCount::INFINITE && output_maximum() == ChanCount::INFINITE) {
976 /* not configured yet */
978 return 1; /* we can support anything the first time we're asked */
982 /* the "input" config for a port insert corresponds to how
983 many output ports it will have.
986 if (output_maximum().get(_default_type) == static_cast<uint32_t>(in)) {
995 PortInsert::configure_io (int32_t ignored_magic, int32_t in, int32_t out)
997 /* do not allow configuration to be changed outside the range of
998 the last request config. or something like that.
1002 /* this is a bit odd:
1004 the number of inputs we are required to handle corresponds
1005 to the number of output ports we need.
1007 the number of outputs we are required to have corresponds
1008 to the number of input ports we need.
1011 set_output_maximum (ChanCount(_default_type, in));
1012 set_output_minimum (ChanCount(_default_type, in));
1013 set_input_maximum (ChanCount(_default_type, out));
1014 set_input_minimum (ChanCount(_default_type, out));
1017 in = n_outputs ().get(_default_type);
1021 out = n_inputs ().get(_default_type);
1024 return ensure_io (ChanCount(_default_type, out), ChanCount(_default_type, in), false, this);
1028 PortInsert::compute_output_streams (int32_t cnt) const
1030 /* puzzling, eh? think about it ... */
1031 return n_inputs ().get(_default_type);
1035 PortInsert::output_streams() const
1041 PortInsert::input_streams() const
1043 return n_outputs ();