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_list.h>
36 #include <ardour/lv2_plugin.h>
40 #include <ardour/vst_plugin.h>
43 #ifdef HAVE_AUDIOUNITS
44 #include <ardour/audio_unit.h>
47 #include <ardour/audioengine.h>
48 #include <ardour/session.h>
49 #include <ardour/types.h>
54 using namespace ARDOUR;
57 const string PluginInsert::port_automation_node_name = "PortAutomation";
59 PluginInsert::PluginInsert (Session& s, boost::shared_ptr<Plugin> plug, Placement placement)
60 : Processor (s, plug->name(), placement)
62 /* the first is the master */
64 _plugins.push_back (plug);
69 Glib::Mutex::Lock em (_session.engine().process_lock());
70 IO::PortCountChanged (max(input_streams(), output_streams()));
73 ProcessorCreated (this); /* EMIT SIGNAL */
76 PluginInsert::PluginInsert (Session& s, const XMLNode& node)
77 : Processor (s, "unnamed plugin insert", PreFader)
79 if (set_state (node)) {
80 throw failed_constructor();
83 // XXX: This would dump all automation, which has already been loaded by
84 // Processor. But this could also have been related to the Parameter change..
85 // will look into this later.
89 Glib::Mutex::Lock em (_session.engine().process_lock());
90 IO::PortCountChanged (max(input_streams(), output_streams()));
94 PluginInsert::PluginInsert (const PluginInsert& other)
95 : Processor (other._session, other._name, other.placement())
97 uint32_t count = other._plugins.size();
99 /* make as many copies as requested */
100 for (uint32_t n = 0; n < count; ++n) {
101 _plugins.push_back (plugin_factory (other.plugin (n)));
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 ()
147 PluginInsert::~PluginInsert ()
149 GoingAway (); /* EMIT SIGNAL */
153 PluginInsert::auto_state_changed (Evoral::Parameter which)
155 if (which.type() != PluginAutomation)
158 boost::shared_ptr<AutomationControl> c
159 = boost::dynamic_pointer_cast<AutomationControl>(data().control (which));
161 if (c && ((AutomationList*)c->list().get())->automation_state() != Off) {
162 _plugins[0]->set_parameter (which.id(), c->list()->eval (_session.transport_frame()));
167 PluginInsert::output_streams() const
169 ChanCount out = _plugins.front()->get_info()->n_outputs;
171 if (out == ChanCount::INFINITE) {
173 return _plugins.front()->output_streams ();
177 out.set_audio (out.n_audio() * _plugins.size());
178 out.set_midi (out.n_midi() * _plugins.size());
185 PluginInsert::input_streams() const
187 ChanCount in = _plugins[0]->get_info()->n_inputs;
189 if (in == ChanCount::INFINITE) {
190 return _plugins[0]->input_streams ();
192 in.set_audio (in.n_audio() * _plugins.size());
193 in.set_midi (in.n_midi() * _plugins.size());
200 PluginInsert::natural_output_streams() const
202 return _plugins[0]->get_info()->n_outputs;
206 PluginInsert::natural_input_streams() const
208 return _plugins[0]->get_info()->n_inputs;
212 PluginInsert::is_generator() const
214 /* XXX more finesse is possible here. VST plugins have a
215 a specific "instrument" flag, for example.
218 return _plugins[0]->get_info()->n_inputs.n_audio() == 0;
222 PluginInsert::set_automatable ()
224 set<Evoral::Parameter> a = _plugins.front()->automatable ();
226 Plugin::ParameterDescriptor desc;
228 for (set<Evoral::Parameter>::iterator i = a.begin(); i != a.end(); ++i) {
229 if (i->type() == PluginAutomation) {
231 _plugins.front()->get_parameter_descriptor(i->id(), desc);
232 Evoral::Parameter param(*i);
233 param.set_range(desc.lower, desc.upper, _plugins.front()->default_value(i->id()));
234 boost::shared_ptr<AutomationList> list(new AutomationList(param));
235 add_control(boost::shared_ptr<AutomationControl>(new PluginControl(this, *i, list)));
241 PluginInsert::parameter_changed (Evoral::Parameter which, float val)
243 if (which.type() != PluginAutomation)
246 vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin();
248 /* don't set the first plugin, just all the slaves */
250 if (i != _plugins.end()) {
252 for (; i != _plugins.end(); ++i) {
253 (*i)->set_parameter (which, val);
259 PluginInsert::set_block_size (nframes_t nframes)
261 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
262 (*i)->set_block_size (nframes);
267 PluginInsert::activate ()
269 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
275 PluginInsert::deactivate ()
277 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
283 PluginInsert::connect_and_run (BufferSet& bufs, nframes_t nframes, nframes_t offset, bool with_auto, nframes_t now)
285 uint32_t in_index = 0;
286 uint32_t out_index = 0;
288 /* Note that we've already required that plugins
289 be able to handle in-place processing.
296 for (Controls::iterator li = data().controls().begin(); li != data().controls().end(); ++li, ++n) {
298 boost::shared_ptr<AutomationControl> c
299 = boost::dynamic_pointer_cast<AutomationControl>(li->second);
301 if (c->parameter().type() == PluginAutomation && c->automation_playback()) {
304 const float val = c->list()->rt_safe_eval (now, valid);
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::silence (nframes_t nframes, nframes_t offset)
324 uint32_t in_index = 0;
325 uint32_t out_index = 0;
328 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
329 (*i)->connect_and_run (_session.get_silent_buffers ((*i)->get_info()->n_inputs), in_index, out_index, nframes, offset);
335 PluginInsert::run_in_place (BufferSet& bufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset)
339 if (_session.transport_rolling()) {
340 automation_run (bufs, nframes, offset);
342 connect_and_run (bufs, nframes, offset, false);
346 /* FIXME: type, audio only */
348 uint32_t in = _plugins[0]->get_info()->n_inputs.n_audio();
349 uint32_t out = _plugins[0]->get_info()->n_outputs.n_audio();
353 /* not active, but something has make up for any channel count increase */
355 for (uint32_t n = out - in; n < out; ++n) {
356 memcpy (bufs.get_audio(n).data(nframes, offset), bufs.get_audio(in - 1).data(nframes, offset), sizeof (Sample) * nframes);
360 bufs.count().set_audio(out);
365 PluginInsert::set_parameter (Evoral::Parameter param, float val)
367 if (param.type() != PluginAutomation)
370 /* the others will be set from the event triggered by this */
372 _plugins[0]->set_parameter (param.id(), val);
374 boost::shared_ptr<AutomationControl> ac
375 = boost::dynamic_pointer_cast<AutomationControl>(data().control(param));
380 warning << "set_parameter called for nonexistant parameter "
381 << EventTypeMap::instance().to_symbol(param) << endmsg;
384 _session.set_dirty();
388 PluginInsert::get_parameter (Evoral::Parameter param)
390 if (param.type() != PluginAutomation)
394 _plugins[0]->get_parameter (param.id());
398 PluginInsert::automation_run (BufferSet& bufs, nframes_t nframes, nframes_t offset)
400 Evoral::ControlEvent next_event (0, 0.0f);
401 nframes_t now = _session.transport_frame ();
402 nframes_t end = now + nframes;
404 Glib::Mutex::Lock lm (data().control_lock(), Glib::TRY_LOCK);
407 connect_and_run (bufs, nframes, offset, false);
411 if (!data().find_next_event (now, end, next_event)) {
413 /* no events have a time within the relevant range */
415 connect_and_run (bufs, nframes, offset, true, now);
421 nframes_t cnt = min (((nframes_t) ceil (next_event.when) - now), nframes);
423 connect_and_run (bufs, cnt, offset, true, now);
429 if (!data().find_next_event (now, end, next_event)) {
434 /* cleanup anything that is left to do */
437 connect_and_run (bufs, nframes, offset, true, now);
442 PluginInsert::default_parameter_value (const Evoral::Parameter& param)
444 if (param.type() != PluginAutomation)
447 if (_plugins.empty()) {
448 fatal << _("programming error: ") << X_("PluginInsert::default_parameter_value() called with no plugin")
453 return _plugins[0]->default_value (param.id());
456 boost::shared_ptr<Plugin>
457 PluginInsert::plugin_factory (boost::shared_ptr<Plugin> other)
459 boost::shared_ptr<LadspaPlugin> lp;
461 boost::shared_ptr<LV2Plugin> lv2p;
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 ((lv2p = boost::dynamic_pointer_cast<LV2Plugin> (other)) != 0) {
474 return boost::shared_ptr<Plugin> (new LV2Plugin (*lv2p));
477 } else if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (other)) != 0) {
478 return boost::shared_ptr<Plugin> (new VSTPlugin (*vp));
480 #ifdef HAVE_AUDIOUNITS
481 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
482 return boost::shared_ptr<Plugin> (new AUPlugin (*ap));
486 fatal << string_compose (_("programming error: %1"),
487 X_("unknown plugin type in PluginInsert::plugin_factory"))
490 return boost::shared_ptr<Plugin> ((Plugin*) 0);
494 PluginInsert::configure_io (ChanCount in, ChanCount out)
496 if (set_count (count_for_configuration (in, out)) < 0) {
500 /* if we're running replicated plugins, each plugin has
501 the same i/o configuration and we may need to announce how many
502 output streams there are.
504 if we running a single plugin, we need to configure it.
507 if (_plugins.front()->configure_io (in, out) < 0) {
511 return Processor::configure_io (in, out);
515 PluginInsert::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
517 if (_plugins.front()->reconfigurable_io()) {
518 /* plugin has flexible I/O, so delegate to it */
519 return _plugins.front()->can_support_io_configuration (in, out);
522 ChanCount outputs = _plugins[0]->get_info()->n_outputs;
523 ChanCount inputs = _plugins[0]->get_info()->n_inputs;
525 if ((inputs.n_total() == 0)
526 || (inputs.n_total() == 1 && outputs == inputs)
527 || (inputs.n_total() == 1 && outputs == inputs
528 && ((inputs.n_audio() == 0 && in.n_audio() == 0)
529 || (inputs.n_midi() == 0 && in.n_midi() == 0)))
535 bool can_replicate = true;
537 /* if number of inputs is a factor of the requested input
538 configuration for every type, we can replicate.
540 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
541 if (inputs.get(*t) >= in.get(*t) || (inputs.get(*t) % in.get(*t) != 0)) {
542 can_replicate = false;
547 if (!can_replicate || (in.n_total() % inputs.n_total() != 0)) {
551 if (inputs.n_total() == 0) {
552 /* instrument plugin, always legal, but throws away any existing streams */
554 } else if (inputs.n_total() == 1 && outputs == inputs
555 && ((inputs.n_audio() == 0 && in.n_audio() == 0)
556 || (inputs.n_midi() == 0 && in.n_midi() == 0))) {
557 /* mono, single-typed plugin, replicate as needed to match in */
559 } else if (inputs == in) {
563 /* replicate - note that we've already verified that
564 the replication count is constant across all data types.
566 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
567 out.set (*t, outputs.get(*t) * (in.get(*t) / inputs.get(*t)));
574 /* Number of plugin instances required to support a given channel configuration.
578 PluginInsert::count_for_configuration (ChanCount in, ChanCount out) const
580 if (_plugins.front()->reconfigurable_io()) {
581 /* plugin has flexible I/O, so the answer is always 1 */
582 /* this could change if we ever decide to replicate AU's */
586 // FIXME: take 'out' into consideration
588 ChanCount outputs = _plugins[0]->get_info()->n_outputs;
589 ChanCount inputs = _plugins[0]->get_info()->n_inputs;
591 if (inputs.n_total() == 0) {
592 /* instrument plugin, always legal, but throws away any existing streams */
596 if (inputs.n_total() == 1 && outputs == inputs
597 && ((inputs.n_audio() == 0 && in.n_audio() == 0)
598 || (inputs.n_midi() == 0 && in.n_midi() == 0))) {
599 /* mono plugin, replicate as needed to match in */
608 // assumes in is valid, so we must be replicating
609 if (inputs.n_total() < in.n_total()
610 && (in.n_total() % inputs.n_total() == 0)) {
612 return in.n_total() / inputs.n_total();
620 PluginInsert::get_state(void)
626 PluginInsert::state (bool full)
628 XMLNode& node = Processor::state (full);
630 node.add_property ("type", _plugins[0]->state_node_name());
631 node.add_property("unique-id", _plugins[0]->unique_id());
632 node.add_property("count", string_compose("%1", _plugins.size()));
633 node.add_child_nocopy (_plugins[0]->get_state());
635 /* add port automation state */
636 //XMLNode *autonode = new XMLNode(port_automation_node_name);
637 set<Evoral::Parameter> automatable = _plugins[0]->automatable();
639 for (set<Evoral::Parameter>::iterator x = automatable.begin(); x != automatable.end(); ++x) {
641 /*XMLNode* child = new XMLNode("port");
642 snprintf(buf, sizeof(buf), "%" PRIu32, *x);
643 child->add_property("number", string(buf));
645 child->add_child_nocopy (automation_list (*x).state (full));
646 autonode->add_child_nocopy (*child);
648 //autonode->add_child_nocopy (((AutomationList*)data().control(*x)->list().get())->state (full));
651 //node.add_child_nocopy (*autonode);
657 PluginInsert::set_state(const XMLNode& node)
659 XMLNodeList nlist = node.children();
660 XMLNodeIterator niter;
661 XMLPropertyList plist;
662 const XMLProperty *prop;
663 ARDOUR::PluginType type;
665 if ((prop = node.property ("type")) == 0) {
666 error << _("XML node describing insert is missing the `type' field") << endmsg;
670 if (prop->value() == X_("ladspa") || prop->value() == X_("Ladspa")) { /* handle old school sessions */
671 type = ARDOUR::LADSPA;
672 } else if (prop->value() == X_("lv2")) {
674 } else if (prop->value() == X_("vst")) {
677 error << string_compose (_("unknown plugin type %1 in plugin insert state"),
683 prop = node.property ("unique-id");
685 error << _("Plugin has no unique ID field") << endmsg;
689 boost::shared_ptr<Plugin> plugin;
691 plugin = find_plugin (_session, prop->value(), type);
694 error << string_compose(_("Found a reference to a plugin (\"%1\") that is unknown.\n"
695 "Perhaps it was removed or moved since it was last used."), prop->value())
702 if ((prop = node.property ("count")) != 0) {
703 sscanf (prop->value().c_str(), "%u", &count);
706 if (_plugins.size() != count) {
708 _plugins.push_back (plugin);
710 for (uint32_t n=1; n < count; ++n) {
711 _plugins.push_back (plugin_factory (plugin));
715 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
716 if ((*niter)->name() == plugin->state_node_name()) {
717 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
718 (*i)->set_state (**niter);
724 const XMLNode* insert_node = &node;
726 // legacy sessions: search for child IOProcessor node
727 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
728 if ((*niter)->name() == "IOProcessor") {
729 insert_node = *niter;
734 Processor::set_state (*insert_node);
736 /* look for port automation node */
738 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
740 if ((*niter)->name() != port_automation_node_name) {
746 XMLNodeConstIterator iter;
751 cnodes = (*niter)->children ("port");
753 for(iter = cnodes.begin(); iter != cnodes.end(); ++iter){
757 if ((cprop = child->property("number")) != 0) {
758 port = cprop->value().c_str();
760 warning << _("PluginInsert: Auto: no ladspa port number") << endmsg;
764 sscanf (port, "%" PRIu32, &port_id);
766 if (port_id >= _plugins[0]->parameter_count()) {
767 warning << _("PluginInsert: Auto: port id out of range") << endmsg;
771 boost::shared_ptr<AutomationControl> c = boost::dynamic_pointer_cast<AutomationControl>(
772 data().control(Evoral::Parameter(PluginAutomation, 0, port_id), true));
774 if (!child->children().empty()) {
775 c->alist()->set_state (*child->children().front());
777 if ((cprop = child->property("auto")) != 0) {
782 sscanf (cprop->value().c_str(), "0x%x", &x);
783 c->alist()->set_automation_state (AutoState (x));
789 c->alist()->set_automation_state (Off);
800 if (niter == nlist.end()) {
801 warning << string_compose(_("XML node describing a port automation is missing the `%1' information"), port_automation_node_name) << endmsg;
804 // The name of the PluginInsert comes from the plugin, nothing else
805 _name = plugin->get_info()->name;
811 PluginInsert::describe_parameter (Evoral::Parameter param)
813 if (param.type() != PluginAutomation)
814 return Automatable::describe_parameter(param);
816 return _plugins[0]->describe_parameter (param);
820 PluginInsert::signal_latency() const
823 return _user_latency;
826 return _plugins[0]->signal_latency ();
830 PluginInsert::type ()
832 boost::shared_ptr<LadspaPlugin> lp;
834 boost::shared_ptr<VSTPlugin> vp;
836 #ifdef HAVE_AUDIOUNITS
837 boost::shared_ptr<AUPlugin> ap;
840 PluginPtr other = plugin ();
842 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
843 return ARDOUR::LADSPA;
845 } else if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (other)) != 0) {
848 #ifdef HAVE_AUDIOUNITS
849 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
850 return ARDOUR::AudioUnit;
854 return (ARDOUR::PluginType) 0;
858 PluginInsert::PluginControl::PluginControl (PluginInsert* p, const Evoral::Parameter ¶m, boost::shared_ptr<AutomationList> list)
859 : AutomationControl (p->session(), param, list, p->describe_parameter(param))
862 Plugin::ParameterDescriptor desc;
863 p->plugin(0)->get_parameter_descriptor (param.id(), desc);
864 _logarithmic = desc.logarithmic;
865 _toggled = desc.toggled;
869 PluginInsert::PluginControl::set_value (float val)
871 /* FIXME: probably should be taking out some lock here.. */
881 /*const float range = _list->get_max_y() - _list->get_min_y();
882 const float lower = _list->get_min_y();
885 val = lower + (range * val);
887 float log_lower = 0.0f;
889 log_lower = log(lower);
892 val = exp(log_lower + log(range) * val);
897 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugin->_plugins.begin();
898 i != _plugin->_plugins.end(); ++i) {
899 (*i)->set_parameter (_list->parameter().id(), val);
902 AutomationControl::set_value(val);
906 PluginInsert::PluginControl::get_value (void) const
908 /* FIXME: probably should be taking out some lock here.. */
910 float val = _plugin->get_parameter (_list->parameter());
924 return ((val - lower) / range);