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/plugin_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>
33 #include <ardour/automation_event.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 const string PluginInsert::port_automation_node_name = "PortAutomation";
55 PluginInsert::PluginInsert (Session& s, boost::shared_ptr<Plugin> plug, Placement placement)
56 : Insert (s, plug->name(), placement)
58 /* the first is the master */
60 _plugins.push_back (plug);
62 _plugins[0]->ParameterChanged.connect (mem_fun (*this, &PluginInsert::parameter_changed));
67 Glib::Mutex::Lock em (_session.engine().process_lock());
68 IO::MoreChannels (max(input_streams(), output_streams()));
71 InsertCreated (this); /* EMIT SIGNAL */
74 PluginInsert::PluginInsert (Session& s, const XMLNode& node)
75 : Insert (s, "unnamed plugin insert", PreFader)
77 if (set_state (node)) {
78 throw failed_constructor();
83 _plugins[0]->ParameterChanged.connect (mem_fun (*this, &PluginInsert::parameter_changed));
86 Glib::Mutex::Lock em (_session.engine().process_lock());
87 IO::MoreChannels (max(input_streams(), output_streams()));
91 PluginInsert::PluginInsert (const PluginInsert& other)
92 : Insert (other._session, other._name, other.placement())
94 uint32_t count = other._plugins.size();
96 /* make as many copies as requested */
97 for (uint32_t n = 0; n < count; ++n) {
98 _plugins.push_back (plugin_factory (other.plugin (n)));
102 _plugins[0]->ParameterChanged.connect (mem_fun (*this, &PluginInsert::parameter_changed));
106 InsertCreated (this); /* EMIT SIGNAL */
110 PluginInsert::set_count (uint32_t num)
112 bool require_state = !_plugins.empty();
114 /* this is a bad idea.... we shouldn't do this while active.
115 only a route holding their redirect_lock should be calling this
120 } else if (num > _plugins.size()) {
121 uint32_t diff = num - _plugins.size();
123 for (uint32_t n = 0; n < diff; ++n) {
124 _plugins.push_back (plugin_factory (_plugins[0]));
127 /* XXX do something */
131 } else if (num < _plugins.size()) {
132 uint32_t diff = _plugins.size() - num;
133 for (uint32_t n= 0; n < diff; ++n) {
142 PluginInsert::init ()
146 set<uint32_t>::iterator s;
149 PluginInsert::~PluginInsert ()
151 GoingAway (); /* EMIT SIGNAL */
155 PluginInsert::automation_list_creation_callback (uint32_t which, AutomationList& alist)
157 alist.automation_state_changed.connect (sigc::bind (mem_fun (*this, &PluginInsert::auto_state_changed), (which)));
161 PluginInsert::auto_state_changed (uint32_t which)
163 AutomationList& alist (automation_list (which));
165 if (alist.automation_state() != Off) {
166 _plugins[0]->set_parameter (which, alist.eval (_session.transport_frame()));
171 PluginInsert::output_streams() const
174 return output_for_input_configuration(_configured_input);
176 return natural_output_streams();
180 PluginInsert::input_streams() const
183 return _configured_input;
185 return natural_input_streams();
189 PluginInsert::natural_output_streams() const
191 return _plugins[0]->get_info()->n_outputs;
195 PluginInsert::natural_input_streams() const
197 return _plugins[0]->get_info()->n_inputs;
201 PluginInsert::is_generator() const
203 /* XXX more finesse is possible here. VST plugins have a
204 a specific "instrument" flag, for example.
207 return _plugins[0]->get_info()->n_inputs.n_audio() == 0;
211 PluginInsert::set_automatable ()
215 a = _plugins.front()->automatable ();
217 for (set<uint32_t>::iterator i = a.begin(); i != a.end(); ++i) {
223 PluginInsert::parameter_changed (uint32_t which, float val)
225 vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin();
227 /* don't set the first plugin, just all the slaves */
229 if (i != _plugins.end()) {
231 for (; i != _plugins.end(); ++i) {
232 (*i)->set_parameter (which, val);
238 PluginInsert::set_block_size (nframes_t nframes)
240 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
241 (*i)->set_block_size (nframes);
246 PluginInsert::activate ()
248 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
254 PluginInsert::deactivate ()
256 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
262 PluginInsert::connect_and_run (BufferSet& bufs, nframes_t nframes, nframes_t offset, bool with_auto, nframes_t now)
264 uint32_t in_index = 0;
265 uint32_t out_index = 0;
267 /* Note that we've already required that plugins
268 be able to handle in-place processing.
273 map<uint32_t,AutomationList*>::iterator li;
276 for (n = 0, li = _parameter_automation.begin(); li != _parameter_automation.end(); ++li, ++n) {
278 AutomationList& alist (*((*li).second));
280 if (alist.automation_playback()) {
283 float val = alist.rt_safe_eval (now, valid);
286 /* set the first plugin, the others will be set via signals */
287 _plugins[0]->set_parameter ((*li).first, val);
294 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
295 (*i)->connect_and_run (bufs, in_index, out_index, nframes, offset);
298 /* leave remaining channel buffers alone */
302 PluginInsert::automation_snapshot (nframes_t now)
304 map<uint32_t,AutomationList*>::iterator li;
306 for (li =_parameter_automation.begin(); li !=_parameter_automation.end(); ++li) {
308 AutomationList *alist = ((*li).second);
309 if (alist != 0 && alist->automation_write ()) {
311 float val = _plugins[0]->get_parameter ((*li).first);
312 alist->rt_add (now, val);
313 _last_automation_snapshot = now;
319 PluginInsert::transport_stopped (nframes_t now)
321 map<uint32_t,AutomationList*>::iterator li;
323 for (li =_parameter_automation.begin(); li !=_parameter_automation.end(); ++li) {
324 AutomationList& alist (*(li->second));
325 alist.reposition_for_rt_add (now);
327 if (alist.automation_state() != Off) {
328 _plugins[0]->set_parameter (li->first, alist.eval (now));
334 PluginInsert::silence (nframes_t nframes, nframes_t offset)
336 uint32_t in_index = 0;
337 uint32_t out_index = 0;
340 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
341 (*i)->connect_and_run (_session.get_silent_buffers ((*i)->get_info()->n_inputs), in_index, out_index, nframes, offset);
347 PluginInsert::run (BufferSet& bufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset)
351 if (_session.transport_rolling()) {
352 automation_run (bufs, nframes, offset);
354 connect_and_run (bufs, nframes, offset, false);
358 /* FIXME: type, audio only */
360 uint32_t in = _plugins[0]->get_info()->n_inputs.n_audio();
361 uint32_t out = _plugins[0]->get_info()->n_outputs.n_audio();
365 /* not active, but something has make up for any channel count increase */
367 for (uint32_t n = out - in; n < out; ++n) {
368 memcpy (bufs.get_audio(n).data(nframes, offset), bufs.get_audio(in - 1).data(nframes, offset), sizeof (Sample) * nframes);
372 bufs.count().set_audio(out);
377 PluginInsert::set_parameter (uint32_t port, float val)
379 /* the others will be set from the event triggered by this */
381 _plugins[0]->set_parameter (port, val);
383 if (automation_list (port).automation_write()) {
384 automation_list (port).add (_session.audible_frame(), val);
387 _session.set_dirty();
391 PluginInsert::automation_run (BufferSet& bufs, nframes_t nframes, nframes_t offset)
393 ControlEvent next_event (0, 0.0f);
394 nframes_t now = _session.transport_frame ();
395 nframes_t end = now + nframes;
397 Glib::Mutex::Lock lm (_automation_lock, Glib::TRY_LOCK);
400 connect_and_run (bufs, nframes, offset, false);
404 if (!find_next_event (now, end, next_event)) {
406 /* no events have a time within the relevant range */
408 connect_and_run (bufs, nframes, offset, true, now);
414 nframes_t cnt = min (((nframes_t) ceil (next_event.when) - now), nframes);
416 connect_and_run (bufs, cnt, offset, true, now);
422 if (!find_next_event (now, end, next_event)) {
427 /* cleanup anything that is left to do */
430 connect_and_run (bufs, nframes, offset, true, now);
435 PluginInsert::default_parameter_value (uint32_t port)
437 if (_plugins.empty()) {
438 fatal << _("programming error: ") << X_("PluginInsert::default_parameter_value() called with no plugin")
443 return _plugins[0]->default_value (port);
447 PluginInsert::set_port_automation_state (uint32_t port, AutoState s)
449 if (port < _plugins[0]->parameter_count()) {
451 AutomationList& al = automation_list (port);
453 if (s != al.automation_state()) {
454 al.set_automation_state (s);
455 _session.set_dirty ();
461 PluginInsert::get_port_automation_state (uint32_t port)
463 if (port < _plugins[0]->parameter_count()) {
464 return automation_list (port).automation_state();
471 PluginInsert::protect_automation ()
473 set<uint32_t> automated_params;
475 what_has_automation (automated_params);
477 for (set<uint32_t>::iterator i = automated_params.begin(); i != automated_params.end(); ++i) {
479 AutomationList& al = automation_list (*i);
481 switch (al.automation_state()) {
483 al.set_automation_state (Off);
486 al.set_automation_state (Play);
494 boost::shared_ptr<Plugin>
495 PluginInsert::plugin_factory (boost::shared_ptr<Plugin> other)
497 boost::shared_ptr<LadspaPlugin> lp;
499 boost::shared_ptr<VSTPlugin> vp;
501 #ifdef HAVE_AUDIOUNITS
502 boost::shared_ptr<AUPlugin> ap;
505 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
506 return boost::shared_ptr<Plugin> (new LadspaPlugin (*lp));
508 } else if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (other)) != 0) {
509 return boost::shared_ptr<Plugin> (new VSTPlugin (*vp));
511 #ifdef HAVE_AUDIOUNITS
512 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
513 return boost::shared_ptr<Plugin> (new AUPlugin (*ap));
517 fatal << string_compose (_("programming error: %1"),
518 X_("unknown plugin type in PluginInsert::plugin_factory"))
521 return boost::shared_ptr<Plugin> ((Plugin*) 0);
525 PluginInsert::configure_io (ChanCount in, ChanCount out)
527 ChanCount matching_out = output_for_input_configuration(out);
528 if (matching_out != out) {
532 bool success = set_count (count_for_configuration(in, out));
534 Insert::configure_io(in, out);
540 PluginInsert::can_support_input_configuration (ChanCount in) const
542 ChanCount outputs = _plugins[0]->get_info()->n_outputs;
543 ChanCount inputs = _plugins[0]->get_info()->n_inputs;
545 /* see output_for_input_configuration below */
546 if ((inputs.n_total() == 0)
547 || (inputs.n_total() == 1 && outputs == inputs)
548 || (inputs.n_total() == 1 && outputs == inputs
549 && ((inputs.n_audio() == 0 && in.n_audio() == 0)
550 || (inputs.n_midi() == 0 && in.n_midi() == 0)))
555 bool can_replicate = true;
557 /* if number of inputs is a factor of the requested input
558 configuration for every type, we can replicate.
560 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
561 if (inputs.get(*t) >= in.get(*t) || (inputs.get(*t) % in.get(*t) != 0)) {
562 can_replicate = false;
567 if (can_replicate && (in.n_total() % inputs.n_total() == 0)) {
575 PluginInsert::output_for_input_configuration (ChanCount in) const
577 ChanCount outputs = _plugins[0]->get_info()->n_outputs;
578 ChanCount inputs = _plugins[0]->get_info()->n_inputs;
580 if (inputs.n_total() == 0) {
581 /* instrument plugin, always legal, but throws away any existing streams */
585 if (inputs.n_total() == 1 && outputs == inputs
586 && ((inputs.n_audio() == 0 && in.n_audio() == 0)
587 || (inputs.n_midi() == 0 && in.n_midi() == 0))) {
588 /* mono plugin, replicate as needed to match in */
597 bool can_replicate = true;
599 /* if number of inputs is a factor of the requested input
600 configuration for every type, we can replicate.
602 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
603 if (inputs.get(*t) >= in.get(*t) || (in.get(*t) % inputs.get(*t) != 0)) {
604 can_replicate = false;
609 if (can_replicate && (inputs.n_total() % in.n_total() == 0)) {
612 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
613 output.set(*t, outputs.get(*t) * (in.get(*t) / inputs.get(*t)));
623 /* Number of plugin instances required to support a given channel configuration.
627 PluginInsert::count_for_configuration (ChanCount in, ChanCount out) const
629 // FIXME: take 'out' into consideration
631 ChanCount outputs = _plugins[0]->get_info()->n_outputs;
632 ChanCount inputs = _plugins[0]->get_info()->n_inputs;
634 if (inputs.n_total() == 0) {
635 /* instrument plugin, always legal, but throws away any existing streams */
639 if (inputs.n_total() == 1 && outputs == inputs
640 && ((inputs.n_audio() == 0 && in.n_audio() == 0)
641 || (inputs.n_midi() == 0 && in.n_midi() == 0))) {
642 /* mono plugin, replicate as needed to match in */
651 // assumes in is valid, so we must be replicating
652 if (inputs.n_total() < in.n_total()
653 && (in.n_total() % inputs.n_total() == 0)) {
655 return in.n_total() / inputs.n_total();
663 PluginInsert::get_state(void)
669 PluginInsert::state (bool full)
672 XMLNode& node = Insert::state (full);
674 node.add_property ("type", _plugins[0]->state_node_name());
675 snprintf(buf, sizeof(buf), "%s", _plugins[0]->name());
676 node.add_property("id", string(buf));
677 if (_plugins[0]->state_node_name() == "ladspa") {
679 snprintf (buf, sizeof (buf), "%ld", _plugins[0]->get_info()->unique_id);
680 node.add_property("unique-id", string(buf));
682 node.add_property("count", string_compose("%1", _plugins.size()));
683 node.add_child_nocopy (_plugins[0]->get_state());
685 /* add port automation state */
686 XMLNode *autonode = new XMLNode(port_automation_node_name);
687 set<uint32_t> automatable = _plugins[0]->automatable();
689 for (set<uint32_t>::iterator x = automatable.begin(); x != automatable.end(); ++x) {
691 XMLNode* child = new XMLNode("port");
692 snprintf(buf, sizeof(buf), "%" PRIu32, *x);
693 child->add_property("number", string(buf));
695 child->add_child_nocopy (automation_list (*x).state (full));
696 autonode->add_child_nocopy (*child);
699 node.add_child_nocopy (*autonode);
705 PluginInsert::set_state(const XMLNode& node)
707 XMLNodeList nlist = node.children();
708 XMLNodeIterator niter;
709 XMLPropertyList plist;
710 const XMLProperty *prop;
712 ARDOUR::PluginType type;
714 if ((prop = node.property ("type")) == 0) {
715 error << _("XML node describing insert is missing the `type' field") << endmsg;
719 if (prop->value() == X_("ladspa") || prop->value() == X_("Ladspa")) { /* handle old school sessions */
720 type = ARDOUR::LADSPA;
721 } else if (prop->value() == X_("vst")) {
724 error << string_compose (_("unknown plugin type %1 in plugin insert state"),
730 prop = node.property ("unique-id");
732 unique = atol(prop->value().c_str());
735 if ((prop = node.property ("id")) == 0) {
736 error << _("XML node describing insert is missing the `id' field") << endmsg;
740 boost::shared_ptr<Plugin> plugin;
743 plugin = find_plugin (_session, "", unique, type);
745 plugin = find_plugin (_session, prop->value(), 0, type);
749 error << string_compose(_("Found a reference to a plugin (\"%1\") that is unknown.\n"
750 "Perhaps it was removed or moved since it was last used."), prop->value())
757 if ((prop = node.property ("count")) != 0) {
758 sscanf (prop->value().c_str(), "%u", &count);
761 if (_plugins.size() != count) {
763 _plugins.push_back (plugin);
765 for (uint32_t n=1; n < count; ++n) {
766 _plugins.push_back (plugin_factory (plugin));
770 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
771 if ((*niter)->name() == plugin->state_node_name()) {
772 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
773 (*i)->set_state (**niter);
779 const XMLNode* insert_node = &node;
781 // legacy sessions: search for child Redirect node
782 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
783 if ((*niter)->name() == "Redirect") {
784 insert_node = *niter;
789 Insert::set_state (*insert_node);
791 /* look for port automation node */
793 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
795 if ((*niter)->name() != port_automation_node_name) {
801 XMLNodeConstIterator iter;
806 cnodes = (*niter)->children ("port");
808 for(iter = cnodes.begin(); iter != cnodes.end(); ++iter){
812 if ((cprop = child->property("number")) != 0) {
813 port = cprop->value().c_str();
815 warning << _("PluginInsert: Auto: no ladspa port number") << endmsg;
819 sscanf (port, "%" PRIu32, &port_id);
821 if (port_id >= _plugins[0]->parameter_count()) {
822 warning << _("PluginInsert: Auto: port id out of range") << endmsg;
826 if (!child->children().empty()) {
827 automation_list (port_id).set_state (*child->children().front());
829 if ((cprop = child->property("auto")) != 0) {
834 sscanf (cprop->value().c_str(), "0x%x", &x);
835 automation_list (port_id).set_automation_state (AutoState (x));
841 automation_list (port_id).set_automation_state (Off);
852 if (niter == nlist.end()) {
853 warning << string_compose(_("XML node describing a port automation is missing the `%1' information"), port_automation_node_name) << endmsg;
856 // The name of the PluginInsert comes from the plugin, nothing else
857 _name = plugin->get_info()->name;
863 PluginInsert::describe_parameter (uint32_t what)
865 return _plugins[0]->describe_parameter (what);
869 PluginInsert::latency()
871 return _plugins[0]->latency ();
875 PluginInsert::type ()
877 boost::shared_ptr<LadspaPlugin> lp;
879 boost::shared_ptr<VSTPlugin> vp;
881 #ifdef HAVE_AUDIOUNITS
882 boost::shared_ptr<AUPlugin> ap;
885 PluginPtr other = plugin ();
887 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
888 return ARDOUR::LADSPA;
890 } else if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (other)) != 0) {
893 #ifdef HAVE_AUDIOUNITS
894 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
895 return ARDOUR::AudioUnit;
899 return (ARDOUR::PluginType) 0;