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/luaproc.h"
37 #include "ardour/plugin.h"
38 #include "ardour/plugin_insert.h"
41 #include "ardour/lv2_plugin.h"
44 #ifdef WINDOWS_VST_SUPPORT
45 #include "ardour/windows_vst_plugin.h"
49 #include "ardour/lxvst_plugin.h"
52 #ifdef AUDIOUNIT_SUPPORT
53 #include "ardour/audio_unit.h"
56 #include "ardour/session.h"
57 #include "ardour/types.h"
62 using namespace ARDOUR;
65 const string PluginInsert::port_automation_node_name = "PortAutomation";
67 PluginInsert::PluginInsert (Session& s, boost::shared_ptr<Plugin> plug)
68 : Processor (s, (plug ? plug->name() : string ("toBeRenamed")))
69 , _signal_analysis_collected_nframes(0)
70 , _signal_analysis_collect_nframes_max(0)
75 , _pending_no_inplace (false)
77 /* the first is the master */
81 create_automatable_parameters ();
85 PluginInsert::~PluginInsert ()
90 PluginInsert::set_count (uint32_t num)
92 bool require_state = !_plugins.empty();
94 /* this is a bad idea.... we shouldn't do this while active.
95 only a route holding their redirect_lock should be calling this
100 } else if (num > _plugins.size()) {
101 uint32_t diff = num - _plugins.size();
103 for (uint32_t n = 0; n < diff; ++n) {
104 boost::shared_ptr<Plugin> p = plugin_factory (_plugins[0]);
111 /* XXX do something */
115 } else if (num < _plugins.size()) {
116 uint32_t diff = _plugins.size() - num;
117 for (uint32_t n= 0; n < diff; ++n) {
127 PluginInsert::set_outputs (const ChanCount& c)
133 PluginInsert::control_list_automation_state_changed (Evoral::Parameter which, AutoState s)
135 if (which.type() != PluginAutomation)
138 boost::shared_ptr<AutomationControl> c
139 = boost::dynamic_pointer_cast<AutomationControl>(control (which));
142 _plugins[0]->set_parameter (which.id(), c->list()->eval (_session.transport_frame()));
147 PluginInsert::output_streams() const
149 assert (_configured);
150 return _configured_out;
154 PluginInsert::input_streams() const
156 assert (_configured);
157 return _configured_in;
161 PluginInsert::internal_output_streams() const
163 assert (!_plugins.empty());
165 PluginInfoPtr info = _plugins.front()->get_info();
167 if (_match.strict_io) {
168 return _configured_in; // XXX
170 else if (info->reconfigurable_io()) {
171 ChanCount out = _plugins.front()->output_streams ();
172 // DEBUG_TRACE (DEBUG::Processors, string_compose ("Plugin insert, reconfigur(able) output streams = %1\n", out));
175 ChanCount out = info->n_outputs;
176 // DEBUG_TRACE (DEBUG::Processors, string_compose ("Plugin insert, static output streams = %1 for %2 plugins\n", out, _plugins.size()));
177 out.set_audio (out.n_audio() * _plugins.size());
178 out.set_midi (out.n_midi() * _plugins.size() + midi_bypass.n_midi());
184 PluginInsert::internal_input_streams() const
186 assert (!_plugins.empty());
190 PluginInfoPtr info = _plugins.front()->get_info();
192 if (info->reconfigurable_io()) {
193 assert (_plugins.size() == 1);
194 in = _plugins.front()->input_streams();
199 DEBUG_TRACE (DEBUG::Processors, string_compose ("Plugin insert, input streams = %1, match using %2\n", in, _match.method));
201 if (_match.method == Split) {
203 /* we are splitting 1 processor input to multiple plugin inputs,
204 so we have a maximum of 1 stream of each type.
206 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
207 if (in.get (*t) > 1) {
213 } else if (_match.method == Hide) {
215 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
216 in.set (*t, in.get (*t) - _match.hide.get (*t));
222 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
223 in.set (*t, in.get (*t) * _plugins.size ());
231 PluginInsert::natural_output_streams() const
233 return _plugins[0]->get_info()->n_outputs;
237 PluginInsert::natural_input_streams() const
239 return _plugins[0]->get_info()->n_inputs;
243 PluginInsert::has_no_inputs() const
245 return _plugins[0]->get_info()->n_inputs == ChanCount::ZERO;
249 PluginInsert::has_no_audio_inputs() const
251 return _plugins[0]->get_info()->n_inputs.n_audio() == 0;
255 PluginInsert::is_midi_instrument() const
257 /* XXX more finesse is possible here. VST plugins have a
258 a specific "instrument" flag, for example.
260 PluginInfoPtr pi = _plugins[0]->get_info();
262 return pi->n_inputs.n_midi() != 0 &&
263 pi->n_outputs.n_audio() > 0;
267 PluginInsert::create_automatable_parameters ()
269 assert (!_plugins.empty());
271 set<Evoral::Parameter> a = _plugins.front()->automatable ();
273 for (set<Evoral::Parameter>::iterator i = a.begin(); i != a.end(); ++i) {
274 if (i->type() == PluginAutomation) {
276 Evoral::Parameter param(*i);
278 ParameterDescriptor desc;
279 _plugins.front()->get_parameter_descriptor(i->id(), desc);
281 can_automate (param);
282 boost::shared_ptr<AutomationList> list(new AutomationList(param, desc));
283 boost::shared_ptr<AutomationControl> c (new PluginControl(this, param, desc, list));
285 _plugins.front()->set_automation_control (i->id(), c);
286 } else if (i->type() == PluginPropertyAutomation) {
287 Evoral::Parameter param(*i);
288 const ParameterDescriptor& desc = _plugins.front()->get_property_descriptor(param.id());
289 if (desc.datatype != Variant::NOTHING) {
290 boost::shared_ptr<AutomationList> list;
291 if (Variant::type_is_numeric(desc.datatype)) {
292 list = boost::shared_ptr<AutomationList>(new AutomationList(param, desc));
294 add_control (boost::shared_ptr<AutomationControl> (new PluginPropertyControl(this, param, desc, list)));
299 /** Called when something outside of this host has modified a plugin
300 * parameter. Responsible for propagating the change to two places:
302 * 1) anything listening to the Control itself
303 * 2) any replicated plugins that make up this PluginInsert.
305 * The PluginInsert is connected to the ParameterChangedExternally signal for
306 * the first (primary) plugin, and here broadcasts that change to any others.
308 * XXX We should probably drop this whole replication idea (Paul, October 2015)
309 * since it isn't used by sensible plugin APIs (AU, LV2).
312 PluginInsert::parameter_changed_externally (uint32_t which, float val)
314 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter (PluginAutomation, 0, which));
316 /* First propagation: alter the underlying value of the control,
317 * without telling the plugin(s) that own/use it to set it.
324 boost::shared_ptr<PluginControl> pc = boost::dynamic_pointer_cast<PluginControl> (ac);
327 pc->catch_up_with_external_value (val);
330 /* Second propagation: tell all plugins except the first to
331 update the value of this parameter. For sane plugin APIs,
332 there are no other plugins, so this is a no-op in those
336 Plugins::iterator i = _plugins.begin();
338 /* don't set the first plugin, just all the slaves */
340 if (i != _plugins.end()) {
342 for (; i != _plugins.end(); ++i) {
343 (*i)->set_parameter (which, val);
349 PluginInsert::set_block_size (pframes_t nframes)
352 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
353 if ((*i)->set_block_size (nframes) != 0) {
361 PluginInsert::activate ()
363 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
367 Processor::activate ();
371 PluginInsert::deactivate ()
373 Processor::deactivate ();
375 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
381 PluginInsert::flush ()
383 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
389 PluginInsert::connect_and_run (BufferSet& bufs, pframes_t nframes, framecnt_t offset, bool with_auto, framepos_t now)
391 PinMappings in_map (_in_map);
392 PinMappings out_map (_out_map);
394 // TODO auto-detect (if PinMappings are not identity maps)
395 _no_inplace = _pending_no_inplace || _plugins.front()->inplace_broken ();
397 // Calculate if, and how many frames we need to collect for analysis
398 framecnt_t collect_signal_nframes = (_signal_analysis_collect_nframes_max -
399 _signal_analysis_collected_nframes);
400 if (nframes < collect_signal_nframes) { // we might not get all frames now
401 collect_signal_nframes = nframes;
405 if (_match.method == Split) {
407 assert (in_map.size () == 1);
408 ChanCount const in_streams = internal_input_streams ();
409 // TODO - inplace & analysis only
410 // for no-inplace the buffer is copied anyway..
411 /* fix the input mapping so that we have maps for each of the plugin's inputs */
413 /* copy the first stream's buffer contents to the others */
414 /* XXX: audio only */
416 uint32_t first_idx = in_map[0].get (DataType::AUDIO, 0, &valid);
418 for (uint32_t i = in_streams.n_audio(); i < natural_input_streams().n_audio(); ++i) {
419 uint32_t idx = in_map[0].get (DataType::AUDIO, i, &valid);
421 bufs.get_audio(idx).read_from(bufs.get_audio(first_idx), nframes, offset, offset);
427 bufs.set_count(ChanCount::max(bufs.count(), _configured_in));
428 bufs.set_count(ChanCount::max(bufs.count(), _configured_out));
430 /* Note that we've already required that plugins
431 be able to handle in-place processing.
438 for (Controls::iterator li = controls().begin(); li != controls().end(); ++li, ++n) {
440 boost::shared_ptr<AutomationControl> c
441 = boost::dynamic_pointer_cast<AutomationControl>(li->second);
443 if (c->list() && c->automation_playback()) {
446 const float val = c->list()->rt_safe_eval (now, valid);
449 /* This is the ONLY place where we are
451 * AutomationControl::set_value_unchecked(). We
452 * know that the control is in
453 * automation playback mode, so no
454 * check on writable() is required
455 * (which must be done in AutomationControl::set_value()
458 c->set_value_unchecked(val);
465 if (collect_signal_nframes > 0) {
467 //std::cerr << "collect input, bufs " << bufs.count().n_audio() << " count, " << bufs.available().n_audio() << " available" << std::endl;
468 //std::cerr << " streams " << internal_input_streams().n_audio() << std::endl;
469 //std::cerr << "filling buffer with " << collect_signal_nframes << " frames at " << _signal_analysis_collected_nframes << std::endl;
471 _signal_analysis_inputs.set_count(internal_input_streams());
473 for (uint32_t i = 0; i < internal_input_streams().n_audio(); ++i) {
474 _signal_analysis_inputs.get_audio(i).read_from(
476 collect_signal_nframes,
477 _signal_analysis_collected_nframes); // offset is for target buffer
483 BufferSet& inplace_bufs = _session.get_noinplace_buffers();
484 ARDOUR::ChanMapping used_outputs;
487 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i, ++pc) {
489 ARDOUR::ChanMapping i_in_map (natural_input_streams());
490 ARDOUR::ChanMapping i_out_map;
491 ARDOUR::ChanCount mapped;
492 ARDOUR::ChanCount backmap;
494 // map inputs sequentially
495 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
496 for (uint32_t in = 0; in < natural_input_streams().get (*t); ++in) {
498 uint32_t in_idx = in_map[pc].get (*t, in, &valid);
499 uint32_t m = mapped.get (*t);
501 inplace_bufs.get (*t, m).read_from (bufs.get (*t, in_idx), nframes, offset, offset);
503 inplace_bufs.get (*t, m).silence (nframes, offset);
505 mapped.set (*t, m + 1);
509 // TODO use map_offset_to() instead ??
513 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
514 for (uint32_t out = 0; out < natural_output_streams().get (*t); ++out) {
515 uint32_t m = mapped.get (*t);
516 inplace_bufs.get (*t, m).silence (nframes, offset);
517 i_out_map.set (*t, out, m);
518 mapped.set (*t, m + 1);
522 if ((*i)->connect_and_run(inplace_bufs, i_in_map, i_out_map, nframes, offset)) {
528 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
529 for (uint32_t out = 0; out < natural_output_streams().get (*t); ++out) {
530 uint32_t m = backmap.get (*t);
532 uint32_t out_idx = out_map[pc].get (*t, out, &valid);
534 bufs.get (*t, out_idx).read_from (inplace_bufs.get (*t, m), nframes, offset, offset);
535 used_outputs.set (*t, out_idx, 1); // mark as used
537 backmap.set (*t, m + 1);
541 /* all instances have completed, now clear outputs that have not been written to.
542 * (except midi bypass)
544 if (bufs.count().n_midi() == 1 && natural_output_streams().get(DataType::MIDI) == 0) {
545 used_outputs.set (DataType::MIDI, 0, 1); // Midi bypass.
547 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
548 for (uint32_t out = 0; out < bufs.count().get (*t); ++out) {
550 used_outputs.get (*t, out, &valid);
551 if (valid) { continue; }
552 bufs.get (*t, out).silence (nframes, offset);
558 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i, ++pc) {
559 if ((*i)->connect_and_run(bufs, in_map[pc], out_map[pc], nframes, offset)) {
565 if (collect_signal_nframes > 0) {
567 //std::cerr << " output, bufs " << bufs.count().n_audio() << " count, " << bufs.available().n_audio() << " available" << std::endl;
568 //std::cerr << " streams " << internal_output_streams().n_audio() << std::endl;
570 _signal_analysis_outputs.set_count(internal_output_streams());
572 for (uint32_t i = 0; i < internal_output_streams().n_audio(); ++i) {
573 _signal_analysis_outputs.get_audio(i).read_from(
575 collect_signal_nframes,
576 _signal_analysis_collected_nframes); // offset is for target buffer
579 _signal_analysis_collected_nframes += collect_signal_nframes;
580 assert(_signal_analysis_collected_nframes <= _signal_analysis_collect_nframes_max);
582 if (_signal_analysis_collected_nframes == _signal_analysis_collect_nframes_max) {
583 _signal_analysis_collect_nframes_max = 0;
584 _signal_analysis_collected_nframes = 0;
586 AnalysisDataGathered(&_signal_analysis_inputs,
587 &_signal_analysis_outputs);
590 /* leave remaining channel buffers alone */
594 PluginInsert::silence (framecnt_t nframes)
600 ChanMapping in_map (natural_input_streams ());
601 ChanMapping out_map (natural_output_streams ());
603 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
604 (*i)->connect_and_run (_session.get_scratch_buffers ((*i)->get_info()->n_inputs, true), in_map, out_map, nframes, 0);
609 PluginInsert::run (BufferSet& bufs, framepos_t start_frame, framepos_t /*end_frame*/, pframes_t nframes, bool)
611 if (_pending_active) {
612 /* run as normal if we are active or moving from inactive to active */
614 if (_session.transport_rolling() || _session.bounce_processing()) {
615 automation_run (bufs, start_frame, nframes);
617 connect_and_run (bufs, nframes, 0, false);
621 uint32_t in = input_streams ().n_audio ();
622 uint32_t out = output_streams().n_audio ();
624 if (has_no_audio_inputs() || in == 0) {
626 /* silence all (audio) outputs. Should really declick
627 * at the transitions of "active"
630 for (uint32_t n = 0; n < out; ++n) {
631 bufs.get_audio (n).silence (nframes);
634 } else if (out > in) {
636 /* not active, but something has make up for any channel count increase */
638 // TODO: option round-robin (n % in) or silence additional buffers ??
639 // for now , simply replicate last buffer
640 for (uint32_t n = in; n < out; ++n) {
641 bufs.get_audio(n).read_from(bufs.get_audio(in - 1), nframes);
645 bufs.count().set_audio (out);
648 _active = _pending_active;
650 /* we have no idea whether the plugin generated silence or not, so mark
651 * all buffers appropriately.
657 PluginInsert::automation_run (BufferSet& bufs, framepos_t start, pframes_t nframes)
659 Evoral::ControlEvent next_event (0, 0.0f);
660 framepos_t now = start;
661 framepos_t end = now + nframes;
662 framecnt_t offset = 0;
664 Glib::Threads::Mutex::Lock lm (control_lock(), Glib::Threads::TRY_LOCK);
667 connect_and_run (bufs, nframes, offset, false);
671 if (!find_next_event (now, end, next_event) || _plugins.front()->requires_fixed_sized_buffers()) {
673 /* no events have a time within the relevant range */
675 connect_and_run (bufs, nframes, offset, true, now);
681 framecnt_t cnt = min (((framecnt_t) ceil (next_event.when) - now), (framecnt_t) nframes);
683 connect_and_run (bufs, cnt, offset, true, now);
689 if (!find_next_event (now, end, next_event)) {
694 /* cleanup anything that is left to do */
697 connect_and_run (bufs, nframes, offset, true, now);
702 PluginInsert::default_parameter_value (const Evoral::Parameter& param)
704 if (param.type() != PluginAutomation)
707 if (_plugins.empty()) {
708 fatal << _("programming error: ") << X_("PluginInsert::default_parameter_value() called with no plugin")
710 abort(); /*NOTREACHED*/
713 return _plugins[0]->default_value (param.id());
718 PluginInsert::can_reset_all_parameters ()
722 for (uint32_t par = 0; par < _plugins[0]->parameter_count(); ++par) {
724 const uint32_t cid = _plugins[0]->nth_parameter (par, ok);
726 if (!ok || !_plugins[0]->parameter_is_input(cid)) {
730 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter(PluginAutomation, 0, cid));
736 if (ac->automation_state() & Play) {
741 return all && (params > 0);
745 PluginInsert::reset_parameters_to_default ()
749 for (uint32_t par = 0; par < _plugins[0]->parameter_count(); ++par) {
751 const uint32_t cid = _plugins[0]->nth_parameter (par, ok);
753 if (!ok || !_plugins[0]->parameter_is_input(cid)) {
757 const float dflt = _plugins[0]->default_value (cid);
758 const float curr = _plugins[0]->get_parameter (cid);
764 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter(PluginAutomation, 0, cid));
769 if (ac->automation_state() & Play) {
774 ac->set_value (dflt, Controllable::NoGroup);
779 boost::shared_ptr<Plugin>
780 PluginInsert::plugin_factory (boost::shared_ptr<Plugin> other)
782 boost::shared_ptr<LadspaPlugin> lp;
783 boost::shared_ptr<LuaProc> lua;
785 boost::shared_ptr<LV2Plugin> lv2p;
787 #ifdef WINDOWS_VST_SUPPORT
788 boost::shared_ptr<WindowsVSTPlugin> vp;
791 boost::shared_ptr<LXVSTPlugin> lxvp;
793 #ifdef AUDIOUNIT_SUPPORT
794 boost::shared_ptr<AUPlugin> ap;
797 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
798 return boost::shared_ptr<Plugin> (new LadspaPlugin (*lp));
799 } else if ((lua = boost::dynamic_pointer_cast<LuaProc> (other)) != 0) {
800 return boost::shared_ptr<Plugin> (new LuaProc (*lua));
802 } else if ((lv2p = boost::dynamic_pointer_cast<LV2Plugin> (other)) != 0) {
803 return boost::shared_ptr<Plugin> (new LV2Plugin (*lv2p));
805 #ifdef WINDOWS_VST_SUPPORT
806 } else if ((vp = boost::dynamic_pointer_cast<WindowsVSTPlugin> (other)) != 0) {
807 return boost::shared_ptr<Plugin> (new WindowsVSTPlugin (*vp));
810 } else if ((lxvp = boost::dynamic_pointer_cast<LXVSTPlugin> (other)) != 0) {
811 return boost::shared_ptr<Plugin> (new LXVSTPlugin (*lxvp));
813 #ifdef AUDIOUNIT_SUPPORT
814 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
815 return boost::shared_ptr<Plugin> (new AUPlugin (*ap));
819 fatal << string_compose (_("programming error: %1"),
820 X_("unknown plugin type in PluginInsert::plugin_factory"))
822 abort(); /*NOTREACHED*/
823 return boost::shared_ptr<Plugin> ((Plugin*) 0);
827 PluginInsert::set_input_map (uint32_t num, ChanMapping m) {
828 if (num < _in_map.size()) {
829 bool changed = _in_map[num] != m;
832 PluginMapChanged (); /* EMIT SIGNAL */
838 PluginInsert::set_output_map (uint32_t num, ChanMapping m) {
839 if (num < _out_map.size()) {
840 bool changed = _out_map[num] != m;
843 PluginMapChanged (); /* EMIT SIGNAL */
849 PluginInsert::configure_io (ChanCount in, ChanCount out)
851 Match old_match = _match;
856 old_in = _configured_in;
857 old_out = _configured_out;
859 old_in = internal_input_streams ();
860 old_out = internal_output_streams ();
864 _configured_out = out;
866 /* TODO use "custom config"
867 * allow user to edit/override output ports, handle strict i/o.
869 * configuration should only applied here.
872 /* set the matching method and number of plugins that we will use to meet this configuration */
873 _match = private_can_support_io_configuration (in, out);
874 if (set_count (_match.plugins) == false) {
875 PluginIoReConfigure (); /* EMIT SIGNAL */
880 /* configure plugins */
881 switch (_match.method) {
884 if (_plugins.front()->configure_io (_plugins.front()->get_info()->n_inputs, out) == false) {
885 PluginIoReConfigure (); /* EMIT SIGNAL */
892 if (_plugins.front()->configure_io (in, out) == false) {
893 PluginIoReConfigure (); /* EMIT SIGNAL */
900 if (old_in == in && old_out == out
901 && old_match.method == _match.method
902 && _in_map.size() == _out_map.size()
903 && _in_map.size() == get_count ()
905 /* If the configuraton has not changed AND there is a custom map,
906 * keep the custom map.
908 // TODO: check if it's valid..
910 /* generate a new mapping */
914 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i, ++pc) {
915 if (_match.method == Split) {
916 /* TODO see PluginInsert::connect_and_run, channel replication */
917 _in_map[pc] = ChanMapping (max (natural_input_streams (), in));
919 _in_map[pc] = ChanMapping (min (natural_input_streams (), in));
921 _out_map[pc] = ChanMapping (min (natural_output_streams(), out));
923 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
924 _in_map[pc].offset_to(*t, pc * natural_input_streams().get(*t));
925 _out_map[pc].offset_to(*t, pc * natural_output_streams().get(*t));
929 //PluginMapChanged (); /* EMIT SIGNAL */
933 if ( (old_match.method != _match.method && (old_match.method == Split || _match.method == Split))
938 PluginIoReConfigure (); /* EMIT SIGNAL */
941 // we don't know the analysis window size, so we must work with the
942 // current buffer size here. each request for data fills in these
943 // buffers and the analyser makes sure it gets enough data for the
945 session().ensure_buffer_set (_signal_analysis_inputs, in);
946 //_signal_analysis_inputs.set_count (in);
948 session().ensure_buffer_set (_signal_analysis_outputs, out);
949 //_signal_analysis_outputs.set_count (out);
951 // std::cerr << "set counts to i" << in.n_audio() << "/o" << out.n_audio() << std::endl;
954 return Processor::configure_io (in, out);
957 /** Decide whether this PluginInsert can support a given IO configuration.
958 * To do this, we run through a set of possible solutions in rough order of
961 * @param in Required input channel count.
962 * @param out Filled in with the output channel count if we return true.
963 * @return true if the given IO configuration can be supported.
966 PluginInsert::can_support_io_configuration (const ChanCount& in, ChanCount& out)
968 return private_can_support_io_configuration (in, out).method != Impossible;
971 /** A private version of can_support_io_configuration which returns the method
972 * by which the configuration can be matched, rather than just whether or not
976 PluginInsert::private_can_support_io_configuration (ChanCount const & inx, ChanCount& out)
978 if (_plugins.empty()) {
982 if (_custom_cfg && !_strict_io) {
984 return Match (ExactMatch, get_count(), false, true); // XXX
987 PluginInfoPtr info = _plugins.front()->get_info();
988 ChanCount in; in += inx;
991 if (info->reconfigurable_io()) {
992 /* Plugin has flexible I/O, so delegate to it */
993 bool const r = _plugins.front()->can_support_io_configuration (in, out);
995 return Match (Impossible, 0);
998 if (_strict_io && in.n_audio() < out.n_audio()) {
999 DEBUG_TRACE (DEBUG::Processors, string_compose ("hiding output ports of reconfigurable %1\n", name()));
1000 out.set (DataType::AUDIO, in.get (DataType::AUDIO));
1003 return Match (Delegate, 1);
1006 ChanCount inputs = info->n_inputs;
1007 ChanCount outputs = info->n_outputs;
1009 if (in.get(DataType::MIDI) == 1 && outputs.get(DataType::MIDI) == 0) {
1010 DEBUG_TRACE ( DEBUG::Processors, string_compose ("bypassing midi-data around %1\n", name()));
1011 midi_bypass.set(DataType::MIDI, 1);
1013 if (in.get(DataType::MIDI) == 1 && inputs.get(DataType::MIDI) == 0) {
1014 DEBUG_TRACE ( DEBUG::Processors, string_compose ("hiding midi-port from plugin %1\n", name()));
1015 in.set(DataType::MIDI, 0);
1018 bool no_inputs = true;
1019 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1020 if (inputs.get (*t) != 0) {
1026 /* replicate no-input generators with strict i/o */
1027 if (no_inputs && _strict_io) {
1029 bool can_replicate = true;
1031 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1032 uint32_t nout = outputs.get (*t);
1033 uint32_t nwant = in.get (*t);
1035 can_replicate = false;
1038 if (nout == nwant) {
1042 if (f == 0 && nwant % nout == 0) {
1046 if (f * nout != nwant) {
1047 can_replicate = false;
1052 if (can_replicate && f > 1) {
1053 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1054 out.set (*t, outputs.get(*t) * f);
1057 return Match (Replicate, f);
1061 if ((no_inputs || inputs == in) && _strict_io) {
1062 /* special case synths and generators */
1063 if (in.n_audio () == 0 && in.n_midi () > 0 && outputs.n_audio () > 0) {
1064 // TODO limit the audio-channel count to track output
1065 out = outputs + midi_bypass;
1066 return Match (ExactMatch, 1);
1068 out = in + midi_bypass;
1069 return Match (ExactMatch, 1, true);
1074 /* no inputs so we can take any input configuration since we throw it away */
1075 out = outputs + midi_bypass;
1076 return Match (NoInputs, 1);
1079 /* Plugin inputs match requested inputs exactly */
1081 out = outputs + midi_bypass;
1082 return Match (ExactMatch, 1);
1085 /* We may be able to run more than one copy of the plugin within this insert
1086 to cope with the insert having more inputs than the plugin.
1087 We allow replication only for plugins with either zero or 1 inputs and outputs
1088 for every valid data type.
1092 bool can_replicate = true;
1093 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1095 uint32_t nin = inputs.get (*t);
1097 // No inputs of this type
1098 if (nin == 0 && in.get(*t) == 0) {
1102 if (nin != 1 || outputs.get (*t) != 1) {
1103 can_replicate = false;
1107 // Potential factor not set yet
1109 f = in.get(*t) / nin;
1112 // Factor for this type does not match another type, can not replicate
1113 if (f != (in.get(*t) / nin)) {
1114 can_replicate = false;
1119 if (can_replicate) {
1121 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1122 out.set (*t, outputs.get(*t) * f);
1125 return Match (Replicate, f);
1128 /* If the processor has exactly one input of a given type, and
1129 the plugin has more, we can feed the single processor input
1130 to some or all of the plugin inputs. This is rather
1131 special-case-y, but the 1-to-many case is by far the
1132 simplest. How do I split thy 2 processor inputs to 3
1133 plugin inputs? Let me count the ways ...
1136 bool can_split = !_strict_io; // XXX
1137 for (DataType::iterator t = DataType::begin(); t != DataType::end() && can_split; ++t) {
1139 bool const can_split_type = (in.get (*t) == 1 && inputs.get (*t) > 1);
1140 bool const nothing_to_do_for_type = (in.get (*t) == 0 && inputs.get (*t) == 0);
1142 if (!can_split_type && !nothing_to_do_for_type) {
1148 out = outputs + midi_bypass;
1149 return Match (Split, 1);
1152 /* If the plugin has more inputs than we want, we can `hide' some of them
1153 by feeding them silence.
1156 bool could_hide = false;
1157 bool cannot_hide = false;
1158 ChanCount hide_channels;
1160 for (DataType::iterator t = DataType::begin(); t != DataType::end() && !cannot_hide; ++t) {
1161 if (inputs.get(*t) > in.get(*t)) {
1162 /* there is potential to hide, since the plugin has more inputs of type t than the insert */
1163 hide_channels.set (*t, inputs.get(*t) - in.get(*t));
1165 } else if (inputs.get(*t) < in.get(*t)) {
1166 /* we definitely cannot hide, since the plugin has fewer inputs of type t than the insert */
1171 if (could_hide && !cannot_hide) {
1174 return Match (Hide, 1, true, false, hide_channels);
1176 out = outputs + midi_bypass;
1177 return Match (Hide, 1, false, false, hide_channels);
1181 midi_bypass.reset();
1182 // TODO make it possible :)
1183 return Match (Impossible, 0);
1187 PluginInsert::get_state ()
1189 return state (true);
1193 PluginInsert::state (bool full)
1195 XMLNode& node = Processor::state (full);
1197 node.add_property("type", _plugins[0]->state_node_name());
1198 node.add_property("unique-id", _plugins[0]->unique_id());
1199 node.add_property("count", string_compose("%1", _plugins.size()));
1201 /* remember actual i/o configuration (for later placeholder
1202 * in case the plugin goes missing) */
1203 node.add_child_nocopy (* _configured_in.state (X_("ConfiguredInput")));
1204 node.add_child_nocopy (* _configured_out.state (X_("ConfiguredOutput")));
1206 // TODO save channel-maps, _custom_cfg & _custom_out
1208 _plugins[0]->set_insert_id(this->id());
1209 node.add_child_nocopy (_plugins[0]->get_state());
1211 for (Controls::iterator c = controls().begin(); c != controls().end(); ++c) {
1212 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> ((*c).second);
1214 node.add_child_nocopy (ac->get_state());
1222 PluginInsert::set_control_ids (const XMLNode& node, int version)
1224 const XMLNodeList& nlist = node.children();
1225 XMLNodeConstIterator iter;
1226 set<Evoral::Parameter>::const_iterator p;
1228 for (iter = nlist.begin(); iter != nlist.end(); ++iter) {
1229 if ((*iter)->name() == Controllable::xml_node_name) {
1230 const XMLProperty* prop;
1232 uint32_t p = (uint32_t)-1;
1234 if ((prop = (*iter)->property (X_("symbol"))) != 0) {
1235 boost::shared_ptr<LV2Plugin> lv2plugin = boost::dynamic_pointer_cast<LV2Plugin> (_plugins[0]);
1237 p = lv2plugin->port_index(prop->value().c_str());
1241 if (p == (uint32_t)-1 && (prop = (*iter)->property (X_("parameter"))) != 0) {
1242 p = atoi (prop->value());
1245 if (p != (uint32_t)-1) {
1247 /* this may create the new controllable */
1249 boost::shared_ptr<Evoral::Control> c = control (Evoral::Parameter (PluginAutomation, 0, p));
1251 #ifndef NO_PLUGIN_STATE
1255 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> (c);
1257 ac->set_state (**iter, version);
1266 PluginInsert::set_state(const XMLNode& node, int version)
1268 XMLNodeList nlist = node.children();
1269 XMLNodeIterator niter;
1270 XMLPropertyList plist;
1271 const XMLProperty *prop;
1272 ARDOUR::PluginType type;
1274 if ((prop = node.property ("type")) == 0) {
1275 error << _("XML node describing plugin is missing the `type' field") << endmsg;
1279 if (prop->value() == X_("ladspa") || prop->value() == X_("Ladspa")) { /* handle old school sessions */
1280 type = ARDOUR::LADSPA;
1281 } else if (prop->value() == X_("lv2")) {
1283 } else if (prop->value() == X_("windows-vst")) {
1284 type = ARDOUR::Windows_VST;
1285 } else if (prop->value() == X_("lxvst")) {
1286 type = ARDOUR::LXVST;
1287 } else if (prop->value() == X_("audiounit")) {
1288 type = ARDOUR::AudioUnit;
1289 } else if (prop->value() == X_("luaproc")) {
1292 error << string_compose (_("unknown plugin type %1 in plugin insert state"),
1298 prop = node.property ("unique-id");
1301 #ifdef WINDOWS_VST_SUPPORT
1302 /* older sessions contain VST plugins with only an "id" field.
1305 if (type == ARDOUR::Windows_VST) {
1306 prop = node.property ("id");
1310 #ifdef LXVST_SUPPORT
1311 /*There shouldn't be any older sessions with linuxVST support.. but anyway..*/
1313 if (type == ARDOUR::LXVST) {
1314 prop = node.property ("id");
1320 error << _("Plugin has no unique ID field") << endmsg;
1325 boost::shared_ptr<Plugin> plugin = find_plugin (_session, prop->value(), type);
1327 /* treat linux and windows VST plugins equivalent if they have the same uniqueID
1328 * allow to move sessions windows <> linux */
1329 #ifdef LXVST_SUPPORT
1330 if (plugin == 0 && type == ARDOUR::Windows_VST) {
1331 type = ARDOUR::LXVST;
1332 plugin = find_plugin (_session, prop->value(), type);
1336 #ifdef WINDOWS_VST_SUPPORT
1337 if (plugin == 0 && type == ARDOUR::LXVST) {
1338 type = ARDOUR::Windows_VST;
1339 plugin = find_plugin (_session, prop->value(), type);
1344 error << string_compose(
1345 _("Found a reference to a plugin (\"%1\") that is unknown.\n"
1346 "Perhaps it was removed or moved since it was last used."),
1352 if (type == ARDOUR::Lua) {
1353 XMLNode *ls = node.child (plugin->state_node_name().c_str());
1354 // we need to load the script to set the name and parameters.
1355 boost::shared_ptr<LuaProc> lp = boost::dynamic_pointer_cast<LuaProc>(plugin);
1357 lp->set_script_from_state (*ls);
1361 // The name of the PluginInsert comes from the plugin, nothing else
1362 _name = plugin->get_info()->name;
1366 // Processor::set_state() will set this, but too late
1367 // for it to be available when setting up plugin
1368 // state. We can't call Processor::set_state() until
1369 // the plugins themselves are created and added.
1373 if (_plugins.empty()) {
1374 /* if we are adding the first plugin, we will need to set
1375 up automatable controls.
1377 add_plugin (plugin);
1378 create_automatable_parameters ();
1379 set_control_ids (node, version);
1382 if ((prop = node.property ("count")) != 0) {
1383 sscanf (prop->value().c_str(), "%u", &count);
1386 if (_plugins.size() != count) {
1387 for (uint32_t n = 1; n < count; ++n) {
1388 add_plugin (plugin_factory (plugin));
1392 Processor::set_state (node, version);
1394 PBD::ID new_id = this->id();
1395 PBD::ID old_id = this->id();
1397 if ((prop = node.property ("id")) != 0) {
1398 old_id = prop->value ();
1401 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1403 /* find the node with the type-specific node name ("lv2", "ladspa", etc)
1404 and set all plugins to the same state.
1407 if ((*niter)->name() == plugin->state_node_name()) {
1409 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
1410 /* Plugin state can include external files which are named after the ID.
1412 * If regenerate_xml_or_string_ids() is set, the ID will already have
1413 * been changed, so we need to use the old ID from the XML to load the
1414 * state and then update the ID.
1416 * When copying a plugin-state, route_ui takes care of of updating the ID,
1417 * but we need to call set_insert_id() to clear the cached plugin-state
1418 * and force a change.
1420 if (!regenerate_xml_or_string_ids ()) {
1421 (*i)->set_insert_id (new_id);
1423 (*i)->set_insert_id (old_id);
1426 (*i)->set_state (**niter, version);
1428 if (regenerate_xml_or_string_ids ()) {
1429 (*i)->set_insert_id (new_id);
1437 if (version < 3000) {
1439 /* Only 2.X sessions need a call to set_parameter_state() - in 3.X and above
1440 this is all handled by Automatable
1443 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1444 if ((*niter)->name() == "Redirect") {
1445 /* XXX do we need to tackle placement? i think not (pd; oct 16 2009) */
1446 Processor::set_state (**niter, version);
1451 set_parameter_state_2X (node, version);
1454 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
1458 (*i)->deactivate ();
1466 PluginInsert::update_id (PBD::ID id)
1469 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
1470 (*i)->set_insert_id (id);
1475 PluginInsert::set_state_dir (const std::string& d)
1477 // state() only saves the state of the first plugin
1478 _plugins[0]->set_state_dir (d);
1482 PluginInsert::set_parameter_state_2X (const XMLNode& node, int version)
1484 XMLNodeList nlist = node.children();
1485 XMLNodeIterator niter;
1487 /* look for port automation node */
1489 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1491 if ((*niter)->name() != port_automation_node_name) {
1497 XMLNodeConstIterator iter;
1502 cnodes = (*niter)->children ("port");
1504 for (iter = cnodes.begin(); iter != cnodes.end(); ++iter){
1508 if ((cprop = child->property("number")) != 0) {
1509 port = cprop->value().c_str();
1511 warning << _("PluginInsert: Auto: no ladspa port number") << endmsg;
1515 sscanf (port, "%" PRIu32, &port_id);
1517 if (port_id >= _plugins[0]->parameter_count()) {
1518 warning << _("PluginInsert: Auto: port id out of range") << endmsg;
1522 boost::shared_ptr<AutomationControl> c = boost::dynamic_pointer_cast<AutomationControl>(
1523 control(Evoral::Parameter(PluginAutomation, 0, port_id), true));
1525 if (c && c->alist()) {
1526 if (!child->children().empty()) {
1527 c->alist()->set_state (*child->children().front(), version);
1529 /* In some cases 2.X saves lists with min_yval and max_yval
1530 being FLT_MIN and FLT_MAX respectively. This causes problems
1531 in A3 because these min/max values are used to compute
1532 where GUI control points should be drawn. If we see such
1533 values, `correct' them to the min/max of the appropriate
1537 float min_y = c->alist()->get_min_y ();
1538 float max_y = c->alist()->get_max_y ();
1540 ParameterDescriptor desc;
1541 _plugins.front()->get_parameter_descriptor (port_id, desc);
1543 if (min_y == FLT_MIN) {
1547 if (max_y == FLT_MAX) {
1551 c->alist()->set_yrange (min_y, max_y);
1554 error << string_compose (_("PluginInsert: automatable control %1 not found - ignored"), port_id) << endmsg;
1566 PluginInsert::describe_parameter (Evoral::Parameter param)
1568 if (param.type() == PluginAutomation) {
1569 return _plugins[0]->describe_parameter (param);
1570 } else if (param.type() == PluginPropertyAutomation) {
1571 boost::shared_ptr<AutomationControl> c(automation_control(param));
1572 if (c && !c->desc().label.empty()) {
1573 return c->desc().label;
1576 return Automatable::describe_parameter(param);
1580 PluginInsert::signal_latency() const
1582 if (_user_latency) {
1583 return _user_latency;
1586 return _plugins[0]->signal_latency ();
1590 PluginInsert::type ()
1592 return plugin()->get_info()->type;
1595 PluginInsert::PluginControl::PluginControl (PluginInsert* p,
1596 const Evoral::Parameter& param,
1597 const ParameterDescriptor& desc,
1598 boost::shared_ptr<AutomationList> list)
1599 : AutomationControl (p->session(), param, desc, list, p->describe_parameter(param))
1603 alist()->reset_default (desc.normal);
1605 list->set_interpolation(Evoral::ControlList::Discrete);
1610 set_flags(Controllable::Toggle);
1614 /** @param val `user' value */
1616 PluginInsert::PluginControl::set_value (double user_val, PBD::Controllable::GroupControlDisposition group_override)
1619 _set_value (user_val, group_override);
1623 PluginInsert::PluginControl::set_value_unchecked (double user_val)
1625 /* used only by automation playback */
1626 _set_value (user_val, Controllable::NoGroup);
1630 PluginInsert::PluginControl::_set_value (double user_val, PBD::Controllable::GroupControlDisposition group_override)
1632 /* FIXME: probably should be taking out some lock here.. */
1634 for (Plugins::iterator i = _plugin->_plugins.begin(); i != _plugin->_plugins.end(); ++i) {
1635 (*i)->set_parameter (_list->parameter().id(), user_val);
1638 boost::shared_ptr<Plugin> iasp = _plugin->_impulseAnalysisPlugin.lock();
1640 iasp->set_parameter (_list->parameter().id(), user_val);
1643 AutomationControl::set_value (user_val, group_override);
1647 PluginInsert::PluginControl::catch_up_with_external_value (double user_val)
1649 AutomationControl::set_value (user_val, Controllable::NoGroup);
1653 PluginInsert::PluginControl::get_state ()
1657 XMLNode& node (AutomationControl::get_state());
1658 ss << parameter().id();
1659 node.add_property (X_("parameter"), ss.str());
1661 boost::shared_ptr<LV2Plugin> lv2plugin = boost::dynamic_pointer_cast<LV2Plugin> (_plugin->_plugins[0]);
1663 node.add_property (X_("symbol"), lv2plugin->port_symbol (parameter().id()));
1670 /** @return `user' val */
1672 PluginInsert::PluginControl::get_value () const
1674 boost::shared_ptr<Plugin> plugin = _plugin->plugin (0);
1680 return plugin->get_parameter (_list->parameter().id());
1683 PluginInsert::PluginPropertyControl::PluginPropertyControl (PluginInsert* p,
1684 const Evoral::Parameter& param,
1685 const ParameterDescriptor& desc,
1686 boost::shared_ptr<AutomationList> list)
1687 : AutomationControl (p->session(), param, desc, list)
1691 alist()->set_yrange (desc.lower, desc.upper);
1692 alist()->reset_default (desc.normal);
1696 set_flags(Controllable::Toggle);
1701 PluginInsert::PluginPropertyControl::set_value (double user_val, PBD::Controllable::GroupControlDisposition /* group_override*/)
1704 set_value_unchecked (user_val);
1709 PluginInsert::PluginPropertyControl::set_value_unchecked (double user_val)
1711 /* Old numeric set_value(), coerce to appropriate datatype if possible.
1712 This is lossy, but better than nothing until Ardour's automation system
1713 can handle various datatypes all the way down. */
1714 const Variant value(_desc.datatype, user_val);
1715 if (value.type() == Variant::NOTHING) {
1716 error << "set_value(double) called for non-numeric property" << endmsg;
1720 for (Plugins::iterator i = _plugin->_plugins.begin(); i != _plugin->_plugins.end(); ++i) {
1721 (*i)->set_property(_list->parameter().id(), value);
1725 AutomationControl::set_value (user_val, Controllable::NoGroup);
1729 PluginInsert::PluginPropertyControl::get_state ()
1733 XMLNode& node (AutomationControl::get_state());
1734 ss << parameter().id();
1735 node.add_property (X_("property"), ss.str());
1736 node.remove_property (X_("value"));
1742 PluginInsert::PluginPropertyControl::get_value () const
1744 return _value.to_double();
1747 boost::shared_ptr<Plugin>
1748 PluginInsert::get_impulse_analysis_plugin()
1750 boost::shared_ptr<Plugin> ret;
1751 if (_impulseAnalysisPlugin.expired()) {
1752 ret = plugin_factory(_plugins[0]);
1753 ret->configure_io (internal_input_streams (), internal_output_streams ());
1754 _impulseAnalysisPlugin = ret;
1756 ret = _impulseAnalysisPlugin.lock();
1763 PluginInsert::collect_signal_for_analysis (framecnt_t nframes)
1765 // called from outside the audio thread, so this should be safe
1766 // only do audio as analysis is (currently) only for audio plugins
1767 _signal_analysis_inputs.ensure_buffers( DataType::AUDIO, internal_input_streams().n_audio(), nframes);
1768 _signal_analysis_outputs.ensure_buffers( DataType::AUDIO, internal_output_streams().n_audio(), nframes);
1770 _signal_analysis_collected_nframes = 0;
1771 _signal_analysis_collect_nframes_max = nframes;
1774 /** Add a plugin to our list */
1776 PluginInsert::add_plugin (boost::shared_ptr<Plugin> plugin)
1778 plugin->set_insert_id (this->id());
1780 if (_plugins.empty()) {
1781 /* first (and probably only) plugin instance - connect to relevant signals
1784 plugin->ParameterChangedExternally.connect_same_thread (*this, boost::bind (&PluginInsert::parameter_changed_externally, this, _1, _2));
1785 plugin->StartTouch.connect_same_thread (*this, boost::bind (&PluginInsert::start_touch, this, _1));
1786 plugin->EndTouch.connect_same_thread (*this, boost::bind (&PluginInsert::end_touch, this, _1));
1789 _plugins.push_back (plugin);
1793 PluginInsert::realtime_handle_transport_stopped ()
1795 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
1796 (*i)->realtime_handle_transport_stopped ();
1801 PluginInsert::realtime_locate ()
1803 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
1804 (*i)->realtime_locate ();
1809 PluginInsert::monitoring_changed ()
1811 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
1812 (*i)->monitoring_changed ();
1817 PluginInsert::start_touch (uint32_t param_id)
1819 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter (PluginAutomation, 0, param_id));
1821 ac->start_touch (session().audible_frame());
1826 PluginInsert::end_touch (uint32_t param_id)
1828 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter (PluginAutomation, 0, param_id));
1830 ac->stop_touch (true, session().audible_frame());