2 Copyright (C) 2000 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "libardour-config.h"
26 #include "pbd/failed_constructor.h"
27 #include "pbd/xml++.h"
28 #include "pbd/convert.h"
30 #include "ardour/audio_buffer.h"
31 #include "ardour/automation_list.h"
32 #include "ardour/buffer_set.h"
33 #include "ardour/debug.h"
34 #include "ardour/event_type_map.h"
35 #include "ardour/ladspa_plugin.h"
36 #include "ardour/luaproc.h"
37 #include "ardour/plugin.h"
38 #include "ardour/plugin_insert.h"
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"
53 #ifdef AUDIOUNIT_SUPPORT
54 #include "ardour/audio_unit.h"
57 #include "ardour/session.h"
58 #include "ardour/types.h"
63 using namespace ARDOUR;
66 const string PluginInsert::port_automation_node_name = "PortAutomation";
68 PluginInsert::PluginInsert (Session& s, boost::shared_ptr<Plugin> plug)
69 : Processor (s, (plug ? plug->name() : string ("toBeRenamed")))
70 , _sc_playback_latency (0)
71 , _sc_capture_latency (0)
72 , _plugin_signal_latency (0)
73 , _signal_analysis_collected_nframes(0)
74 , _signal_analysis_collect_nframes_max(0)
79 , _maps_from_state (false)
80 , _bypass_port (UINT32_MAX)
82 /* the first is the master */
86 create_automatable_parameters ();
87 const ChanCount& sc (sidechain_input_pins ());
88 if (sc.n_audio () > 0 || sc.n_midi () > 0) {
89 add_sidechain (sc.n_audio (), sc.n_midi ());
94 PluginInsert::~PluginInsert ()
99 PluginInsert::set_strict_io (bool b)
101 bool changed = _strict_io != b;
104 PluginConfigChanged (); /* EMIT SIGNAL */
109 PluginInsert::set_count (uint32_t num)
111 bool require_state = !_plugins.empty();
113 if (require_state && num > 1 && plugin (0)->get_info ()->type == ARDOUR::AudioUnit) {
114 // we don't allow to replicate AUs
118 /* this is a bad idea.... we shouldn't do this while active.
119 * only a route holding their redirect_lock should be calling this
124 } else if (num > _plugins.size()) {
125 uint32_t diff = num - _plugins.size();
127 for (uint32_t n = 0; n < diff; ++n) {
128 boost::shared_ptr<Plugin> p = plugin_factory (_plugins[0]);
132 XMLNode& state = _plugins[0]->get_state ();
133 p->set_state (state, Stateful::loading_state_version);
140 PluginConfigChanged (); /* EMIT SIGNAL */
142 } else if (num < _plugins.size()) {
143 uint32_t diff = _plugins.size() - num;
144 for (uint32_t n= 0; n < diff; ++n) {
147 PluginConfigChanged (); /* EMIT SIGNAL */
155 PluginInsert::set_sinks (const ChanCount& c)
158 /* no signal, change will only be visible after re-config */
162 PluginInsert::set_outputs (const ChanCount& c)
164 bool changed = (_custom_out != c) && _custom_cfg;
167 PluginConfigChanged (); /* EMIT SIGNAL */
172 PluginInsert::set_custom_cfg (bool b)
174 bool changed = _custom_cfg != b;
177 PluginConfigChanged (); /* EMIT SIGNAL */
182 PluginInsert::set_preset_out (const ChanCount& c)
184 bool changed = _preset_out != c;
186 if (changed && !_custom_cfg) {
187 PluginConfigChanged (); /* EMIT SIGNAL */
193 PluginInsert::add_sidechain (uint32_t n_audio, uint32_t n_midi)
195 // caller must hold process lock
199 std::ostringstream n;
200 if (n_audio > 0 || n_midi > 0) {
201 n << "Sidechain " << Session::next_name_id ();
203 n << "TO BE RESET FROM XML";
205 SideChain *sc = new SideChain (_session, n.str ());
206 _sidechain = boost::shared_ptr<SideChain> (sc);
207 _sidechain->activate ();
208 for (uint32_t n = 0; n < n_audio; ++n) {
209 _sidechain->input()->add_port ("", owner(), DataType::AUDIO); // add a port, don't connect.
211 for (uint32_t n = 0; n < n_midi; ++n) {
212 _sidechain->input()->add_port ("", owner(), DataType::MIDI); // add a port, don't connect.
214 PluginConfigChanged (); /* EMIT SIGNAL */
219 PluginInsert::del_sidechain ()
225 _sc_playback_latency = 0;
226 _sc_capture_latency = 0;
227 PluginConfigChanged (); /* EMIT SIGNAL */
232 PluginInsert::set_sidechain_latency (uint32_t capture, uint32_t playback)
235 (_sc_playback_latency != playback || _sc_capture_latency != capture)) {
236 _sc_capture_latency = capture;
237 _sc_playback_latency = playback;
238 LatencyRange pl; pl.min = pl.max = playback;
239 LatencyRange cl; cl.min = cl.max = capture;
240 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: capture %2 playback; %3\n", _sidechain->name (), capture, playback));
241 PortSet& ps (_sidechain->input ()->ports ());
242 for (PortSet::iterator p = ps.begin(); p != ps.end(); ++p) {
243 p->set_private_latency_range (pl, true);
244 p->set_private_latency_range (cl, false);
250 PluginInsert::control_list_automation_state_changed (Evoral::Parameter which, AutoState s)
252 if (which.type() != PluginAutomation)
255 boost::shared_ptr<AutomationControl> c
256 = boost::dynamic_pointer_cast<AutomationControl>(control (which));
259 _plugins[0]->set_parameter (which.id(), c->list()->eval (_session.transport_frame()));
264 PluginInsert::output_streams() const
266 assert (_configured);
267 return _configured_out;
271 PluginInsert::input_streams() const
273 assert (_configured);
274 return _configured_in;
278 PluginInsert::internal_streams() const
280 assert (_configured);
281 return _configured_internal;
285 PluginInsert::internal_output_streams() const
287 assert (!_plugins.empty());
289 PluginInfoPtr info = _plugins.front()->get_info();
291 if (info->reconfigurable_io()) {
292 ChanCount out = _plugins.front()->output_streams ();
293 // DEBUG_TRACE (DEBUG::Processors, string_compose ("Plugin insert, reconfigur(able) output streams = %1\n", out));
296 ChanCount out = info->n_outputs;
297 // DEBUG_TRACE (DEBUG::Processors, string_compose ("Plugin insert, static output streams = %1 for %2 plugins\n", out, _plugins.size()));
298 out.set_audio (out.n_audio() * _plugins.size());
299 out.set_midi (out.n_midi() * _plugins.size());
305 PluginInsert::internal_input_streams() const
307 assert (!_plugins.empty());
311 PluginInfoPtr info = _plugins.front()->get_info();
313 if (info->reconfigurable_io()) {
314 in = _plugins.front()->input_streams();
319 DEBUG_TRACE (DEBUG::Processors, string_compose ("Plugin insert, input streams = %1, match using %2\n", in, _match.method));
321 if (_match.method == Split) {
323 /* we are splitting 1 processor input to multiple plugin inputs,
324 so we have a maximum of 1 stream of each type.
326 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
327 if (in.get (*t) > 1) {
333 } else if (_match.method == Hide) {
335 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
336 in.set (*t, in.get (*t) - _match.hide.get (*t));
342 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
343 in.set (*t, in.get (*t) * _plugins.size ());
351 PluginInsert::natural_output_streams() const
354 if (is_channelstrip ()) {
355 return ChanCount::min (_configured_out, ChanCount (DataType::AUDIO, 2));
358 return _plugins[0]->get_info()->n_outputs;
362 PluginInsert::natural_input_streams() const
365 if (is_channelstrip ()) {
366 return ChanCount::min (_configured_in, ChanCount (DataType::AUDIO, 2));
369 return _plugins[0]->get_info()->n_inputs;
373 PluginInsert::sidechain_input_pins() const
375 return _cached_sidechain_pins;
379 PluginInsert::has_no_inputs() const
381 return _plugins[0]->get_info()->n_inputs == ChanCount::ZERO;
385 PluginInsert::has_no_audio_inputs() const
387 return _plugins[0]->get_info()->n_inputs.n_audio() == 0;
391 PluginInsert::plugin_latency () const {
392 return _plugins.front()->signal_latency ();
396 PluginInsert::needs_midi_input() const
398 PluginInfoPtr pip = _plugins[0]->get_info();
399 if (pip->needs_midi_input ()) {
402 return pip->n_inputs.n_midi() != 0 && pip->n_outputs.n_audio() != 0;
406 PluginInsert::has_output_presets (ChanCount in, ChanCount out)
408 if (!_configured && _plugins[0]->get_info ()->reconfigurable_io ()) {
409 // collect possible configurations, prefer given in/out
410 _plugins[0]->can_support_io_configuration (in, out);
413 PluginOutputConfiguration ppc (_plugins[0]->possible_output ());
415 if (ppc.size () == 0) {
418 if (!strict_io () && ppc.size () == 1) {
422 if (strict_io () && ppc.size () == 1) {
423 // "stereo" is currently preferred default for instruments
424 if (ppc.find (2) != ppc.end ()) {
428 if (!needs_midi_input ()) {
435 PluginInsert::create_automatable_parameters ()
437 assert (!_plugins.empty());
439 boost::shared_ptr<Plugin> plugin = _plugins.front();
440 set<Evoral::Parameter> a = _plugins.front()->automatable ();
442 for (uint32_t i = 0; i < plugin->parameter_count(); ++i) {
443 if (!plugin->parameter_is_control (i) || !plugin->parameter_is_input (i)) {
446 Evoral::Parameter param (PluginAutomation, 0, i);
448 ParameterDescriptor desc;
449 plugin->get_parameter_descriptor(i, desc);
451 const bool automatable = a.find(param) != a.end();
454 can_automate (param);
456 boost::shared_ptr<AutomationList> list(new AutomationList(param, desc));
457 boost::shared_ptr<AutomationControl> c (new PluginControl(this, param, desc, list));
459 c->set_flags (Controllable::Flag ((int)c->flags() | Controllable::NotAutomatable));
462 plugin->set_automation_control (i, c);
466 const Plugin::PropertyDescriptors& pdl (plugin->get_supported_properties ());
467 for (Plugin::PropertyDescriptors::const_iterator p = pdl.begin(); p != pdl.end(); ++p) {
468 Evoral::Parameter param (PluginPropertyAutomation, 0, p->first);
469 const ParameterDescriptor& desc = plugin->get_property_descriptor(param.id());
470 if (desc.datatype != Variant::NOTHING) {
471 boost::shared_ptr<AutomationList> list;
472 if (Variant::type_is_numeric(desc.datatype)) {
473 list = boost::shared_ptr<AutomationList>(new AutomationList(param, desc));
475 add_control (boost::shared_ptr<AutomationControl> (new PluginPropertyControl(this, param, desc, list)));
479 _bypass_port = plugin->designated_bypass_port ();
481 if (_bypass_port != UINT32_MAX) {
482 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter (PluginAutomation, 0, _bypass_port));
483 if (0 == (ac->flags () & Controllable::NotAutomatable)) {
484 ac->alist()->automation_state_changed.connect_same_thread (*this, boost::bind (&PluginInsert::bypassable_changed, this));
485 ac->Changed.connect_same_thread (*this, boost::bind (&PluginInsert::enable_changed, this));
489 /** Called when something outside of this host has modified a plugin
490 * parameter. Responsible for propagating the change to two places:
492 * 1) anything listening to the Control itself
493 * 2) any replicated plugins that make up this PluginInsert.
495 * The PluginInsert is connected to the ParameterChangedExternally signal for
496 * the first (primary) plugin, and here broadcasts that change to any others.
498 * XXX We should probably drop this whole replication idea (Paul, October 2015)
499 * since it isn't used by sensible plugin APIs (AU, LV2).
502 PluginInsert::parameter_changed_externally (uint32_t which, float val)
504 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter (PluginAutomation, 0, which));
506 /* First propagation: alter the underlying value of the control,
507 * without telling the plugin(s) that own/use it to set it.
514 boost::shared_ptr<PluginControl> pc = boost::dynamic_pointer_cast<PluginControl> (ac);
517 pc->catch_up_with_external_value (val);
520 /* Second propagation: tell all plugins except the first to
521 update the value of this parameter. For sane plugin APIs,
522 there are no other plugins, so this is a no-op in those
526 Plugins::iterator i = _plugins.begin();
528 /* don't set the first plugin, just all the slaves */
530 if (i != _plugins.end()) {
532 for (; i != _plugins.end(); ++i) {
533 (*i)->set_parameter (which, val);
539 PluginInsert::set_block_size (pframes_t nframes)
542 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
543 if ((*i)->set_block_size (nframes) != 0) {
551 PluginInsert::activate ()
553 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
557 Processor::activate ();
558 /* when setting state e.g ProcessorBox::paste_processor_state ()
559 * the plugin is not yet owned by a route.
560 * but no matter. Route::add_processors() will call activate () again
565 if (_plugin_signal_latency != signal_latency ()) {
566 _plugin_signal_latency = signal_latency ();
572 PluginInsert::deactivate ()
574 Processor::deactivate ();
576 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
579 if (_plugin_signal_latency != signal_latency ()) {
580 _plugin_signal_latency = signal_latency ();
586 PluginInsert::flush ()
588 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
594 PluginInsert::enable (bool yn)
596 if (_bypass_port == UINT32_MAX) {
603 if (!_pending_active) {
606 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter (PluginAutomation, 0, _bypass_port));
607 ac->set_value (yn ? 1.0 : 0.0, Controllable::NoGroup);
613 PluginInsert::enabled () const
615 if (_bypass_port == UINT32_MAX) {
616 return Processor::enabled ();
618 boost::shared_ptr<const AutomationControl> ac = boost::const_pointer_cast<AutomationControl> (automation_control (Evoral::Parameter (PluginAutomation, 0, _bypass_port)));
619 return (ac->get_value () > 0 && _pending_active);
624 PluginInsert::bypassable () const
626 if (_bypass_port == UINT32_MAX) {
629 boost::shared_ptr<const AutomationControl> ac = boost::const_pointer_cast<AutomationControl> (automation_control (Evoral::Parameter (PluginAutomation, 0, _bypass_port)));
631 return !ac->automation_playback ();
636 PluginInsert::enable_changed ()
642 PluginInsert::bypassable_changed ()
644 BypassableChanged ();
648 PluginInsert::inplace_silence_unconnected (BufferSet& bufs, const PinMappings& out_map, framecnt_t nframes, framecnt_t offset) const
650 // TODO optimize: store "unconnected" in a fixed set.
651 // it only changes on reconfiguration.
652 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
653 for (uint32_t out = 0; out < bufs.count().get (*t); ++out) {
655 if (*t == DataType::MIDI && out == 0 && has_midi_bypass ()) {
656 mapped = true; // in-place Midi bypass
658 for (uint32_t pc = 0; pc < get_count() && !mapped; ++pc) {
659 PinMappings::const_iterator i = out_map.find (pc);
660 if (i == out_map.end ()) {
663 const ChanMapping& outmap (i->second);
664 for (uint32_t o = 0; o < natural_output_streams().get (*t); ++o) {
666 uint32_t idx = outmap.get (*t, o, &valid);
667 if (valid && idx == out) {
674 bufs.get (*t, out).silence (nframes, offset);
681 PluginInsert::connect_and_run (BufferSet& bufs, framepos_t start, framepos_t end, double speed, pframes_t nframes, framecnt_t offset, bool with_auto)
683 // TODO: atomically copy maps & _no_inplace
684 PinMappings in_map (_in_map);
685 PinMappings out_map (_out_map);
686 ChanMapping thru_map (_thru_map);
687 if (_mapping_changed) { // ToDo use a counters, increment until match.
688 _no_inplace = check_inplace ();
689 _mapping_changed = false;
692 if (_latency_changed) {
693 /* delaylines are configured with the max possible latency (as reported by the plugin)
694 * so this won't allocate memory (unless the plugin lied about its max latency)
695 * It may still 'click' though, since the fixed delaylines are not de-clicked.
696 * Then again plugin-latency changes are not click-free to begin with.
698 * This is also worst case, there is currently no concept of per-stream latency.
700 * e.g. Two identical latent plugins:
701 * 1st plugin: process left (latent), bypass right.
702 * 2nd plugin: bypass left, process right (latent).
703 * -> currently this yields 2 times latency of the plugin,
705 _latency_changed = false;
706 _delaybuffers.set (ChanCount::max(bufs.count(), _configured_out), plugin_latency ());
709 if (_match.method == Split && !_no_inplace) {
710 // TODO: also use this optimization if one source-buffer
711 // feeds _all_ *connected* inputs.
712 // currently this is *first* buffer to all only --
713 // see PluginInsert::check_inplace
714 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
715 if (_configured_internal.get (*t) == 0) {
719 uint32_t first_idx = in_map[0].get (*t, 0, &valid);
720 assert (valid && first_idx == 0); // check_inplace ensures this
721 /* copy the first stream's buffer contents to the others */
722 for (uint32_t i = 1; i < natural_input_streams ().get (*t); ++i) {
723 uint32_t idx = in_map[0].get (*t, i, &valid);
726 bufs.get (*t, i).read_from (bufs.get (*t, first_idx), nframes, offset, offset);
730 /* the copy operation produces a linear monotonic input map */
731 in_map[0] = ChanMapping (natural_input_streams ());
734 bufs.set_count(ChanCount::max(bufs.count(), _configured_internal));
735 bufs.set_count(ChanCount::max(bufs.count(), _configured_out));
741 for (Controls::iterator li = controls().begin(); li != controls().end(); ++li, ++n) {
743 boost::shared_ptr<AutomationControl> c
744 = boost::dynamic_pointer_cast<AutomationControl>(li->second);
746 if (c->list() && c->automation_playback()) {
749 const float val = c->list()->rt_safe_eval (start, valid);
752 /* This is the ONLY place where we are
754 * AutomationControl::set_value_unchecked(). We
755 * know that the control is in
756 * automation playback mode, so no
757 * check on writable() is required
758 * (which must be done in AutomationControl::set_value()
761 c->set_value_unchecked(val);
768 /* Calculate if, and how many frames we need to collect for analysis */
769 framecnt_t collect_signal_nframes = (_signal_analysis_collect_nframes_max -
770 _signal_analysis_collected_nframes);
771 if (nframes < collect_signal_nframes) { // we might not get all frames now
772 collect_signal_nframes = nframes;
775 if (collect_signal_nframes > 0) {
777 //std::cerr << "collect input, bufs " << bufs.count().n_audio() << " count, " << bufs.available().n_audio() << " available" << std::endl;
778 //std::cerr << " streams " << internal_input_streams().n_audio() << std::endl;
779 //std::cerr << "filling buffer with " << collect_signal_nframes << " frames at " << _signal_analysis_collected_nframes << std::endl;
781 _signal_analysis_inputs.set_count(input_streams());
783 for (uint32_t i = 0; i < input_streams().n_audio(); ++i) {
784 _signal_analysis_inputs.get_audio(i).read_from (
786 collect_signal_nframes,
787 _signal_analysis_collected_nframes); // offset is for target buffer
792 if (is_channelstrip ()) {
793 if (_configured_in.n_audio() > 0) {
794 ChanMapping mb_in_map (ChanCount::min (_configured_in, ChanCount (DataType::AUDIO, 2)));
795 ChanMapping mb_out_map (ChanCount::min (_configured_out, ChanCount (DataType::AUDIO, 2)));
797 _plugins.front()->connect_and_run (bufs, start, end, speed, mb_in_map, mb_out_map, nframes, offset);
799 for (uint32_t out = _configured_in.n_audio (); out < bufs.count().get (DataType::AUDIO); ++out) {
800 bufs.get (DataType::AUDIO, out).silence (nframes, offset);
806 // TODO optimize -- build maps once.
808 BufferSet& inplace_bufs = _session.get_noinplace_buffers();
809 ARDOUR::ChanMapping used_outputs;
811 assert (inplace_bufs.count () >= natural_input_streams () + _configured_out);
813 /* build used-output map */
814 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i, ++pc) {
815 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
816 for (uint32_t out = 0; out < natural_output_streams().get (*t); ++out) {
818 uint32_t out_idx = out_map[pc].get (*t, out, &valid);
820 used_outputs.set (*t, out_idx, 1); // mark as used
825 /* copy thru data to outputs before processing in-place */
826 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
827 for (uint32_t out = 0; out < bufs.count().get (*t); ++out) {
829 uint32_t in_idx = thru_map.get (*t, out, &valid);
830 uint32_t m = out + natural_input_streams ().get (*t);
832 _delaybuffers.delay (*t, out, inplace_bufs.get (*t, m), bufs.get (*t, in_idx), nframes, offset, offset);
833 used_outputs.set (*t, out, 1); // mark as used
835 used_outputs.get (*t, out, &valid);
837 /* the plugin is expected to write here, but may not :(
838 * (e.g. drumgizmo w/o kit loaded)
840 inplace_bufs.get (*t, m).silence (nframes);
847 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i, ++pc) {
849 ARDOUR::ChanMapping i_in_map (natural_input_streams());
850 ARDOUR::ChanMapping i_out_map (out_map[pc]);
851 ARDOUR::ChanCount mapped;
853 /* map inputs sequentially */
854 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
855 for (uint32_t in = 0; in < natural_input_streams().get (*t); ++in) {
857 uint32_t in_idx = in_map[pc].get (*t, in, &valid);
858 uint32_t m = mapped.get (*t);
860 inplace_bufs.get (*t, m).read_from (bufs.get (*t, in_idx), nframes, offset, offset);
862 inplace_bufs.get (*t, m).silence (nframes, offset);
864 mapped.set (*t, m + 1);
868 /* outputs are mapped to inplace_bufs after the inputs */
869 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
870 i_out_map.offset_to (*t, natural_input_streams ().get (*t));
873 if ((*i)->connect_and_run (inplace_bufs, start, end, speed, i_in_map, i_out_map, nframes, offset)) {
878 /* all instances have completed, now copy data that was written
879 * and zero unconnected buffers */
880 ARDOUR::ChanMapping nonzero_out (used_outputs);
881 if (has_midi_bypass ()) {
882 nonzero_out.set (DataType::MIDI, 0, 1); // Midi bypass.
884 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
885 for (uint32_t out = 0; out < bufs.count().get (*t); ++out) {
887 used_outputs.get (*t, out, &valid);
889 nonzero_out.get (*t, out, &valid);
891 bufs.get (*t, out).silence (nframes, offset);
894 uint32_t m = out + natural_input_streams ().get (*t);
895 bufs.get (*t, out).read_from (inplace_bufs.get (*t, m), nframes, offset, offset);
900 /* in-place processing */
902 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i, ++pc) {
903 if ((*i)->connect_and_run(bufs, start, end, speed, in_map[pc], out_map[pc], nframes, offset)) {
907 // now silence unconnected outputs
908 inplace_silence_unconnected (bufs, _out_map, nframes, offset);
911 if (collect_signal_nframes > 0) {
913 //std::cerr << " output, bufs " << bufs.count().n_audio() << " count, " << bufs.available().n_audio() << " available" << std::endl;
914 //std::cerr << " streams " << internal_output_streams().n_audio() << std::endl;
916 _signal_analysis_outputs.set_count(output_streams());
918 for (uint32_t i = 0; i < output_streams().n_audio(); ++i) {
919 _signal_analysis_outputs.get_audio(i).read_from(
921 collect_signal_nframes,
922 _signal_analysis_collected_nframes); // offset is for target buffer
925 _signal_analysis_collected_nframes += collect_signal_nframes;
926 assert(_signal_analysis_collected_nframes <= _signal_analysis_collect_nframes_max);
928 if (_signal_analysis_collected_nframes == _signal_analysis_collect_nframes_max) {
929 _signal_analysis_collect_nframes_max = 0;
930 _signal_analysis_collected_nframes = 0;
932 AnalysisDataGathered(&_signal_analysis_inputs,
933 &_signal_analysis_outputs);
937 if (_plugin_signal_latency != signal_latency ()) {
938 _plugin_signal_latency = signal_latency ();
944 PluginInsert::bypass (BufferSet& bufs, pframes_t nframes)
946 /* bypass the plugin(s) not the whole processor.
947 * -> use mappings just like connect_and_run
950 // TODO: atomically copy maps & _no_inplace
951 const ChanMapping in_map (no_sc_input_map ());
952 const ChanMapping out_map (output_map ());
953 if (_mapping_changed) {
954 _no_inplace = check_inplace ();
955 _mapping_changed = false;
958 bufs.set_count(ChanCount::max(bufs.count(), _configured_internal));
959 bufs.set_count(ChanCount::max(bufs.count(), _configured_out));
962 ChanMapping thru_map (_thru_map);
964 BufferSet& inplace_bufs = _session.get_noinplace_buffers();
966 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
967 for (uint32_t in = 0; in < _configured_internal.get (*t); ++in) {
968 inplace_bufs.get (*t, in).read_from (bufs.get (*t, in), nframes, 0, 0);
971 ARDOUR::ChanMapping used_outputs;
973 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
974 for (uint32_t out = 0; out < _configured_out.get (*t); ++out) {
976 uint32_t in_idx = thru_map.get (*t, out, &valid);
978 bufs.get (*t, out).read_from (inplace_bufs.get (*t, in_idx), nframes, 0, 0);
979 used_outputs.set (*t, out, 1); // mark as used
983 // plugin no-op: assume every plugin has an internal identity map
984 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
985 for (uint32_t out = 0; out < _configured_out.get (*t); ++out) {
987 uint32_t src_idx = out_map.get_src (*t, out, &valid);
991 uint32_t in_idx = in_map.get (*t, src_idx, &valid);
995 bufs.get (*t, out).read_from (inplace_bufs.get (*t, in_idx), nframes, 0, 0);
996 used_outputs.set (*t, out, 1); // mark as used
999 // now silence all unused outputs
1000 if (has_midi_bypass ()) {
1001 used_outputs.set (DataType::MIDI, 0, 1); // Midi bypass.
1003 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1004 for (uint32_t out = 0; out < _configured_out.get (*t); ++out) {
1006 used_outputs.get (*t, out, &valid);
1008 bufs.get (*t, out).silence (nframes, 0);
1013 if (_match.method == Split) {
1014 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1015 if (_configured_internal.get (*t) == 0) {
1018 // copy/feeds _all_ *connected* inputs, copy the first buffer
1020 uint32_t first_idx = in_map.get (*t, 0, &valid);
1021 assert (valid && first_idx == 0); // check_inplace ensures this
1022 for (uint32_t i = 1; i < natural_input_streams ().get (*t); ++i) {
1023 uint32_t idx = in_map.get (*t, i, &valid);
1026 bufs.get (*t, i).read_from (bufs.get (*t, first_idx), nframes, 0, 0);
1032 // apply output map and/or monotonic but not identity i/o mappings
1033 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1034 for (uint32_t out = 0; out < _configured_out.get (*t); ++out) {
1036 uint32_t src_idx = out_map.get_src (*t, out, &valid);
1038 bufs.get (*t, out).silence (nframes, 0);
1041 uint32_t in_idx = in_map.get (*t, src_idx, &valid);
1043 bufs.get (*t, out).silence (nframes, 0);
1046 if (in_idx != src_idx) {
1047 bufs.get (*t, out).read_from (bufs.get (*t, in_idx), nframes, 0, 0);
1055 PluginInsert::silence (framecnt_t nframes, framepos_t start_frame)
1061 _delaybuffers.flush ();
1063 ChanMapping in_map (natural_input_streams ());
1064 ChanMapping out_map (natural_output_streams ());
1065 ChanCount maxbuf = ChanCount::max (natural_input_streams (), natural_output_streams());
1067 if (is_channelstrip ()) {
1068 if (_configured_in.n_audio() > 0) {
1069 _plugins.front()->connect_and_run (_session.get_scratch_buffers (maxbuf, true), start_frame, start_frame + nframes, 1.0, in_map, out_map, nframes, 0);
1073 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
1074 (*i)->connect_and_run (_session.get_scratch_buffers (maxbuf, true), start_frame, start_frame + nframes, 1.0, in_map, out_map, nframes, 0);
1079 PluginInsert::run (BufferSet& bufs, framepos_t start_frame, framepos_t end_frame, double speed, pframes_t nframes, bool)
1082 // collect sidechain input for complete cycle (!)
1083 // TODO we need delaylines here for latency compensation
1084 _sidechain->run (bufs, start_frame, end_frame, speed, nframes, true);
1087 if (_pending_active) {
1088 /* run as normal if we are active or moving from inactive to active */
1090 if (_session.transport_rolling() || _session.bounce_processing()) {
1091 automation_run (bufs, start_frame, end_frame, speed, nframes);
1093 Glib::Threads::Mutex::Lock lm (control_lock(), Glib::Threads::TRY_LOCK);
1094 connect_and_run (bufs, start_frame, end_frame, speed, nframes, 0, lm.locked());
1098 bypass (bufs, nframes);
1099 _delaybuffers.flush ();
1102 _active = _pending_active;
1104 /* we have no idea whether the plugin generated silence or not, so mark
1105 * all buffers appropriately.
1110 PluginInsert::automation_run (BufferSet& bufs, framepos_t start, framepos_t end, double speed, pframes_t nframes)
1112 Evoral::ControlEvent next_event (0, 0.0f);
1113 framecnt_t offset = 0;
1115 Glib::Threads::Mutex::Lock lm (control_lock(), Glib::Threads::TRY_LOCK);
1118 connect_and_run (bufs, start, end, speed, nframes, offset, false);
1122 if (!find_next_event (start, end, next_event) || _plugins.front()->requires_fixed_sized_buffers()) {
1124 /* no events have a time within the relevant range */
1126 connect_and_run (bufs, start, end, speed, nframes, offset, true);
1132 framecnt_t cnt = min (((framecnt_t) ceil (next_event.when) - start), (framecnt_t) nframes);
1134 connect_and_run (bufs, start, start + cnt, speed, cnt, offset, true); // XXX (start + cnt) * speed
1140 if (!find_next_event (start, end, next_event)) {
1145 /* cleanup anything that is left to do */
1148 connect_and_run (bufs, start, start + nframes, speed, nframes, offset, true);
1153 PluginInsert::default_parameter_value (const Evoral::Parameter& param)
1155 if (param.type() != PluginAutomation)
1158 if (_plugins.empty()) {
1159 fatal << _("programming error: ") << X_("PluginInsert::default_parameter_value() called with no plugin")
1161 abort(); /*NOTREACHED*/
1164 return _plugins[0]->default_value (param.id());
1169 PluginInsert::can_reset_all_parameters ()
1172 uint32_t params = 0;
1173 for (uint32_t par = 0; par < _plugins[0]->parameter_count(); ++par) {
1175 const uint32_t cid = _plugins[0]->nth_parameter (par, ok);
1177 if (!ok || !_plugins[0]->parameter_is_input(cid)) {
1181 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter(PluginAutomation, 0, cid));
1187 if (ac->automation_state() & Play) {
1192 return all && (params > 0);
1196 PluginInsert::reset_parameters_to_default ()
1200 for (uint32_t par = 0; par < _plugins[0]->parameter_count(); ++par) {
1202 const uint32_t cid = _plugins[0]->nth_parameter (par, ok);
1204 if (!ok || !_plugins[0]->parameter_is_input(cid)) {
1208 const float dflt = _plugins[0]->default_value (cid);
1209 const float curr = _plugins[0]->get_parameter (cid);
1215 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter(PluginAutomation, 0, cid));
1220 if (ac->automation_state() & Play) {
1225 ac->set_value (dflt, Controllable::NoGroup);
1230 boost::shared_ptr<Plugin>
1231 PluginInsert::plugin_factory (boost::shared_ptr<Plugin> other)
1233 boost::shared_ptr<LadspaPlugin> lp;
1234 boost::shared_ptr<LuaProc> lua;
1236 boost::shared_ptr<LV2Plugin> lv2p;
1238 #ifdef WINDOWS_VST_SUPPORT
1239 boost::shared_ptr<WindowsVSTPlugin> vp;
1241 #ifdef LXVST_SUPPORT
1242 boost::shared_ptr<LXVSTPlugin> lxvp;
1244 #ifdef AUDIOUNIT_SUPPORT
1245 boost::shared_ptr<AUPlugin> ap;
1248 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
1249 return boost::shared_ptr<Plugin> (new LadspaPlugin (*lp));
1250 } else if ((lua = boost::dynamic_pointer_cast<LuaProc> (other)) != 0) {
1251 return boost::shared_ptr<Plugin> (new LuaProc (*lua));
1253 } else if ((lv2p = boost::dynamic_pointer_cast<LV2Plugin> (other)) != 0) {
1254 return boost::shared_ptr<Plugin> (new LV2Plugin (*lv2p));
1256 #ifdef WINDOWS_VST_SUPPORT
1257 } else if ((vp = boost::dynamic_pointer_cast<WindowsVSTPlugin> (other)) != 0) {
1258 return boost::shared_ptr<Plugin> (new WindowsVSTPlugin (*vp));
1260 #ifdef LXVST_SUPPORT
1261 } else if ((lxvp = boost::dynamic_pointer_cast<LXVSTPlugin> (other)) != 0) {
1262 return boost::shared_ptr<Plugin> (new LXVSTPlugin (*lxvp));
1264 #ifdef AUDIOUNIT_SUPPORT
1265 } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
1266 return boost::shared_ptr<Plugin> (new AUPlugin (*ap));
1270 fatal << string_compose (_("programming error: %1"),
1271 X_("unknown plugin type in PluginInsert::plugin_factory"))
1273 abort(); /*NOTREACHED*/
1274 return boost::shared_ptr<Plugin> ((Plugin*) 0);
1278 PluginInsert::set_input_map (uint32_t num, ChanMapping m) {
1279 if (num < _in_map.size()) {
1280 bool changed = _in_map[num] != m;
1282 changed |= sanitize_maps ();
1284 PluginMapChanged (); /* EMIT SIGNAL */
1285 _mapping_changed = true;
1286 _session.set_dirty();
1292 PluginInsert::set_output_map (uint32_t num, ChanMapping m) {
1293 if (num < _out_map.size()) {
1294 bool changed = _out_map[num] != m;
1296 changed |= sanitize_maps ();
1298 PluginMapChanged (); /* EMIT SIGNAL */
1299 _mapping_changed = true;
1300 _session.set_dirty();
1306 PluginInsert::set_thru_map (ChanMapping m) {
1307 bool changed = _thru_map != m;
1309 changed |= sanitize_maps ();
1311 PluginMapChanged (); /* EMIT SIGNAL */
1312 _mapping_changed = true;
1313 _session.set_dirty();
1318 PluginInsert::pre_seed (const ChanCount& in, const ChanCount& out,
1319 const ChanMapping& im, const ChanMapping& om, const ChanMapping& tm)
1321 if (_configured) { return false; }
1322 _configured_in = in;
1323 _configured_out = out;
1327 _maps_from_state = in.n_total () > 0 && out.n_total () > 0;
1332 PluginInsert::input_map () const
1336 for (PinMappings::const_iterator i = _in_map.begin (); i != _in_map.end (); ++i, ++pc) {
1337 ChanMapping m (i->second);
1338 const ChanMapping::Mappings& mp ((*i).second.mappings());
1339 for (ChanMapping::Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
1340 for (ChanMapping::TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
1341 rv.set (tm->first, i->first + pc * natural_input_streams().get(tm->first), i->second);
1350 PluginInsert::no_sc_input_map () const
1354 for (PinMappings::const_iterator i = _in_map.begin (); i != _in_map.end (); ++i, ++pc) {
1355 ChanMapping m (i->second);
1356 const ChanMapping::Mappings& mp ((*i).second.mappings());
1357 for (ChanMapping::Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
1358 uint32_t ins = natural_input_streams().get(tm->first) - _cached_sidechain_pins.get(tm->first);
1359 for (ChanMapping::TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
1360 if (i->first < ins) {
1361 rv.set (tm->first, i->first + pc * ins, i->second);
1370 PluginInsert::output_map () const
1374 for (PinMappings::const_iterator i = _out_map.begin (); i != _out_map.end (); ++i, ++pc) {
1375 ChanMapping m (i->second);
1376 const ChanMapping::Mappings& mp ((*i).second.mappings());
1377 for (ChanMapping::Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
1378 for (ChanMapping::TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
1379 rv.set (tm->first, i->first + pc * natural_output_streams().get(tm->first), i->second);
1383 if (has_midi_bypass ()) {
1384 rv.set (DataType::MIDI, 0, 0);
1391 PluginInsert::has_midi_bypass () const
1393 if (_configured_in.n_midi () == 1 && _configured_out.n_midi () == 1
1394 && natural_output_streams ().n_midi () == 0) {
1401 PluginInsert::has_midi_thru () const
1403 if (_configured_in.n_midi () == 1 && _configured_out.n_midi () == 1
1404 && natural_input_streams ().n_midi () == 0 && natural_output_streams ().n_midi () == 0) {
1412 PluginInsert::is_channelstrip () const {
1413 return _plugins.front()->is_channelstrip();
1418 PluginInsert::check_inplace ()
1420 bool inplace_ok = !_plugins.front()->inplace_broken ();
1422 if (_thru_map.n_total () > 0) {
1423 // TODO once midi-bypass is part of the mapping, ignore it
1427 if (_match.method == Split && inplace_ok) {
1428 assert (get_count() == 1);
1429 assert (_in_map.size () == 1);
1430 if (!_out_map[0].is_monotonic ()) {
1433 if (_configured_internal != _configured_in) {
1434 /* no sidechain -- TODO we could allow this with
1435 * some more logic in PluginInsert::connect_and_run().
1437 * PluginInsert::reset_map() already maps it.
1442 for (DataType::iterator t = DataType::begin(); t != DataType::end() && inplace_ok; ++t) {
1443 if (_configured_internal.get (*t) == 0) {
1447 uint32_t first_idx = _in_map[0].get (*t, 0, &valid);
1448 if (!valid || first_idx != 0) {
1449 // so far only allow to copy the *first* stream's buffer to others
1452 for (uint32_t i = 1; i < natural_input_streams ().get (*t); ++i) {
1453 uint32_t idx = _in_map[0].get (*t, i, &valid);
1454 if (valid && idx != first_idx) {
1463 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: In Place Split Map\n", name()));
1468 for (uint32_t pc = 0; pc < get_count() && inplace_ok ; ++pc) {
1469 if (!_in_map[pc].is_monotonic ()) {
1472 if (!_out_map[pc].is_monotonic ()) {
1478 /* check if every output is fed by the corresponding input
1480 * this prevents in-port 1 -> sink-pin 2 || source-pin 1 -> out port 1, source-pin 2 -> out port 2
1481 * (with in-place, source-pin 1 -> out port 1 overwrites in-port 1)
1483 * but allows in-port 1 -> sink-pin 2 || source-pin 2 -> out port 1
1485 ChanMapping in_map (input_map ());
1486 const ChanMapping::Mappings out_m (output_map ().mappings ());
1487 for (ChanMapping::Mappings::const_iterator t = out_m.begin (); t != out_m.end () && inplace_ok; ++t) {
1488 for (ChanMapping::TypeMapping::const_iterator c = (*t).second.begin (); c != (*t).second.end () ; ++c) {
1489 /* src-pin: c->first, out-port: c->second */
1491 uint32_t in_port = in_map.get (t->first, c->first, &valid);
1492 if (valid && in_port != c->second) {
1500 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: %2\n", name(), inplace_ok ? "In-Place" : "No Inplace Processing"));
1501 return !inplace_ok; // no-inplace
1505 PluginInsert::sanitize_maps ()
1507 bool changed = false;
1508 /* strip dead wood */
1509 PinMappings new_ins;
1510 PinMappings new_outs;
1511 ChanMapping new_thru;
1513 for (uint32_t pc = 0; pc < get_count(); ++pc) {
1515 ChanMapping new_out;
1516 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1517 for (uint32_t i = 0; i < natural_input_streams().get (*t); ++i) {
1519 uint32_t idx = _in_map[pc].get (*t, i, &valid);
1520 if (valid && idx < _configured_internal.get (*t)) {
1521 new_in.set (*t, i, idx);
1524 for (uint32_t o = 0; o < natural_output_streams().get (*t); ++o) {
1526 uint32_t idx = _out_map[pc].get (*t, o, &valid);
1527 if (valid && idx < _configured_out.get (*t)) {
1528 new_out.set (*t, o, idx);
1532 if (_in_map[pc] != new_in || _out_map[pc] != new_out) {
1535 new_ins[pc] = new_in;
1536 new_outs[pc] = new_out;
1539 /* prevent dup output assignments */
1540 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1541 for (uint32_t o = 0; o < _configured_out.get (*t); ++o) {
1542 bool mapped = false;
1543 for (uint32_t pc = 0; pc < get_count(); ++pc) {
1545 uint32_t idx = new_outs[pc].get_src (*t, o, &valid);
1546 if (valid && mapped) {
1547 new_outs[pc].unset (*t, idx);
1555 /* remove excess thru */
1556 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1557 for (uint32_t o = 0; o < _configured_out.get (*t); ++o) {
1559 uint32_t idx = _thru_map.get (*t, o, &valid);
1560 if (valid && idx < _configured_internal.get (*t)) {
1561 new_thru.set (*t, o, idx);
1566 /* prevent out + thru, existing plugin outputs override thru */
1567 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1568 for (uint32_t o = 0; o < _configured_out.get (*t); ++o) {
1569 bool mapped = false;
1571 for (uint32_t pc = 0; pc < get_count(); ++pc) {
1572 new_outs[pc].get_src (*t, o, &mapped);
1573 if (mapped) { break; }
1575 if (!mapped) { continue; }
1576 uint32_t idx = new_thru.get (*t, o, &valid);
1578 new_thru.unset (*t, idx);
1583 if (has_midi_bypass ()) {
1584 // TODO: include midi-bypass in the thru set,
1585 // remove dedicated handling.
1586 new_thru.unset (DataType::MIDI, 0);
1589 if (_in_map != new_ins || _out_map != new_outs || _thru_map != new_thru) {
1593 _out_map = new_outs;
1594 _thru_map = new_thru;
1600 PluginInsert::reset_map (bool emit)
1602 const PinMappings old_in (_in_map);
1603 const PinMappings old_out (_out_map);
1607 _thru_map = ChanMapping ();
1609 /* build input map */
1610 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1611 uint32_t sc = 0; // side-chain round-robin (all instances)
1613 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i, ++pc) {
1614 const uint32_t nis = natural_input_streams ().get(*t);
1615 const uint32_t stride = nis - sidechain_input_pins().get (*t);
1617 /* SC inputs are last in the plugin-insert.. */
1618 const uint32_t sc_start = _configured_in.get (*t);
1619 const uint32_t sc_len = _configured_internal.get (*t) - sc_start;
1620 /* ...but may not be at the end of the plugin ports.
1621 * in case the side-chain is not the last port, shift connections back.
1622 * and connect to side-chain
1625 uint32_t ic = 0; // split inputs
1626 const uint32_t cend = _configured_in.get (*t);
1628 for (uint32_t in = 0; in < nis; ++in) {
1629 const Plugin::IOPortDescription& iod (_plugins[pc]->describe_io_port (*t, true, in));
1630 if (iod.is_sidechain) {
1631 /* connect sidechain sinks to sidechain inputs in round-robin fashion */
1632 if (sc_len > 0) {// side-chain may be hidden
1633 _in_map[pc].set (*t, in, sc_start + sc);
1634 sc = (sc + 1) % sc_len;
1638 if (_match.method == Split) {
1639 if (cend == 0) { continue; }
1640 if (_strict_io && ic + stride * pc >= cend) {
1643 /* connect *no* sidechain sinks in round-robin fashion */
1644 _in_map[pc].set (*t, in, ic + stride * pc);
1645 if (_strict_io && (ic + 1) == cend) {
1648 ic = (ic + 1) % cend;
1650 uint32_t s = in - shift;
1651 if (stride * pc + s < cend) {
1652 _in_map[pc].set (*t, in, s + stride * pc);
1660 /* build output map */
1662 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i, ++pc) {
1663 _out_map[pc] = ChanMapping (ChanCount::min (natural_output_streams(), _configured_out));
1664 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1665 _out_map[pc].offset_to(*t, pc * natural_output_streams().get(*t));
1670 if (old_in == _in_map && old_out == _out_map) {
1674 PluginMapChanged (); /* EMIT SIGNAL */
1675 _mapping_changed = true;
1676 _session.set_dirty();
1682 PluginInsert::configure_io (ChanCount in, ChanCount out)
1684 Match old_match = _match;
1686 ChanCount old_internal;
1690 old_pins = natural_input_streams();
1691 old_in = _configured_in;
1692 old_out = _configured_out;
1693 old_internal = _configured_internal;
1695 _configured_in = in;
1696 _configured_internal = in;
1697 _configured_out = out;
1700 /* TODO hide midi-bypass, and custom outs. Best /fake/ "out" here.
1701 * (currently _sidechain->configure_io always succeeds
1702 * since Processor::configure_io() succeeds)
1704 if (!_sidechain->configure_io (in, out)) {
1705 DEBUG_TRACE (DEBUG::ChanMapping, "Sidechain configuration failed\n");
1708 _configured_internal += _sidechain->input()->n_ports();
1710 // include (static_cast<Route*>owner())->name() ??
1711 _sidechain->input ()-> set_pretty_name (string_compose (_("SC %1"), name ()));
1714 /* get plugin configuration */
1715 _match = private_can_support_io_configuration (in, out);
1717 if (DEBUG_ENABLED(DEBUG::ChanMapping)) {
1719 DEBUG_STR_APPEND(a, string_compose ("%1: ", name()));
1720 DEBUG_STR_APPEND(a, _match);
1721 DEBUG_TRACE (DEBUG::ChanMapping, DEBUG_STR(a).str());
1725 /* set the matching method and number of plugins that we will use to meet this configuration */
1726 if (set_count (_match.plugins) == false) {
1727 PluginIoReConfigure (); /* EMIT SIGNAL */
1728 _configured = false;
1732 /* configure plugins */
1733 switch (_match.method) {
1736 if (_plugins.front()->configure_io (natural_input_streams(), out) == false) {
1737 PluginIoReConfigure (); /* EMIT SIGNAL */
1738 _configured = false;
1744 ChanCount din (_configured_internal);
1745 ChanCount dout (din); // hint
1747 if (_custom_sinks.n_total () > 0) {
1748 din = _custom_sinks;
1751 } else if (_preset_out.n_audio () > 0) {
1752 dout.set (DataType::AUDIO, _preset_out.n_audio ());
1753 } else if (dout.n_midi () > 0 && dout.n_audio () == 0) {
1754 dout.set (DataType::AUDIO, 2);
1756 if (out.n_audio () == 0) { out.set (DataType::AUDIO, 1); }
1758 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: Delegate lookup : %2 %3\n", name(), din, dout));
1759 bool const r = _plugins.front()->can_support_io_configuration (din, dout, &useins);
1761 if (useins.n_audio() == 0) {
1764 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: Delegate configuration: %2 %3\n", name(), useins, dout));
1766 if (_plugins.front()->configure_io (useins, dout) == false) {
1767 PluginIoReConfigure (); /* EMIT SIGNAL */
1768 _configured = false;
1772 _custom_sinks = din;
1777 if (_plugins.front()->configure_io (in, out) == false) {
1778 PluginIoReConfigure (); /* EMIT SIGNAL */
1779 _configured = false;
1785 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",
1787 _configured ? "Y" : "N",
1788 _maps_from_state ? "Y" : "N",
1789 old_in == in ? "==" : "!=",
1790 old_out == out ? "==" : "!=",
1791 old_pins == natural_input_streams () ? "==" : "!=",
1792 old_match.method == _match.method ? "==" : "!=",
1793 old_match.custom_cfg == _match.custom_cfg ? "==" : "!=",
1794 _in_map.size() == get_count () ? "==" : "!=",
1795 _out_map.size() == get_count () ? "==" : "!="
1798 bool mapping_changed = false;
1799 if (old_in == in && old_out == out
1801 && old_pins == natural_input_streams ()
1802 && old_match.method == _match.method
1803 && old_match.custom_cfg == _match.custom_cfg
1804 && _in_map.size() == _out_map.size()
1805 && _in_map.size() == get_count ()
1807 assert (_maps_from_state == false);
1808 /* If the configuration has not changed, keep the mapping */
1809 mapping_changed = sanitize_maps ();
1810 } else if (_match.custom_cfg && _configured) {
1811 assert (_maps_from_state == false);
1812 /* don't touch the map in manual mode */
1813 mapping_changed = sanitize_maps ();
1816 if (is_channelstrip ()) {
1817 /* fake channel map - for wire display */
1820 _thru_map = ChanMapping ();
1821 _in_map[0] = ChanMapping (ChanCount::min (_configured_in, ChanCount (DataType::AUDIO, 2)));
1822 _out_map[0] = ChanMapping (ChanCount::min (_configured_out, ChanCount (DataType::AUDIO, 2)));
1823 /* set "thru" map for in-place forward of audio */
1824 for (uint32_t i = 2; i < _configured_in.n_audio(); ++i) {
1825 _thru_map.set (DataType::AUDIO, i, i);
1827 /* and midi (after implicit 1st channel bypass) */
1828 for (uint32_t i = 1; i < _configured_in.n_midi(); ++i) {
1829 _thru_map.set (DataType::MIDI, i, i);
1833 if (_maps_from_state && old_in == in && old_out == out) {
1834 mapping_changed = true;
1837 /* generate a new mapping */
1838 mapping_changed = reset_map (false);
1840 _maps_from_state = false;
1843 if (mapping_changed) {
1844 PluginMapChanged (); /* EMIT SIGNAL */
1847 if (DEBUG_ENABLED(DEBUG::ChanMapping)) {
1850 DEBUG_STR_APPEND(a, "\n--------<<--------\n");
1851 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i, ++pc) {
1853 DEBUG_STR_APPEND(a, "----><----\n");
1855 DEBUG_STR_APPEND(a, string_compose ("Channel Map for %1 plugin %2\n", name(), pc));
1856 DEBUG_STR_APPEND(a, " * Inputs:\n");
1857 DEBUG_STR_APPEND(a, _in_map[pc]);
1858 DEBUG_STR_APPEND(a, " * Outputs:\n");
1859 DEBUG_STR_APPEND(a, _out_map[pc]);
1861 DEBUG_STR_APPEND(a, " * Thru:\n");
1862 DEBUG_STR_APPEND(a, _thru_map);
1863 DEBUG_STR_APPEND(a, "-------->>--------\n");
1864 DEBUG_TRACE (DEBUG::ChanMapping, DEBUG_STR(a).str());
1869 _no_inplace = check_inplace ();
1870 _mapping_changed = false;
1872 /* only the "noinplace_buffers" thread buffers need to be this large,
1873 * this can be optimized. other buffers are fine with
1874 * ChanCount::max (natural_input_streams (), natural_output_streams())
1875 * and route.cc's max (configured_in, configured_out)
1877 * no-inplace copies "thru" outputs (to emulate in-place) for
1878 * all outputs (to prevent overwrite) into a temporary space
1879 * which also holds input buffers (in case the plugin does process
1880 * in-place and overwrites those).
1882 * this buffers need to be at least as
1883 * natural_input_streams () + possible outputs.
1885 * sidechain inputs add a constraint on the input:
1886 * configured input + sidechain (=_configured_internal)
1888 * NB. this also satisfies
1889 * max (natural_input_streams(), natural_output_streams())
1890 * which is needed for silence runs
1892 _required_buffers = ChanCount::max (_configured_internal,
1893 natural_input_streams () + ChanCount::max (_configured_out, natural_output_streams () * get_count ()));
1895 if (old_in != in || old_out != out || old_internal != _configured_internal
1896 || old_pins != natural_input_streams ()
1897 || (old_match.method != _match.method && (old_match.method == Split || _match.method == Split))
1899 PluginIoReConfigure (); /* EMIT SIGNAL */
1902 _delaybuffers.configure (_configured_out, _plugins.front ()->max_latency ());
1903 _latency_changed = true;
1905 // we don't know the analysis window size, so we must work with the
1906 // current buffer size here. each request for data fills in these
1907 // buffers and the analyser makes sure it gets enough data for the
1909 session().ensure_buffer_set (_signal_analysis_inputs, in);
1910 _signal_analysis_inputs.set_count (in);
1912 session().ensure_buffer_set (_signal_analysis_outputs, out);
1913 _signal_analysis_outputs.set_count (out);
1915 // std::cerr << "set counts to i" << in.n_audio() << "/o" << out.n_audio() << std::endl;
1918 return Processor::configure_io (in, out);
1921 /** Decide whether this PluginInsert can support a given IO configuration.
1922 * To do this, we run through a set of possible solutions in rough order of
1925 * @param in Required input channel count.
1926 * @param out Filled in with the output channel count if we return true.
1927 * @return true if the given IO configuration can be supported.
1930 PluginInsert::can_support_io_configuration (const ChanCount& in, ChanCount& out)
1933 _sidechain->can_support_io_configuration (in, out); // never fails, sets "out"
1935 return private_can_support_io_configuration (in, out).method != Impossible;
1939 PluginInsert::private_can_support_io_configuration (ChanCount const& in, ChanCount& out) const
1941 if (!_custom_cfg && _preset_out.n_audio () > 0) {
1942 // preseed hint (for variable i/o)
1943 out.set (DataType::AUDIO, _preset_out.n_audio ());
1946 Match rv = internal_can_support_io_configuration (in, out);
1948 if (!_custom_cfg && _preset_out.n_audio () > 0) {
1949 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: using output preset: %2\n", name(), _preset_out));
1950 out.set (DataType::AUDIO, _preset_out.n_audio ());
1955 /** A private version of can_support_io_configuration which returns the method
1956 * by which the configuration can be matched, rather than just whether or not
1960 PluginInsert::internal_can_support_io_configuration (ChanCount const & inx, ChanCount& out) const
1962 if (_plugins.empty()) {
1967 if (is_channelstrip ()) {
1969 return Match (ExactMatch, 1);
1973 /* if a user specified a custom cfg, so be it. */
1975 PluginInfoPtr info = _plugins.front()->get_info();
1977 if (info->reconfigurable_io()) {
1978 return Match (Delegate, 1, _strict_io, true);
1980 return Match (ExactMatch, get_count(), _strict_io, true);
1984 /* try automatic configuration */
1985 Match m = PluginInsert::automatic_can_support_io_configuration (inx, out);
1987 PluginInfoPtr info = _plugins.front()->get_info();
1988 ChanCount inputs = info->n_inputs;
1989 ChanCount outputs = info->n_outputs;
1991 /* handle case strict-i/o */
1992 if (_strict_io && m.method != Impossible) {
1995 /* special case MIDI instruments */
1996 if (needs_midi_input ()) {
1997 // output = midi-bypass + at most master-out channels.
1998 ChanCount max_out (DataType::AUDIO, 2); // TODO use master-out
1999 max_out.set (DataType::MIDI, out.get(DataType::MIDI));
2000 out = ChanCount::min (out, max_out);
2001 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: special case strict-i/o instrument\n", name()));
2007 if (inx.n_audio () != out.n_audio ()) { // ignore midi bypass
2008 /* replicate processor to match output count (generators and such)
2009 * at least enough to feed every output port. */
2010 uint32_t f = 1; // at least one. e.g. control data filters, no in, no out.
2011 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2012 uint32_t nout = outputs.get (*t);
2013 if (nout == 0 || inx.get(*t) == 0) { continue; }
2014 f = max (f, (uint32_t) ceil (inx.get(*t) / (float)nout));
2017 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: special case strict-i/o for generator\n", name()));
2018 return Match (Replicate, f, _strict_io);
2029 if (m.method != Impossible) {
2033 ChanCount ns_inputs = inputs - sidechain_input_pins ();
2035 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: resolving 'Impossible' match...\n", name()));
2037 if (info->reconfigurable_io()) {
2040 if (out.n_midi () > 0 && out.n_audio () == 0) { out.set (DataType::AUDIO, 2); }
2041 if (out.n_audio () == 0) { out.set (DataType::AUDIO, 1); }
2042 bool const r = _plugins.front()->can_support_io_configuration (inx + sidechain_input_pins (), out, &useins);
2044 // houston, we have a problem.
2045 return Match (Impossible, 0);
2048 if (inx.n_midi () > 0 && out.n_midi () == 0) { out.set (DataType::MIDI, 1); }
2049 return Match (Delegate, 1, _strict_io);
2052 ChanCount midi_bypass;
2053 if (inx.get(DataType::MIDI) == 1 && outputs.get(DataType::MIDI) == 0) {
2054 midi_bypass.set (DataType::MIDI, 1);
2057 // add at least as many plugins so that output count matches input count (w/o sidechain pins)
2059 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2060 uint32_t nin = ns_inputs.get (*t);
2061 uint32_t nout = outputs.get (*t);
2062 if (nin == 0 || inx.get(*t) == 0) { continue; }
2063 // prefer floor() so the count won't overly increase IFF (nin < nout)
2064 f = max (f, (uint32_t) floor (inx.get(*t) / (float)nout));
2066 if (f > 0 && outputs * f >= _configured_out) {
2067 out = outputs * f + midi_bypass;
2068 return Match (Replicate, f, _strict_io);
2071 // add at least as many plugins needed to connect all inputs (w/o sidechain pins)
2073 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2074 uint32_t nin = ns_inputs.get (*t);
2075 if (nin == 0 || inx.get(*t) == 0) { continue; }
2076 f = max (f, (uint32_t) ceil (inx.get(*t) / (float)nin));
2079 out = outputs * f + midi_bypass;
2080 return Match (Replicate, f, _strict_io);
2083 // add at least as many plugins needed to connect all inputs
2085 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2086 uint32_t nin = inputs.get (*t);
2087 if (nin == 0 || inx.get(*t) == 0) { continue; }
2088 f = max (f, (uint32_t) ceil (inx.get(*t) / (float)nin));
2090 out = outputs * f + midi_bypass;
2091 return Match (Replicate, f, _strict_io);
2094 /* this is the original Ardour 3/4 behavior, mainly for backwards compatibility */
2096 PluginInsert::automatic_can_support_io_configuration (ChanCount const & inx, ChanCount& out) const
2098 if (_plugins.empty()) {
2102 PluginInfoPtr info = _plugins.front()->get_info();
2103 ChanCount in; in += inx;
2104 ChanCount midi_bypass;
2106 if (info->reconfigurable_io()) {
2107 /* Plugin has flexible I/O, so delegate to it
2108 * pre-seed outputs, plugin tries closest match
2111 if (out.n_midi () > 0 && out.n_audio () == 0) { out.set (DataType::AUDIO, 2); }
2112 if (out.n_audio () == 0) { out.set (DataType::AUDIO, 1); }
2113 bool const r = _plugins.front()->can_support_io_configuration (in + sidechain_input_pins (), out);
2115 return Match (Impossible, 0);
2118 if (in.n_midi () > 0 && out.n_midi () == 0) { out.set (DataType::MIDI, 1); }
2119 return Match (Delegate, 1);
2122 ChanCount inputs = info->n_inputs;
2123 ChanCount outputs = info->n_outputs;
2124 ChanCount ns_inputs = inputs - sidechain_input_pins ();
2126 if (in.get(DataType::MIDI) == 1 && outputs.get(DataType::MIDI) == 0) {
2127 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: bypassing midi-data\n", name()));
2128 midi_bypass.set (DataType::MIDI, 1);
2130 if (in.get(DataType::MIDI) == 1 && inputs.get(DataType::MIDI) == 0) {
2131 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: hiding midi-port from plugin\n", name()));
2132 in.set(DataType::MIDI, 0);
2135 // add internally provided sidechain ports
2136 ChanCount insc = in + sidechain_input_ports ();
2138 bool no_inputs = true;
2139 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2140 if (inputs.get (*t) != 0) {
2147 /* no inputs so we can take any input configuration since we throw it away */
2148 out = outputs + midi_bypass;
2149 return Match (NoInputs, 1);
2152 /* Plugin inputs match requested inputs + side-chain-ports exactly */
2153 if (inputs == insc) {
2154 out = outputs + midi_bypass;
2155 return Match (ExactMatch, 1);
2158 /* Plugin inputs matches without side-chain-pins */
2159 if (ns_inputs == in) {
2160 out = outputs + midi_bypass;
2161 return Match (ExactMatch, 1);
2164 /* We may be able to run more than one copy of the plugin within this insert
2165 to cope with the insert having more inputs than the plugin.
2166 We allow replication only for plugins with either zero or 1 inputs and outputs
2167 for every valid data type.
2171 bool can_replicate = true;
2172 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2174 // ignore side-chains
2175 uint32_t nin = ns_inputs.get (*t);
2177 // No inputs of this type
2178 if (nin == 0 && in.get(*t) == 0) {
2182 if (nin != 1 || outputs.get (*t) != 1) {
2183 can_replicate = false;
2187 // Potential factor not set yet
2189 f = in.get(*t) / nin;
2192 // Factor for this type does not match another type, can not replicate
2193 if (f != (in.get(*t) / nin)) {
2194 can_replicate = false;
2199 if (can_replicate && f > 0) {
2200 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2201 out.set (*t, outputs.get(*t) * f);
2204 return Match (Replicate, f);
2207 /* If the processor has exactly one input of a given type, and
2208 the plugin has more, we can feed the single processor input
2209 to some or all of the plugin inputs. This is rather
2210 special-case-y, but the 1-to-many case is by far the
2211 simplest. How do I split thy 2 processor inputs to 3
2212 plugin inputs? Let me count the ways ...
2215 bool can_split = true;
2216 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2218 bool const can_split_type = (in.get (*t) == 1 && ns_inputs.get (*t) > 1);
2219 bool const nothing_to_do_for_type = (in.get (*t) == 0 && inputs.get (*t) == 0);
2221 if (!can_split_type && !nothing_to_do_for_type) {
2227 out = outputs + midi_bypass;
2228 return Match (Split, 1);
2231 /* If the plugin has more inputs than we want, we can `hide' some of them
2232 by feeding them silence.
2235 bool could_hide = false;
2236 bool cannot_hide = false;
2237 ChanCount hide_channels;
2239 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2240 if (inputs.get(*t) > in.get(*t)) {
2241 /* there is potential to hide, since the plugin has more inputs of type t than the insert */
2242 hide_channels.set (*t, inputs.get(*t) - in.get(*t));
2244 } else if (inputs.get(*t) < in.get(*t)) {
2245 /* we definitely cannot hide, since the plugin has fewer inputs of type t than the insert */
2250 if (could_hide && !cannot_hide) {
2251 out = outputs + midi_bypass;
2252 return Match (Hide, 1, false, false, hide_channels);
2255 return Match (Impossible, 0);
2260 PluginInsert::get_state ()
2262 return state (true);
2266 PluginInsert::state (bool full)
2268 XMLNode& node = Processor::state (full);
2270 node.add_property("type", _plugins[0]->state_node_name());
2271 node.add_property("unique-id", _plugins[0]->unique_id());
2272 node.add_property("count", string_compose("%1", _plugins.size()));
2274 /* remember actual i/o configuration (for later placeholder
2275 * in case the plugin goes missing) */
2276 node.add_child_nocopy (* _configured_in.state (X_("ConfiguredInput")));
2277 node.add_child_nocopy (* _custom_sinks.state (X_("CustomSinks")));
2278 node.add_child_nocopy (* _configured_out.state (X_("ConfiguredOutput")));
2279 node.add_child_nocopy (* _preset_out.state (X_("PresetOutput")));
2281 /* save custom i/o config */
2282 node.add_property("custom", _custom_cfg ? "yes" : "no");
2283 for (uint32_t pc = 0; pc < get_count(); ++pc) {
2285 snprintf (tmp, sizeof(tmp), "InputMap-%d", pc);
2286 node.add_child_nocopy (* _in_map[pc].state (tmp));
2287 snprintf (tmp, sizeof(tmp), "OutputMap-%d", pc);
2288 node.add_child_nocopy (* _out_map[pc].state (tmp));
2290 node.add_child_nocopy (* _thru_map.state ("ThruMap"));
2293 node.add_child_nocopy (_sidechain->state (full));
2296 _plugins[0]->set_insert_id(this->id());
2297 node.add_child_nocopy (_plugins[0]->get_state());
2299 for (Controls::iterator c = controls().begin(); c != controls().end(); ++c) {
2300 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> ((*c).second);
2302 node.add_child_nocopy (ac->get_state());
2310 PluginInsert::set_control_ids (const XMLNode& node, int version)
2312 const XMLNodeList& nlist = node.children();
2313 XMLNodeConstIterator iter;
2314 set<Evoral::Parameter>::const_iterator p;
2316 for (iter = nlist.begin(); iter != nlist.end(); ++iter) {
2317 if ((*iter)->name() == Controllable::xml_node_name) {
2318 XMLProperty const * prop;
2320 uint32_t p = (uint32_t)-1;
2322 if ((prop = (*iter)->property (X_("symbol"))) != 0) {
2323 boost::shared_ptr<LV2Plugin> lv2plugin = boost::dynamic_pointer_cast<LV2Plugin> (_plugins[0]);
2325 p = lv2plugin->port_index(prop->value().c_str());
2329 if (p == (uint32_t)-1 && (prop = (*iter)->property (X_("parameter"))) != 0) {
2330 p = atoi (prop->value());
2333 if (p != (uint32_t)-1) {
2335 /* this may create the new controllable */
2337 boost::shared_ptr<Evoral::Control> c = control (Evoral::Parameter (PluginAutomation, 0, p));
2339 #ifndef NO_PLUGIN_STATE
2343 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> (c);
2345 ac->set_state (**iter, version);
2354 PluginInsert::set_state(const XMLNode& node, int version)
2356 XMLNodeList nlist = node.children();
2357 XMLNodeIterator niter;
2358 XMLPropertyList plist;
2359 XMLProperty const * prop;
2360 ARDOUR::PluginType type;
2362 if ((prop = node.property ("type")) == 0) {
2363 error << _("XML node describing plugin is missing the `type' field") << endmsg;
2367 if (prop->value() == X_("ladspa") || prop->value() == X_("Ladspa")) { /* handle old school sessions */
2368 type = ARDOUR::LADSPA;
2369 } else if (prop->value() == X_("lv2")) {
2371 } else if (prop->value() == X_("windows-vst")) {
2372 type = ARDOUR::Windows_VST;
2373 } else if (prop->value() == X_("lxvst")) {
2374 type = ARDOUR::LXVST;
2375 } else if (prop->value() == X_("audiounit")) {
2376 type = ARDOUR::AudioUnit;
2377 } else if (prop->value() == X_("luaproc")) {
2380 error << string_compose (_("unknown plugin type %1 in plugin insert state"),
2386 prop = node.property ("unique-id");
2389 #ifdef WINDOWS_VST_SUPPORT
2390 /* older sessions contain VST plugins with only an "id" field.
2393 if (type == ARDOUR::Windows_VST) {
2394 prop = node.property ("id");
2398 #ifdef LXVST_SUPPORT
2399 /*There shouldn't be any older sessions with linuxVST support.. but anyway..*/
2401 if (type == ARDOUR::LXVST) {
2402 prop = node.property ("id");
2408 error << _("Plugin has no unique ID field") << endmsg;
2413 boost::shared_ptr<Plugin> plugin = find_plugin (_session, prop->value(), type);
2415 /* treat linux and windows VST plugins equivalent if they have the same uniqueID
2416 * allow to move sessions windows <> linux */
2417 #ifdef LXVST_SUPPORT
2418 if (plugin == 0 && type == ARDOUR::Windows_VST) {
2419 type = ARDOUR::LXVST;
2420 plugin = find_plugin (_session, prop->value(), type);
2424 #ifdef WINDOWS_VST_SUPPORT
2425 if (plugin == 0 && type == ARDOUR::LXVST) {
2426 type = ARDOUR::Windows_VST;
2427 plugin = find_plugin (_session, prop->value(), type);
2431 if (plugin == 0 && type == ARDOUR::Lua) {
2432 /* unique ID (sha1 of script) was not found,
2433 * load the plugin from the serialized version in the
2434 * session-file instead.
2436 boost::shared_ptr<LuaProc> lp (new LuaProc (_session.engine(), _session, ""));
2437 XMLNode *ls = node.child (lp->state_node_name().c_str());
2439 lp->set_script_from_state (*ls);
2445 error << string_compose(
2446 _("Found a reference to a plugin (\"%1\") that is unknown.\n"
2447 "Perhaps it was removed or moved since it was last used."),
2453 // The name of the PluginInsert comes from the plugin, nothing else
2454 _name = plugin->get_info()->name;
2458 // Processor::set_state() will set this, but too late
2459 // for it to be available when setting up plugin
2460 // state. We can't call Processor::set_state() until
2461 // the plugins themselves are created and added.
2465 if (_plugins.empty()) {
2466 /* if we are adding the first plugin, we will need to set
2467 up automatable controls.
2469 add_plugin (plugin);
2470 create_automatable_parameters ();
2471 set_control_ids (node, version);
2474 if ((prop = node.property ("count")) != 0) {
2475 sscanf (prop->value().c_str(), "%u", &count);
2478 if (_plugins.size() != count) {
2479 for (uint32_t n = 1; n < count; ++n) {
2480 add_plugin (plugin_factory (plugin));
2484 Processor::set_state (node, version);
2486 PBD::ID new_id = this->id();
2487 PBD::ID old_id = this->id();
2489 if ((prop = node.property ("id")) != 0) {
2490 old_id = prop->value ();
2493 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2495 /* find the node with the type-specific node name ("lv2", "ladspa", etc)
2496 and set all plugins to the same state.
2499 if ((*niter)->name() == plugin->state_node_name()) {
2501 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
2502 /* Plugin state can include external files which are named after the ID.
2504 * If regenerate_xml_or_string_ids() is set, the ID will already have
2505 * been changed, so we need to use the old ID from the XML to load the
2506 * state and then update the ID.
2508 * When copying a plugin-state, route_ui takes care of of updating the ID,
2509 * but we need to call set_insert_id() to clear the cached plugin-state
2510 * and force a change.
2512 if (!regenerate_xml_or_string_ids ()) {
2513 (*i)->set_insert_id (new_id);
2515 (*i)->set_insert_id (old_id);
2518 (*i)->set_state (**niter, version);
2520 if (regenerate_xml_or_string_ids ()) {
2521 (*i)->set_insert_id (new_id);
2529 if (version < 3000) {
2531 /* Only 2.X sessions need a call to set_parameter_state() - in 3.X and above
2532 this is all handled by Automatable
2535 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2536 if ((*niter)->name() == "Redirect") {
2537 /* XXX do we need to tackle placement? i think not (pd; oct 16 2009) */
2538 Processor::set_state (**niter, version);
2543 set_parameter_state_2X (node, version);
2546 if ((prop = node.property (X_("custom"))) != 0) {
2547 _custom_cfg = string_is_affirmative (prop->value());
2550 uint32_t in_maps = 0;
2551 uint32_t out_maps = 0;
2552 XMLNodeList kids = node.children ();
2553 for (XMLNodeIterator i = kids.begin(); i != kids.end(); ++i) {
2554 if ((*i)->name() == X_("ConfiguredInput")) {
2555 _configured_in = ChanCount(**i);
2557 if ((*i)->name() == X_("CustomSinks")) {
2558 _custom_sinks = ChanCount(**i);
2560 if ((*i)->name() == X_("ConfiguredOutput")) {
2561 _custom_out = ChanCount(**i);
2562 _configured_out = ChanCount(**i);
2564 if ((*i)->name() == X_("PresetOutput")) {
2565 _preset_out = ChanCount(**i);
2567 if (strncmp ((*i)->name ().c_str(), X_("InputMap-"), 9) == 0) {
2568 long pc = atol (&((*i)->name().c_str()[9]));
2569 if (pc >= 0 && pc <= (long) get_count()) {
2570 _in_map[pc] = ChanMapping (**i);
2574 if (strncmp ((*i)->name ().c_str(), X_("OutputMap-"), 10) == 0) {
2575 long pc = atol (&((*i)->name().c_str()[10]));
2576 if (pc >= 0 && pc <= (long) get_count()) {
2577 _out_map[pc] = ChanMapping (**i);
2581 if ((*i)->name () == "ThruMap") {
2582 _thru_map = ChanMapping (**i);
2585 // sidechain is a Processor (IO)
2586 if ((*i)->name () == Processor::state_node_name) {
2590 _sidechain->set_state (**i, version);
2594 if (in_maps == out_maps && out_maps >0 && out_maps == get_count()) {
2595 _maps_from_state = true;
2598 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
2602 (*i)->deactivate ();
2606 PluginConfigChanged (); /* EMIT SIGNAL */
2611 PluginInsert::update_id (PBD::ID id)
2614 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
2615 (*i)->set_insert_id (id);
2620 PluginInsert::set_state_dir (const std::string& d)
2622 // state() only saves the state of the first plugin
2623 _plugins[0]->set_state_dir (d);
2627 PluginInsert::set_parameter_state_2X (const XMLNode& node, int version)
2629 XMLNodeList nlist = node.children();
2630 XMLNodeIterator niter;
2632 /* look for port automation node */
2634 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2636 if ((*niter)->name() != port_automation_node_name) {
2641 XMLProperty const * cprop;
2642 XMLNodeConstIterator iter;
2647 cnodes = (*niter)->children ("port");
2649 for (iter = cnodes.begin(); iter != cnodes.end(); ++iter){
2653 if ((cprop = child->property("number")) != 0) {
2654 port = cprop->value().c_str();
2656 warning << _("PluginInsert: Auto: no ladspa port number") << endmsg;
2660 sscanf (port, "%" PRIu32, &port_id);
2662 if (port_id >= _plugins[0]->parameter_count()) {
2663 warning << _("PluginInsert: Auto: port id out of range") << endmsg;
2667 boost::shared_ptr<AutomationControl> c = boost::dynamic_pointer_cast<AutomationControl>(
2668 control(Evoral::Parameter(PluginAutomation, 0, port_id), true));
2670 if (c && c->alist()) {
2671 if (!child->children().empty()) {
2672 c->alist()->set_state (*child->children().front(), version);
2674 /* In some cases 2.X saves lists with min_yval and max_yval
2675 being FLT_MIN and FLT_MAX respectively. This causes problems
2676 in A3 because these min/max values are used to compute
2677 where GUI control points should be drawn. If we see such
2678 values, `correct' them to the min/max of the appropriate
2682 float min_y = c->alist()->get_min_y ();
2683 float max_y = c->alist()->get_max_y ();
2685 ParameterDescriptor desc;
2686 _plugins.front()->get_parameter_descriptor (port_id, desc);
2688 if (min_y == FLT_MIN) {
2692 if (max_y == FLT_MAX) {
2696 c->alist()->set_yrange (min_y, max_y);
2699 error << string_compose (_("PluginInsert: automatable control %1 not found - ignored"), port_id) << endmsg;
2711 PluginInsert::describe_parameter (Evoral::Parameter param)
2713 if (param.type() == PluginAutomation) {
2714 return _plugins[0]->describe_parameter (param);
2715 } else if (param.type() == PluginPropertyAutomation) {
2716 boost::shared_ptr<AutomationControl> c(automation_control(param));
2717 if (c && !c->desc().label.empty()) {
2718 return c->desc().label;
2721 return Automatable::describe_parameter(param);
2725 PluginInsert::signal_latency() const
2727 if (!_pending_active) {
2730 if (_user_latency) {
2731 return _user_latency;
2734 return _plugins[0]->signal_latency ();
2738 PluginInsert::type ()
2740 return plugin()->get_info()->type;
2743 PluginInsert::PluginControl::PluginControl (PluginInsert* p,
2744 const Evoral::Parameter& param,
2745 const ParameterDescriptor& desc,
2746 boost::shared_ptr<AutomationList> list)
2747 : AutomationControl (p->session(), param, desc, list, p->describe_parameter(param))
2751 alist()->reset_default (desc.normal);
2753 list->set_interpolation(Evoral::ControlList::Discrete);
2758 /** @param val `user' value */
2761 PluginInsert::PluginControl::actually_set_value (double user_val, PBD::Controllable::GroupControlDisposition group_override)
2763 /* FIXME: probably should be taking out some lock here.. */
2765 for (Plugins::iterator i = _plugin->_plugins.begin(); i != _plugin->_plugins.end(); ++i) {
2766 (*i)->set_parameter (_list->parameter().id(), user_val);
2769 boost::shared_ptr<Plugin> iasp = _plugin->_impulseAnalysisPlugin.lock();
2771 iasp->set_parameter (_list->parameter().id(), user_val);
2774 AutomationControl::actually_set_value (user_val, group_override);
2778 PluginInsert::PluginControl::catch_up_with_external_value (double user_val)
2780 AutomationControl::actually_set_value (user_val, Controllable::NoGroup);
2784 PluginInsert::PluginControl::get_state ()
2788 XMLNode& node (AutomationControl::get_state());
2789 ss << parameter().id();
2790 node.add_property (X_("parameter"), ss.str());
2792 boost::shared_ptr<LV2Plugin> lv2plugin = boost::dynamic_pointer_cast<LV2Plugin> (_plugin->_plugins[0]);
2794 node.add_property (X_("symbol"), lv2plugin->port_symbol (parameter().id()));
2801 /** @return `user' val */
2803 PluginInsert::PluginControl::get_value () const
2805 boost::shared_ptr<Plugin> plugin = _plugin->plugin (0);
2811 return plugin->get_parameter (_list->parameter().id());
2814 PluginInsert::PluginPropertyControl::PluginPropertyControl (PluginInsert* p,
2815 const Evoral::Parameter& param,
2816 const ParameterDescriptor& desc,
2817 boost::shared_ptr<AutomationList> list)
2818 : AutomationControl (p->session(), param, desc, list)
2822 alist()->set_yrange (desc.lower, desc.upper);
2823 alist()->reset_default (desc.normal);
2828 PluginInsert::PluginPropertyControl::actually_set_value (double user_val, Controllable::GroupControlDisposition gcd)
2830 /* Old numeric set_value(), coerce to appropriate datatype if possible.
2831 This is lossy, but better than nothing until Ardour's automation system
2832 can handle various datatypes all the way down. */
2833 const Variant value(_desc.datatype, user_val);
2834 if (value.type() == Variant::NOTHING) {
2835 error << "set_value(double) called for non-numeric property" << endmsg;
2839 for (Plugins::iterator i = _plugin->_plugins.begin(); i != _plugin->_plugins.end(); ++i) {
2840 (*i)->set_property(_list->parameter().id(), value);
2845 AutomationControl::actually_set_value (user_val, gcd);
2849 PluginInsert::PluginPropertyControl::get_state ()
2853 XMLNode& node (AutomationControl::get_state());
2854 ss << parameter().id();
2855 node.add_property (X_("property"), ss.str());
2856 node.remove_property (X_("value"));
2862 PluginInsert::PluginPropertyControl::get_value () const
2864 return _value.to_double();
2867 boost::shared_ptr<Plugin>
2868 PluginInsert::get_impulse_analysis_plugin()
2870 boost::shared_ptr<Plugin> ret;
2871 if (_impulseAnalysisPlugin.expired()) {
2872 // LV2 in particular uses various _session params
2873 // during init() -- most notably block_size..
2875 ret = plugin_factory(_plugins[0]);
2876 ChanCount out (internal_output_streams ());
2877 if (ret->get_info ()->reconfigurable_io ()) {
2878 // populate get_info ()->n_inputs and ->n_outputs
2880 ret->can_support_io_configuration (internal_input_streams (), out, &useins);
2881 assert (out == internal_output_streams ());
2883 ret->configure_io (internal_input_streams (), out);
2884 _impulseAnalysisPlugin = ret;
2886 ret = _impulseAnalysisPlugin.lock();
2893 PluginInsert::collect_signal_for_analysis (framecnt_t nframes)
2895 // called from outside the audio thread, so this should be safe
2896 // only do audio as analysis is (currently) only for audio plugins
2897 _signal_analysis_inputs.ensure_buffers (DataType::AUDIO, input_streams().n_audio(), nframes);
2898 _signal_analysis_outputs.ensure_buffers (DataType::AUDIO, output_streams().n_audio(), nframes);
2900 _signal_analysis_collected_nframes = 0;
2901 _signal_analysis_collect_nframes_max = nframes;
2904 /** Add a plugin to our list */
2906 PluginInsert::add_plugin (boost::shared_ptr<Plugin> plugin)
2908 plugin->set_insert_id (this->id());
2910 if (_plugins.empty()) {
2911 /* first (and probably only) plugin instance - connect to relevant signals */
2913 plugin->ParameterChangedExternally.connect_same_thread (*this, boost::bind (&PluginInsert::parameter_changed_externally, this, _1, _2));
2914 plugin->StartTouch.connect_same_thread (*this, boost::bind (&PluginInsert::start_touch, this, _1));
2915 plugin->EndTouch.connect_same_thread (*this, boost::bind (&PluginInsert::end_touch, this, _1));
2916 _custom_sinks = plugin->get_info()->n_inputs;
2917 // cache sidechain port count
2918 _cached_sidechain_pins.reset ();
2919 const ChanCount& nis (plugin->get_info()->n_inputs);
2920 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2921 for (uint32_t in = 0; in < nis.get (*t); ++in) {
2922 const Plugin::IOPortDescription& iod (plugin->describe_io_port (*t, true, in));
2923 if (iod.is_sidechain) {
2924 _cached_sidechain_pins.set (*t, 1 + _cached_sidechain_pins.n(*t));
2929 #if (defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT)
2930 boost::shared_ptr<VSTPlugin> vst = boost::dynamic_pointer_cast<VSTPlugin> (plugin);
2932 vst->set_insert (this, _plugins.size ());
2936 _plugins.push_back (plugin);
2940 PluginInsert::load_preset (ARDOUR::Plugin::PresetRecord pr)
2943 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
2944 if (! (*i)->load_preset (pr)) {
2952 PluginInsert::realtime_handle_transport_stopped ()
2954 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
2955 (*i)->realtime_handle_transport_stopped ();
2960 PluginInsert::realtime_locate ()
2962 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
2963 (*i)->realtime_locate ();
2968 PluginInsert::monitoring_changed ()
2970 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
2971 (*i)->monitoring_changed ();
2976 PluginInsert::latency_changed ()
2978 // this is called in RT context, LatencyChanged is emitted after run()
2979 _latency_changed = true;
2980 // XXX This also needs a proper API not an owner() hack.
2982 static_cast<Route*>(owner ())->processor_latency_changed (); /* EMIT SIGNAL */
2986 PluginInsert::start_touch (uint32_t param_id)
2988 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter (PluginAutomation, 0, param_id));
2990 ac->start_touch (session().audible_frame());
2995 PluginInsert::end_touch (uint32_t param_id)
2997 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter (PluginAutomation, 0, param_id));
2999 ac->stop_touch (true, session().audible_frame());
3003 std::ostream& operator<<(std::ostream& o, const ARDOUR::PluginInsert::Match& m)
3006 case PluginInsert::Impossible: o << "Impossible"; break;
3007 case PluginInsert::Delegate: o << "Delegate"; break;
3008 case PluginInsert::NoInputs: o << "NoInputs"; break;
3009 case PluginInsert::ExactMatch: o << "ExactMatch"; break;
3010 case PluginInsert::Replicate: o << "Replicate"; break;
3011 case PluginInsert::Split: o << "Split"; break;
3012 case PluginInsert::Hide: o << "Hide"; break;
3014 o << " cnt: " << m.plugins
3015 << (m.strict_io ? " strict-io" : "")
3016 << (m.custom_cfg ? " custom-cfg" : "");
3017 if (m.method == PluginInsert::Hide) {
3018 o << " hide: " << m.hide;