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