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_frame()));
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 add_control (boost::shared_ptr<AutomationControl> (new PluginPropertyControl(this, param, desc, list)));
499 _bypass_port = plugin->designated_bypass_port ();
501 /* special case VST effSetBypass */
502 if (_bypass_port == UINT32_MAX -1) {
503 // emulate VST Bypass
504 Evoral::Parameter param (PluginAutomation, 0, _bypass_port);
505 ParameterDescriptor desc;
506 desc.label = _("Plugin Enable");
511 boost::shared_ptr<AutomationList> list(new AutomationList(param, desc));
512 boost::shared_ptr<AutomationControl> c (new PluginControl(this, param, desc, list));
516 if (_bypass_port != UINT32_MAX) {
517 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter (PluginAutomation, 0, _bypass_port));
518 if (0 == (ac->flags () & Controllable::NotAutomatable)) {
519 ac->alist()->automation_state_changed.connect_same_thread (*this, boost::bind (&PluginInsert::bypassable_changed, this));
520 ac->Changed.connect_same_thread (*this, boost::bind (&PluginInsert::enable_changed, this));
523 plugin->PresetPortSetValue.connect_same_thread (*this, boost::bind (&PluginInsert::preset_load_set_value, this, _1, _2));
526 /** Called when something outside of this host has modified a plugin
527 * parameter. Responsible for propagating the change to two places:
529 * 1) anything listening to the Control itself
530 * 2) any replicated plugins that make up this PluginInsert.
532 * The PluginInsert is connected to the ParameterChangedExternally signal for
533 * the first (primary) plugin, and here broadcasts that change to any others.
535 * XXX We should probably drop this whole replication idea (Paul, October 2015)
536 * since it isn't used by sensible plugin APIs (AU, LV2).
539 PluginInsert::parameter_changed_externally (uint32_t which, float val)
541 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter (PluginAutomation, 0, which));
543 /* First propagation: alter the underlying value of the control,
544 * without telling the plugin(s) that own/use it to set it.
551 boost::shared_ptr<PluginControl> pc = boost::dynamic_pointer_cast<PluginControl> (ac);
554 pc->catch_up_with_external_value (val);
557 /* Second propagation: tell all plugins except the first to
558 update the value of this parameter. For sane plugin APIs,
559 there are no other plugins, so this is a no-op in those
563 Plugins::iterator i = _plugins.begin();
565 /* don't set the first plugin, just all the slaves */
567 if (i != _plugins.end()) {
569 for (; i != _plugins.end(); ++i) {
570 (*i)->set_parameter (which, val);
576 PluginInsert::set_block_size (pframes_t nframes)
579 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
580 if ((*i)->set_block_size (nframes) != 0) {
588 PluginInsert::activate ()
590 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
594 Processor::activate ();
595 /* when setting state e.g ProcessorBox::paste_processor_state ()
596 * the plugin is not yet owned by a route.
597 * but no matter. Route::add_processors() will call activate () again
602 if (_plugin_signal_latency != signal_latency ()) {
603 _plugin_signal_latency = signal_latency ();
609 PluginInsert::deactivate ()
611 Processor::deactivate ();
613 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
616 if (_plugin_signal_latency != signal_latency ()) {
617 _plugin_signal_latency = signal_latency ();
623 PluginInsert::flush ()
625 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
631 PluginInsert::enable (bool yn)
633 if (_bypass_port == UINT32_MAX) {
640 if (!_pending_active) {
643 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter (PluginAutomation, 0, _bypass_port));
644 const double val = yn ? 1.0 : 0.0;
645 ac->set_value (val, Controllable::NoGroup);
647 #ifdef ALLOW_VST_BYPASS_TO_FAIL // yet unused, see also vst_plugin.cc
648 /* special case VST.. bypass may fail */
649 if (_bypass_port == UINT32_MAX - 1) {
650 /* check if bypass worked */
651 if (ac->get_value () != val) {
652 warning << _("PluginInsert: VST Bypass failed, falling back to host bypass.") << endmsg;
653 // set plugin to enabled (not-byassed)
654 ac->set_value (1.0, Controllable::NoGroup);
655 // ..and use host-provided hard-bypass
670 PluginInsert::enabled () const
672 if (_bypass_port == UINT32_MAX) {
673 return Processor::enabled ();
675 boost::shared_ptr<const AutomationControl> ac = boost::const_pointer_cast<AutomationControl> (automation_control (Evoral::Parameter (PluginAutomation, 0, _bypass_port)));
676 return (ac->get_value () > 0 && _pending_active);
681 PluginInsert::bypassable () const
683 if (_bypass_port == UINT32_MAX) {
686 boost::shared_ptr<const AutomationControl> ac = boost::const_pointer_cast<AutomationControl> (automation_control (Evoral::Parameter (PluginAutomation, 0, _bypass_port)));
688 return !ac->automation_playback ();
693 PluginInsert::enable_changed ()
699 PluginInsert::bypassable_changed ()
701 BypassableChanged ();
705 PluginInsert::preset_load_set_value (uint32_t p, float v)
707 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter(PluginAutomation, 0, p));
712 if (ac->automation_state() & Play) {
717 ac->set_value (v, Controllable::NoGroup);
722 PluginInsert::inplace_silence_unconnected (BufferSet& bufs, const PinMappings& out_map, framecnt_t nframes, framecnt_t offset) const
724 // TODO optimize: store "unconnected" in a fixed set.
725 // it only changes on reconfiguration.
726 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
727 for (uint32_t out = 0; out < bufs.count().get (*t); ++out) {
729 if (*t == DataType::MIDI && out == 0 && has_midi_bypass ()) {
730 mapped = true; // in-place Midi bypass
732 for (uint32_t pc = 0; pc < get_count() && !mapped; ++pc) {
733 PinMappings::const_iterator i = out_map.find (pc);
734 if (i == out_map.end ()) {
737 const ChanMapping& outmap (i->second);
738 for (uint32_t o = 0; o < natural_output_streams().get (*t); ++o) {
740 uint32_t idx = outmap.get (*t, o, &valid);
741 if (valid && idx == out) {
748 bufs.get (*t, out).silence (nframes, offset);
755 PluginInsert::connect_and_run (BufferSet& bufs, framepos_t start, framepos_t end, double speed, pframes_t nframes, framecnt_t offset, bool with_auto)
757 // TODO: atomically copy maps & _no_inplace
758 PinMappings in_map (_in_map);
759 PinMappings out_map (_out_map);
760 ChanMapping thru_map (_thru_map);
761 if (_mapping_changed) { // ToDo use a counters, increment until match.
762 _no_inplace = check_inplace ();
763 _mapping_changed = false;
766 if (_latency_changed) {
767 /* delaylines are configured with the max possible latency (as reported by the plugin)
768 * so this won't allocate memory (unless the plugin lied about its max latency)
769 * It may still 'click' though, since the fixed delaylines are not de-clicked.
770 * Then again plugin-latency changes are not click-free to begin with.
772 * This is also worst case, there is currently no concept of per-stream latency.
774 * e.g. Two identical latent plugins:
775 * 1st plugin: process left (latent), bypass right.
776 * 2nd plugin: bypass left, process right (latent).
777 * -> currently this yields 2 times latency of the plugin,
779 _latency_changed = false;
780 _delaybuffers.set (ChanCount::max(bufs.count(), _configured_out), plugin_latency ());
783 if (_match.method == Split && !_no_inplace) {
784 // TODO: also use this optimization if one source-buffer
785 // feeds _all_ *connected* inputs.
786 // currently this is *first* buffer to all only --
787 // see PluginInsert::check_inplace
788 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
789 if (_configured_internal.get (*t) == 0) {
793 uint32_t first_idx = in_map[0].get (*t, 0, &valid);
794 assert (valid && first_idx == 0); // check_inplace ensures this
795 /* copy the first stream's buffer contents to the others */
796 for (uint32_t i = 1; i < natural_input_streams ().get (*t); ++i) {
797 uint32_t idx = in_map[0].get (*t, i, &valid);
800 bufs.get (*t, i).read_from (bufs.get (*t, first_idx), nframes, offset, offset);
804 /* the copy operation produces a linear monotonic input map */
805 in_map[0] = ChanMapping (natural_input_streams ());
808 bufs.set_count(ChanCount::max(bufs.count(), _configured_internal));
809 bufs.set_count(ChanCount::max(bufs.count(), _configured_out));
815 for (Controls::iterator li = controls().begin(); li != controls().end(); ++li, ++n) {
817 boost::shared_ptr<AutomationControl> c
818 = boost::dynamic_pointer_cast<AutomationControl>(li->second);
820 if (c->list() && c->automation_playback()) {
823 const float val = c->list()->rt_safe_eval (start, valid);
826 /* This is the ONLY place where we are
828 * AutomationControl::set_value_unchecked(). We
829 * know that the control is in
830 * automation playback mode, so no
831 * check on writable() is required
832 * (which must be done in AutomationControl::set_value()
835 c->set_value_unchecked(val);
842 /* Calculate if, and how many frames we need to collect for analysis */
843 framecnt_t collect_signal_nframes = (_signal_analysis_collect_nframes_max -
844 _signal_analysis_collected_nframes);
845 if (nframes < collect_signal_nframes) { // we might not get all frames now
846 collect_signal_nframes = nframes;
849 if (collect_signal_nframes > 0) {
851 //std::cerr << "collect input, bufs " << bufs.count().n_audio() << " count, " << bufs.available().n_audio() << " available" << std::endl;
852 //std::cerr << " streams " << internal_input_streams().n_audio() << std::endl;
853 //std::cerr << "filling buffer with " << collect_signal_nframes << " frames at " << _signal_analysis_collected_nframes << std::endl;
855 _signal_analysis_inputs.set_count(input_streams());
857 for (uint32_t i = 0; i < input_streams().n_audio(); ++i) {
858 _signal_analysis_inputs.get_audio(i).read_from (
860 collect_signal_nframes,
861 _signal_analysis_collected_nframes); // offset is for target buffer
866 if (is_channelstrip ()) {
867 if (_configured_in.n_audio() > 0) {
868 ChanMapping mb_in_map (ChanCount::min (_configured_in, ChanCount (DataType::AUDIO, 2)));
869 ChanMapping mb_out_map (ChanCount::min (_configured_out, ChanCount (DataType::AUDIO, 2)));
871 _plugins.front()->connect_and_run (bufs, start, end, speed, mb_in_map, mb_out_map, nframes, offset);
873 for (uint32_t out = _configured_in.n_audio (); out < bufs.count().get (DataType::AUDIO); ++out) {
874 bufs.get (DataType::AUDIO, out).silence (nframes, offset);
880 // TODO optimize -- build maps once.
882 BufferSet& inplace_bufs = _session.get_noinplace_buffers();
883 ARDOUR::ChanMapping used_outputs;
885 assert (inplace_bufs.count () >= natural_input_streams () + _configured_out);
887 /* build used-output map */
888 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i, ++pc) {
889 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
890 for (uint32_t out = 0; out < natural_output_streams().get (*t); ++out) {
892 uint32_t out_idx = out_map[pc].get (*t, out, &valid);
894 used_outputs.set (*t, out_idx, 1); // mark as used
899 /* copy thru data to outputs before processing in-place */
900 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
901 for (uint32_t out = 0; out < bufs.count().get (*t); ++out) {
903 uint32_t in_idx = thru_map.get (*t, out, &valid);
904 uint32_t m = out + natural_input_streams ().get (*t);
906 _delaybuffers.delay (*t, out, inplace_bufs.get (*t, m), bufs.get (*t, in_idx), nframes, offset, offset);
907 used_outputs.set (*t, out, 1); // mark as used
909 used_outputs.get (*t, out, &valid);
911 /* the plugin is expected to write here, but may not :(
912 * (e.g. drumgizmo w/o kit loaded)
914 inplace_bufs.get (*t, m).silence (nframes);
921 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i, ++pc) {
923 ARDOUR::ChanMapping i_in_map (natural_input_streams());
924 ARDOUR::ChanMapping i_out_map (out_map[pc]);
925 ARDOUR::ChanCount mapped;
927 /* map inputs sequentially */
928 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
929 for (uint32_t in = 0; in < natural_input_streams().get (*t); ++in) {
931 uint32_t in_idx = in_map[pc].get (*t, in, &valid);
932 uint32_t m = mapped.get (*t);
934 inplace_bufs.get (*t, m).read_from (bufs.get (*t, in_idx), nframes, offset, offset);
936 inplace_bufs.get (*t, m).silence (nframes, offset);
938 mapped.set (*t, m + 1);
942 /* outputs are mapped to inplace_bufs after the inputs */
943 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
944 i_out_map.offset_to (*t, natural_input_streams ().get (*t));
947 if ((*i)->connect_and_run (inplace_bufs, start, end, speed, i_in_map, i_out_map, nframes, offset)) {
952 /* all instances have completed, now copy data that was written
953 * and zero unconnected buffers */
954 ARDOUR::ChanMapping nonzero_out (used_outputs);
955 if (has_midi_bypass ()) {
956 nonzero_out.set (DataType::MIDI, 0, 1); // Midi bypass.
958 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
959 for (uint32_t out = 0; out < bufs.count().get (*t); ++out) {
961 used_outputs.get (*t, out, &valid);
963 nonzero_out.get (*t, out, &valid);
965 bufs.get (*t, out).silence (nframes, offset);
968 uint32_t m = out + natural_input_streams ().get (*t);
969 bufs.get (*t, out).read_from (inplace_bufs.get (*t, m), nframes, offset, offset);
974 /* in-place processing */
976 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i, ++pc) {
977 if ((*i)->connect_and_run(bufs, start, end, speed, in_map[pc], out_map[pc], nframes, offset)) {
981 // now silence unconnected outputs
982 inplace_silence_unconnected (bufs, _out_map, nframes, offset);
985 if (collect_signal_nframes > 0) {
987 //std::cerr << " output, bufs " << bufs.count().n_audio() << " count, " << bufs.available().n_audio() << " available" << std::endl;
988 //std::cerr << " streams " << internal_output_streams().n_audio() << std::endl;
990 _signal_analysis_outputs.set_count(output_streams());
992 for (uint32_t i = 0; i < output_streams().n_audio(); ++i) {
993 _signal_analysis_outputs.get_audio(i).read_from(
995 collect_signal_nframes,
996 _signal_analysis_collected_nframes); // offset is for target buffer
999 _signal_analysis_collected_nframes += collect_signal_nframes;
1000 assert(_signal_analysis_collected_nframes <= _signal_analysis_collect_nframes_max);
1002 if (_signal_analysis_collected_nframes == _signal_analysis_collect_nframes_max) {
1003 _signal_analysis_collect_nframes_max = 0;
1004 _signal_analysis_collected_nframes = 0;
1006 AnalysisDataGathered(&_signal_analysis_inputs,
1007 &_signal_analysis_outputs);
1011 if (_plugin_signal_latency != signal_latency ()) {
1012 _plugin_signal_latency = signal_latency ();
1018 PluginInsert::bypass (BufferSet& bufs, pframes_t nframes)
1020 /* bypass the plugin(s) not the whole processor.
1021 * -> use mappings just like connect_and_run
1024 // TODO: atomically copy maps & _no_inplace
1025 const ChanMapping in_map (no_sc_input_map ());
1026 const ChanMapping out_map (output_map ());
1027 if (_mapping_changed) {
1028 _no_inplace = check_inplace ();
1029 _mapping_changed = false;
1032 bufs.set_count(ChanCount::max(bufs.count(), _configured_internal));
1033 bufs.set_count(ChanCount::max(bufs.count(), _configured_out));
1036 ChanMapping thru_map (_thru_map);
1038 BufferSet& inplace_bufs = _session.get_noinplace_buffers();
1040 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1041 for (uint32_t in = 0; in < _configured_internal.get (*t); ++in) {
1042 inplace_bufs.get (*t, in).read_from (bufs.get (*t, in), nframes, 0, 0);
1045 ARDOUR::ChanMapping used_outputs;
1047 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1048 for (uint32_t out = 0; out < _configured_out.get (*t); ++out) {
1050 uint32_t in_idx = thru_map.get (*t, out, &valid);
1052 bufs.get (*t, out).read_from (inplace_bufs.get (*t, in_idx), nframes, 0, 0);
1053 used_outputs.set (*t, out, 1); // mark as used
1057 // plugin no-op: assume every plugin has an internal identity map
1058 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1059 for (uint32_t out = 0; out < _configured_out.get (*t); ++out) {
1061 uint32_t src_idx = out_map.get_src (*t, out, &valid);
1065 uint32_t in_idx = in_map.get (*t, src_idx, &valid);
1069 bufs.get (*t, out).read_from (inplace_bufs.get (*t, in_idx), nframes, 0, 0);
1070 used_outputs.set (*t, out, 1); // mark as used
1073 // now silence all unused outputs
1074 if (has_midi_bypass ()) {
1075 used_outputs.set (DataType::MIDI, 0, 1); // Midi bypass.
1077 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1078 for (uint32_t out = 0; out < _configured_out.get (*t); ++out) {
1080 used_outputs.get (*t, out, &valid);
1082 bufs.get (*t, out).silence (nframes, 0);
1087 if (_match.method == Split) {
1088 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1089 if (_configured_internal.get (*t) == 0) {
1092 // copy/feeds _all_ *connected* inputs, copy the first buffer
1094 uint32_t first_idx = in_map.get (*t, 0, &valid);
1095 assert (valid && first_idx == 0); // check_inplace ensures this
1096 for (uint32_t i = 1; i < natural_input_streams ().get (*t); ++i) {
1097 uint32_t idx = in_map.get (*t, i, &valid);
1100 bufs.get (*t, i).read_from (bufs.get (*t, first_idx), nframes, 0, 0);
1106 // apply output map and/or monotonic but not identity i/o mappings
1107 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1108 for (uint32_t out = 0; out < _configured_out.get (*t); ++out) {
1110 uint32_t src_idx = out_map.get_src (*t, out, &valid);
1112 bufs.get (*t, out).silence (nframes, 0);
1115 uint32_t in_idx = in_map.get (*t, src_idx, &valid);
1117 bufs.get (*t, out).silence (nframes, 0);
1120 if (in_idx != src_idx) {
1121 bufs.get (*t, out).read_from (bufs.get (*t, in_idx), nframes, 0, 0);
1129 PluginInsert::silence (framecnt_t nframes, framepos_t start_frame)
1135 _delaybuffers.flush ();
1137 ChanMapping in_map (natural_input_streams ());
1138 ChanMapping out_map (natural_output_streams ());
1139 ChanCount maxbuf = ChanCount::max (natural_input_streams (), natural_output_streams());
1141 if (is_channelstrip ()) {
1142 if (_configured_in.n_audio() > 0) {
1143 _plugins.front()->connect_and_run (_session.get_scratch_buffers (maxbuf, true), start_frame, start_frame + nframes, 1.0, in_map, out_map, nframes, 0);
1147 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
1148 (*i)->connect_and_run (_session.get_scratch_buffers (maxbuf, true), start_frame, start_frame + nframes, 1.0, in_map, out_map, nframes, 0);
1153 PluginInsert::run (BufferSet& bufs, framepos_t start_frame, framepos_t end_frame, double speed, pframes_t nframes, bool)
1156 // collect sidechain input for complete cycle (!)
1157 // TODO we need delaylines here for latency compensation
1158 _sidechain->run (bufs, start_frame, end_frame, speed, nframes, true);
1161 if (_pending_active) {
1162 /* run as normal if we are active or moving from inactive to active */
1164 if (_session.transport_rolling() || _session.bounce_processing()) {
1165 automation_run (bufs, start_frame, end_frame, speed, nframes);
1167 Glib::Threads::Mutex::Lock lm (control_lock(), Glib::Threads::TRY_LOCK);
1168 connect_and_run (bufs, start_frame, end_frame, speed, nframes, 0, lm.locked());
1172 bypass (bufs, nframes);
1173 _delaybuffers.flush ();
1176 _active = _pending_active;
1178 /* we have no idea whether the plugin generated silence or not, so mark
1179 * all buffers appropriately.
1184 PluginInsert::automation_run (BufferSet& bufs, framepos_t start, framepos_t end, double speed, pframes_t nframes)
1186 Evoral::ControlEvent next_event (0, 0.0f);
1187 framecnt_t offset = 0;
1189 Glib::Threads::Mutex::Lock lm (control_lock(), Glib::Threads::TRY_LOCK);
1192 connect_and_run (bufs, start, end, speed, nframes, offset, false);
1196 if (!find_next_event (start, end, next_event) || _plugins.front()->requires_fixed_sized_buffers()) {
1198 /* no events have a time within the relevant range */
1200 connect_and_run (bufs, start, end, speed, nframes, offset, true);
1206 framecnt_t cnt = min (((framecnt_t) ceil (next_event.when) - start), (framecnt_t) nframes);
1208 connect_and_run (bufs, start, start + cnt, speed, cnt, offset, true); // XXX (start + cnt) * speed
1214 if (!find_next_event (start, end, next_event)) {
1219 /* cleanup anything that is left to do */
1222 connect_and_run (bufs, start, start + nframes, speed, nframes, offset, true);
1227 PluginInsert::default_parameter_value (const Evoral::Parameter& param)
1229 if (param.type() != PluginAutomation)
1232 if (_plugins.empty()) {
1233 fatal << _("programming error: ") << X_("PluginInsert::default_parameter_value() called with no plugin")
1235 abort(); /*NOTREACHED*/
1238 return _plugins[0]->default_value (param.id());
1243 PluginInsert::can_reset_all_parameters ()
1246 uint32_t params = 0;
1247 for (uint32_t par = 0; par < _plugins[0]->parameter_count(); ++par) {
1249 const uint32_t cid = _plugins[0]->nth_parameter (par, ok);
1251 if (!ok || !_plugins[0]->parameter_is_input(cid)) {
1255 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter(PluginAutomation, 0, cid));
1261 if (ac->automation_state() & Play) {
1266 return all && (params > 0);
1270 PluginInsert::reset_parameters_to_default ()
1274 for (uint32_t par = 0; par < _plugins[0]->parameter_count(); ++par) {
1276 const uint32_t cid = _plugins[0]->nth_parameter (par, ok);
1278 if (!ok || !_plugins[0]->parameter_is_input(cid)) {
1282 const float dflt = _plugins[0]->default_value (cid);
1283 const float curr = _plugins[0]->get_parameter (cid);
1289 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter(PluginAutomation, 0, cid));
1294 if (ac->automation_state() & Play) {
1299 ac->set_value (dflt, Controllable::NoGroup);
1304 boost::shared_ptr<Plugin>
1305 PluginInsert::plugin_factory (boost::shared_ptr<Plugin> other)
1307 boost::shared_ptr<LadspaPlugin> lp;
1308 boost::shared_ptr<LuaProc> lua;
1310 boost::shared_ptr<LV2Plugin> lv2p;
1312 #ifdef WINDOWS_VST_SUPPORT
1313 boost::shared_ptr<WindowsVSTPlugin> vp;
1315 #ifdef LXVST_SUPPORT
1316 boost::shared_ptr<LXVSTPlugin> lxvp;
1318 #ifdef MACVST_SUPPORT
1319 boost::shared_ptr<MacVSTPlugin> mvp;
1321 #ifdef AUDIOUNIT_SUPPORT
1322 boost::shared_ptr<AUPlugin> ap;
1325 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
1326 return boost::shared_ptr<Plugin> (new LadspaPlugin (*lp));
1327 } else if ((lua = boost::dynamic_pointer_cast<LuaProc> (other)) != 0) {
1328 return boost::shared_ptr<Plugin> (new LuaProc (*lua));
1330 } else if ((lv2p = boost::dynamic_pointer_cast<LV2Plugin> (other)) != 0) {
1331 return boost::shared_ptr<Plugin> (new LV2Plugin (*lv2p));
1333 #ifdef WINDOWS_VST_SUPPORT
1334 } else if ((vp = boost::dynamic_pointer_cast<WindowsVSTPlugin> (other)) != 0) {
1335 return boost::shared_ptr<Plugin> (new WindowsVSTPlugin (*vp));
1337 #ifdef LXVST_SUPPORT
1338 } else if ((lxvp = boost::dynamic_pointer_cast<LXVSTPlugin> (other)) != 0) {
1339 return boost::shared_ptr<Plugin> (new LXVSTPlugin (*lxvp));
1341 #ifdef MACVST_SUPPORT
1342 } else if ((mvp = boost::dynamic_pointer_cast<MacVSTPlugin> (other)) != 0) {
1343 return boost::shared_ptr<Plugin> (new MacVSTPlugin (*mvp));
1345 #ifdef AUDIOUNIT_SUPPORT
1346 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
1347 return boost::shared_ptr<Plugin> (new AUPlugin (*ap));
1351 fatal << string_compose (_("programming error: %1"),
1352 X_("unknown plugin type in PluginInsert::plugin_factory"))
1354 abort(); /*NOTREACHED*/
1355 return boost::shared_ptr<Plugin> ((Plugin*) 0);
1359 PluginInsert::set_input_map (uint32_t num, ChanMapping m) {
1360 if (num < _in_map.size()) {
1361 bool changed = _in_map[num] != m;
1363 changed |= sanitize_maps ();
1365 PluginMapChanged (); /* EMIT SIGNAL */
1366 _mapping_changed = true;
1367 _session.set_dirty();
1373 PluginInsert::set_output_map (uint32_t num, ChanMapping m) {
1374 if (num < _out_map.size()) {
1375 bool changed = _out_map[num] != m;
1377 changed |= sanitize_maps ();
1379 PluginMapChanged (); /* EMIT SIGNAL */
1380 _mapping_changed = true;
1381 _session.set_dirty();
1387 PluginInsert::set_thru_map (ChanMapping m) {
1388 bool changed = _thru_map != m;
1390 changed |= sanitize_maps ();
1392 PluginMapChanged (); /* EMIT SIGNAL */
1393 _mapping_changed = true;
1394 _session.set_dirty();
1399 PluginInsert::pre_seed (const ChanCount& in, const ChanCount& out,
1400 const ChanMapping& im, const ChanMapping& om, const ChanMapping& tm)
1402 if (_configured) { return false; }
1403 _configured_in = in;
1404 _configured_out = out;
1408 _maps_from_state = in.n_total () > 0 && out.n_total () > 0;
1413 PluginInsert::input_map () const
1417 for (PinMappings::const_iterator i = _in_map.begin (); i != _in_map.end (); ++i, ++pc) {
1418 ChanMapping m (i->second);
1419 const ChanMapping::Mappings& mp ((*i).second.mappings());
1420 for (ChanMapping::Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
1421 for (ChanMapping::TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
1422 rv.set (tm->first, i->first + pc * natural_input_streams().get(tm->first), i->second);
1431 PluginInsert::no_sc_input_map () const
1435 for (PinMappings::const_iterator i = _in_map.begin (); i != _in_map.end (); ++i, ++pc) {
1436 ChanMapping m (i->second);
1437 const ChanMapping::Mappings& mp ((*i).second.mappings());
1438 for (ChanMapping::Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
1439 uint32_t ins = natural_input_streams().get(tm->first) - _cached_sidechain_pins.get(tm->first);
1440 for (ChanMapping::TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
1441 if (i->first < ins) {
1442 rv.set (tm->first, i->first + pc * ins, i->second);
1451 PluginInsert::output_map () const
1455 for (PinMappings::const_iterator i = _out_map.begin (); i != _out_map.end (); ++i, ++pc) {
1456 ChanMapping m (i->second);
1457 const ChanMapping::Mappings& mp ((*i).second.mappings());
1458 for (ChanMapping::Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
1459 for (ChanMapping::TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
1460 rv.set (tm->first, i->first + pc * natural_output_streams().get(tm->first), i->second);
1464 if (has_midi_bypass ()) {
1465 rv.set (DataType::MIDI, 0, 0);
1472 PluginInsert::has_midi_bypass () const
1474 if (_configured_in.n_midi () == 1 && _configured_out.n_midi () == 1
1475 && natural_output_streams ().n_midi () == 0) {
1482 PluginInsert::has_midi_thru () const
1484 if (_configured_in.n_midi () == 1 && _configured_out.n_midi () == 1
1485 && natural_input_streams ().n_midi () == 0 && natural_output_streams ().n_midi () == 0) {
1493 PluginInsert::is_channelstrip () const {
1494 return _plugins.front()->is_channelstrip();
1499 PluginInsert::check_inplace ()
1501 bool inplace_ok = !_plugins.front()->inplace_broken ();
1503 if (_thru_map.n_total () > 0) {
1504 // TODO once midi-bypass is part of the mapping, ignore it
1508 if (_match.method == Split && inplace_ok) {
1509 assert (get_count() == 1);
1510 assert (_in_map.size () == 1);
1511 if (!_out_map[0].is_monotonic ()) {
1514 if (_configured_internal != _configured_in) {
1515 /* no sidechain -- TODO we could allow this with
1516 * some more logic in PluginInsert::connect_and_run().
1518 * PluginInsert::reset_map() already maps it.
1523 for (DataType::iterator t = DataType::begin(); t != DataType::end() && inplace_ok; ++t) {
1524 if (_configured_internal.get (*t) == 0) {
1528 uint32_t first_idx = _in_map[0].get (*t, 0, &valid);
1529 if (!valid || first_idx != 0) {
1530 // so far only allow to copy the *first* stream's buffer to others
1533 for (uint32_t i = 1; i < natural_input_streams ().get (*t); ++i) {
1534 uint32_t idx = _in_map[0].get (*t, i, &valid);
1535 if (valid && idx != first_idx) {
1544 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: In Place Split Map\n", name()));
1549 for (uint32_t pc = 0; pc < get_count() && inplace_ok ; ++pc) {
1550 if (!_in_map[pc].is_monotonic ()) {
1553 if (!_out_map[pc].is_monotonic ()) {
1559 /* check if every output is fed by the corresponding input
1561 * this prevents in-port 1 -> sink-pin 2 || source-pin 1 -> out port 1, source-pin 2 -> out port 2
1562 * (with in-place, source-pin 1 -> out port 1 overwrites in-port 1)
1564 * but allows in-port 1 -> sink-pin 2 || source-pin 2 -> out port 1
1566 ChanMapping in_map (input_map ());
1567 const ChanMapping::Mappings out_m (output_map ().mappings ());
1568 for (ChanMapping::Mappings::const_iterator t = out_m.begin (); t != out_m.end () && inplace_ok; ++t) {
1569 for (ChanMapping::TypeMapping::const_iterator c = (*t).second.begin (); c != (*t).second.end () ; ++c) {
1570 /* src-pin: c->first, out-port: c->second */
1572 uint32_t in_port = in_map.get (t->first, c->first, &valid);
1573 if (valid && in_port != c->second) {
1581 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: %2\n", name(), inplace_ok ? "In-Place" : "No Inplace Processing"));
1582 return !inplace_ok; // no-inplace
1586 PluginInsert::sanitize_maps ()
1588 bool changed = false;
1589 /* strip dead wood */
1590 PinMappings new_ins;
1591 PinMappings new_outs;
1592 ChanMapping new_thru;
1594 for (uint32_t pc = 0; pc < get_count(); ++pc) {
1596 ChanMapping new_out;
1597 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1598 for (uint32_t i = 0; i < natural_input_streams().get (*t); ++i) {
1600 uint32_t idx = _in_map[pc].get (*t, i, &valid);
1601 if (valid && idx < _configured_internal.get (*t)) {
1602 new_in.set (*t, i, idx);
1605 for (uint32_t o = 0; o < natural_output_streams().get (*t); ++o) {
1607 uint32_t idx = _out_map[pc].get (*t, o, &valid);
1608 if (valid && idx < _configured_out.get (*t)) {
1609 new_out.set (*t, o, idx);
1613 if (_in_map[pc] != new_in || _out_map[pc] != new_out) {
1616 new_ins[pc] = new_in;
1617 new_outs[pc] = new_out;
1620 /* prevent dup output assignments */
1621 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1622 for (uint32_t o = 0; o < _configured_out.get (*t); ++o) {
1623 bool mapped = false;
1624 for (uint32_t pc = 0; pc < get_count(); ++pc) {
1626 uint32_t idx = new_outs[pc].get_src (*t, o, &valid);
1627 if (valid && mapped) {
1628 new_outs[pc].unset (*t, idx);
1636 /* remove excess thru */
1637 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1638 for (uint32_t o = 0; o < _configured_out.get (*t); ++o) {
1640 uint32_t idx = _thru_map.get (*t, o, &valid);
1641 if (valid && idx < _configured_internal.get (*t)) {
1642 new_thru.set (*t, o, idx);
1647 /* prevent out + thru, existing plugin outputs override thru */
1648 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1649 for (uint32_t o = 0; o < _configured_out.get (*t); ++o) {
1650 bool mapped = false;
1652 for (uint32_t pc = 0; pc < get_count(); ++pc) {
1653 new_outs[pc].get_src (*t, o, &mapped);
1654 if (mapped) { break; }
1656 if (!mapped) { continue; }
1657 uint32_t idx = new_thru.get (*t, o, &valid);
1659 new_thru.unset (*t, idx);
1664 if (has_midi_bypass ()) {
1665 // TODO: include midi-bypass in the thru set,
1666 // remove dedicated handling.
1667 new_thru.unset (DataType::MIDI, 0);
1670 if (_in_map != new_ins || _out_map != new_outs || _thru_map != new_thru) {
1674 _out_map = new_outs;
1675 _thru_map = new_thru;
1681 PluginInsert::reset_map (bool emit)
1683 const PinMappings old_in (_in_map);
1684 const PinMappings old_out (_out_map);
1688 _thru_map = ChanMapping ();
1690 /* build input map */
1691 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1692 uint32_t sc = 0; // side-chain round-robin (all instances)
1694 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i, ++pc) {
1695 const uint32_t nis = natural_input_streams ().get(*t);
1696 const uint32_t stride = nis - sidechain_input_pins().get (*t);
1698 /* SC inputs are last in the plugin-insert.. */
1699 const uint32_t sc_start = _configured_in.get (*t);
1700 const uint32_t sc_len = _configured_internal.get (*t) - sc_start;
1701 /* ...but may not be at the end of the plugin ports.
1702 * in case the side-chain is not the last port, shift connections back.
1703 * and connect to side-chain
1706 uint32_t ic = 0; // split inputs
1707 const uint32_t cend = _configured_in.get (*t);
1709 for (uint32_t in = 0; in < nis; ++in) {
1710 const Plugin::IOPortDescription& iod (_plugins[pc]->describe_io_port (*t, true, in));
1711 if (iod.is_sidechain) {
1712 /* connect sidechain sinks to sidechain inputs in round-robin fashion */
1713 if (sc_len > 0) {// side-chain may be hidden
1714 _in_map[pc].set (*t, in, sc_start + sc);
1715 sc = (sc + 1) % sc_len;
1719 if (_match.method == Split) {
1720 if (cend == 0) { continue; }
1721 if (_strict_io && ic + stride * pc >= cend) {
1724 /* connect *no* sidechain sinks in round-robin fashion */
1725 _in_map[pc].set (*t, in, ic + stride * pc);
1726 if (_strict_io && (ic + 1) == cend) {
1729 ic = (ic + 1) % cend;
1731 uint32_t s = in - shift;
1732 if (stride * pc + s < cend) {
1733 _in_map[pc].set (*t, in, s + stride * pc);
1741 /* build output map */
1743 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i, ++pc) {
1744 _out_map[pc] = ChanMapping (ChanCount::min (natural_output_streams(), _configured_out));
1745 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1746 _out_map[pc].offset_to(*t, pc * natural_output_streams().get(*t));
1751 if (old_in == _in_map && old_out == _out_map) {
1755 PluginMapChanged (); /* EMIT SIGNAL */
1756 _mapping_changed = true;
1757 _session.set_dirty();
1763 PluginInsert::configure_io (ChanCount in, ChanCount out)
1765 Match old_match = _match;
1767 ChanCount old_internal;
1771 old_pins = natural_input_streams();
1772 old_in = _configured_in;
1773 old_out = _configured_out;
1774 old_internal = _configured_internal;
1776 _configured_in = in;
1777 _configured_internal = in;
1778 _configured_out = out;
1781 /* TODO hide midi-bypass, and custom outs. Best /fake/ "out" here.
1782 * (currently _sidechain->configure_io always succeeds
1783 * since Processor::configure_io() succeeds)
1785 if (!_sidechain->configure_io (in, out)) {
1786 DEBUG_TRACE (DEBUG::ChanMapping, "Sidechain configuration failed\n");
1789 _configured_internal += _sidechain->input()->n_ports();
1791 // include (static_cast<Route*>owner())->name() ??
1792 _sidechain->input ()-> set_pretty_name (string_compose (_("SC %1"), name ()));
1795 /* get plugin configuration */
1796 _match = private_can_support_io_configuration (in, out);
1798 if (DEBUG_ENABLED(DEBUG::ChanMapping)) {
1800 DEBUG_STR_APPEND(a, string_compose ("%1: ", name()));
1801 DEBUG_STR_APPEND(a, _match);
1802 DEBUG_TRACE (DEBUG::ChanMapping, DEBUG_STR(a).str());
1806 /* set the matching method and number of plugins that we will use to meet this configuration */
1807 if (set_count (_match.plugins) == false) {
1808 PluginIoReConfigure (); /* EMIT SIGNAL */
1809 _configured = false;
1813 /* configure plugins */
1814 switch (_match.method) {
1817 if (_plugins.front()->configure_io (natural_input_streams(), out) == false) {
1818 PluginIoReConfigure (); /* EMIT SIGNAL */
1819 _configured = false;
1825 ChanCount din (_configured_internal);
1826 ChanCount dout (din); // hint
1828 if (_custom_sinks.n_total () > 0) {
1829 din = _custom_sinks;
1832 } else if (_preset_out.n_audio () > 0) {
1833 dout.set (DataType::AUDIO, _preset_out.n_audio ());
1834 } else if (dout.n_midi () > 0 && dout.n_audio () == 0) {
1835 dout.set (DataType::AUDIO, 2);
1837 if (out.n_audio () == 0) { out.set (DataType::AUDIO, 1); }
1839 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: Delegate lookup : %2 %3\n", name(), din, dout));
1840 bool const r = _plugins.front()->can_support_io_configuration (din, dout, &useins);
1842 if (useins.n_audio() == 0) {
1845 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: Delegate configuration: %2 %3\n", name(), useins, dout));
1847 if (_plugins.front()->configure_io (useins, dout) == false) {
1848 PluginIoReConfigure (); /* EMIT SIGNAL */
1849 _configured = false;
1853 _custom_sinks = din;
1858 if (_plugins.front()->configure_io (in, out) == false) {
1859 PluginIoReConfigure (); /* EMIT SIGNAL */
1860 _configured = false;
1866 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",
1868 _configured ? "Y" : "N",
1869 _maps_from_state ? "Y" : "N",
1870 old_in == in ? "==" : "!=",
1871 old_out == out ? "==" : "!=",
1872 old_pins == natural_input_streams () ? "==" : "!=",
1873 old_match.method == _match.method ? "==" : "!=",
1874 old_match.custom_cfg == _match.custom_cfg ? "==" : "!=",
1875 _in_map.size() == get_count () ? "==" : "!=",
1876 _out_map.size() == get_count () ? "==" : "!="
1879 bool mapping_changed = false;
1880 if (old_in == in && old_out == out
1882 && old_pins == natural_input_streams ()
1883 && old_match.method == _match.method
1884 && old_match.custom_cfg == _match.custom_cfg
1885 && _in_map.size() == _out_map.size()
1886 && _in_map.size() == get_count ()
1888 /* If the configuration has not changed, keep the mapping */
1889 mapping_changed = sanitize_maps ();
1890 } else if (_match.custom_cfg && _configured) {
1891 /* don't touch the map in manual mode */
1892 mapping_changed = sanitize_maps ();
1895 if (is_channelstrip ()) {
1896 /* fake channel map - for wire display */
1899 _thru_map = ChanMapping ();
1900 _in_map[0] = ChanMapping (ChanCount::min (_configured_in, ChanCount (DataType::AUDIO, 2)));
1901 _out_map[0] = ChanMapping (ChanCount::min (_configured_out, ChanCount (DataType::AUDIO, 2)));
1902 /* set "thru" map for in-place forward of audio */
1903 for (uint32_t i = 2; i < _configured_in.n_audio(); ++i) {
1904 _thru_map.set (DataType::AUDIO, i, i);
1906 /* and midi (after implicit 1st channel bypass) */
1907 for (uint32_t i = 1; i < _configured_in.n_midi(); ++i) {
1908 _thru_map.set (DataType::MIDI, i, i);
1912 if (_maps_from_state && old_in == in && old_out == out) {
1913 mapping_changed = true;
1916 /* generate a new mapping */
1917 mapping_changed = reset_map (false);
1919 _maps_from_state = false;
1922 if (mapping_changed) {
1923 PluginMapChanged (); /* EMIT SIGNAL */
1926 if (DEBUG_ENABLED(DEBUG::ChanMapping)) {
1929 DEBUG_STR_APPEND(a, "\n--------<<--------\n");
1930 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i, ++pc) {
1932 DEBUG_STR_APPEND(a, "----><----\n");
1934 DEBUG_STR_APPEND(a, string_compose ("Channel Map for %1 plugin %2\n", name(), pc));
1935 DEBUG_STR_APPEND(a, " * Inputs:\n");
1936 DEBUG_STR_APPEND(a, _in_map[pc]);
1937 DEBUG_STR_APPEND(a, " * Outputs:\n");
1938 DEBUG_STR_APPEND(a, _out_map[pc]);
1940 DEBUG_STR_APPEND(a, " * Thru:\n");
1941 DEBUG_STR_APPEND(a, _thru_map);
1942 DEBUG_STR_APPEND(a, "-------->>--------\n");
1943 DEBUG_TRACE (DEBUG::ChanMapping, DEBUG_STR(a).str());
1948 _no_inplace = check_inplace ();
1949 _mapping_changed = false;
1951 /* only the "noinplace_buffers" thread buffers need to be this large,
1952 * this can be optimized. other buffers are fine with
1953 * ChanCount::max (natural_input_streams (), natural_output_streams())
1954 * and route.cc's max (configured_in, configured_out)
1956 * no-inplace copies "thru" outputs (to emulate in-place) for
1957 * all outputs (to prevent overwrite) into a temporary space
1958 * which also holds input buffers (in case the plugin does process
1959 * in-place and overwrites those).
1961 * this buffers need to be at least as
1962 * natural_input_streams () + possible outputs.
1964 * sidechain inputs add a constraint on the input:
1965 * configured input + sidechain (=_configured_internal)
1967 * NB. this also satisfies
1968 * max (natural_input_streams(), natural_output_streams())
1969 * which is needed for silence runs
1971 _required_buffers = ChanCount::max (_configured_internal,
1972 natural_input_streams () + ChanCount::max (_configured_out, natural_output_streams () * get_count ()));
1974 if (old_in != in || old_out != out || old_internal != _configured_internal
1975 || old_pins != natural_input_streams ()
1976 || (old_match.method != _match.method && (old_match.method == Split || _match.method == Split))
1978 PluginIoReConfigure (); /* EMIT SIGNAL */
1981 _delaybuffers.configure (_configured_out, _plugins.front ()->max_latency ());
1982 _latency_changed = true;
1984 // we don't know the analysis window size, so we must work with the
1985 // current buffer size here. each request for data fills in these
1986 // buffers and the analyser makes sure it gets enough data for the
1988 session().ensure_buffer_set (_signal_analysis_inputs, in);
1989 _signal_analysis_inputs.set_count (in);
1991 session().ensure_buffer_set (_signal_analysis_outputs, out);
1992 _signal_analysis_outputs.set_count (out);
1994 // std::cerr << "set counts to i" << in.n_audio() << "/o" << out.n_audio() << std::endl;
1997 return Processor::configure_io (in, out);
2000 /** Decide whether this PluginInsert can support a given IO configuration.
2001 * To do this, we run through a set of possible solutions in rough order of
2004 * @param in Required input channel count.
2005 * @param out Filled in with the output channel count if we return true.
2006 * @return true if the given IO configuration can be supported.
2009 PluginInsert::can_support_io_configuration (const ChanCount& in, ChanCount& out)
2012 _sidechain->can_support_io_configuration (in, out); // never fails, sets "out"
2014 return private_can_support_io_configuration (in, out).method != Impossible;
2018 PluginInsert::private_can_support_io_configuration (ChanCount const& in, ChanCount& out) const
2020 if (!_custom_cfg && _preset_out.n_audio () > 0) {
2021 // preseed hint (for variable i/o)
2022 out.set (DataType::AUDIO, _preset_out.n_audio ());
2025 Match rv = internal_can_support_io_configuration (in, out);
2027 if (!_custom_cfg && _preset_out.n_audio () > 0) {
2028 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: using output preset: %2\n", name(), _preset_out));
2029 out.set (DataType::AUDIO, _preset_out.n_audio ());
2034 /** A private version of can_support_io_configuration which returns the method
2035 * by which the configuration can be matched, rather than just whether or not
2039 PluginInsert::internal_can_support_io_configuration (ChanCount const & inx, ChanCount& out) const
2041 if (_plugins.empty()) {
2046 if (is_channelstrip ()) {
2048 return Match (ExactMatch, 1);
2052 /* if a user specified a custom cfg, so be it. */
2054 PluginInfoPtr info = _plugins.front()->get_info();
2056 if (info->reconfigurable_io()) {
2057 return Match (Delegate, 1, _strict_io, true);
2059 return Match (ExactMatch, get_count(), _strict_io, true);
2063 /* try automatic configuration */
2064 Match m = PluginInsert::automatic_can_support_io_configuration (inx, out);
2066 PluginInfoPtr info = _plugins.front()->get_info();
2067 ChanCount inputs = info->n_inputs;
2068 ChanCount outputs = info->n_outputs;
2070 /* handle case strict-i/o */
2071 if (_strict_io && m.method != Impossible) {
2074 /* special case MIDI instruments */
2075 if (is_instrument ()) {
2076 // output = midi-bypass + at most master-out channels.
2077 ChanCount max_out (DataType::AUDIO, 2); // TODO use master-out
2078 max_out.set (DataType::MIDI, out.get(DataType::MIDI));
2079 out = ChanCount::min (out, max_out);
2080 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: special case strict-i/o instrument\n", name()));
2086 if (inx.n_audio () != out.n_audio ()) { // ignore midi bypass
2087 /* replicate processor to match output count (generators and such)
2088 * at least enough to feed every output port. */
2089 uint32_t f = 1; // at least one. e.g. control data filters, no in, no out.
2090 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2091 uint32_t nout = outputs.get (*t);
2092 if (nout == 0 || inx.get(*t) == 0) { continue; }
2093 f = max (f, (uint32_t) ceil (inx.get(*t) / (float)nout));
2096 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: special case strict-i/o for generator\n", name()));
2097 return Match (Replicate, f, _strict_io);
2108 if (m.method != Impossible) {
2112 ChanCount ns_inputs = inputs - sidechain_input_pins ();
2114 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: resolving 'Impossible' match...\n", name()));
2116 if (info->reconfigurable_io()) {
2119 if (out.n_midi () > 0 && out.n_audio () == 0) { out.set (DataType::AUDIO, 2); }
2120 if (out.n_audio () == 0) { out.set (DataType::AUDIO, 1); }
2121 bool const r = _plugins.front()->can_support_io_configuration (inx + sidechain_input_pins (), out, &useins);
2123 // houston, we have a problem.
2124 return Match (Impossible, 0);
2127 if (inx.n_midi () > 0 && out.n_midi () == 0) { out.set (DataType::MIDI, 1); }
2128 return Match (Delegate, 1, _strict_io);
2131 ChanCount midi_bypass;
2132 if (inx.get(DataType::MIDI) == 1 && outputs.get(DataType::MIDI) == 0) {
2133 midi_bypass.set (DataType::MIDI, 1);
2136 // add at least as many plugins so that output count matches input count (w/o sidechain pins)
2138 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2139 uint32_t nin = ns_inputs.get (*t);
2140 uint32_t nout = outputs.get (*t);
2141 if (nin == 0 || inx.get(*t) == 0) { continue; }
2142 // prefer floor() so the count won't overly increase IFF (nin < nout)
2143 f = max (f, (uint32_t) floor (inx.get(*t) / (float)nout));
2145 if (f > 0 && outputs * f >= _configured_out) {
2146 out = outputs * f + midi_bypass;
2147 return Match (Replicate, f, _strict_io);
2150 // add at least as many plugins needed to connect all inputs (w/o sidechain pins)
2152 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2153 uint32_t nin = ns_inputs.get (*t);
2154 if (nin == 0 || inx.get(*t) == 0) { continue; }
2155 f = max (f, (uint32_t) ceil (inx.get(*t) / (float)nin));
2158 out = outputs * f + midi_bypass;
2159 return Match (Replicate, f, _strict_io);
2162 // add at least as many plugins needed to connect all inputs
2164 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2165 uint32_t nin = inputs.get (*t);
2166 if (nin == 0 || inx.get(*t) == 0) { continue; }
2167 f = max (f, (uint32_t) ceil (inx.get(*t) / (float)nin));
2169 out = outputs * f + midi_bypass;
2170 return Match (Replicate, f, _strict_io);
2173 /* this is the original Ardour 3/4 behavior, mainly for backwards compatibility */
2175 PluginInsert::automatic_can_support_io_configuration (ChanCount const & inx, ChanCount& out) const
2177 if (_plugins.empty()) {
2181 PluginInfoPtr info = _plugins.front()->get_info();
2182 ChanCount in; in += inx;
2183 ChanCount midi_bypass;
2185 if (info->reconfigurable_io()) {
2186 /* Plugin has flexible I/O, so delegate to it
2187 * pre-seed outputs, plugin tries closest match
2190 if (out.n_midi () > 0 && out.n_audio () == 0) { out.set (DataType::AUDIO, 2); }
2191 if (out.n_audio () == 0) { out.set (DataType::AUDIO, 1); }
2192 bool const r = _plugins.front()->can_support_io_configuration (in + sidechain_input_pins (), out);
2194 return Match (Impossible, 0);
2197 if (in.n_midi () > 0 && out.n_midi () == 0) { out.set (DataType::MIDI, 1); }
2198 return Match (Delegate, 1);
2201 ChanCount inputs = info->n_inputs;
2202 ChanCount outputs = info->n_outputs;
2203 ChanCount ns_inputs = inputs - sidechain_input_pins ();
2205 if (in.get(DataType::MIDI) == 1 && outputs.get(DataType::MIDI) == 0) {
2206 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: bypassing midi-data\n", name()));
2207 midi_bypass.set (DataType::MIDI, 1);
2209 if (in.get(DataType::MIDI) == 1 && inputs.get(DataType::MIDI) == 0) {
2210 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: hiding midi-port from plugin\n", name()));
2211 in.set(DataType::MIDI, 0);
2214 // add internally provided sidechain ports
2215 ChanCount insc = in + sidechain_input_ports ();
2217 bool no_inputs = true;
2218 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2219 if (inputs.get (*t) != 0) {
2226 /* no inputs so we can take any input configuration since we throw it away */
2227 out = outputs + midi_bypass;
2228 return Match (NoInputs, 1);
2231 /* Plugin inputs match requested inputs + side-chain-ports exactly */
2232 if (inputs == insc) {
2233 out = outputs + midi_bypass;
2234 return Match (ExactMatch, 1);
2237 /* Plugin inputs matches without side-chain-pins */
2238 if (ns_inputs == in) {
2239 out = outputs + midi_bypass;
2240 return Match (ExactMatch, 1);
2243 /* We may be able to run more than one copy of the plugin within this insert
2244 to cope with the insert having more inputs than the plugin.
2245 We allow replication only for plugins with either zero or 1 inputs and outputs
2246 for every valid data type.
2250 bool can_replicate = true;
2251 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2253 // ignore side-chains
2254 uint32_t nin = ns_inputs.get (*t);
2256 // No inputs of this type
2257 if (nin == 0 && in.get(*t) == 0) {
2261 if (nin != 1 || outputs.get (*t) != 1) {
2262 can_replicate = false;
2266 // Potential factor not set yet
2268 f = in.get(*t) / nin;
2271 // Factor for this type does not match another type, can not replicate
2272 if (f != (in.get(*t) / nin)) {
2273 can_replicate = false;
2278 if (can_replicate && f > 0) {
2279 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2280 out.set (*t, outputs.get(*t) * f);
2283 return Match (Replicate, f);
2286 /* If the processor has exactly one input of a given type, and
2287 the plugin has more, we can feed the single processor input
2288 to some or all of the plugin inputs. This is rather
2289 special-case-y, but the 1-to-many case is by far the
2290 simplest. How do I split thy 2 processor inputs to 3
2291 plugin inputs? Let me count the ways ...
2294 bool can_split = true;
2295 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2297 bool const can_split_type = (in.get (*t) == 1 && ns_inputs.get (*t) > 1);
2298 bool const nothing_to_do_for_type = (in.get (*t) == 0 && inputs.get (*t) == 0);
2300 if (!can_split_type && !nothing_to_do_for_type) {
2306 out = outputs + midi_bypass;
2307 return Match (Split, 1);
2310 /* If the plugin has more inputs than we want, we can `hide' some of them
2311 by feeding them silence.
2314 bool could_hide = false;
2315 bool cannot_hide = false;
2316 ChanCount hide_channels;
2318 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2319 if (inputs.get(*t) > in.get(*t)) {
2320 /* there is potential to hide, since the plugin has more inputs of type t than the insert */
2321 hide_channels.set (*t, inputs.get(*t) - in.get(*t));
2323 } else if (inputs.get(*t) < in.get(*t)) {
2324 /* we definitely cannot hide, since the plugin has fewer inputs of type t than the insert */
2329 if (could_hide && !cannot_hide) {
2330 out = outputs + midi_bypass;
2331 return Match (Hide, 1, false, false, hide_channels);
2334 return Match (Impossible, 0);
2339 PluginInsert::get_state ()
2341 return state (true);
2345 PluginInsert::state (bool full)
2347 XMLNode& node = Processor::state (full);
2349 node.set_property("type", _plugins[0]->state_node_name());
2350 node.set_property("unique-id", _plugins[0]->unique_id());
2351 node.set_property("count", (uint32_t)_plugins.size());
2353 /* remember actual i/o configuration (for later placeholder
2354 * in case the plugin goes missing) */
2355 node.add_child_nocopy (* _configured_in.state (X_("ConfiguredInput")));
2356 node.add_child_nocopy (* _custom_sinks.state (X_("CustomSinks")));
2357 node.add_child_nocopy (* _configured_out.state (X_("ConfiguredOutput")));
2358 node.add_child_nocopy (* _preset_out.state (X_("PresetOutput")));
2360 /* save custom i/o config */
2361 node.set_property("custom", _custom_cfg);
2362 for (uint32_t pc = 0; pc < get_count(); ++pc) {
2364 snprintf (tmp, sizeof(tmp), "InputMap-%d", pc);
2365 node.add_child_nocopy (* _in_map[pc].state (tmp));
2366 snprintf (tmp, sizeof(tmp), "OutputMap-%d", pc);
2367 node.add_child_nocopy (* _out_map[pc].state (tmp));
2369 node.add_child_nocopy (* _thru_map.state ("ThruMap"));
2372 node.add_child_nocopy (_sidechain->state (full));
2375 _plugins[0]->set_insert_id(this->id());
2376 node.add_child_nocopy (_plugins[0]->get_state());
2378 for (Controls::iterator c = controls().begin(); c != controls().end(); ++c) {
2379 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> ((*c).second);
2381 node.add_child_nocopy (ac->get_state());
2389 PluginInsert::set_control_ids (const XMLNode& node, int version)
2391 const XMLNodeList& nlist = node.children();
2392 XMLNodeConstIterator iter;
2393 set<Evoral::Parameter>::const_iterator p;
2395 for (iter = nlist.begin(); iter != nlist.end(); ++iter) {
2396 if ((*iter)->name() == Controllable::xml_node_name) {
2398 uint32_t p = (uint32_t)-1;
2401 if ((*iter)->get_property (X_("symbol"), str)) {
2402 boost::shared_ptr<LV2Plugin> lv2plugin = boost::dynamic_pointer_cast<LV2Plugin> (_plugins[0]);
2404 p = lv2plugin->port_index(str.c_str());
2408 if (p == (uint32_t)-1) {
2409 (*iter)->get_property (X_("parameter"), p);
2412 if (p != (uint32_t)-1) {
2414 /* this may create the new controllable */
2416 boost::shared_ptr<Evoral::Control> c = control (Evoral::Parameter (PluginAutomation, 0, p));
2418 #ifndef NO_PLUGIN_STATE
2422 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> (c);
2424 ac->set_state (**iter, version);
2433 PluginInsert::set_state(const XMLNode& node, int version)
2435 XMLNodeList nlist = node.children();
2436 XMLNodeIterator niter;
2437 XMLPropertyList plist;
2438 ARDOUR::PluginType type;
2441 if (!node.get_property ("type", str)) {
2442 error << _("XML node describing plugin is missing the `type' field") << endmsg;
2446 if (str == X_("ladspa") || str == X_("Ladspa")) { /* handle old school sessions */
2447 type = ARDOUR::LADSPA;
2448 } else if (str == X_("lv2")) {
2450 } else if (str == X_("windows-vst")) {
2451 type = ARDOUR::Windows_VST;
2452 } else if (str == X_("lxvst")) {
2453 type = ARDOUR::LXVST;
2454 } else if (str == X_("mac-vst")) {
2455 type = ARDOUR::MacVST;
2456 } else if (str == X_("audiounit")) {
2457 type = ARDOUR::AudioUnit;
2458 } else if (str == X_("luaproc")) {
2461 error << string_compose (_("unknown plugin type %1 in plugin insert state"), str) << endmsg;
2465 XMLProperty const * prop = node.property ("unique-id");
2468 #ifdef WINDOWS_VST_SUPPORT
2469 /* older sessions contain VST plugins with only an "id" field. */
2470 if (type == ARDOUR::Windows_VST) {
2471 prop = node.property ("id");
2475 #ifdef LXVST_SUPPORT
2476 /*There shouldn't be any older sessions with linuxVST support.. but anyway..*/
2477 if (type == ARDOUR::LXVST) {
2478 prop = node.property ("id");
2485 error << _("Plugin has no unique ID field") << endmsg;
2490 boost::shared_ptr<Plugin> plugin = find_plugin (_session, prop->value(), type);
2491 bool any_vst = false;
2493 /* treat VST plugins equivalent if they have the same uniqueID
2494 * allow to move sessions windows <> linux */
2495 #ifdef LXVST_SUPPORT
2496 if (plugin == 0 && (type == ARDOUR::Windows_VST || type == ARDOUR::MacVST)) {
2497 type = ARDOUR::LXVST;
2498 plugin = find_plugin (_session, prop->value(), type);
2499 if (plugin) { any_vst = true; }
2503 #ifdef WINDOWS_VST_SUPPORT
2504 if (plugin == 0 && (type == ARDOUR::LXVST || type == ARDOUR::MacVST)) {
2505 type = ARDOUR::Windows_VST;
2506 plugin = find_plugin (_session, prop->value(), type);
2507 if (plugin) { any_vst = true; }
2511 #ifdef MACVST_SUPPORT
2512 if (plugin == 0 && (type == ARDOUR::Windows_VST || type == ARDOUR::LXVST)) {
2513 type = ARDOUR::MacVST;
2514 plugin = find_plugin (_session, prop->value(), type);
2515 if (plugin) { any_vst = true; }
2519 if (plugin == 0 && type == ARDOUR::Lua) {
2520 /* unique ID (sha1 of script) was not found,
2521 * load the plugin from the serialized version in the
2522 * session-file instead.
2524 boost::shared_ptr<LuaProc> lp (new LuaProc (_session.engine(), _session, ""));
2525 XMLNode *ls = node.child (lp->state_node_name().c_str());
2527 lp->set_script_from_state (*ls);
2533 error << string_compose(
2534 _("Found a reference to a plugin (\"%1\") that is unknown.\n"
2535 "Perhaps it was removed or moved since it was last used."),
2541 // The name of the PluginInsert comes from the plugin, nothing else
2542 _name = plugin->get_info()->name;
2546 // Processor::set_state() will set this, but too late
2547 // for it to be available when setting up plugin
2548 // state. We can't call Processor::set_state() until
2549 // the plugins themselves are created and added.
2553 if (_plugins.empty()) {
2554 /* if we are adding the first plugin, we will need to set
2555 up automatable controls.
2557 add_plugin (plugin);
2558 create_automatable_parameters ();
2559 set_control_ids (node, version);
2562 node.get_property ("count", count);
2564 if (_plugins.size() != count) {
2565 for (uint32_t n = 1; n < count; ++n) {
2566 add_plugin (plugin_factory (plugin));
2570 Processor::set_state (node, version);
2572 PBD::ID new_id = this->id();
2573 PBD::ID old_id = this->id();
2575 node.get_property ("id", old_id);
2577 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2579 /* find the node with the type-specific node name ("lv2", "ladspa", etc)
2580 and set all plugins to the same state.
2583 if ( ((*niter)->name() == plugin->state_node_name())
2584 || (any_vst && ((*niter)->name() == "lxvst" || (*niter)->name() == "windows-vst" || (*niter)->name() == "mac-vst"))
2587 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
2588 /* Plugin state can include external files which are named after the ID.
2590 * If regenerate_xml_or_string_ids() is set, the ID will already have
2591 * been changed, so we need to use the old ID from the XML to load the
2592 * state and then update the ID.
2594 * When copying a plugin-state, route_ui takes care of of updating the ID,
2595 * but we need to call set_insert_id() to clear the cached plugin-state
2596 * and force a change.
2598 if (!regenerate_xml_or_string_ids ()) {
2599 (*i)->set_insert_id (new_id);
2601 (*i)->set_insert_id (old_id);
2604 (*i)->set_state (**niter, version);
2606 if (regenerate_xml_or_string_ids ()) {
2607 (*i)->set_insert_id (new_id);
2611 /* when copying plugin state, notify UI */
2612 for (Controls::const_iterator li = controls().begin(); li != controls().end(); ++li) {
2613 boost::shared_ptr<PBD::Controllable> c = boost::dynamic_pointer_cast<PBD::Controllable> (li->second);
2615 c->Changed (false, Controllable::NoGroup); /* EMIT SIGNAL */
2623 if (version < 3000) {
2625 /* Only 2.X sessions need a call to set_parameter_state() - in 3.X and above
2626 this is all handled by Automatable
2629 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2630 if ((*niter)->name() == "Redirect") {
2631 /* XXX do we need to tackle placement? i think not (pd; oct 16 2009) */
2632 Processor::set_state (**niter, version);
2637 set_parameter_state_2X (node, version);
2640 node.get_property (X_("custom"), _custom_cfg);
2642 uint32_t in_maps = 0;
2643 uint32_t out_maps = 0;
2644 XMLNodeList kids = node.children ();
2645 for (XMLNodeIterator i = kids.begin(); i != kids.end(); ++i) {
2646 if ((*i)->name() == X_("ConfiguredInput")) {
2647 _configured_in = ChanCount(**i);
2649 if ((*i)->name() == X_("CustomSinks")) {
2650 _custom_sinks = ChanCount(**i);
2652 if ((*i)->name() == X_("ConfiguredOutput")) {
2653 _custom_out = ChanCount(**i);
2654 _configured_out = ChanCount(**i);
2656 if ((*i)->name() == X_("PresetOutput")) {
2657 _preset_out = ChanCount(**i);
2659 if (strncmp ((*i)->name ().c_str(), X_("InputMap-"), 9) == 0) {
2660 long pc = atol (&((*i)->name().c_str()[9]));
2661 if (pc >= 0 && pc <= (long) get_count()) {
2662 _in_map[pc] = ChanMapping (**i);
2666 if (strncmp ((*i)->name ().c_str(), X_("OutputMap-"), 10) == 0) {
2667 long pc = atol (&((*i)->name().c_str()[10]));
2668 if (pc >= 0 && pc <= (long) get_count()) {
2669 _out_map[pc] = ChanMapping (**i);
2673 if ((*i)->name () == "ThruMap") {
2674 _thru_map = ChanMapping (**i);
2677 // sidechain is a Processor (IO)
2678 if ((*i)->name () == Processor::state_node_name) {
2682 if (!regenerate_xml_or_string_ids ()) {
2683 _sidechain->set_state (**i, version);
2688 if (in_maps == out_maps && out_maps >0 && out_maps == get_count()) {
2689 _maps_from_state = true;
2692 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
2696 (*i)->deactivate ();
2700 PluginConfigChanged (); /* EMIT SIGNAL */
2705 PluginInsert::update_id (PBD::ID id)
2708 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
2709 (*i)->set_insert_id (id);
2714 PluginInsert::set_owner (SessionObject* o)
2716 Processor::set_owner (o);
2717 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
2718 (*i)->set_owner (o);
2723 PluginInsert::set_state_dir (const std::string& d)
2725 // state() only saves the state of the first plugin
2726 _plugins[0]->set_state_dir (d);
2730 PluginInsert::set_parameter_state_2X (const XMLNode& node, int version)
2732 XMLNodeList nlist = node.children();
2733 XMLNodeIterator niter;
2735 /* look for port automation node */
2737 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2739 if ((*niter)->name() != port_automation_node_name) {
2744 XMLNodeConstIterator iter;
2748 cnodes = (*niter)->children ("port");
2750 for (iter = cnodes.begin(); iter != cnodes.end(); ++iter){
2754 if (!child->get_property("number", port_id)) {
2755 warning << _("PluginInsert: Auto: no ladspa port number") << endmsg;
2759 if (port_id >= _plugins[0]->parameter_count()) {
2760 warning << _("PluginInsert: Auto: port id out of range") << endmsg;
2764 boost::shared_ptr<AutomationControl> c = boost::dynamic_pointer_cast<AutomationControl>(
2765 control(Evoral::Parameter(PluginAutomation, 0, port_id), true));
2767 if (c && c->alist()) {
2768 if (!child->children().empty()) {
2769 c->alist()->set_state (*child->children().front(), version);
2771 /* In some cases 2.X saves lists with min_yval and max_yval
2772 being FLT_MIN and FLT_MAX respectively. This causes problems
2773 in A3 because these min/max values are used to compute
2774 where GUI control points should be drawn. If we see such
2775 values, `correct' them to the min/max of the appropriate
2779 float min_y = c->alist()->get_min_y ();
2780 float max_y = c->alist()->get_max_y ();
2782 ParameterDescriptor desc;
2783 _plugins.front()->get_parameter_descriptor (port_id, desc);
2785 if (min_y == FLT_MIN) {
2789 if (max_y == FLT_MAX) {
2793 c->alist()->set_yrange (min_y, max_y);
2796 error << string_compose (_("PluginInsert: automatable control %1 not found - ignored"), port_id) << endmsg;
2806 boost::shared_ptr<ReadOnlyControl>
2807 PluginInsert::control_output (uint32_t num) const
2809 CtrlOutMap::const_iterator i = _control_outputs.find (num);
2810 if (i == _control_outputs.end ()) {
2811 return boost::shared_ptr<ReadOnlyControl> ();
2818 PluginInsert::describe_parameter (Evoral::Parameter param)
2820 if (param.type() == PluginAutomation) {
2821 return _plugins[0]->describe_parameter (param);
2822 } else if (param.type() == PluginPropertyAutomation) {
2823 boost::shared_ptr<AutomationControl> c(automation_control(param));
2824 if (c && !c->desc().label.empty()) {
2825 return c->desc().label;
2828 return Automatable::describe_parameter(param);
2832 PluginInsert::signal_latency() const
2834 if (!_pending_active) {
2837 if (_user_latency) {
2838 return _user_latency;
2841 return _plugins[0]->signal_latency ();
2845 PluginInsert::type ()
2847 return plugin()->get_info()->type;
2850 PluginInsert::PluginControl::PluginControl (PluginInsert* p,
2851 const Evoral::Parameter& param,
2852 const ParameterDescriptor& desc,
2853 boost::shared_ptr<AutomationList> list)
2854 : AutomationControl (p->session(), param, desc, list, p->describe_parameter(param))
2858 alist()->reset_default (desc.normal);
2860 list->set_interpolation(Evoral::ControlList::Discrete);
2865 /** @param val `user' value */
2868 PluginInsert::PluginControl::actually_set_value (double user_val, PBD::Controllable::GroupControlDisposition group_override)
2870 /* FIXME: probably should be taking out some lock here.. */
2872 for (Plugins::iterator i = _plugin->_plugins.begin(); i != _plugin->_plugins.end(); ++i) {
2873 (*i)->set_parameter (_list->parameter().id(), user_val);
2876 boost::shared_ptr<Plugin> iasp = _plugin->_impulseAnalysisPlugin.lock();
2878 iasp->set_parameter (_list->parameter().id(), user_val);
2881 AutomationControl::actually_set_value (user_val, group_override);
2885 PluginInsert::PluginControl::catch_up_with_external_value (double user_val)
2887 AutomationControl::actually_set_value (user_val, Controllable::NoGroup);
2891 PluginInsert::PluginControl::get_state ()
2893 XMLNode& node (AutomationControl::get_state());
2894 node.set_property (X_("parameter"), parameter().id());
2896 boost::shared_ptr<LV2Plugin> lv2plugin = boost::dynamic_pointer_cast<LV2Plugin> (_plugin->_plugins[0]);
2898 node.set_property (X_("symbol"), lv2plugin->port_symbol (parameter().id()));
2905 /** @return `user' val */
2907 PluginInsert::PluginControl::get_value () const
2909 boost::shared_ptr<Plugin> plugin = _plugin->plugin (0);
2915 return plugin->get_parameter (_list->parameter().id());
2918 PluginInsert::PluginPropertyControl::PluginPropertyControl (PluginInsert* p,
2919 const Evoral::Parameter& param,
2920 const ParameterDescriptor& desc,
2921 boost::shared_ptr<AutomationList> list)
2922 : AutomationControl (p->session(), param, desc, list)
2926 alist()->set_yrange (desc.lower, desc.upper);
2927 alist()->reset_default (desc.normal);
2932 PluginInsert::PluginPropertyControl::actually_set_value (double user_val, Controllable::GroupControlDisposition gcd)
2934 /* Old numeric set_value(), coerce to appropriate datatype if possible.
2935 This is lossy, but better than nothing until Ardour's automation system
2936 can handle various datatypes all the way down. */
2937 const Variant value(_desc.datatype, user_val);
2938 if (value.type() == Variant::NOTHING) {
2939 error << "set_value(double) called for non-numeric property" << endmsg;
2943 for (Plugins::iterator i = _plugin->_plugins.begin(); i != _plugin->_plugins.end(); ++i) {
2944 (*i)->set_property(_list->parameter().id(), value);
2949 AutomationControl::actually_set_value (user_val, gcd);
2953 PluginInsert::PluginPropertyControl::get_state ()
2955 XMLNode& node (AutomationControl::get_state());
2956 node.set_property (X_("property"), parameter().id());
2957 node.remove_property (X_("value"));
2963 PluginInsert::PluginPropertyControl::get_value () const
2965 return _value.to_double();
2968 boost::shared_ptr<Plugin>
2969 PluginInsert::get_impulse_analysis_plugin()
2971 boost::shared_ptr<Plugin> ret;
2972 if (_impulseAnalysisPlugin.expired()) {
2973 // LV2 in particular uses various _session params
2974 // during init() -- most notably block_size..
2976 ret = plugin_factory(_plugins[0]);
2977 ChanCount out (internal_output_streams ());
2978 if (ret->get_info ()->reconfigurable_io ()) {
2979 // populate get_info ()->n_inputs and ->n_outputs
2981 ret->can_support_io_configuration (internal_input_streams (), out, &useins);
2982 assert (out == internal_output_streams ());
2984 ret->configure_io (internal_input_streams (), out);
2985 ret->set_owner (_owner);
2986 _impulseAnalysisPlugin = ret;
2988 ret = _impulseAnalysisPlugin.lock();
2995 PluginInsert::collect_signal_for_analysis (framecnt_t nframes)
2997 // called from outside the audio thread, so this should be safe
2998 // only do audio as analysis is (currently) only for audio plugins
2999 _signal_analysis_inputs.ensure_buffers (DataType::AUDIO, input_streams().n_audio(), nframes);
3000 _signal_analysis_outputs.ensure_buffers (DataType::AUDIO, output_streams().n_audio(), nframes);
3002 _signal_analysis_collected_nframes = 0;
3003 _signal_analysis_collect_nframes_max = nframes;
3006 /** Add a plugin to our list */
3008 PluginInsert::add_plugin (boost::shared_ptr<Plugin> plugin)
3010 plugin->set_insert_id (this->id());
3011 plugin->set_owner (_owner);
3013 if (_plugins.empty()) {
3014 /* first (and probably only) plugin instance - connect to relevant signals */
3016 plugin->ParameterChangedExternally.connect_same_thread (*this, boost::bind (&PluginInsert::parameter_changed_externally, this, _1, _2));
3017 plugin->StartTouch.connect_same_thread (*this, boost::bind (&PluginInsert::start_touch, this, _1));
3018 plugin->EndTouch.connect_same_thread (*this, boost::bind (&PluginInsert::end_touch, this, _1));
3019 _custom_sinks = plugin->get_info()->n_inputs;
3020 // cache sidechain port count
3021 _cached_sidechain_pins.reset ();
3022 const ChanCount& nis (plugin->get_info()->n_inputs);
3023 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
3024 for (uint32_t in = 0; in < nis.get (*t); ++in) {
3025 const Plugin::IOPortDescription& iod (plugin->describe_io_port (*t, true, in));
3026 if (iod.is_sidechain) {
3027 _cached_sidechain_pins.set (*t, 1 + _cached_sidechain_pins.n(*t));
3032 #if (defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT || defined MACVST_SUPPORT)
3033 boost::shared_ptr<VSTPlugin> vst = boost::dynamic_pointer_cast<VSTPlugin> (plugin);
3035 vst->set_insert (this, _plugins.size ());
3039 _plugins.push_back (plugin);
3043 PluginInsert::load_preset (ARDOUR::Plugin::PresetRecord pr)
3046 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
3047 if (! (*i)->load_preset (pr)) {
3055 PluginInsert::realtime_handle_transport_stopped ()
3057 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
3058 (*i)->realtime_handle_transport_stopped ();
3063 PluginInsert::realtime_locate ()
3065 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
3066 (*i)->realtime_locate ();
3071 PluginInsert::monitoring_changed ()
3073 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
3074 (*i)->monitoring_changed ();
3079 PluginInsert::latency_changed ()
3081 // this is called in RT context, LatencyChanged is emitted after run()
3082 _latency_changed = true;
3083 // XXX This also needs a proper API not an owner() hack.
3085 static_cast<Route*>(owner ())->processor_latency_changed (); /* EMIT SIGNAL */
3089 PluginInsert::start_touch (uint32_t param_id)
3091 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter (PluginAutomation, 0, param_id));
3093 // ToDo subtract _plugin_signal_latency from audible_frame() when rolling, assert > 0
3094 ac->start_touch (session().audible_frame());
3099 PluginInsert::end_touch (uint32_t param_id)
3101 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter (PluginAutomation, 0, param_id));
3103 // ToDo subtract _plugin_signal_latency from audible_frame() when rolling, assert > 0
3104 ac->stop_touch (true, session().audible_frame());
3108 std::ostream& operator<<(std::ostream& o, const ARDOUR::PluginInsert::Match& m)
3111 case PluginInsert::Impossible: o << "Impossible"; break;
3112 case PluginInsert::Delegate: o << "Delegate"; break;
3113 case PluginInsert::NoInputs: o << "NoInputs"; break;
3114 case PluginInsert::ExactMatch: o << "ExactMatch"; break;
3115 case PluginInsert::Replicate: o << "Replicate"; break;
3116 case PluginInsert::Split: o << "Split"; break;
3117 case PluginInsert::Hide: o << "Hide"; break;
3119 o << " cnt: " << m.plugins
3120 << (m.strict_io ? " strict-io" : "")
3121 << (m.custom_cfg ? " custom-cfg" : "");
3122 if (m.method == PluginInsert::Hide) {
3123 o << " hide: " << m.hide;