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"
40 #include "ardour/lv2_plugin.h"
43 #ifdef WINDOWS_VST_SUPPORT
44 #include "ardour/windows_vst_plugin.h"
48 #include "ardour/lxvst_plugin.h"
51 #ifdef AUDIOUNIT_SUPPORT
52 #include "ardour/audio_unit.h"
55 #include "ardour/session.h"
56 #include "ardour/types.h"
61 using namespace ARDOUR;
64 const string PluginInsert::port_automation_node_name = "PortAutomation";
66 PluginInsert::PluginInsert (Session& s, boost::shared_ptr<Plugin> plug)
67 : Processor (s, (plug ? plug->name() : string ("toBeRenamed")))
68 , _signal_analysis_collected_nframes(0)
69 , _signal_analysis_collect_nframes_max(0)
71 /* the first is the master */
75 create_automatable_parameters ();
80 PluginInsert::set_count (uint32_t num)
82 bool require_state = !_plugins.empty();
84 /* this is a bad idea.... we shouldn't do this while active.
85 only a route holding their redirect_lock should be calling this
90 } else if (num > _plugins.size()) {
91 uint32_t diff = num - _plugins.size();
93 for (uint32_t n = 0; n < diff; ++n) {
94 boost::shared_ptr<Plugin> p = plugin_factory (_plugins[0]);
101 /* XXX do something */
105 } else if (num < _plugins.size()) {
106 uint32_t diff = _plugins.size() - num;
107 for (uint32_t n= 0; n < diff; ++n) {
115 PluginInsert::~PluginInsert ()
120 PluginInsert::control_list_automation_state_changed (Evoral::Parameter which, AutoState s)
122 if (which.type() != PluginAutomation)
125 boost::shared_ptr<AutomationControl> c
126 = boost::dynamic_pointer_cast<AutomationControl>(control (which));
129 _plugins[0]->set_parameter (which.id(), c->list()->eval (_session.transport_frame()));
134 PluginInsert::output_streams() const
136 assert (!_plugins.empty());
138 PluginInfoPtr info = _plugins.front()->get_info();
140 if (info->reconfigurable_io()) {
141 ChanCount out = _plugins.front()->output_streams ();
142 // DEBUG_TRACE (DEBUG::Processors, string_compose ("Plugin insert, reconfigur(able) output streams = %1\n", out));
145 ChanCount out = info->n_outputs;
146 // DEBUG_TRACE (DEBUG::Processors, string_compose ("Plugin insert, static output streams = %1 for %2 plugins\n", out, _plugins.size()));
147 out.set_audio (out.n_audio() * _plugins.size());
148 out.set_midi (out.n_midi() * _plugins.size() + midi_bypass.n_midi());
154 PluginInsert::input_streams() const
156 assert (!_plugins.empty());
160 PluginInfoPtr info = _plugins.front()->get_info();
162 if (info->reconfigurable_io()) {
163 assert (_plugins.size() == 1);
164 in = _plugins.front()->input_streams();
169 DEBUG_TRACE (DEBUG::Processors, string_compose ("Plugin insert, input streams = %1, match using %2\n", in, _match.method));
171 if (_match.method == Split) {
173 /* we are splitting 1 processor input to multiple plugin inputs,
174 so we have a maximum of 1 stream of each type.
176 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
177 if (in.get (*t) > 1) {
183 } else if (_match.method == Hide) {
185 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
186 in.set (*t, in.get (*t) - _match.hide.get (*t));
192 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
193 in.set (*t, in.get (*t) * _plugins.size ());
201 PluginInsert::natural_output_streams() const
203 return _plugins[0]->get_info()->n_outputs;
207 PluginInsert::natural_input_streams() const
209 return _plugins[0]->get_info()->n_inputs;
213 PluginInsert::has_no_inputs() const
215 return _plugins[0]->get_info()->n_inputs == ChanCount::ZERO;
219 PluginInsert::has_no_audio_inputs() const
221 return _plugins[0]->get_info()->n_inputs.n_audio() == 0;
225 PluginInsert::is_midi_instrument() const
227 /* XXX more finesse is possible here. VST plugins have a
228 a specific "instrument" flag, for example.
230 PluginInfoPtr pi = _plugins[0]->get_info();
232 return pi->n_inputs.n_midi() != 0 &&
233 pi->n_outputs.n_audio() > 0;
237 PluginInsert::create_automatable_parameters ()
239 assert (!_plugins.empty());
241 set<Evoral::Parameter> a = _plugins.front()->automatable ();
243 for (set<Evoral::Parameter>::iterator i = a.begin(); i != a.end(); ++i) {
244 if (i->type() == PluginAutomation) {
246 Evoral::Parameter param(*i);
248 ParameterDescriptor desc;
249 _plugins.front()->get_parameter_descriptor(i->id(), desc);
251 /* the Parameter belonging to the actual plugin doesn't have its range set
252 but we want the Controllable related to this Parameter to have those limits.
255 param.set_range (desc.lower, desc.upper, _plugins.front()->default_value(i->id()), desc.toggled);
256 can_automate (param);
257 boost::shared_ptr<AutomationList> list(new AutomationList(param));
258 add_control (boost::shared_ptr<AutomationControl> (new PluginControl(this, param, desc, list)));
259 } else if (i->type() == PluginPropertyAutomation) {
260 Evoral::Parameter param(*i);
261 const ParameterDescriptor& desc = _plugins.front()->get_property_descriptor(param.id());
262 if (desc.datatype != Variant::VOID) {
263 boost::shared_ptr<AutomationList> list;
264 if (Variant::type_is_numeric(desc.datatype)) {
265 list = boost::shared_ptr<AutomationList>(new AutomationList(param));
267 add_control (boost::shared_ptr<AutomationControl> (new PluginPropertyControl(this, param, desc, list)));
274 PluginInsert::parameter_changed (uint32_t which, float val)
276 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter (PluginAutomation, 0, which));
281 Plugins::iterator i = _plugins.begin();
283 /* don't set the first plugin, just all the slaves */
285 if (i != _plugins.end()) {
287 for (; i != _plugins.end(); ++i) {
288 (*i)->set_parameter (which, val);
295 PluginInsert::set_block_size (pframes_t nframes)
298 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
299 if ((*i)->set_block_size (nframes) != 0) {
307 PluginInsert::activate ()
309 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
313 Processor::activate ();
317 PluginInsert::deactivate ()
319 Processor::deactivate ();
321 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
327 PluginInsert::flush ()
329 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
335 PluginInsert::connect_and_run (BufferSet& bufs, pframes_t nframes, framecnt_t offset, bool with_auto, framepos_t now)
337 // Calculate if, and how many frames we need to collect for analysis
338 framecnt_t collect_signal_nframes = (_signal_analysis_collect_nframes_max -
339 _signal_analysis_collected_nframes);
340 if (nframes < collect_signal_nframes) { // we might not get all frames now
341 collect_signal_nframes = nframes;
344 ChanCount const in_streams = input_streams ();
345 ChanCount const out_streams = output_streams ();
347 ChanMapping in_map (in_streams);
348 ChanMapping out_map (out_streams);
350 if (_match.method == Split) {
351 /* fix the input mapping so that we have maps for each of the plugin's inputs */
352 in_map = ChanMapping (natural_input_streams ());
354 /* copy the first stream's buffer contents to the others */
355 /* XXX: audio only */
356 uint32_t first_idx = in_map.get (DataType::AUDIO, 0, &valid);
358 for (uint32_t i = in_streams.n_audio(); i < natural_input_streams().n_audio(); ++i) {
359 bufs.get_audio(in_map.get (DataType::AUDIO, i, &valid)).read_from(bufs.get_audio(first_idx), nframes, offset, offset);
364 bufs.set_count(ChanCount::max(bufs.count(), in_streams));
365 bufs.set_count(ChanCount::max(bufs.count(), out_streams));
367 /* Note that we've already required that plugins
368 be able to handle in-place processing.
375 for (Controls::iterator li = controls().begin(); li != controls().end(); ++li, ++n) {
377 boost::shared_ptr<AutomationControl> c
378 = boost::dynamic_pointer_cast<AutomationControl>(li->second);
380 if (c->list() && c->automation_playback()) {
383 const float val = c->list()->rt_safe_eval (now, valid);
393 if (collect_signal_nframes > 0) {
395 //std::cerr << "collect input, bufs " << bufs.count().n_audio() << " count, " << bufs.available().n_audio() << " available" << std::endl;
396 //std::cerr << " streams " << input_streams().n_audio() << std::endl;
397 //std::cerr << "filling buffer with " << collect_signal_nframes << " frames at " << _signal_analysis_collected_nframes << std::endl;
399 _signal_analysis_inputs.set_count(input_streams());
401 for (uint32_t i = 0; i < input_streams().n_audio(); ++i) {
402 _signal_analysis_inputs.get_audio(i).read_from(
404 collect_signal_nframes,
405 _signal_analysis_collected_nframes); // offset is for target buffer
410 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
411 (*i)->connect_and_run(bufs, in_map, out_map, nframes, offset);
412 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
413 in_map.offset_to(*t, natural_input_streams().get(*t));
414 out_map.offset_to(*t, natural_output_streams().get(*t));
418 if (collect_signal_nframes > 0) {
420 //std::cerr << " output, bufs " << bufs.count().n_audio() << " count, " << bufs.available().n_audio() << " available" << std::endl;
421 //std::cerr << " streams " << output_streams().n_audio() << std::endl;
423 _signal_analysis_outputs.set_count(output_streams());
425 for (uint32_t i = 0; i < output_streams().n_audio(); ++i) {
426 _signal_analysis_outputs.get_audio(i).read_from(
428 collect_signal_nframes,
429 _signal_analysis_collected_nframes); // offset is for target buffer
432 _signal_analysis_collected_nframes += collect_signal_nframes;
433 assert(_signal_analysis_collected_nframes <= _signal_analysis_collect_nframes_max);
435 if (_signal_analysis_collected_nframes == _signal_analysis_collect_nframes_max) {
436 _signal_analysis_collect_nframes_max = 0;
437 _signal_analysis_collected_nframes = 0;
439 AnalysisDataGathered(&_signal_analysis_inputs,
440 &_signal_analysis_outputs);
443 /* leave remaining channel buffers alone */
447 PluginInsert::silence (framecnt_t nframes)
453 ChanMapping in_map(input_streams());
454 ChanMapping out_map(output_streams());
456 if (_match.method == Split) {
457 /* fix the input mapping so that we have maps for each of the plugin's inputs */
458 in_map = ChanMapping (natural_input_streams ());
461 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
462 (*i)->connect_and_run (_session.get_scratch_buffers ((*i)->get_info()->n_inputs, true), in_map, out_map, nframes, 0);
467 PluginInsert::run (BufferSet& bufs, framepos_t start_frame, framepos_t /*end_frame*/, pframes_t nframes, bool)
469 if (_pending_active) {
470 /* run as normal if we are active or moving from inactive to active */
472 if (_session.transport_rolling() || _session.bounce_processing()) {
473 automation_run (bufs, start_frame, nframes);
475 connect_and_run (bufs, nframes, 0, false);
479 uint32_t in = input_streams ().n_audio ();
480 uint32_t out = output_streams().n_audio ();
482 if (has_no_audio_inputs() || in == 0) {
484 /* silence all (audio) outputs. Should really declick
485 * at the transitions of "active"
488 for (uint32_t n = 0; n < out; ++n) {
489 bufs.get_audio (n).silence (nframes);
492 } else if (out > in) {
494 /* not active, but something has make up for any channel count increase */
496 // TODO: option round-robin (n % in) or silence additional buffers ??
497 // for now , simply replicate last buffer
498 for (uint32_t n = in; n < out; ++n) {
499 bufs.get_audio(n).read_from(bufs.get_audio(in - 1), nframes);
503 bufs.count().set_audio (out);
506 _active = _pending_active;
508 /* we have no idea whether the plugin generated silence or not, so mark
509 * all buffers appropriately.
515 PluginInsert::set_parameter (Evoral::Parameter param, float val)
517 if (param.type() != PluginAutomation) {
521 /* the others will be set from the event triggered by this */
523 _plugins[0]->set_parameter (param.id(), val);
525 boost::shared_ptr<AutomationControl> ac
526 = boost::dynamic_pointer_cast<AutomationControl>(control(param));
531 warning << "set_parameter called for nonexistant parameter "
532 << EventTypeMap::instance().to_symbol(param) << endmsg;
535 _session.set_dirty();
539 PluginInsert::get_parameter (Evoral::Parameter param)
541 if (param.type() != PluginAutomation) {
544 assert (!_plugins.empty ());
545 return _plugins[0]->get_parameter (param.id());
550 PluginInsert::automation_run (BufferSet& bufs, framepos_t start, pframes_t nframes)
552 Evoral::ControlEvent next_event (0, 0.0f);
553 framepos_t now = start;
554 framepos_t end = now + nframes;
555 framecnt_t offset = 0;
557 Glib::Threads::Mutex::Lock lm (control_lock(), Glib::Threads::TRY_LOCK);
560 connect_and_run (bufs, nframes, offset, false);
564 if (!find_next_event (now, end, next_event) || requires_fixed_sized_buffers()) {
566 /* no events have a time within the relevant range */
568 connect_and_run (bufs, nframes, offset, true, now);
574 framecnt_t cnt = min (((framecnt_t) ceil (next_event.when) - now), (framecnt_t) nframes);
576 connect_and_run (bufs, cnt, offset, true, now);
582 if (!find_next_event (now, end, next_event)) {
587 /* cleanup anything that is left to do */
590 connect_and_run (bufs, nframes, offset, true, now);
595 PluginInsert::default_parameter_value (const Evoral::Parameter& param)
597 if (param.type() != PluginAutomation)
600 if (_plugins.empty()) {
601 fatal << _("programming error: ") << X_("PluginInsert::default_parameter_value() called with no plugin")
606 return _plugins[0]->default_value (param.id());
609 boost::shared_ptr<Plugin>
610 PluginInsert::plugin_factory (boost::shared_ptr<Plugin> other)
612 boost::shared_ptr<LadspaPlugin> lp;
614 boost::shared_ptr<LV2Plugin> lv2p;
616 #ifdef WINDOWS_VST_SUPPORT
617 boost::shared_ptr<WindowsVSTPlugin> vp;
620 boost::shared_ptr<LXVSTPlugin> lxvp;
622 #ifdef AUDIOUNIT_SUPPORT
623 boost::shared_ptr<AUPlugin> ap;
626 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
627 return boost::shared_ptr<Plugin> (new LadspaPlugin (*lp));
629 } else if ((lv2p = boost::dynamic_pointer_cast<LV2Plugin> (other)) != 0) {
630 return boost::shared_ptr<Plugin> (new LV2Plugin (*lv2p));
632 #ifdef WINDOWS_VST_SUPPORT
633 } else if ((vp = boost::dynamic_pointer_cast<WindowsVSTPlugin> (other)) != 0) {
634 return boost::shared_ptr<Plugin> (new WindowsVSTPlugin (*vp));
637 } else if ((lxvp = boost::dynamic_pointer_cast<LXVSTPlugin> (other)) != 0) {
638 return boost::shared_ptr<Plugin> (new LXVSTPlugin (*lxvp));
640 #ifdef AUDIOUNIT_SUPPORT
641 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
642 return boost::shared_ptr<Plugin> (new AUPlugin (*ap));
646 fatal << string_compose (_("programming error: %1"),
647 X_("unknown plugin type in PluginInsert::plugin_factory"))
650 return boost::shared_ptr<Plugin> ((Plugin*) 0);
654 PluginInsert::configure_io (ChanCount in, ChanCount out)
656 Match old_match = _match;
657 ChanCount old_in = input_streams ();
658 ChanCount old_out = output_streams ();
660 /* set the matching method and number of plugins that we will use to meet this configuration */
661 _match = private_can_support_io_configuration (in, out);
662 if (set_count (_match.plugins) == false) {
666 if ( (old_match.method != _match.method && (old_match.method == Split || _match.method == Split))
671 PluginIoReConfigure (); /* EMIT SIGNAL */
674 /* configure plugins */
675 switch (_match.method) {
678 if (_plugins.front()->configure_io (_plugins.front()->get_info()->n_inputs, out)) {
684 if (_plugins.front()->configure_io (in, out) == false) {
690 // we don't know the analysis window size, so we must work with the
691 // current buffer size here. each request for data fills in these
692 // buffers and the analyser makes sure it gets enough data for the
694 session().ensure_buffer_set (_signal_analysis_inputs, in);
695 //_signal_analysis_inputs.set_count (in);
697 session().ensure_buffer_set (_signal_analysis_outputs, out);
698 //_signal_analysis_outputs.set_count (out);
700 // std::cerr << "set counts to i" << in.n_audio() << "/o" << out.n_audio() << std::endl;
702 return Processor::configure_io (in, out);
705 /** Decide whether this PluginInsert can support a given IO configuration.
706 * To do this, we run through a set of possible solutions in rough order of
709 * @param in Required input channel count.
710 * @param out Filled in with the output channel count if we return true.
711 * @return true if the given IO configuration can be supported.
714 PluginInsert::can_support_io_configuration (const ChanCount& in, ChanCount& out)
716 return private_can_support_io_configuration (in, out).method != Impossible;
719 /** A private version of can_support_io_configuration which returns the method
720 * by which the configuration can be matched, rather than just whether or not
724 PluginInsert::private_can_support_io_configuration (ChanCount const & inx, ChanCount& out)
726 if (_plugins.empty()) {
730 PluginInfoPtr info = _plugins.front()->get_info();
731 ChanCount in; in += inx;
734 if (info->reconfigurable_io()) {
735 /* Plugin has flexible I/O, so delegate to it */
736 bool const r = _plugins.front()->can_support_io_configuration (in, out);
738 return Match (Impossible, 0);
741 return Match (Delegate, 1);
744 ChanCount inputs = info->n_inputs;
745 ChanCount outputs = info->n_outputs;
747 if (in.get(DataType::MIDI) == 1 && outputs.get(DataType::MIDI) == 0) {
748 DEBUG_TRACE ( DEBUG::Processors, string_compose ("bypassing midi-data around %1\n", name()));
749 midi_bypass.set(DataType::MIDI, 1);
751 if (in.get(DataType::MIDI) == 1 && inputs.get(DataType::MIDI) == 0) {
752 DEBUG_TRACE ( DEBUG::Processors, string_compose ("hiding midi-port from plugin %1\n", name()));
753 in.set(DataType::MIDI, 0);
756 bool no_inputs = true;
757 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
758 if (inputs.get (*t) != 0) {
765 /* no inputs so we can take any input configuration since we throw it away */
766 out = outputs + midi_bypass;
767 return Match (NoInputs, 1);
770 /* Plugin inputs match requested inputs exactly */
772 out = outputs + midi_bypass;
773 return Match (ExactMatch, 1);
776 /* We may be able to run more than one copy of the plugin within this insert
777 to cope with the insert having more inputs than the plugin.
778 We allow replication only for plugins with either zero or 1 inputs and outputs
779 for every valid data type.
783 bool can_replicate = true;
784 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
786 uint32_t nin = inputs.get (*t);
788 // No inputs of this type
789 if (nin == 0 && in.get(*t) == 0) {
793 if (nin != 1 || outputs.get (*t) != 1) {
794 can_replicate = false;
798 // Potential factor not set yet
800 f = in.get(*t) / nin;
803 // Factor for this type does not match another type, can not replicate
804 if (f != (in.get(*t) / nin)) {
805 can_replicate = false;
811 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
812 out.set (*t, outputs.get(*t) * f);
815 return Match (Replicate, f);
818 /* If the processor has exactly one input of a given type, and
819 the plugin has more, we can feed the single processor input
820 to some or all of the plugin inputs. This is rather
821 special-case-y, but the 1-to-many case is by far the
822 simplest. How do I split thy 2 processor inputs to 3
823 plugin inputs? Let me count the ways ...
826 bool can_split = true;
827 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
829 bool const can_split_type = (in.get (*t) == 1 && inputs.get (*t) > 1);
830 bool const nothing_to_do_for_type = (in.get (*t) == 0 && inputs.get (*t) == 0);
832 if (!can_split_type && !nothing_to_do_for_type) {
838 out = outputs + midi_bypass;
839 return Match (Split, 1);
842 /* If the plugin has more inputs than we want, we can `hide' some of them
843 by feeding them silence.
846 bool could_hide = false;
847 bool cannot_hide = false;
848 ChanCount hide_channels;
850 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
851 if (inputs.get(*t) > in.get(*t)) {
852 /* there is potential to hide, since the plugin has more inputs of type t than the insert */
853 hide_channels.set (*t, inputs.get(*t) - in.get(*t));
855 } else if (inputs.get(*t) < in.get(*t)) {
856 /* we definitely cannot hide, since the plugin has fewer inputs of type t than the insert */
861 if (could_hide && !cannot_hide) {
862 out = outputs + midi_bypass;
863 return Match (Hide, 1, hide_channels);
867 return Match (Impossible, 0);
871 PluginInsert::get_state ()
877 PluginInsert::state (bool full)
879 XMLNode& node = Processor::state (full);
881 node.add_property("type", _plugins[0]->state_node_name());
882 node.add_property("unique-id", _plugins[0]->unique_id());
883 node.add_property("count", string_compose("%1", _plugins.size()));
884 node.add_child_nocopy (_plugins[0]->get_state());
886 for (Controls::iterator c = controls().begin(); c != controls().end(); ++c) {
887 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> ((*c).second);
889 node.add_child_nocopy (ac->get_state());
897 PluginInsert::set_control_ids (const XMLNode& node, int version)
899 const XMLNodeList& nlist = node.children();
900 XMLNodeConstIterator iter;
901 set<Evoral::Parameter>::const_iterator p;
903 for (iter = nlist.begin(); iter != nlist.end(); ++iter) {
904 if ((*iter)->name() == Controllable::xml_node_name) {
905 const XMLProperty* prop;
907 if ((prop = (*iter)->property (X_("parameter"))) != 0) {
908 uint32_t p = atoi (prop->value());
910 /* this may create the new controllable */
912 boost::shared_ptr<Evoral::Control> c = control (Evoral::Parameter (PluginAutomation, 0, p));
914 #ifndef NO_PLUGIN_STATE
918 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> (c);
920 ac->set_state (**iter, version);
929 PluginInsert::set_state(const XMLNode& node, int version)
931 XMLNodeList nlist = node.children();
932 XMLNodeIterator niter;
933 XMLPropertyList plist;
934 const XMLProperty *prop;
935 ARDOUR::PluginType type;
937 if ((prop = node.property ("type")) == 0) {
938 error << _("XML node describing plugin is missing the `type' field") << endmsg;
942 if (prop->value() == X_("ladspa") || prop->value() == X_("Ladspa")) { /* handle old school sessions */
943 type = ARDOUR::LADSPA;
944 } else if (prop->value() == X_("lv2")) {
946 } else if (prop->value() == X_("windows-vst")) {
947 type = ARDOUR::Windows_VST;
948 } else if (prop->value() == X_("lxvst")) {
949 type = ARDOUR::LXVST;
950 } else if (prop->value() == X_("audiounit")) {
951 type = ARDOUR::AudioUnit;
953 error << string_compose (_("unknown plugin type %1 in plugin insert state"),
959 prop = node.property ("unique-id");
962 #ifdef WINDOWS_VST_SUPPORT
963 /* older sessions contain VST plugins with only an "id" field.
966 if (type == ARDOUR::Windows_VST) {
967 prop = node.property ("id");
972 /*There shouldn't be any older sessions with linuxVST support.. but anyway..*/
974 if (type == ARDOUR::LXVST) {
975 prop = node.property ("id");
981 error << _("Plugin has no unique ID field") << endmsg;
986 boost::shared_ptr<Plugin> plugin = find_plugin (_session, prop->value(), type);
988 /* treat linux and windows VST plugins equivalent if they have the same uniqueID
989 * allow to move sessions windows <> linux */
991 if (plugin == 0 && type == ARDOUR::Windows_VST) {
992 type = ARDOUR::LXVST;
993 plugin = find_plugin (_session, prop->value(), type);
997 #ifdef WINDOWS_VST_SUPPORT
998 if (plugin == 0 && type == ARDOUR::LXVST) {
999 type = ARDOUR::Windows_VST;
1000 plugin = find_plugin (_session, prop->value(), type);
1005 error << string_compose(
1006 _("Found a reference to a plugin (\"%1\") that is unknown.\n"
1007 "Perhaps it was removed or moved since it was last used."),
1013 // The name of the PluginInsert comes from the plugin, nothing else
1014 _name = plugin->get_info()->name;
1018 // Processor::set_state() will set this, but too late
1019 // for it to be available when setting up plugin
1020 // state. We can't call Processor::set_state() until
1021 // the plugins themselves are created and added.
1025 if (_plugins.empty()) {
1026 /* if we are adding the first plugin, we will need to set
1027 up automatable controls.
1029 add_plugin (plugin);
1030 create_automatable_parameters ();
1031 set_control_ids (node, version);
1034 if ((prop = node.property ("count")) != 0) {
1035 sscanf (prop->value().c_str(), "%u", &count);
1038 if (_plugins.size() != count) {
1039 for (uint32_t n = 1; n < count; ++n) {
1040 add_plugin (plugin_factory (plugin));
1044 Processor::set_state (node, version);
1046 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1048 /* find the node with the type-specific node name ("lv2", "ladspa", etc)
1049 and set all plugins to the same state.
1052 if ((*niter)->name() == plugin->state_node_name()) {
1054 plugin->set_state (**niter, version);
1056 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
1057 (*i)->set_state (**niter, version);
1064 if (version < 3000) {
1066 /* Only 2.X sessions need a call to set_parameter_state() - in 3.X and above
1067 this is all handled by Automatable
1070 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1071 if ((*niter)->name() == "Redirect") {
1072 /* XXX do we need to tackle placement? i think not (pd; oct 16 2009) */
1073 Processor::set_state (**niter, version);
1078 set_parameter_state_2X (node, version);
1081 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
1085 (*i)->deactivate ();
1093 PluginInsert::set_parameter_state_2X (const XMLNode& node, int version)
1095 XMLNodeList nlist = node.children();
1096 XMLNodeIterator niter;
1098 /* look for port automation node */
1100 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1102 if ((*niter)->name() != port_automation_node_name) {
1108 XMLNodeConstIterator iter;
1113 cnodes = (*niter)->children ("port");
1115 for (iter = cnodes.begin(); iter != cnodes.end(); ++iter){
1119 if ((cprop = child->property("number")) != 0) {
1120 port = cprop->value().c_str();
1122 warning << _("PluginInsert: Auto: no ladspa port number") << endmsg;
1126 sscanf (port, "%" PRIu32, &port_id);
1128 if (port_id >= _plugins[0]->parameter_count()) {
1129 warning << _("PluginInsert: Auto: port id out of range") << endmsg;
1133 boost::shared_ptr<AutomationControl> c = boost::dynamic_pointer_cast<AutomationControl>(
1134 control(Evoral::Parameter(PluginAutomation, 0, port_id), true));
1137 if (!child->children().empty()) {
1138 c->alist()->set_state (*child->children().front(), version);
1140 /* In some cases 2.X saves lists with min_yval and max_yval
1141 being FLT_MIN and FLT_MAX respectively. This causes problems
1142 in A3 because these min/max values are used to compute
1143 where GUI control points should be drawn. If we see such
1144 values, `correct' them to the min/max of the appropriate
1148 float min_y = c->alist()->get_min_y ();
1149 float max_y = c->alist()->get_max_y ();
1151 ParameterDescriptor desc;
1152 _plugins.front()->get_parameter_descriptor (port_id, desc);
1154 if (min_y == FLT_MIN) {
1158 if (max_y == FLT_MAX) {
1162 c->alist()->set_yrange (min_y, max_y);
1165 error << string_compose (_("PluginInsert: automatable control %1 not found - ignored"), port_id) << endmsg;
1177 PluginInsert::describe_parameter (Evoral::Parameter param)
1179 if (param.type() == PluginAutomation) {
1180 return _plugins[0]->describe_parameter (param);
1181 } else if (param.type() == PluginPropertyAutomation) {
1182 boost::shared_ptr<AutomationControl> c(automation_control(param));
1183 if (c && !c->desc().label.empty()) {
1184 return c->desc().label;
1187 return Automatable::describe_parameter(param);
1191 PluginInsert::signal_latency() const
1193 if (_user_latency) {
1194 return _user_latency;
1197 return _plugins[0]->signal_latency ();
1201 PluginInsert::type ()
1203 return plugin()->get_info()->type;
1206 PluginInsert::PluginControl::PluginControl (PluginInsert* p,
1207 const Evoral::Parameter& param,
1208 const ParameterDescriptor& desc,
1209 boost::shared_ptr<AutomationList> list)
1210 : AutomationControl (p->session(), param, desc, list, p->describe_parameter(param))
1214 alist()->reset_default (desc.normal);
1218 set_flags(Controllable::Toggle);
1222 /** @param val `user' value */
1224 PluginInsert::PluginControl::set_value (double user_val)
1226 /* FIXME: probably should be taking out some lock here.. */
1228 for (Plugins::iterator i = _plugin->_plugins.begin(); i != _plugin->_plugins.end(); ++i) {
1229 (*i)->set_parameter (_list->parameter().id(), user_val);
1232 boost::shared_ptr<Plugin> iasp = _plugin->_impulseAnalysisPlugin.lock();
1234 iasp->set_parameter (_list->parameter().id(), user_val);
1237 AutomationControl::set_value (user_val);
1241 PluginInsert::PluginControl::internal_to_interface (double val) const
1243 val = Controllable::internal_to_interface(val);
1245 if (_desc.logarithmic) {
1247 val = pow (val, 1/1.5);
1257 PluginInsert::PluginControl::interface_to_internal (double val) const
1259 if (_desc.logarithmic) {
1263 val = pow (val, 1.5);
1267 val = Controllable::interface_to_internal(val);
1273 PluginInsert::PluginControl::get_state ()
1277 XMLNode& node (AutomationControl::get_state());
1278 ss << parameter().id();
1279 node.add_property (X_("parameter"), ss.str());
1284 /** @return `user' val */
1286 PluginInsert::PluginControl::get_value () const
1288 /* FIXME: probably should be taking out some lock here.. */
1289 return _plugin->get_parameter (_list->parameter());
1292 PluginInsert::PluginPropertyControl::PluginPropertyControl (PluginInsert* p,
1293 const Evoral::Parameter& param,
1294 const ParameterDescriptor& desc,
1295 boost::shared_ptr<AutomationList> list)
1296 : AutomationControl (p->session(), param, desc, list)
1300 alist()->set_yrange (desc.lower, desc.upper);
1301 alist()->reset_default (desc.normal);
1305 set_flags(Controllable::Toggle);
1310 PluginInsert::PluginPropertyControl::set_value (double user_val)
1312 /* Old numeric set_value(), coerce to appropriate datatype if possible.
1313 This is lossy, but better than nothing until Ardour's automation system
1314 can handle various datatypes all the way down. */
1315 const Variant value(_desc.datatype, user_val);
1316 if (value.type() == Variant::VOID) {
1317 error << "set_value(double) called for non-numeric property" << endmsg;
1321 for (Plugins::iterator i = _plugin->_plugins.begin(); i != _plugin->_plugins.end(); ++i) {
1322 (*i)->set_property(_list->parameter().id(), value);
1326 AutomationControl::set_value(user_val);
1330 PluginInsert::PluginPropertyControl::get_state ()
1334 XMLNode& node (AutomationControl::get_state());
1335 ss << parameter().id();
1336 node.add_property (X_("property"), ss.str());
1337 node.remove_property (X_("value"));
1343 PluginInsert::PluginPropertyControl::get_value () const
1345 return _value.to_double();
1348 boost::shared_ptr<Plugin>
1349 PluginInsert::get_impulse_analysis_plugin()
1351 boost::shared_ptr<Plugin> ret;
1352 if (_impulseAnalysisPlugin.expired()) {
1353 ret = plugin_factory(_plugins[0]);
1354 _impulseAnalysisPlugin = ret;
1356 ret = _impulseAnalysisPlugin.lock();
1363 PluginInsert::collect_signal_for_analysis (framecnt_t nframes)
1365 // called from outside the audio thread, so this should be safe
1366 // only do audio as analysis is (currently) only for audio plugins
1367 _signal_analysis_inputs.ensure_buffers( DataType::AUDIO, input_streams().n_audio(), nframes);
1368 _signal_analysis_outputs.ensure_buffers( DataType::AUDIO, output_streams().n_audio(), nframes);
1370 _signal_analysis_collected_nframes = 0;
1371 _signal_analysis_collect_nframes_max = nframes;
1374 /** Add a plugin to our list */
1376 PluginInsert::add_plugin (boost::shared_ptr<Plugin> plugin)
1378 plugin->set_insert_id (this->id());
1380 if (_plugins.empty()) {
1381 /* first (and probably only) plugin instance - connect to relevant signals
1384 plugin->ParameterChanged.connect_same_thread (*this, boost::bind (&PluginInsert::parameter_changed, this, _1, _2));
1385 plugin->StartTouch.connect_same_thread (*this, boost::bind (&PluginInsert::start_touch, this, _1));
1386 plugin->EndTouch.connect_same_thread (*this, boost::bind (&PluginInsert::end_touch, this, _1));
1389 _plugins.push_back (plugin);
1393 PluginInsert::realtime_handle_transport_stopped ()
1395 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
1396 (*i)->realtime_handle_transport_stopped ();
1401 PluginInsert::realtime_locate ()
1403 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
1404 (*i)->realtime_locate ();
1409 PluginInsert::monitoring_changed ()
1411 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
1412 (*i)->monitoring_changed ();
1417 PluginInsert::start_touch (uint32_t param_id)
1419 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter (PluginAutomation, 0, param_id));
1421 ac->start_touch (session().audible_frame());
1426 PluginInsert::end_touch (uint32_t param_id)
1428 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter (PluginAutomation, 0, param_id));
1430 ac->stop_touch (true, session().audible_frame());