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.
23 #include <sigc++/bind.h>
25 #include <pbd/failed_constructor.h>
26 #include <pbd/xml++.h>
28 #include <ardour/insert.h>
29 #include <ardour/plugin.h>
30 #include <ardour/port.h>
31 #include <ardour/route.h>
32 #include <ardour/ladspa_plugin.h>
33 #include <ardour/vst_plugin.h>
34 #include <ardour/audioengine.h>
35 #include <ardour/session.h>
40 using namespace ARDOUR;
43 Insert::Insert(Session& s, Placement p)
44 : Redirect (s, s.next_insert_name(), p)
49 Insert::Insert(Session& s, Placement p, int imin, int imax, int omin, int omax)
50 : Redirect (s, s.next_insert_name(), p, imin, imax, omin, omax)
54 Insert::Insert(Session& s, string name, Placement p)
55 : Redirect (s, name, p)
59 /***************************************************************
60 Plugin inserts: send data through a plugin
61 ***************************************************************/
63 const string PluginInsert::port_automation_node_name = "PortAutomation";
65 PluginInsert::PluginInsert (Session& s, boost::shared_ptr<Plugin> plug, Placement placement)
66 : Insert (s, plug->name(), placement)
68 /* the first is the master */
70 _plugins.push_back (plug);
72 _plugins[0]->ParameterChanged.connect (mem_fun (*this, &PluginInsert::parameter_changed));
76 save_state (_("initial state"));
79 Glib::Mutex::Lock em (_session.engine().process_lock());
80 IO::MoreOutputs (output_streams ());
83 RedirectCreated (this); /* EMIT SIGNAL */
86 PluginInsert::PluginInsert (Session& s, const XMLNode& node)
87 : Insert (s, "will change", PreFader)
89 if (set_state (node)) {
90 throw failed_constructor();
95 save_state (_("initial state"));
97 _plugins[0]->ParameterChanged.connect (mem_fun (*this, &PluginInsert::parameter_changed));
100 Glib::Mutex::Lock em (_session.engine().process_lock());
101 IO::MoreOutputs (output_streams());
105 PluginInsert::PluginInsert (const PluginInsert& other)
106 : Insert (other._session, other.plugin()->name(), other.placement())
108 uint32_t count = other._plugins.size();
110 /* make as many copies as requested */
111 for (uint32_t n = 0; n < count; ++n) {
112 _plugins.push_back (plugin_factory (other.plugin (n)));
116 _plugins[0]->ParameterChanged.connect (mem_fun (*this, &PluginInsert::parameter_changed));
120 save_state (_("initial state"));
122 RedirectCreated (this); /* EMIT SIGNAL */
126 PluginInsert::set_count (uint32_t num)
128 bool require_state = !_plugins.empty();
130 /* this is a bad idea.... we shouldn't do this while active.
131 only a route holding their redirect_lock should be calling this
136 } else if (num > _plugins.size()) {
137 uint32_t diff = num - _plugins.size();
139 for (uint32_t n = 0; n < diff; ++n) {
140 _plugins.push_back (plugin_factory (_plugins[0]));
143 /* XXX do something */
147 } else if (num < _plugins.size()) {
148 uint32_t diff = _plugins.size() - num;
149 for (uint32_t n= 0; n < diff; ++n) {
158 PluginInsert::init ()
162 set<uint32_t>::iterator s;
165 PluginInsert::~PluginInsert ()
167 GoingAway (this); /* EMIT SIGNAL */
171 PluginInsert::automation_list_creation_callback (uint32_t which, AutomationList& alist)
173 alist.automation_state_changed.connect (sigc::bind (mem_fun (*this, &PluginInsert::auto_state_changed), (which)));
177 PluginInsert::auto_state_changed (uint32_t which)
179 AutomationList& alist (automation_list (which));
181 if (alist.automation_state() != Off) {
182 _plugins[0]->set_parameter (which, alist.eval (_session.transport_frame()));
187 PluginInsert::output_streams() const
189 return _plugins[0]->get_info()->n_outputs * _plugins.size();
193 PluginInsert::input_streams() const
195 return _plugins[0]->get_info()->n_inputs * _plugins.size();
199 PluginInsert::natural_output_streams() const
201 return _plugins[0]->get_info()->n_outputs;
205 PluginInsert::natural_input_streams() const
207 return _plugins[0]->get_info()->n_inputs;
211 PluginInsert::is_generator() const
213 /* XXX more finesse is possible here. VST plugins have a
214 a specific "instrument" flag, for example.
217 return _plugins[0]->get_info()->n_inputs == 0;
221 PluginInsert::set_automatable ()
225 a = _plugins.front()->automatable ();
227 for (set<uint32_t>::iterator i = a.begin(); i != a.end(); ++i) {
233 PluginInsert::parameter_changed (uint32_t which, float val)
235 vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin();
237 /* don't set the first plugin, just all the slaves */
239 if (i != _plugins.end()) {
241 for (; i != _plugins.end(); ++i) {
242 (*i)->set_parameter (which, val);
248 PluginInsert::set_block_size (jack_nframes_t nframes)
250 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
251 (*i)->set_block_size (nframes);
256 PluginInsert::activate ()
258 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
264 PluginInsert::deactivate ()
266 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
272 PluginInsert::connect_and_run (vector<Sample*>& bufs, uint32_t nbufs, jack_nframes_t nframes, jack_nframes_t offset, bool with_auto, jack_nframes_t now)
274 int32_t in_index = 0;
275 int32_t out_index = 0;
277 /* Note that we've already required that plugins
278 be able to handle in-place processing.
283 map<uint32_t,AutomationList*>::iterator li;
286 for (n = 0, li = parameter_automation.begin(); li != parameter_automation.end(); ++li, ++n) {
288 AutomationList& alist (*((*li).second));
290 if (alist.automation_playback()) {
293 float val = alist.rt_safe_eval (now, valid);
296 /* set the first plugin, the others will be set via signals */
297 _plugins[0]->set_parameter ((*li).first, val);
304 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
305 (*i)->connect_and_run (bufs, nbufs, in_index, out_index, nframes, offset);
308 /* leave remaining channel buffers alone */
312 PluginInsert::automation_snapshot (jack_nframes_t now)
314 map<uint32_t,AutomationList*>::iterator li;
316 for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
318 AutomationList *alist = ((*li).second);
319 if (alist != 0 && alist->automation_write ()) {
321 float val = _plugins[0]->get_parameter ((*li).first);
322 alist->rt_add (now, val);
323 last_automation_snapshot = now;
329 PluginInsert::transport_stopped (jack_nframes_t now)
331 map<uint32_t,AutomationList*>::iterator li;
333 for (li = parameter_automation.begin(); li != parameter_automation.end(); ++li) {
334 AutomationList& alist (*(li->second));
335 alist.reposition_for_rt_add (now);
337 if (alist.automation_state() != Off) {
338 _plugins[0]->set_parameter (li->first, alist.eval (now));
344 PluginInsert::silence (jack_nframes_t nframes, jack_nframes_t offset)
346 int32_t in_index = 0;
347 int32_t out_index = 0;
352 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
353 n = (*i) -> get_info()->n_inputs;
354 (*i)->connect_and_run (_session.get_silent_buffers (n), n, in_index, out_index, nframes, offset);
360 PluginInsert::run (vector<Sample *>& bufs, uint32_t nbufs, jack_nframes_t nframes, jack_nframes_t offset)
364 if (_session.transport_rolling()) {
365 automation_run (bufs, nbufs, nframes, offset);
367 connect_and_run (bufs, nbufs, nframes, offset, false);
370 uint32_t in = _plugins[0]->get_info()->n_inputs;
371 uint32_t out = _plugins[0]->get_info()->n_outputs;
375 /* not active, but something has make up for any channel count increase */
377 for (uint32_t n = out - in; n < out; ++n) {
378 memcpy (bufs[n], bufs[in - 1], sizeof (Sample) * nframes);
385 PluginInsert::set_parameter (uint32_t port, float val)
387 /* the others will be set from the event triggered by this */
389 _plugins[0]->set_parameter (port, val);
391 if (automation_list (port).automation_write()) {
392 automation_list (port).add (_session.audible_frame(), val);
395 _session.set_dirty();
399 PluginInsert::automation_run (vector<Sample *>& bufs, uint32_t nbufs, jack_nframes_t nframes, jack_nframes_t offset)
401 ControlEvent next_event (0, 0.0f);
402 jack_nframes_t now = _session.transport_frame ();
403 jack_nframes_t end = now + nframes;
405 Glib::Mutex::Lock lm (_automation_lock, Glib::TRY_LOCK);
408 connect_and_run (bufs, nbufs, nframes, offset, false);
412 if (!find_next_event (now, end, next_event)) {
414 /* no events have a time within the relevant range */
416 connect_and_run (bufs, nbufs, nframes, offset, true, now);
422 jack_nframes_t cnt = min (((jack_nframes_t) floor (next_event.when) - now), nframes);
424 connect_and_run (bufs, nbufs, cnt, offset, true, now);
430 if (!find_next_event (now, end, next_event)) {
435 /* cleanup anything that is left to do */
438 connect_and_run (bufs, nbufs, nframes, offset, true, now);
443 PluginInsert::default_parameter_value (uint32_t port)
445 if (_plugins.empty()) {
446 fatal << _("programming error: ") << X_("PluginInsert::default_parameter_value() called with no plugin")
451 return _plugins[0]->default_value (port);
455 PluginInsert::set_port_automation_state (uint32_t port, AutoState s)
457 if (port < _plugins[0]->parameter_count()) {
459 AutomationList& al = automation_list (port);
461 if (s != al.automation_state()) {
462 al.set_automation_state (s);
463 last_automation_snapshot = 0;
464 _session.set_dirty ();
470 PluginInsert::get_port_automation_state (uint32_t port)
472 if (port < _plugins[0]->parameter_count()) {
473 return automation_list (port).automation_state();
480 PluginInsert::protect_automation ()
482 set<uint32_t> automated_params;
484 what_has_automation (automated_params);
486 for (set<uint32_t>::iterator i = automated_params.begin(); i != automated_params.end(); ++i) {
488 AutomationList& al = automation_list (*i);
490 switch (al.automation_state()) {
493 al.set_automation_state (Off);
501 boost::shared_ptr<Plugin>
502 PluginInsert::plugin_factory (boost::shared_ptr<Plugin> other)
504 boost::shared_ptr<LadspaPlugin> lp;
506 boost::shared_ptr<VSTPlugin> vp;
509 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
510 return boost::shared_ptr<Plugin> (new LadspaPlugin (*lp));
512 } else if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (other)) != 0) {
513 return boost::shared_ptr<Plugin> (new VSTPlugin (*vp));
517 fatal << string_compose (_("programming error: %1"),
518 X_("unknown plugin type in PluginInsert::plugin_factory"))
521 return boost::shared_ptr<Plugin> ((Plugin*) 0);
525 PluginInsert::compute_output_streams (int32_t cnt) const
527 return _plugins[0]->get_info()->n_outputs * cnt;
531 PluginInsert::configure_io (int32_t magic, int32_t in, int32_t out)
533 return set_count (magic);
537 PluginInsert::can_support_input_configuration (int32_t in) const
539 int32_t outputs = _plugins[0]->get_info()->n_outputs;
540 int32_t inputs = _plugins[0]->get_info()->n_inputs;
544 /* instrument plugin, always legal, but it throws
545 away any existing active streams.
551 if (outputs == 1 && inputs == 1) {
552 /* mono plugin, replicate as needed */
561 if ((inputs < in) && (inputs % in == 0)) {
563 /* number of inputs is a factor of the requested input
564 configuration, so we can replicate.
576 PluginInsert::get_state(void)
582 PluginInsert::state (bool full)
585 XMLNode *node = new XMLNode("Insert");
587 node->add_child_nocopy (Redirect::state (full));
589 node->add_property ("type", _plugins[0]->state_node_name());
590 snprintf(buf, sizeof(buf), "%s", _plugins[0]->name());
591 node->add_property("id", string(buf));
592 if (_plugins[0]->state_node_name() == "ladspa") {
594 snprintf (buf, sizeof (buf), "%ld", _plugins[0]->get_info()->unique_id);
595 node->add_property("unique-id", string(buf));
597 node->add_property("count", string_compose("%1", _plugins.size()));
598 node->add_child_nocopy (_plugins[0]->get_state());
600 /* add port automation state */
601 XMLNode *autonode = new XMLNode(port_automation_node_name);
602 set<uint32_t> automatable = _plugins[0]->automatable();
604 for (set<uint32_t>::iterator x = automatable.begin(); x != automatable.end(); ++x) {
606 XMLNode* child = new XMLNode("port");
607 snprintf(buf, sizeof(buf), "%" PRIu32, *x);
608 child->add_property("number", string(buf));
611 snprintf(buf, sizeof(buf), "0x%x", automation_list (*x).automation_state ());
613 snprintf(buf, sizeof(buf), "0x%x", ARDOUR::Off);
615 child->add_property("auto", string(buf));
617 autonode->add_child_nocopy (*child);
620 node->add_child_nocopy (*autonode);
626 PluginInsert::set_state(const XMLNode& node)
628 XMLNodeList nlist = node.children();
629 XMLNodeIterator niter;
630 XMLPropertyList plist;
631 const XMLProperty *prop;
633 PluginInfo::Type type;
635 if ((prop = node.property ("type")) == 0) {
636 error << _("XML node describing insert is missing the `type' field") << endmsg;
640 if (prop->value() == X_("ladspa") || prop->value() == X_("Ladspa")) { /* handle old school sessions */
641 type = PluginInfo::LADSPA;
642 } else if (prop->value() == X_("vst")) {
643 type = PluginInfo::VST;
645 error << string_compose (_("unknown plugin type %1 in plugin insert state"),
651 prop = node.property ("unique-id");
653 unique = atol(prop->value().c_str());
656 if ((prop = node.property ("id")) == 0) {
657 error << _("XML node describing insert is missing the `id' field") << endmsg;
661 boost::shared_ptr<Plugin> plugin;
664 plugin = find_plugin (_session, "", unique, type);
666 plugin = find_plugin (_session, prop->value(), 0, type);
670 error << string_compose(_("Found a reference to a plugin (\"%1\") that is unknown.\n"
671 "Perhaps it was removed or moved since it was last used."), prop->value())
678 if ((prop = node.property ("count")) != 0) {
679 sscanf (prop->value().c_str(), "%u", &count);
682 if (_plugins.size() != count) {
684 _plugins.push_back (plugin);
686 for (uint32_t n=1; n < count; ++n) {
687 _plugins.push_back (plugin_factory (plugin));
691 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
692 if ((*niter)->name() == plugin->state_node_name()) {
693 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
694 (*i)->set_state (**niter);
700 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
701 if ((*niter)->name() == Redirect::state_node_name) {
702 Redirect::set_state (**niter);
707 if (niter == nlist.end()) {
708 error << _("XML node describing insert is missing a Redirect node") << endmsg;
712 if (niter == nlist.end()) {
713 error << string_compose(_("XML node describing a plugin insert is missing the `%1' information"), plugin->state_node_name()) << endmsg;
717 /* look for port automation node */
719 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
720 if ((*niter)->name() == port_automation_node_name) {
723 XMLNodeConstIterator iter;
728 cnodes = (*niter)->children ("port");
730 for(iter = cnodes.begin(); iter != cnodes.end(); ++iter){
734 if ((cprop = child->property("number")) != 0) {
735 port = cprop->value().c_str();
737 warning << _("PluginInsert: Auto: no ladspa port number") << endmsg;
741 sscanf (port, "%" PRIu32, &port_id);
743 if (port_id >= _plugins[0]->parameter_count()) {
744 warning << _("PluginInsert: Auto: port id out of range") << endmsg;
748 if ((cprop = child->property("auto")) != 0) {
750 sscanf (cprop->value().c_str(), "0x%x", &x);
751 automation_list (port_id).set_automation_state (AutoState (x));
759 if (niter == nlist.end()) {
760 warning << string_compose(_("XML node describing a port automation is missing the `%1' information"), port_automation_node_name) << endmsg;
763 // The name of the PluginInsert comes from the plugin, nothing else
764 set_name(plugin->get_info()->name,this);
770 PluginInsert::describe_parameter (uint32_t what)
772 return _plugins[0]->describe_parameter (what);
776 PluginInsert::latency()
778 return _plugins[0]->latency ();
782 PluginInsert::store_state (PluginInsertState& state) const
784 Redirect::store_state (state);
785 _plugins[0]->store_state (state.plugin_state);
789 PluginInsert::restore_state (StateManager::State& state)
791 PluginInsertState* pistate = dynamic_cast<PluginInsertState*> (&state);
793 Redirect::restore_state (state);
795 _plugins[0]->restore_state (pistate->plugin_state);
801 PluginInsert::state_factory (std::string why) const
803 PluginInsertState* state = new PluginInsertState (why);
805 store_state (*state);
810 /***************************************************************
811 Port inserts: send output to a port, pick up input at a port
812 ***************************************************************/
814 PortInsert::PortInsert (Session& s, Placement p)
815 : Insert (s, p, 1, -1, 1, -1)
818 save_state (_("initial state"));
819 RedirectCreated (this); /* EMIT SIGNAL */
822 PortInsert::PortInsert (const PortInsert& other)
823 : Insert (other._session, other.placement(), 1, -1, 1, -1)
826 save_state (_("initial state"));
827 RedirectCreated (this); /* EMIT SIGNAL */
833 if (add_input_port ("", this)) {
834 error << _("PortInsert: cannot add input port") << endmsg;
835 throw failed_constructor();
838 if (add_output_port ("", this)) {
839 error << _("PortInsert: cannot add output port") << endmsg;
840 throw failed_constructor();
844 PortInsert::PortInsert (Session& s, const XMLNode& node)
845 : Insert (s, "will change", PreFader)
847 if (set_state (node)) {
848 throw failed_constructor();
851 RedirectCreated (this); /* EMIT SIGNAL */
854 PortInsert::~PortInsert ()
860 PortInsert::run (vector<Sample *>& bufs, uint32_t nbufs, jack_nframes_t nframes, jack_nframes_t offset)
862 if (n_outputs() == 0) {
867 /* deliver silence */
868 silence (nframes, offset);
873 vector<Port*>::iterator o;
874 vector<Port*>::iterator i;
878 for (o = _outputs.begin(), n = 0; o != _outputs.end(); ++o, ++n) {
879 memcpy ((*o)->get_buffer (nframes) + offset, bufs[min(nbufs,n)], sizeof (Sample) * nframes);
880 (*o)->mark_silence (false);
885 for (i = _inputs.begin(), n = 0; i != _inputs.end(); ++i, ++n) {
886 memcpy (bufs[min(nbufs,n)], (*i)->get_buffer (nframes) + offset, sizeof (Sample) * nframes);
891 PortInsert::get_state(void)
897 PortInsert::state (bool full)
899 XMLNode *node = new XMLNode("Insert");
901 node->add_child_nocopy (Redirect::state(full));
902 node->add_property("type", "port");
908 PortInsert::set_state(const XMLNode& node)
910 XMLNodeList nlist = node.children();
911 XMLNodeIterator niter;
912 XMLPropertyList plist;
913 const XMLProperty *prop;
915 if ((prop = node.property ("type")) == 0) {
916 error << _("XML node describing insert is missing the `type' field") << endmsg;
920 if (prop->value() != "port") {
921 error << _("non-port insert XML used for port plugin insert") << endmsg;
925 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
926 if ((*niter)->name() == Redirect::state_node_name) {
927 Redirect::set_state (**niter);
932 if (niter == nlist.end()) {
933 error << _("XML node describing insert is missing a Redirect node") << endmsg;
941 PortInsert::latency()
943 /* because we deliver and collect within the same cycle,
944 all I/O is necessarily delayed by at least frames_per_cycle().
946 if the return port for insert has its own latency, we
947 need to take that into account too.
950 return _session.engine().frames_per_cycle() + input_latency();
954 PortInsert::can_support_input_configuration (int32_t in) const
956 if (input_maximum() == -1 && output_maximum() == -1) {
958 /* not configured yet */
960 return 1; /* we can support anything the first time we're asked */
964 /* the "input" config for a port insert corresponds to how
965 many output ports it will have.
968 if (output_maximum() == in) {
977 PortInsert::configure_io (int32_t ignored_magic, int32_t in, int32_t out)
979 /* do not allow configuration to be changed outside the range of
980 the last request config. or something like that.
984 /* this is a bit odd:
986 the number of inputs we are required to handle corresponds
987 to the number of output ports we need.
989 the number of outputs we are required to have corresponds
990 to the number of input ports we need.
993 set_output_maximum (in);
994 set_output_minimum (in);
995 set_input_maximum (out);
996 set_input_minimum (out);
1006 return ensure_io (out, in, false, this);
1010 PortInsert::compute_output_streams (int32_t cnt) const
1012 /* puzzling, eh? think about it ... */
1017 PortInsert::output_streams() const
1023 PortInsert::input_streams() const
1025 return n_outputs ();