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/types_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"
39 #include "ardour/port.h"
42 #include "ardour/lv2_plugin.h"
45 #ifdef WINDOWS_VST_SUPPORT
46 #include "ardour/windows_vst_plugin.h"
50 #include "ardour/lxvst_plugin.h"
54 #include "ardour/mac_vst_plugin.h"
57 #ifdef AUDIOUNIT_SUPPORT
58 #include "ardour/audio_unit.h"
61 #include "ardour/session.h"
62 #include "ardour/types.h"
67 using namespace ARDOUR;
70 const string PluginInsert::port_automation_node_name = "PortAutomation";
72 PluginInsert::PluginInsert (Session& s, boost::shared_ptr<Plugin> plug)
73 : Processor (s, (plug ? plug->name() : string ("toBeRenamed")))
74 , _sc_playback_latency (0)
75 , _sc_capture_latency (0)
76 , _plugin_signal_latency (0)
77 , _signal_analysis_collected_nframes(0)
78 , _signal_analysis_collect_nframes_max(0)
83 , _maps_from_state (false)
84 , _latency_changed (false)
85 , _bypass_port (UINT32_MAX)
87 /* the first is the master */
91 create_automatable_parameters ();
92 const ChanCount& sc (sidechain_input_pins ());
93 if (sc.n_audio () > 0 || sc.n_midi () > 0) {
94 add_sidechain (sc.n_audio (), sc.n_midi ());
99 PluginInsert::~PluginInsert ()
101 for (CtrlOutMap::const_iterator i = _control_outputs.begin(); i != _control_outputs.end(); ++i) {
102 boost::dynamic_pointer_cast<ReadOnlyControl>(i->second)->drop_references ();
107 PluginInsert::set_strict_io (bool b)
109 bool changed = _strict_io != b;
112 PluginConfigChanged (); /* EMIT SIGNAL */
117 PluginInsert::set_count (uint32_t num)
119 bool require_state = !_plugins.empty();
121 if (require_state && num > 1 && plugin (0)->get_info ()->type == ARDOUR::AudioUnit) {
122 // we don't allow to replicate AUs
126 /* this is a bad idea.... we shouldn't do this while active.
127 * only a route holding their redirect_lock should be calling this
132 } else if (num > _plugins.size()) {
133 uint32_t diff = num - _plugins.size();
135 for (uint32_t n = 0; n < diff; ++n) {
136 boost::shared_ptr<Plugin> p = plugin_factory (_plugins[0]);
140 XMLNode& state = _plugins[0]->get_state ();
141 p->set_state (state, Stateful::loading_state_version);
148 PluginConfigChanged (); /* EMIT SIGNAL */
150 } else if (num < _plugins.size()) {
151 uint32_t diff = _plugins.size() - num;
152 for (uint32_t n= 0; n < diff; ++n) {
155 PluginConfigChanged (); /* EMIT SIGNAL */
163 PluginInsert::set_sinks (const ChanCount& c)
166 /* no signal, change will only be visible after re-config */
170 PluginInsert::set_outputs (const ChanCount& c)
172 bool changed = (_custom_out != c) && _custom_cfg;
175 PluginConfigChanged (); /* EMIT SIGNAL */
180 PluginInsert::set_custom_cfg (bool b)
182 bool changed = _custom_cfg != b;
185 PluginConfigChanged (); /* EMIT SIGNAL */
190 PluginInsert::set_preset_out (const ChanCount& c)
192 bool changed = _preset_out != c;
194 if (changed && !_custom_cfg) {
195 PluginConfigChanged (); /* EMIT SIGNAL */
201 PluginInsert::add_sidechain (uint32_t n_audio, uint32_t n_midi)
203 // caller must hold process lock
207 std::ostringstream n;
208 if (n_audio > 0 || n_midi > 0) {
209 n << "Sidechain " << Session::next_name_id ();
211 n << "TO BE RESET FROM XML";
213 SideChain *sc = new SideChain (_session, n.str ());
214 _sidechain = boost::shared_ptr<SideChain> (sc);
215 _sidechain->activate ();
216 for (uint32_t n = 0; n < n_audio; ++n) {
217 _sidechain->input()->add_port ("", owner(), DataType::AUDIO); // add a port, don't connect.
219 for (uint32_t n = 0; n < n_midi; ++n) {
220 _sidechain->input()->add_port ("", owner(), DataType::MIDI); // add a port, don't connect.
222 PluginConfigChanged (); /* EMIT SIGNAL */
227 PluginInsert::del_sidechain ()
233 _sc_playback_latency = 0;
234 _sc_capture_latency = 0;
235 PluginConfigChanged (); /* EMIT SIGNAL */
240 PluginInsert::set_sidechain_latency (uint32_t capture, uint32_t playback)
243 (_sc_playback_latency != playback || _sc_capture_latency != capture)) {
244 _sc_capture_latency = capture;
245 _sc_playback_latency = playback;
246 LatencyRange pl; pl.min = pl.max = playback;
247 LatencyRange cl; cl.min = cl.max = capture;
248 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: capture %2 playback; %3\n", _sidechain->name (), capture, playback));
249 PortSet& ps (_sidechain->input ()->ports ());
250 for (PortSet::iterator p = ps.begin(); p != ps.end(); ++p) {
251 p->set_private_latency_range (pl, true);
252 p->set_private_latency_range (cl, false);
258 PluginInsert::control_list_automation_state_changed (Evoral::Parameter which, AutoState s)
260 if (which.type() != PluginAutomation)
263 boost::shared_ptr<AutomationControl> c
264 = boost::dynamic_pointer_cast<AutomationControl>(control (which));
267 _plugins[0]->set_parameter (which.id(), c->list()->eval (_session.transport_sample()));
272 PluginInsert::output_streams() const
274 assert (_configured);
275 return _configured_out;
279 PluginInsert::input_streams() const
281 assert (_configured);
282 return _configured_in;
286 PluginInsert::internal_streams() const
288 assert (_configured);
289 return _configured_internal;
293 PluginInsert::internal_output_streams() const
295 assert (!_plugins.empty());
297 PluginInfoPtr info = _plugins.front()->get_info();
299 if (info->reconfigurable_io()) {
300 ChanCount out = _plugins.front()->output_streams ();
301 // DEBUG_TRACE (DEBUG::Processors, string_compose ("Plugin insert, reconfigur(able) output streams = %1\n", out));
304 ChanCount out = info->n_outputs;
305 // DEBUG_TRACE (DEBUG::Processors, string_compose ("Plugin insert, static output streams = %1 for %2 plugins\n", out, _plugins.size()));
306 out.set_audio (out.n_audio() * _plugins.size());
307 out.set_midi (out.n_midi() * _plugins.size());
313 PluginInsert::internal_input_streams() const
315 assert (!_plugins.empty());
319 PluginInfoPtr info = _plugins.front()->get_info();
321 if (info->reconfigurable_io()) {
322 in = _plugins.front()->input_streams();
327 DEBUG_TRACE (DEBUG::Processors, string_compose ("Plugin insert, input streams = %1, match using %2\n", in, _match.method));
329 if (_match.method == Split) {
331 /* we are splitting 1 processor input to multiple plugin inputs,
332 so we have a maximum of 1 stream of each type.
334 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
335 if (in.get (*t) > 1) {
341 } else if (_match.method == Hide) {
343 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
344 in.set (*t, in.get (*t) - _match.hide.get (*t));
350 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
351 in.set (*t, in.get (*t) * _plugins.size ());
359 PluginInsert::natural_output_streams() const
362 if (is_channelstrip ()) {
363 return ChanCount::min (_configured_out, ChanCount (DataType::AUDIO, 2));
366 return _plugins[0]->get_info()->n_outputs;
370 PluginInsert::natural_input_streams() const
373 if (is_channelstrip ()) {
374 return ChanCount::min (_configured_in, ChanCount (DataType::AUDIO, 2));
377 return _plugins[0]->get_info()->n_inputs;
381 PluginInsert::sidechain_input_pins() const
383 return _cached_sidechain_pins;
387 PluginInsert::has_no_inputs() const
389 return _plugins[0]->get_info()->n_inputs == ChanCount::ZERO;
393 PluginInsert::has_no_audio_inputs() const
395 return _plugins[0]->get_info()->n_inputs.n_audio() == 0;
399 PluginInsert::plugin_latency () const {
400 return _plugins.front()->signal_latency ();
404 PluginInsert::is_instrument() const
406 PluginInfoPtr pip = _plugins[0]->get_info();
407 if (pip->is_instrument ()) {
410 return pip->n_inputs.n_midi () != 0 && pip->n_outputs.n_audio () > 0 && pip->n_inputs.n_audio () == 0;
414 PluginInsert::has_output_presets (ChanCount in, ChanCount out)
416 if (!_configured && _plugins[0]->get_info ()->reconfigurable_io ()) {
417 // collect possible configurations, prefer given in/out
418 _plugins[0]->can_support_io_configuration (in, out);
421 PluginOutputConfiguration ppc (_plugins[0]->possible_output ());
423 if (ppc.size () == 0) {
426 if (!strict_io () && ppc.size () == 1) {
430 if (strict_io () && ppc.size () == 1) {
431 // "stereo" is currently preferred default for instruments
432 if (ppc.find (2) != ppc.end ()) {
437 if (ppc.size () == 1 && ppc.find (0) != ppc.end () && !_plugins[0]->get_info ()->reconfigurable_io ()) {
438 // some midi-sequencer (e.g. QMidiArp) or other midi-out plugin
439 // pretending to be an "Instrument"
443 if (!is_instrument ()) {
450 PluginInsert::create_automatable_parameters ()
452 assert (!_plugins.empty());
454 boost::shared_ptr<Plugin> plugin = _plugins.front();
455 set<Evoral::Parameter> a = _plugins.front()->automatable ();
457 for (uint32_t i = 0; i < plugin->parameter_count(); ++i) {
458 if (!plugin->parameter_is_control (i)) {
462 ParameterDescriptor desc;
463 plugin->get_parameter_descriptor(i, desc);
465 if (!plugin->parameter_is_input (i)) {
466 _control_outputs[i] = boost::shared_ptr<ReadOnlyControl> (new ReadOnlyControl (plugin, desc, i));
469 Evoral::Parameter param (PluginAutomation, 0, i);
471 const bool automatable = a.find(param) != a.end();
474 can_automate (param);
476 boost::shared_ptr<AutomationList> list(new AutomationList(param, desc));
477 boost::shared_ptr<AutomationControl> c (new PluginControl(this, param, desc, list));
479 c->set_flags (Controllable::Flag ((int)c->flags() | Controllable::NotAutomatable));
482 plugin->set_automation_control (i, c);
486 const Plugin::PropertyDescriptors& pdl (plugin->get_supported_properties ());
487 for (Plugin::PropertyDescriptors::const_iterator p = pdl.begin(); p != pdl.end(); ++p) {
488 Evoral::Parameter param (PluginPropertyAutomation, 0, p->first);
489 const ParameterDescriptor& desc = plugin->get_property_descriptor(param.id());
490 if (desc.datatype != Variant::NOTHING) {
491 boost::shared_ptr<AutomationList> list;
492 if (Variant::type_is_numeric(desc.datatype)) {
493 list = boost::shared_ptr<AutomationList>(new AutomationList(param, desc));
495 boost::shared_ptr<AutomationControl> c (new PluginPropertyControl(this, param, desc, list));
496 if (!Variant::type_is_numeric(desc.datatype)) {
497 c->set_flags (Controllable::Flag ((int)c->flags() | Controllable::NotAutomatable));
503 _bypass_port = plugin->designated_bypass_port ();
505 /* special case VST effSetBypass */
506 if (_bypass_port == UINT32_MAX -1) {
507 // emulate VST Bypass
508 Evoral::Parameter param (PluginAutomation, 0, _bypass_port);
509 ParameterDescriptor desc;
510 desc.label = _("Plugin Enable");
515 boost::shared_ptr<AutomationList> list(new AutomationList(param, desc));
516 boost::shared_ptr<AutomationControl> c (new PluginControl(this, param, desc, list));
520 if (_bypass_port != UINT32_MAX) {
521 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter (PluginAutomation, 0, _bypass_port));
522 if (0 == (ac->flags () & Controllable::NotAutomatable)) {
523 ac->alist()->automation_state_changed.connect_same_thread (*this, boost::bind (&PluginInsert::bypassable_changed, this));
524 ac->Changed.connect_same_thread (*this, boost::bind (&PluginInsert::enable_changed, this));
527 plugin->PresetPortSetValue.connect_same_thread (*this, boost::bind (&PluginInsert::preset_load_set_value, this, _1, _2));
530 /** Called when something outside of this host has modified a plugin
531 * parameter. Responsible for propagating the change to two places:
533 * 1) anything listening to the Control itself
534 * 2) any replicated plugins that make up this PluginInsert.
536 * The PluginInsert is connected to the ParameterChangedExternally signal for
537 * the first (primary) plugin, and here broadcasts that change to any others.
539 * XXX We should probably drop this whole replication idea (Paul, October 2015)
540 * since it isn't used by sensible plugin APIs (AU, LV2).
543 PluginInsert::parameter_changed_externally (uint32_t which, float val)
545 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter (PluginAutomation, 0, which));
547 /* First propagation: alter the underlying value of the control,
548 * without telling the plugin(s) that own/use it to set it.
555 boost::shared_ptr<PluginControl> pc = boost::dynamic_pointer_cast<PluginControl> (ac);
558 pc->catch_up_with_external_value (val);
561 /* Second propagation: tell all plugins except the first to
562 update the value of this parameter. For sane plugin APIs,
563 there are no other plugins, so this is a no-op in those
567 Plugins::iterator i = _plugins.begin();
569 /* don't set the first plugin, just all the slaves */
571 if (i != _plugins.end()) {
573 for (; i != _plugins.end(); ++i) {
574 (*i)->set_parameter (which, val);
580 PluginInsert::set_block_size (pframes_t nframes)
583 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
584 if ((*i)->set_block_size (nframes) != 0) {
592 PluginInsert::automation_run (samplepos_t start, pframes_t nframes)
594 // XXX does not work when rolling backwards
595 if (_loop_location && nframes > 0) {
596 const samplepos_t loop_start = _loop_location->start ();
597 const samplepos_t loop_end = _loop_location->end ();
598 const samplecnt_t looplen = loop_end - loop_start;
600 samplecnt_t remain = nframes;
601 samplepos_t start_pos = start;
604 if (start_pos >= loop_end) {
605 sampleoffset_t start_off = (start_pos - loop_start) % looplen;
606 start_pos = loop_start + start_off;
608 samplecnt_t move = std::min ((samplecnt_t)nframes, loop_end - start_pos);
610 Automatable::automation_run (start_pos, move);
616 Automatable::automation_run (start, nframes);
620 PluginInsert::find_next_event (double now, double end, Evoral::ControlEvent& next_event, bool only_active) const
622 bool rv = Automatable::find_next_event (now, end, next_event, only_active);
624 if (_loop_location && now < end) {
626 end = ceil (next_event.when);
628 const samplepos_t loop_end = _loop_location->end ();
629 assert (now < loop_end); // due to map_loop_range ()
630 if (end > loop_end) {
631 next_event.when = loop_end;
639 PluginInsert::activate ()
641 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
645 Processor::activate ();
646 /* when setting state e.g ProcessorBox::paste_processor_state ()
647 * the plugin is not yet owned by a route.
648 * but no matter. Route::add_processors() will call activate () again
653 if (_plugin_signal_latency != signal_latency ()) {
654 _plugin_signal_latency = signal_latency ();
660 PluginInsert::deactivate ()
662 Processor::deactivate ();
664 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
667 if (_plugin_signal_latency != signal_latency ()) {
668 _plugin_signal_latency = signal_latency ();
674 PluginInsert::flush ()
676 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
682 PluginInsert::enable (bool yn)
684 if (_bypass_port == UINT32_MAX) {
691 if (!_pending_active) {
694 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter (PluginAutomation, 0, _bypass_port));
695 const double val = yn ? 1.0 : 0.0;
696 ac->set_value (val, Controllable::NoGroup);
698 #ifdef ALLOW_VST_BYPASS_TO_FAIL // yet unused, see also vst_plugin.cc
699 /* special case VST.. bypass may fail */
700 if (_bypass_port == UINT32_MAX - 1) {
701 /* check if bypass worked */
702 if (ac->get_value () != val) {
703 warning << _("PluginInsert: VST Bypass failed, falling back to host bypass.") << endmsg;
704 // set plugin to enabled (not-byassed)
705 ac->set_value (1.0, Controllable::NoGroup);
706 // ..and use host-provided hard-bypass
721 PluginInsert::enabled () const
723 if (_bypass_port == UINT32_MAX) {
724 return Processor::enabled ();
726 boost::shared_ptr<const AutomationControl> ac = boost::const_pointer_cast<AutomationControl> (automation_control (Evoral::Parameter (PluginAutomation, 0, _bypass_port)));
727 return (ac->get_value () > 0 && _pending_active);
732 PluginInsert::bypassable () const
734 if (_bypass_port == UINT32_MAX) {
737 boost::shared_ptr<const AutomationControl> ac = boost::const_pointer_cast<AutomationControl> (automation_control (Evoral::Parameter (PluginAutomation, 0, _bypass_port)));
739 return !ac->automation_playback ();
744 PluginInsert::enable_changed ()
750 PluginInsert::bypassable_changed ()
752 BypassableChanged ();
756 PluginInsert::write_immediate_event (size_t size, const uint8_t* buf)
759 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
760 if (!(*i)->write_immediate_event (size, buf)) {
768 PluginInsert::preset_load_set_value (uint32_t p, float v)
770 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter(PluginAutomation, 0, p));
775 if (ac->automation_state() & Play) {
780 ac->set_value (v, Controllable::NoGroup);
785 PluginInsert::inplace_silence_unconnected (BufferSet& bufs, const PinMappings& out_map, samplecnt_t nframes, samplecnt_t offset) const
787 // TODO optimize: store "unconnected" in a fixed set.
788 // it only changes on reconfiguration.
789 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
790 for (uint32_t out = 0; out < bufs.count().get (*t); ++out) {
792 if (*t == DataType::MIDI && out == 0 && has_midi_bypass ()) {
793 mapped = true; // in-place Midi bypass
795 for (uint32_t pc = 0; pc < get_count() && !mapped; ++pc) {
796 PinMappings::const_iterator i = out_map.find (pc);
797 if (i == out_map.end ()) {
800 const ChanMapping& outmap (i->second);
801 for (uint32_t o = 0; o < natural_output_streams().get (*t); ++o) {
803 uint32_t idx = outmap.get (*t, o, &valid);
804 if (valid && idx == out) {
811 bufs.get (*t, out).silence (nframes, offset);
818 PluginInsert::connect_and_run (BufferSet& bufs, samplepos_t start, samplepos_t end, double speed, pframes_t nframes, samplecnt_t offset, bool with_auto)
820 // TODO: atomically copy maps & _no_inplace
821 PinMappings in_map (_in_map);
822 PinMappings out_map (_out_map);
823 ChanMapping thru_map (_thru_map);
824 if (_mapping_changed) { // ToDo use a counters, increment until match.
825 _no_inplace = check_inplace ();
826 _mapping_changed = false;
829 if (_latency_changed) {
830 /* delaylines are configured with the max possible latency (as reported by the plugin)
831 * so this won't allocate memory (unless the plugin lied about its max latency)
832 * It may still 'click' though, since the fixed delaylines are not de-clicked.
833 * Then again plugin-latency changes are not click-free to begin with.
835 * This is also worst case, there is currently no concept of per-stream latency.
837 * e.g. Two identical latent plugins:
838 * 1st plugin: process left (latent), bypass right.
839 * 2nd plugin: bypass left, process right (latent).
840 * -> currently this yields 2 times latency of the plugin,
842 _latency_changed = false;
843 _delaybuffers.set (ChanCount::max(bufs.count(), _configured_out), plugin_latency ());
846 if (_match.method == Split && !_no_inplace) {
847 // TODO: also use this optimization if one source-buffer
848 // feeds _all_ *connected* inputs.
849 // currently this is *first* buffer to all only --
850 // see PluginInsert::check_inplace
851 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
852 if (_configured_internal.get (*t) == 0) {
856 uint32_t first_idx = in_map[0].get (*t, 0, &valid);
857 assert (valid && first_idx == 0); // check_inplace ensures this
858 /* copy the first stream's buffer contents to the others */
859 for (uint32_t i = 1; i < natural_input_streams ().get (*t); ++i) {
860 uint32_t idx = in_map[0].get (*t, i, &valid);
863 bufs.get (*t, i).read_from (bufs.get (*t, first_idx), nframes, offset, offset);
867 /* the copy operation produces a linear monotonic input map */
868 in_map[0] = ChanMapping (natural_input_streams ());
871 bufs.set_count(ChanCount::max(bufs.count(), _configured_internal));
872 bufs.set_count(ChanCount::max(bufs.count(), _configured_out));
878 for (Controls::iterator li = controls().begin(); li != controls().end(); ++li, ++n) {
880 boost::shared_ptr<AutomationControl> c
881 = boost::dynamic_pointer_cast<AutomationControl>(li->second);
883 if (c->list() && c->automation_playback()) {
886 const float val = c->list()->rt_safe_eval (start, valid);
889 /* This is the ONLY place where we are
891 * AutomationControl::set_value_unchecked(). We
892 * know that the control is in
893 * automation playback mode, so no
894 * check on writable() is required
895 * (which must be done in AutomationControl::set_value()
898 c->set_value_unchecked(val);
905 /* Calculate if, and how many samples we need to collect for analysis */
906 samplecnt_t collect_signal_nframes = (_signal_analysis_collect_nframes_max -
907 _signal_analysis_collected_nframes);
908 if (nframes < collect_signal_nframes) { // we might not get all samples now
909 collect_signal_nframes = nframes;
912 if (collect_signal_nframes > 0) {
914 //std::cerr << "collect input, bufs " << bufs.count().n_audio() << " count, " << bufs.available().n_audio() << " available" << std::endl;
915 //std::cerr << " streams " << internal_input_streams().n_audio() << std::endl;
916 //std::cerr << "filling buffer with " << collect_signal_nframes << " samples at " << _signal_analysis_collected_nframes << std::endl;
918 _signal_analysis_inputs.set_count(input_streams());
920 for (uint32_t i = 0; i < input_streams().n_audio(); ++i) {
921 _signal_analysis_inputs.get_audio(i).read_from (
923 collect_signal_nframes,
924 _signal_analysis_collected_nframes); // offset is for target buffer
929 if (is_channelstrip ()) {
930 if (_configured_in.n_audio() > 0) {
931 ChanMapping mb_in_map (ChanCount::min (_configured_in, ChanCount (DataType::AUDIO, 2)));
932 ChanMapping mb_out_map (ChanCount::min (_configured_out, ChanCount (DataType::AUDIO, 2)));
934 _plugins.front()->connect_and_run (bufs, start, end, speed, mb_in_map, mb_out_map, nframes, offset);
936 for (uint32_t out = _configured_in.n_audio (); out < bufs.count().get (DataType::AUDIO); ++out) {
937 bufs.get (DataType::AUDIO, out).silence (nframes, offset);
943 // TODO optimize -- build maps once.
945 BufferSet& inplace_bufs = _session.get_noinplace_buffers();
946 ARDOUR::ChanMapping used_outputs;
948 assert (inplace_bufs.count () >= natural_input_streams () + _configured_out);
950 /* build used-output map */
951 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i, ++pc) {
952 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
953 for (uint32_t out = 0; out < natural_output_streams().get (*t); ++out) {
955 uint32_t out_idx = out_map[pc].get (*t, out, &valid);
957 used_outputs.set (*t, out_idx, 1); // mark as used
962 /* copy thru data to outputs before processing in-place */
963 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
964 for (uint32_t out = 0; out < bufs.count().get (*t); ++out) {
966 uint32_t in_idx = thru_map.get (*t, out, &valid);
967 uint32_t m = out + natural_input_streams ().get (*t);
969 _delaybuffers.delay (*t, out, inplace_bufs.get (*t, m), bufs.get (*t, in_idx), nframes, offset, offset);
970 used_outputs.set (*t, out, 1); // mark as used
972 used_outputs.get (*t, out, &valid);
974 /* the plugin is expected to write here, but may not :(
975 * (e.g. drumgizmo w/o kit loaded)
977 inplace_bufs.get (*t, m).silence (nframes);
984 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i, ++pc) {
986 ARDOUR::ChanMapping i_in_map (natural_input_streams());
987 ARDOUR::ChanMapping i_out_map (out_map[pc]);
988 ARDOUR::ChanCount mapped;
990 /* map inputs sequentially */
991 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
992 for (uint32_t in = 0; in < natural_input_streams().get (*t); ++in) {
994 uint32_t in_idx = in_map[pc].get (*t, in, &valid);
995 uint32_t m = mapped.get (*t);
997 inplace_bufs.get (*t, m).read_from (bufs.get (*t, in_idx), nframes, offset, offset);
999 inplace_bufs.get (*t, m).silence (nframes, offset);
1001 mapped.set (*t, m + 1);
1005 /* outputs are mapped to inplace_bufs after the inputs */
1006 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1007 i_out_map.offset_to (*t, natural_input_streams ().get (*t));
1010 if ((*i)->connect_and_run (inplace_bufs, start, end, speed, i_in_map, i_out_map, nframes, offset)) {
1015 /* all instances have completed, now copy data that was written
1016 * and zero unconnected buffers */
1017 ARDOUR::ChanMapping nonzero_out (used_outputs);
1018 if (has_midi_bypass ()) {
1019 nonzero_out.set (DataType::MIDI, 0, 1); // Midi bypass.
1021 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1022 for (uint32_t out = 0; out < bufs.count().get (*t); ++out) {
1024 used_outputs.get (*t, out, &valid);
1026 nonzero_out.get (*t, out, &valid);
1028 bufs.get (*t, out).silence (nframes, offset);
1031 uint32_t m = out + natural_input_streams ().get (*t);
1032 bufs.get (*t, out).read_from (inplace_bufs.get (*t, m), nframes, offset, offset);
1037 /* in-place processing */
1039 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i, ++pc) {
1040 if ((*i)->connect_and_run(bufs, start, end, speed, in_map[pc], out_map[pc], nframes, offset)) {
1044 // now silence unconnected outputs
1045 inplace_silence_unconnected (bufs, _out_map, nframes, offset);
1048 if (collect_signal_nframes > 0) {
1050 //std::cerr << " output, bufs " << bufs.count().n_audio() << " count, " << bufs.available().n_audio() << " available" << std::endl;
1051 //std::cerr << " streams " << internal_output_streams().n_audio() << std::endl;
1053 _signal_analysis_outputs.set_count(output_streams());
1055 for (uint32_t i = 0; i < output_streams().n_audio(); ++i) {
1056 _signal_analysis_outputs.get_audio(i).read_from(
1058 collect_signal_nframes,
1059 _signal_analysis_collected_nframes); // offset is for target buffer
1062 _signal_analysis_collected_nframes += collect_signal_nframes;
1063 assert(_signal_analysis_collected_nframes <= _signal_analysis_collect_nframes_max);
1065 if (_signal_analysis_collected_nframes == _signal_analysis_collect_nframes_max) {
1066 _signal_analysis_collect_nframes_max = 0;
1067 _signal_analysis_collected_nframes = 0;
1069 AnalysisDataGathered(&_signal_analysis_inputs,
1070 &_signal_analysis_outputs);
1074 if (_plugin_signal_latency != signal_latency ()) {
1075 _plugin_signal_latency = signal_latency ();
1081 PluginInsert::bypass (BufferSet& bufs, pframes_t nframes)
1083 /* bypass the plugin(s) not the whole processor.
1084 * -> use mappings just like connect_and_run
1087 // TODO: atomically copy maps & _no_inplace
1088 const ChanMapping in_map (no_sc_input_map ());
1089 const ChanMapping out_map (output_map ());
1090 if (_mapping_changed) {
1091 _no_inplace = check_inplace ();
1092 _mapping_changed = false;
1095 bufs.set_count(ChanCount::max(bufs.count(), _configured_internal));
1096 bufs.set_count(ChanCount::max(bufs.count(), _configured_out));
1099 ChanMapping thru_map (_thru_map);
1101 BufferSet& inplace_bufs = _session.get_noinplace_buffers();
1103 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1104 for (uint32_t in = 0; in < _configured_internal.get (*t); ++in) {
1105 inplace_bufs.get (*t, in).read_from (bufs.get (*t, in), nframes, 0, 0);
1108 ARDOUR::ChanMapping used_outputs;
1110 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1111 for (uint32_t out = 0; out < _configured_out.get (*t); ++out) {
1113 uint32_t in_idx = thru_map.get (*t, out, &valid);
1115 bufs.get (*t, out).read_from (inplace_bufs.get (*t, in_idx), nframes, 0, 0);
1116 used_outputs.set (*t, out, 1); // mark as used
1120 // plugin no-op: assume every plugin has an internal identity map
1121 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1122 for (uint32_t out = 0; out < _configured_out.get (*t); ++out) {
1124 uint32_t src_idx = out_map.get_src (*t, out, &valid);
1128 uint32_t in_idx = in_map.get (*t, src_idx, &valid);
1132 bufs.get (*t, out).read_from (inplace_bufs.get (*t, in_idx), nframes, 0, 0);
1133 used_outputs.set (*t, out, 1); // mark as used
1136 // now silence all unused outputs
1137 if (has_midi_bypass ()) {
1138 used_outputs.set (DataType::MIDI, 0, 1); // Midi bypass.
1140 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1141 for (uint32_t out = 0; out < _configured_out.get (*t); ++out) {
1143 used_outputs.get (*t, out, &valid);
1145 bufs.get (*t, out).silence (nframes, 0);
1150 if (_match.method == Split) {
1151 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1152 if (_configured_internal.get (*t) == 0) {
1155 // copy/feeds _all_ *connected* inputs, copy the first buffer
1157 uint32_t first_idx = in_map.get (*t, 0, &valid);
1158 assert (valid && first_idx == 0); // check_inplace ensures this
1159 for (uint32_t i = 1; i < natural_input_streams ().get (*t); ++i) {
1160 uint32_t idx = in_map.get (*t, i, &valid);
1163 bufs.get (*t, i).read_from (bufs.get (*t, first_idx), nframes, 0, 0);
1169 // apply output map and/or monotonic but not identity i/o mappings
1170 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1171 for (uint32_t out = 0; out < _configured_out.get (*t); ++out) {
1173 uint32_t src_idx = out_map.get_src (*t, out, &valid);
1175 bufs.get (*t, out).silence (nframes, 0);
1178 uint32_t in_idx = in_map.get (*t, src_idx, &valid);
1180 bufs.get (*t, out).silence (nframes, 0);
1183 if (in_idx != src_idx) {
1184 bufs.get (*t, out).read_from (bufs.get (*t, in_idx), nframes, 0, 0);
1192 PluginInsert::silence (samplecnt_t nframes, samplepos_t start_sample)
1194 automation_run (start_sample, nframes); // evaluate automation only
1197 // XXX delaybuffers need to be offset by nframes
1201 _delaybuffers.flush ();
1203 ChanMapping in_map (natural_input_streams ());
1204 ChanMapping out_map (natural_output_streams ());
1205 ChanCount maxbuf = ChanCount::max (natural_input_streams (), natural_output_streams());
1207 if (is_channelstrip ()) {
1208 if (_configured_in.n_audio() > 0) {
1209 _plugins.front()->connect_and_run (_session.get_scratch_buffers (maxbuf, true), start_sample, start_sample + nframes, 1.0, in_map, out_map, nframes, 0);
1213 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
1214 (*i)->connect_and_run (_session.get_scratch_buffers (maxbuf, true), start_sample, start_sample + nframes, 1.0, in_map, out_map, nframes, 0);
1219 PluginInsert::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_sample, double speed, pframes_t nframes, bool)
1222 // collect sidechain input for complete cycle (!)
1223 // TODO we need delaylines here for latency compensation
1224 _sidechain->run (bufs, start_sample, end_sample, speed, nframes, true);
1227 if (_pending_active) {
1228 /* run as normal if we are active or moving from inactive to active */
1230 if (_session.transport_rolling() || _session.bounce_processing()) {
1231 automate_and_run (bufs, start_sample, end_sample, speed, nframes);
1233 Glib::Threads::Mutex::Lock lm (control_lock(), Glib::Threads::TRY_LOCK);
1234 connect_and_run (bufs, start_sample, end_sample, speed, nframes, 0, lm.locked());
1238 // XXX should call ::silence() to run plugin(s) for consistent load.
1239 // We'll need to change this anyway when bypass can be automated
1240 bypass (bufs, nframes);
1241 automation_run (start_sample, nframes); // evaluate automation only
1242 _delaybuffers.flush ();
1245 _active = _pending_active;
1247 /* we have no idea whether the plugin generated silence or not, so mark
1248 * all buffers appropriately.
1253 PluginInsert::automate_and_run (BufferSet& bufs, samplepos_t start, samplepos_t end, double speed, pframes_t nframes)
1255 Evoral::ControlEvent next_event (0, 0.0f);
1256 samplecnt_t offset = 0;
1258 Glib::Threads::Mutex::Lock lm (control_lock(), Glib::Threads::TRY_LOCK);
1261 connect_and_run (bufs, start, end, speed, nframes, offset, false);
1265 /* map start back into loop-range, adjust end */
1266 map_loop_range (start, end);
1268 if (!find_next_event (start, end, next_event) || _plugins.front()->requires_fixed_sized_buffers()) {
1270 /* no events have a time within the relevant range */
1272 connect_and_run (bufs, start, end, speed, nframes, offset, true);
1278 samplecnt_t cnt = min (((samplecnt_t) ceil (next_event.when) - start), (samplecnt_t) nframes);
1280 connect_and_run (bufs, start, start + cnt, speed, cnt, offset, true); // XXX (start + cnt) * speed
1286 map_loop_range (start, end);
1288 if (!find_next_event (start, end, next_event)) {
1293 /* cleanup anything that is left to do */
1296 connect_and_run (bufs, start, start + nframes, speed, nframes, offset, true);
1301 PluginInsert::default_parameter_value (const Evoral::Parameter& param)
1303 if (param.type() != PluginAutomation)
1306 if (_plugins.empty()) {
1307 fatal << _("programming error: ") << X_("PluginInsert::default_parameter_value() called with no plugin")
1309 abort(); /*NOTREACHED*/
1312 return _plugins[0]->default_value (param.id());
1317 PluginInsert::can_reset_all_parameters ()
1320 uint32_t params = 0;
1321 for (uint32_t par = 0; par < _plugins[0]->parameter_count(); ++par) {
1323 const uint32_t cid = _plugins[0]->nth_parameter (par, ok);
1325 if (!ok || !_plugins[0]->parameter_is_input(cid)) {
1329 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter(PluginAutomation, 0, cid));
1335 if (ac->automation_state() & Play) {
1340 return all && (params > 0);
1344 PluginInsert::reset_parameters_to_default ()
1348 for (uint32_t par = 0; par < _plugins[0]->parameter_count(); ++par) {
1350 const uint32_t cid = _plugins[0]->nth_parameter (par, ok);
1352 if (!ok || !_plugins[0]->parameter_is_input(cid)) {
1356 const float dflt = _plugins[0]->default_value (cid);
1357 const float curr = _plugins[0]->get_parameter (cid);
1363 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter(PluginAutomation, 0, cid));
1368 if (ac->automation_state() & Play) {
1373 ac->set_value (dflt, Controllable::NoGroup);
1378 boost::shared_ptr<Plugin>
1379 PluginInsert::plugin_factory (boost::shared_ptr<Plugin> other)
1381 boost::shared_ptr<LadspaPlugin> lp;
1382 boost::shared_ptr<LuaProc> lua;
1384 boost::shared_ptr<LV2Plugin> lv2p;
1386 #ifdef WINDOWS_VST_SUPPORT
1387 boost::shared_ptr<WindowsVSTPlugin> vp;
1389 #ifdef LXVST_SUPPORT
1390 boost::shared_ptr<LXVSTPlugin> lxvp;
1392 #ifdef MACVST_SUPPORT
1393 boost::shared_ptr<MacVSTPlugin> mvp;
1395 #ifdef AUDIOUNIT_SUPPORT
1396 boost::shared_ptr<AUPlugin> ap;
1399 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
1400 return boost::shared_ptr<Plugin> (new LadspaPlugin (*lp));
1401 } else if ((lua = boost::dynamic_pointer_cast<LuaProc> (other)) != 0) {
1402 return boost::shared_ptr<Plugin> (new LuaProc (*lua));
1404 } else if ((lv2p = boost::dynamic_pointer_cast<LV2Plugin> (other)) != 0) {
1405 return boost::shared_ptr<Plugin> (new LV2Plugin (*lv2p));
1407 #ifdef WINDOWS_VST_SUPPORT
1408 } else if ((vp = boost::dynamic_pointer_cast<WindowsVSTPlugin> (other)) != 0) {
1409 return boost::shared_ptr<Plugin> (new WindowsVSTPlugin (*vp));
1411 #ifdef LXVST_SUPPORT
1412 } else if ((lxvp = boost::dynamic_pointer_cast<LXVSTPlugin> (other)) != 0) {
1413 return boost::shared_ptr<Plugin> (new LXVSTPlugin (*lxvp));
1415 #ifdef MACVST_SUPPORT
1416 } else if ((mvp = boost::dynamic_pointer_cast<MacVSTPlugin> (other)) != 0) {
1417 return boost::shared_ptr<Plugin> (new MacVSTPlugin (*mvp));
1419 #ifdef AUDIOUNIT_SUPPORT
1420 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
1421 return boost::shared_ptr<Plugin> (new AUPlugin (*ap));
1425 fatal << string_compose (_("programming error: %1"),
1426 X_("unknown plugin type in PluginInsert::plugin_factory"))
1428 abort(); /*NOTREACHED*/
1429 return boost::shared_ptr<Plugin> ((Plugin*) 0);
1433 PluginInsert::set_input_map (uint32_t num, ChanMapping m) {
1434 if (num < _in_map.size()) {
1435 bool changed = _in_map[num] != m;
1437 changed |= sanitize_maps ();
1439 PluginMapChanged (); /* EMIT SIGNAL */
1440 _mapping_changed = true;
1441 _session.set_dirty();
1447 PluginInsert::set_output_map (uint32_t num, ChanMapping m) {
1448 if (num < _out_map.size()) {
1449 bool changed = _out_map[num] != m;
1451 changed |= sanitize_maps ();
1453 PluginMapChanged (); /* EMIT SIGNAL */
1454 _mapping_changed = true;
1455 _session.set_dirty();
1461 PluginInsert::set_thru_map (ChanMapping m) {
1462 bool changed = _thru_map != m;
1464 changed |= sanitize_maps ();
1466 PluginMapChanged (); /* EMIT SIGNAL */
1467 _mapping_changed = true;
1468 _session.set_dirty();
1473 PluginInsert::pre_seed (const ChanCount& in, const ChanCount& out,
1474 const ChanMapping& im, const ChanMapping& om, const ChanMapping& tm)
1476 if (_configured) { return false; }
1477 _configured_in = in;
1478 _configured_out = out;
1482 _maps_from_state = in.n_total () > 0 && out.n_total () > 0;
1487 PluginInsert::input_map () const
1491 for (PinMappings::const_iterator i = _in_map.begin (); i != _in_map.end (); ++i, ++pc) {
1492 ChanMapping m (i->second);
1493 const ChanMapping::Mappings& mp ((*i).second.mappings());
1494 for (ChanMapping::Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
1495 for (ChanMapping::TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
1496 rv.set (tm->first, i->first + pc * natural_input_streams().get(tm->first), i->second);
1505 PluginInsert::no_sc_input_map () const
1509 for (PinMappings::const_iterator i = _in_map.begin (); i != _in_map.end (); ++i, ++pc) {
1510 ChanMapping m (i->second);
1511 const ChanMapping::Mappings& mp ((*i).second.mappings());
1512 for (ChanMapping::Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
1513 uint32_t ins = natural_input_streams().get(tm->first) - _cached_sidechain_pins.get(tm->first);
1514 for (ChanMapping::TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
1515 if (i->first < ins) {
1516 rv.set (tm->first, i->first + pc * ins, i->second);
1525 PluginInsert::output_map () const
1529 for (PinMappings::const_iterator i = _out_map.begin (); i != _out_map.end (); ++i, ++pc) {
1530 ChanMapping m (i->second);
1531 const ChanMapping::Mappings& mp ((*i).second.mappings());
1532 for (ChanMapping::Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
1533 for (ChanMapping::TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
1534 rv.set (tm->first, i->first + pc * natural_output_streams().get(tm->first), i->second);
1538 if (has_midi_bypass ()) {
1539 rv.set (DataType::MIDI, 0, 0);
1546 PluginInsert::has_midi_bypass () const
1548 if (_configured_in.n_midi () == 1 && _configured_out.n_midi () == 1
1549 && natural_output_streams ().n_midi () == 0) {
1556 PluginInsert::has_midi_thru () const
1558 if (_configured_in.n_midi () == 1 && _configured_out.n_midi () == 1
1559 && natural_input_streams ().n_midi () == 0 && natural_output_streams ().n_midi () == 0) {
1567 PluginInsert::is_channelstrip () const {
1568 return _plugins.front()->is_channelstrip();
1573 PluginInsert::check_inplace ()
1575 bool inplace_ok = !_plugins.front()->inplace_broken ();
1577 if (_thru_map.n_total () > 0) {
1578 // TODO once midi-bypass is part of the mapping, ignore it
1582 if (_match.method == Split && inplace_ok) {
1583 assert (get_count() == 1);
1584 assert (_in_map.size () == 1);
1585 if (!_out_map[0].is_monotonic ()) {
1588 if (_configured_internal != _configured_in) {
1589 /* no sidechain -- TODO we could allow this with
1590 * some more logic in PluginInsert::connect_and_run().
1592 * PluginInsert::reset_map() already maps it.
1597 for (DataType::iterator t = DataType::begin(); t != DataType::end() && inplace_ok; ++t) {
1598 if (_configured_internal.get (*t) == 0) {
1602 uint32_t first_idx = _in_map[0].get (*t, 0, &valid);
1603 if (!valid || first_idx != 0) {
1604 // so far only allow to copy the *first* stream's buffer to others
1607 for (uint32_t i = 1; i < natural_input_streams ().get (*t); ++i) {
1608 uint32_t idx = _in_map[0].get (*t, i, &valid);
1609 if (valid && idx != first_idx) {
1618 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: In Place Split Map\n", name()));
1623 for (uint32_t pc = 0; pc < get_count() && inplace_ok ; ++pc) {
1624 if (!_in_map[pc].is_monotonic ()) {
1627 if (!_out_map[pc].is_monotonic ()) {
1633 /* check if every output is fed by the corresponding input
1635 * this prevents in-port 1 -> sink-pin 2 || source-pin 1 -> out port 1, source-pin 2 -> out port 2
1636 * (with in-place, source-pin 1 -> out port 1 overwrites in-port 1)
1638 * but allows in-port 1 -> sink-pin 2 || source-pin 2 -> out port 1
1640 ChanMapping in_map (input_map ());
1641 const ChanMapping::Mappings out_m (output_map ().mappings ());
1642 for (ChanMapping::Mappings::const_iterator t = out_m.begin (); t != out_m.end () && inplace_ok; ++t) {
1643 for (ChanMapping::TypeMapping::const_iterator c = (*t).second.begin (); c != (*t).second.end () ; ++c) {
1644 /* src-pin: c->first, out-port: c->second */
1646 uint32_t in_port = in_map.get (t->first, c->first, &valid);
1647 if (valid && in_port != c->second) {
1655 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: %2\n", name(), inplace_ok ? "In-Place" : "No Inplace Processing"));
1656 return !inplace_ok; // no-inplace
1660 PluginInsert::sanitize_maps ()
1662 bool changed = false;
1663 /* strip dead wood */
1664 PinMappings new_ins;
1665 PinMappings new_outs;
1666 ChanMapping new_thru;
1668 for (uint32_t pc = 0; pc < get_count(); ++pc) {
1670 ChanMapping new_out;
1671 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1672 for (uint32_t i = 0; i < natural_input_streams().get (*t); ++i) {
1674 uint32_t idx = _in_map[pc].get (*t, i, &valid);
1675 if (valid && idx < _configured_internal.get (*t)) {
1676 new_in.set (*t, i, idx);
1679 for (uint32_t o = 0; o < natural_output_streams().get (*t); ++o) {
1681 uint32_t idx = _out_map[pc].get (*t, o, &valid);
1682 if (valid && idx < _configured_out.get (*t)) {
1683 new_out.set (*t, o, idx);
1687 if (_in_map[pc] != new_in || _out_map[pc] != new_out) {
1690 new_ins[pc] = new_in;
1691 new_outs[pc] = new_out;
1694 /* prevent dup output assignments */
1695 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1696 for (uint32_t o = 0; o < _configured_out.get (*t); ++o) {
1697 bool mapped = false;
1698 for (uint32_t pc = 0; pc < get_count(); ++pc) {
1700 uint32_t idx = new_outs[pc].get_src (*t, o, &valid);
1701 if (valid && mapped) {
1702 new_outs[pc].unset (*t, idx);
1710 /* remove excess thru */
1711 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1712 for (uint32_t o = 0; o < _configured_out.get (*t); ++o) {
1714 uint32_t idx = _thru_map.get (*t, o, &valid);
1715 if (valid && idx < _configured_internal.get (*t)) {
1716 new_thru.set (*t, o, idx);
1721 /* prevent out + thru, existing plugin outputs override thru */
1722 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1723 for (uint32_t o = 0; o < _configured_out.get (*t); ++o) {
1724 bool mapped = false;
1726 for (uint32_t pc = 0; pc < get_count(); ++pc) {
1727 new_outs[pc].get_src (*t, o, &mapped);
1728 if (mapped) { break; }
1730 if (!mapped) { continue; }
1731 uint32_t idx = new_thru.get (*t, o, &valid);
1733 new_thru.unset (*t, idx);
1738 if (has_midi_bypass ()) {
1739 // TODO: include midi-bypass in the thru set,
1740 // remove dedicated handling.
1741 new_thru.unset (DataType::MIDI, 0);
1744 if (_in_map != new_ins || _out_map != new_outs || _thru_map != new_thru) {
1748 _out_map = new_outs;
1749 _thru_map = new_thru;
1755 PluginInsert::reset_map (bool emit)
1757 const PinMappings old_in (_in_map);
1758 const PinMappings old_out (_out_map);
1762 _thru_map = ChanMapping ();
1764 /* build input map */
1765 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1766 uint32_t sc = 0; // side-chain round-robin (all instances)
1768 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i, ++pc) {
1769 const uint32_t nis = natural_input_streams ().get(*t);
1770 const uint32_t stride = nis - sidechain_input_pins().get (*t);
1772 /* SC inputs are last in the plugin-insert.. */
1773 const uint32_t sc_start = _configured_in.get (*t);
1774 const uint32_t sc_len = _configured_internal.get (*t) - sc_start;
1775 /* ...but may not be at the end of the plugin ports.
1776 * in case the side-chain is not the last port, shift connections back.
1777 * and connect to side-chain
1780 uint32_t ic = 0; // split inputs
1781 const uint32_t cend = _configured_in.get (*t);
1783 for (uint32_t in = 0; in < nis; ++in) {
1784 const Plugin::IOPortDescription& iod (_plugins[pc]->describe_io_port (*t, true, in));
1785 if (iod.is_sidechain) {
1786 /* connect sidechain sinks to sidechain inputs in round-robin fashion */
1787 if (sc_len > 0) {// side-chain may be hidden
1788 _in_map[pc].set (*t, in, sc_start + sc);
1789 sc = (sc + 1) % sc_len;
1793 if (_match.method == Split) {
1794 if (cend == 0) { continue; }
1795 if (_strict_io && ic + stride * pc >= cend) {
1798 /* connect *no* sidechain sinks in round-robin fashion */
1799 _in_map[pc].set (*t, in, ic + stride * pc);
1800 if (_strict_io && (ic + 1) == cend) {
1803 ic = (ic + 1) % cend;
1805 uint32_t s = in - shift;
1806 if (stride * pc + s < cend) {
1807 _in_map[pc].set (*t, in, s + stride * pc);
1815 /* build output map */
1817 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i, ++pc) {
1818 _out_map[pc] = ChanMapping (ChanCount::min (natural_output_streams(), _configured_out));
1819 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1820 _out_map[pc].offset_to(*t, pc * natural_output_streams().get(*t));
1825 if (old_in == _in_map && old_out == _out_map) {
1829 PluginMapChanged (); /* EMIT SIGNAL */
1830 _mapping_changed = true;
1831 _session.set_dirty();
1837 PluginInsert::configure_io (ChanCount in, ChanCount out)
1839 Match old_match = _match;
1841 ChanCount old_internal;
1845 old_pins = natural_input_streams();
1846 old_in = _configured_in;
1847 old_out = _configured_out;
1848 old_internal = _configured_internal;
1850 _configured_in = in;
1851 _configured_internal = in;
1852 _configured_out = out;
1855 /* TODO hide midi-bypass, and custom outs. Best /fake/ "out" here.
1856 * (currently _sidechain->configure_io always succeeds
1857 * since Processor::configure_io() succeeds)
1859 if (!_sidechain->configure_io (in, out)) {
1860 DEBUG_TRACE (DEBUG::ChanMapping, "Sidechain configuration failed\n");
1863 _configured_internal += _sidechain->input()->n_ports();
1865 // include (static_cast<Route*>owner())->name() ??
1866 _sidechain->input ()-> set_pretty_name (string_compose (_("SC %1"), name ()));
1869 /* get plugin configuration */
1870 _match = private_can_support_io_configuration (in, out);
1872 if (DEBUG_ENABLED(DEBUG::ChanMapping)) {
1874 DEBUG_STR_APPEND(a, string_compose ("%1: ", name()));
1875 DEBUG_STR_APPEND(a, _match);
1876 DEBUG_TRACE (DEBUG::ChanMapping, DEBUG_STR(a).str());
1880 /* set the matching method and number of plugins that we will use to meet this configuration */
1881 if (set_count (_match.plugins) == false) {
1882 PluginIoReConfigure (); /* EMIT SIGNAL */
1883 _configured = false;
1887 /* configure plugins */
1888 switch (_match.method) {
1891 if (_plugins.front()->configure_io (natural_input_streams(), out) == false) {
1892 PluginIoReConfigure (); /* EMIT SIGNAL */
1893 _configured = false;
1899 ChanCount din (_configured_internal);
1900 ChanCount dout (din); // hint
1902 if (_custom_sinks.n_total () > 0) {
1903 din = _custom_sinks;
1906 } else if (_preset_out.n_audio () > 0) {
1907 dout.set (DataType::AUDIO, _preset_out.n_audio ());
1908 } else if (dout.n_midi () > 0 && dout.n_audio () == 0) {
1909 dout.set (DataType::AUDIO, 2);
1911 if (out.n_audio () == 0) { out.set (DataType::AUDIO, 1); }
1913 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: Delegate lookup : %2 %3\n", name(), din, dout));
1914 bool const r = _plugins.front()->can_support_io_configuration (din, dout, &useins);
1916 if (useins.n_audio() == 0) {
1919 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: Delegate configuration: %2 %3\n", name(), useins, dout));
1921 if (_plugins.front()->configure_io (useins, dout) == false) {
1922 PluginIoReConfigure (); /* EMIT SIGNAL */
1923 _configured = false;
1927 _custom_sinks = din;
1932 if (_plugins.front()->configure_io (in, out) == false) {
1933 PluginIoReConfigure (); /* EMIT SIGNAL */
1934 _configured = false;
1940 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: cfg:%2 state:%3 chn-in:%4 chn-out:%5 inpin:%6 match:%7 cust:%8 size-in:%9 size-out:%10\n",
1942 _configured ? "Y" : "N",
1943 _maps_from_state ? "Y" : "N",
1944 old_in == in ? "==" : "!=",
1945 old_out == out ? "==" : "!=",
1946 old_pins == natural_input_streams () ? "==" : "!=",
1947 old_match.method == _match.method ? "==" : "!=",
1948 old_match.custom_cfg == _match.custom_cfg ? "==" : "!=",
1949 _in_map.size() == get_count () ? "==" : "!=",
1950 _out_map.size() == get_count () ? "==" : "!="
1953 bool mapping_changed = false;
1954 if (old_in == in && old_out == out
1956 && old_pins == natural_input_streams ()
1957 && old_match.method == _match.method
1958 && old_match.custom_cfg == _match.custom_cfg
1959 && _in_map.size() == _out_map.size()
1960 && _in_map.size() == get_count ()
1962 /* If the configuration has not changed, keep the mapping */
1963 mapping_changed = sanitize_maps ();
1964 } else if (_match.custom_cfg && _configured) {
1965 /* don't touch the map in manual mode */
1966 mapping_changed = sanitize_maps ();
1969 if (is_channelstrip ()) {
1970 /* fake channel map - for wire display */
1973 _thru_map = ChanMapping ();
1974 _in_map[0] = ChanMapping (ChanCount::min (_configured_in, ChanCount (DataType::AUDIO, 2)));
1975 _out_map[0] = ChanMapping (ChanCount::min (_configured_out, ChanCount (DataType::AUDIO, 2)));
1976 /* set "thru" map for in-place forward of audio */
1977 for (uint32_t i = 2; i < _configured_in.n_audio(); ++i) {
1978 _thru_map.set (DataType::AUDIO, i, i);
1980 /* and midi (after implicit 1st channel bypass) */
1981 for (uint32_t i = 1; i < _configured_in.n_midi(); ++i) {
1982 _thru_map.set (DataType::MIDI, i, i);
1986 if (_maps_from_state && old_in == in && old_out == out) {
1987 mapping_changed = true;
1990 /* generate a new mapping */
1991 mapping_changed = reset_map (false);
1993 _maps_from_state = false;
1996 if (mapping_changed) {
1997 PluginMapChanged (); /* EMIT SIGNAL */
2000 if (DEBUG_ENABLED(DEBUG::ChanMapping)) {
2003 DEBUG_STR_APPEND(a, "\n--------<<--------\n");
2004 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i, ++pc) {
2006 DEBUG_STR_APPEND(a, "----><----\n");
2008 DEBUG_STR_APPEND(a, string_compose ("Channel Map for %1 plugin %2\n", name(), pc));
2009 DEBUG_STR_APPEND(a, " * Inputs:\n");
2010 DEBUG_STR_APPEND(a, _in_map[pc]);
2011 DEBUG_STR_APPEND(a, " * Outputs:\n");
2012 DEBUG_STR_APPEND(a, _out_map[pc]);
2014 DEBUG_STR_APPEND(a, " * Thru:\n");
2015 DEBUG_STR_APPEND(a, _thru_map);
2016 DEBUG_STR_APPEND(a, "-------->>--------\n");
2017 DEBUG_TRACE (DEBUG::ChanMapping, DEBUG_STR(a).str());
2022 _no_inplace = check_inplace ();
2023 _mapping_changed = false;
2025 /* only the "noinplace_buffers" thread buffers need to be this large,
2026 * this can be optimized. other buffers are fine with
2027 * ChanCount::max (natural_input_streams (), natural_output_streams())
2028 * and route.cc's max (configured_in, configured_out)
2030 * no-inplace copies "thru" outputs (to emulate in-place) for
2031 * all outputs (to prevent overwrite) into a temporary space
2032 * which also holds input buffers (in case the plugin does process
2033 * in-place and overwrites those).
2035 * this buffers need to be at least as
2036 * natural_input_streams () + possible outputs.
2038 * sidechain inputs add a constraint on the input:
2039 * configured input + sidechain (=_configured_internal)
2041 * NB. this also satisfies
2042 * max (natural_input_streams(), natural_output_streams())
2043 * which is needed for silence runs
2045 _required_buffers = ChanCount::max (_configured_internal,
2046 natural_input_streams () + ChanCount::max (_configured_out, natural_output_streams () * get_count ()));
2048 if (old_in != in || old_out != out || old_internal != _configured_internal
2049 || old_pins != natural_input_streams ()
2050 || (old_match.method != _match.method && (old_match.method == Split || _match.method == Split))
2052 PluginIoReConfigure (); /* EMIT SIGNAL */
2055 _delaybuffers.configure (_configured_out, _plugins.front ()->max_latency ());
2056 _latency_changed = true;
2058 // we don't know the analysis window size, so we must work with the
2059 // current buffer size here. each request for data fills in these
2060 // buffers and the analyser makes sure it gets enough data for the
2062 session().ensure_buffer_set (_signal_analysis_inputs, in);
2063 _signal_analysis_inputs.set_count (in);
2065 session().ensure_buffer_set (_signal_analysis_outputs, out);
2066 _signal_analysis_outputs.set_count (out);
2068 // std::cerr << "set counts to i" << in.n_audio() << "/o" << out.n_audio() << std::endl;
2071 return Processor::configure_io (in, out);
2074 /** Decide whether this PluginInsert can support a given IO configuration.
2075 * To do this, we run through a set of possible solutions in rough order of
2078 * @param in Required input channel count.
2079 * @param out Filled in with the output channel count if we return true.
2080 * @return true if the given IO configuration can be supported.
2083 PluginInsert::can_support_io_configuration (const ChanCount& in, ChanCount& out)
2086 _sidechain->can_support_io_configuration (in, out); // never fails, sets "out"
2088 return private_can_support_io_configuration (in, out).method != Impossible;
2092 PluginInsert::private_can_support_io_configuration (ChanCount const& in, ChanCount& out) const
2094 if (!_custom_cfg && _preset_out.n_audio () > 0) {
2095 // preseed hint (for variable i/o)
2096 out.set (DataType::AUDIO, _preset_out.n_audio ());
2099 Match rv = internal_can_support_io_configuration (in, out);
2101 if (!_custom_cfg && _preset_out.n_audio () > 0) {
2102 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: using output preset: %2\n", name(), _preset_out));
2103 out.set (DataType::AUDIO, _preset_out.n_audio ());
2108 /** A private version of can_support_io_configuration which returns the method
2109 * by which the configuration can be matched, rather than just whether or not
2113 PluginInsert::internal_can_support_io_configuration (ChanCount const & inx, ChanCount& out) const
2115 if (_plugins.empty()) {
2120 if (is_channelstrip ()) {
2122 return Match (ExactMatch, 1);
2126 /* if a user specified a custom cfg, so be it. */
2128 PluginInfoPtr info = _plugins.front()->get_info();
2130 if (info->reconfigurable_io()) {
2131 return Match (Delegate, 1, _strict_io, true);
2133 return Match (ExactMatch, get_count(), _strict_io, true);
2137 /* try automatic configuration */
2138 Match m = PluginInsert::automatic_can_support_io_configuration (inx, out);
2140 PluginInfoPtr info = _plugins.front()->get_info();
2141 ChanCount inputs = info->n_inputs;
2142 ChanCount outputs = info->n_outputs;
2144 /* handle case strict-i/o */
2145 if (_strict_io && m.method != Impossible) {
2148 /* special case MIDI instruments */
2149 if (is_instrument ()) {
2150 // output = midi-bypass + at most master-out channels.
2151 ChanCount max_out (DataType::AUDIO, 2); // TODO use master-out
2152 max_out.set (DataType::MIDI, out.get(DataType::MIDI));
2153 out = ChanCount::min (out, max_out);
2154 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: special case strict-i/o instrument\n", name()));
2160 if (inx.n_audio () != out.n_audio ()) { // ignore midi bypass
2161 /* replicate processor to match output count (generators and such)
2162 * at least enough to feed every output port. */
2163 uint32_t f = 1; // at least one. e.g. control data filters, no in, no out.
2164 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2165 uint32_t nout = outputs.get (*t);
2166 if (nout == 0 || inx.get(*t) == 0) { continue; }
2167 f = max (f, (uint32_t) ceil (inx.get(*t) / (float)nout));
2170 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: special case strict-i/o for generator\n", name()));
2171 return Match (Replicate, f, _strict_io);
2182 if (m.method != Impossible) {
2186 ChanCount ns_inputs = inputs - sidechain_input_pins ();
2188 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: resolving 'Impossible' match...\n", name()));
2190 if (info->reconfigurable_io()) {
2193 if (out.n_midi () > 0 && out.n_audio () == 0) { out.set (DataType::AUDIO, 2); }
2194 if (out.n_audio () == 0) { out.set (DataType::AUDIO, 1); }
2195 bool const r = _plugins.front()->can_support_io_configuration (inx + sidechain_input_pins (), out, &useins);
2197 // houston, we have a problem.
2198 return Match (Impossible, 0);
2201 if (inx.n_midi () > 0 && out.n_midi () == 0) { out.set (DataType::MIDI, 1); }
2202 return Match (Delegate, 1, _strict_io);
2205 ChanCount midi_bypass;
2206 if (inx.get(DataType::MIDI) == 1 && outputs.get(DataType::MIDI) == 0) {
2207 midi_bypass.set (DataType::MIDI, 1);
2210 // add at least as many plugins so that output count matches input count (w/o sidechain pins)
2212 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2213 uint32_t nin = ns_inputs.get (*t);
2214 uint32_t nout = outputs.get (*t);
2215 if (nin == 0 || inx.get(*t) == 0) { continue; }
2216 // prefer floor() so the count won't overly increase IFF (nin < nout)
2217 f = max (f, (uint32_t) floor (inx.get(*t) / (float)nout));
2219 if (f > 0 && outputs * f >= _configured_out) {
2220 out = outputs * f + midi_bypass;
2221 return Match (Replicate, f, _strict_io);
2224 // add at least as many plugins needed to connect all inputs (w/o sidechain pins)
2226 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2227 uint32_t nin = ns_inputs.get (*t);
2228 if (nin == 0 || inx.get(*t) == 0) { continue; }
2229 f = max (f, (uint32_t) ceil (inx.get(*t) / (float)nin));
2232 out = outputs * f + midi_bypass;
2233 return Match (Replicate, f, _strict_io);
2236 // add at least as many plugins needed to connect all inputs
2238 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2239 uint32_t nin = inputs.get (*t);
2240 if (nin == 0 || inx.get(*t) == 0) { continue; }
2241 f = max (f, (uint32_t) ceil (inx.get(*t) / (float)nin));
2243 out = outputs * f + midi_bypass;
2244 return Match (Replicate, f, _strict_io);
2247 /* this is the original Ardour 3/4 behavior, mainly for backwards compatibility */
2249 PluginInsert::automatic_can_support_io_configuration (ChanCount const & inx, ChanCount& out) const
2251 if (_plugins.empty()) {
2255 PluginInfoPtr info = _plugins.front()->get_info();
2256 ChanCount in; in += inx;
2257 ChanCount midi_bypass;
2259 if (info->reconfigurable_io()) {
2260 /* Plugin has flexible I/O, so delegate to it
2261 * pre-seed outputs, plugin tries closest match
2264 if (out.n_midi () > 0 && out.n_audio () == 0) { out.set (DataType::AUDIO, 2); }
2265 if (out.n_audio () == 0) { out.set (DataType::AUDIO, 1); }
2266 bool const r = _plugins.front()->can_support_io_configuration (in + sidechain_input_pins (), out);
2268 return Match (Impossible, 0);
2271 if (in.n_midi () > 0 && out.n_midi () == 0) { out.set (DataType::MIDI, 1); }
2272 return Match (Delegate, 1);
2275 ChanCount inputs = info->n_inputs;
2276 ChanCount outputs = info->n_outputs;
2277 ChanCount ns_inputs = inputs - sidechain_input_pins ();
2279 if (in.get(DataType::MIDI) == 1 && outputs.get(DataType::MIDI) == 0) {
2280 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: bypassing midi-data\n", name()));
2281 midi_bypass.set (DataType::MIDI, 1);
2283 if (in.get(DataType::MIDI) == 1 && inputs.get(DataType::MIDI) == 0) {
2284 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: hiding midi-port from plugin\n", name()));
2285 in.set(DataType::MIDI, 0);
2288 // add internally provided sidechain ports
2289 ChanCount insc = in + sidechain_input_ports ();
2291 bool no_inputs = true;
2292 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2293 if (inputs.get (*t) != 0) {
2300 /* no inputs so we can take any input configuration since we throw it away */
2301 out = outputs + midi_bypass;
2302 return Match (NoInputs, 1);
2305 /* Plugin inputs match requested inputs + side-chain-ports exactly */
2306 if (inputs == insc) {
2307 out = outputs + midi_bypass;
2308 return Match (ExactMatch, 1);
2311 /* Plugin inputs matches without side-chain-pins */
2312 if (ns_inputs == in) {
2313 out = outputs + midi_bypass;
2314 return Match (ExactMatch, 1);
2317 /* We may be able to run more than one copy of the plugin within this insert
2318 to cope with the insert having more inputs than the plugin.
2319 We allow replication only for plugins with either zero or 1 inputs and outputs
2320 for every valid data type.
2324 bool can_replicate = true;
2325 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2327 // ignore side-chains
2328 uint32_t nin = ns_inputs.get (*t);
2330 // No inputs of this type
2331 if (nin == 0 && in.get(*t) == 0) {
2335 if (nin != 1 || outputs.get (*t) != 1) {
2336 can_replicate = false;
2340 // Potential factor not set yet
2342 f = in.get(*t) / nin;
2345 // Factor for this type does not match another type, can not replicate
2346 if (f != (in.get(*t) / nin)) {
2347 can_replicate = false;
2352 if (can_replicate && f > 0) {
2353 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2354 out.set (*t, outputs.get(*t) * f);
2357 return Match (Replicate, f);
2360 /* If the processor has exactly one input of a given type, and
2361 the plugin has more, we can feed the single processor input
2362 to some or all of the plugin inputs. This is rather
2363 special-case-y, but the 1-to-many case is by far the
2364 simplest. How do I split thy 2 processor inputs to 3
2365 plugin inputs? Let me count the ways ...
2368 bool can_split = true;
2369 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2371 bool const can_split_type = (in.get (*t) == 1 && ns_inputs.get (*t) > 1);
2372 bool const nothing_to_do_for_type = (in.get (*t) == 0 && inputs.get (*t) == 0);
2374 if (!can_split_type && !nothing_to_do_for_type) {
2380 out = outputs + midi_bypass;
2381 return Match (Split, 1);
2384 /* If the plugin has more inputs than we want, we can `hide' some of them
2385 by feeding them silence.
2388 bool could_hide = false;
2389 bool cannot_hide = false;
2390 ChanCount hide_channels;
2392 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2393 if (inputs.get(*t) > in.get(*t)) {
2394 /* there is potential to hide, since the plugin has more inputs of type t than the insert */
2395 hide_channels.set (*t, inputs.get(*t) - in.get(*t));
2397 } else if (inputs.get(*t) < in.get(*t)) {
2398 /* we definitely cannot hide, since the plugin has fewer inputs of type t than the insert */
2403 if (could_hide && !cannot_hide) {
2404 out = outputs + midi_bypass;
2405 return Match (Hide, 1, false, false, hide_channels);
2408 return Match (Impossible, 0);
2413 PluginInsert::state ()
2415 XMLNode& node = Processor::state ();
2417 node.set_property("type", _plugins[0]->state_node_name());
2418 node.set_property("unique-id", _plugins[0]->unique_id());
2419 node.set_property("count", (uint32_t)_plugins.size());
2421 /* remember actual i/o configuration (for later placeholder
2422 * in case the plugin goes missing) */
2423 node.add_child_nocopy (* _configured_in.state (X_("ConfiguredInput")));
2424 node.add_child_nocopy (* _custom_sinks.state (X_("CustomSinks")));
2425 node.add_child_nocopy (* _configured_out.state (X_("ConfiguredOutput")));
2426 node.add_child_nocopy (* _preset_out.state (X_("PresetOutput")));
2428 /* save custom i/o config */
2429 node.set_property("custom", _custom_cfg);
2430 for (uint32_t pc = 0; pc < get_count(); ++pc) {
2432 snprintf (tmp, sizeof(tmp), "InputMap-%d", pc);
2433 node.add_child_nocopy (* _in_map[pc].state (tmp));
2434 snprintf (tmp, sizeof(tmp), "OutputMap-%d", pc);
2435 node.add_child_nocopy (* _out_map[pc].state (tmp));
2437 node.add_child_nocopy (* _thru_map.state ("ThruMap"));
2440 node.add_child_nocopy (_sidechain->get_state ());
2443 _plugins[0]->set_insert_id(this->id());
2444 node.add_child_nocopy (_plugins[0]->get_state());
2446 for (Controls::iterator c = controls().begin(); c != controls().end(); ++c) {
2447 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> ((*c).second);
2449 node.add_child_nocopy (ac->get_state());
2457 PluginInsert::set_control_ids (const XMLNode& node, int version)
2459 const XMLNodeList& nlist = node.children();
2460 XMLNodeConstIterator iter;
2461 set<Evoral::Parameter>::const_iterator p;
2463 for (iter = nlist.begin(); iter != nlist.end(); ++iter) {
2464 if ((*iter)->name() == Controllable::xml_node_name) {
2466 uint32_t p = (uint32_t)-1;
2469 if ((*iter)->get_property (X_("symbol"), str)) {
2470 boost::shared_ptr<LV2Plugin> lv2plugin = boost::dynamic_pointer_cast<LV2Plugin> (_plugins[0]);
2472 p = lv2plugin->port_index(str.c_str());
2476 if (p == (uint32_t)-1) {
2477 (*iter)->get_property (X_("parameter"), p);
2480 if (p != (uint32_t)-1) {
2482 /* this may create the new controllable */
2484 boost::shared_ptr<Evoral::Control> c = control (Evoral::Parameter (PluginAutomation, 0, p));
2486 #ifndef NO_PLUGIN_STATE
2490 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> (c);
2492 ac->set_state (**iter, version);
2501 PluginInsert::set_state(const XMLNode& node, int version)
2503 XMLNodeList nlist = node.children();
2504 XMLNodeIterator niter;
2505 XMLPropertyList plist;
2506 ARDOUR::PluginType type;
2509 if (!node.get_property ("type", str)) {
2510 error << _("XML node describing plugin is missing the `type' field") << endmsg;
2514 if (str == X_("ladspa") || str == X_("Ladspa")) { /* handle old school sessions */
2515 type = ARDOUR::LADSPA;
2516 } else if (str == X_("lv2")) {
2518 } else if (str == X_("windows-vst")) {
2519 type = ARDOUR::Windows_VST;
2520 } else if (str == X_("lxvst")) {
2521 type = ARDOUR::LXVST;
2522 } else if (str == X_("mac-vst")) {
2523 type = ARDOUR::MacVST;
2524 } else if (str == X_("audiounit")) {
2525 type = ARDOUR::AudioUnit;
2526 } else if (str == X_("luaproc")) {
2529 error << string_compose (_("unknown plugin type %1 in plugin insert state"), str) << endmsg;
2533 XMLProperty const * prop = node.property ("unique-id");
2536 #ifdef WINDOWS_VST_SUPPORT
2537 /* older sessions contain VST plugins with only an "id" field. */
2538 if (type == ARDOUR::Windows_VST) {
2539 prop = node.property ("id");
2543 #ifdef LXVST_SUPPORT
2544 /*There shouldn't be any older sessions with linuxVST support.. but anyway..*/
2545 if (type == ARDOUR::LXVST) {
2546 prop = node.property ("id");
2553 error << _("Plugin has no unique ID field") << endmsg;
2558 boost::shared_ptr<Plugin> plugin = find_plugin (_session, prop->value(), type);
2559 bool any_vst = false;
2561 /* treat VST plugins equivalent if they have the same uniqueID
2562 * allow to move sessions windows <> linux */
2563 #ifdef LXVST_SUPPORT
2564 if (plugin == 0 && (type == ARDOUR::Windows_VST || type == ARDOUR::MacVST)) {
2565 type = ARDOUR::LXVST;
2566 plugin = find_plugin (_session, prop->value(), type);
2567 if (plugin) { any_vst = true; }
2571 #ifdef WINDOWS_VST_SUPPORT
2572 if (plugin == 0 && (type == ARDOUR::LXVST || type == ARDOUR::MacVST)) {
2573 type = ARDOUR::Windows_VST;
2574 plugin = find_plugin (_session, prop->value(), type);
2575 if (plugin) { any_vst = true; }
2579 #ifdef MACVST_SUPPORT
2580 if (plugin == 0 && (type == ARDOUR::Windows_VST || type == ARDOUR::LXVST)) {
2581 type = ARDOUR::MacVST;
2582 plugin = find_plugin (_session, prop->value(), type);
2583 if (plugin) { any_vst = true; }
2587 if (plugin == 0 && type == ARDOUR::Lua) {
2588 /* unique ID (sha1 of script) was not found,
2589 * load the plugin from the serialized version in the
2590 * session-file instead.
2592 boost::shared_ptr<LuaProc> lp (new LuaProc (_session.engine(), _session, ""));
2593 XMLNode *ls = node.child (lp->state_node_name().c_str());
2595 lp->set_script_from_state (*ls);
2601 error << string_compose(
2602 _("Found a reference to a plugin (\"%1\") that is unknown.\n"
2603 "Perhaps it was removed or moved since it was last used."),
2609 // The name of the PluginInsert comes from the plugin, nothing else
2610 _name = plugin->get_info()->name;
2614 // Processor::set_state() will set this, but too late
2615 // for it to be available when setting up plugin
2616 // state. We can't call Processor::set_state() until
2617 // the plugins themselves are created and added.
2621 if (_plugins.empty()) {
2622 /* if we are adding the first plugin, we will need to set
2623 up automatable controls.
2625 add_plugin (plugin);
2626 create_automatable_parameters ();
2627 set_control_ids (node, version);
2630 node.get_property ("count", count);
2632 if (_plugins.size() != count) {
2633 for (uint32_t n = 1; n < count; ++n) {
2634 add_plugin (plugin_factory (plugin));
2638 Processor::set_state (node, version);
2640 PBD::ID new_id = this->id();
2641 PBD::ID old_id = this->id();
2643 node.get_property ("id", old_id);
2645 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2647 /* find the node with the type-specific node name ("lv2", "ladspa", etc)
2648 and set all plugins to the same state.
2651 if ( ((*niter)->name() == plugin->state_node_name())
2652 || (any_vst && ((*niter)->name() == "lxvst" || (*niter)->name() == "windows-vst" || (*niter)->name() == "mac-vst"))
2655 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
2656 /* Plugin state can include external files which are named after the ID.
2658 * If regenerate_xml_or_string_ids() is set, the ID will already have
2659 * been changed, so we need to use the old ID from the XML to load the
2660 * state and then update the ID.
2662 * When copying a plugin-state, route_ui takes care of of updating the ID,
2663 * but we need to call set_insert_id() to clear the cached plugin-state
2664 * and force a change.
2666 if (!regenerate_xml_or_string_ids ()) {
2667 (*i)->set_insert_id (new_id);
2669 (*i)->set_insert_id (old_id);
2672 (*i)->set_state (**niter, version);
2674 if (regenerate_xml_or_string_ids ()) {
2675 (*i)->set_insert_id (new_id);
2679 /* when copying plugin state, notify UI */
2680 for (Controls::const_iterator li = controls().begin(); li != controls().end(); ++li) {
2681 boost::shared_ptr<PBD::Controllable> c = boost::dynamic_pointer_cast<PBD::Controllable> (li->second);
2683 c->Changed (false, Controllable::NoGroup); /* EMIT SIGNAL */
2691 if (version < 3000) {
2693 /* Only 2.X sessions need a call to set_parameter_state() - in 3.X and above
2694 this is all handled by Automatable
2697 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2698 if ((*niter)->name() == "Redirect") {
2699 /* XXX do we need to tackle placement? i think not (pd; oct 16 2009) */
2700 Processor::set_state (**niter, version);
2705 set_parameter_state_2X (node, version);
2708 node.get_property (X_("custom"), _custom_cfg);
2710 uint32_t in_maps = 0;
2711 uint32_t out_maps = 0;
2712 XMLNodeList kids = node.children ();
2713 for (XMLNodeIterator i = kids.begin(); i != kids.end(); ++i) {
2714 if ((*i)->name() == X_("ConfiguredInput")) {
2715 _configured_in = ChanCount(**i);
2717 if ((*i)->name() == X_("CustomSinks")) {
2718 _custom_sinks = ChanCount(**i);
2720 if ((*i)->name() == X_("ConfiguredOutput")) {
2721 _custom_out = ChanCount(**i);
2722 _configured_out = ChanCount(**i);
2724 if ((*i)->name() == X_("PresetOutput")) {
2725 _preset_out = ChanCount(**i);
2727 if (strncmp ((*i)->name ().c_str(), X_("InputMap-"), 9) == 0) {
2728 long pc = atol (&((*i)->name().c_str()[9]));
2729 if (pc >= 0 && pc <= (long) get_count()) {
2730 _in_map[pc] = ChanMapping (**i);
2734 if (strncmp ((*i)->name ().c_str(), X_("OutputMap-"), 10) == 0) {
2735 long pc = atol (&((*i)->name().c_str()[10]));
2736 if (pc >= 0 && pc <= (long) get_count()) {
2737 _out_map[pc] = ChanMapping (**i);
2741 if ((*i)->name () == "ThruMap") {
2742 _thru_map = ChanMapping (**i);
2745 // sidechain is a Processor (IO)
2746 if ((*i)->name () == Processor::state_node_name) {
2750 if (!regenerate_xml_or_string_ids ()) {
2751 _sidechain->set_state (**i, version);
2756 if (in_maps == out_maps && out_maps >0 && out_maps == get_count()) {
2757 _maps_from_state = true;
2760 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
2764 (*i)->deactivate ();
2768 PluginConfigChanged (); /* EMIT SIGNAL */
2773 PluginInsert::update_id (PBD::ID id)
2776 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
2777 (*i)->set_insert_id (id);
2782 PluginInsert::set_owner (SessionObject* o)
2784 Processor::set_owner (o);
2785 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
2786 (*i)->set_owner (o);
2791 PluginInsert::set_state_dir (const std::string& d)
2793 // state() only saves the state of the first plugin
2794 _plugins[0]->set_state_dir (d);
2798 PluginInsert::set_parameter_state_2X (const XMLNode& node, int version)
2800 XMLNodeList nlist = node.children();
2801 XMLNodeIterator niter;
2803 /* look for port automation node */
2805 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2807 if ((*niter)->name() != port_automation_node_name) {
2812 XMLNodeConstIterator iter;
2816 cnodes = (*niter)->children ("port");
2818 for (iter = cnodes.begin(); iter != cnodes.end(); ++iter){
2822 if (!child->get_property("number", port_id)) {
2823 warning << _("PluginInsert: Auto: no ladspa port number") << endmsg;
2827 if (port_id >= _plugins[0]->parameter_count()) {
2828 warning << _("PluginInsert: Auto: port id out of range") << endmsg;
2832 boost::shared_ptr<AutomationControl> c = boost::dynamic_pointer_cast<AutomationControl>(
2833 control(Evoral::Parameter(PluginAutomation, 0, port_id), true));
2835 if (c && c->alist()) {
2836 if (!child->children().empty()) {
2837 c->alist()->set_state (*child->children().front(), version);
2840 error << string_compose (_("PluginInsert: automatable control %1 not found - ignored"), port_id) << endmsg;
2850 boost::shared_ptr<ReadOnlyControl>
2851 PluginInsert::control_output (uint32_t num) const
2853 CtrlOutMap::const_iterator i = _control_outputs.find (num);
2854 if (i == _control_outputs.end ()) {
2855 return boost::shared_ptr<ReadOnlyControl> ();
2862 PluginInsert::describe_parameter (Evoral::Parameter param)
2864 if (param.type() == PluginAutomation) {
2865 return _plugins[0]->describe_parameter (param);
2866 } else if (param.type() == PluginPropertyAutomation) {
2867 boost::shared_ptr<AutomationControl> c(automation_control(param));
2868 if (c && !c->desc().label.empty()) {
2869 return c->desc().label;
2872 return Automatable::describe_parameter(param);
2876 PluginInsert::signal_latency() const
2878 if (!_pending_active) {
2881 if (_user_latency) {
2882 return _user_latency;
2885 return _plugins[0]->signal_latency ();
2889 PluginInsert::type ()
2891 return plugin()->get_info()->type;
2894 PluginInsert::PluginControl::PluginControl (PluginInsert* p,
2895 const Evoral::Parameter& param,
2896 const ParameterDescriptor& desc,
2897 boost::shared_ptr<AutomationList> list)
2898 : AutomationControl (p->session(), param, desc, list, p->describe_parameter(param))
2903 list->set_interpolation(Evoral::ControlList::Discrete);
2908 /** @param val `user' value */
2911 PluginInsert::PluginControl::actually_set_value (double user_val, PBD::Controllable::GroupControlDisposition group_override)
2913 /* FIXME: probably should be taking out some lock here.. */
2915 for (Plugins::iterator i = _plugin->_plugins.begin(); i != _plugin->_plugins.end(); ++i) {
2916 (*i)->set_parameter (_list->parameter().id(), user_val);
2919 boost::shared_ptr<Plugin> iasp = _plugin->_impulseAnalysisPlugin.lock();
2921 iasp->set_parameter (_list->parameter().id(), user_val);
2924 AutomationControl::actually_set_value (user_val, group_override);
2928 PluginInsert::PluginControl::catch_up_with_external_value (double user_val)
2930 AutomationControl::actually_set_value (user_val, Controllable::NoGroup);
2934 PluginInsert::PluginControl::get_state ()
2936 XMLNode& node (AutomationControl::get_state());
2937 node.set_property (X_("parameter"), parameter().id());
2939 boost::shared_ptr<LV2Plugin> lv2plugin = boost::dynamic_pointer_cast<LV2Plugin> (_plugin->_plugins[0]);
2941 node.set_property (X_("symbol"), lv2plugin->port_symbol (parameter().id()));
2948 /** @return `user' val */
2950 PluginInsert::PluginControl::get_value () const
2952 boost::shared_ptr<Plugin> plugin = _plugin->plugin (0);
2958 return plugin->get_parameter (_list->parameter().id());
2961 PluginInsert::PluginPropertyControl::PluginPropertyControl (PluginInsert* p,
2962 const Evoral::Parameter& param,
2963 const ParameterDescriptor& desc,
2964 boost::shared_ptr<AutomationList> list)
2965 : AutomationControl (p->session(), param, desc, list)
2971 PluginInsert::PluginPropertyControl::actually_set_value (double user_val, Controllable::GroupControlDisposition gcd)
2973 /* Old numeric set_value(), coerce to appropriate datatype if possible.
2974 This is lossy, but better than nothing until Ardour's automation system
2975 can handle various datatypes all the way down. */
2976 const Variant value(_desc.datatype, user_val);
2977 if (value.type() == Variant::NOTHING) {
2978 error << "set_value(double) called for non-numeric property" << endmsg;
2982 for (Plugins::iterator i = _plugin->_plugins.begin(); i != _plugin->_plugins.end(); ++i) {
2983 (*i)->set_property(_list->parameter().id(), value);
2988 AutomationControl::actually_set_value (user_val, gcd);
2992 PluginInsert::PluginPropertyControl::get_state ()
2994 XMLNode& node (AutomationControl::get_state());
2995 node.set_property (X_("property"), parameter().id());
2996 node.remove_property (X_("value"));
3002 PluginInsert::PluginPropertyControl::get_value () const
3004 return _value.to_double();
3007 boost::shared_ptr<Plugin>
3008 PluginInsert::get_impulse_analysis_plugin()
3010 boost::shared_ptr<Plugin> ret;
3011 if (_impulseAnalysisPlugin.expired()) {
3012 // LV2 in particular uses various _session params
3013 // during init() -- most notably block_size..
3015 ret = plugin_factory(_plugins[0]);
3016 ChanCount out (internal_output_streams ());
3017 if (ret->get_info ()->reconfigurable_io ()) {
3018 // populate get_info ()->n_inputs and ->n_outputs
3020 ret->can_support_io_configuration (internal_input_streams (), out, &useins);
3021 assert (out == internal_output_streams ());
3023 ret->configure_io (internal_input_streams (), out);
3024 ret->set_owner (_owner);
3025 _impulseAnalysisPlugin = ret;
3027 ret = _impulseAnalysisPlugin.lock();
3034 PluginInsert::collect_signal_for_analysis (samplecnt_t nframes)
3036 // called from outside the audio thread, so this should be safe
3037 // only do audio as analysis is (currently) only for audio plugins
3038 _signal_analysis_inputs.ensure_buffers (DataType::AUDIO, input_streams().n_audio(), nframes);
3039 _signal_analysis_outputs.ensure_buffers (DataType::AUDIO, output_streams().n_audio(), nframes);
3041 _signal_analysis_collected_nframes = 0;
3042 _signal_analysis_collect_nframes_max = nframes;
3045 /** Add a plugin to our list */
3047 PluginInsert::add_plugin (boost::shared_ptr<Plugin> plugin)
3049 plugin->set_insert_id (this->id());
3050 plugin->set_owner (_owner);
3052 if (_plugins.empty()) {
3053 /* first (and probably only) plugin instance - connect to relevant signals */
3055 plugin->ParameterChangedExternally.connect_same_thread (*this, boost::bind (&PluginInsert::parameter_changed_externally, this, _1, _2));
3056 plugin->StartTouch.connect_same_thread (*this, boost::bind (&PluginInsert::start_touch, this, _1));
3057 plugin->EndTouch.connect_same_thread (*this, boost::bind (&PluginInsert::end_touch, this, _1));
3058 _custom_sinks = plugin->get_info()->n_inputs;
3059 // cache sidechain port count
3060 _cached_sidechain_pins.reset ();
3061 const ChanCount& nis (plugin->get_info()->n_inputs);
3062 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
3063 for (uint32_t in = 0; in < nis.get (*t); ++in) {
3064 const Plugin::IOPortDescription& iod (plugin->describe_io_port (*t, true, in));
3065 if (iod.is_sidechain) {
3066 _cached_sidechain_pins.set (*t, 1 + _cached_sidechain_pins.n(*t));
3071 #if (defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT || defined MACVST_SUPPORT)
3072 boost::shared_ptr<VSTPlugin> vst = boost::dynamic_pointer_cast<VSTPlugin> (plugin);
3074 vst->set_insert (this, _plugins.size ());
3078 _plugins.push_back (plugin);
3082 PluginInsert::load_preset (ARDOUR::Plugin::PresetRecord pr)
3085 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
3086 if (! (*i)->load_preset (pr)) {
3094 PluginInsert::realtime_handle_transport_stopped ()
3096 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
3097 (*i)->realtime_handle_transport_stopped ();
3102 PluginInsert::realtime_locate ()
3104 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
3105 (*i)->realtime_locate ();
3110 PluginInsert::monitoring_changed ()
3112 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
3113 (*i)->monitoring_changed ();
3118 PluginInsert::latency_changed ()
3120 // this is called in RT context, LatencyChanged is emitted after run()
3121 _latency_changed = true;
3122 // XXX This also needs a proper API not an owner() hack.
3124 static_cast<Route*>(owner ())->processor_latency_changed (); /* EMIT SIGNAL */
3128 PluginInsert::start_touch (uint32_t param_id)
3130 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter (PluginAutomation, 0, param_id));
3132 // ToDo subtract _plugin_signal_latency from audible_sample() when rolling, assert > 0
3133 ac->start_touch (session().audible_sample());
3138 PluginInsert::end_touch (uint32_t param_id)
3140 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter (PluginAutomation, 0, param_id));
3142 // ToDo subtract _plugin_signal_latency from audible_sample() when rolling, assert > 0
3143 ac->stop_touch (session().audible_sample());
3147 std::ostream& operator<<(std::ostream& o, const ARDOUR::PluginInsert::Match& m)
3150 case PluginInsert::Impossible: o << "Impossible"; break;
3151 case PluginInsert::Delegate: o << "Delegate"; break;
3152 case PluginInsert::NoInputs: o << "NoInputs"; break;
3153 case PluginInsert::ExactMatch: o << "ExactMatch"; break;
3154 case PluginInsert::Replicate: o << "Replicate"; break;
3155 case PluginInsert::Split: o << "Split"; break;
3156 case PluginInsert::Hide: o << "Hide"; break;
3158 o << " cnt: " << m.plugins
3159 << (m.strict_io ? " strict-io" : "")
3160 << (m.custom_cfg ? " custom-cfg" : "");
3161 if (m.method == PluginInsert::Hide) {
3162 o << " hide: " << m.hide;