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 : Processor (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 ProcessorCreated (this); /* EMIT SIGNAL */
74 PluginInsert::PluginInsert (Session& s, const XMLNode& node)
75 : Processor (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 : Processor (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 ProcessorCreated (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 (ParamID which, AutomationList& alist)
157 alist.automation_state_changed.connect (sigc::bind (mem_fun (*this, &PluginInsert::auto_state_changed), (which)));
161 PluginInsert::auto_state_changed (ParamID which)
163 if (which.type() != PluginAutomation)
166 AutomationList* alist = automation_list (which);
168 if (alist && alist->automation_state() != Off) {
169 _plugins[0]->set_parameter (which.id(), alist->eval (_session.transport_frame()));
174 PluginInsert::output_streams() const
177 return output_for_input_configuration(_configured_input);
179 return natural_output_streams();
183 PluginInsert::input_streams() const
186 return _configured_input;
188 return natural_input_streams();
192 PluginInsert::natural_output_streams() const
194 return _plugins[0]->get_info()->n_outputs;
198 PluginInsert::natural_input_streams() const
200 return _plugins[0]->get_info()->n_inputs;
204 PluginInsert::is_generator() const
206 /* XXX more finesse is possible here. VST plugins have a
207 a specific "instrument" flag, for example.
210 return _plugins[0]->get_info()->n_inputs.n_audio() == 0;
214 PluginInsert::set_automatable ()
218 a = _plugins.front()->automatable ();
220 for (set<ParamID>::iterator i = a.begin(); i != a.end(); ++i) {
226 PluginInsert::parameter_changed (ParamID which, float val)
228 if (which.type() != PluginAutomation)
231 vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin();
233 /* don't set the first plugin, just all the slaves */
235 if (i != _plugins.end()) {
237 for (; i != _plugins.end(); ++i) {
238 (*i)->set_parameter (which, val);
244 PluginInsert::set_block_size (nframes_t nframes)
246 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
247 (*i)->set_block_size (nframes);
252 PluginInsert::activate ()
254 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
260 PluginInsert::deactivate ()
262 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
268 PluginInsert::connect_and_run (BufferSet& bufs, nframes_t nframes, nframes_t offset, bool with_auto, nframes_t now)
270 uint32_t in_index = 0;
271 uint32_t out_index = 0;
273 /* Note that we've already required that plugins
274 be able to handle in-place processing.
279 map<ParamID,AutomationList*>::iterator li;
282 for (n = 0, li = _parameter_automation.begin(); li != _parameter_automation.end(); ++li, ++n) {
284 AutomationList& alist (*((*li).second));
286 if (alist.param_id().type() == PluginAutomation && alist.automation_playback()) {
289 float val = alist.rt_safe_eval (now, valid);
292 /* set the first plugin, the others will be set via signals */
293 _plugins[0]->set_parameter ((*li).first, val);
300 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
301 (*i)->connect_and_run (bufs, in_index, out_index, nframes, offset);
304 /* leave remaining channel buffers alone */
308 PluginInsert::automation_snapshot (nframes_t now)
310 map<ParamID,AutomationList*>::iterator li;
312 for (li =_parameter_automation.begin(); li !=_parameter_automation.end(); ++li) {
314 AutomationList *alist = ((*li).second);
315 if (alist != 0 && alist->param_id().type() == PluginAutomation
316 && alist->automation_write ()) {
318 float val = _plugins[0]->get_parameter ((*li).first);
319 alist->rt_add (now, val);
320 _last_automation_snapshot = now;
326 PluginInsert::transport_stopped (nframes_t now)
328 map<ParamID,AutomationList*>::iterator li;
330 for (li =_parameter_automation.begin(); li !=_parameter_automation.end(); ++li) {
331 AutomationList& alist (*(li->second));
332 alist.reposition_for_rt_add (now);
334 if (alist.param_id().type() == PluginAutomation && alist.automation_state() != Off) {
335 _plugins[0]->set_parameter (li->first, alist.eval (now));
341 PluginInsert::silence (nframes_t nframes, nframes_t offset)
343 uint32_t in_index = 0;
344 uint32_t out_index = 0;
347 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
348 (*i)->connect_and_run (_session.get_silent_buffers ((*i)->get_info()->n_inputs), in_index, out_index, nframes, offset);
354 PluginInsert::run (BufferSet& bufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset)
358 if (_session.transport_rolling()) {
359 automation_run (bufs, nframes, offset);
361 connect_and_run (bufs, nframes, offset, false);
365 /* FIXME: type, audio only */
367 uint32_t in = _plugins[0]->get_info()->n_inputs.n_audio();
368 uint32_t out = _plugins[0]->get_info()->n_outputs.n_audio();
372 /* not active, but something has make up for any channel count increase */
374 for (uint32_t n = out - in; n < out; ++n) {
375 memcpy (bufs.get_audio(n).data(nframes, offset), bufs.get_audio(in - 1).data(nframes, offset), sizeof (Sample) * nframes);
379 bufs.count().set_audio(out);
384 PluginInsert::set_parameter (ParamID param, float val)
386 if (param.type() != PluginAutomation)
389 /* the others will be set from the event triggered by this */
391 _plugins[0]->set_parameter (param.id(), val);
393 if (automation_list (param) && automation_list (param)->automation_write()) {
394 automation_list (param)->add (_session.audible_frame(), val);
397 _session.set_dirty();
401 PluginInsert::automation_run (BufferSet& bufs, nframes_t nframes, nframes_t offset)
403 ControlEvent next_event (0, 0.0f);
404 nframes_t now = _session.transport_frame ();
405 nframes_t end = now + nframes;
407 Glib::Mutex::Lock lm (_automation_lock, Glib::TRY_LOCK);
410 connect_and_run (bufs, nframes, offset, false);
414 if (!find_next_event (now, end, next_event)) {
416 /* no events have a time within the relevant range */
418 connect_and_run (bufs, nframes, offset, true, now);
424 nframes_t cnt = min (((nframes_t) ceil (next_event.when) - now), nframes);
426 connect_and_run (bufs, cnt, offset, true, now);
432 if (!find_next_event (now, end, next_event)) {
437 /* cleanup anything that is left to do */
440 connect_and_run (bufs, nframes, offset, true, now);
445 PluginInsert::default_parameter_value (ParamID param)
447 if (param.type() != PluginAutomation)
450 if (_plugins.empty()) {
451 fatal << _("programming error: ") << X_("PluginInsert::default_parameter_value() called with no plugin")
456 return _plugins[0]->default_value (param.id());
459 boost::shared_ptr<Plugin>
460 PluginInsert::plugin_factory (boost::shared_ptr<Plugin> other)
462 boost::shared_ptr<LadspaPlugin> lp;
464 boost::shared_ptr<VSTPlugin> vp;
466 #ifdef HAVE_AUDIOUNITS
467 boost::shared_ptr<AUPlugin> ap;
470 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
471 return boost::shared_ptr<Plugin> (new LadspaPlugin (*lp));
473 } else if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (other)) != 0) {
474 return boost::shared_ptr<Plugin> (new VSTPlugin (*vp));
476 #ifdef HAVE_AUDIOUNITS
477 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
478 return boost::shared_ptr<Plugin> (new AUPlugin (*ap));
482 fatal << string_compose (_("programming error: %1"),
483 X_("unknown plugin type in PluginInsert::plugin_factory"))
486 return boost::shared_ptr<Plugin> ((Plugin*) 0);
490 PluginInsert::configure_io (ChanCount in, ChanCount out)
492 ChanCount matching_out = output_for_input_configuration(out);
493 if (matching_out != out) {
497 bool success = set_count (count_for_configuration(in, out));
499 Processor::configure_io(in, out);
505 PluginInsert::can_support_input_configuration (ChanCount in) const
507 ChanCount outputs = _plugins[0]->get_info()->n_outputs;
508 ChanCount inputs = _plugins[0]->get_info()->n_inputs;
510 /* see output_for_input_configuration below */
511 if ((inputs.n_total() == 0)
512 || (inputs.n_total() == 1 && outputs == inputs)
513 || (inputs.n_total() == 1 && outputs == inputs
514 && ((inputs.n_audio() == 0 && in.n_audio() == 0)
515 || (inputs.n_midi() == 0 && in.n_midi() == 0)))
520 bool can_replicate = true;
522 /* if number of inputs is a factor of the requested input
523 configuration for every type, we can replicate.
525 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
526 if (inputs.get(*t) >= in.get(*t) || (inputs.get(*t) % in.get(*t) != 0)) {
527 can_replicate = false;
532 if (can_replicate && (in.n_total() % inputs.n_total() == 0)) {
540 PluginInsert::output_for_input_configuration (ChanCount in) const
542 ChanCount outputs = _plugins[0]->get_info()->n_outputs;
543 ChanCount inputs = _plugins[0]->get_info()->n_inputs;
545 if (inputs.n_total() == 0) {
546 /* instrument plugin, always legal, but throws away any existing streams */
550 if (inputs.n_total() == 1 && outputs == inputs
551 && ((inputs.n_audio() == 0 && in.n_audio() == 0)
552 || (inputs.n_midi() == 0 && in.n_midi() == 0))) {
553 /* mono plugin, replicate as needed to match in */
562 bool can_replicate = true;
564 /* if number of inputs is a factor of the requested input
565 configuration for every type, we can replicate.
567 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
568 if (inputs.get(*t) >= in.get(*t) || (in.get(*t) % inputs.get(*t) != 0)) {
569 can_replicate = false;
574 if (can_replicate && (inputs.n_total() % in.n_total() == 0)) {
577 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
578 output.set(*t, outputs.get(*t) * (in.get(*t) / inputs.get(*t)));
588 /* Number of plugin instances required to support a given channel configuration.
592 PluginInsert::count_for_configuration (ChanCount in, ChanCount out) const
594 // FIXME: take 'out' into consideration
596 ChanCount outputs = _plugins[0]->get_info()->n_outputs;
597 ChanCount inputs = _plugins[0]->get_info()->n_inputs;
599 if (inputs.n_total() == 0) {
600 /* instrument plugin, always legal, but throws away any existing streams */
604 if (inputs.n_total() == 1 && outputs == inputs
605 && ((inputs.n_audio() == 0 && in.n_audio() == 0)
606 || (inputs.n_midi() == 0 && in.n_midi() == 0))) {
607 /* mono plugin, replicate as needed to match in */
616 // assumes in is valid, so we must be replicating
617 if (inputs.n_total() < in.n_total()
618 && (in.n_total() % inputs.n_total() == 0)) {
620 return in.n_total() / inputs.n_total();
628 PluginInsert::get_state(void)
634 PluginInsert::state (bool full)
637 XMLNode& node = Processor::state (full);
639 node.add_property ("type", _plugins[0]->state_node_name());
640 snprintf(buf, sizeof(buf), "%s", _plugins[0]->name());
641 node.add_property("id", string(buf));
642 if (_plugins[0]->state_node_name() == "ladspa") {
644 snprintf (buf, sizeof (buf), "%ld", _plugins[0]->get_info()->unique_id);
645 node.add_property("unique-id", string(buf));
647 node.add_property("count", string_compose("%1", _plugins.size()));
648 node.add_child_nocopy (_plugins[0]->get_state());
650 /* add port automation state */
651 XMLNode *autonode = new XMLNode(port_automation_node_name);
652 set<ParamID> automatable = _plugins[0]->automatable();
654 for (set<ParamID>::iterator x = automatable.begin(); x != automatable.end(); ++x) {
656 /*XMLNode* child = new XMLNode("port");
657 snprintf(buf, sizeof(buf), "%" PRIu32, *x);
658 child->add_property("number", string(buf));
660 child->add_child_nocopy (automation_list (*x).state (full));
661 autonode->add_child_nocopy (*child);
663 autonode->add_child_nocopy (automation_list (*x)->state (full));
666 node.add_child_nocopy (*autonode);
672 PluginInsert::set_state(const XMLNode& node)
674 XMLNodeList nlist = node.children();
675 XMLNodeIterator niter;
676 XMLPropertyList plist;
677 const XMLProperty *prop;
679 ARDOUR::PluginType type;
681 if ((prop = node.property ("type")) == 0) {
682 error << _("XML node describing insert is missing the `type' field") << endmsg;
686 if (prop->value() == X_("ladspa") || prop->value() == X_("Ladspa")) { /* handle old school sessions */
687 type = ARDOUR::LADSPA;
688 } else if (prop->value() == X_("vst")) {
691 error << string_compose (_("unknown plugin type %1 in plugin insert state"),
697 prop = node.property ("unique-id");
699 unique = atol(prop->value().c_str());
702 if ((prop = node.property ("id")) == 0) {
703 error << _("XML node describing insert is missing the `id' field") << endmsg;
707 boost::shared_ptr<Plugin> plugin;
710 plugin = find_plugin (_session, "", unique, type);
712 plugin = find_plugin (_session, prop->value(), 0, type);
716 error << string_compose(_("Found a reference to a plugin (\"%1\") that is unknown.\n"
717 "Perhaps it was removed or moved since it was last used."), prop->value())
724 if ((prop = node.property ("count")) != 0) {
725 sscanf (prop->value().c_str(), "%u", &count);
728 if (_plugins.size() != count) {
730 _plugins.push_back (plugin);
732 for (uint32_t n=1; n < count; ++n) {
733 _plugins.push_back (plugin_factory (plugin));
737 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
738 if ((*niter)->name() == plugin->state_node_name()) {
739 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
740 (*i)->set_state (**niter);
746 const XMLNode* insert_node = &node;
748 // legacy sessions: search for child IOProcessor node
749 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
750 if ((*niter)->name() == "IOProcessor") {
751 insert_node = *niter;
756 Processor::set_state (*insert_node);
758 /* look for port automation node */
760 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
762 if ((*niter)->name() != port_automation_node_name) {
768 XMLNodeConstIterator iter;
773 cnodes = (*niter)->children ("port");
775 for(iter = cnodes.begin(); iter != cnodes.end(); ++iter){
779 if ((cprop = child->property("number")) != 0) {
780 port = cprop->value().c_str();
782 warning << _("PluginInsert: Auto: no ladspa port number") << endmsg;
786 sscanf (port, "%" PRIu32, &port_id);
788 if (port_id >= _plugins[0]->parameter_count()) {
789 warning << _("PluginInsert: Auto: port id out of range") << endmsg;
793 if (!child->children().empty()) {
794 automation_list (ParamID(PluginAutomation, port_id), true)->set_state (*child->children().front());
796 if ((cprop = child->property("auto")) != 0) {
801 sscanf (cprop->value().c_str(), "0x%x", &x);
802 automation_list (ParamID(PluginAutomation, port_id), true)->set_automation_state (AutoState (x));
808 automation_list (ParamID(PluginAutomation, port_id), true)->set_automation_state (Off);
819 if (niter == nlist.end()) {
820 warning << string_compose(_("XML node describing a port automation is missing the `%1' information"), port_automation_node_name) << endmsg;
823 // The name of the PluginInsert comes from the plugin, nothing else
824 _name = plugin->get_info()->name;
830 PluginInsert::describe_parameter (ParamID param)
832 if (param.type() != PluginAutomation)
833 return Automatable::describe_parameter(param);
835 return _plugins[0]->describe_parameter (param);
839 PluginInsert::latency()
841 return _plugins[0]->latency ();
845 PluginInsert::type ()
847 boost::shared_ptr<LadspaPlugin> lp;
849 boost::shared_ptr<VSTPlugin> vp;
851 #ifdef HAVE_AUDIOUNITS
852 boost::shared_ptr<AUPlugin> ap;
855 PluginPtr other = plugin ();
857 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
858 return ARDOUR::LADSPA;
860 } else if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (other)) != 0) {
863 #ifdef HAVE_AUDIOUNITS
864 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
865 return ARDOUR::AudioUnit;
869 return (ARDOUR::PluginType) 0;