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.
21 #include "libardour-config.h"
26 #include "pbd/failed_constructor.h"
27 #include "pbd/xml++.h"
28 #include "pbd/convert.h"
30 #include "ardour/audio_buffer.h"
31 #include "ardour/automation_list.h"
32 #include "ardour/buffer_set.h"
33 #include "ardour/debug.h"
34 #include "ardour/event_type_map.h"
35 #include "ardour/ladspa_plugin.h"
36 #include "ardour/plugin.h"
37 #include "ardour/plugin_insert.h"
38 #include "ardour/port.h"
39 #include "ardour/route.h"
42 #include "ardour/lv2_plugin.h"
46 #include "ardour/vst_plugin.h"
50 #include "ardour/lxvst_plugin.h"
53 #ifdef AUDIOUNIT_SUPPORT
54 #include "ardour/audio_unit.h"
57 #include "ardour/audioengine.h"
58 #include "ardour/session.h"
59 #include "ardour/types.h"
64 using namespace ARDOUR;
67 const string PluginInsert::port_automation_node_name = "PortAutomation";
69 PluginInsert::PluginInsert (Session& s, boost::shared_ptr<Plugin> plug)
70 : Processor (s, (plug ? plug->name() : string ("toBeRenamed")))
71 , _signal_analysis_collected_nframes(0)
72 , _signal_analysis_collect_nframes_max(0)
74 /* the first is the master */
78 create_automatable_parameters ();
83 PluginInsert::set_count (uint32_t num)
85 bool require_state = !_plugins.empty();
87 /* this is a bad idea.... we shouldn't do this while active.
88 only a route holding their redirect_lock should be calling this
93 } else if (num > _plugins.size()) {
94 uint32_t diff = num - _plugins.size();
96 for (uint32_t n = 0; n < diff; ++n) {
97 add_plugin_with_activation (plugin_factory (_plugins[0]));
100 /* XXX do something */
104 } else if (num < _plugins.size()) {
105 uint32_t diff = _plugins.size() - num;
106 for (uint32_t n= 0; n < diff; ++n) {
114 PluginInsert::~PluginInsert ()
119 PluginInsert::control_list_automation_state_changed (Evoral::Parameter which, AutoState s)
121 if (which.type() != PluginAutomation)
124 boost::shared_ptr<AutomationControl> c
125 = boost::dynamic_pointer_cast<AutomationControl>(control (which));
128 _plugins[0]->set_parameter (which.id(), c->list()->eval (_session.transport_frame()));
133 PluginInsert::output_streams() const
135 assert (!_plugins.empty());
137 if (_plugins.front()->reconfigurable_io()) {
138 ChanCount out = _plugins.front()->output_streams ();
139 DEBUG_TRACE (DEBUG::Processors, string_compose ("Plugin insert, reconfigur(able) output streams = %1\n", out));
142 ChanCount out = _plugins.front()->get_info()->n_outputs;
143 DEBUG_TRACE (DEBUG::Processors, string_compose ("Plugin insert, static output streams = %1 for %2 plugins\n", out, _plugins.size()));
144 out.set_audio (out.n_audio() * _plugins.size());
145 out.set_midi (out.n_midi() * _plugins.size());
151 PluginInsert::input_streams() const
153 assert (!_plugins.empty());
157 if (_plugins.front()->reconfigurable_io()) {
158 assert (_plugins.size() == 1);
159 in = _plugins.front()->input_streams();
161 in = _plugins[0]->get_info()->n_inputs;
164 DEBUG_TRACE (DEBUG::Processors, string_compose ("Plugin insert, input streams = %1, match using %2\n", in, _match.method));
166 if (_match.method == Split) {
168 /* we are splitting 1 processor input to multiple plugin inputs,
169 so we have a maximum of 1 stream of each type.
171 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
172 if (in.get (*t) > 1) {
178 } else if (_match.method == Hide) {
180 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
181 in.set (*t, in.get (*t) - _match.hide.get (*t));
187 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
188 in.set (*t, in.get (*t) * _plugins.size ());
196 PluginInsert::natural_output_streams() const
198 return _plugins[0]->get_info()->n_outputs;
202 PluginInsert::natural_input_streams() const
204 return _plugins[0]->get_info()->n_inputs;
208 PluginInsert::has_no_inputs() const
210 return _plugins[0]->get_info()->n_inputs == ChanCount::ZERO;
214 PluginInsert::has_no_audio_inputs() const
216 return _plugins[0]->get_info()->n_inputs.n_audio() == 0;
220 PluginInsert::is_midi_instrument() const
222 /* XXX more finesse is possible here. VST plugins have a
223 a specific "instrument" flag, for example.
225 PluginInfoPtr pi = _plugins[0]->get_info();
227 return pi->n_inputs.n_midi() != 0 &&
228 pi->n_outputs.n_audio() > 0;
232 PluginInsert::create_automatable_parameters ()
234 assert (!_plugins.empty());
236 set<Evoral::Parameter> a = _plugins.front()->automatable ();
238 Plugin::ParameterDescriptor desc;
240 for (set<Evoral::Parameter>::iterator i = a.begin(); i != a.end(); ++i) {
241 if (i->type() == PluginAutomation) {
243 Evoral::Parameter param(*i);
245 _plugins.front()->get_parameter_descriptor(i->id(), desc);
247 /* the Parameter belonging to the actual plugin doesn't have its range set
248 but we want the Controllable related to this Parameter to have those limits.
251 param.set_range (desc.lower, desc.upper, _plugins.front()->default_value(i->id()), desc.toggled);
252 can_automate (param);
253 boost::shared_ptr<AutomationList> list(new AutomationList(param));
254 add_control (boost::shared_ptr<AutomationControl> (new PluginControl(this, param, list)));
260 PluginInsert::parameter_changed (Evoral::Parameter which, float val)
262 if (which.type() != PluginAutomation)
265 Plugins::iterator i = _plugins.begin();
267 /* don't set the first plugin, just all the slaves */
269 if (i != _plugins.end()) {
271 for (; i != _plugins.end(); ++i) {
272 (*i)->set_parameter (which, val);
278 PluginInsert::set_block_size (pframes_t nframes)
281 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
282 if ((*i)->set_block_size (nframes) != 0) {
290 PluginInsert::activate ()
292 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
296 Processor::activate ();
300 PluginInsert::deactivate ()
302 Processor::deactivate ();
304 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
310 PluginInsert::flush ()
312 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
318 PluginInsert::connect_and_run (BufferSet& bufs, pframes_t nframes, framecnt_t offset, bool with_auto, framepos_t now)
320 // Calculate if, and how many frames we need to collect for analysis
321 framecnt_t collect_signal_nframes = (_signal_analysis_collect_nframes_max -
322 _signal_analysis_collected_nframes);
323 if (nframes < collect_signal_nframes) { // we might not get all frames now
324 collect_signal_nframes = nframes;
327 ChanCount const in_streams = input_streams ();
328 ChanCount const out_streams = output_streams ();
330 ChanMapping in_map (in_streams);
331 ChanMapping out_map (out_streams);
333 if (_match.method == Split) {
334 /* fix the input mapping so that we have maps for each of the plugin's inputs */
335 in_map = ChanMapping (natural_input_streams ());
337 /* copy the first stream's buffer contents to the others */
338 /* XXX: audio only */
339 Sample const * mono = bufs.get_audio (in_map.get (DataType::AUDIO, 0, &valid)).data (offset);
340 for (uint32_t i = in_streams.n_audio(); i < natural_input_streams().n_audio(); ++i) {
341 memcpy (bufs.get_audio (in_map.get (DataType::AUDIO, i, &valid)).data (offset), mono, sizeof (Sample) * nframes);
345 if (_match.method == Hide) {
346 /* Silence the hidden input buffers */
347 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
348 for (uint32_t i = in_streams.get(*t); i < (in_streams.get(*t) + _match.hide.get(*t)); ++i) {
349 bufs.get(*t, i).silence (nframes);
354 /* Note that we've already required that plugins
355 be able to handle in-place processing.
362 for (Controls::iterator li = controls().begin(); li != controls().end(); ++li, ++n) {
364 boost::shared_ptr<AutomationControl> c
365 = boost::dynamic_pointer_cast<AutomationControl>(li->second);
367 if (c->parameter().type() == PluginAutomation && c->automation_playback()) {
370 const float val = c->list()->rt_safe_eval (now, valid);
380 if (collect_signal_nframes > 0) {
382 //std::cerr << "collect input, bufs " << bufs.count().n_audio() << " count, " << bufs.available().n_audio() << " available" << std::endl;
383 //std::cerr << " streams " << input_streams().n_audio() << std::endl;
384 //std::cerr << "filling buffer with " << collect_signal_nframes << " frames at " << _signal_analysis_collected_nframes << std::endl;
386 _signal_analysis_inputs.set_count(input_streams());
388 for (uint32_t i = 0; i < input_streams().n_audio(); ++i) {
389 _signal_analysis_inputs.get_audio(i).read_from(
391 collect_signal_nframes,
392 _signal_analysis_collected_nframes); // offset is for target buffer
397 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
398 (*i)->connect_and_run(bufs, in_map, out_map, nframes, offset);
399 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
400 in_map.offset_to(*t, natural_input_streams().get(*t));
401 out_map.offset_to(*t, natural_output_streams().get(*t));
405 if (collect_signal_nframes > 0) {
407 //std::cerr << " output, bufs " << bufs.count().n_audio() << " count, " << bufs.available().n_audio() << " available" << std::endl;
408 //std::cerr << " streams " << output_streams().n_audio() << std::endl;
410 _signal_analysis_outputs.set_count(output_streams());
412 for (uint32_t i = 0; i < output_streams().n_audio(); ++i) {
413 _signal_analysis_outputs.get_audio(i).read_from(
415 collect_signal_nframes,
416 _signal_analysis_collected_nframes); // offset is for target buffer
419 _signal_analysis_collected_nframes += collect_signal_nframes;
420 assert(_signal_analysis_collected_nframes <= _signal_analysis_collect_nframes_max);
422 if (_signal_analysis_collected_nframes == _signal_analysis_collect_nframes_max) {
423 _signal_analysis_collect_nframes_max = 0;
424 _signal_analysis_collected_nframes = 0;
426 AnalysisDataGathered(&_signal_analysis_inputs,
427 &_signal_analysis_outputs);
430 /* leave remaining channel buffers alone */
434 PluginInsert::silence (framecnt_t nframes)
440 ChanMapping in_map(input_streams());
441 ChanMapping out_map(output_streams());
443 if (_match.method == Split) {
444 /* fix the input mapping so that we have maps for each of the plugin's inputs */
445 in_map = ChanMapping (natural_input_streams ());
448 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
449 (*i)->connect_and_run (_session.get_silent_buffers ((*i)->get_info()->n_inputs), in_map, out_map, nframes, 0);
454 PluginInsert::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /*end_frame*/, pframes_t nframes, bool)
456 if (_pending_active) {
457 /* run as normal if we are active or moving from inactive to active */
459 if (_session.transport_rolling()) {
460 automation_run (bufs, nframes);
462 connect_and_run (bufs, nframes, 0, false);
467 if (has_no_audio_inputs()) {
469 /* silence all (audio) outputs. Should really declick
470 * at the transitions of "active"
473 uint32_t out = output_streams().n_audio ();
475 for (uint32_t n = 0; n < out; ++n) {
476 bufs.get_audio (n).silence (nframes);
479 bufs.count().set_audio (out);
483 /* does this need to be done with MIDI? it appears not */
485 uint32_t in = input_streams ().n_audio ();
486 uint32_t out = output_streams().n_audio ();
490 /* not active, but something has make up for any channel count increase */
492 for (uint32_t n = out - in; n < out; ++n) {
493 memcpy (bufs.get_audio (n).data(), bufs.get_audio(in - 1).data(), sizeof (Sample) * nframes);
497 bufs.count().set_audio (out);
501 _active = _pending_active;
503 /* we have no idea whether the plugin generated silence or not, so mark
504 * all buffers appropriately.
507 bufs.is_silent (false);
511 PluginInsert::set_parameter (Evoral::Parameter param, float val)
513 if (param.type() != PluginAutomation) {
517 /* the others will be set from the event triggered by this */
519 _plugins[0]->set_parameter (param.id(), val);
521 boost::shared_ptr<AutomationControl> ac
522 = boost::dynamic_pointer_cast<AutomationControl>(control(param));
527 warning << "set_parameter called for nonexistant parameter "
528 << EventTypeMap::instance().to_symbol(param) << endmsg;
531 _session.set_dirty();
535 PluginInsert::get_parameter (Evoral::Parameter param)
537 if (param.type() != PluginAutomation) {
540 assert (!_plugins.empty ());
541 return _plugins[0]->get_parameter (param.id());
546 PluginInsert::automation_run (BufferSet& bufs, pframes_t nframes)
548 Evoral::ControlEvent next_event (0, 0.0f);
549 framepos_t now = _session.transport_frame ();
550 framepos_t end = now + nframes;
551 framecnt_t offset = 0;
553 Glib::Mutex::Lock lm (control_lock(), Glib::TRY_LOCK);
556 connect_and_run (bufs, nframes, offset, false);
560 if (!find_next_event (now, end, next_event) || requires_fixed_sized_buffers()) {
562 /* no events have a time within the relevant range */
564 connect_and_run (bufs, nframes, offset, true, now);
570 framecnt_t cnt = min (((framecnt_t) ceil (next_event.when) - now), (framecnt_t) nframes);
572 connect_and_run (bufs, cnt, offset, true, now);
578 if (!find_next_event (now, end, next_event)) {
583 /* cleanup anything that is left to do */
586 connect_and_run (bufs, nframes, offset, true, now);
591 PluginInsert::default_parameter_value (const Evoral::Parameter& param)
593 if (param.type() != PluginAutomation)
596 if (_plugins.empty()) {
597 fatal << _("programming error: ") << X_("PluginInsert::default_parameter_value() called with no plugin")
602 return _plugins[0]->default_value (param.id());
605 boost::shared_ptr<Plugin>
606 PluginInsert::plugin_factory (boost::shared_ptr<Plugin> other)
608 boost::shared_ptr<LadspaPlugin> lp;
610 boost::shared_ptr<LV2Plugin> lv2p;
613 boost::shared_ptr<VSTPlugin> vp;
616 boost::shared_ptr<LXVSTPlugin> lxvp;
618 #ifdef AUDIOUNIT_SUPPORT
619 boost::shared_ptr<AUPlugin> ap;
622 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
623 return boost::shared_ptr<Plugin> (new LadspaPlugin (*lp));
625 } else if ((lv2p = boost::dynamic_pointer_cast<LV2Plugin> (other)) != 0) {
626 return boost::shared_ptr<Plugin> (new LV2Plugin (*lv2p));
629 } else if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (other)) != 0) {
630 return boost::shared_ptr<Plugin> (new VSTPlugin (*vp));
633 } else if ((lxvp = boost::dynamic_pointer_cast<LXVSTPlugin> (other)) != 0) {
634 return boost::shared_ptr<Plugin> (new LXVSTPlugin (*lxvp));
636 #ifdef AUDIOUNIT_SUPPORT
637 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
638 return boost::shared_ptr<Plugin> (new AUPlugin (*ap));
642 fatal << string_compose (_("programming error: %1"),
643 X_("unknown plugin type in PluginInsert::plugin_factory"))
646 return boost::shared_ptr<Plugin> ((Plugin*) 0);
650 PluginInsert::configure_io (ChanCount in, ChanCount out)
652 Match old_match = _match;
654 /* set the matching method and number of plugins that we will use to meet this configuration */
655 _match = private_can_support_io_configuration (in, out);
656 if (set_count (_match.plugins) == false) {
660 /* a signal needs emitting if we start or stop splitting */
661 if (old_match.method != _match.method && (old_match.method == Split || _match.method == Split)) {
662 SplittingChanged (); /* EMIT SIGNAL */
665 /* configure plugins */
666 switch (_match.method) {
669 if (_plugins.front()->configure_io (_plugins.front()->get_info()->n_inputs, out)) {
675 if (_plugins.front()->configure_io (in, out) == false) {
681 // we don't know the analysis window size, so we must work with the
682 // current buffer size here. each request for data fills in these
683 // buffers and the analyser makes sure it gets enough data for the
685 session().ensure_buffer_set (_signal_analysis_inputs, in);
686 //_signal_analysis_inputs.set_count (in);
688 session().ensure_buffer_set (_signal_analysis_outputs, out);
689 //_signal_analysis_outputs.set_count (out);
691 // std::cerr << "set counts to i" << in.n_audio() << "/o" << out.n_audio() << std::endl;
693 return Processor::configure_io (in, out);
696 /** Decide whether this PluginInsert can support a given IO configuration.
697 * To do this, we run through a set of possible solutions in rough order of
700 * @param in Required input channel count.
701 * @param out Filled in with the output channel count if we return true.
702 * @return true if the given IO configuration can be supported.
705 PluginInsert::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
707 return private_can_support_io_configuration (in, out).method != Impossible;
710 /** A private version of can_support_io_configuration which returns the method
711 * by which the configuration can be matched, rather than just whether or not
715 PluginInsert::private_can_support_io_configuration (ChanCount const & in, ChanCount& out) const
717 if (_plugins.front()->reconfigurable_io()) {
718 /* Plugin has flexible I/O, so delegate to it */
719 bool const r = _plugins.front()->can_support_io_configuration (in, out);
721 return Match (Impossible, 0);
724 return Match (Delegate, 1);
727 ChanCount inputs = _plugins[0]->get_info()->n_inputs;
728 ChanCount outputs = _plugins[0]->get_info()->n_outputs;
730 bool no_inputs = true;
731 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
732 if (inputs.get (*t) != 0) {
739 /* no inputs so we can take any input configuration since we throw it away */
741 return Match (NoInputs, 1);
744 /* Plugin inputs match requested inputs exactly */
747 return Match (ExactMatch, 1);
750 /* We may be able to run more than one copy of the plugin within this insert
751 to cope with the insert having more inputs than the plugin.
752 We allow replication only for plugins with either zero or 1 inputs and outputs
753 for every valid data type.
757 bool can_replicate = true;
758 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
760 uint32_t nin = inputs.get (*t);
762 // No inputs of this type
763 if (nin == 0 && in.get(*t) == 0) {
767 if (nin != 1 || outputs.get (*t) != 1) {
768 can_replicate = false;
772 // Potential factor not set yet
774 f = in.get(*t) / nin;
777 // Factor for this type does not match another type, can not replicate
778 if (f != (in.get(*t) / nin)) {
779 can_replicate = false;
785 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
786 out.set (*t, outputs.get(*t) * f);
788 return Match (Replicate, f);
791 /* If the processor has exactly one input of a given type, and
792 the plugin has more, we can feed the single processor input
793 to some or all of the plugin inputs. This is rather
794 special-case-y, but the 1-to-many case is by far the
795 simplest. How do I split thy 2 processor inputs to 3
796 plugin inputs? Let me count the ways ...
799 bool can_split = true;
800 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
802 bool const can_split_type = (in.get (*t) == 1 && inputs.get (*t) > 1);
803 bool const nothing_to_do_for_type = (in.get (*t) == 0 && inputs.get (*t) == 0);
805 if (!can_split_type && !nothing_to_do_for_type) {
812 return Match (Split, 1);
815 /* If the plugin has more inputs than we want, we can `hide' some of them
816 by feeding them silence.
819 bool could_hide = false;
820 bool cannot_hide = false;
821 ChanCount hide_channels;
823 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
824 if (inputs.get(*t) > in.get(*t)) {
825 /* there is potential to hide, since the plugin has more inputs of type t than the insert */
826 hide_channels.set (*t, inputs.get(*t) - in.get(*t));
828 } else if (inputs.get(*t) < in.get(*t)) {
829 /* we definitely cannot hide, since the plugin has fewer inputs of type t than the insert */
834 if (could_hide && !cannot_hide) {
836 return Match (Hide, 1, hide_channels);
839 return Match (Impossible, 0);
843 PluginInsert::get_state ()
849 PluginInsert::state (bool full)
851 XMLNode& node = Processor::state (full);
853 node.add_property("type", _plugins[0]->state_node_name());
854 node.add_property("unique-id", _plugins[0]->unique_id());
855 node.add_property("count", string_compose("%1", _plugins.size()));
856 node.add_child_nocopy (_plugins[0]->get_state());
858 for (Controls::iterator c = controls().begin(); c != controls().end(); ++c) {
859 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> ((*c).second);
861 node.add_child_nocopy (ac->get_state());
869 PluginInsert::set_control_ids (const XMLNode& node, int version)
871 const XMLNodeList& nlist = node.children();
872 XMLNodeConstIterator iter;
873 set<Evoral::Parameter>::const_iterator p;
875 for (iter = nlist.begin(); iter != nlist.end(); ++iter) {
876 if ((*iter)->name() == Controllable::xml_node_name) {
877 const XMLProperty* prop;
879 if ((prop = (*iter)->property (X_("parameter"))) != 0) {
880 uint32_t p = atoi (prop->value());
881 boost::shared_ptr<Evoral::Control> c = control (Evoral::Parameter (PluginAutomation, 0, p));
885 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> (c);
887 ac->set_state (**iter, version);
895 PluginInsert::set_state(const XMLNode& node, int version)
897 XMLNodeList nlist = node.children();
898 XMLNodeIterator niter;
899 XMLPropertyList plist;
900 const XMLProperty *prop;
901 ARDOUR::PluginType type;
903 if ((prop = node.property ("type")) == 0) {
904 error << _("XML node describing plugin is missing the `type' field") << endmsg;
908 if (prop->value() == X_("ladspa") || prop->value() == X_("Ladspa")) { /* handle old school sessions */
909 type = ARDOUR::LADSPA;
910 } else if (prop->value() == X_("lv2")) {
912 } else if (prop->value() == X_("vst")) {
914 } else if (prop->value() == X_("lxvst")) {
915 type = ARDOUR::LXVST;
916 } else if (prop->value() == X_("audiounit")) {
917 type = ARDOUR::AudioUnit;
919 error << string_compose (_("unknown plugin type %1 in plugin insert state"),
925 prop = node.property ("unique-id");
929 /* older sessions contain VST plugins with only an "id" field.
932 if (type == ARDOUR::VST) {
933 prop = node.property ("id");
938 /*There shouldn't be any older sessions with linuxVST support.. but anyway..*/
940 if (type == ARDOUR::LXVST) {
941 prop = node.property ("id");
947 error << _("Plugin has no unique ID field") << endmsg;
952 boost::shared_ptr<Plugin> plugin = find_plugin (_session, prop->value(), type);
955 error << string_compose(
956 _("Found a reference to a plugin (\"%1\") that is unknown.\n"
957 "Perhaps it was removed or moved since it was last used."),
963 // The name of the PluginInsert comes from the plugin, nothing else
964 _name = plugin->get_info()->name;
968 // Processor::set_state() will set this, but too late
969 // for it to be available when setting up plugin
970 // state. We can't call Processor::set_state() until
971 // the plugins themselves are created and added.
975 if (_plugins.empty()) {
976 /* if we are adding the first plugin, we will need to set
977 up automatable controls.
980 create_automatable_parameters ();
981 set_control_ids (node, version);
984 if ((prop = node.property ("count")) != 0) {
985 sscanf (prop->value().c_str(), "%u", &count);
988 if (_plugins.size() != count) {
989 for (uint32_t n = 1; n < count; ++n) {
990 add_plugin (plugin_factory (plugin));
994 Processor::set_state (node, version);
996 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
998 /* find the node with the type-specific node name ("lv2", "ladspa", etc)
999 and set all plugins to the same state.
1002 if ((*niter)->name() == plugin->state_node_name()) {
1004 plugin->set_state (**niter, version);
1006 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
1007 (*i)->set_state (**niter, version);
1014 if (version < 3000) {
1016 /* Only 2.X sessions need a call to set_parameter_state() - in 3.X and above
1017 this is all handled by Automatable
1020 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1021 if ((*niter)->name() == "Redirect") {
1022 /* XXX do we need to tackle placement? i think not (pd; oct 16 2009) */
1023 Processor::set_state (**niter, version);
1028 set_parameter_state_2X (node, version);
1031 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
1035 (*i)->deactivate ();
1043 PluginInsert::set_parameter_state_2X (const XMLNode& node, int version)
1045 XMLNodeList nlist = node.children();
1046 XMLNodeIterator niter;
1048 /* look for port automation node */
1050 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1052 if ((*niter)->name() != port_automation_node_name) {
1058 XMLNodeConstIterator iter;
1063 cnodes = (*niter)->children ("port");
1065 for (iter = cnodes.begin(); iter != cnodes.end(); ++iter){
1069 if ((cprop = child->property("number")) != 0) {
1070 port = cprop->value().c_str();
1072 warning << _("PluginInsert: Auto: no ladspa port number") << endmsg;
1076 sscanf (port, "%" PRIu32, &port_id);
1078 if (port_id >= _plugins[0]->parameter_count()) {
1079 warning << _("PluginInsert: Auto: port id out of range") << endmsg;
1083 boost::shared_ptr<AutomationControl> c = boost::dynamic_pointer_cast<AutomationControl>(
1084 control(Evoral::Parameter(PluginAutomation, 0, port_id), true));
1087 if (!child->children().empty()) {
1088 c->alist()->set_state (*child->children().front(), version);
1090 /* In some cases 2.X saves lists with min_yval and max_yval
1091 being FLT_MIN and FLT_MAX respectively. This causes problems
1092 in A3 because these min/max values are used to compute
1093 where GUI control points should be drawn. If we see such
1094 values, `correct' them to the min/max of the appropriate
1098 float min_y = c->alist()->get_min_y ();
1099 float max_y = c->alist()->get_max_y ();
1101 Plugin::ParameterDescriptor desc;
1102 _plugins.front()->get_parameter_descriptor (port_id, desc);
1104 if (min_y == FLT_MIN) {
1108 if (max_y == FLT_MAX) {
1112 c->alist()->set_yrange (min_y, max_y);
1115 error << string_compose (_("PluginInsert: automatable control %1 not found - ignored"), port_id) << endmsg;
1127 PluginInsert::describe_parameter (Evoral::Parameter param)
1129 if (param.type() != PluginAutomation) {
1130 return Automatable::describe_parameter(param);
1133 return _plugins[0]->describe_parameter (param);
1137 PluginInsert::signal_latency() const
1139 if (_user_latency) {
1140 return _user_latency;
1143 return _plugins[0]->signal_latency ();
1147 PluginInsert::type ()
1149 return plugin()->get_info()->type;
1152 PluginInsert::PluginControl::PluginControl (PluginInsert* p, const Evoral::Parameter ¶m, boost::shared_ptr<AutomationList> list)
1153 : AutomationControl (p->session(), param, list, p->describe_parameter(param))
1156 Plugin::ParameterDescriptor desc;
1157 p->plugin(0)->get_parameter_descriptor (param.id(), desc);
1158 _logarithmic = desc.logarithmic;
1159 _sr_dependent = desc.sr_dependent;
1160 _toggled = desc.toggled;
1163 /** @param val `user' value */
1165 PluginInsert::PluginControl::set_value (double user_val)
1167 /* FIXME: probably should be taking out some lock here.. */
1169 double const plugin_val = user_to_plugin (user_val);
1171 for (Plugins::iterator i = _plugin->_plugins.begin(); i != _plugin->_plugins.end(); ++i) {
1172 (*i)->set_parameter (_list->parameter().id(), plugin_val);
1175 boost::shared_ptr<Plugin> iasp = _plugin->_impulseAnalysisPlugin.lock();
1177 iasp->set_parameter (_list->parameter().id(), plugin_val);
1180 AutomationControl::set_value (user_val);
1184 PluginInsert::PluginControl::user_to_plugin (double val) const
1186 /* no known transformations at this time */
1191 PluginInsert::PluginControl::user_to_ui (double val) const
1205 PluginInsert::PluginControl::ui_to_user (double val) const
1214 /** Convert plugin values to UI values. See pbd/controllable.h */
1216 PluginInsert::PluginControl::plugin_to_ui (double val) const
1218 return user_to_ui (plugin_to_user (val));
1222 PluginInsert::PluginControl::plugin_to_user (double val) const
1224 /* no known transformations at this time */
1229 PluginInsert::PluginControl::get_state ()
1233 XMLNode& node (AutomationControl::get_state());
1234 ss << parameter().id();
1235 node.add_property (X_("parameter"), ss.str());
1240 /** @return `user' val */
1242 PluginInsert::PluginControl::get_value () const
1244 /* FIXME: probably should be taking out some lock here.. */
1246 return plugin_to_user (_plugin->get_parameter (_list->parameter()));
1249 boost::shared_ptr<Plugin>
1250 PluginInsert::get_impulse_analysis_plugin()
1252 boost::shared_ptr<Plugin> ret;
1253 if (_impulseAnalysisPlugin.expired()) {
1254 ret = plugin_factory(_plugins[0]);
1255 _impulseAnalysisPlugin = ret;
1257 ret = _impulseAnalysisPlugin.lock();
1264 PluginInsert::collect_signal_for_analysis (framecnt_t nframes)
1266 // called from outside the audio thread, so this should be safe
1267 // only do audio as analysis is (currently) only for audio plugins
1268 _signal_analysis_inputs.ensure_buffers( DataType::AUDIO, input_streams().n_audio(), nframes);
1269 _signal_analysis_outputs.ensure_buffers( DataType::AUDIO, output_streams().n_audio(), nframes);
1271 _signal_analysis_collected_nframes = 0;
1272 _signal_analysis_collect_nframes_max = nframes;
1275 /** Add a plugin to our list and activate it if we have already been activated */
1277 PluginInsert::add_plugin_with_activation (boost::shared_ptr<Plugin> plugin)
1279 plugin->set_insert_info (this);
1280 _plugins.push_back (plugin);
1282 plugin->activate ();
1286 /** Add a plugin to our list */
1288 PluginInsert::add_plugin (boost::shared_ptr<Plugin> plugin)
1290 plugin->set_insert_info (this);
1291 _plugins.push_back (plugin);
1295 PluginInsert::realtime_handle_transport_stopped ()
1297 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
1298 (*i)->realtime_handle_transport_stopped ();