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>
35 #include <ardour/vst_plugin.h>
38 #ifdef HAVE_AUDIOUNITS
39 #include <ardour/audio_unit.h>
42 #include <ardour/audioengine.h>
43 #include <ardour/session.h>
44 #include <ardour/types.h>
49 using namespace ARDOUR;
52 const string PluginInsert::port_automation_node_name = "PortAutomation";
54 PluginInsert::PluginInsert (Session& s, boost::shared_ptr<Plugin> plug, Placement placement)
55 : Insert (s, plug->name(), placement)
57 /* the first is the master */
59 _plugins.push_back (plug);
61 _plugins[0]->ParameterChanged.connect (mem_fun (*this, &PluginInsert::parameter_changed));
66 Glib::Mutex::Lock em (_session.engine().process_lock());
67 IO::MoreChannels (max(input_streams(), output_streams()));
70 RedirectCreated (this); /* EMIT SIGNAL */
73 PluginInsert::PluginInsert (Session& s, const XMLNode& node)
74 : Insert (s, "will change", PreFader)
76 if (set_state (node)) {
77 throw failed_constructor();
82 _plugins[0]->ParameterChanged.connect (mem_fun (*this, &PluginInsert::parameter_changed));
85 Glib::Mutex::Lock em (_session.engine().process_lock());
86 IO::MoreChannels (max(input_streams(), output_streams()));
90 PluginInsert::PluginInsert (const PluginInsert& other)
91 : Insert (other._session, other.plugin()->name(), other.placement())
93 uint32_t count = other._plugins.size();
95 /* make as many copies as requested */
96 for (uint32_t n = 0; n < count; ++n) {
97 _plugins.push_back (plugin_factory (other.plugin (n)));
101 _plugins[0]->ParameterChanged.connect (mem_fun (*this, &PluginInsert::parameter_changed));
105 RedirectCreated (this); /* EMIT SIGNAL */
109 PluginInsert::set_count (uint32_t num)
111 bool require_state = !_plugins.empty();
113 /* this is a bad idea.... we shouldn't do this while active.
114 only a route holding their redirect_lock should be calling this
119 } else if (num > _plugins.size()) {
120 uint32_t diff = num - _plugins.size();
122 for (uint32_t n = 0; n < diff; ++n) {
123 _plugins.push_back (plugin_factory (_plugins[0]));
126 /* XXX do something */
130 } else if (num < _plugins.size()) {
131 uint32_t diff = _plugins.size() - num;
132 for (uint32_t n= 0; n < diff; ++n) {
141 PluginInsert::init ()
145 set<uint32_t>::iterator s;
148 PluginInsert::~PluginInsert ()
150 GoingAway (); /* EMIT SIGNAL */
154 PluginInsert::automation_list_creation_callback (uint32_t which, AutomationList& alist)
156 alist.automation_state_changed.connect (sigc::bind (mem_fun (*this, &PluginInsert::auto_state_changed), (which)));
160 PluginInsert::auto_state_changed (uint32_t which)
162 AutomationList& alist (automation_list (which));
164 if (alist.automation_state() != Off) {
165 _plugins[0]->set_parameter (which, alist.eval (_session.transport_frame()));
170 PluginInsert::output_streams() const
173 return output_for_input_configuration(_configured_input);
175 return natural_output_streams();
179 PluginInsert::input_streams() const
182 return _configured_input;
184 return natural_input_streams();
188 PluginInsert::natural_output_streams() const
190 return _plugins[0]->get_info()->n_outputs;
194 PluginInsert::natural_input_streams() const
196 return _plugins[0]->get_info()->n_inputs;
200 PluginInsert::is_generator() const
202 /* XXX more finesse is possible here. VST plugins have a
203 a specific "instrument" flag, for example.
206 return _plugins[0]->get_info()->n_inputs.n_audio() == 0;
210 PluginInsert::set_automatable ()
214 a = _plugins.front()->automatable ();
216 for (set<uint32_t>::iterator i = a.begin(); i != a.end(); ++i) {
222 PluginInsert::parameter_changed (uint32_t which, float val)
224 vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin();
226 /* don't set the first plugin, just all the slaves */
228 if (i != _plugins.end()) {
230 for (; i != _plugins.end(); ++i) {
231 (*i)->set_parameter (which, val);
237 PluginInsert::set_block_size (nframes_t nframes)
239 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
240 (*i)->set_block_size (nframes);
245 PluginInsert::activate ()
247 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
253 PluginInsert::deactivate ()
255 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
261 PluginInsert::connect_and_run (BufferSet& bufs, nframes_t nframes, nframes_t offset, bool with_auto, nframes_t now)
263 uint32_t in_index = 0;
264 uint32_t out_index = 0;
266 /* Note that we've already required that plugins
267 be able to handle in-place processing.
272 map<uint32_t,AutomationList*>::iterator li;
275 for (n = 0, li = parameter_automation.begin(); li != parameter_automation.end(); ++li, ++n) {
277 AutomationList& alist (*((*li).second));
279 if (alist.automation_playback()) {
282 float val = alist.rt_safe_eval (now, valid);
285 /* set the first plugin, the others will be set via signals */
286 _plugins[0]->set_parameter ((*li).first, val);
293 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
294 (*i)->connect_and_run (bufs, in_index, out_index, nframes, offset);
297 /* leave remaining channel buffers alone */
301 PluginInsert::automation_snapshot (nframes_t now)
303 map<uint32_t,AutomationList*>::iterator li;
305 for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
307 AutomationList *alist = ((*li).second);
308 if (alist != 0 && alist->automation_write ()) {
310 float val = _plugins[0]->get_parameter ((*li).first);
311 alist->rt_add (now, val);
312 last_automation_snapshot = now;
318 PluginInsert::transport_stopped (nframes_t now)
320 map<uint32_t,AutomationList*>::iterator li;
322 for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
323 AutomationList& alist (*(li->second));
324 alist.reposition_for_rt_add (now);
326 if (alist.automation_state() != Off) {
327 _plugins[0]->set_parameter (li->first, alist.eval (now));
333 PluginInsert::silence (nframes_t nframes, nframes_t offset)
335 uint32_t in_index = 0;
336 uint32_t out_index = 0;
339 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
340 (*i)->connect_and_run (_session.get_silent_buffers ((*i)->get_info()->n_inputs), in_index, out_index, nframes, offset);
346 PluginInsert::run (BufferSet& bufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset)
350 if (_session.transport_rolling()) {
351 automation_run (bufs, nframes, offset);
353 connect_and_run (bufs, nframes, offset, false);
356 uint32_t in = _plugins[0]->get_info()->n_inputs.n_audio();
357 uint32_t out = _plugins[0]->get_info()->n_outputs.n_audio();
361 /* not active, but something has make up for any channel count increase */
363 for (uint32_t n = out - in; n < out; ++n) {
364 memcpy (bufs.get_audio(n).data(nframes, offset), bufs.get_audio(in - 1).data(nframes, offset), sizeof (Sample) * nframes);
368 bufs.count().set(_default_type, out);
373 PluginInsert::set_parameter (uint32_t port, float val)
375 /* the others will be set from the event triggered by this */
377 _plugins[0]->set_parameter (port, val);
379 if (automation_list (port).automation_write()) {
380 automation_list (port).add (_session.audible_frame(), val);
383 _session.set_dirty();
387 PluginInsert::automation_run (BufferSet& bufs, nframes_t nframes, nframes_t offset)
389 ControlEvent next_event (0, 0.0f);
390 nframes_t now = _session.transport_frame ();
391 nframes_t end = now + nframes;
393 Glib::Mutex::Lock lm (_automation_lock, Glib::TRY_LOCK);
396 connect_and_run (bufs, nframes, offset, false);
400 if (!find_next_event (now, end, next_event)) {
402 /* no events have a time within the relevant range */
404 connect_and_run (bufs, nframes, offset, true, now);
410 nframes_t cnt = min (((nframes_t) ceil (next_event.when) - now), nframes);
412 connect_and_run (bufs, cnt, offset, true, now);
418 if (!find_next_event (now, end, next_event)) {
423 /* cleanup anything that is left to do */
426 connect_and_run (bufs, nframes, offset, true, now);
431 PluginInsert::default_parameter_value (uint32_t port)
433 if (_plugins.empty()) {
434 fatal << _("programming error: ") << X_("PluginInsert::default_parameter_value() called with no plugin")
439 return _plugins[0]->default_value (port);
443 PluginInsert::set_port_automation_state (uint32_t port, AutoState s)
445 if (port < _plugins[0]->parameter_count()) {
447 AutomationList& al = automation_list (port);
449 if (s != al.automation_state()) {
450 al.set_automation_state (s);
451 _session.set_dirty ();
457 PluginInsert::get_port_automation_state (uint32_t port)
459 if (port < _plugins[0]->parameter_count()) {
460 return automation_list (port).automation_state();
467 PluginInsert::protect_automation ()
469 set<uint32_t> automated_params;
471 what_has_automation (automated_params);
473 for (set<uint32_t>::iterator i = automated_params.begin(); i != automated_params.end(); ++i) {
475 AutomationList& al = automation_list (*i);
477 switch (al.automation_state()) {
479 al.set_automation_state (Off);
482 al.set_automation_state (Play);
490 boost::shared_ptr<Plugin>
491 PluginInsert::plugin_factory (boost::shared_ptr<Plugin> other)
493 boost::shared_ptr<LadspaPlugin> lp;
495 boost::shared_ptr<VSTPlugin> vp;
497 #ifdef HAVE_AUDIOUNITS
498 boost::shared_ptr<AUPlugin> ap;
501 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
502 return boost::shared_ptr<Plugin> (new LadspaPlugin (*lp));
504 } else if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (other)) != 0) {
505 return boost::shared_ptr<Plugin> (new VSTPlugin (*vp));
507 #ifdef HAVE_AUDIOUNITS
508 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
509 return boost::shared_ptr<Plugin> (new AUPlugin (*ap));
513 fatal << string_compose (_("programming error: %1"),
514 X_("unknown plugin type in PluginInsert::plugin_factory"))
517 return boost::shared_ptr<Plugin> ((Plugin*) 0);
521 PluginInsert::configure_io (ChanCount in, ChanCount out)
523 ChanCount matching_out = output_for_input_configuration(out);
524 if (matching_out != out) {
528 bool success = set_count (count_for_configuration(in, out));
531 _configured_input = in;
538 PluginInsert::can_support_input_configuration (ChanCount in_count) const
540 int32_t outputs = _plugins[0]->get_info()->n_outputs.get(_default_type);
541 int32_t inputs = _plugins[0]->get_info()->n_inputs.get(_default_type);
542 int32_t in = in_count.get(_default_type);
544 /* see output_for_input_configuration below */
546 || (outputs == 1 && inputs == 1)
548 || ((inputs < in) && (inputs % in == 0))) {
556 PluginInsert::output_for_input_configuration (ChanCount in) const
558 ChanCount outputs = _plugins[0]->get_info()->n_outputs;
559 ChanCount inputs = _plugins[0]->get_info()->n_inputs;
561 if (inputs.n_total() == 0) {
562 /* instrument plugin, always legal, but throws away any existing streams */
566 if (inputs.n_total() == 1 && outputs == inputs) {
567 /* mono plugin, replicate as needed to match in */
576 // FIXME: single type plugins only. can we do this for instruments?
577 if ((inputs.n_total() == inputs.get(_default_type))
578 && ((in.n_total() == in.get(_default_type))
579 && (inputs.n_total() < in.n_total())
580 && (inputs.n_total() % in.n_total() == 0))) {
582 /* number of inputs is a factor of the requested input
583 configuration, so we can replicate.
586 return ChanCount(_default_type, in.n_total() / inputs.n_total());
593 /* Number of plugin instances required to support a given channel configuration.
597 PluginInsert::count_for_configuration (ChanCount in, ChanCount out) const
599 // FIXME: take 'out' into consideration
601 ChanCount outputs = _plugins[0]->get_info()->n_outputs;
602 ChanCount inputs = _plugins[0]->get_info()->n_inputs;
604 if (inputs.n_total() == 0) {
605 /* instrument plugin, always legal, but throws away any existing streams */
609 if (inputs.n_total() == 1 && outputs == inputs) {
610 /* mono plugin, replicate as needed to match in */
619 // FIXME: single type plugins only. can we do this for instruments?
620 if ((inputs.n_total() == inputs.get(_default_type))
621 && ((in.n_total() == in.get(_default_type))
622 && (inputs.n_total() < in.n_total())
623 && (inputs.n_total() % in.n_total() == 0))) {
625 /* number of inputs is a factor of the requested input
626 configuration, so we can replicate.
629 return in.n_total() / inputs.n_total();
637 PluginInsert::get_state(void)
643 PluginInsert::state (bool full)
646 XMLNode *node = new XMLNode("Insert");
648 node->add_child_nocopy (Redirect::state (full));
650 node->add_property ("type", _plugins[0]->state_node_name());
651 snprintf(buf, sizeof(buf), "%s", _plugins[0]->name());
652 node->add_property("id", string(buf));
653 if (_plugins[0]->state_node_name() == "ladspa") {
655 snprintf (buf, sizeof (buf), "%ld", _plugins[0]->get_info()->unique_id);
656 node->add_property("unique-id", string(buf));
658 node->add_property("count", string_compose("%1", _plugins.size()));
659 node->add_child_nocopy (_plugins[0]->get_state());
661 /* add port automation state */
662 XMLNode *autonode = new XMLNode(port_automation_node_name);
663 set<uint32_t> automatable = _plugins[0]->automatable();
665 for (set<uint32_t>::iterator x = automatable.begin(); x != automatable.end(); ++x) {
667 XMLNode* child = new XMLNode("port");
668 snprintf(buf, sizeof(buf), "%" PRIu32, *x);
669 child->add_property("number", string(buf));
671 child->add_child_nocopy (automation_list (*x).state (full));
672 autonode->add_child_nocopy (*child);
675 node->add_child_nocopy (*autonode);
681 PluginInsert::set_state(const XMLNode& node)
683 XMLNodeList nlist = node.children();
684 XMLNodeIterator niter;
685 XMLPropertyList plist;
686 const XMLProperty *prop;
688 ARDOUR::PluginType type;
690 if ((prop = node.property ("type")) == 0) {
691 error << _("XML node describing insert is missing the `type' field") << endmsg;
695 if (prop->value() == X_("ladspa") || prop->value() == X_("Ladspa")) { /* handle old school sessions */
696 type = ARDOUR::LADSPA;
697 } else if (prop->value() == X_("vst")) {
700 error << string_compose (_("unknown plugin type %1 in plugin insert state"),
706 prop = node.property ("unique-id");
708 unique = atol(prop->value().c_str());
711 if ((prop = node.property ("id")) == 0) {
712 error << _("XML node describing insert is missing the `id' field") << endmsg;
716 boost::shared_ptr<Plugin> plugin;
719 plugin = find_plugin (_session, "", unique, type);
721 plugin = find_plugin (_session, prop->value(), 0, type);
725 error << string_compose(_("Found a reference to a plugin (\"%1\") that is unknown.\n"
726 "Perhaps it was removed or moved since it was last used."), prop->value())
733 if ((prop = node.property ("count")) != 0) {
734 sscanf (prop->value().c_str(), "%u", &count);
737 if (_plugins.size() != count) {
739 _plugins.push_back (plugin);
741 for (uint32_t n=1; n < count; ++n) {
742 _plugins.push_back (plugin_factory (plugin));
746 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
747 if ((*niter)->name() == plugin->state_node_name()) {
748 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
749 (*i)->set_state (**niter);
755 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
756 if ((*niter)->name() == Redirect::state_node_name) {
757 Redirect::set_state (**niter);
762 if (niter == nlist.end()) {
763 error << _("XML node describing insert is missing a Redirect node") << endmsg;
767 if (niter == nlist.end()) {
768 error << string_compose(_("XML node describing a plugin insert is missing the `%1' information"), plugin->state_node_name()) << endmsg;
772 /* look for port automation node */
774 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
776 if ((*niter)->name() != port_automation_node_name) {
782 XMLNodeConstIterator iter;
787 cnodes = (*niter)->children ("port");
789 for(iter = cnodes.begin(); iter != cnodes.end(); ++iter){
793 if ((cprop = child->property("number")) != 0) {
794 port = cprop->value().c_str();
796 warning << _("PluginInsert: Auto: no ladspa port number") << endmsg;
800 sscanf (port, "%" PRIu32, &port_id);
802 if (port_id >= _plugins[0]->parameter_count()) {
803 warning << _("PluginInsert: Auto: port id out of range") << endmsg;
807 if (!child->children().empty()) {
808 automation_list (port_id).set_state (*child->children().front());
810 if ((cprop = child->property("auto")) != 0) {
815 sscanf (cprop->value().c_str(), "0x%x", &x);
816 automation_list (port_id).set_automation_state (AutoState (x));
822 automation_list (port_id).set_automation_state (Off);
833 if (niter == nlist.end()) {
834 warning << string_compose(_("XML node describing a port automation is missing the `%1' information"), port_automation_node_name) << endmsg;
837 // The name of the PluginInsert comes from the plugin, nothing else
838 set_name(plugin->get_info()->name,this);
844 PluginInsert::describe_parameter (uint32_t what)
846 return _plugins[0]->describe_parameter (what);
850 PluginInsert::latency()
852 return _plugins[0]->latency ();
856 PluginInsert::type ()
858 boost::shared_ptr<LadspaPlugin> lp;
860 boost::shared_ptr<VSTPlugin> vp;
862 #ifdef HAVE_AUDIOUNITS
863 boost::shared_ptr<AUPlugin> ap;
866 PluginPtr other = plugin ();
868 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
869 return ARDOUR::LADSPA;
871 } else if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (other)) != 0) {
874 #ifdef HAVE_AUDIOUNITS
875 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
876 return ARDOUR::AudioUnit;
880 return (ARDOUR::PluginType) 0;