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 // set_automatable ();
86 Glib::Mutex::Lock em (_session.engine().process_lock());
87 IO::PortCountChanged (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)));
103 ProcessorCreated (this); /* EMIT SIGNAL */
107 PluginInsert::set_count (uint32_t num)
109 bool require_state = !_plugins.empty();
111 /* this is a bad idea.... we shouldn't do this while active.
112 only a route holding their redirect_lock should be calling this
117 } else if (num > _plugins.size()) {
118 uint32_t diff = num - _plugins.size();
120 for (uint32_t n = 0; n < diff; ++n) {
121 _plugins.push_back (plugin_factory (_plugins[0]));
124 /* XXX do something */
128 } else if (num < _plugins.size()) {
129 uint32_t diff = _plugins.size() - num;
130 for (uint32_t n= 0; n < diff; ++n) {
139 PluginInsert::init ()
144 PluginInsert::~PluginInsert ()
146 GoingAway (); /* EMIT SIGNAL */
150 PluginInsert::auto_state_changed (Parameter which)
152 if (which.type() != PluginAutomation)
155 boost::shared_ptr<AutomationControl> c
156 = boost::dynamic_pointer_cast<AutomationControl>(data().control (which));
158 if (c && ((AutomationList*)c->list().get())->automation_state() != Off) {
159 _plugins[0]->set_parameter (which.id(), c->list()->eval (_session.transport_frame()));
164 PluginInsert::output_streams() const
166 ChanCount out = _plugins.front()->get_info()->n_outputs;
168 if (out == ChanCount::INFINITE) {
170 return _plugins.front()->output_streams ();
174 out.set_audio (out.n_audio() * _plugins.size());
175 out.set_midi (out.n_midi() * _plugins.size());
182 PluginInsert::input_streams() const
184 ChanCount in = _plugins[0]->get_info()->n_inputs;
186 if (in == ChanCount::INFINITE) {
187 return _plugins[0]->input_streams ();
189 in.set_audio (in.n_audio() * _plugins.size());
190 in.set_midi (in.n_midi() * _plugins.size());
197 PluginInsert::natural_output_streams() const
199 return _plugins[0]->get_info()->n_outputs;
203 PluginInsert::natural_input_streams() const
205 return _plugins[0]->get_info()->n_inputs;
209 PluginInsert::is_generator() const
211 /* XXX more finesse is possible here. VST plugins have a
212 a specific "instrument" flag, for example.
215 return _plugins[0]->get_info()->n_inputs.n_audio() == 0;
219 PluginInsert::set_automatable ()
221 set<Parameter> a = _plugins.front()->automatable ();
223 Plugin::ParameterDescriptor desc;
225 for (set<Parameter>::iterator i = a.begin(); i != a.end(); ++i) {
226 if (i->type() == PluginAutomation) {
228 _plugins.front()->get_parameter_descriptor(i->id(), desc);
230 param.set_range(desc.lower, desc.upper, _plugins.front()->default_value(i->id()));
231 boost::shared_ptr<AutomationList> list(new AutomationList(param));
232 add_control(boost::shared_ptr<AutomationControl>(new PluginControl(*this, list)));
238 PluginInsert::parameter_changed (Parameter which, float val)
240 if (which.type() != PluginAutomation)
243 vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin();
245 /* don't set the first plugin, just all the slaves */
247 if (i != _plugins.end()) {
249 for (; i != _plugins.end(); ++i) {
250 (*i)->set_parameter (which, val);
256 PluginInsert::set_block_size (nframes_t nframes)
258 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
259 (*i)->set_block_size (nframes);
264 PluginInsert::activate ()
266 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
272 PluginInsert::deactivate ()
274 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
280 PluginInsert::connect_and_run (BufferSet& bufs, nframes_t nframes, nframes_t offset, bool with_auto, nframes_t now)
282 uint32_t in_index = 0;
283 uint32_t out_index = 0;
285 /* Note that we've already required that plugins
286 be able to handle in-place processing.
293 for (Controls::iterator li = data().controls().begin(); li != data().controls().end(); ++li, ++n) {
295 boost::shared_ptr<AutomationControl> c
296 = boost::dynamic_pointer_cast<AutomationControl>(li->second);
298 if (c->parameter().type() == PluginAutomation && c->automation_playback()) {
301 const float val = c->list()->rt_safe_eval (now, valid);
311 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
312 (*i)->connect_and_run (bufs, in_index, out_index, nframes, offset);
315 /* leave remaining channel buffers alone */
319 PluginInsert::silence (nframes_t nframes, nframes_t offset)
321 uint32_t in_index = 0;
322 uint32_t out_index = 0;
325 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
326 (*i)->connect_and_run (_session.get_silent_buffers ((*i)->get_info()->n_inputs), in_index, out_index, nframes, offset);
332 PluginInsert::run_in_place (BufferSet& bufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset)
336 if (_session.transport_rolling()) {
337 automation_run (bufs, nframes, offset);
339 connect_and_run (bufs, nframes, offset, false);
343 /* FIXME: type, audio only */
345 uint32_t in = _plugins[0]->get_info()->n_inputs.n_audio();
346 uint32_t out = _plugins[0]->get_info()->n_outputs.n_audio();
350 /* not active, but something has make up for any channel count increase */
352 for (uint32_t n = out - in; n < out; ++n) {
353 memcpy (bufs.get_audio(n).data(nframes, offset), bufs.get_audio(in - 1).data(nframes, offset), sizeof (Sample) * nframes);
357 bufs.count().set_audio(out);
362 PluginInsert::set_parameter (Parameter param, float val)
364 if (param.type() != PluginAutomation)
367 /* the others will be set from the event triggered by this */
369 _plugins[0]->set_parameter (param.id(), val);
371 boost::shared_ptr<Evoral::Control> c = data().control (param);
375 _session.set_dirty();
379 PluginInsert::get_parameter (Parameter param)
381 if (param.type() != PluginAutomation)
385 _plugins[0]->get_parameter (param.id());
389 PluginInsert::automation_run (BufferSet& bufs, nframes_t nframes, nframes_t offset)
391 ControlEvent next_event (0, 0.0f);
392 nframes_t now = _session.transport_frame ();
393 nframes_t end = now + nframes;
395 Glib::Mutex::Lock lm (data().control_lock(), Glib::TRY_LOCK);
398 connect_and_run (bufs, nframes, offset, false);
402 if (!data().find_next_event (now, end, next_event)) {
404 /* no events have a time within the relevant range */
406 connect_and_run (bufs, nframes, offset, true, now);
412 nframes_t cnt = min (((nframes_t) ceil (next_event.when) - now), nframes);
414 connect_and_run (bufs, cnt, offset, true, now);
420 if (!data().find_next_event (now, end, next_event)) {
425 /* cleanup anything that is left to do */
428 connect_and_run (bufs, nframes, offset, true, now);
433 PluginInsert::default_parameter_value (const Evoral::Parameter& param)
435 if (param.type() != PluginAutomation)
438 if (_plugins.empty()) {
439 fatal << _("programming error: ") << X_("PluginInsert::default_parameter_value() called with no plugin")
444 return _plugins[0]->default_value (param.id());
447 boost::shared_ptr<Plugin>
448 PluginInsert::plugin_factory (boost::shared_ptr<Plugin> other)
450 boost::shared_ptr<LadspaPlugin> lp;
452 boost::shared_ptr<LV2Plugin> lv2p;
455 boost::shared_ptr<VSTPlugin> vp;
457 #ifdef HAVE_AUDIOUNITS
458 boost::shared_ptr<AUPlugin> ap;
461 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
462 return boost::shared_ptr<Plugin> (new LadspaPlugin (*lp));
464 } else if ((lv2p = boost::dynamic_pointer_cast<LV2Plugin> (other)) != 0) {
465 return boost::shared_ptr<Plugin> (new LV2Plugin (*lv2p));
468 } else if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (other)) != 0) {
469 return boost::shared_ptr<Plugin> (new VSTPlugin (*vp));
471 #ifdef HAVE_AUDIOUNITS
472 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
473 return boost::shared_ptr<Plugin> (new AUPlugin (*ap));
477 fatal << string_compose (_("programming error: %1"),
478 X_("unknown plugin type in PluginInsert::plugin_factory"))
481 return boost::shared_ptr<Plugin> ((Plugin*) 0);
485 PluginInsert::configure_io (ChanCount in, ChanCount out)
487 if (set_count (count_for_configuration (in, out)) < 0) {
491 /* if we're running replicated plugins, each plugin has
492 the same i/o configuration and we may need to announce how many
493 output streams there are.
495 if we running a single plugin, we need to configure it.
498 if (_plugins.front()->configure_io (in, out) < 0) {
502 return Processor::configure_io (in, out);
506 PluginInsert::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
508 if (_plugins.front()->reconfigurable_io()) {
509 /* plugin has flexible I/O, so delegate to it */
510 return _plugins.front()->can_support_io_configuration (in, out);
513 ChanCount outputs = _plugins[0]->get_info()->n_outputs;
514 ChanCount inputs = _plugins[0]->get_info()->n_inputs;
516 if ((inputs.n_total() == 0)
517 || (inputs.n_total() == 1 && outputs == inputs)
518 || (inputs.n_total() == 1 && outputs == inputs
519 && ((inputs.n_audio() == 0 && in.n_audio() == 0)
520 || (inputs.n_midi() == 0 && in.n_midi() == 0)))
526 bool can_replicate = true;
528 /* if number of inputs is a factor of the requested input
529 configuration for every type, we can replicate.
531 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
532 if (inputs.get(*t) >= in.get(*t) || (inputs.get(*t) % in.get(*t) != 0)) {
533 can_replicate = false;
538 if (!can_replicate || (in.n_total() % inputs.n_total() != 0)) {
542 if (inputs.n_total() == 0) {
543 /* instrument plugin, always legal, but throws away any existing streams */
545 } else if (inputs.n_total() == 1 && outputs == inputs
546 && ((inputs.n_audio() == 0 && in.n_audio() == 0)
547 || (inputs.n_midi() == 0 && in.n_midi() == 0))) {
548 /* mono, single-typed plugin, replicate as needed to match in */
550 } else if (inputs == in) {
554 /* replicate - note that we've already verified that
555 the replication count is constant across all data types.
557 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
558 out.set (*t, outputs.get(*t) * (in.get(*t) / inputs.get(*t)));
565 /* Number of plugin instances required to support a given channel configuration.
569 PluginInsert::count_for_configuration (ChanCount in, ChanCount out) const
571 if (_plugins.front()->reconfigurable_io()) {
572 /* plugin has flexible I/O, so the answer is always 1 */
573 /* this could change if we ever decide to replicate AU's */
577 // FIXME: take 'out' into consideration
579 ChanCount outputs = _plugins[0]->get_info()->n_outputs;
580 ChanCount inputs = _plugins[0]->get_info()->n_inputs;
582 if (inputs.n_total() == 0) {
583 /* instrument plugin, always legal, but throws away any existing streams */
587 if (inputs.n_total() == 1 && outputs == inputs
588 && ((inputs.n_audio() == 0 && in.n_audio() == 0)
589 || (inputs.n_midi() == 0 && in.n_midi() == 0))) {
590 /* mono plugin, replicate as needed to match in */
599 // assumes in is valid, so we must be replicating
600 if (inputs.n_total() < in.n_total()
601 && (in.n_total() % inputs.n_total() == 0)) {
603 return in.n_total() / inputs.n_total();
611 PluginInsert::get_state(void)
617 PluginInsert::state (bool full)
619 XMLNode& node = Processor::state (full);
621 node.add_property ("type", _plugins[0]->state_node_name());
622 node.add_property("unique-id", _plugins[0]->unique_id());
623 node.add_property("count", string_compose("%1", _plugins.size()));
624 node.add_child_nocopy (_plugins[0]->get_state());
626 /* add port automation state */
627 XMLNode *autonode = new XMLNode(port_automation_node_name);
628 set<Parameter> automatable = _plugins[0]->automatable();
630 for (set<Parameter>::iterator x = automatable.begin(); x != automatable.end(); ++x) {
632 /*XMLNode* child = new XMLNode("port");
633 snprintf(buf, sizeof(buf), "%" PRIu32, *x);
634 child->add_property("number", string(buf));
636 child->add_child_nocopy (automation_list (*x).state (full));
637 autonode->add_child_nocopy (*child);
639 autonode->add_child_nocopy (((AutomationList*)data().control(*x)->list().get())->state (full));
642 node.add_child_nocopy (*autonode);
648 PluginInsert::set_state(const XMLNode& node)
650 XMLNodeList nlist = node.children();
651 XMLNodeIterator niter;
652 XMLPropertyList plist;
653 const XMLProperty *prop;
654 ARDOUR::PluginType type;
656 if ((prop = node.property ("type")) == 0) {
657 error << _("XML node describing insert is missing the `type' field") << endmsg;
661 if (prop->value() == X_("ladspa") || prop->value() == X_("Ladspa")) { /* handle old school sessions */
662 type = ARDOUR::LADSPA;
663 } else if (prop->value() == X_("lv2")) {
665 } else if (prop->value() == X_("vst")) {
668 error << string_compose (_("unknown plugin type %1 in plugin insert state"),
674 prop = node.property ("unique-id");
676 error << _("Plugin has no unique ID field") << endmsg;
680 boost::shared_ptr<Plugin> plugin;
682 plugin = find_plugin (_session, prop->value(), type);
685 error << string_compose(_("Found a reference to a plugin (\"%1\") that is unknown.\n"
686 "Perhaps it was removed or moved since it was last used."), prop->value())
693 if ((prop = node.property ("count")) != 0) {
694 sscanf (prop->value().c_str(), "%u", &count);
697 if (_plugins.size() != count) {
699 _plugins.push_back (plugin);
701 for (uint32_t n=1; n < count; ++n) {
702 _plugins.push_back (plugin_factory (plugin));
706 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
707 if ((*niter)->name() == plugin->state_node_name()) {
708 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
709 (*i)->set_state (**niter);
715 const XMLNode* insert_node = &node;
717 // legacy sessions: search for child IOProcessor node
718 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
719 if ((*niter)->name() == "IOProcessor") {
720 insert_node = *niter;
725 Processor::set_state (*insert_node);
727 /* look for port automation node */
729 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
731 if ((*niter)->name() != port_automation_node_name) {
737 XMLNodeConstIterator iter;
742 cnodes = (*niter)->children ("port");
744 for(iter = cnodes.begin(); iter != cnodes.end(); ++iter){
748 if ((cprop = child->property("number")) != 0) {
749 port = cprop->value().c_str();
751 warning << _("PluginInsert: Auto: no ladspa port number") << endmsg;
755 sscanf (port, "%" PRIu32, &port_id);
757 if (port_id >= _plugins[0]->parameter_count()) {
758 warning << _("PluginInsert: Auto: port id out of range") << endmsg;
762 boost::shared_ptr<AutomationControl> c = boost::dynamic_pointer_cast<AutomationControl>(
763 data().control(Parameter(PluginAutomation, port_id), true));
765 if (!child->children().empty()) {
766 c->alist()->set_state (*child->children().front());
768 if ((cprop = child->property("auto")) != 0) {
773 sscanf (cprop->value().c_str(), "0x%x", &x);
774 c->alist()->set_automation_state (AutoState (x));
780 c->alist()->set_automation_state (Off);
791 if (niter == nlist.end()) {
792 warning << string_compose(_("XML node describing a port automation is missing the `%1' information"), port_automation_node_name) << endmsg;
795 // The name of the PluginInsert comes from the plugin, nothing else
796 _name = plugin->get_info()->name;
802 PluginInsert::describe_parameter (Parameter param)
804 if (param.type() != PluginAutomation)
805 return Automatable::describe_parameter(param);
807 return _plugins[0]->describe_parameter (param);
811 PluginInsert::signal_latency() const
814 return _user_latency;
817 return _plugins[0]->signal_latency ();
821 PluginInsert::type ()
823 boost::shared_ptr<LadspaPlugin> lp;
825 boost::shared_ptr<VSTPlugin> vp;
827 #ifdef HAVE_AUDIOUNITS
828 boost::shared_ptr<AUPlugin> ap;
831 PluginPtr other = plugin ();
833 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
834 return ARDOUR::LADSPA;
836 } else if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (other)) != 0) {
839 #ifdef HAVE_AUDIOUNITS
840 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
841 return ARDOUR::AudioUnit;
845 return (ARDOUR::PluginType) 0;
849 PluginInsert::PluginControl::PluginControl (PluginInsert& p, boost::shared_ptr<AutomationList> list)
850 : AutomationControl (p.session(), list->parameter(), list, p.describe_parameter(list->parameter()))
854 Plugin::ParameterDescriptor desc;
855 p.plugin(0)->get_parameter_descriptor (list->parameter().id(), desc);
856 _logarithmic = desc.logarithmic;
857 _toggled = desc.toggled;
861 PluginInsert::PluginControl::set_value (float val)
863 /* FIXME: probably should be taking out some lock here.. */
873 /*const float range = _list->get_max_y() - _list->get_min_y();
874 const float lower = _list->get_min_y();
877 val = lower + (range * val);
879 float log_lower = 0.0f;
881 log_lower = log(lower);
884 val = exp(log_lower + log(range) * val);
889 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugin._plugins.begin();
890 i != _plugin._plugins.end(); ++i) {
891 (*i)->set_parameter (_list->parameter().id(), val);
894 AutomationControl::set_value(val);
898 PluginInsert::PluginControl::get_value (void) const
900 /* FIXME: probably should be taking out some lock here.. */
902 float val = _plugin.get_parameter (_list->parameter());
916 return ((val - lower) / range);