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 ChanCount out = _plugins.front()->get_info()->n_outputs;
137 DEBUG_TRACE (DEBUG::Processors, string_compose ("Plugin insert, static output streams = %1\n", out));
139 if (out == ChanCount::INFINITE) {
140 return _plugins.front()->output_streams ();
142 out.set_audio (out.n_audio() * _plugins.size());
143 out.set_midi (out.n_midi() * _plugins.size());
149 PluginInsert::input_streams() const
151 ChanCount in = _plugins[0]->get_info()->n_inputs;
153 DEBUG_TRACE (DEBUG::Processors, string_compose ("Plugin insert, static input streams = %1, match using %2\n", in, _match.method));
155 if (_match.method == Split) {
157 /* we are splitting 1 processor input to multiple plugin inputs,
158 so we have a maximum of 1 stream of each type.
160 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
161 if (in.get (*t) > 1) {
167 } else if (_match.method == Hide) {
169 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
170 in.set (*t, in.get (*t) - _match.hide.get (*t));
174 } else if (in == ChanCount::INFINITE) {
176 return _plugins[0]->input_streams ();
180 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
181 in.set (*t, in.get (*t) * _plugins.size ());
189 PluginInsert::natural_output_streams() const
191 return _plugins[0]->get_info()->n_outputs;
195 PluginInsert::natural_input_streams() const
197 return _plugins[0]->get_info()->n_inputs;
201 PluginInsert::has_no_inputs() const
203 return _plugins[0]->get_info()->n_inputs == ChanCount::ZERO;
207 PluginInsert::has_no_audio_inputs() const
209 return _plugins[0]->get_info()->n_inputs.n_audio() == 0;
213 PluginInsert::is_midi_instrument() const
215 /* XXX more finesse is possible here. VST plugins have a
216 a specific "instrument" flag, for example.
218 PluginInfoPtr pi = _plugins[0]->get_info();
220 return pi->n_inputs.n_midi() != 0 &&
221 pi->n_outputs.n_audio() > 0;
225 PluginInsert::create_automatable_parameters ()
227 assert (!_plugins.empty());
229 set<Evoral::Parameter> a = _plugins.front()->automatable ();
231 Plugin::ParameterDescriptor desc;
233 for (set<Evoral::Parameter>::iterator i = a.begin(); i != a.end(); ++i) {
234 if (i->type() == PluginAutomation) {
236 Evoral::Parameter param(*i);
238 _plugins.front()->get_parameter_descriptor(i->id(), desc);
240 /* the Parameter belonging to the actual plugin doesn't have its range set
241 but we want the Controllable related to this Parameter to have those limits.
244 param.set_range (desc.lower, desc.upper, _plugins.front()->default_value(i->id()), desc.toggled);
245 can_automate (param);
246 boost::shared_ptr<AutomationList> list(new AutomationList(param));
247 add_control (boost::shared_ptr<AutomationControl> (new PluginControl(this, param, list)));
253 PluginInsert::parameter_changed (Evoral::Parameter which, float val)
255 if (which.type() != PluginAutomation)
258 Plugins::iterator i = _plugins.begin();
260 /* don't set the first plugin, just all the slaves */
262 if (i != _plugins.end()) {
264 for (; i != _plugins.end(); ++i) {
265 (*i)->set_parameter (which, val);
271 PluginInsert::set_block_size (pframes_t nframes)
274 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
275 if ((*i)->set_block_size (nframes) != 0) {
283 PluginInsert::activate ()
285 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
289 Processor::activate ();
293 PluginInsert::deactivate ()
295 Processor::deactivate ();
297 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
303 PluginInsert::flush ()
305 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
311 PluginInsert::connect_and_run (BufferSet& bufs, pframes_t nframes, framecnt_t offset, bool with_auto, framepos_t now)
313 // Calculate if, and how many frames we need to collect for analysis
314 framecnt_t collect_signal_nframes = (_signal_analysis_collect_nframes_max -
315 _signal_analysis_collected_nframes);
316 if (nframes < collect_signal_nframes) { // we might not get all frames now
317 collect_signal_nframes = nframes;
320 ChanCount const in_streams = input_streams ();
321 ChanCount const out_streams = output_streams ();
323 ChanMapping in_map (in_streams);
324 ChanMapping out_map (out_streams);
326 if (_match.method == Split) {
327 /* fix the input mapping so that we have maps for each of the plugin's inputs */
328 in_map = ChanMapping (natural_input_streams ());
330 /* copy the first stream's buffer contents to the others */
331 /* XXX: audio only */
332 Sample const * mono = bufs.get_audio (in_map.get (DataType::AUDIO, 0)).data (offset);
333 for (uint32_t i = in_streams.n_audio(); i < natural_input_streams().n_audio(); ++i) {
334 memcpy (bufs.get_audio (in_map.get (DataType::AUDIO, i)).data (offset), mono, sizeof (Sample) * nframes);
338 if (_match.method == Hide) {
339 /* Silence the hidden input buffers */
340 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
341 for (uint32_t i = in_streams.get(*t); i < (in_streams.get(*t) + _match.hide.get(*t)); ++i) {
342 bufs.get(*t, i).silence (nframes);
347 /* Note that we've already required that plugins
348 be able to handle in-place processing.
355 for (Controls::iterator li = controls().begin(); li != controls().end(); ++li, ++n) {
357 boost::shared_ptr<AutomationControl> c
358 = boost::dynamic_pointer_cast<AutomationControl>(li->second);
360 if (c->parameter().type() == PluginAutomation && c->automation_playback()) {
363 const float val = c->list()->rt_safe_eval (now, valid);
373 if (collect_signal_nframes > 0) {
375 //std::cerr << "collect input, bufs " << bufs.count().n_audio() << " count, " << bufs.available().n_audio() << " available" << std::endl;
376 //std::cerr << " streams " << input_streams().n_audio() << std::endl;
377 //std::cerr << "filling buffer with " << collect_signal_nframes << " frames at " << _signal_analysis_collected_nframes << std::endl;
379 _signal_analysis_inputs.set_count(input_streams());
381 for (uint32_t i = 0; i < input_streams().n_audio(); ++i) {
382 _signal_analysis_inputs.get_audio(i).read_from(
384 collect_signal_nframes,
385 _signal_analysis_collected_nframes); // offset is for target buffer
390 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
391 (*i)->connect_and_run(bufs, in_map, out_map, nframes, offset);
392 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
393 in_map.offset_to(*t, natural_input_streams().get(*t));
394 out_map.offset_to(*t, natural_output_streams().get(*t));
398 if (collect_signal_nframes > 0) {
400 //std::cerr << " output, bufs " << bufs.count().n_audio() << " count, " << bufs.available().n_audio() << " available" << std::endl;
401 //std::cerr << " streams " << output_streams().n_audio() << std::endl;
403 _signal_analysis_outputs.set_count(output_streams());
405 for (uint32_t i = 0; i < output_streams().n_audio(); ++i) {
406 _signal_analysis_outputs.get_audio(i).read_from(
408 collect_signal_nframes,
409 _signal_analysis_collected_nframes); // offset is for target buffer
412 _signal_analysis_collected_nframes += collect_signal_nframes;
413 assert(_signal_analysis_collected_nframes <= _signal_analysis_collect_nframes_max);
415 if (_signal_analysis_collected_nframes == _signal_analysis_collect_nframes_max) {
416 _signal_analysis_collect_nframes_max = 0;
417 _signal_analysis_collected_nframes = 0;
419 AnalysisDataGathered(&_signal_analysis_inputs,
420 &_signal_analysis_outputs);
423 /* leave remaining channel buffers alone */
427 PluginInsert::silence (framecnt_t nframes)
433 ChanMapping in_map(input_streams());
434 ChanMapping out_map(output_streams());
436 if (_match.method == Split) {
437 /* fix the input mapping so that we have maps for each of the plugin's inputs */
438 in_map = ChanMapping (natural_input_streams ());
441 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
442 (*i)->connect_and_run (_session.get_silent_buffers ((*i)->get_info()->n_inputs), in_map, out_map, nframes, 0);
447 PluginInsert::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /*end_frame*/, pframes_t nframes, bool)
449 if (_pending_active) {
450 /* run as normal if we are active or moving from inactive to active */
452 if (_session.transport_rolling()) {
453 automation_run (bufs, nframes);
455 connect_and_run (bufs, nframes, 0, false);
460 if (has_no_audio_inputs()) {
462 /* silence all (audio) outputs. Should really declick
463 * at the transitions of "active"
466 uint32_t out = _plugins[0]->get_info()->n_outputs.n_audio();
468 for (uint32_t n = 0; n < out; ++n) {
469 bufs.get_audio (n).silence (nframes);
472 bufs.count().set_audio (out);
476 /* does this need to be done with MIDI? it appears not */
478 uint32_t in = _plugins[0]->get_info()->n_inputs.n_audio();
479 uint32_t out = _plugins[0]->get_info()->n_outputs.n_audio();
483 /* not active, but something has make up for any channel count increase */
485 for (uint32_t n = out - in; n < out; ++n) {
486 memcpy (bufs.get_audio(n).data(), bufs.get_audio(in - 1).data(), sizeof (Sample) * nframes);
490 bufs.count().set_audio (out);
494 _active = _pending_active;
498 PluginInsert::set_parameter (Evoral::Parameter param, float val)
500 if (param.type() != PluginAutomation) {
504 /* the others will be set from the event triggered by this */
506 _plugins[0]->set_parameter (param.id(), val);
508 boost::shared_ptr<AutomationControl> ac
509 = boost::dynamic_pointer_cast<AutomationControl>(control(param));
514 warning << "set_parameter called for nonexistant parameter "
515 << EventTypeMap::instance().to_symbol(param) << endmsg;
518 _session.set_dirty();
522 PluginInsert::get_parameter (Evoral::Parameter param)
524 if (param.type() != PluginAutomation) {
527 assert (!_plugins.empty ());
528 return _plugins[0]->get_parameter (param.id());
533 PluginInsert::automation_run (BufferSet& bufs, pframes_t nframes)
535 Evoral::ControlEvent next_event (0, 0.0f);
536 framepos_t now = _session.transport_frame ();
537 framepos_t end = now + nframes;
538 framecnt_t offset = 0;
540 Glib::Mutex::Lock lm (control_lock(), Glib::TRY_LOCK);
543 connect_and_run (bufs, nframes, offset, false);
547 if (!find_next_event (now, end, next_event) || requires_fixed_sized_buffers()) {
549 /* no events have a time within the relevant range */
551 connect_and_run (bufs, nframes, offset, true, now);
557 framecnt_t cnt = min (((framecnt_t) ceil (next_event.when) - now), (framecnt_t) nframes);
559 connect_and_run (bufs, cnt, offset, true, now);
565 if (!find_next_event (now, end, next_event)) {
570 /* cleanup anything that is left to do */
573 connect_and_run (bufs, nframes, offset, true, now);
578 PluginInsert::default_parameter_value (const Evoral::Parameter& param)
580 if (param.type() != PluginAutomation)
583 if (_plugins.empty()) {
584 fatal << _("programming error: ") << X_("PluginInsert::default_parameter_value() called with no plugin")
589 return _plugins[0]->default_value (param.id());
592 boost::shared_ptr<Plugin>
593 PluginInsert::plugin_factory (boost::shared_ptr<Plugin> other)
595 boost::shared_ptr<LadspaPlugin> lp;
597 boost::shared_ptr<LV2Plugin> lv2p;
600 boost::shared_ptr<VSTPlugin> vp;
603 boost::shared_ptr<LXVSTPlugin> lxvp;
605 #ifdef AUDIOUNIT_SUPPORT
606 boost::shared_ptr<AUPlugin> ap;
609 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
610 return boost::shared_ptr<Plugin> (new LadspaPlugin (*lp));
612 } else if ((lv2p = boost::dynamic_pointer_cast<LV2Plugin> (other)) != 0) {
613 return boost::shared_ptr<Plugin> (new LV2Plugin (*lv2p));
616 } else if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (other)) != 0) {
617 return boost::shared_ptr<Plugin> (new VSTPlugin (*vp));
620 } else if ((lxvp = boost::dynamic_pointer_cast<LXVSTPlugin> (other)) != 0) {
621 return boost::shared_ptr<Plugin> (new LXVSTPlugin (*lxvp));
623 #ifdef AUDIOUNIT_SUPPORT
624 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
625 return boost::shared_ptr<Plugin> (new AUPlugin (*ap));
629 fatal << string_compose (_("programming error: %1"),
630 X_("unknown plugin type in PluginInsert::plugin_factory"))
633 return boost::shared_ptr<Plugin> ((Plugin*) 0);
637 PluginInsert::configure_io (ChanCount in, ChanCount out)
639 Match old_match = _match;
641 /* set the matching method and number of plugins that we will use to meet this configuration */
642 _match = private_can_support_io_configuration (in, out);
643 if (set_count (_match.plugins) == false) {
647 /* a signal needs emitting if we start or stop splitting */
648 if (old_match.method != _match.method && (old_match.method == Split || _match.method == Split)) {
649 SplittingChanged (); /* EMIT SIGNAL */
652 /* configure plugins */
653 switch (_match.method) {
656 if (_plugins.front()->configure_io (_plugins.front()->get_info()->n_inputs, out)) {
662 if (_plugins.front()->configure_io (in, out) == false) {
668 // we don't know the analysis window size, so we must work with the
669 // current buffer size here. each request for data fills in these
670 // buffers and the analyser makes sure it gets enough data for the
672 session().ensure_buffer_set (_signal_analysis_inputs, in);
673 //_signal_analysis_inputs.set_count (in);
675 session().ensure_buffer_set (_signal_analysis_outputs, out);
676 //_signal_analysis_outputs.set_count (out);
678 // std::cerr << "set counts to i" << in.n_audio() << "/o" << out.n_audio() << std::endl;
680 return Processor::configure_io (in, out);
683 /** Decide whether this PluginInsert can support a given IO configuration.
684 * To do this, we run through a set of possible solutions in rough order of
687 * @param in Required input channel count.
688 * @param out Filled in with the output channel count if we return true.
689 * @return true if the given IO configuration can be supported.
692 PluginInsert::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
694 return private_can_support_io_configuration (in, out).method != Impossible;
697 /** A private version of can_support_io_configuration which returns the method
698 * by which the configuration can be matched, rather than just whether or not
702 PluginInsert::private_can_support_io_configuration (ChanCount const & in, ChanCount& out) const
704 if (_plugins.front()->reconfigurable_io()) {
705 /* Plugin has flexible I/O, so delegate to it */
706 bool const r = _plugins.front()->can_support_io_configuration (in, out);
708 return Match (Impossible, 0);
711 return Match (Delegate, 1);
714 ChanCount inputs = _plugins[0]->get_info()->n_inputs;
715 ChanCount outputs = _plugins[0]->get_info()->n_outputs;
717 bool no_inputs = true;
718 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
719 if (inputs.get (*t) != 0) {
726 /* no inputs so we can take any input configuration since we throw it away */
728 return Match (NoInputs, 1);
731 /* Plugin inputs match requested inputs exactly */
734 return Match (ExactMatch, 1);
737 /* We may be able to run more than one copy of the plugin within this insert
738 to cope with the insert having more inputs than the plugin.
739 We allow replication only for plugins with either zero or 1 inputs and outputs
740 for every valid data type.
744 bool can_replicate = true;
745 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
747 uint32_t nin = inputs.get (*t);
749 // No inputs of this type
750 if (nin == 0 && in.get(*t) == 0) {
754 if (nin != 1 || outputs.get (*t) != 1) {
755 can_replicate = false;
759 // Potential factor not set yet
761 f = in.get(*t) / nin;
764 // Factor for this type does not match another type, can not replicate
765 if (f != (in.get(*t) / nin)) {
766 can_replicate = false;
772 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
773 out.set (*t, outputs.get(*t) * f);
775 return Match (Replicate, f);
778 /* If the processor has exactly one input of a given type, and
779 the plugin has more, we can feed the single processor input
780 to some or all of the plugin inputs. This is rather
781 special-case-y, but the 1-to-many case is by far the
782 simplest. How do I split thy 2 processor inputs to 3
783 plugin inputs? Let me count the ways ...
786 bool can_split = true;
787 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
789 bool const can_split_type = (in.get (*t) == 1 && inputs.get (*t) > 1);
790 bool const nothing_to_do_for_type = (in.get (*t) == 0 && inputs.get (*t) == 0);
792 if (!can_split_type && !nothing_to_do_for_type) {
799 return Match (Split, 1);
802 /* If the plugin has more inputs than we want, we can `hide' some of them
803 by feeding them silence.
806 bool can_hide = false;
807 ChanCount hide_channels;
809 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
810 if (inputs.get(*t) > in.get(*t)) {
811 hide_channels.set (*t, inputs.get(*t) - in.get(*t));
818 return Match (Hide, 1, hide_channels);
821 return Match (Impossible, 0);
825 PluginInsert::get_state ()
831 PluginInsert::state (bool full)
833 XMLNode& node = Processor::state (full);
835 node.add_property("type", _plugins[0]->state_node_name());
836 node.add_property("unique-id", _plugins[0]->unique_id());
837 node.add_property("count", string_compose("%1", _plugins.size()));
838 node.add_child_nocopy (_plugins[0]->get_state());
840 for (Controls::iterator c = controls().begin(); c != controls().end(); ++c) {
841 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> ((*c).second);
843 node.add_child_nocopy (ac->get_state());
851 PluginInsert::set_control_ids (const XMLNode& node, int version)
853 const XMLNodeList& nlist = node.children();
854 XMLNodeConstIterator iter;
855 set<Evoral::Parameter>::const_iterator p;
857 for (iter = nlist.begin(); iter != nlist.end(); ++iter) {
858 if ((*iter)->name() == Controllable::xml_node_name) {
859 const XMLProperty* prop;
861 if ((prop = (*iter)->property (X_("parameter"))) != 0) {
862 uint32_t p = atoi (prop->value());
863 boost::shared_ptr<Evoral::Control> c = control (Evoral::Parameter (PluginAutomation, 0, p));
867 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> (c);
869 ac->set_state (**iter, version);
877 PluginInsert::set_state(const XMLNode& node, int version)
879 XMLNodeList nlist = node.children();
880 XMLNodeIterator niter;
881 XMLPropertyList plist;
882 const XMLProperty *prop;
883 ARDOUR::PluginType type;
885 if ((prop = node.property ("type")) == 0) {
886 error << _("XML node describing plugin is missing the `type' field") << endmsg;
890 if (prop->value() == X_("ladspa") || prop->value() == X_("Ladspa")) { /* handle old school sessions */
891 type = ARDOUR::LADSPA;
892 } else if (prop->value() == X_("lv2")) {
894 } else if (prop->value() == X_("vst")) {
896 } else if (prop->value() == X_("lxvst")) {
897 type = ARDOUR::LXVST;
898 } else if (prop->value() == X_("audiounit")) {
899 type = ARDOUR::AudioUnit;
901 error << string_compose (_("unknown plugin type %1 in plugin insert state"),
907 prop = node.property ("unique-id");
911 /* older sessions contain VST plugins with only an "id" field.
914 if (type == ARDOUR::VST) {
915 prop = node.property ("id");
920 /*There shouldn't be any older sessions with linuxVST support.. but anyway..*/
922 if (type == ARDOUR::LXVST) {
923 prop = node.property ("id");
929 error << _("Plugin has no unique ID field") << endmsg;
934 boost::shared_ptr<Plugin> plugin = find_plugin (_session, prop->value(), type);
937 error << string_compose(
938 _("Found a reference to a plugin (\"%1\") that is unknown.\n"
939 "Perhaps it was removed or moved since it was last used."),
945 // The name of the PluginInsert comes from the plugin, nothing else
946 _name = plugin->get_info()->name;
950 // Processor::set_state() will set this, but too late
951 // for it to be available when setting up plugin
952 // state. We can't call Processor::set_state() until
953 // the plugins themselves are created and added.
957 if (_plugins.empty()) {
958 /* if we are adding the first plugin, we will need to set
959 up automatable controls.
962 create_automatable_parameters ();
963 set_control_ids (node, version);
966 if ((prop = node.property ("count")) != 0) {
967 sscanf (prop->value().c_str(), "%u", &count);
970 if (_plugins.size() != count) {
971 for (uint32_t n = 1; n < count; ++n) {
972 add_plugin (plugin_factory (plugin));
976 Processor::set_state (node, version);
978 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
980 /* find the node with the type-specific node name ("lv2", "ladspa", etc)
981 and set all plugins to the same state.
984 if ((*niter)->name() == plugin->state_node_name()) {
986 plugin->set_state (**niter, version);
988 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
989 (*i)->set_state (**niter, version);
996 if (version < 3000) {
998 /* Only 2.X sessions need a call to set_parameter_state() - in 3.X and above
999 this is all handled by Automatable
1002 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1003 if ((*niter)->name() == "Redirect") {
1004 /* XXX do we need to tackle placement? i think not (pd; oct 16 2009) */
1005 Processor::set_state (**niter, version);
1010 set_parameter_state_2X (node, version);
1013 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
1017 (*i)->deactivate ();
1025 PluginInsert::set_parameter_state_2X (const XMLNode& node, int version)
1027 XMLNodeList nlist = node.children();
1028 XMLNodeIterator niter;
1030 /* look for port automation node */
1032 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1034 if ((*niter)->name() != port_automation_node_name) {
1040 XMLNodeConstIterator iter;
1045 cnodes = (*niter)->children ("port");
1047 for (iter = cnodes.begin(); iter != cnodes.end(); ++iter){
1051 if ((cprop = child->property("number")) != 0) {
1052 port = cprop->value().c_str();
1054 warning << _("PluginInsert: Auto: no ladspa port number") << endmsg;
1058 sscanf (port, "%" PRIu32, &port_id);
1060 if (port_id >= _plugins[0]->parameter_count()) {
1061 warning << _("PluginInsert: Auto: port id out of range") << endmsg;
1065 boost::shared_ptr<AutomationControl> c = boost::dynamic_pointer_cast<AutomationControl>(
1066 control(Evoral::Parameter(PluginAutomation, 0, port_id), true));
1069 if (!child->children().empty()) {
1070 c->alist()->set_state (*child->children().front(), version);
1072 /* In some cases 2.X saves lists with min_yval and max_yval
1073 being FLT_MIN and FLT_MAX respectively. This causes problems
1074 in A3 because these min/max values are used to compute
1075 where GUI control points should be drawn. If we see such
1076 values, `correct' them to the min/max of the appropriate
1080 float min_y = c->alist()->get_min_y ();
1081 float max_y = c->alist()->get_max_y ();
1083 Plugin::ParameterDescriptor desc;
1084 _plugins.front()->get_parameter_descriptor (port_id, desc);
1086 if (min_y == FLT_MIN) {
1090 if (max_y == FLT_MAX) {
1094 c->alist()->set_yrange (min_y, max_y);
1097 error << string_compose (_("PluginInsert: automatable control %1 not found - ignored"), port_id) << endmsg;
1109 PluginInsert::describe_parameter (Evoral::Parameter param)
1111 if (param.type() != PluginAutomation) {
1112 return Automatable::describe_parameter(param);
1115 return _plugins[0]->describe_parameter (param);
1119 PluginInsert::signal_latency() const
1121 if (_user_latency) {
1122 return _user_latency;
1125 return _plugins[0]->signal_latency ();
1129 PluginInsert::type ()
1131 return plugin()->get_info()->type;
1134 PluginInsert::PluginControl::PluginControl (PluginInsert* p, const Evoral::Parameter ¶m, boost::shared_ptr<AutomationList> list)
1135 : AutomationControl (p->session(), param, list, p->describe_parameter(param))
1138 Plugin::ParameterDescriptor desc;
1139 p->plugin(0)->get_parameter_descriptor (param.id(), desc);
1140 _logarithmic = desc.logarithmic;
1141 _sr_dependent = desc.sr_dependent;
1142 _toggled = desc.toggled;
1145 /** @param val `user' value */
1147 PluginInsert::PluginControl::set_value (double user_val)
1149 /* FIXME: probably should be taking out some lock here.. */
1151 double const plugin_val = user_to_plugin (user_val);
1153 for (Plugins::iterator i = _plugin->_plugins.begin(); i != _plugin->_plugins.end(); ++i) {
1154 (*i)->set_parameter (_list->parameter().id(), plugin_val);
1157 boost::shared_ptr<Plugin> iasp = _plugin->_impulseAnalysisPlugin.lock();
1159 iasp->set_parameter (_list->parameter().id(), plugin_val);
1162 AutomationControl::set_value (user_val);
1166 PluginInsert::PluginControl::user_to_plugin (double val) const
1168 /* no known transformations at this time */
1173 PluginInsert::PluginControl::user_to_ui (double val) const
1187 PluginInsert::PluginControl::ui_to_user (double val) const
1196 /** Convert plugin values to UI values. See pbd/controllable.h */
1198 PluginInsert::PluginControl::plugin_to_ui (double val) const
1200 return user_to_ui (plugin_to_user (val));
1204 PluginInsert::PluginControl::plugin_to_user (double val) const
1206 /* no known transformations at this time */
1211 PluginInsert::PluginControl::get_state ()
1215 XMLNode& node (AutomationControl::get_state());
1216 ss << parameter().id();
1217 node.add_property (X_("parameter"), ss.str());
1222 /** @return `user' val */
1224 PluginInsert::PluginControl::get_value () const
1226 /* FIXME: probably should be taking out some lock here.. */
1228 return plugin_to_user (_plugin->get_parameter (_list->parameter()));
1231 boost::shared_ptr<Plugin>
1232 PluginInsert::get_impulse_analysis_plugin()
1234 boost::shared_ptr<Plugin> ret;
1235 if (_impulseAnalysisPlugin.expired()) {
1236 ret = plugin_factory(_plugins[0]);
1237 _impulseAnalysisPlugin = ret;
1239 ret = _impulseAnalysisPlugin.lock();
1246 PluginInsert::collect_signal_for_analysis (framecnt_t nframes)
1248 // called from outside the audio thread, so this should be safe
1249 // only do audio as analysis is (currently) only for audio plugins
1250 _signal_analysis_inputs.ensure_buffers( DataType::AUDIO, input_streams().n_audio(), nframes);
1251 _signal_analysis_outputs.ensure_buffers( DataType::AUDIO, output_streams().n_audio(), nframes);
1253 _signal_analysis_collected_nframes = 0;
1254 _signal_analysis_collect_nframes_max = nframes;
1257 /** Add a plugin to our list and activate it if we have already been activated */
1259 PluginInsert::add_plugin_with_activation (boost::shared_ptr<Plugin> plugin)
1261 plugin->set_insert_info (this);
1262 _plugins.push_back (plugin);
1264 plugin->activate ();
1268 /** Add a plugin to our list */
1270 PluginInsert::add_plugin (boost::shared_ptr<Plugin> plugin)
1272 plugin->set_insert_info (this);
1273 _plugins.push_back (plugin);
1277 PluginInsert::realtime_handle_transport_stopped ()
1279 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
1280 (*i)->realtime_handle_transport_stopped ();