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, &valid)).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, &valid)).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;
496 /* we have no idea whether the plugin generated silence or not, so mark
497 * all buffers appropriately.
500 bufs.is_silent (false);
504 PluginInsert::set_parameter (Evoral::Parameter param, float val)
506 if (param.type() != PluginAutomation) {
510 /* the others will be set from the event triggered by this */
512 _plugins[0]->set_parameter (param.id(), val);
514 boost::shared_ptr<AutomationControl> ac
515 = boost::dynamic_pointer_cast<AutomationControl>(control(param));
520 warning << "set_parameter called for nonexistant parameter "
521 << EventTypeMap::instance().to_symbol(param) << endmsg;
524 _session.set_dirty();
528 PluginInsert::get_parameter (Evoral::Parameter param)
530 if (param.type() != PluginAutomation) {
533 assert (!_plugins.empty ());
534 return _plugins[0]->get_parameter (param.id());
539 PluginInsert::automation_run (BufferSet& bufs, pframes_t nframes)
541 Evoral::ControlEvent next_event (0, 0.0f);
542 framepos_t now = _session.transport_frame ();
543 framepos_t end = now + nframes;
544 framecnt_t offset = 0;
546 Glib::Mutex::Lock lm (control_lock(), Glib::TRY_LOCK);
549 connect_and_run (bufs, nframes, offset, false);
553 if (!find_next_event (now, end, next_event) || requires_fixed_sized_buffers()) {
555 /* no events have a time within the relevant range */
557 connect_and_run (bufs, nframes, offset, true, now);
563 framecnt_t cnt = min (((framecnt_t) ceil (next_event.when) - now), (framecnt_t) nframes);
565 connect_and_run (bufs, cnt, offset, true, now);
571 if (!find_next_event (now, end, next_event)) {
576 /* cleanup anything that is left to do */
579 connect_and_run (bufs, nframes, offset, true, now);
584 PluginInsert::default_parameter_value (const Evoral::Parameter& param)
586 if (param.type() != PluginAutomation)
589 if (_plugins.empty()) {
590 fatal << _("programming error: ") << X_("PluginInsert::default_parameter_value() called with no plugin")
595 return _plugins[0]->default_value (param.id());
598 boost::shared_ptr<Plugin>
599 PluginInsert::plugin_factory (boost::shared_ptr<Plugin> other)
601 boost::shared_ptr<LadspaPlugin> lp;
603 boost::shared_ptr<LV2Plugin> lv2p;
606 boost::shared_ptr<VSTPlugin> vp;
609 boost::shared_ptr<LXVSTPlugin> lxvp;
611 #ifdef AUDIOUNIT_SUPPORT
612 boost::shared_ptr<AUPlugin> ap;
615 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
616 return boost::shared_ptr<Plugin> (new LadspaPlugin (*lp));
618 } else if ((lv2p = boost::dynamic_pointer_cast<LV2Plugin> (other)) != 0) {
619 return boost::shared_ptr<Plugin> (new LV2Plugin (*lv2p));
622 } else if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (other)) != 0) {
623 return boost::shared_ptr<Plugin> (new VSTPlugin (*vp));
626 } else if ((lxvp = boost::dynamic_pointer_cast<LXVSTPlugin> (other)) != 0) {
627 return boost::shared_ptr<Plugin> (new LXVSTPlugin (*lxvp));
629 #ifdef AUDIOUNIT_SUPPORT
630 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
631 return boost::shared_ptr<Plugin> (new AUPlugin (*ap));
635 fatal << string_compose (_("programming error: %1"),
636 X_("unknown plugin type in PluginInsert::plugin_factory"))
639 return boost::shared_ptr<Plugin> ((Plugin*) 0);
643 PluginInsert::configure_io (ChanCount in, ChanCount out)
645 Match old_match = _match;
647 /* set the matching method and number of plugins that we will use to meet this configuration */
648 _match = private_can_support_io_configuration (in, out);
649 if (set_count (_match.plugins) == false) {
653 /* a signal needs emitting if we start or stop splitting */
654 if (old_match.method != _match.method && (old_match.method == Split || _match.method == Split)) {
655 SplittingChanged (); /* EMIT SIGNAL */
658 /* configure plugins */
659 switch (_match.method) {
662 if (_plugins.front()->configure_io (_plugins.front()->get_info()->n_inputs, out)) {
668 if (_plugins.front()->configure_io (in, out) == false) {
674 // we don't know the analysis window size, so we must work with the
675 // current buffer size here. each request for data fills in these
676 // buffers and the analyser makes sure it gets enough data for the
678 session().ensure_buffer_set (_signal_analysis_inputs, in);
679 //_signal_analysis_inputs.set_count (in);
681 session().ensure_buffer_set (_signal_analysis_outputs, out);
682 //_signal_analysis_outputs.set_count (out);
684 // std::cerr << "set counts to i" << in.n_audio() << "/o" << out.n_audio() << std::endl;
686 return Processor::configure_io (in, out);
689 /** Decide whether this PluginInsert can support a given IO configuration.
690 * To do this, we run through a set of possible solutions in rough order of
693 * @param in Required input channel count.
694 * @param out Filled in with the output channel count if we return true.
695 * @return true if the given IO configuration can be supported.
698 PluginInsert::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
700 return private_can_support_io_configuration (in, out).method != Impossible;
703 /** A private version of can_support_io_configuration which returns the method
704 * by which the configuration can be matched, rather than just whether or not
708 PluginInsert::private_can_support_io_configuration (ChanCount const & in, ChanCount& out) const
710 if (_plugins.front()->reconfigurable_io()) {
711 /* Plugin has flexible I/O, so delegate to it */
712 bool const r = _plugins.front()->can_support_io_configuration (in, out);
714 return Match (Impossible, 0);
717 return Match (Delegate, 1);
720 ChanCount inputs = _plugins[0]->get_info()->n_inputs;
721 ChanCount outputs = _plugins[0]->get_info()->n_outputs;
723 bool no_inputs = true;
724 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
725 if (inputs.get (*t) != 0) {
732 /* no inputs so we can take any input configuration since we throw it away */
734 return Match (NoInputs, 1);
737 /* Plugin inputs match requested inputs exactly */
740 return Match (ExactMatch, 1);
743 /* We may be able to run more than one copy of the plugin within this insert
744 to cope with the insert having more inputs than the plugin.
745 We allow replication only for plugins with either zero or 1 inputs and outputs
746 for every valid data type.
750 bool can_replicate = true;
751 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
753 uint32_t nin = inputs.get (*t);
755 // No inputs of this type
756 if (nin == 0 && in.get(*t) == 0) {
760 if (nin != 1 || outputs.get (*t) != 1) {
761 can_replicate = false;
765 // Potential factor not set yet
767 f = in.get(*t) / nin;
770 // Factor for this type does not match another type, can not replicate
771 if (f != (in.get(*t) / nin)) {
772 can_replicate = false;
778 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
779 out.set (*t, outputs.get(*t) * f);
781 return Match (Replicate, f);
784 /* If the processor has exactly one input of a given type, and
785 the plugin has more, we can feed the single processor input
786 to some or all of the plugin inputs. This is rather
787 special-case-y, but the 1-to-many case is by far the
788 simplest. How do I split thy 2 processor inputs to 3
789 plugin inputs? Let me count the ways ...
792 bool can_split = true;
793 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
795 bool const can_split_type = (in.get (*t) == 1 && inputs.get (*t) > 1);
796 bool const nothing_to_do_for_type = (in.get (*t) == 0 && inputs.get (*t) == 0);
798 if (!can_split_type && !nothing_to_do_for_type) {
805 return Match (Split, 1);
808 /* If the plugin has more inputs than we want, we can `hide' some of them
809 by feeding them silence.
812 bool could_hide = false;
813 bool cannot_hide = false;
814 ChanCount hide_channels;
816 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
817 if (inputs.get(*t) > in.get(*t)) {
818 /* there is potential to hide, since the plugin has more inputs of type t than the insert */
819 hide_channels.set (*t, inputs.get(*t) - in.get(*t));
821 } else if (inputs.get(*t) < in.get(*t)) {
822 /* we definitely cannot hide, since the plugin has fewer inputs of type t than the insert */
827 if (could_hide && !cannot_hide) {
829 return Match (Hide, 1, hide_channels);
832 return Match (Impossible, 0);
836 PluginInsert::get_state ()
842 PluginInsert::state (bool full)
844 XMLNode& node = Processor::state (full);
846 node.add_property("type", _plugins[0]->state_node_name());
847 node.add_property("unique-id", _plugins[0]->unique_id());
848 node.add_property("count", string_compose("%1", _plugins.size()));
849 node.add_child_nocopy (_plugins[0]->get_state());
851 for (Controls::iterator c = controls().begin(); c != controls().end(); ++c) {
852 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> ((*c).second);
854 node.add_child_nocopy (ac->get_state());
862 PluginInsert::set_control_ids (const XMLNode& node, int version)
864 const XMLNodeList& nlist = node.children();
865 XMLNodeConstIterator iter;
866 set<Evoral::Parameter>::const_iterator p;
868 for (iter = nlist.begin(); iter != nlist.end(); ++iter) {
869 if ((*iter)->name() == Controllable::xml_node_name) {
870 const XMLProperty* prop;
872 if ((prop = (*iter)->property (X_("parameter"))) != 0) {
873 uint32_t p = atoi (prop->value());
874 boost::shared_ptr<Evoral::Control> c = control (Evoral::Parameter (PluginAutomation, 0, p));
878 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> (c);
880 ac->set_state (**iter, version);
888 PluginInsert::set_state(const XMLNode& node, int version)
890 XMLNodeList nlist = node.children();
891 XMLNodeIterator niter;
892 XMLPropertyList plist;
893 const XMLProperty *prop;
894 ARDOUR::PluginType type;
896 if ((prop = node.property ("type")) == 0) {
897 error << _("XML node describing plugin is missing the `type' field") << endmsg;
901 if (prop->value() == X_("ladspa") || prop->value() == X_("Ladspa")) { /* handle old school sessions */
902 type = ARDOUR::LADSPA;
903 } else if (prop->value() == X_("lv2")) {
905 } else if (prop->value() == X_("vst")) {
907 } else if (prop->value() == X_("lxvst")) {
908 type = ARDOUR::LXVST;
909 } else if (prop->value() == X_("audiounit")) {
910 type = ARDOUR::AudioUnit;
912 error << string_compose (_("unknown plugin type %1 in plugin insert state"),
918 prop = node.property ("unique-id");
922 /* older sessions contain VST plugins with only an "id" field.
925 if (type == ARDOUR::VST) {
926 prop = node.property ("id");
931 /*There shouldn't be any older sessions with linuxVST support.. but anyway..*/
933 if (type == ARDOUR::LXVST) {
934 prop = node.property ("id");
940 error << _("Plugin has no unique ID field") << endmsg;
945 boost::shared_ptr<Plugin> plugin = find_plugin (_session, prop->value(), type);
948 error << string_compose(
949 _("Found a reference to a plugin (\"%1\") that is unknown.\n"
950 "Perhaps it was removed or moved since it was last used."),
956 // The name of the PluginInsert comes from the plugin, nothing else
957 _name = plugin->get_info()->name;
961 // Processor::set_state() will set this, but too late
962 // for it to be available when setting up plugin
963 // state. We can't call Processor::set_state() until
964 // the plugins themselves are created and added.
968 if (_plugins.empty()) {
969 /* if we are adding the first plugin, we will need to set
970 up automatable controls.
973 create_automatable_parameters ();
974 set_control_ids (node, version);
977 if ((prop = node.property ("count")) != 0) {
978 sscanf (prop->value().c_str(), "%u", &count);
981 if (_plugins.size() != count) {
982 for (uint32_t n = 1; n < count; ++n) {
983 add_plugin (plugin_factory (plugin));
987 Processor::set_state (node, version);
989 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
991 /* find the node with the type-specific node name ("lv2", "ladspa", etc)
992 and set all plugins to the same state.
995 if ((*niter)->name() == plugin->state_node_name()) {
997 plugin->set_state (**niter, version);
999 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
1000 (*i)->set_state (**niter, version);
1007 if (version < 3000) {
1009 /* Only 2.X sessions need a call to set_parameter_state() - in 3.X and above
1010 this is all handled by Automatable
1013 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1014 if ((*niter)->name() == "Redirect") {
1015 /* XXX do we need to tackle placement? i think not (pd; oct 16 2009) */
1016 Processor::set_state (**niter, version);
1021 set_parameter_state_2X (node, version);
1024 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
1028 (*i)->deactivate ();
1036 PluginInsert::set_parameter_state_2X (const XMLNode& node, int version)
1038 XMLNodeList nlist = node.children();
1039 XMLNodeIterator niter;
1041 /* look for port automation node */
1043 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1045 if ((*niter)->name() != port_automation_node_name) {
1051 XMLNodeConstIterator iter;
1056 cnodes = (*niter)->children ("port");
1058 for (iter = cnodes.begin(); iter != cnodes.end(); ++iter){
1062 if ((cprop = child->property("number")) != 0) {
1063 port = cprop->value().c_str();
1065 warning << _("PluginInsert: Auto: no ladspa port number") << endmsg;
1069 sscanf (port, "%" PRIu32, &port_id);
1071 if (port_id >= _plugins[0]->parameter_count()) {
1072 warning << _("PluginInsert: Auto: port id out of range") << endmsg;
1076 boost::shared_ptr<AutomationControl> c = boost::dynamic_pointer_cast<AutomationControl>(
1077 control(Evoral::Parameter(PluginAutomation, 0, port_id), true));
1080 if (!child->children().empty()) {
1081 c->alist()->set_state (*child->children().front(), version);
1083 /* In some cases 2.X saves lists with min_yval and max_yval
1084 being FLT_MIN and FLT_MAX respectively. This causes problems
1085 in A3 because these min/max values are used to compute
1086 where GUI control points should be drawn. If we see such
1087 values, `correct' them to the min/max of the appropriate
1091 float min_y = c->alist()->get_min_y ();
1092 float max_y = c->alist()->get_max_y ();
1094 Plugin::ParameterDescriptor desc;
1095 _plugins.front()->get_parameter_descriptor (port_id, desc);
1097 if (min_y == FLT_MIN) {
1101 if (max_y == FLT_MAX) {
1105 c->alist()->set_yrange (min_y, max_y);
1108 error << string_compose (_("PluginInsert: automatable control %1 not found - ignored"), port_id) << endmsg;
1120 PluginInsert::describe_parameter (Evoral::Parameter param)
1122 if (param.type() != PluginAutomation) {
1123 return Automatable::describe_parameter(param);
1126 return _plugins[0]->describe_parameter (param);
1130 PluginInsert::signal_latency() const
1132 if (_user_latency) {
1133 return _user_latency;
1136 return _plugins[0]->signal_latency ();
1140 PluginInsert::type ()
1142 return plugin()->get_info()->type;
1145 PluginInsert::PluginControl::PluginControl (PluginInsert* p, const Evoral::Parameter ¶m, boost::shared_ptr<AutomationList> list)
1146 : AutomationControl (p->session(), param, list, p->describe_parameter(param))
1149 Plugin::ParameterDescriptor desc;
1150 p->plugin(0)->get_parameter_descriptor (param.id(), desc);
1151 _logarithmic = desc.logarithmic;
1152 _sr_dependent = desc.sr_dependent;
1153 _toggled = desc.toggled;
1156 /** @param val `user' value */
1158 PluginInsert::PluginControl::set_value (double user_val)
1160 /* FIXME: probably should be taking out some lock here.. */
1162 double const plugin_val = user_to_plugin (user_val);
1164 for (Plugins::iterator i = _plugin->_plugins.begin(); i != _plugin->_plugins.end(); ++i) {
1165 (*i)->set_parameter (_list->parameter().id(), plugin_val);
1168 boost::shared_ptr<Plugin> iasp = _plugin->_impulseAnalysisPlugin.lock();
1170 iasp->set_parameter (_list->parameter().id(), plugin_val);
1173 AutomationControl::set_value (user_val);
1177 PluginInsert::PluginControl::user_to_plugin (double val) const
1179 /* no known transformations at this time */
1184 PluginInsert::PluginControl::user_to_ui (double val) const
1198 PluginInsert::PluginControl::ui_to_user (double val) const
1207 /** Convert plugin values to UI values. See pbd/controllable.h */
1209 PluginInsert::PluginControl::plugin_to_ui (double val) const
1211 return user_to_ui (plugin_to_user (val));
1215 PluginInsert::PluginControl::plugin_to_user (double val) const
1217 /* no known transformations at this time */
1222 PluginInsert::PluginControl::get_state ()
1226 XMLNode& node (AutomationControl::get_state());
1227 ss << parameter().id();
1228 node.add_property (X_("parameter"), ss.str());
1233 /** @return `user' val */
1235 PluginInsert::PluginControl::get_value () const
1237 /* FIXME: probably should be taking out some lock here.. */
1239 return plugin_to_user (_plugin->get_parameter (_list->parameter()));
1242 boost::shared_ptr<Plugin>
1243 PluginInsert::get_impulse_analysis_plugin()
1245 boost::shared_ptr<Plugin> ret;
1246 if (_impulseAnalysisPlugin.expired()) {
1247 ret = plugin_factory(_plugins[0]);
1248 _impulseAnalysisPlugin = ret;
1250 ret = _impulseAnalysisPlugin.lock();
1257 PluginInsert::collect_signal_for_analysis (framecnt_t nframes)
1259 // called from outside the audio thread, so this should be safe
1260 // only do audio as analysis is (currently) only for audio plugins
1261 _signal_analysis_inputs.ensure_buffers( DataType::AUDIO, input_streams().n_audio(), nframes);
1262 _signal_analysis_outputs.ensure_buffers( DataType::AUDIO, output_streams().n_audio(), nframes);
1264 _signal_analysis_collected_nframes = 0;
1265 _signal_analysis_collect_nframes_max = nframes;
1268 /** Add a plugin to our list and activate it if we have already been activated */
1270 PluginInsert::add_plugin_with_activation (boost::shared_ptr<Plugin> plugin)
1272 plugin->set_insert_info (this);
1273 _plugins.push_back (plugin);
1275 plugin->activate ();
1279 /** Add a plugin to our list */
1281 PluginInsert::add_plugin (boost::shared_ptr<Plugin> plugin)
1283 plugin->set_insert_info (this);
1284 _plugins.push_back (plugin);
1288 PluginInsert::realtime_handle_transport_stopped ()
1290 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
1291 (*i)->realtime_handle_transport_stopped ();