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);
65 Glib::Mutex::Lock em (_session.engine().process_lock());
66 IO::MoreChannels (max(input_streams(), output_streams()));
69 ProcessorCreated (this); /* EMIT SIGNAL */
72 PluginInsert::PluginInsert (Session& s, const XMLNode& node)
73 : Processor (s, "unnamed plugin insert", PreFader)
75 if (set_state (node)) {
76 throw failed_constructor();
82 Glib::Mutex::Lock em (_session.engine().process_lock());
83 IO::MoreChannels (max(input_streams(), output_streams()));
87 PluginInsert::PluginInsert (const PluginInsert& other)
88 : Processor (other._session, other._name, other.placement())
90 uint32_t count = other._plugins.size();
92 /* make as many copies as requested */
93 for (uint32_t n = 0; n < count; ++n) {
94 _plugins.push_back (plugin_factory (other.plugin (n)));
99 ProcessorCreated (this); /* EMIT SIGNAL */
103 PluginInsert::set_count (uint32_t num)
105 bool require_state = !_plugins.empty();
107 /* this is a bad idea.... we shouldn't do this while active.
108 only a route holding their redirect_lock should be calling this
113 } else if (num > _plugins.size()) {
114 uint32_t diff = num - _plugins.size();
116 for (uint32_t n = 0; n < diff; ++n) {
117 _plugins.push_back (plugin_factory (_plugins[0]));
120 /* XXX do something */
124 } else if (num < _plugins.size()) {
125 uint32_t diff = _plugins.size() - num;
126 for (uint32_t n= 0; n < diff; ++n) {
135 PluginInsert::init ()
140 PluginInsert::~PluginInsert ()
142 GoingAway (); /* EMIT SIGNAL */
146 PluginInsert::auto_state_changed (Parameter which)
148 if (which.type() != PluginAutomation)
151 boost::shared_ptr<AutomationControl> c = control (which);
153 if (c && c->list()->automation_state() != Off) {
154 _plugins[0]->set_parameter (which.id(), c->list()->eval (_session.transport_frame()));
159 PluginInsert::output_streams() const
162 return output_for_input_configuration(_configured_input);
164 return natural_output_streams();
168 PluginInsert::input_streams() const
171 return _configured_input;
173 return natural_input_streams();
177 PluginInsert::natural_output_streams() const
179 return _plugins[0]->get_info()->n_outputs;
183 PluginInsert::natural_input_streams() const
185 return _plugins[0]->get_info()->n_inputs;
189 PluginInsert::is_generator() const
191 /* XXX more finesse is possible here. VST plugins have a
192 a specific "instrument" flag, for example.
195 return _plugins[0]->get_info()->n_inputs.n_audio() == 0;
199 PluginInsert::set_automatable ()
201 set<Parameter> a = _plugins.front()->automatable ();
203 Plugin::ParameterDescriptor desc;
205 for (set<Parameter>::iterator i = a.begin(); i != a.end(); ++i) {
206 if (i->type() == PluginAutomation) {
208 _plugins.front()->get_parameter_descriptor(i->id(), desc);
209 boost::shared_ptr<AutomationList> list(new AutomationList(
211 //(desc.min_unbound ? FLT_MIN : desc.lower),
212 //(desc.max_unbound ? FLT_MAX : desc.upper),
213 desc.lower, desc.upper,
214 _plugins.front()->default_value(i->id())));
216 add_control(boost::shared_ptr<AutomationControl>(
217 new PluginControl(*this, list)));
223 PluginInsert::parameter_changed (Parameter which, float val)
225 if (which.type() != PluginAutomation)
228 vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin();
230 /* don't set the first plugin, just all the slaves */
232 if (i != _plugins.end()) {
234 for (; i != _plugins.end(); ++i) {
235 (*i)->set_parameter (which, val);
241 PluginInsert::set_block_size (nframes_t nframes)
243 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
244 (*i)->set_block_size (nframes);
249 PluginInsert::activate ()
251 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
257 PluginInsert::deactivate ()
259 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
265 PluginInsert::connect_and_run (BufferSet& bufs, nframes_t nframes, nframes_t offset, bool with_auto, nframes_t now)
267 uint32_t in_index = 0;
268 uint32_t out_index = 0;
270 /* Note that we've already required that plugins
271 be able to handle in-place processing.
278 for (Controls::iterator li = _controls.begin(); li != _controls.end(); ++li, ++n) {
280 boost::shared_ptr<AutomationControl> c = li->second;
282 if (c->parameter().type() == PluginAutomation && c->list()->automation_playback()) {
285 const float val = c->list()->rt_safe_eval (now, valid);
295 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
296 (*i)->connect_and_run (bufs, in_index, out_index, nframes, offset);
299 /* leave remaining channel buffers alone */
303 PluginInsert::silence (nframes_t nframes, nframes_t offset)
305 uint32_t in_index = 0;
306 uint32_t out_index = 0;
309 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
310 (*i)->connect_and_run (_session.get_silent_buffers ((*i)->get_info()->n_inputs), in_index, out_index, nframes, offset);
316 PluginInsert::run_in_place (BufferSet& bufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset)
320 if (_session.transport_rolling()) {
321 automation_run (bufs, nframes, offset);
323 connect_and_run (bufs, nframes, offset, false);
327 /* FIXME: type, audio only */
329 uint32_t in = _plugins[0]->get_info()->n_inputs.n_audio();
330 uint32_t out = _plugins[0]->get_info()->n_outputs.n_audio();
334 /* not active, but something has make up for any channel count increase */
336 for (uint32_t n = out - in; n < out; ++n) {
337 memcpy (bufs.get_audio(n).data(nframes, offset), bufs.get_audio(in - 1).data(nframes, offset), sizeof (Sample) * nframes);
341 bufs.count().set_audio(out);
346 PluginInsert::set_parameter (Parameter param, float val)
348 if (param.type() != PluginAutomation)
351 /* the others will be set from the event triggered by this */
353 _plugins[0]->set_parameter (param.id(), val);
355 boost::shared_ptr<AutomationControl> c = control (param);
360 _session.set_dirty();
364 PluginInsert::get_parameter (Parameter param)
366 if (param.type() != PluginAutomation)
370 _plugins[0]->get_parameter (param.id());
374 PluginInsert::automation_run (BufferSet& bufs, nframes_t nframes, nframes_t offset)
376 ControlEvent next_event (0, 0.0f);
377 nframes_t now = _session.transport_frame ();
378 nframes_t end = now + nframes;
380 Glib::Mutex::Lock lm (_automation_lock, Glib::TRY_LOCK);
383 connect_and_run (bufs, nframes, offset, false);
387 if (!find_next_event (now, end, next_event)) {
389 /* no events have a time within the relevant range */
391 connect_and_run (bufs, nframes, offset, true, now);
397 nframes_t cnt = min (((nframes_t) ceil (next_event.when) - now), nframes);
399 connect_and_run (bufs, cnt, offset, true, now);
405 if (!find_next_event (now, end, next_event)) {
410 /* cleanup anything that is left to do */
413 connect_and_run (bufs, nframes, offset, true, now);
418 PluginInsert::default_parameter_value (Parameter param)
420 if (param.type() != PluginAutomation)
423 if (_plugins.empty()) {
424 fatal << _("programming error: ") << X_("PluginInsert::default_parameter_value() called with no plugin")
429 return _plugins[0]->default_value (param.id());
432 boost::shared_ptr<Plugin>
433 PluginInsert::plugin_factory (boost::shared_ptr<Plugin> other)
435 boost::shared_ptr<LadspaPlugin> lp;
437 boost::shared_ptr<VSTPlugin> vp;
439 #ifdef HAVE_AUDIOUNITS
440 boost::shared_ptr<AUPlugin> ap;
443 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
444 return boost::shared_ptr<Plugin> (new LadspaPlugin (*lp));
446 } else if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (other)) != 0) {
447 return boost::shared_ptr<Plugin> (new VSTPlugin (*vp));
449 #ifdef HAVE_AUDIOUNITS
450 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
451 return boost::shared_ptr<Plugin> (new AUPlugin (*ap));
455 fatal << string_compose (_("programming error: %1"),
456 X_("unknown plugin type in PluginInsert::plugin_factory"))
459 return boost::shared_ptr<Plugin> ((Plugin*) 0);
463 PluginInsert::configure_io (ChanCount in, ChanCount out)
465 ChanCount matching_out = output_for_input_configuration(out);
466 if (matching_out != out) {
470 bool success = set_count (count_for_configuration(in, out));
472 Processor::configure_io(in, out);
478 PluginInsert::can_support_input_configuration (ChanCount in) const
480 ChanCount outputs = _plugins[0]->get_info()->n_outputs;
481 ChanCount inputs = _plugins[0]->get_info()->n_inputs;
483 /* see output_for_input_configuration below */
484 if ((inputs.n_total() == 0)
485 || (inputs.n_total() == 1 && outputs == inputs)
486 || (inputs.n_total() == 1 && outputs == inputs
487 && ((inputs.n_audio() == 0 && in.n_audio() == 0)
488 || (inputs.n_midi() == 0 && in.n_midi() == 0)))
493 bool can_replicate = true;
495 /* if number of inputs is a factor of the requested input
496 configuration for every type, we can replicate.
498 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
499 if (inputs.get(*t) >= in.get(*t) || (inputs.get(*t) % in.get(*t) != 0)) {
500 can_replicate = false;
505 if (can_replicate && (in.n_total() % inputs.n_total() == 0)) {
513 PluginInsert::output_for_input_configuration (ChanCount in) const
515 ChanCount outputs = _plugins[0]->get_info()->n_outputs;
516 ChanCount inputs = _plugins[0]->get_info()->n_inputs;
518 if (inputs.n_total() == 0) {
519 /* instrument plugin, always legal, but throws away any existing streams */
523 if (inputs.n_total() == 1 && outputs == inputs
524 && ((inputs.n_audio() == 0 && in.n_audio() == 0)
525 || (inputs.n_midi() == 0 && in.n_midi() == 0))) {
526 /* mono plugin, replicate as needed to match in */
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) || (in.get(*t) % inputs.get(*t) != 0)) {
542 can_replicate = false;
547 if (can_replicate && (inputs.n_total() % in.n_total() == 0)) {
550 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
551 output.set(*t, outputs.get(*t) * (in.get(*t) / inputs.get(*t)));
561 /* Number of plugin instances required to support a given channel configuration.
565 PluginInsert::count_for_configuration (ChanCount in, ChanCount out) const
567 // FIXME: take 'out' into consideration
569 ChanCount outputs = _plugins[0]->get_info()->n_outputs;
570 ChanCount inputs = _plugins[0]->get_info()->n_inputs;
572 if (inputs.n_total() == 0) {
573 /* instrument plugin, always legal, but throws away any existing streams */
577 if (inputs.n_total() == 1 && outputs == inputs
578 && ((inputs.n_audio() == 0 && in.n_audio() == 0)
579 || (inputs.n_midi() == 0 && in.n_midi() == 0))) {
580 /* mono plugin, replicate as needed to match in */
589 // assumes in is valid, so we must be replicating
590 if (inputs.n_total() < in.n_total()
591 && (in.n_total() % inputs.n_total() == 0)) {
593 return in.n_total() / inputs.n_total();
601 PluginInsert::get_state(void)
607 PluginInsert::state (bool full)
610 XMLNode& node = Processor::state (full);
612 node.add_property ("type", _plugins[0]->state_node_name());
613 snprintf(buf, sizeof(buf), "%s", _plugins[0]->name());
614 node.add_property("id", string(buf));
615 if (_plugins[0]->state_node_name() == "ladspa") {
617 snprintf (buf, sizeof (buf), "%ld", _plugins[0]->get_info()->unique_id);
618 node.add_property("unique-id", string(buf));
620 node.add_property("count", string_compose("%1", _plugins.size()));
621 node.add_child_nocopy (_plugins[0]->get_state());
623 /* add port automation state */
624 XMLNode *autonode = new XMLNode(port_automation_node_name);
625 set<Parameter> automatable = _plugins[0]->automatable();
627 for (set<Parameter>::iterator x = automatable.begin(); x != automatable.end(); ++x) {
629 /*XMLNode* child = new XMLNode("port");
630 snprintf(buf, sizeof(buf), "%" PRIu32, *x);
631 child->add_property("number", string(buf));
633 child->add_child_nocopy (automation_list (*x).state (full));
634 autonode->add_child_nocopy (*child);
636 autonode->add_child_nocopy (control(*x)->list()->state (full));
639 node.add_child_nocopy (*autonode);
645 PluginInsert::set_state(const XMLNode& node)
647 XMLNodeList nlist = node.children();
648 XMLNodeIterator niter;
649 XMLPropertyList plist;
650 const XMLProperty *prop;
652 ARDOUR::PluginType type;
654 if ((prop = node.property ("type")) == 0) {
655 error << _("XML node describing insert is missing the `type' field") << endmsg;
659 if (prop->value() == X_("ladspa") || prop->value() == X_("Ladspa")) { /* handle old school sessions */
660 type = ARDOUR::LADSPA;
661 } else if (prop->value() == X_("vst")) {
664 error << string_compose (_("unknown plugin type %1 in plugin insert state"),
670 prop = node.property ("unique-id");
672 unique = atol(prop->value().c_str());
675 if ((prop = node.property ("id")) == 0) {
676 error << _("XML node describing insert is missing the `id' field") << endmsg;
680 boost::shared_ptr<Plugin> plugin;
683 plugin = find_plugin (_session, "", unique, type);
685 plugin = find_plugin (_session, prop->value(), 0, type);
689 error << string_compose(_("Found a reference to a plugin (\"%1\") that is unknown.\n"
690 "Perhaps it was removed or moved since it was last used."), prop->value())
697 if ((prop = node.property ("count")) != 0) {
698 sscanf (prop->value().c_str(), "%u", &count);
701 if (_plugins.size() != count) {
703 _plugins.push_back (plugin);
705 for (uint32_t n=1; n < count; ++n) {
706 _plugins.push_back (plugin_factory (plugin));
710 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
711 if ((*niter)->name() == plugin->state_node_name()) {
712 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
713 (*i)->set_state (**niter);
719 const XMLNode* insert_node = &node;
721 // legacy sessions: search for child IOProcessor node
722 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
723 if ((*niter)->name() == "IOProcessor") {
724 insert_node = *niter;
729 Processor::set_state (*insert_node);
731 /* look for port automation node */
733 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
735 if ((*niter)->name() != port_automation_node_name) {
741 XMLNodeConstIterator iter;
746 cnodes = (*niter)->children ("port");
748 for(iter = cnodes.begin(); iter != cnodes.end(); ++iter){
752 if ((cprop = child->property("number")) != 0) {
753 port = cprop->value().c_str();
755 warning << _("PluginInsert: Auto: no ladspa port number") << endmsg;
759 sscanf (port, "%" PRIu32, &port_id);
761 if (port_id >= _plugins[0]->parameter_count()) {
762 warning << _("PluginInsert: Auto: port id out of range") << endmsg;
766 if (!child->children().empty()) {
767 control (Parameter(PluginAutomation, port_id), true)->list()->set_state (*child->children().front());
769 if ((cprop = child->property("auto")) != 0) {
774 sscanf (cprop->value().c_str(), "0x%x", &x);
775 control (Parameter(PluginAutomation, port_id), true)->list()->set_automation_state (AutoState (x));
781 control (Parameter(PluginAutomation, port_id), true)->list()->set_automation_state (Off);
792 if (niter == nlist.end()) {
793 warning << string_compose(_("XML node describing a port automation is missing the `%1' information"), port_automation_node_name) << endmsg;
796 // The name of the PluginInsert comes from the plugin, nothing else
797 _name = plugin->get_info()->name;
803 PluginInsert::describe_parameter (Parameter param)
805 if (param.type() != PluginAutomation)
806 return Automatable::describe_parameter(param);
808 return _plugins[0]->describe_parameter (param);
812 PluginInsert::signal_latency() const
815 return _user_latency;
818 return _plugins[0]->signal_latency ();
822 PluginInsert::type ()
824 boost::shared_ptr<LadspaPlugin> lp;
826 boost::shared_ptr<VSTPlugin> vp;
828 #ifdef HAVE_AUDIOUNITS
829 boost::shared_ptr<AUPlugin> ap;
832 PluginPtr other = plugin ();
834 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
835 return ARDOUR::LADSPA;
837 } else if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (other)) != 0) {
840 #ifdef HAVE_AUDIOUNITS
841 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
842 return ARDOUR::AudioUnit;
846 return (ARDOUR::PluginType) 0;
850 PluginInsert::PluginControl::PluginControl (PluginInsert& p, boost::shared_ptr<AutomationList> list)
851 : AutomationControl (p.session(), list, p.describe_parameter(list->parameter()))
855 Plugin::ParameterDescriptor desc;
856 p.plugin(0)->get_parameter_descriptor (list->parameter().id(), desc);
857 _logarithmic = desc.logarithmic;
858 _toggled = desc.toggled;
862 PluginInsert::PluginControl::set_value (float val)
864 /* FIXME: probably should be taking out some lock here.. */
874 /*const float range = _list->get_max_y() - _list->get_min_y();
875 const float lower = _list->get_min_y();
878 val = lower + (range * val);
880 float log_lower = 0.0f;
882 log_lower = log(lower);
885 val = exp(log_lower + log(range) * val);
890 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugin._plugins.begin();
891 i != _plugin._plugins.end(); ++i) {
892 (*i)->set_parameter (_list->parameter().id(), val);
895 AutomationControl::set_value(val);
899 PluginInsert::PluginControl::get_value (void) const
901 /* FIXME: probably should be taking out some lock here.. */
903 float val = _plugin.get_parameter (_list->parameter());
917 return ((val - lower) / range);