Tidy up PluginInsert's handling of how it chooses to map plugin IO to that of the...
[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/convert.h"
29
30 #include "ardour/audio_buffer.h"
31 #include "ardour/automation_list.h"
32 #include "ardour/buffer_set.h"
33 #include "ardour/event_type_map.h"
34 #include "ardour/ladspa_plugin.h"
35 #include "ardour/plugin.h"
36 #include "ardour/plugin_insert.h"
37 #include "ardour/port.h"
38 #include "ardour/route.h"
39
40 #ifdef LV2_SUPPORT
41 #include "ardour/lv2_plugin.h"
42 #endif
43
44 #ifdef VST_SUPPORT
45 #include "ardour/vst_plugin.h"
46 #endif
47
48 #ifdef LXVST_SUPPORT
49 #include "ardour/lxvst_plugin.h"
50 #endif
51
52 #ifdef HAVE_AUDIOUNITS
53 #include "ardour/audio_unit.h"
54 #endif
55
56 #include "ardour/audioengine.h"
57 #include "ardour/session.h"
58 #include "ardour/types.h"
59
60 #include "i18n.h"
61
62 using namespace std;
63 using namespace ARDOUR;
64 using namespace PBD;
65
66 const string PluginInsert::port_automation_node_name = "PortAutomation";
67
68 PluginInsert::PluginInsert (Session& s, boost::shared_ptr<Plugin> plug)
69         : Processor (s, (plug ? plug->name() : string ("toBeRenamed")))
70         , _signal_analysis_collected_nframes(0)
71         , _signal_analysis_collect_nframes_max(0)
72         , _matching_method (Impossible)
73 {
74         /* the first is the master */
75
76         if (plug) {
77                 add_plugin (plug);
78                 create_automatable_parameters ();
79
80                 Glib::Mutex::Lock em (_session.engine().process_lock());
81                 IO::PortCountChanged (max(input_streams(), output_streams()));
82         }
83 }
84
85 bool
86 PluginInsert::set_count (uint32_t num)
87 {
88         bool require_state = !_plugins.empty();
89
90         /* this is a bad idea.... we shouldn't do this while active.
91            only a route holding their redirect_lock should be calling this
92         */
93
94         if (num == 0) {
95                 return false;
96         } else if (num > _plugins.size()) {
97                 uint32_t diff = num - _plugins.size();
98
99                 for (uint32_t n = 0; n < diff; ++n) {
100                         add_plugin_with_activation (plugin_factory (_plugins[0]));
101
102                         if (require_state) {
103                                 /* XXX do something */
104                         }
105                 }
106
107         } else if (num < _plugins.size()) {
108                 uint32_t diff = _plugins.size() - num;
109                 for (uint32_t n= 0; n < diff; ++n) {
110                         _plugins.pop_back();
111                 }
112         }
113
114         return true;
115 }
116
117 PluginInsert::~PluginInsert ()
118 {
119 }
120
121 void
122 PluginInsert::control_list_automation_state_changed (Evoral::Parameter which, AutoState s)
123 {
124         if (which.type() != PluginAutomation)
125                 return;
126
127         boost::shared_ptr<AutomationControl> c
128                         = boost::dynamic_pointer_cast<AutomationControl>(control (which));
129
130         if (c && s != Off) {
131                 _plugins[0]->set_parameter (which.id(), c->list()->eval (_session.transport_frame()));
132         }
133 }
134
135 ChanCount
136 PluginInsert::output_streams() const
137 {
138         ChanCount out = _plugins.front()->get_info()->n_outputs;
139
140         if (out == ChanCount::INFINITE) {
141                 return _plugins.front()->output_streams ();
142         } else {
143                 out.set_audio (out.n_audio() * _plugins.size());
144                 out.set_midi (out.n_midi() * _plugins.size());
145                 return out;
146         }
147 }
148
149 ChanCount
150 PluginInsert::input_streams() const
151 {
152         ChanCount in = _plugins[0]->get_info()->n_inputs;
153
154         if (_matching_method == Split) {
155
156                 /* we are splitting 1 processor input to multiple plugin inputs,
157                    so we have a maximum of 1 stream of each type.
158                 */
159                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
160                         if (in.get (*t) > 1) {
161                                 in.set (*t, 1);
162                         }
163                 }
164                 return in;
165
166         } else if (in == ChanCount::INFINITE) {
167                 
168                 return _plugins[0]->input_streams ();
169                 
170         } else {
171                 
172                 in.set_audio (in.n_audio() * _plugins.size());
173                 in.set_midi (in.n_midi() * _plugins.size());
174                 return in;
175                 
176         }
177 }
178
179 ChanCount
180 PluginInsert::natural_output_streams() const
181 {
182         return _plugins[0]->get_info()->n_outputs;
183 }
184
185 ChanCount
186 PluginInsert::natural_input_streams() const
187 {
188         return _plugins[0]->get_info()->n_inputs;
189 }
190
191 bool
192 PluginInsert::has_no_inputs() const
193 {
194         return _plugins[0]->get_info()->n_inputs == ChanCount::ZERO;
195 }
196
197 bool
198 PluginInsert::has_no_audio_inputs() const
199 {
200         return _plugins[0]->get_info()->n_inputs.n_audio() == 0;
201 }
202
203 bool
204 PluginInsert::is_midi_instrument() const
205 {
206         /* XXX more finesse is possible here. VST plugins have a
207            a specific "instrument" flag, for example.
208          */
209         PluginInfoPtr pi = _plugins[0]->get_info();
210
211         return pi->n_inputs.n_midi() != 0 &&
212                 pi->n_outputs.n_audio() > 0;
213 }
214
215 void
216 PluginInsert::create_automatable_parameters ()
217 {
218         assert (!_plugins.empty());
219
220         set<Evoral::Parameter> a = _plugins.front()->automatable ();
221
222         Plugin::ParameterDescriptor desc;
223
224         for (set<Evoral::Parameter>::iterator i = a.begin(); i != a.end(); ++i) {
225                 if (i->type() == PluginAutomation) {
226
227                         Evoral::Parameter param(*i);
228
229                         _plugins.front()->get_parameter_descriptor(i->id(), desc);
230
231                         /* the Parameter belonging to the actual plugin doesn't have its range set
232                            but we want the Controllable related to this Parameter to have those limits.
233                         */
234
235                         param.set_range (desc.lower, desc.upper, _plugins.front()->default_value(i->id()), desc.toggled);
236                         can_automate (param);
237                         boost::shared_ptr<AutomationList> list(new AutomationList(param));
238                         add_control (boost::shared_ptr<AutomationControl> (new PluginControl(this, param, list)));
239                 }
240         }
241 }
242
243 void
244 PluginInsert::parameter_changed (Evoral::Parameter which, float val)
245 {
246         if (which.type() != PluginAutomation)
247                 return;
248
249         Plugins::iterator i = _plugins.begin();
250
251         /* don't set the first plugin, just all the slaves */
252
253         if (i != _plugins.end()) {
254                 ++i;
255                 for (; i != _plugins.end(); ++i) {
256                         (*i)->set_parameter (which, val);
257                 }
258         }
259 }
260
261 int
262 PluginInsert::set_block_size (pframes_t nframes)
263 {
264         int ret = 0;
265         for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
266                 if ((*i)->set_block_size (nframes) != 0) {
267                         ret = -1;
268                 }
269         }
270         return ret;
271 }
272
273 void
274 PluginInsert::activate ()
275 {
276         for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
277                 (*i)->activate ();
278         }
279
280         Processor::activate ();
281 }
282
283 void
284 PluginInsert::deactivate ()
285 {
286         Processor::deactivate ();
287
288         for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
289                 (*i)->deactivate ();
290         }
291 }
292
293 void
294 PluginInsert::flush ()
295 {
296         for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
297                 (*i)->flush ();
298         }
299 }
300
301 void
302 PluginInsert::connect_and_run (BufferSet& bufs, pframes_t nframes, framecnt_t offset, bool with_auto, framepos_t now)
303 {
304         // Calculate if, and how many frames we need to collect for analysis
305         framecnt_t collect_signal_nframes = (_signal_analysis_collect_nframes_max -
306                                              _signal_analysis_collected_nframes);
307         if (nframes < collect_signal_nframes) { // we might not get all frames now
308                 collect_signal_nframes = nframes;
309         }
310
311         ChanMapping in_map(input_streams());
312         ChanMapping out_map(output_streams());
313
314         if (_matching_method == Split) {
315                 /* fix the input mapping so that we have maps for each of the plugin's inputs */
316                 in_map = ChanMapping (natural_input_streams ());
317
318                 /* copy the first stream's buffer contents to the others */
319                 /* XXX: audio only */
320                 Sample const * mono = bufs.get_audio (in_map.get (DataType::AUDIO, 0)).data (offset);
321                 for (uint32_t i = input_streams().n_audio(); i < natural_input_streams().n_audio(); ++i) {
322                         memcpy (bufs.get_audio (in_map.get (DataType::AUDIO, i)).data (offset), mono, sizeof (Sample) * nframes);
323                 }
324         }
325
326         /* Note that we've already required that plugins
327            be able to handle in-place processing.
328         */
329
330         if (with_auto) {
331
332                 uint32_t n = 0;
333
334                 for (Controls::iterator li = controls().begin(); li != controls().end(); ++li, ++n) {
335
336                         boost::shared_ptr<AutomationControl> c
337                                 = boost::dynamic_pointer_cast<AutomationControl>(li->second);
338
339                         if (c->parameter().type() == PluginAutomation && c->automation_playback()) {
340                                 bool valid;
341
342                                 const float val = c->list()->rt_safe_eval (now, valid);
343
344                                 if (valid) {
345                                         c->set_value(val);
346                                 }
347
348                         }
349                 }
350         }
351
352         if (collect_signal_nframes > 0) {
353                 // collect input
354                 //std::cerr << "collect input, bufs " << bufs.count().n_audio() << " count,  " << bufs.available().n_audio() << " available" << std::endl;
355                 //std::cerr << "               streams " << input_streams().n_audio() << std::endl;
356                 //std::cerr << "filling buffer with " << collect_signal_nframes << " frames at " << _signal_analysis_collected_nframes << std::endl;
357
358                 _signal_analysis_inputs.set_count(input_streams());
359
360                 for (uint32_t i = 0; i < input_streams().n_audio(); ++i) {
361                         _signal_analysis_inputs.get_audio(i).read_from(
362                                 bufs.get_audio(i),
363                                 collect_signal_nframes,
364                                 _signal_analysis_collected_nframes); // offset is for target buffer
365                 }
366
367         }
368
369         for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
370                 (*i)->connect_and_run(bufs, in_map, out_map, nframes, offset);
371                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
372                         in_map.offset_to(*t, natural_input_streams().get(*t));
373                         out_map.offset_to(*t, natural_output_streams().get(*t));
374                 }
375         }
376
377         if (collect_signal_nframes > 0) {
378                 // collect output
379                 //std::cerr << "       output, bufs " << bufs.count().n_audio() << " count,  " << bufs.available().n_audio() << " available" << std::endl;
380                 //std::cerr << "               streams " << output_streams().n_audio() << std::endl;
381
382                 _signal_analysis_outputs.set_count(output_streams());
383
384                 for (uint32_t i = 0; i < output_streams().n_audio(); ++i) {
385                         _signal_analysis_outputs.get_audio(i).read_from(
386                                 bufs.get_audio(i),
387                                 collect_signal_nframes,
388                                 _signal_analysis_collected_nframes); // offset is for target buffer
389                 }
390
391                 _signal_analysis_collected_nframes += collect_signal_nframes;
392                 assert(_signal_analysis_collected_nframes <= _signal_analysis_collect_nframes_max);
393
394                 if (_signal_analysis_collected_nframes == _signal_analysis_collect_nframes_max) {
395                         _signal_analysis_collect_nframes_max = 0;
396                         _signal_analysis_collected_nframes   = 0;
397
398                         AnalysisDataGathered(&_signal_analysis_inputs,
399                                              &_signal_analysis_outputs);
400                 }
401         }
402         /* leave remaining channel buffers alone */
403 }
404
405 void
406 PluginInsert::silence (framecnt_t nframes)
407 {
408         if (!active ()) {
409                 return;
410         }
411
412         ChanMapping in_map(input_streams());
413         ChanMapping out_map(output_streams());
414
415         if (_matching_method == Split) {
416                 /* fix the input mapping so that we have maps for each of the plugin's inputs */
417                 in_map = ChanMapping (natural_input_streams ());
418         }
419
420         for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
421                 (*i)->connect_and_run (_session.get_silent_buffers ((*i)->get_info()->n_inputs), in_map, out_map, nframes, 0);
422         }
423 }
424
425 void
426 PluginInsert::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /*end_frame*/, pframes_t nframes, bool)
427 {
428         if (_pending_active) {
429                 /* run as normal if we are active or moving from inactive to active */
430
431                 if (_session.transport_rolling()) {
432                         automation_run (bufs, nframes);
433                 } else {
434                         connect_and_run (bufs, nframes, 0, false);
435                 }
436
437         } else {
438
439                 if (has_no_audio_inputs()) {
440
441                         /* silence all (audio) outputs. Should really declick
442                          * at the transitions of "active"
443                          */
444
445                         uint32_t out = _plugins[0]->get_info()->n_outputs.n_audio();
446
447                         for (uint32_t n = 0; n < out; ++n) {
448                                 bufs.get_audio (n).silence (nframes);
449                         }
450
451                         bufs.count().set_audio (out);
452
453                 } else {
454
455                         /* does this need to be done with MIDI? it appears not */
456
457                         uint32_t in = _plugins[0]->get_info()->n_inputs.n_audio();
458                         uint32_t out = _plugins[0]->get_info()->n_outputs.n_audio();
459
460                         if (out > in) {
461
462                                 /* not active, but something has make up for any channel count increase */
463
464                                 for (uint32_t n = out - in; n < out; ++n) {
465                                         memcpy (bufs.get_audio(n).data(), bufs.get_audio(in - 1).data(), sizeof (Sample) * nframes);
466                                 }
467                         }
468
469                         bufs.count().set_audio (out);
470                 }
471         }
472
473         _active = _pending_active;
474 }
475
476 void
477 PluginInsert::set_parameter (Evoral::Parameter param, float val)
478 {
479         if (param.type() != PluginAutomation) {
480                 return;
481         }
482
483         /* the others will be set from the event triggered by this */
484
485         _plugins[0]->set_parameter (param.id(), val);
486
487         boost::shared_ptr<AutomationControl> ac
488                         = boost::dynamic_pointer_cast<AutomationControl>(control(param));
489
490         if (ac) {
491                 ac->set_value(val);
492         } else {
493                 warning << "set_parameter called for nonexistant parameter "
494                         << EventTypeMap::instance().to_symbol(param) << endmsg;
495         }
496
497         _session.set_dirty();
498 }
499
500 float
501 PluginInsert::get_parameter (Evoral::Parameter param)
502 {
503         if (param.type() != PluginAutomation) {
504                 return 0.0;
505         } else {
506                 assert (!_plugins.empty ());
507                 return _plugins[0]->get_parameter (param.id());
508         }
509 }
510
511 void
512 PluginInsert::automation_run (BufferSet& bufs, pframes_t nframes)
513 {
514         Evoral::ControlEvent next_event (0, 0.0f);
515         framepos_t now = _session.transport_frame ();
516         framepos_t end = now + nframes;
517         framecnt_t offset = 0;
518
519         Glib::Mutex::Lock lm (control_lock(), Glib::TRY_LOCK);
520
521         if (!lm.locked()) {
522                 connect_and_run (bufs, nframes, offset, false);
523                 return;
524         }
525
526         if (!find_next_event (now, end, next_event) || requires_fixed_sized_buffers()) {
527
528                 /* no events have a time within the relevant range */
529
530                 connect_and_run (bufs, nframes, offset, true, now);
531                 return;
532         }
533
534         while (nframes) {
535
536                 framecnt_t cnt = min (((framecnt_t) ceil (next_event.when) - now), (framecnt_t) nframes);
537
538                 connect_and_run (bufs, cnt, offset, true, now);
539
540                 nframes -= cnt;
541                 offset += cnt;
542                 now += cnt;
543
544                 if (!find_next_event (now, end, next_event)) {
545                         break;
546                 }
547         }
548
549         /* cleanup anything that is left to do */
550
551         if (nframes) {
552                 connect_and_run (bufs, nframes, offset, true, now);
553         }
554 }
555
556 float
557 PluginInsert::default_parameter_value (const Evoral::Parameter& param)
558 {
559         if (param.type() != PluginAutomation)
560                 return 1.0;
561
562         if (_plugins.empty()) {
563                 fatal << _("programming error: ") << X_("PluginInsert::default_parameter_value() called with no plugin")
564                       << endmsg;
565                 /*NOTREACHED*/
566         }
567
568         return _plugins[0]->default_value (param.id());
569 }
570
571 boost::shared_ptr<Plugin>
572 PluginInsert::plugin_factory (boost::shared_ptr<Plugin> other)
573 {
574         boost::shared_ptr<LadspaPlugin> lp;
575 #ifdef LV2_SUPPORT
576         boost::shared_ptr<LV2Plugin> lv2p;
577 #endif
578 #ifdef VST_SUPPORT
579         boost::shared_ptr<VSTPlugin> vp;
580 #endif
581 #ifdef LXVST_SUPPORT
582         boost::shared_ptr<LXVSTPlugin> lxvp;
583 #endif
584 #ifdef HAVE_AUDIOUNITS
585         boost::shared_ptr<AUPlugin> ap;
586 #endif
587
588         if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
589                 return boost::shared_ptr<Plugin> (new LadspaPlugin (*lp));
590 #ifdef LV2_SUPPORT
591         } else if ((lv2p = boost::dynamic_pointer_cast<LV2Plugin> (other)) != 0) {
592                 return boost::shared_ptr<Plugin> (new LV2Plugin (*lv2p));
593 #endif
594 #ifdef VST_SUPPORT
595         } else if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (other)) != 0) {
596                 return boost::shared_ptr<Plugin> (new VSTPlugin (*vp));
597 #endif
598 #ifdef LXVST_SUPPORT
599         } else if ((lxvp = boost::dynamic_pointer_cast<LXVSTPlugin> (other)) != 0) {
600                 return boost::shared_ptr<Plugin> (new LXVSTPlugin (*lxvp));
601 #endif
602 #ifdef HAVE_AUDIOUNITS
603         } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
604                 return boost::shared_ptr<Plugin> (new AUPlugin (*ap));
605 #endif
606         }
607
608         fatal << string_compose (_("programming error: %1"),
609                           X_("unknown plugin type in PluginInsert::plugin_factory"))
610               << endmsg;
611         /*NOTREACHED*/
612         return boost::shared_ptr<Plugin> ((Plugin*) 0);
613 }
614
615 bool
616 PluginInsert::configure_io (ChanCount in, ChanCount out)
617 {
618         MatchingMethod old_matching_method = _matching_method;
619
620         /* set the matching method and number of plugins that we will use to meet this configuration */
621         pair<MatchingMethod, int32_t> const r = private_can_support_io_configuration (in, out);
622         _matching_method = r.first;
623         if (set_count (r.second) == false) {
624                 return false;
625         }
626
627         /* a signal needs emitting if we start or stop splitting */
628         if (old_matching_method != _matching_method && (old_matching_method == Split || _matching_method == Split)) {
629                 SplittingChanged (); /* EMIT SIGNAL */
630         }
631
632         /* configure plugins */
633         switch (_matching_method) {
634         case Split:
635                 if (_plugins.front()->configure_io (_plugins.front()->get_info()->n_inputs, out)) {
636                         return false;
637                 }
638                 break;
639
640         default:
641                 if (_plugins.front()->configure_io (in, out) == false) {
642                         return false;
643                 }
644                 break;
645         }
646
647         // we don't know the analysis window size, so we must work with the
648         // current buffer size here. each request for data fills in these
649         // buffers and the analyser makes sure it gets enough data for the
650         // analysis window
651         session().ensure_buffer_set (_signal_analysis_inputs, in);
652         //_signal_analysis_inputs.set_count (in);
653
654         session().ensure_buffer_set (_signal_analysis_outputs, out);
655         //_signal_analysis_outputs.set_count (out);
656
657         // std::cerr << "set counts to i" << in.n_audio() << "/o" << out.n_audio() << std::endl;
658
659         return Processor::configure_io (in, out);
660 }
661
662 /** Decide whether this PluginInsert can support a given IO configuration.
663  *  To do this, we run through a set of possible solutions in rough order of
664  *  preference.
665  *
666  *  @param in Required input channel count.
667  *  @param out Filled in with the output channel count if we return true.
668  *  @return true if the given IO configuration can be supported.
669  */
670 bool
671 PluginInsert::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
672 {
673         return private_can_support_io_configuration (in, out).first != Impossible;
674 }
675
676 /** A private version of can_support_io_configuration which returns the method
677  *  by which the configuration can be matched, rather than just whether or not
678  *  it can be.
679  */
680 pair<PluginInsert::MatchingMethod, int32_t>
681 PluginInsert::private_can_support_io_configuration (ChanCount const & in, ChanCount& out) const
682 {
683         if (_plugins.front()->reconfigurable_io()) {
684                 /* Plugin has flexible I/O, so delegate to it */
685                 bool const r = _plugins.front()->can_support_io_configuration (in, out);
686                 if (!r) {
687                         return make_pair (Impossible, 0);
688                 }
689
690                 return make_pair (Delegate, 1);
691         }
692
693         ChanCount inputs  = _plugins[0]->get_info()->n_inputs;
694         ChanCount outputs = _plugins[0]->get_info()->n_outputs;
695
696         bool no_inputs = true;
697         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
698                 if (inputs.get (*t) != 0) {
699                         no_inputs = false;
700                         break;
701                 }
702         }
703
704         if (no_inputs) {
705                 /* no inputs so we can take any input configuration since we throw it away */
706                 out = outputs;
707                 return make_pair (NoInputs, 1);
708         }
709
710         /* Plugin inputs match requested inputs exactly */
711         if (inputs == in) {
712                 out = outputs;
713                 return make_pair (ExactMatch, 1);
714         }
715
716         /* We may be able to run more than one copy of the plugin within this insert
717            to cope with the insert having more inputs than the plugin.
718            We allow replication only for plugins with either zero or 1 inputs and outputs
719            for every valid data type.
720         */
721         
722         uint32_t f             = 0;
723         bool     can_replicate = true;
724         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
725
726                 uint32_t nin = inputs.get (*t);
727
728                 // No inputs of this type
729                 if (nin == 0 && in.get(*t) == 0) {
730                         continue;
731                 }
732
733                 if (nin != 1 || outputs.get (*t) != 1) {
734                         can_replicate = false;
735                         break;
736                 }
737
738                 // Potential factor not set yet
739                 if (f == 0) {
740                         f = in.get(*t) / nin;
741                 }
742
743                 // Factor for this type does not match another type, can not replicate
744                 if (f != (in.get(*t) / nin)) {
745                         can_replicate = false;
746                         break;
747                 }
748         }
749
750         if (can_replicate) {
751                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
752                         out.set (*t, outputs.get(*t) * f);
753                 }
754                 return make_pair (Replicate, f);
755         }
756
757         /* If the processor has exactly one input of a given type, and
758            the plugin has more, we can feed the single processor input
759            to some or all of the plugin inputs.  This is rather
760            special-case-y, but the 1-to-many case is by far the
761            simplest.  How do I split thy 2 processor inputs to 3
762            plugin inputs?  Let me count the ways ...
763         */
764
765         bool can_split = true;
766         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
767
768                 bool const can_split_type = (in.get (*t) == 1 && inputs.get (*t) > 1);
769                 bool const nothing_to_do_for_type = (in.get (*t) == 0 && inputs.get (*t) == 0);
770
771                 if (!can_split_type && !nothing_to_do_for_type) {
772                         can_split = false;
773                 }
774         }
775
776         if (can_split) {
777                 out = outputs;
778                 return make_pair (Split, 1);
779         }
780
781         return make_pair (Impossible, 0);
782 }
783
784 XMLNode&
785 PluginInsert::get_state ()
786 {
787         return state (true);
788 }
789
790 XMLNode&
791 PluginInsert::state (bool full)
792 {
793         XMLNode& node = Processor::state (full);
794
795         node.add_property("type", _plugins[0]->state_node_name());
796         node.add_property("unique-id", _plugins[0]->unique_id());
797         node.add_property("count", string_compose("%1", _plugins.size()));
798         node.add_child_nocopy (_plugins[0]->get_state());
799
800         for (Controls::iterator c = controls().begin(); c != controls().end(); ++c) {
801                 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> ((*c).second);
802                 if (ac) {
803                         node.add_child_nocopy (ac->get_state());
804                 }
805         }
806
807         return node;
808 }
809
810 void
811 PluginInsert::set_control_ids (const XMLNode& node, int version)
812 {
813         const XMLNodeList& nlist = node.children();
814         XMLNodeConstIterator iter;
815         set<Evoral::Parameter>::const_iterator p;
816
817         for (iter = nlist.begin(); iter != nlist.end(); ++iter) {
818                 if ((*iter)->name() == Controllable::xml_node_name) {
819                         const XMLProperty* prop;
820
821                         if ((prop = (*iter)->property (X_("parameter"))) != 0) {
822                                 uint32_t p = atoi (prop->value());
823                                 boost::shared_ptr<Evoral::Control> c = control (Evoral::Parameter (PluginAutomation, 0, p));
824                                 if (!c) {
825                                         continue;
826                                 }
827                                 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> (c);
828                                 if (ac) {
829                                         ac->set_state (**iter, version);
830                                 }
831                         }
832                 }
833         }
834 }
835
836 int
837 PluginInsert::set_state(const XMLNode& node, int version)
838 {
839         XMLNodeList nlist = node.children();
840         XMLNodeIterator niter;
841         XMLPropertyList plist;
842         const XMLProperty *prop;
843         ARDOUR::PluginType type;
844
845         if ((prop = node.property ("type")) == 0) {
846                 error << _("XML node describing plugin is missing the `type' field") << endmsg;
847                 return -1;
848         }
849
850         if (prop->value() == X_("ladspa") || prop->value() == X_("Ladspa")) { /* handle old school sessions */
851                 type = ARDOUR::LADSPA;
852         } else if (prop->value() == X_("lv2")) {
853                 type = ARDOUR::LV2;
854         } else if (prop->value() == X_("vst")) {
855                 type = ARDOUR::VST;
856         } else if (prop->value() == X_("lxvst")) {
857                 type = ARDOUR::LXVST;
858         } else if (prop->value() == X_("audiounit")) {
859                 type = ARDOUR::AudioUnit;
860         } else {
861                 error << string_compose (_("unknown plugin type %1 in plugin insert state"),
862                                   prop->value())
863                       << endmsg;
864                 return -1;
865         }
866
867         prop = node.property ("unique-id");
868
869         if (prop == 0) {
870 #ifdef VST_SUPPORT
871                 /* older sessions contain VST plugins with only an "id" field.
872                  */
873
874                 if (type == ARDOUR::VST) {
875                         prop = node.property ("id");
876                 }
877 #endif
878
879 #ifdef LXVST_SUPPORT
880                 /*There shouldn't be any older sessions with linuxVST support.. but anyway..*/
881
882                 if (type == ARDOUR::LXVST) {
883                         prop = node.property ("id");
884                 }
885 #endif
886                 /* recheck  */
887
888                 if (prop == 0) {
889                         error << _("Plugin has no unique ID field") << endmsg;
890                         return -1;
891                 }
892         }
893
894         boost::shared_ptr<Plugin> plugin = find_plugin (_session, prop->value(), type);
895
896         if (plugin == 0) {
897                 error << string_compose(
898                         _("Found a reference to a plugin (\"%1\") that is unknown.\n"
899                           "Perhaps it was removed or moved since it was last used."),
900                         prop->value())
901                       << endmsg;
902                 return -1;
903         }
904
905         // The name of the PluginInsert comes from the plugin, nothing else
906         _name = plugin->get_info()->name;
907
908         uint32_t count = 1;
909
910         // Processor::set_state() will set this, but too late
911         // for it to be available when setting up plugin
912         // state. We can't call Processor::set_state() until
913         // the plugins themselves are created and added.
914
915         if ((prop = node.property ("id")) != 0) {
916                 _id = prop->value();
917         }
918
919         if (_plugins.empty()) {
920                 /* if we are adding the first plugin, we will need to set
921                    up automatable controls.
922                 */
923                 add_plugin (plugin);
924                 create_automatable_parameters ();
925                 set_control_ids (node, version);
926         }
927
928         if ((prop = node.property ("count")) != 0) {
929                 sscanf (prop->value().c_str(), "%u", &count);
930         }
931
932         if (_plugins.size() != count) {
933                 for (uint32_t n = 1; n < count; ++n) {
934                         add_plugin (plugin_factory (plugin));
935                 }
936         }
937
938         Processor::set_state (node, version);
939
940         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
941
942                 /* find the node with the type-specific node name ("lv2", "ladspa", etc)
943                    and set all plugins to the same state.
944                 */
945
946                 if ((*niter)->name() == plugin->state_node_name()) {
947
948                         plugin->set_state (**niter, version);
949
950                         for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
951                                 (*i)->set_state (**niter, version);
952                         }
953
954                         break;
955                 }
956         }
957
958         if (version < 3000) {
959
960                 /* Only 2.X sessions need a call to set_parameter_state() - in 3.X and above
961                    this is all handled by Automatable
962                 */
963
964                 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
965                         if ((*niter)->name() == "Redirect") {
966                                 /* XXX do we need to tackle placement? i think not (pd; oct 16 2009) */
967                                 Processor::set_state (**niter, version);
968                                 break;
969                         }
970                 }
971
972                 set_parameter_state_2X (node, version);
973         }
974
975         for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
976                 if (active()) {
977                         (*i)->activate ();
978                 } else {
979                         (*i)->deactivate ();
980                 }
981         }
982
983         /* catch up on I/O */
984
985         {
986                 Glib::Mutex::Lock em (_session.engine().process_lock());
987                 IO::PortCountChanged (max(input_streams(), output_streams()));
988         }
989
990         return 0;
991 }
992
993 void
994 PluginInsert::set_parameter_state_2X (const XMLNode& node, int version)
995 {
996         XMLNodeList nlist = node.children();
997         XMLNodeIterator niter;
998
999         /* look for port automation node */
1000
1001         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1002
1003                 if ((*niter)->name() != port_automation_node_name) {
1004                         continue;
1005                 }
1006
1007                 XMLNodeList cnodes;
1008                 XMLProperty *cprop;
1009                 XMLNodeConstIterator iter;
1010                 XMLNode *child;
1011                 const char *port;
1012                 uint32_t port_id;
1013
1014                 cnodes = (*niter)->children ("port");
1015
1016                 for (iter = cnodes.begin(); iter != cnodes.end(); ++iter){
1017
1018                         child = *iter;
1019
1020                         if ((cprop = child->property("number")) != 0) {
1021                                 port = cprop->value().c_str();
1022                         } else {
1023                                 warning << _("PluginInsert: Auto: no ladspa port number") << endmsg;
1024                                 continue;
1025                         }
1026
1027                         sscanf (port, "%" PRIu32, &port_id);
1028
1029                         if (port_id >= _plugins[0]->parameter_count()) {
1030                                 warning << _("PluginInsert: Auto: port id out of range") << endmsg;
1031                                 continue;
1032                         }
1033
1034                         boost::shared_ptr<AutomationControl> c = boost::dynamic_pointer_cast<AutomationControl>(
1035                                         control(Evoral::Parameter(PluginAutomation, 0, port_id), true));
1036
1037                         if (c) {
1038                                 if (!child->children().empty()) {
1039                                         c->alist()->set_state (*child->children().front(), version);
1040
1041                                         /* In some cases 2.X saves lists with min_yval and max_yval
1042                                            being FLT_MIN and FLT_MAX respectively.  This causes problems
1043                                            in A3 because these min/max values are used to compute
1044                                            where GUI control points should be drawn.  If we see such
1045                                            values, `correct' them to the min/max of the appropriate
1046                                            parameter.
1047                                         */
1048
1049                                         float min_y = c->alist()->get_min_y ();
1050                                         float max_y = c->alist()->get_max_y ();
1051
1052                                         Plugin::ParameterDescriptor desc;
1053                                         _plugins.front()->get_parameter_descriptor (port_id, desc);
1054
1055                                         if (min_y == FLT_MIN) {
1056                                                 min_y = desc.lower;
1057                                         }
1058
1059                                         if (max_y == FLT_MAX) {
1060                                                 max_y = desc.upper;
1061                                         }
1062
1063                                         c->alist()->set_yrange (min_y, max_y);
1064                                 }
1065                         } else {
1066                                 error << string_compose (_("PluginInsert: automatable control %1 not found - ignored"), port_id) << endmsg;
1067                         }
1068                 }
1069
1070                 /* done */
1071
1072                 break;
1073         }
1074 }
1075
1076
1077 string
1078 PluginInsert::describe_parameter (Evoral::Parameter param)
1079 {
1080         if (param.type() != PluginAutomation) {
1081                 return Automatable::describe_parameter(param);
1082         }
1083
1084         return _plugins[0]->describe_parameter (param);
1085 }
1086
1087 ARDOUR::framecnt_t
1088 PluginInsert::signal_latency() const
1089 {
1090         if (_user_latency) {
1091                 return _user_latency;
1092         }
1093
1094         return _plugins[0]->signal_latency ();
1095 }
1096
1097 ARDOUR::PluginType
1098 PluginInsert::type ()
1099 {
1100        return plugin()->get_info()->type;
1101 }
1102
1103 PluginInsert::PluginControl::PluginControl (PluginInsert* p, const Evoral::Parameter &param, boost::shared_ptr<AutomationList> list)
1104         : AutomationControl (p->session(), param, list, p->describe_parameter(param))
1105         , _plugin (p)
1106 {
1107         Plugin::ParameterDescriptor desc;
1108         p->plugin(0)->get_parameter_descriptor (param.id(), desc);
1109         _logarithmic = desc.logarithmic;
1110         _sr_dependent = desc.sr_dependent;
1111         _toggled = desc.toggled;
1112 }
1113
1114 /** @param val `user' value */
1115 void
1116 PluginInsert::PluginControl::set_value (double user_val)
1117 {
1118         /* FIXME: probably should be taking out some lock here.. */
1119
1120         double const plugin_val = user_to_plugin (user_val);
1121
1122         for (Plugins::iterator i = _plugin->_plugins.begin(); i != _plugin->_plugins.end(); ++i) {
1123                 (*i)->set_parameter (_list->parameter().id(), plugin_val);
1124         }
1125
1126         boost::shared_ptr<Plugin> iasp = _plugin->_impulseAnalysisPlugin.lock();
1127         if (iasp) {
1128                 iasp->set_parameter (_list->parameter().id(), plugin_val);
1129         }
1130
1131         AutomationControl::set_value (user_val);
1132 }
1133
1134 double
1135 PluginInsert::PluginControl::user_to_plugin (double val) const
1136 {
1137         /* no known transformations at this time */
1138         return val;
1139 }
1140
1141 double
1142 PluginInsert::PluginControl::user_to_ui (double val) const
1143 {
1144         if (_logarithmic) {
1145                 if (val > 0) {
1146                         val = log (val);
1147                 } else {
1148                         val = 0;
1149                 }
1150         }
1151
1152         return val;
1153 }
1154
1155 double
1156 PluginInsert::PluginControl::ui_to_user (double val) const
1157 {
1158         if (_logarithmic) {
1159                 val = exp (val);
1160         }
1161
1162         return val;
1163 }
1164
1165 /** Convert plugin values to UI values.  See pbd/controllable.h */
1166 double
1167 PluginInsert::PluginControl::plugin_to_ui (double val) const
1168 {
1169         return user_to_ui (plugin_to_user (val));
1170 }
1171
1172 double
1173 PluginInsert::PluginControl::plugin_to_user (double val) const
1174 {
1175         /* no known transformations at this time */
1176         return val;
1177 }
1178
1179 XMLNode&
1180 PluginInsert::PluginControl::get_state ()
1181 {
1182         stringstream ss;
1183
1184         XMLNode& node (AutomationControl::get_state());
1185         ss << parameter().id();
1186         node.add_property (X_("parameter"), ss.str());
1187
1188         return node;
1189 }
1190
1191 /** @return `user' val */
1192 double
1193 PluginInsert::PluginControl::get_value () const
1194 {
1195         /* FIXME: probably should be taking out some lock here.. */
1196
1197         return plugin_to_user (_plugin->get_parameter (_list->parameter()));
1198 }
1199
1200 boost::shared_ptr<Plugin>
1201 PluginInsert::get_impulse_analysis_plugin()
1202 {
1203         boost::shared_ptr<Plugin> ret;
1204         if (_impulseAnalysisPlugin.expired()) {
1205                 ret = plugin_factory(_plugins[0]);
1206                 _impulseAnalysisPlugin = ret;
1207         } else {
1208                 ret = _impulseAnalysisPlugin.lock();
1209         }
1210
1211         return ret;
1212 }
1213
1214 void
1215 PluginInsert::collect_signal_for_analysis (framecnt_t nframes)
1216 {
1217         // called from outside the audio thread, so this should be safe
1218         // only do audio as analysis is (currently) only for audio plugins
1219         _signal_analysis_inputs.ensure_buffers(  DataType::AUDIO, input_streams().n_audio(),  nframes);
1220         _signal_analysis_outputs.ensure_buffers( DataType::AUDIO, output_streams().n_audio(), nframes);
1221
1222         _signal_analysis_collected_nframes   = 0;
1223         _signal_analysis_collect_nframes_max = nframes;
1224 }
1225
1226 /** Add a plugin to our list and activate it if we have already been activated */
1227 void
1228 PluginInsert::add_plugin_with_activation (boost::shared_ptr<Plugin> plugin)
1229 {
1230         plugin->set_insert_info (this);
1231         _plugins.push_back (plugin);
1232         if (active()) {
1233                 plugin->activate ();
1234         }
1235 }
1236
1237 /** Add a plugin to our list */
1238 void
1239 PluginInsert::add_plugin (boost::shared_ptr<Plugin> plugin)
1240 {
1241         plugin->set_insert_info (this);
1242         _plugins.push_back (plugin);
1243 }
1244
1245 void
1246 PluginInsert::realtime_handle_transport_stopped ()
1247 {
1248         for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
1249                 (*i)->realtime_handle_transport_stopped ();
1250         }
1251 }