NO-OP: whitespace, comments
[ardour.git] / libs / ardour / plugin_insert.cc
1 /*
2     Copyright (C) 2000 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #ifdef WAF_BUILD
21 #include "libardour-config.h"
22 #endif
23
24 #include <string>
25
26 #include "pbd/failed_constructor.h"
27 #include "pbd/xml++.h"
28 #include "pbd/types_convert.h"
29
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"
40
41 #ifdef LV2_SUPPORT
42 #include "ardour/lv2_plugin.h"
43 #endif
44
45 #ifdef WINDOWS_VST_SUPPORT
46 #include "ardour/windows_vst_plugin.h"
47 #endif
48
49 #ifdef LXVST_SUPPORT
50 #include "ardour/lxvst_plugin.h"
51 #endif
52
53 #ifdef MACVST_SUPPORT
54 #include "ardour/mac_vst_plugin.h"
55 #endif
56
57 #ifdef AUDIOUNIT_SUPPORT
58 #include "ardour/audio_unit.h"
59 #endif
60
61 #include "ardour/session.h"
62 #include "ardour/types.h"
63
64 #include "pbd/i18n.h"
65
66 using namespace std;
67 using namespace ARDOUR;
68 using namespace PBD;
69
70 const string PluginInsert::port_automation_node_name = "PortAutomation";
71
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_collect_nsamples (0)
78         , _signal_analysis_collect_nsamples_max (0)
79         , _configured (false)
80         , _no_inplace (false)
81         , _strict_io (false)
82         , _custom_cfg (false)
83         , _maps_from_state (false)
84         , _latency_changed (false)
85         , _bypass_port (UINT32_MAX)
86         , _stat_reset (0)
87 {
88         /* the first is the master */
89
90         if (plug) {
91                 add_plugin (plug);
92                 create_automatable_parameters ();
93                 const ChanCount& sc (sidechain_input_pins ());
94                 if (sc.n_audio () > 0 || sc.n_midi () > 0) {
95                         add_sidechain (sc.n_audio (), sc.n_midi ());
96                 }
97         }
98 }
99
100 PluginInsert::~PluginInsert ()
101 {
102         for (CtrlOutMap::const_iterator i = _control_outputs.begin(); i != _control_outputs.end(); ++i) {
103                 boost::dynamic_pointer_cast<ReadOnlyControl>(i->second)->drop_references ();
104         }
105 }
106
107 void
108 PluginInsert::set_strict_io (bool b)
109 {
110         bool changed = _strict_io != b;
111         _strict_io = b;
112         if (changed) {
113                 PluginConfigChanged (); /* EMIT SIGNAL */
114         }
115 }
116
117 bool
118 PluginInsert::set_count (uint32_t num)
119 {
120         bool require_state = !_plugins.empty();
121
122         if (require_state && num > 1 && plugin (0)->get_info ()->type == ARDOUR::AudioUnit) {
123                 // we don't allow to replicate AUs
124                 return false;
125         }
126
127         /* this is a bad idea.... we shouldn't do this while active.
128          * only a route holding their redirect_lock should be calling this
129          */
130
131         if (num == 0) {
132                 return false;
133         } else if (num > _plugins.size()) {
134                 uint32_t diff = num - _plugins.size();
135
136                 for (uint32_t n = 0; n < diff; ++n) {
137                         boost::shared_ptr<Plugin> p = plugin_factory (_plugins[0]);
138                         add_plugin (p);
139
140                         if (require_state) {
141                                 XMLNode& state = _plugins[0]->get_state ();
142                                 p->set_state (state, Stateful::loading_state_version);
143                         }
144
145                         if (active ()) {
146                                 p->activate ();
147                         }
148                 }
149                 PluginConfigChanged (); /* EMIT SIGNAL */
150
151         } else if (num < _plugins.size()) {
152                 uint32_t diff = _plugins.size() - num;
153                 for (uint32_t n= 0; n < diff; ++n) {
154                         _plugins.pop_back();
155                 }
156                 PluginConfigChanged (); /* EMIT SIGNAL */
157         }
158
159         return true;
160 }
161
162
163 void
164 PluginInsert::set_sinks (const ChanCount& c)
165 {
166         _custom_sinks = c;
167         /* no signal, change will only be visible after re-config */
168 }
169
170 void
171 PluginInsert::set_outputs (const ChanCount& c)
172 {
173         bool changed = (_custom_out != c) && _custom_cfg;
174         _custom_out = c;
175         if (changed) {
176                 PluginConfigChanged (); /* EMIT SIGNAL */
177         }
178 }
179
180 void
181 PluginInsert::set_custom_cfg (bool b)
182 {
183         bool changed = _custom_cfg != b;
184         _custom_cfg = b;
185         if (changed) {
186                 PluginConfigChanged (); /* EMIT SIGNAL */
187         }
188 }
189
190 bool
191 PluginInsert::set_preset_out (const ChanCount& c)
192 {
193         bool changed = _preset_out != c;
194         _preset_out = c;
195         if (changed && !_custom_cfg) {
196                 PluginConfigChanged (); /* EMIT SIGNAL */
197         }
198         return changed;
199 }
200
201 bool
202 PluginInsert::add_sidechain (uint32_t n_audio, uint32_t n_midi)
203 {
204         // caller must hold process lock
205         if (_sidechain) {
206                 return false;
207         }
208         std::ostringstream n;
209         if (n_audio == 0 && n_midi == 0) {
210                 n << "TO BE RESET FROM XML";
211         } else if (owner()) {
212                 n << "SC " << owner()->name() << "/" << name() << " " << Session::next_name_id ();
213         } else {
214                 n << "tobeRenamed";
215         }
216         SideChain *sc = new SideChain (_session, n.str ());
217         _sidechain = boost::shared_ptr<SideChain> (sc);
218         _sidechain->activate ();
219         for (uint32_t n = 0; n < n_audio; ++n) {
220                 _sidechain->input()->add_port ("", owner(), DataType::AUDIO); // add a port, don't connect.
221         }
222         for (uint32_t n = 0; n < n_midi; ++n) {
223                 _sidechain->input()->add_port ("", owner(), DataType::MIDI); // add a port, don't connect.
224         }
225         PluginConfigChanged (); /* EMIT SIGNAL */
226         return true;
227 }
228
229 bool
230 PluginInsert::del_sidechain ()
231 {
232         if (!_sidechain) {
233                 return false;
234         }
235         _sidechain.reset ();
236         _sc_playback_latency = 0;
237         _sc_capture_latency = 0;
238         PluginConfigChanged (); /* EMIT SIGNAL */
239         return true;
240 }
241
242 void
243 PluginInsert::update_sidechain_name ()
244 {
245         if (!_sidechain) {
246                 return;
247         }
248
249         std::ostringstream n;
250
251         n << "SC ";
252         if (owner()) {
253                 n << owner()->name() << "/";
254         }
255
256         n << name() << " " << Session::next_name_id ();
257
258         _sidechain->set_name (n.str());
259 }
260
261 void
262 PluginInsert::control_list_automation_state_changed (Evoral::Parameter which, AutoState s)
263 {
264         if (which.type() != PluginAutomation)
265                 return;
266
267         boost::shared_ptr<AutomationControl> c
268                         = boost::dynamic_pointer_cast<AutomationControl>(control (which));
269
270         if (c && s != Off) {
271                 _plugins[0]->set_parameter (which.id(), c->list()->eval (_session.transport_sample()));
272         }
273 }
274
275 ChanCount
276 PluginInsert::output_streams() const
277 {
278         assert (_configured);
279         return _configured_out;
280 }
281
282 ChanCount
283 PluginInsert::input_streams() const
284 {
285         assert (_configured);
286         return _configured_in;
287 }
288
289 ChanCount
290 PluginInsert::internal_streams() const
291 {
292         assert (_configured);
293         return _configured_internal;
294 }
295
296 ChanCount
297 PluginInsert::internal_output_streams() const
298 {
299         assert (!_plugins.empty());
300
301         PluginInfoPtr info = _plugins.front()->get_info();
302
303         if (info->reconfigurable_io()) {
304                 ChanCount out = _plugins.front()->output_streams ();
305                 // DEBUG_TRACE (DEBUG::Processors, string_compose ("Plugin insert, reconfigur(able) output streams = %1\n", out));
306                 return out;
307         } else {
308                 ChanCount out = info->n_outputs;
309                 // DEBUG_TRACE (DEBUG::Processors, string_compose ("Plugin insert, static output streams = %1 for %2 plugins\n", out, _plugins.size()));
310                 out.set_audio (out.n_audio() * _plugins.size());
311                 out.set_midi (out.n_midi() * _plugins.size());
312                 return out;
313         }
314 }
315
316 ChanCount
317 PluginInsert::internal_input_streams() const
318 {
319         assert (!_plugins.empty());
320
321         ChanCount in;
322
323         PluginInfoPtr info = _plugins.front()->get_info();
324
325         if (info->reconfigurable_io()) {
326                 in = _plugins.front()->input_streams();
327         } else {
328                 in = info->n_inputs;
329         }
330
331         DEBUG_TRACE (DEBUG::Processors, string_compose ("Plugin insert, input streams = %1, match using %2\n", in, _match.method));
332
333         if (_match.method == Split) {
334
335                 /* we are splitting 1 processor input to multiple plugin inputs,
336                    so we have a maximum of 1 stream of each type.
337                 */
338                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
339                         if (in.get (*t) > 1) {
340                                 in.set (*t, 1);
341                         }
342                 }
343                 return in;
344
345         } else if (_match.method == Hide) {
346
347                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
348                         in.set (*t, in.get (*t) - _match.hide.get (*t));
349                 }
350                 return in;
351
352         } else {
353
354                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
355                         in.set (*t, in.get (*t) * _plugins.size ());
356                 }
357
358                 return in;
359         }
360 }
361
362 ChanCount
363 PluginInsert::natural_output_streams() const
364 {
365 #ifdef MIXBUS
366         if (is_channelstrip ()) {
367                 return ChanCount::min (_configured_out, ChanCount (DataType::AUDIO, 2));
368         }
369 #endif
370         return _plugins[0]->get_info()->n_outputs;
371 }
372
373 ChanCount
374 PluginInsert::natural_input_streams() const
375 {
376 #ifdef MIXBUS
377         if (is_channelstrip ()) {
378                 return ChanCount::min (_configured_in, ChanCount (DataType::AUDIO, 2));
379         }
380 #endif
381         return _plugins[0]->get_info()->n_inputs;
382 }
383
384 ChanCount
385 PluginInsert::sidechain_input_pins() const
386 {
387         return _cached_sidechain_pins;
388 }
389
390 bool
391 PluginInsert::has_no_inputs() const
392 {
393         return _plugins[0]->get_info()->n_inputs == ChanCount::ZERO;
394 }
395
396 bool
397 PluginInsert::has_no_audio_inputs() const
398 {
399         return _plugins[0]->get_info()->n_inputs.n_audio() == 0;
400 }
401
402 samplecnt_t
403 PluginInsert::plugin_latency () const {
404         return _plugins.front()->signal_latency ();
405 }
406
407 bool
408 PluginInsert::is_instrument() const
409 {
410         PluginInfoPtr pip = _plugins[0]->get_info();
411         return (pip->is_instrument ());
412 }
413
414 bool
415 PluginInsert::has_output_presets (ChanCount in, ChanCount out)
416 {
417         if (!_configured && _plugins[0]->get_info ()->reconfigurable_io ()) {
418                 // collect possible configurations, prefer given in/out
419                 _plugins[0]->can_support_io_configuration (in, out);
420         }
421
422         PluginOutputConfiguration ppc (_plugins[0]->possible_output ());
423
424         if (ppc.size () == 0) {
425                 return false;
426         }
427         if (!strict_io () && ppc.size () == 1) {
428                 return false;
429         }
430
431         if (strict_io () && ppc.size () == 1) {
432                 // "stereo" is currently preferred default for instruments
433                 if (ppc.find (2) != ppc.end ()) {
434                         return false;
435                 }
436         }
437
438         if (ppc.size () == 1 && ppc.find (0) != ppc.end () && !_plugins[0]->get_info ()->reconfigurable_io ()) {
439                 // some midi-sequencer (e.g. QMidiArp) or other midi-out plugin
440                 // pretending to be an "Instrument"
441                 return false;
442         }
443
444         if (!is_instrument ()) {
445                         return false;
446         }
447         return true;
448 }
449
450 void
451 PluginInsert::create_automatable_parameters ()
452 {
453         assert (!_plugins.empty());
454
455         boost::shared_ptr<Plugin> plugin = _plugins.front();
456         set<Evoral::Parameter> a = _plugins.front()->automatable ();
457
458         const uint32_t limit_automatables = Config->get_limit_n_automatables ();
459
460         for (uint32_t i = 0; i < plugin->parameter_count(); ++i) {
461                 if (!plugin->parameter_is_control (i)) {
462                         continue;
463                 }
464
465                 ParameterDescriptor desc;
466                 plugin->get_parameter_descriptor(i, desc);
467
468                 if (!plugin->parameter_is_input (i)) {
469                         _control_outputs[i] = boost::shared_ptr<ReadOnlyControl> (new ReadOnlyControl (plugin, desc, i));
470                         continue;
471                 }
472                 Evoral::Parameter param (PluginAutomation, 0, i);
473
474                 const bool automatable = a.find(param) != a.end();
475
476                 boost::shared_ptr<AutomationList> list(new AutomationList(param, desc));
477                 boost::shared_ptr<AutomationControl> c (new PluginControl(this, param, desc, list));
478                 if (!automatable || (limit_automatables > 0 && i > limit_automatables)) {
479                         c->set_flags (Controllable::Flag ((int)c->flags() | Controllable::NotAutomatable));
480                 }
481                 add_control (c);
482                 plugin->set_automation_control (i, c);
483         }
484
485
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));
494                         }
495                         boost::shared_ptr<AutomationControl> c (new PluginPropertyControl(this, param, desc, list));
496                         if (!Variant::type_is_numeric(desc.datatype)) {
497                                 c->set_flags (Controllable::Flag ((int)c->flags() | Controllable::NotAutomatable));
498                         }
499                         add_control (c);
500                 }
501         }
502
503         _bypass_port = plugin->designated_bypass_port ();
504
505         /* special case VST effSetBypass */
506         if (_bypass_port == UINT32_MAX -1) {
507                 // emulate VST Bypass
508                 Evoral::Parameter param (PluginAutomation, 0, _bypass_port);
509                 ParameterDescriptor desc;
510                 desc.label = _("Plugin Enable");
511                 desc.toggled  = true;
512                 desc.normal = 1;
513                 desc.lower  = 0;
514                 desc.upper  = 1;
515                 boost::shared_ptr<AutomationList> list(new AutomationList(param, desc));
516                 boost::shared_ptr<AutomationControl> c (new PluginControl(this, param, desc, list));
517                 add_control (c);
518         }
519
520         if (_bypass_port != UINT32_MAX) {
521                 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter (PluginAutomation, 0, _bypass_port));
522                 if (0 == (ac->flags () & Controllable::NotAutomatable)) {
523                         ac->alist()->automation_state_changed.connect_same_thread (*this, boost::bind (&PluginInsert::bypassable_changed, this));
524                         ac->Changed.connect_same_thread (*this, boost::bind (&PluginInsert::enable_changed, this));
525                 }
526         }
527         plugin->PresetPortSetValue.connect_same_thread (*this, boost::bind (&PluginInsert::preset_load_set_value, this, _1, _2));
528 }
529
530 /** Called when something outside of this host has modified a plugin
531  * parameter. Responsible for propagating the change to two places:
532  *
533  *   1) anything listening to the Control itself
534  *   2) any replicated plugins that make up this PluginInsert.
535  *
536  * The PluginInsert is connected to the ParameterChangedExternally signal for
537  * the first (primary) plugin, and here broadcasts that change to any others.
538  *
539  * XXX We should probably drop this whole replication idea (Paul, October 2015)
540  * since it isn't used by sensible plugin APIs (AU, LV2).
541  */
542 void
543 PluginInsert::parameter_changed_externally (uint32_t which, float val)
544 {
545         boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter (PluginAutomation, 0, which));
546
547         /* First propagation: alter the underlying value of the control,
548          * without telling the plugin(s) that own/use it to set it.
549          */
550
551         if (!ac) {
552                 return;
553         }
554
555         boost::shared_ptr<PluginControl> pc = boost::dynamic_pointer_cast<PluginControl> (ac);
556
557         if (pc) {
558                 pc->catch_up_with_external_value (val);
559         }
560
561         /* Second propagation: tell all plugins except the first to
562            update the value of this parameter. For sane plugin APIs,
563            there are no other plugins, so this is a no-op in those
564            cases.
565         */
566
567         Plugins::iterator i = _plugins.begin();
568
569         /* don't set the first plugin, just all the slaves */
570
571         if (i != _plugins.end()) {
572                 ++i;
573                 for (; i != _plugins.end(); ++i) {
574                         (*i)->set_parameter (which, val);
575                 }
576         }
577         boost::shared_ptr<Plugin> iasp = _impulseAnalysisPlugin.lock();
578         if (iasp) {
579                 iasp->set_parameter (which, val);
580         }
581 }
582
583 int
584 PluginInsert::set_block_size (pframes_t nframes)
585 {
586         int ret = 0;
587         for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
588                 if ((*i)->set_block_size (nframes) != 0) {
589                         ret = -1;
590                 }
591         }
592         return ret;
593 }
594
595 void
596 PluginInsert::automation_run (samplepos_t start, pframes_t nframes, bool only_active)
597 {
598         // XXX does not work when rolling backwards
599         if (_loop_location && nframes > 0) {
600                 const samplepos_t loop_start = _loop_location->start ();
601                 const samplepos_t loop_end   = _loop_location->end ();
602                 const samplecnt_t looplen    = loop_end - loop_start;
603
604                 samplecnt_t remain = nframes;
605                 samplepos_t start_pos = start;
606
607                 while (remain > 0) {
608                         if (start_pos >= loop_end) {
609                                 sampleoffset_t start_off = (start_pos - loop_start) % looplen;
610                                 start_pos = loop_start + start_off;
611                         }
612                         samplecnt_t move = std::min ((samplecnt_t)nframes, loop_end - start_pos);
613
614                         Automatable::automation_run (start_pos, move, only_active);
615                         remain -= move;
616                         start_pos += move;
617                 }
618                 return;
619         }
620         Automatable::automation_run (start, nframes, only_active);
621 }
622
623 bool
624 PluginInsert::find_next_event (double now, double end, Evoral::ControlEvent& next_event, bool only_active) const
625 {
626         bool rv = Automatable::find_next_event (now, end, next_event, only_active);
627
628         if (_loop_location && now < end) {
629                 if (rv) {
630                         end = ceil (next_event.when);
631                 }
632                 const samplepos_t loop_end = _loop_location->end ();
633                 assert (now < loop_end); // due to map_loop_range ()
634                 if (end > loop_end) {
635                         next_event.when = loop_end;
636                         rv = true;
637                 }
638         }
639         return rv;
640 }
641
642 void
643 PluginInsert::activate ()
644 {
645         _timing_stats.reset ();
646         for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
647                 (*i)->activate ();
648         }
649
650         Processor::activate ();
651         /* when setting state e.g ProcessorBox::paste_processor_state ()
652          * the plugin is not yet owned by a route.
653          * but no matter.  Route::add_processors() will call activate () again
654          */
655         if (!owner ()) {
656                 return;
657         }
658
659         const samplecnt_t l = effective_latency ();
660         if (_plugin_signal_latency != l) {
661                 _plugin_signal_latency = l;
662                 latency_changed ();
663         }
664 }
665
666 void
667 PluginInsert::deactivate ()
668 {
669 #ifdef MIXBUS
670         if (is_nonbypassable ()) {
671                 return;
672         }
673 #endif
674         _timing_stats.reset ();
675         Processor::deactivate ();
676
677         for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
678                 (*i)->deactivate ();
679         }
680
681         const samplecnt_t l = effective_latency ();
682         if (_plugin_signal_latency != l) {
683                 _plugin_signal_latency = l;
684                 latency_changed ();
685         }
686 }
687
688 void
689 PluginInsert::flush ()
690 {
691         for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
692                 (*i)->flush ();
693         }
694 }
695
696 void
697 PluginInsert::enable (bool yn)
698 {
699         if (_bypass_port == UINT32_MAX) {
700                 if (yn) {
701                         activate ();
702                 } else {
703                         deactivate ();
704                 }
705         } else {
706                 if (!_pending_active) {
707                         activate ();
708                 }
709                 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter (PluginAutomation, 0, _bypass_port));
710                 const double val = yn ? 1.0 : 0.0;
711                 ac->set_value (val, Controllable::NoGroup);
712
713 #ifdef ALLOW_VST_BYPASS_TO_FAIL // yet unused, see also vst_plugin.cc
714                 /* special case VST.. bypass may fail */
715                 if (_bypass_port == UINT32_MAX - 1) {
716                         /* check if bypass worked */
717                         if (ac->get_value () != val) {
718                                 warning << _("PluginInsert: VST Bypass failed, falling back to host bypass.") << endmsg;
719                                 // set plugin to enabled (not-byassed)
720                                 ac->set_value (1.0, Controllable::NoGroup);
721                                 // ..and use host-provided hard-bypass
722                                 if (yn) {
723                                         activate ();
724                                 } else {
725                                         deactivate ();
726                                 }
727                                 return;
728                         }
729                 }
730 #endif
731                 ActiveChanged ();
732         }
733 }
734
735 bool
736 PluginInsert::enabled () const
737 {
738         if (_bypass_port == UINT32_MAX) {
739                 return Processor::enabled ();
740         } else {
741                 boost::shared_ptr<const AutomationControl> ac = boost::const_pointer_cast<AutomationControl> (automation_control (Evoral::Parameter (PluginAutomation, 0, _bypass_port)));
742                 return (ac->get_value () > 0 && _pending_active);
743         }
744 }
745
746 bool
747 PluginInsert::bypassable () const
748 {
749         if (_bypass_port == UINT32_MAX) {
750                 return true;
751         } else {
752                 boost::shared_ptr<const AutomationControl> ac = boost::const_pointer_cast<AutomationControl> (automation_control (Evoral::Parameter (PluginAutomation, 0, _bypass_port)));
753
754                 return !ac->automation_playback ();
755         }
756 }
757
758 void
759 PluginInsert::enable_changed ()
760 {
761         ActiveChanged ();
762 }
763
764 void
765 PluginInsert::bypassable_changed ()
766 {
767         BypassableChanged ();
768 }
769
770 bool
771 PluginInsert::write_immediate_event (size_t size, const uint8_t* buf)
772 {
773         bool rv = true;
774         for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
775                 if (!(*i)->write_immediate_event (size, buf)) {
776                         rv = false;
777                 }
778         }
779         return rv;
780 }
781
782 void
783 PluginInsert::preset_load_set_value (uint32_t p, float v)
784 {
785         boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter(PluginAutomation, 0, p));
786         if (!ac) {
787                 return;
788         }
789
790         if (ac->automation_state() & Play) {
791                 return;
792         }
793
794         start_touch (p);
795         ac->set_value (v, Controllable::NoGroup);
796         end_touch (p);
797 }
798
799 void
800 PluginInsert::inplace_silence_unconnected (BufferSet& bufs, const PinMappings& out_map, samplecnt_t nframes, samplecnt_t offset) const
801 {
802         // TODO optimize: store "unconnected" in a fixed set.
803         // it only changes on reconfiguration.
804         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
805                 for (uint32_t out = 0; out < bufs.count().get (*t); ++out) {
806                         bool mapped = false;
807                         if (*t == DataType::MIDI && out == 0 && has_midi_bypass ()) {
808                                 mapped = true; // in-place Midi bypass
809                         }
810                         for (uint32_t pc = 0; pc < get_count() && !mapped; ++pc) {
811                                 PinMappings::const_iterator i = out_map.find (pc);
812                                 if (i == out_map.end ()) {
813                                         continue;
814                                 }
815                                 const ChanMapping& outmap (i->second);
816                                 for (uint32_t o = 0; o < natural_output_streams().get (*t); ++o) {
817                                         bool valid;
818                                         uint32_t idx = outmap.get (*t, o, &valid);
819                                         if (valid && idx == out) {
820                                                 mapped = true;
821                                                 break;
822                                         }
823                                 }
824                         }
825                         if (!mapped) {
826                                 bufs.get (*t, out).silence (nframes, offset);
827                         }
828                 }
829         }
830 }
831
832 void
833 PluginInsert::connect_and_run (BufferSet& bufs, samplepos_t start, samplepos_t end, double speed, pframes_t nframes, samplecnt_t offset, bool with_auto)
834 {
835         if (_mapping_changed) { // ToDo use a counter, increment until match
836                 _no_inplace = check_inplace ();
837                 _mapping_changed = false;
838         }
839         // TODO: atomically copy maps & _no_inplace
840         PinMappings in_map (_in_map); // TODO Split case below overrides, use const& in_map
841         PinMappings const& out_map (_out_map);
842         ChanMapping const& thru_map (_thru_map);
843
844         if (_latency_changed) {
845                 /* delaylines are configured with the max possible latency (as reported by the plugin)
846                  * so this won't allocate memory (unless the plugin lied about its max latency)
847                  * It may still 'click' though, since the fixed delaylines are not de-clicked.
848                  * Then again plugin-latency changes are not click-free to begin with.
849                  *
850                  * This is also worst case, there is currently no concept of per-stream latency.
851                  *
852                  * e.g.  Two identical latent plugins:
853                  *   1st plugin: process left (latent), bypass right.
854                  *   2nd plugin: bypass left, process right (latent).
855                  * -> currently this yields 2 times latency of the plugin,
856                  */
857                 _latency_changed = false;
858                 _delaybuffers.set (ChanCount::max(bufs.count(), _configured_out), plugin_latency ());
859         }
860
861         if (_match.method == Split && !_no_inplace) {
862                 // TODO: also use this optimization if one source-buffer
863                 // feeds _all_ *connected* inputs.
864                 // currently this is *first* buffer to all only --
865                 // see PluginInsert::check_inplace
866                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
867                         if (_configured_internal.get (*t) == 0) {
868                                 continue;
869                         }
870                         bool valid;
871                         uint32_t first_idx = in_map.p(0).get (*t, 0, &valid);
872                         assert (valid && first_idx == 0); // check_inplace ensures this
873                         /* copy the first stream's buffer contents to the others */
874                         for (uint32_t i = 1; i < natural_input_streams ().get (*t); ++i) {
875                                 uint32_t idx = in_map.p(0).get (*t, i, &valid);
876                                 if (valid) {
877                                         assert (idx == 0);
878                                         bufs.get (*t, i).read_from (bufs.get (*t, first_idx), nframes, offset, offset);
879                                 }
880                         }
881                 }
882                 /* the copy operation produces a linear monotonic input map */
883                 in_map[0] = ChanMapping (natural_input_streams ());
884         }
885
886         bufs.set_count(ChanCount::max(bufs.count(), _configured_internal));
887         bufs.set_count(ChanCount::max(bufs.count(), _configured_out));
888
889         if (with_auto) {
890                 boost::shared_ptr<ControlList> cl = _automated_controls.reader ();
891                 for (ControlList::const_iterator ci = cl->begin(); ci != cl->end(); ++ci) {
892                         AutomationControl& c = *(ci->get());
893                         boost::shared_ptr<const Evoral::ControlList> clist (c.list());
894                         /* we still need to check for Touch and Latch */
895                         if (clist && (static_cast<AutomationList const&> (*clist)).automation_playback ()) {
896                                 bool valid;
897                                 const float val = c.list()->rt_safe_eval (start, valid);
898                                 if (valid) {
899                                         c.set_value_unchecked(val);
900                                 }
901                         }
902                 }
903         }
904
905         if (_signal_analysis_collect_nsamples_max > 0) {
906                 if (_signal_analysis_collect_nsamples < _signal_analysis_collect_nsamples_max) {
907                         samplecnt_t ns = std::min ((samplecnt_t) nframes, _signal_analysis_collect_nsamples_max - _signal_analysis_collect_nsamples);
908                         _signal_analysis_inputs.set_count (ChanCount (DataType::AUDIO, input_streams().n_audio()));
909
910                         for (uint32_t i = 0; i < input_streams().n_audio(); ++i) {
911                                 _signal_analysis_inputs.get_audio(i).read_from (
912                                                 bufs.get_audio(i),
913                                                 ns,
914                                                 _signal_analysis_collect_nsamples);
915                         }
916                 }
917                 _signal_analysis_collect_nsamples += nframes;
918         }
919
920 #ifdef MIXBUS
921         if (is_channelstrip ()) {
922                 if (_configured_in.n_audio() > 0) {
923                         ChanMapping mb_in_map (ChanCount::min (_configured_in, ChanCount (DataType::AUDIO, 2)));
924                         ChanMapping mb_out_map (ChanCount::min (_configured_out, ChanCount (DataType::AUDIO, 2)));
925
926                         _plugins.front()->connect_and_run (bufs, start, end, speed, mb_in_map, mb_out_map, nframes, offset);
927
928                         for (uint32_t out = _configured_in.n_audio (); out < bufs.count().get (DataType::AUDIO); ++out) {
929                                 bufs.get (DataType::AUDIO, out).silence (nframes, offset);
930                         }
931                 }
932         } else
933 #endif
934         if (_no_inplace) {
935                 // TODO optimize -- build maps once.
936                 uint32_t pc = 0;
937                 BufferSet& inplace_bufs  = _session.get_noinplace_buffers();
938                 ARDOUR::ChanMapping used_outputs;
939
940                 assert (inplace_bufs.count () >= natural_input_streams () + _configured_out);
941
942                 /* build used-output map */
943                 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i, ++pc) {
944                         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
945                                 for (uint32_t out = 0; out < natural_output_streams().get (*t); ++out) {
946                                         bool valid;
947                                         uint32_t out_idx = out_map.p(pc).get (*t, out, &valid);
948                                         if (valid) {
949                                                 used_outputs.set (*t, out_idx, 1); // mark as used
950                                         }
951                                 }
952                         }
953                 }
954                 /* copy thru data to outputs before processing in-place */
955                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
956                         for (uint32_t out = 0; out < bufs.count().get (*t); ++out) {
957                                 bool valid;
958                                 uint32_t in_idx = thru_map.get (*t, out, &valid);
959                                 uint32_t m = out + natural_input_streams ().get (*t);
960                                 if (valid) {
961                                         _delaybuffers.delay (*t, out, inplace_bufs.get (*t, m), bufs.get (*t, in_idx), nframes, offset, offset);
962                                         used_outputs.set (*t, out, 1); // mark as used
963                                 } else {
964                                         used_outputs.get (*t, out, &valid);
965                                         if (valid) {
966                                                 /* the plugin is expected to write here, but may not :(
967                                                  * (e.g. drumgizmo w/o kit loaded)
968                                                  */
969                                                 inplace_bufs.get (*t, m).silence (nframes);
970                                         }
971                                 }
972                         }
973                 }
974
975                 pc = 0;
976                 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i, ++pc) {
977
978                         ARDOUR::ChanMapping i_in_map (natural_input_streams());
979                         ARDOUR::ChanMapping i_out_map (out_map.p(pc));
980                         ARDOUR::ChanCount mapped;
981
982                         /* map inputs sequentially */
983                         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
984                                 for (uint32_t in = 0; in < natural_input_streams().get (*t); ++in) {
985                                         bool valid;
986                                         uint32_t in_idx = in_map.p(pc).get (*t, in, &valid);
987                                         uint32_t m = mapped.get (*t);
988                                         if (valid) {
989                                                 inplace_bufs.get (*t, m).read_from (bufs.get (*t, in_idx), nframes, offset, offset);
990                                         } else {
991                                                 inplace_bufs.get (*t, m).silence (nframes, offset);
992                                         }
993                                         mapped.set (*t, m + 1);
994                                 }
995                         }
996
997                         /* outputs are mapped to inplace_bufs after the inputs */
998                         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
999                                 i_out_map.offset_to (*t, natural_input_streams ().get (*t));
1000                         }
1001
1002                         if ((*i)->connect_and_run (inplace_bufs, start, end, speed, i_in_map, i_out_map, nframes, offset)) {
1003                                 deactivate ();
1004                         }
1005                 }
1006
1007                 /* all instances have completed, now copy data that was written
1008                  * and zero unconnected buffers */
1009                 ARDOUR::ChanMapping nonzero_out (used_outputs);
1010                 if (has_midi_bypass ()) {
1011                         nonzero_out.set (DataType::MIDI, 0, 1); // Midi bypass.
1012                 }
1013                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1014                         for (uint32_t out = 0; out < bufs.count().get (*t); ++out) {
1015                                 bool valid;
1016                                 used_outputs.get (*t, out, &valid);
1017                                 if (!valid) {
1018                                         nonzero_out.get (*t, out, &valid);
1019                                         if (!valid) {
1020                                                 bufs.get (*t, out).silence (nframes, offset);
1021                                         }
1022                                 } else {
1023                                         uint32_t m = out + natural_input_streams ().get (*t);
1024                                         bufs.get (*t, out).read_from (inplace_bufs.get (*t, m), nframes, offset, offset);
1025                                 }
1026                         }
1027                 }
1028         } else {
1029                 /* in-place processing */
1030                 uint32_t pc = 0;
1031                 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i, ++pc) {
1032                         if ((*i)->connect_and_run(bufs, start, end, speed, in_map.p(pc), out_map.p(pc), nframes, offset)) {
1033                                 deactivate ();
1034                         }
1035                 }
1036                 // now silence unconnected outputs
1037                 inplace_silence_unconnected (bufs, _out_map, nframes, offset);
1038         }
1039
1040         const samplecnt_t l = effective_latency ();
1041         if (_plugin_signal_latency != l) {
1042                 _plugin_signal_latency = l;
1043                 _signal_analysis_collect_nsamples = 0;
1044                 latency_changed ();
1045         }
1046
1047         if (_signal_analysis_collect_nsamples > l) {
1048                 assert (_signal_analysis_collect_nsamples_max > 0);
1049                 assert (_signal_analysis_collect_nsamples >= nframes);
1050                 samplecnt_t sample_pos = _signal_analysis_collect_nsamples - nframes;
1051
1052                 samplecnt_t dst_off = sample_pos >= l ? sample_pos - l : 0;
1053                 samplecnt_t src_off = sample_pos >= l ? 0 : l - sample_pos;
1054                 samplecnt_t n_copy = std::min ((samplecnt_t)nframes, _signal_analysis_collect_nsamples - l);
1055                 n_copy = std::min (n_copy, _signal_analysis_collect_nsamples_max - dst_off);
1056
1057                 _signal_analysis_outputs.set_count (ChanCount (DataType::AUDIO, output_streams().n_audio()));
1058
1059                 for (uint32_t i = 0; i < output_streams().n_audio(); ++i) {
1060                         _signal_analysis_outputs.get_audio(i).read_from(
1061                                 bufs.get_audio(i), n_copy, dst_off, src_off);
1062                 }
1063
1064                 if (dst_off + n_copy == _signal_analysis_collect_nsamples_max) {
1065                         _signal_analysis_collect_nsamples_max = 0;
1066                         _signal_analysis_collect_nsamples     = 0;
1067
1068                         AnalysisDataGathered (&_signal_analysis_inputs, &_signal_analysis_outputs);
1069                 }
1070         }
1071 }
1072
1073 void
1074 PluginInsert::bypass (BufferSet& bufs, pframes_t nframes)
1075 {
1076         /* bypass the plugin(s) not the whole processor.
1077          * -> use mappings just like connect_and_run
1078          */
1079         if (_mapping_changed) {
1080                 _no_inplace = check_inplace ();
1081                 _mapping_changed = false;
1082         }
1083         // TODO: atomically copy maps & _no_inplace
1084         ChanMapping const& in_map (no_sc_input_map ());
1085         ChanMapping const& out_map (output_map ());
1086
1087         bufs.set_count(ChanCount::max(bufs.count(), _configured_internal));
1088         bufs.set_count(ChanCount::max(bufs.count(), _configured_out));
1089
1090         if (_no_inplace) {
1091                 ChanMapping thru_map (_thru_map);
1092
1093                 BufferSet& inplace_bufs  = _session.get_noinplace_buffers();
1094                 // copy all inputs
1095                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1096                         for (uint32_t in = 0; in < _configured_internal.get (*t); ++in) {
1097                                 inplace_bufs.get (*t, in).read_from (bufs.get (*t, in), nframes, 0, 0);
1098                         }
1099                 }
1100                 ARDOUR::ChanMapping used_outputs;
1101                 // copy thru
1102                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1103                         for (uint32_t out = 0; out < _configured_out.get (*t); ++out) {
1104                                 bool valid;
1105                                 uint32_t in_idx = thru_map.get (*t, out, &valid);
1106                                 if (valid) {
1107                                         bufs.get (*t, out).read_from (inplace_bufs.get (*t, in_idx), nframes, 0, 0);
1108                                         used_outputs.set (*t, out, 1); // mark as used
1109                                 }
1110                         }
1111                 }
1112                 // plugin no-op: assume every plugin has an internal identity map
1113                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1114                         for (uint32_t out = 0; out < _configured_out.get (*t); ++out) {
1115                                 bool valid;
1116                                 uint32_t src_idx = out_map.get_src (*t, out, &valid);
1117                                 if (!valid) {
1118                                         continue;
1119                                 }
1120                                 uint32_t in_idx = in_map.get (*t, src_idx, &valid);
1121                                 if (!valid) {
1122                                         continue;
1123                                 }
1124                                 bufs.get (*t, out).read_from (inplace_bufs.get (*t, in_idx), nframes, 0, 0);
1125                                 used_outputs.set (*t, out, 1); // mark as used
1126                         }
1127                 }
1128                 // now silence all unused outputs
1129                 if (has_midi_bypass ()) {
1130                         used_outputs.set (DataType::MIDI, 0, 1); // Midi bypass.
1131                 }
1132                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1133                         for (uint32_t out = 0; out < _configured_out.get (*t); ++out) {
1134                                 bool valid;
1135                                 used_outputs.get (*t, out, &valid);
1136                                 if (!valid) {
1137                                                 bufs.get (*t, out).silence (nframes, 0);
1138                                 }
1139                         }
1140                 }
1141         } else {
1142                 if (_match.method == Split) {
1143                         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1144                                 if (_configured_internal.get (*t) == 0) {
1145                                         continue;
1146                                 }
1147                                 // copy/feeds _all_ *connected* inputs, copy the first buffer
1148                                 bool valid;
1149                                 uint32_t first_idx = in_map.get (*t, 0, &valid);
1150                                 assert (valid && first_idx == 0); // check_inplace ensures this
1151                                 for (uint32_t i = 1; i < natural_input_streams ().get (*t); ++i) {
1152                                         uint32_t idx = in_map.get (*t, i, &valid);
1153                                         if (valid) {
1154                                                 assert (idx == 0);
1155                                                 bufs.get (*t, i).read_from (bufs.get (*t, first_idx), nframes, 0, 0);
1156                                         }
1157                                 }
1158                         }
1159                 }
1160
1161                 // apply output map and/or monotonic but not identity i/o mappings
1162                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1163                         for (uint32_t out = 0; out < _configured_out.get (*t); ++out) {
1164                                 bool valid;
1165                                 uint32_t src_idx = out_map.get_src (*t, out, &valid);
1166                                 if (!valid) {
1167                                         bufs.get (*t, out).silence (nframes, 0);
1168                                         continue;
1169                                 }
1170                                 uint32_t in_idx = in_map.get (*t, src_idx, &valid);
1171                                 if (!valid) {
1172                                         bufs.get (*t, out).silence (nframes, 0);
1173                                         continue;
1174                                 }
1175                                 if (in_idx != src_idx) {
1176                                         bufs.get (*t, out).read_from (bufs.get (*t, in_idx), nframes, 0, 0);
1177                                 }
1178                         }
1179                 }
1180         }
1181 }
1182
1183 void
1184 PluginInsert::silence (samplecnt_t nframes, samplepos_t start_sample)
1185 {
1186         automation_run (start_sample, nframes, true); // evaluate automation only
1187
1188         if (!active ()) {
1189                 // XXX delaybuffers need to be offset by nframes
1190                 return;
1191         }
1192
1193         _delaybuffers.flush ();
1194
1195         const ChanMapping in_map (natural_input_streams ());
1196         const ChanMapping out_map (natural_output_streams ());
1197         ChanCount maxbuf = ChanCount::max (natural_input_streams (), natural_output_streams());
1198 #ifdef MIXBUS
1199         if (is_channelstrip ()) {
1200                 if (_configured_in.n_audio() > 0) {
1201                         _plugins.front()->connect_and_run (_session.get_scratch_buffers (maxbuf, true), start_sample, start_sample + nframes, 1.0, in_map, out_map, nframes, 0);
1202                 }
1203         } else
1204 #endif
1205         for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
1206                 (*i)->connect_and_run (_session.get_scratch_buffers (maxbuf, true), start_sample, start_sample + nframes, 1.0, in_map, out_map, nframes, 0);
1207         }
1208 }
1209
1210 void
1211 PluginInsert::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_sample, double speed, pframes_t nframes, bool)
1212 {
1213         if (_sidechain) {
1214                 // collect sidechain input for complete cycle (!)
1215                 // TODO we need delaylines here for latency compensation
1216                 _sidechain->run (bufs, start_sample, end_sample, speed, nframes, true);
1217         }
1218
1219         if (g_atomic_int_compare_and_exchange (&_stat_reset, 1, 0)) {
1220                 _timing_stats.reset ();
1221         }
1222
1223         if (_pending_active) {
1224 #if defined MIXBUS && defined NDEBUG
1225                 if (!is_channelstrip ()) {
1226                         _timing_stats.start ();
1227                 }
1228 #else
1229                 _timing_stats.start ();
1230 #endif
1231                 /* run as normal if we are active or moving from inactive to active */
1232
1233                 if (_session.transport_rolling() || _session.bounce_processing()) {
1234                         automate_and_run (bufs, start_sample, end_sample, speed, nframes);
1235                 } else {
1236                         Glib::Threads::Mutex::Lock lm (control_lock(), Glib::Threads::TRY_LOCK);
1237                         connect_and_run (bufs, start_sample, end_sample, speed, nframes, 0, lm.locked());
1238                 }
1239 #if defined MIXBUS && defined NDEBUG
1240                 if (!is_channelstrip ()) {
1241                         _timing_stats.update ();
1242                 }
1243 #else
1244                 _timing_stats.update ();
1245 #endif
1246
1247         } else {
1248                 _timing_stats.reset ();
1249                 // XXX should call ::silence() to run plugin(s) for consistent load.
1250                 // We'll need to change this anyway when bypass can be automated
1251                 bypass (bufs, nframes);
1252                 automation_run (start_sample, nframes, true); // evaluate automation only
1253                 _delaybuffers.flush ();
1254         }
1255
1256         _active = _pending_active;
1257
1258         /* we have no idea whether the plugin generated silence or not, so mark
1259          * all buffers appropriately.
1260          */
1261 }
1262
1263 void
1264 PluginInsert::automate_and_run (BufferSet& bufs, samplepos_t start, samplepos_t end, double speed, pframes_t nframes)
1265 {
1266         Evoral::ControlEvent next_event (0, 0.0f);
1267         samplecnt_t offset = 0;
1268
1269         Glib::Threads::Mutex::Lock lm (control_lock(), Glib::Threads::TRY_LOCK);
1270
1271         if (!lm.locked()) {
1272                 connect_and_run (bufs, start, end, speed, nframes, offset, false);
1273                 return;
1274         }
1275
1276         /* map start back into loop-range, adjust end */
1277         map_loop_range (start, end);
1278
1279         if (!find_next_event (start, end, next_event) || _plugins.front()->requires_fixed_sized_buffers()) {
1280
1281                 /* no events have a time within the relevant range */
1282
1283                 connect_and_run (bufs, start, end, speed, nframes, offset, true);
1284                 return;
1285         }
1286
1287         while (nframes) {
1288
1289                 samplecnt_t cnt = min (((samplecnt_t) ceil (next_event.when) - start), (samplecnt_t) nframes);
1290
1291                 connect_and_run (bufs, start, start + cnt, speed, cnt, offset, true); // XXX (start + cnt) * speed
1292
1293                 nframes -= cnt;
1294                 offset += cnt;
1295                 start += cnt;
1296
1297                 map_loop_range (start, end);
1298
1299                 if (!find_next_event (start, end, next_event)) {
1300                         break;
1301                 }
1302         }
1303
1304         /* cleanup anything that is left to do */
1305
1306         if (nframes) {
1307                 connect_and_run (bufs, start, start + nframes, speed, nframes, offset, true);
1308         }
1309 }
1310
1311 float
1312 PluginInsert::default_parameter_value (const Evoral::Parameter& param)
1313 {
1314         if (param.type() != PluginAutomation)
1315                 return 1.0;
1316
1317         if (_plugins.empty()) {
1318                 fatal << _("programming error: ") << X_("PluginInsert::default_parameter_value() called with no plugin")
1319                       << endmsg;
1320                 abort(); /*NOTREACHED*/
1321         }
1322
1323         return _plugins[0]->default_value (param.id());
1324 }
1325
1326
1327 bool
1328 PluginInsert::can_reset_all_parameters ()
1329 {
1330         bool all = true;
1331         uint32_t params = 0;
1332         for (uint32_t par = 0; par < _plugins[0]->parameter_count(); ++par) {
1333                 bool ok=false;
1334                 const uint32_t cid = _plugins[0]->nth_parameter (par, ok);
1335
1336                 if (!ok || !_plugins[0]->parameter_is_input(cid)) {
1337                         continue;
1338                 }
1339
1340                 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter(PluginAutomation, 0, cid));
1341                 if (!ac) {
1342                         continue;
1343                 }
1344
1345                 ++params;
1346                 if (ac->automation_state() & Play) {
1347                         all = false;
1348                         break;
1349                 }
1350         }
1351         return all && (params > 0);
1352 }
1353
1354 bool
1355 PluginInsert::reset_parameters_to_default ()
1356 {
1357         bool all = true;
1358
1359         for (uint32_t par = 0; par < _plugins[0]->parameter_count(); ++par) {
1360                 bool ok=false;
1361                 const uint32_t cid = _plugins[0]->nth_parameter (par, ok);
1362
1363                 if (!ok || !_plugins[0]->parameter_is_input(cid)) {
1364                         continue;
1365                 }
1366
1367                 const float dflt = _plugins[0]->default_value (cid);
1368                 const float curr = _plugins[0]->get_parameter (cid);
1369
1370                 if (dflt == curr) {
1371                         continue;
1372                 }
1373
1374                 boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter(PluginAutomation, 0, cid));
1375                 if (!ac) {
1376                         continue;
1377                 }
1378
1379                 if (ac->automation_state() & Play) {
1380                         all = false;
1381                         continue;
1382                 }
1383
1384                 ac->set_value (dflt, Controllable::NoGroup);
1385         }
1386         return all;
1387 }
1388
1389 boost::shared_ptr<Plugin>
1390 PluginInsert::plugin_factory (boost::shared_ptr<Plugin> other)
1391 {
1392         boost::shared_ptr<LadspaPlugin> lp;
1393         boost::shared_ptr<LuaProc> lua;
1394 #ifdef LV2_SUPPORT
1395         boost::shared_ptr<LV2Plugin> lv2p;
1396 #endif
1397 #ifdef WINDOWS_VST_SUPPORT
1398         boost::shared_ptr<WindowsVSTPlugin> vp;
1399 #endif
1400 #ifdef LXVST_SUPPORT
1401         boost::shared_ptr<LXVSTPlugin> lxvp;
1402 #endif
1403 #ifdef MACVST_SUPPORT
1404         boost::shared_ptr<MacVSTPlugin> mvp;
1405 #endif
1406 #ifdef AUDIOUNIT_SUPPORT
1407         boost::shared_ptr<AUPlugin> ap;
1408 #endif
1409
1410         if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
1411                 return boost::shared_ptr<Plugin> (new LadspaPlugin (*lp));
1412         } else if ((lua = boost::dynamic_pointer_cast<LuaProc> (other)) != 0) {
1413                 return boost::shared_ptr<Plugin> (new LuaProc (*lua));
1414 #ifdef LV2_SUPPORT
1415         } else if ((lv2p = boost::dynamic_pointer_cast<LV2Plugin> (other)) != 0) {
1416                 return boost::shared_ptr<Plugin> (new LV2Plugin (*lv2p));
1417 #endif
1418 #ifdef WINDOWS_VST_SUPPORT
1419         } else if ((vp = boost::dynamic_pointer_cast<WindowsVSTPlugin> (other)) != 0) {
1420                 return boost::shared_ptr<Plugin> (new WindowsVSTPlugin (*vp));
1421 #endif
1422 #ifdef LXVST_SUPPORT
1423         } else if ((lxvp = boost::dynamic_pointer_cast<LXVSTPlugin> (other)) != 0) {
1424                 return boost::shared_ptr<Plugin> (new LXVSTPlugin (*lxvp));
1425 #endif
1426 #ifdef MACVST_SUPPORT
1427         } else if ((mvp = boost::dynamic_pointer_cast<MacVSTPlugin> (other)) != 0) {
1428                 return boost::shared_ptr<Plugin> (new MacVSTPlugin (*mvp));
1429 #endif
1430 #ifdef AUDIOUNIT_SUPPORT
1431         } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
1432                 return boost::shared_ptr<Plugin> (new AUPlugin (*ap));
1433 #endif
1434         }
1435
1436         fatal << string_compose (_("programming error: %1"),
1437                           X_("unknown plugin type in PluginInsert::plugin_factory"))
1438               << endmsg;
1439         abort(); /*NOTREACHED*/
1440         return boost::shared_ptr<Plugin> ((Plugin*) 0);
1441 }
1442
1443 void
1444 PluginInsert::set_input_map (uint32_t num, ChanMapping m) {
1445         if (num < _in_map.size()) {
1446                 bool changed = _in_map[num] != m;
1447                 _in_map[num] = m;
1448                 changed |= sanitize_maps ();
1449                 if (changed) {
1450                         PluginMapChanged (); /* EMIT SIGNAL */
1451                         _mapping_changed = true;
1452                         _session.set_dirty();
1453                 }
1454         }
1455 }
1456
1457 void
1458 PluginInsert::set_output_map (uint32_t num, ChanMapping m) {
1459         if (num < _out_map.size()) {
1460                 bool changed = _out_map[num] != m;
1461                 _out_map[num] = m;
1462                 changed |= sanitize_maps ();
1463                 if (changed) {
1464                         PluginMapChanged (); /* EMIT SIGNAL */
1465                         _mapping_changed = true;
1466                         _session.set_dirty();
1467                 }
1468         }
1469 }
1470
1471 void
1472 PluginInsert::set_thru_map (ChanMapping m) {
1473         bool changed = _thru_map != m;
1474         _thru_map = m;
1475         changed |= sanitize_maps ();
1476         if (changed) {
1477                 PluginMapChanged (); /* EMIT SIGNAL */
1478                 _mapping_changed = true;
1479                 _session.set_dirty();
1480         }
1481 }
1482
1483 bool
1484 PluginInsert::pre_seed (const ChanCount& in, const ChanCount& out,
1485                 const ChanMapping& im, const ChanMapping& om, const ChanMapping& tm)
1486 {
1487         if (_configured) { return false; }
1488         _configured_in = in;
1489         _configured_out = out;
1490         _in_map[0] = im;
1491         _out_map[0] = om;
1492         _thru_map = tm;
1493         _maps_from_state = in.n_total () > 0 && out.n_total () > 0;
1494         return true;
1495 }
1496
1497 ChanMapping
1498 PluginInsert::input_map () const
1499 {
1500         ChanMapping rv;
1501         uint32_t pc = 0;
1502         for (PinMappings::const_iterator i = _in_map.begin (); i != _in_map.end (); ++i, ++pc) {
1503                 ChanMapping m (i->second);
1504                 const ChanMapping::Mappings& mp ((*i).second.mappings());
1505                 for (ChanMapping::Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
1506                         for (ChanMapping::TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
1507                                 rv.set (tm->first, i->first + pc * natural_input_streams().get(tm->first), i->second);
1508                         }
1509                 }
1510         }
1511         return rv;
1512 }
1513
1514
1515 ChanMapping
1516 PluginInsert::no_sc_input_map () const
1517 {
1518         ChanMapping rv;
1519         uint32_t pc = 0;
1520         for (PinMappings::const_iterator i = _in_map.begin (); i != _in_map.end (); ++i, ++pc) {
1521                 ChanMapping m (i->second);
1522                 const ChanMapping::Mappings& mp ((*i).second.mappings());
1523                 for (ChanMapping::Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
1524                         uint32_t ins = natural_input_streams().get(tm->first) - _cached_sidechain_pins.get(tm->first);
1525                         for (ChanMapping::TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
1526                                 if (i->first < ins) {
1527                                         rv.set (tm->first, i->first + pc * ins, i->second);
1528                                 }
1529                         }
1530                 }
1531         }
1532         if (has_midi_thru ()) {
1533                 rv.set (DataType::MIDI, 0, 0);
1534         }
1535         return rv;
1536 }
1537
1538 ChanMapping
1539 PluginInsert::output_map () const
1540 {
1541         ChanMapping rv;
1542         uint32_t pc = 0;
1543         for (PinMappings::const_iterator i = _out_map.begin (); i != _out_map.end (); ++i, ++pc) {
1544                 ChanMapping m (i->second);
1545                 const ChanMapping::Mappings& mp ((*i).second.mappings());
1546                 for (ChanMapping::Mappings::const_iterator tm = mp.begin(); tm != mp.end(); ++tm) {
1547                         for (ChanMapping::TypeMapping::const_iterator i = tm->second.begin(); i != tm->second.end(); ++i) {
1548                                 rv.set (tm->first, i->first + pc * natural_output_streams().get(tm->first), i->second);
1549                         }
1550                 }
1551         }
1552         if (has_midi_bypass ()) {
1553                 rv.set (DataType::MIDI, 0, 0);
1554         }
1555
1556         return rv;
1557 }
1558
1559 bool
1560 PluginInsert::has_midi_bypass () const
1561 {
1562         if (_configured_in.n_midi () == 1 && _configured_out.n_midi () == 1
1563                         && natural_output_streams ().n_midi () == 0) {
1564                 return true;
1565         }
1566         return false;
1567 }
1568
1569 bool
1570 PluginInsert::has_midi_thru () const
1571 {
1572         if (_configured_in.n_midi () == 1 && _configured_out.n_midi () == 1
1573                         && natural_input_streams ().n_midi () == 0 && natural_output_streams ().n_midi () == 0) {
1574                 return true;
1575         }
1576         return false;
1577 }
1578
1579 bool
1580 PluginInsert::is_channelstrip () const
1581 {
1582 #ifdef MIXBUS
1583         return _plugins.front()->is_channelstrip();
1584 #else
1585         return false;
1586 #endif
1587 }
1588
1589 bool
1590 PluginInsert::is_nonbypassable () const
1591 {
1592 #ifdef MIXBUS
1593         return _plugins.front()->is_nonbypassable ();
1594 #else
1595         return false;
1596 #endif
1597 }
1598
1599 bool
1600 PluginInsert::show_on_ctrl_surface () const
1601 {
1602         if (is_channelstrip () || !is_nonbypassable ()) {
1603                 return false;
1604         }
1605         return true;
1606 }
1607
1608 bool
1609 PluginInsert::check_inplace ()
1610 {
1611         bool inplace_ok = !_plugins.front()->inplace_broken ();
1612
1613         if (_thru_map.n_total () > 0) {
1614                 // TODO once midi-bypass is part of the mapping, ignore it
1615                 inplace_ok = false;
1616         }
1617
1618         if (_match.method == Split && inplace_ok) {
1619                 assert (get_count() == 1);
1620                 assert (_in_map.size () == 1);
1621                 if (!_out_map[0].is_monotonic ()) {
1622                         inplace_ok = false;
1623                 }
1624                 if (_configured_internal != _configured_in) {
1625                         /* no sidechain -- TODO we could allow this with
1626                          * some more logic in PluginInsert::connect_and_run().
1627                          *
1628                          * PluginInsert::reset_map() already maps it.
1629                          */
1630                         inplace_ok = false;
1631                 }
1632                 /* check mapping */
1633                 for (DataType::iterator t = DataType::begin(); t != DataType::end() && inplace_ok; ++t) {
1634                         if (_configured_internal.get (*t) == 0) {
1635                                 continue;
1636                         }
1637                         bool valid;
1638                         uint32_t first_idx = _in_map[0].get (*t, 0, &valid);
1639                         if (!valid || first_idx != 0) {
1640                                 // so far only allow to copy the *first* stream's buffer to others
1641                                 inplace_ok = false;
1642                         } else {
1643                                 for (uint32_t i = 1; i < natural_input_streams ().get (*t); ++i) {
1644                                         uint32_t idx = _in_map[0].get (*t, i, &valid);
1645                                         if (valid && idx != first_idx) {
1646                                                 inplace_ok = false;
1647                                                 break;
1648                                         }
1649                                 }
1650                         }
1651                 }
1652
1653                 if (inplace_ok) {
1654                         DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: In Place Split Map\n", name()));
1655                         return false;
1656                 }
1657         }
1658
1659         for (uint32_t pc = 0; pc < get_count() && inplace_ok ; ++pc) {
1660                 if (!_in_map[pc].is_monotonic ()) {
1661                         inplace_ok = false;
1662                 }
1663                 if (!_out_map[pc].is_monotonic ()) {
1664                         inplace_ok = false;
1665                 }
1666         }
1667
1668         if (inplace_ok) {
1669                 /* check if every output is fed by the corresponding input
1670                  *
1671                  * this prevents  in-port 1 -> sink-pin 2  ||  source-pin 1 -> out port 1, source-pin 2 -> out port 2
1672                  * (with in-place,  source-pin 1 -> out port 1 overwrites in-port 1)
1673                  *
1674                  * but allows     in-port 1 -> sink-pin 2  ||  source-pin 2 -> out port 1
1675                  */
1676                 ChanMapping const& in_map (input_map ());
1677                 const ChanMapping::Mappings out_m (output_map ().mappings ());
1678                 for (ChanMapping::Mappings::const_iterator t = out_m.begin (); t != out_m.end () && inplace_ok; ++t) {
1679                         for (ChanMapping::TypeMapping::const_iterator c = (*t).second.begin (); c != (*t).second.end () ; ++c) {
1680                                 /* src-pin: c->first, out-port: c->second */
1681                                 bool valid;
1682                                 uint32_t in_port = in_map.get (t->first, c->first, &valid);
1683                                 if (valid && in_port != c->second) {
1684                                         inplace_ok = false;
1685                                         break;
1686                                 }
1687                         }
1688                 }
1689         }
1690
1691         DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: %2\n", name(), inplace_ok ? "In-Place" : "No Inplace Processing"));
1692         return !inplace_ok; // no-inplace
1693 }
1694
1695 bool
1696 PluginInsert::sanitize_maps ()
1697 {
1698         bool changed = false;
1699         /* strip dead wood */
1700         PinMappings new_ins;
1701         PinMappings new_outs;
1702         ChanMapping new_thru;
1703
1704         for (uint32_t pc = 0; pc < get_count(); ++pc) {
1705                 ChanMapping new_in;
1706                 ChanMapping new_out;
1707                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1708                         for (uint32_t i = 0; i < natural_input_streams().get (*t); ++i) {
1709                                 bool valid;
1710                                 uint32_t idx = _in_map[pc].get (*t, i, &valid);
1711                                 if (valid && idx < _configured_internal.get (*t)) {
1712                                         new_in.set (*t, i, idx);
1713                                 }
1714                         }
1715                         for (uint32_t o = 0; o < natural_output_streams().get (*t); ++o) {
1716                                 bool valid;
1717                                 uint32_t idx = _out_map[pc].get (*t, o, &valid);
1718                                 if (valid && idx < _configured_out.get (*t)) {
1719                                         new_out.set (*t, o, idx);
1720                                 }
1721                         }
1722                 }
1723                 if (_in_map[pc] != new_in || _out_map[pc] != new_out) {
1724                         changed = true;
1725                 }
1726                 new_ins[pc] = new_in;
1727                 new_outs[pc] = new_out;
1728         }
1729
1730         /* prevent dup output assignments */
1731         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1732                 for (uint32_t o = 0; o < _configured_out.get (*t); ++o) {
1733                         bool mapped = false;
1734                         for (uint32_t pc = 0; pc < get_count(); ++pc) {
1735                                 bool valid;
1736                                 uint32_t idx = new_outs[pc].get_src (*t, o, &valid);
1737                                 if (valid && mapped) {
1738                                         new_outs[pc].unset (*t, idx);
1739                                 } else if (valid) {
1740                                         mapped = true;
1741                                 }
1742                         }
1743                 }
1744         }
1745
1746         /* remove excess thru */
1747         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1748                 for (uint32_t o = 0; o < _configured_out.get (*t); ++o) {
1749                         bool valid;
1750                         uint32_t idx = _thru_map.get (*t, o, &valid);
1751                         if (valid && idx < _configured_internal.get (*t)) {
1752                                 new_thru.set (*t, o, idx);
1753                         }
1754                 }
1755         }
1756
1757         /* prevent out + thru,  existing plugin outputs override thru */
1758         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1759                 for (uint32_t o = 0; o < _configured_out.get (*t); ++o) {
1760                         bool mapped = false;
1761                         bool valid;
1762                         for (uint32_t pc = 0; pc < get_count(); ++pc) {
1763                                 new_outs[pc].get_src (*t, o, &mapped);
1764                                 if (mapped) { break; }
1765                         }
1766                         if (!mapped) { continue; }
1767                         uint32_t idx = new_thru.get (*t, o, &valid);
1768                         if (mapped) {
1769                                 new_thru.unset (*t, idx);
1770                         }
1771                 }
1772         }
1773
1774         if (has_midi_bypass ()) {
1775                 // TODO: include midi-bypass in the thru set,
1776                 // remove dedicated handling.
1777                 new_thru.unset (DataType::MIDI, 0);
1778         }
1779
1780         if (_in_map != new_ins || _out_map != new_outs || _thru_map != new_thru) {
1781                 changed = true;
1782         }
1783         _in_map = new_ins;
1784         _out_map = new_outs;
1785         _thru_map = new_thru;
1786
1787         return changed;
1788 }
1789
1790 bool
1791 PluginInsert::reset_map (bool emit)
1792 {
1793         const PinMappings old_in (_in_map);
1794         const PinMappings old_out (_out_map);
1795
1796         _in_map.clear ();
1797         _out_map.clear ();
1798         _thru_map = ChanMapping ();
1799
1800         /* build input map */
1801         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1802                 uint32_t sc = 0; // side-chain round-robin (all instances)
1803                 uint32_t pc = 0;
1804                 for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i, ++pc) {
1805                         const uint32_t nis = natural_input_streams ().get(*t);
1806                         const uint32_t stride = nis - sidechain_input_pins().get (*t);
1807
1808                         /* SC inputs are last in the plugin-insert.. */
1809                         const uint32_t sc_start = _configured_in.get (*t);
1810                         const uint32_t sc_len = _configured_internal.get (*t) - sc_start;
1811                         /* ...but may not be at the end of the plugin ports.
1812                          * in case the side-chain is not the last port, shift connections back.
1813                          * and connect to side-chain
1814                          */
1815                         uint32_t shift = 0;
1816                         uint32_t ic = 0; // split inputs
1817                         const uint32_t cend = _configured_in.get (*t);
1818
1819                         for (uint32_t in = 0; in < nis; ++in) {
1820                                 const Plugin::IOPortDescription& iod (_plugins[pc]->describe_io_port (*t, true, in));
1821                                 if (iod.is_sidechain) {
1822                                         /* connect sidechain sinks to sidechain inputs in round-robin fashion */
1823                                         if (sc_len > 0) {// side-chain may be hidden
1824                                                 _in_map[pc].set (*t, in, sc_start + sc);
1825                                                 sc = (sc + 1) % sc_len;
1826                                         }
1827                                         ++shift;
1828                                 } else {
1829                                         if (_match.method == Split) {
1830                                                 if (cend == 0) { continue; }
1831                                                 if (_strict_io && ic + stride * pc >= cend) {
1832                                                         break;
1833                                                 }
1834                                                 /* connect *no* sidechain sinks in round-robin fashion */
1835                                                 _in_map[pc].set (*t, in, ic + stride * pc);
1836                                                 if (_strict_io && (ic + 1) == cend) {
1837                                                         break;
1838                                                 }
1839                                                 ic = (ic + 1) % cend;
1840                                         } else {
1841                                                 uint32_t s = in - shift;
1842                                                 if (stride * pc + s < cend) {
1843                                                         _in_map[pc].set (*t, in, s + stride * pc);
1844                                                 }
1845                                         }
1846                                 }
1847                         }
1848                 }
1849         }
1850
1851         /* build output map */
1852         uint32_t pc = 0;
1853         for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i, ++pc) {
1854                 _out_map[pc] = ChanMapping (ChanCount::min (natural_output_streams(), _configured_out));
1855                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1856                         _out_map[pc].offset_to(*t, pc * natural_output_streams().get(*t));
1857                 }
1858         }
1859
1860         sanitize_maps ();
1861         if (old_in == _in_map && old_out == _out_map) {
1862                 return false;
1863         }
1864         if (emit) {
1865                 PluginMapChanged (); /* EMIT SIGNAL */
1866                 _mapping_changed = true;
1867                 _session.set_dirty();
1868         }
1869         return true;
1870 }
1871
1872 bool
1873 PluginInsert::configure_io (ChanCount in, ChanCount out)
1874 {
1875         Match old_match = _match;
1876         ChanCount old_in;
1877         ChanCount old_internal;
1878         ChanCount old_out;
1879         ChanCount old_pins;
1880
1881         old_pins = natural_input_streams();
1882         old_in = _configured_in;
1883         old_out = _configured_out;
1884         old_internal = _configured_internal;
1885
1886         _configured_in = in;
1887         _configured_internal = in;
1888         _configured_out = out;
1889
1890         if (_sidechain) {
1891                 /* TODO hide midi-bypass, and custom outs. Best /fake/ "out" here.
1892                  * (currently _sidechain->configure_io always succeeds
1893                  *  since Processor::configure_io() succeeds)
1894                  */
1895                 if (!_sidechain->configure_io (in, out)) {
1896                         DEBUG_TRACE (DEBUG::ChanMapping, "Sidechain configuration failed\n");
1897                         return false;
1898                 }
1899                 _configured_internal += _sidechain->input()->n_ports();
1900
1901                 // include (static_cast<Route*>owner())->name() ??
1902                 _sidechain->input ()-> set_pretty_name (string_compose (_("SC %1"), name ()));
1903         }
1904
1905         /* get plugin configuration */
1906         _match = private_can_support_io_configuration (in, out);
1907 #ifndef NDEBUG
1908         if (DEBUG_ENABLED(DEBUG::ChanMapping)) {
1909                 DEBUG_STR_DECL(a);
1910                 DEBUG_STR_APPEND(a, string_compose ("%1: ",  name()));
1911                 DEBUG_STR_APPEND(a, _match);
1912                 DEBUG_TRACE (DEBUG::ChanMapping, DEBUG_STR(a).str());
1913         }
1914 #endif
1915
1916         /* set the matching method and number of plugins that we will use to meet this configuration */
1917         if (set_count (_match.plugins) == false) {
1918                 PluginIoReConfigure (); /* EMIT SIGNAL */
1919                 _configured = false;
1920                 return false;
1921         }
1922
1923         /* configure plugins */
1924         switch (_match.method) {
1925         case Split:
1926         case Hide:
1927                 if (_plugins.front()->configure_io (natural_input_streams(), out) == false) {
1928                         PluginIoReConfigure (); /* EMIT SIGNAL */
1929                         _configured = false;
1930                         return false;
1931                 }
1932                 break;
1933         case Delegate:
1934                 {
1935                         ChanCount din (_configured_internal);
1936                         ChanCount dout (din); // hint
1937                         if (_custom_cfg) {
1938                                 if (_custom_sinks.n_total () > 0) {
1939                                         din = _custom_sinks;
1940                                 }
1941                                 dout = _custom_out;
1942                         } else if (_preset_out.n_audio () > 0) {
1943                                 dout.set (DataType::AUDIO, _preset_out.n_audio ());
1944                         } else if (dout.n_midi () > 0 && dout.n_audio () == 0) {
1945                                 dout.set (DataType::AUDIO, 2);
1946                         }
1947                         if (out.n_audio () == 0) { out.set (DataType::AUDIO, 1); }
1948                         ChanCount useins;
1949                         DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: Delegate lookup : %2 %3\n", name(), din, dout));
1950                         bool const r = _plugins.front()->can_support_io_configuration (din, dout, &useins);
1951                         assert (r);
1952                         if (useins.n_audio() == 0) {
1953                                 useins = din;
1954                         }
1955                         DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: Delegate configuration: %2 %3\n", name(), useins, dout));
1956
1957                         if (_plugins.front()->configure_io (useins, dout) == false) {
1958                                 PluginIoReConfigure (); /* EMIT SIGNAL */
1959                                 _configured = false;
1960                                 return false;
1961                         }
1962                         if (!_custom_cfg) {
1963                                 _custom_sinks = din;
1964                         }
1965                 }
1966                 break;
1967         default:
1968                 if (_plugins.front()->configure_io (in, out) == false) {
1969                         PluginIoReConfigure (); /* EMIT SIGNAL */
1970                         _configured = false;
1971                         return false;
1972                 }
1973                 break;
1974         }
1975
1976         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",
1977                                 name (),
1978                                 _configured ? "Y" : "N",
1979                                 _maps_from_state ? "Y" : "N",
1980                                 old_in == in ? "==" : "!=",
1981                                 old_out == out ? "==" : "!=",
1982                                 old_pins == natural_input_streams () ? "==" : "!=",
1983                                 old_match.method == _match.method ? "==" : "!=",
1984                                 old_match.custom_cfg == _match.custom_cfg ? "==" : "!=",
1985                                 _in_map.size() == get_count () ? "==" : "!=",
1986                                 _out_map.size() == get_count () ? "==" : "!="
1987                                 ));
1988
1989         bool mapping_changed = false;
1990         if (old_in == in && old_out == out
1991                         && _configured
1992                         && old_pins == natural_input_streams ()
1993                         && old_match.method == _match.method
1994                         && old_match.custom_cfg == _match.custom_cfg
1995                         && _in_map.size() == _out_map.size()
1996                         && _in_map.size() == get_count ()
1997                  ) {
1998                 /* If the configuration has not changed, keep the mapping */
1999                 mapping_changed = sanitize_maps ();
2000         } else if (_match.custom_cfg && _configured) {
2001                 /* don't touch the map in manual mode */
2002                 mapping_changed = sanitize_maps ();
2003         } else {
2004 #ifdef MIXBUS
2005                 if (is_channelstrip ()) {
2006                         /* fake channel map - for wire display */
2007                         _in_map.clear ();
2008                         _out_map.clear ();
2009                         _thru_map = ChanMapping ();
2010                         _in_map[0] = ChanMapping (ChanCount::min (_configured_in, ChanCount (DataType::AUDIO, 2)));
2011                         _out_map[0] = ChanMapping (ChanCount::min (_configured_out, ChanCount (DataType::AUDIO, 2)));
2012                         /* set "thru" map for in-place forward of audio */
2013                         for (uint32_t i = 2; i < _configured_in.n_audio(); ++i) {
2014                                 _thru_map.set (DataType::AUDIO, i, i);
2015                         }
2016                         /* and midi (after implicit 1st channel bypass) */
2017                         for (uint32_t i = 1; i < _configured_in.n_midi(); ++i) {
2018                                 _thru_map.set (DataType::MIDI, i, i);
2019                         }
2020                 } else
2021 #endif
2022                 if (_maps_from_state && old_in == in && old_out == out) {
2023                         mapping_changed = true;
2024                         sanitize_maps ();
2025                 } else {
2026                         /* generate a new mapping */
2027                         mapping_changed = reset_map (false);
2028                 }
2029                 _maps_from_state = false;
2030         }
2031
2032         if (mapping_changed) {
2033                 PluginMapChanged (); /* EMIT SIGNAL */
2034
2035 #ifndef NDEBUG
2036                 if (DEBUG_ENABLED(DEBUG::ChanMapping)) {
2037                         uint32_t pc = 0;
2038                         DEBUG_STR_DECL(a);
2039                         DEBUG_STR_APPEND(a, "\n--------<<--------\n");
2040                         for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i, ++pc) {
2041                                 if (pc > 0) {
2042                         DEBUG_STR_APPEND(a, "----><----\n");
2043                                 }
2044                                 DEBUG_STR_APPEND(a, string_compose ("Channel Map for %1 plugin %2\n", name(), pc));
2045                                 DEBUG_STR_APPEND(a, " * Inputs:\n");
2046                                 DEBUG_STR_APPEND(a, _in_map[pc]);
2047                                 DEBUG_STR_APPEND(a, " * Outputs:\n");
2048                                 DEBUG_STR_APPEND(a, _out_map[pc]);
2049                         }
2050                         DEBUG_STR_APPEND(a, " * Thru:\n");
2051                         DEBUG_STR_APPEND(a, _thru_map);
2052                         DEBUG_STR_APPEND(a, "-------->>--------\n");
2053                         DEBUG_TRACE (DEBUG::ChanMapping, DEBUG_STR(a).str());
2054                 }
2055 #endif
2056         }
2057
2058         _no_inplace = check_inplace ();
2059         _mapping_changed = false;
2060
2061         /* only the "noinplace_buffers" thread buffers need to be this large,
2062          * this can be optimized. other buffers are fine with
2063          * ChanCount::max (natural_input_streams (), natural_output_streams())
2064          * and route.cc's max (configured_in, configured_out)
2065          *
2066          * no-inplace copies "thru" outputs (to emulate in-place) for
2067          * all outputs (to prevent overwrite) into a temporary space
2068          * which also holds input buffers (in case the plugin does process
2069          * in-place and overwrites those).
2070          *
2071          * this buffers need to be at least as
2072          *   natural_input_streams () + possible outputs.
2073          *
2074          * sidechain inputs add a constraint on the input:
2075          * configured input + sidechain (=_configured_internal)
2076          *
2077          * NB. this also satisfies
2078          * max (natural_input_streams(), natural_output_streams())
2079          * which is needed for silence runs
2080          */
2081         _required_buffers = ChanCount::max (_configured_internal,
2082                         natural_input_streams () + ChanCount::max (_configured_out, natural_output_streams () * get_count ()));
2083
2084         if (old_in != in || old_out != out || old_internal != _configured_internal
2085                         || old_pins != natural_input_streams ()
2086                         || (old_match.method != _match.method && (old_match.method == Split || _match.method == Split))
2087                  ) {
2088                 PluginIoReConfigure (); /* EMIT SIGNAL */
2089         }
2090
2091         _delaybuffers.configure (_configured_out, _plugins.front ()->max_latency ());
2092         _latency_changed = true;
2093
2094         /* we don't know the analysis window size, so we must work with the
2095          * current buffer size here. each request for data fills in these
2096          * buffers and the analyser makes sure it gets enough data for the
2097          * analysis window. We also only analyze audio, so we can ignore
2098          * MIDI buffers.
2099          */
2100         ChanCount cc_analysis_in (DataType::AUDIO, in.n_audio());
2101         ChanCount cc_analysis_out (DataType::AUDIO, out.n_audio());
2102
2103         session().ensure_buffer_set (_signal_analysis_inputs, cc_analysis_in);
2104         _signal_analysis_inputs.set_count (cc_analysis_in);
2105
2106         session().ensure_buffer_set (_signal_analysis_outputs, cc_analysis_out);
2107         _signal_analysis_outputs.set_count (cc_analysis_out);
2108
2109         // std::cerr << "set counts to i" << in.n_audio() << "/o" << out.n_audio() << std::endl;
2110
2111         _configured = true;
2112         return Processor::configure_io (in, out);
2113 }
2114
2115 /** Decide whether this PluginInsert can support a given IO configuration.
2116  *  To do this, we run through a set of possible solutions in rough order of
2117  *  preference.
2118  *
2119  *  @param in Required input channel count.
2120  *  @param out Filled in with the output channel count if we return true.
2121  *  @return true if the given IO configuration can be supported.
2122  */
2123 bool
2124 PluginInsert::can_support_io_configuration (const ChanCount& in, ChanCount& out)
2125 {
2126         if (_sidechain) {
2127                 _sidechain->can_support_io_configuration (in, out); // never fails, sets "out"
2128         }
2129         return private_can_support_io_configuration (in, out).method != Impossible;
2130 }
2131
2132 PluginInsert::Match
2133 PluginInsert::private_can_support_io_configuration (ChanCount const& in, ChanCount& out) const
2134 {
2135         if (!_custom_cfg && _preset_out.n_audio () > 0) {
2136                 // preseed hint (for variable i/o)
2137                 out.set (DataType::AUDIO, _preset_out.n_audio ());
2138         }
2139
2140         Match rv = internal_can_support_io_configuration (in, out);
2141
2142         if (!_custom_cfg && _preset_out.n_audio () > 0) {
2143                 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: using output preset: %2\n", name(), _preset_out));
2144                 out.set (DataType::AUDIO, _preset_out.n_audio ());
2145         }
2146         return rv;
2147 }
2148
2149 /** A private version of can_support_io_configuration which returns the method
2150  *  by which the configuration can be matched, rather than just whether or not
2151  *  it can be.
2152  */
2153 PluginInsert::Match
2154 PluginInsert::internal_can_support_io_configuration (ChanCount const & inx, ChanCount& out) const
2155 {
2156         if (_plugins.empty()) {
2157                 return Match();
2158         }
2159
2160 #ifdef MIXBUS
2161         if (is_channelstrip ()) {
2162                 out = inx;
2163                 return Match (ExactMatch, 1);
2164         }
2165 #endif
2166
2167         /* if a user specified a custom cfg, so be it. */
2168         if (_custom_cfg) {
2169                 PluginInfoPtr info = _plugins.front()->get_info();
2170                 out = _custom_out;
2171                 if (info->reconfigurable_io()) {
2172                         return Match (Delegate, 1, _strict_io, true);
2173                 } else {
2174                         return Match (ExactMatch, get_count(), _strict_io, true);
2175                 }
2176         }
2177
2178         /* try automatic configuration */
2179         Match m = PluginInsert::automatic_can_support_io_configuration (inx, out);
2180
2181         PluginInfoPtr info = _plugins.front()->get_info();
2182         ChanCount inputs  = info->n_inputs;
2183         ChanCount outputs = info->n_outputs;
2184
2185         /* handle case strict-i/o */
2186         if (_strict_io && m.method != Impossible) {
2187                 m.strict_io = true;
2188
2189                 /* special case MIDI instruments */
2190                 if (is_instrument ()) {
2191                         // output = midi-bypass + at most master-out channels.
2192                         ChanCount max_out (DataType::AUDIO, 2); // TODO use master-out
2193                         max_out.set (DataType::MIDI, out.get(DataType::MIDI));
2194                         out = ChanCount::min (out, max_out);
2195                         DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: special case strict-i/o instrument\n", name()));
2196                         return m;
2197                 }
2198
2199                 switch (m.method) {
2200                         case NoInputs:
2201                                 if (inx.n_audio () != out.n_audio ()) { // ignore midi bypass
2202                                         /* replicate processor to match output count (generators and such)
2203                                          * at least enough to feed every output port. */
2204                                         uint32_t f = 1; // at least one. e.g. control data filters, no in, no out.
2205                                         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2206                                                 uint32_t nout = outputs.get (*t);
2207                                                 if (nout == 0 || inx.get(*t) == 0) { continue; }
2208                                                 f = max (f, (uint32_t) ceil (inx.get(*t) / (float)nout));
2209                                         }
2210                                         out = inx;
2211                                         DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: special case strict-i/o for generator\n", name()));
2212                                         return Match (Replicate, f, _strict_io);
2213                                 }
2214                                 break;
2215                         default:
2216                                 break;
2217                 }
2218
2219                 out = inx;
2220                 return m;
2221         }
2222
2223         if (m.method != Impossible) {
2224                 return m;
2225         }
2226
2227         ChanCount ns_inputs  = inputs - sidechain_input_pins ();
2228
2229         DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: resolving 'Impossible' match...\n", name()));
2230
2231         if (info->reconfigurable_io()) {
2232                 ChanCount useins;
2233                 out = inx; // hint
2234                 if (out.n_midi () > 0 && out.n_audio () == 0) { out.set (DataType::AUDIO, 2); }
2235                 if (out.n_audio () == 0) { out.set (DataType::AUDIO, 1); }
2236                 bool const r = _plugins.front()->can_support_io_configuration (inx + sidechain_input_pins (), out, &useins);
2237                 if (!r) {
2238                         // houston, we have a problem.
2239                         return Match (Impossible, 0);
2240                 }
2241                 // midi bypass
2242                 if (inx.n_midi () > 0 && out.n_midi () == 0) { out.set (DataType::MIDI, 1); }
2243                 return Match (Delegate, 1, _strict_io);
2244         }
2245
2246         ChanCount midi_bypass;
2247         if (inx.get(DataType::MIDI) == 1 && outputs.get(DataType::MIDI) == 0) {
2248                 midi_bypass.set (DataType::MIDI, 1);
2249         }
2250
2251         // add at least as many plugins so that output count matches input count (w/o sidechain pins)
2252         uint32_t f = 0;
2253         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2254                 uint32_t nin = ns_inputs.get (*t);
2255                 uint32_t nout = outputs.get (*t);
2256                 if (nin == 0 || inx.get(*t) == 0) { continue; }
2257                 // prefer floor() so the count won't overly increase IFF (nin < nout)
2258                 f = max (f, (uint32_t) floor (inx.get(*t) / (float)nout));
2259         }
2260         if (f > 0 && outputs * f >= _configured_out) {
2261                 out = outputs * f + midi_bypass;
2262                 return Match (Replicate, f, _strict_io);
2263         }
2264
2265         // add at least as many plugins needed to connect all inputs (w/o sidechain pins)
2266         f = 0;
2267         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2268                 uint32_t nin = ns_inputs.get (*t);
2269                 if (nin == 0 || inx.get(*t) == 0) { continue; }
2270                 f = max (f, (uint32_t) ceil (inx.get(*t) / (float)nin));
2271         }
2272         if (f > 0) {
2273                 out = outputs * f + midi_bypass;
2274                 return Match (Replicate, f, _strict_io);
2275         }
2276
2277         // add at least as many plugins needed to connect all inputs
2278         f = 1;
2279         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2280                 uint32_t nin = inputs.get (*t);
2281                 if (nin == 0 || inx.get(*t) == 0) { continue; }
2282                 f = max (f, (uint32_t) ceil (inx.get(*t) / (float)nin));
2283         }
2284         out = outputs * f + midi_bypass;
2285         return Match (Replicate, f, _strict_io);
2286 }
2287
2288 /* this is the original Ardour 3/4 behavior, mainly for backwards compatibility */
2289 PluginInsert::Match
2290 PluginInsert::automatic_can_support_io_configuration (ChanCount const & inx, ChanCount& out) const
2291 {
2292         if (_plugins.empty()) {
2293                 return Match();
2294         }
2295
2296         PluginInfoPtr info = _plugins.front()->get_info();
2297         ChanCount in; in += inx;
2298         ChanCount midi_bypass;
2299
2300         if (info->reconfigurable_io()) {
2301                 /* Plugin has flexible I/O, so delegate to it
2302                  * pre-seed outputs, plugin tries closest match
2303                  */
2304                 out = in; // hint
2305                 if (out.n_midi () > 0 && out.n_audio () == 0) { out.set (DataType::AUDIO, 2); }
2306                 if (out.n_audio () == 0) { out.set (DataType::AUDIO, 1); }
2307                 bool const r = _plugins.front()->can_support_io_configuration (in + sidechain_input_pins (), out);
2308                 if (!r) {
2309                         return Match (Impossible, 0);
2310                 }
2311                 // midi bypass
2312                 if (in.n_midi () > 0 && out.n_midi () == 0) { out.set (DataType::MIDI, 1); }
2313                 return Match (Delegate, 1);
2314         }
2315
2316         ChanCount inputs  = info->n_inputs;
2317         ChanCount outputs = info->n_outputs;
2318         ChanCount ns_inputs  = inputs - sidechain_input_pins ();
2319
2320         if (in.get(DataType::MIDI) == 1 && outputs.get(DataType::MIDI) == 0) {
2321                 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: bypassing midi-data\n", name()));
2322                 midi_bypass.set (DataType::MIDI, 1);
2323         }
2324         if (in.get(DataType::MIDI) == 1 && inputs.get(DataType::MIDI) == 0) {
2325                 DEBUG_TRACE (DEBUG::ChanMapping, string_compose ("%1: hiding midi-port from plugin\n", name()));
2326                 in.set(DataType::MIDI, 0);
2327         }
2328
2329         // add internally provided sidechain ports
2330         ChanCount insc = in + sidechain_input_ports ();
2331
2332         bool no_inputs = true;
2333         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2334                 if (inputs.get (*t) != 0) {
2335                         no_inputs = false;
2336                         break;
2337                 }
2338         }
2339
2340         if (no_inputs) {
2341                 /* no inputs so we can take any input configuration since we throw it away */
2342                 out = outputs + midi_bypass;
2343                 return Match (NoInputs, 1);
2344         }
2345
2346         /* Plugin inputs match requested inputs + side-chain-ports exactly */
2347         if (inputs == insc) {
2348                 out = outputs + midi_bypass;
2349                 return Match (ExactMatch, 1);
2350         }
2351
2352         /* Plugin inputs matches without side-chain-pins */
2353         if (ns_inputs == in) {
2354                 out = outputs + midi_bypass;
2355                 return Match (ExactMatch, 1);
2356         }
2357
2358         /* We may be able to run more than one copy of the plugin within this insert
2359            to cope with the insert having more inputs than the plugin.
2360            We allow replication only for plugins with either zero or 1 inputs and outputs
2361            for every valid data type.
2362         */
2363
2364         uint32_t f             = 0;
2365         bool     can_replicate = true;
2366         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2367
2368                 // ignore side-chains
2369                 uint32_t nin = ns_inputs.get (*t);
2370
2371                 // No inputs of this type
2372                 if (nin == 0 && in.get(*t) == 0) {
2373                         continue;
2374                 }
2375
2376                 if (nin != 1 || outputs.get (*t) != 1) {
2377                         can_replicate = false;
2378                         break;
2379                 }
2380
2381                 // Potential factor not set yet
2382                 if (f == 0) {
2383                         f = in.get(*t) / nin;
2384                 }
2385
2386                 // Factor for this type does not match another type, can not replicate
2387                 if (f != (in.get(*t) / nin)) {
2388                         can_replicate = false;
2389                         break;
2390                 }
2391         }
2392
2393         if (can_replicate && f > 0) {
2394                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2395                         out.set (*t, outputs.get(*t) * f);
2396                 }
2397                 out += midi_bypass;
2398                 return Match (Replicate, f);
2399         }
2400
2401         /* If the processor has exactly one input of a given type, and
2402            the plugin has more, we can feed the single processor input
2403            to some or all of the plugin inputs.  This is rather
2404            special-case-y, but the 1-to-many case is by far the
2405            simplest.  How do I split thy 2 processor inputs to 3
2406            plugin inputs?  Let me count the ways ...
2407         */
2408
2409         bool can_split = true;
2410         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2411
2412                 bool const can_split_type = (in.get (*t) == 1 && ns_inputs.get (*t) > 1);
2413                 bool const nothing_to_do_for_type = (in.get (*t) == 0 && inputs.get (*t) == 0);
2414
2415                 if (!can_split_type && !nothing_to_do_for_type) {
2416                         can_split = false;
2417                 }
2418         }
2419
2420         if (can_split) {
2421                 out = outputs + midi_bypass;
2422                 return Match (Split, 1);
2423         }
2424
2425         /* If the plugin has more inputs than we want, we can `hide' some of them
2426            by feeding them silence.
2427         */
2428
2429         bool could_hide = false;
2430         bool cannot_hide = false;
2431         ChanCount hide_channels;
2432
2433         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
2434                 if (inputs.get(*t) > in.get(*t)) {
2435                         /* there is potential to hide, since the plugin has more inputs of type t than the insert */
2436                         hide_channels.set (*t, inputs.get(*t) - in.get(*t));
2437                         could_hide = true;
2438                 } else if (inputs.get(*t) < in.get(*t)) {
2439                         /* we definitely cannot hide, since the plugin has fewer inputs of type t than the insert */
2440                         cannot_hide = true;
2441                 }
2442         }
2443
2444         if (could_hide && !cannot_hide) {
2445                 out = outputs + midi_bypass;
2446                 return Match (Hide, 1, false, false, hide_channels);
2447         }
2448
2449         return Match (Impossible, 0);
2450 }
2451
2452
2453 XMLNode&
2454 PluginInsert::state ()
2455 {
2456         XMLNode& node = Processor::state ();
2457
2458         node.set_property("type", _plugins[0]->state_node_name());
2459         node.set_property("unique-id", _plugins[0]->unique_id());
2460         node.set_property("count", (uint32_t)_plugins.size());
2461
2462         /* remember actual i/o configuration (for later placeholder
2463          * in case the plugin goes missing) */
2464         node.add_child_nocopy (* _configured_in.state (X_("ConfiguredInput")));
2465         node.add_child_nocopy (* _custom_sinks.state (X_("CustomSinks")));
2466         node.add_child_nocopy (* _configured_out.state (X_("ConfiguredOutput")));
2467         node.add_child_nocopy (* _preset_out.state (X_("PresetOutput")));
2468
2469         /* save custom i/o config */
2470         node.set_property("custom", _custom_cfg);
2471         for (uint32_t pc = 0; pc < get_count(); ++pc) {
2472                 char tmp[128];
2473                 snprintf (tmp, sizeof(tmp), "InputMap-%d", pc);
2474                 node.add_child_nocopy (* _in_map[pc].state (tmp));
2475                 snprintf (tmp, sizeof(tmp), "OutputMap-%d", pc);
2476                 node.add_child_nocopy (* _out_map[pc].state (tmp));
2477         }
2478         node.add_child_nocopy (* _thru_map.state ("ThruMap"));
2479
2480         if (_sidechain) {
2481                 node.add_child_nocopy (_sidechain->get_state ());
2482         }
2483
2484         _plugins[0]->set_insert_id(this->id());
2485         node.add_child_nocopy (_plugins[0]->get_state());
2486
2487         for (Controls::iterator c = controls().begin(); c != controls().end(); ++c) {
2488                 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> ((*c).second);
2489                 if (ac) {
2490                         node.add_child_nocopy (ac->get_state());
2491                 }
2492         }
2493
2494         return node;
2495 }
2496
2497 void
2498 PluginInsert::set_control_ids (const XMLNode& node, int version)
2499 {
2500         const XMLNodeList& nlist = node.children();
2501         for (XMLNodeConstIterator iter = nlist.begin(); iter != nlist.end(); ++iter) {
2502                 if ((*iter)->name() != Controllable::xml_node_name) {
2503                         continue;
2504                 }
2505
2506                 uint32_t p = (uint32_t)-1;
2507 #ifdef LV2_SUPPORT
2508                 std::string str;
2509                 if ((*iter)->get_property (X_("symbol"), str)) {
2510                         boost::shared_ptr<LV2Plugin> lv2plugin = boost::dynamic_pointer_cast<LV2Plugin> (_plugins[0]);
2511                         if (lv2plugin) {
2512                                 p = lv2plugin->port_index(str.c_str());
2513                         }
2514                 }
2515 #endif
2516                 if (p == (uint32_t)-1) {
2517                         (*iter)->get_property (X_("parameter"), p);
2518                 }
2519
2520                 if (p == (uint32_t)-1) {
2521                         continue;
2522                 }
2523
2524                 /* this may create the new controllable */
2525                 boost::shared_ptr<Evoral::Control> c = control (Evoral::Parameter (PluginAutomation, 0, p));
2526
2527 #ifndef NO_PLUGIN_STATE
2528                 if (!c) {
2529                         continue;
2530                 }
2531                 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> (c);
2532                 if (ac) {
2533                         ac->set_state (**iter, version);
2534                 }
2535 #endif
2536         }
2537 }
2538
2539 void
2540 PluginInsert::update_control_values (const XMLNode& node, int version)
2541 {
2542         const XMLNodeList& nlist = node.children();
2543         for (XMLNodeConstIterator iter = nlist.begin(); iter != nlist.end(); ++iter) {
2544                 if ((*iter)->name() != Controllable::xml_node_name) {
2545                         continue;
2546                 }
2547
2548                 float val;
2549                 if (!(*iter)->get_property (X_("value"), val)) {
2550                         continue;
2551                 }
2552
2553                 uint32_t p = (uint32_t)-1;
2554 #ifdef LV2_SUPPORT
2555                 std::string str;
2556                 if ((*iter)->get_property (X_("symbol"), str)) {
2557                         boost::shared_ptr<LV2Plugin> lv2plugin = boost::dynamic_pointer_cast<LV2Plugin> (_plugins[0]);
2558                         if (lv2plugin) {
2559                                 p = lv2plugin->port_index(str.c_str());
2560                         }
2561                 }
2562 #endif
2563                 if (p == (uint32_t)-1) {
2564                         (*iter)->get_property (X_("parameter"), p);
2565                 }
2566
2567                 if (p == (uint32_t)-1) {
2568                         continue;
2569                 }
2570
2571                 /* lookup controllable */
2572                 boost::shared_ptr<Evoral::Control> c = control (Evoral::Parameter (PluginAutomation, 0, p), false);
2573                 if (!c) {
2574                         continue;
2575                 }
2576                 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> (c);
2577                 if (ac) {
2578                         ac->set_value (val, Controllable::NoGroup);
2579                 }
2580         }
2581 }
2582
2583 int
2584 PluginInsert::set_state(const XMLNode& node, int version)
2585 {
2586         XMLNodeList nlist = node.children();
2587         XMLNodeIterator niter;
2588         XMLPropertyList plist;
2589         ARDOUR::PluginType type;
2590
2591         std::string str;
2592         if (!node.get_property ("type", str)) {
2593                 error << _("XML node describing plugin is missing the `type' field") << endmsg;
2594                 return -1;
2595         }
2596
2597         if (str == X_("ladspa") || str == X_("Ladspa")) { /* handle old school sessions */
2598                 type = ARDOUR::LADSPA;
2599         } else if (str == X_("lv2")) {
2600                 type = ARDOUR::LV2;
2601         } else if (str == X_("windows-vst")) {
2602                 type = ARDOUR::Windows_VST;
2603         } else if (str == X_("lxvst")) {
2604                 type = ARDOUR::LXVST;
2605         } else if (str == X_("mac-vst")) {
2606                 type = ARDOUR::MacVST;
2607         } else if (str == X_("audiounit")) {
2608                 type = ARDOUR::AudioUnit;
2609         } else if (str == X_("luaproc")) {
2610                 type = ARDOUR::Lua;
2611         } else {
2612                 error << string_compose (_("unknown plugin type %1 in plugin insert state"), str) << endmsg;
2613                 return -1;
2614         }
2615
2616         XMLProperty const * prop = node.property ("unique-id");
2617
2618         if (prop == 0) {
2619 #ifdef WINDOWS_VST_SUPPORT
2620                 /* older sessions contain VST plugins with only an "id" field.  */
2621                 if (type == ARDOUR::Windows_VST) {
2622                         prop = node.property ("id");
2623                 }
2624 #endif
2625
2626 #ifdef LXVST_SUPPORT
2627                 /*There shouldn't be any older sessions with linuxVST support.. but anyway..*/
2628                 if (type == ARDOUR::LXVST) {
2629                         prop = node.property ("id");
2630                 }
2631 #endif
2632
2633                 /* recheck  */
2634
2635                 if (prop == 0) {
2636                         error << _("Plugin has no unique ID field") << endmsg;
2637                         return -1;
2638                 }
2639         }
2640
2641         boost::shared_ptr<Plugin> plugin = find_plugin (_session, prop->value(), type);
2642         bool any_vst = false;
2643
2644         /* treat VST plugins equivalent if they have the same uniqueID
2645          * allow to move sessions windows <> linux */
2646 #ifdef LXVST_SUPPORT
2647         if (plugin == 0 && (type == ARDOUR::Windows_VST || type == ARDOUR::MacVST)) {
2648                 type = ARDOUR::LXVST;
2649                 plugin = find_plugin (_session, prop->value(), type);
2650                 if (plugin) { any_vst = true; }
2651         }
2652 #endif
2653
2654 #ifdef WINDOWS_VST_SUPPORT
2655         if (plugin == 0 && (type == ARDOUR::LXVST || type == ARDOUR::MacVST)) {
2656                 type = ARDOUR::Windows_VST;
2657                 plugin = find_plugin (_session, prop->value(), type);
2658                 if (plugin) { any_vst = true; }
2659         }
2660 #endif
2661
2662 #ifdef MACVST_SUPPORT
2663         if (plugin == 0 && (type == ARDOUR::Windows_VST || type == ARDOUR::LXVST)) {
2664                 type = ARDOUR::MacVST;
2665                 plugin = find_plugin (_session, prop->value(), type);
2666                 if (plugin) { any_vst = true; }
2667         }
2668 #endif
2669
2670         if (plugin == 0 && type == ARDOUR::Lua) {
2671                 /* unique ID (sha1 of script) was not found,
2672                  * load the plugin from the serialized version in the
2673                  * session-file instead.
2674                  */
2675                 boost::shared_ptr<LuaProc> lp (new LuaProc (_session.engine(), _session, ""));
2676                 XMLNode *ls = node.child (lp->state_node_name().c_str());
2677                 if (ls && lp) {
2678                         lp->set_script_from_state (*ls);
2679                         plugin = lp;
2680                 }
2681         }
2682
2683         if (plugin == 0) {
2684                 error << string_compose(
2685                         _("Found a reference to a plugin (\"%1\") that is unknown.\n"
2686                           "Perhaps it was removed or moved since it was last used."),
2687                         prop->value())
2688                       << endmsg;
2689                 return -1;
2690         }
2691
2692         // The name of the PluginInsert comes from the plugin, nothing else
2693         _name = plugin->get_info()->name;
2694
2695         uint32_t count = 1;
2696
2697         // Processor::set_state() will set this, but too late
2698         // for it to be available when setting up plugin
2699         // state. We can't call Processor::set_state() until
2700         // the plugins themselves are created and added.
2701
2702         set_id (node);
2703
2704         if (_plugins.empty()) {
2705                 /* if we are adding the first plugin, we will need to set
2706                  * up automatable controls.
2707                 */
2708                 add_plugin (plugin);
2709                 create_automatable_parameters ();
2710                 set_control_ids (node, version);
2711         } else {
2712                 /* update controllable value only (copy plugin state) */
2713                 update_control_values (node, version);
2714         }
2715
2716         node.get_property ("count", count);
2717
2718         if (_plugins.size() != count) {
2719                 for (uint32_t n = 1; n < count; ++n) {
2720                         add_plugin (plugin_factory (plugin));
2721                 }
2722         }
2723
2724         Processor::set_state (node, version);
2725
2726         PBD::ID new_id = this->id();
2727         PBD::ID old_id = this->id();
2728
2729         node.get_property ("id", old_id);
2730
2731         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2732
2733                 /* find the node with the type-specific node name ("lv2", "ladspa", etc)
2734                    and set all plugins to the same state.
2735                 */
2736
2737                 if (   ((*niter)->name() == plugin->state_node_name())
2738                     || (any_vst && ((*niter)->name() == "lxvst" || (*niter)->name() == "windows-vst" || (*niter)->name() == "mac-vst"))
2739                    ) {
2740
2741                         for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
2742                                 /* Plugin state can include external files which are named after the ID.
2743                                  *
2744                                  * If regenerate_xml_or_string_ids() is set, the ID will already have
2745                                  * been changed, so we need to use the old ID from the XML to load the
2746                                  * state and then update the ID.
2747                                  *
2748                                  * When copying a plugin-state, route_ui takes care of of updating the ID,
2749                                  * but we need to call set_insert_id() to clear the cached plugin-state
2750                                  * and force a change.
2751                                  */
2752                                 if (!regenerate_xml_or_string_ids ()) {
2753                                         (*i)->set_insert_id (new_id);
2754                                 } else {
2755                                         (*i)->set_insert_id (old_id);
2756                                 }
2757
2758                                 (*i)->set_state (**niter, version);
2759
2760                                 if (regenerate_xml_or_string_ids ()) {
2761                                         (*i)->set_insert_id (new_id);
2762                                 }
2763                         }
2764
2765                         /* when copying plugin state, notify UI */
2766                         for (Controls::const_iterator li = controls().begin(); li != controls().end(); ++li) {
2767                                 boost::shared_ptr<PBD::Controllable> c = boost::dynamic_pointer_cast<PBD::Controllable> (li->second);
2768                                 if (c) {
2769                                         c->Changed (false, Controllable::NoGroup); /* EMIT SIGNAL */
2770                                 }
2771                         }
2772
2773                         break;
2774                 }
2775         }
2776
2777         if (version < 3000) {
2778
2779                 /* Only 2.X sessions need a call to set_parameter_state() - in 3.X and above
2780                    this is all handled by Automatable
2781                 */
2782
2783                 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2784                         if ((*niter)->name() == "Redirect") {
2785                                 /* XXX do we need to tackle placement? i think not (pd; oct 16 2009) */
2786                                 Processor::set_state (**niter, version);
2787                                 break;
2788                         }
2789                 }
2790
2791                 set_parameter_state_2X (node, version);
2792         }
2793
2794         node.get_property (X_("custom"), _custom_cfg);
2795
2796         uint32_t in_maps = 0;
2797         uint32_t out_maps = 0;
2798         XMLNodeList kids = node.children ();
2799         for (XMLNodeIterator i = kids.begin(); i != kids.end(); ++i) {
2800                 if ((*i)->name() == X_("ConfiguredInput")) {
2801                         _configured_in = ChanCount(**i);
2802                 }
2803                 if ((*i)->name() == X_("CustomSinks")) {
2804                         _custom_sinks = ChanCount(**i);
2805                 }
2806                 if ((*i)->name() == X_("ConfiguredOutput")) {
2807                         _custom_out = ChanCount(**i);
2808                         _configured_out = ChanCount(**i);
2809                 }
2810                 if ((*i)->name() == X_("PresetOutput")) {
2811                         _preset_out = ChanCount(**i);
2812                 }
2813                 if (strncmp ((*i)->name ().c_str(), X_("InputMap-"), 9) == 0) {
2814                         long pc = atol (&((*i)->name().c_str()[9]));
2815                         if (pc >= 0 && pc <= (long) get_count()) {
2816                                 _in_map[pc] = ChanMapping (**i);
2817                                 ++in_maps;
2818                         }
2819                 }
2820                 if (strncmp ((*i)->name ().c_str(), X_("OutputMap-"), 10) == 0) {
2821                         long pc = atol (&((*i)->name().c_str()[10]));
2822                         if (pc >= 0 && pc <= (long) get_count()) {
2823                                 _out_map[pc] = ChanMapping (**i);
2824                                 ++out_maps;
2825                         }
2826                 }
2827                 if ((*i)->name () ==  "ThruMap") {
2828                                 _thru_map = ChanMapping (**i);
2829                 }
2830
2831                 // sidechain is a Processor (IO)
2832                 if ((*i)->name () ==  Processor::state_node_name) {
2833                         if (!_sidechain) {
2834                                 if (regenerate_xml_or_string_ids ()) {
2835                                         add_sidechain_from_xml (**i, version);
2836                                 } else {
2837                                         add_sidechain (0);
2838                                 }
2839                         }
2840                         if (!regenerate_xml_or_string_ids ()) {
2841                                 _sidechain->set_state (**i, version);
2842                         } else {
2843                                 update_sidechain_name ();
2844                         }
2845                 }
2846         }
2847
2848         if (in_maps == out_maps && out_maps >0 && out_maps == get_count()) {
2849                 _maps_from_state = true;
2850         }
2851
2852         for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
2853                 if (active()) {
2854                         (*i)->activate ();
2855                 } else {
2856                         (*i)->deactivate ();
2857                 }
2858         }
2859
2860         PluginConfigChanged (); /* EMIT SIGNAL */
2861         return 0;
2862 }
2863
2864 void
2865 PluginInsert::update_id (PBD::ID id)
2866 {
2867         set_id (id.to_s());
2868         for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
2869                 (*i)->set_insert_id (id);
2870         }
2871 }
2872
2873 void
2874 PluginInsert::set_owner (SessionObject* o)
2875 {
2876         Processor::set_owner (o);
2877         for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
2878                 (*i)->set_owner (o);
2879         }
2880 }
2881
2882 void
2883 PluginInsert::set_state_dir (const std::string& d)
2884 {
2885         // state() only saves the state of the first plugin
2886         _plugins[0]->set_state_dir (d);
2887 }
2888
2889 void
2890 PluginInsert::set_parameter_state_2X (const XMLNode& node, int version)
2891 {
2892         XMLNodeList nlist = node.children();
2893         XMLNodeIterator niter;
2894
2895         /* look for port automation node */
2896
2897         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2898
2899                 if ((*niter)->name() != port_automation_node_name) {
2900                         continue;
2901                 }
2902
2903                 XMLNodeList cnodes;
2904                 XMLNodeConstIterator iter;
2905                 XMLNode *child;
2906                 uint32_t port_id;
2907
2908                 cnodes = (*niter)->children ("port");
2909
2910                 for (iter = cnodes.begin(); iter != cnodes.end(); ++iter){
2911
2912                         child = *iter;
2913
2914                         if (!child->get_property("number", port_id)) {
2915                                 warning << _("PluginInsert: Auto: no ladspa port number") << endmsg;
2916                                 continue;
2917                         }
2918
2919                         if (port_id >= _plugins[0]->parameter_count()) {
2920                                 warning << _("PluginInsert: Auto: port id out of range") << endmsg;
2921                                 continue;
2922                         }
2923
2924                         boost::shared_ptr<AutomationControl> c = boost::dynamic_pointer_cast<AutomationControl>(
2925                                         control(Evoral::Parameter(PluginAutomation, 0, port_id), true));
2926
2927                         if (c && c->alist()) {
2928                                 if (!child->children().empty()) {
2929                                         c->alist()->set_state (*child->children().front(), version);
2930                                 }
2931                         } else {
2932                                 error << string_compose (_("PluginInsert: automatable control %1 not found - ignored"), port_id) << endmsg;
2933                         }
2934                 }
2935
2936                 /* done */
2937
2938                 break;
2939         }
2940 }
2941
2942 boost::shared_ptr<ReadOnlyControl>
2943 PluginInsert::control_output (uint32_t num) const
2944 {
2945         CtrlOutMap::const_iterator i = _control_outputs.find (num);
2946         if (i == _control_outputs.end ()) {
2947                 return boost::shared_ptr<ReadOnlyControl> ();
2948         } else {
2949                 return (*i).second;
2950         }
2951 }
2952
2953 string
2954 PluginInsert::describe_parameter (Evoral::Parameter param)
2955 {
2956         if (param.type() == PluginAutomation) {
2957                 return _plugins[0]->describe_parameter (param);
2958         } else if (param.type() == PluginPropertyAutomation) {
2959                 boost::shared_ptr<AutomationControl> c(automation_control(param));
2960                 if (c && !c->desc().label.empty()) {
2961                         return c->desc().label;
2962                 }
2963         }
2964         return Automatable::describe_parameter(param);
2965 }
2966
2967 ARDOUR::samplecnt_t
2968 PluginInsert::signal_latency() const
2969 {
2970         if (!_pending_active) {
2971                 return 0;
2972         }
2973         return plugin_latency ();
2974 }
2975
2976 ARDOUR::PluginType
2977 PluginInsert::type ()
2978 {
2979        return plugin()->get_info()->type;
2980 }
2981
2982 PluginInsert::PluginControl::PluginControl (PluginInsert*                     p,
2983                                             const Evoral::Parameter&          param,
2984                                             const ParameterDescriptor&        desc,
2985                                             boost::shared_ptr<AutomationList> list)
2986         : AutomationControl (p->session(), param, desc, list, p->describe_parameter(param))
2987         , _plugin (p)
2988 {
2989         if (alist()) {
2990                 if (desc.toggled) {
2991                         list->set_interpolation(Evoral::ControlList::Discrete);
2992                 }
2993         }
2994 }
2995
2996 /** @param val `user' value */
2997
2998 void
2999 PluginInsert::PluginControl::actually_set_value (double user_val, PBD::Controllable::GroupControlDisposition group_override)
3000 {
3001         /* FIXME: probably should be taking out some lock here.. */
3002
3003         for (Plugins::iterator i = _plugin->_plugins.begin(); i != _plugin->_plugins.end(); ++i) {
3004                 (*i)->set_parameter (_list->parameter().id(), user_val);
3005         }
3006
3007         boost::shared_ptr<Plugin> iasp = _plugin->_impulseAnalysisPlugin.lock();
3008         if (iasp) {
3009                 iasp->set_parameter (_list->parameter().id(), user_val);
3010         }
3011
3012         AutomationControl::actually_set_value (user_val, group_override);
3013 }
3014
3015 void
3016 PluginInsert::PluginControl::catch_up_with_external_value (double user_val)
3017 {
3018         AutomationControl::actually_set_value (user_val, Controllable::NoGroup);
3019 }
3020
3021 XMLNode&
3022 PluginInsert::PluginControl::get_state ()
3023 {
3024         XMLNode& node (AutomationControl::get_state());
3025         node.set_property (X_("parameter"), parameter().id());
3026 #ifdef LV2_SUPPORT
3027         boost::shared_ptr<LV2Plugin> lv2plugin = boost::dynamic_pointer_cast<LV2Plugin> (_plugin->_plugins[0]);
3028         if (lv2plugin) {
3029                 node.set_property (X_("symbol"), lv2plugin->port_symbol (parameter().id()));
3030         }
3031 #endif
3032
3033         return node;
3034 }
3035
3036 /** @return `user' val */
3037 double
3038 PluginInsert::PluginControl::get_value () const
3039 {
3040         boost::shared_ptr<Plugin> plugin = _plugin->plugin (0);
3041
3042         if (!plugin) {
3043                 return 0.0;
3044         }
3045
3046         return plugin->get_parameter (_list->parameter().id());
3047 }
3048
3049 std::string
3050 PluginInsert::PluginControl::get_user_string () const
3051 {
3052         boost::shared_ptr<Plugin> plugin = _plugin->plugin (0);
3053         if (plugin) {
3054                 char buf[32];
3055                 if (plugin->print_parameter (parameter().id(), buf, sizeof(buf))) {
3056                         assert (strlen (buf) > 0);
3057                         return std::string (buf) + " (" + AutomationControl::get_user_string () + ")";
3058                 }
3059         }
3060         return AutomationControl::get_user_string ();
3061 }
3062
3063 PluginInsert::PluginPropertyControl::PluginPropertyControl (PluginInsert*                     p,
3064                                                             const Evoral::Parameter&          param,
3065                                                             const ParameterDescriptor&        desc,
3066                                                             boost::shared_ptr<AutomationList> list)
3067         : AutomationControl (p->session(), param, desc, list)
3068         , _plugin (p)
3069 {
3070 }
3071
3072 void
3073 PluginInsert::PluginPropertyControl::actually_set_value (double user_val, Controllable::GroupControlDisposition gcd)
3074 {
3075         /* Old numeric set_value(), coerce to appropriate datatype if possible.
3076            This is lossy, but better than nothing until Ardour's automation system
3077            can handle various datatypes all the way down. */
3078         const Variant value(_desc.datatype, user_val);
3079         if (value.type() == Variant::NOTHING) {
3080                 error << "set_value(double) called for non-numeric property" << endmsg;
3081                 return;
3082         }
3083
3084         for (Plugins::iterator i = _plugin->_plugins.begin(); i != _plugin->_plugins.end(); ++i) {
3085                 (*i)->set_property(_list->parameter().id(), value);
3086         }
3087
3088         _value = value;
3089
3090         AutomationControl::actually_set_value (user_val, gcd);
3091 }
3092
3093 XMLNode&
3094 PluginInsert::PluginPropertyControl::get_state ()
3095 {
3096         XMLNode& node (AutomationControl::get_state());
3097         node.set_property (X_("property"), parameter().id());
3098         node.remove_property (X_("value"));
3099
3100         return node;
3101 }
3102
3103 double
3104 PluginInsert::PluginPropertyControl::get_value () const
3105 {
3106         return _value.to_double();
3107 }
3108
3109 boost::shared_ptr<Plugin>
3110 PluginInsert::get_impulse_analysis_plugin()
3111 {
3112         boost::shared_ptr<Plugin> ret;
3113         if (_impulseAnalysisPlugin.expired()) {
3114                 // LV2 in particular uses various _session params
3115                 // during init() -- most notably block_size..
3116                 // not great.
3117                 ret = plugin_factory(_plugins[0]);
3118                 ret->use_for_impulse_analysis ();
3119                 ChanCount out (internal_output_streams ());
3120                 if (ret->get_info ()->reconfigurable_io ()) {
3121                         // populate get_info ()->n_inputs and ->n_outputs
3122                         ChanCount useins;
3123                         ret->can_support_io_configuration (internal_input_streams (), out, &useins);
3124                         assert (out == internal_output_streams ());
3125                 }
3126                 ret->configure_io (internal_input_streams (), out);
3127                 ret->set_owner (_owner);
3128                 _impulseAnalysisPlugin = ret;
3129         } else {
3130                 ret = _impulseAnalysisPlugin.lock();
3131         }
3132
3133         return ret;
3134 }
3135
3136 void
3137 PluginInsert::collect_signal_for_analysis (samplecnt_t nframes)
3138 {
3139         if (_signal_analysis_collect_nsamples_max != 0
3140                         || _signal_analysis_collect_nsamples  != 0) {
3141                 return;
3142         }
3143
3144         // called from outside the audio thread, so this should be safe
3145         // only do audio as analysis is (currently) only for audio plugins
3146         _signal_analysis_inputs.ensure_buffers (DataType::AUDIO, input_streams().n_audio(),  nframes);
3147         _signal_analysis_outputs.ensure_buffers (DataType::AUDIO, output_streams().n_audio(), nframes);
3148
3149         /* these however should not be set while processing,
3150          * however in the given order, this should be fine.
3151          */
3152         _signal_analysis_collect_nsamples     = 0;
3153         _signal_analysis_collect_nsamples_max = nframes;
3154 }
3155
3156 /** Add a plugin to our list */
3157 void
3158 PluginInsert::add_plugin (boost::shared_ptr<Plugin> plugin)
3159 {
3160         plugin->set_insert_id (this->id());
3161         plugin->set_owner (_owner);
3162
3163         if (_plugins.empty()) {
3164                 /* first (and probably only) plugin instance - connect to relevant signals */
3165
3166                 plugin->ParameterChangedExternally.connect_same_thread (*this, boost::bind (&PluginInsert::parameter_changed_externally, this, _1, _2));
3167                 plugin->StartTouch.connect_same_thread (*this, boost::bind (&PluginInsert::start_touch, this, _1));
3168                 plugin->EndTouch.connect_same_thread (*this, boost::bind (&PluginInsert::end_touch, this, _1));
3169                 _custom_sinks = plugin->get_info()->n_inputs;
3170                 // cache sidechain port count
3171                 _cached_sidechain_pins.reset ();
3172                 const ChanCount& nis (plugin->get_info()->n_inputs);
3173                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
3174                         for (uint32_t in = 0; in < nis.get (*t); ++in) {
3175                                 const Plugin::IOPortDescription& iod (plugin->describe_io_port (*t, true, in));
3176                                 if (iod.is_sidechain) {
3177                                         _cached_sidechain_pins.set (*t, 1 + _cached_sidechain_pins.n(*t));
3178                                 }
3179                         }
3180                 }
3181         }
3182 #if (defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT || defined MACVST_SUPPORT)
3183         boost::shared_ptr<VSTPlugin> vst = boost::dynamic_pointer_cast<VSTPlugin> (plugin);
3184         if (vst) {
3185                 vst->set_insert (this, _plugins.size ());
3186         }
3187 #endif
3188
3189         _plugins.push_back (plugin);
3190 }
3191
3192 void
3193 PluginInsert::add_sidechain_from_xml (const XMLNode& node, int version)
3194 {
3195         if (version < 3000) {
3196                 return;
3197         }
3198
3199         XMLNodeList nlist = node.children();
3200
3201         if (nlist.size() == 0) {
3202                 return;
3203         }
3204
3205         uint32_t audio = 0;
3206         uint32_t midi = 0;
3207
3208         XMLNodeConstIterator it = nlist.front()->children().begin();
3209         for ( ; it != nlist.front()->children().end(); ++ it) {
3210                 if ((*it)->name() == "Port") {
3211                         DataType type(DataType::NIL);
3212                         (*it)->get_property ("type", type);
3213                         if (type == DataType::AUDIO) {
3214                                 ++audio;
3215                         } else if (type == DataType::MIDI) {
3216                                 ++midi;
3217                         }
3218                 }
3219         }
3220
3221         ChanCount in_cc = ChanCount();
3222         in_cc.set (DataType::AUDIO, audio);
3223         in_cc.set (DataType::MIDI, midi);
3224
3225         add_sidechain (audio, midi);
3226 }
3227
3228 bool
3229 PluginInsert::load_preset (ARDOUR::Plugin::PresetRecord pr)
3230 {
3231         bool ok = true;
3232         for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
3233                 if (! (*i)->load_preset (pr)) {
3234                         ok = false;
3235                 }
3236         }
3237
3238         boost::shared_ptr<Plugin> iasp = _impulseAnalysisPlugin.lock();
3239         if (iasp) {
3240                 iasp->load_preset (pr);
3241         }
3242
3243         return ok;
3244 }
3245
3246 void
3247 PluginInsert::realtime_handle_transport_stopped ()
3248 {
3249         for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
3250                 (*i)->realtime_handle_transport_stopped ();
3251         }
3252 }
3253
3254 void
3255 PluginInsert::realtime_locate ()
3256 {
3257         for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
3258                 (*i)->realtime_locate ();
3259         }
3260 }
3261
3262 void
3263 PluginInsert::monitoring_changed ()
3264 {
3265         for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
3266                 (*i)->monitoring_changed ();
3267         }
3268 }
3269
3270 void
3271 PluginInsert::latency_changed ()
3272 {
3273         // this is called in RT context, LatencyChanged is emitted after run()
3274         _latency_changed = true;
3275         LatencyChanged (); /* EMIT SIGNAL */
3276         // XXX This needs a proper API not an owner() hack:
3277         // TODO Route should subscribe to LatencyChanged() and forward it
3278         // to the session as processor_latency_changed.
3279         assert (owner ());
3280         static_cast<Route*>(owner ())->processor_latency_changed (); /* EMIT SIGNAL */
3281 }
3282
3283 void
3284 PluginInsert::start_touch (uint32_t param_id)
3285 {
3286         boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter (PluginAutomation, 0, param_id));
3287         if (ac) {
3288                 // ToDo subtract _plugin_signal_latency  from audible_sample() when rolling, assert > 0
3289                 ac->start_touch (session().audible_sample());
3290         }
3291 }
3292
3293 void
3294 PluginInsert::end_touch (uint32_t param_id)
3295 {
3296         boost::shared_ptr<AutomationControl> ac = automation_control (Evoral::Parameter (PluginAutomation, 0, param_id));
3297         if (ac) {
3298                 // ToDo subtract _plugin_signal_latency  from audible_sample() when rolling, assert > 0
3299                 ac->stop_touch (session().audible_sample());
3300         }
3301 }
3302
3303 bool
3304 PluginInsert::provides_stats () const
3305 {
3306 #if defined MIXBUS && defined NDEBUG
3307         if (is_channelstrip () || !display_to_user ()) {
3308                 return false;
3309         }
3310 #endif
3311         return true;
3312 }
3313
3314 bool
3315 PluginInsert::get_stats (uint64_t& min, uint64_t& max, double& avg, double& dev) const
3316 {
3317         /* TODO: consider taking a try/lock: Don't run concurrently with
3318          * TimingStats::update, TimingStats::reset.
3319          */
3320         return _timing_stats.get_stats (min, max, avg, dev);
3321 }
3322
3323 void
3324 PluginInsert::clear_stats ()
3325 {
3326         g_atomic_int_set (&_stat_reset, 1);
3327 }
3328
3329 std::ostream& operator<<(std::ostream& o, const ARDOUR::PluginInsert::Match& m)
3330 {
3331         switch (m.method) {
3332                 case PluginInsert::Impossible: o << "Impossible"; break;
3333                 case PluginInsert::Delegate:   o << "Delegate"; break;
3334                 case PluginInsert::NoInputs:   o << "NoInputs"; break;
3335                 case PluginInsert::ExactMatch: o << "ExactMatch"; break;
3336                 case PluginInsert::Replicate:  o << "Replicate"; break;
3337                 case PluginInsert::Split:      o << "Split"; break;
3338                 case PluginInsert::Hide:       o << "Hide"; break;
3339         }
3340         o << " cnt: " << m.plugins
3341                 << (m.strict_io ? " strict-io" : "")
3342                 << (m.custom_cfg ? " custom-cfg" : "");
3343         if (m.method == PluginInsert::Hide) {
3344                 o << " hide: " << m.hide;
3345         }
3346         o << "\n";
3347         return o;
3348 }