Merge with trunk R2935.
[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 #include <string>
21
22 #include <sigc++/bind.h>
23
24 #include <pbd/failed_constructor.h>
25 #include <pbd/xml++.h>
26
27 #include <ardour/plugin_insert.h>
28 #include <ardour/plugin.h>
29 #include <ardour/port.h>
30 #include <ardour/route.h>
31 #include <ardour/ladspa_plugin.h>
32 #include <ardour/buffer_set.h>
33 #include <ardour/automation_event.h>
34
35 #ifdef HAVE_SLV2
36 #include <ardour/lv2_plugin.h>
37 #endif
38
39 #ifdef VST_SUPPORT
40 #include <ardour/vst_plugin.h>
41 #endif
42
43 #ifdef HAVE_AUDIOUNITS
44 #include <ardour/audio_unit.h>
45 #endif
46
47 #include <ardour/audioengine.h>
48 #include <ardour/session.h>
49 #include <ardour/types.h>
50
51 #include "i18n.h"
52
53 using namespace std;
54 using namespace ARDOUR;
55 using namespace PBD;
56
57 const string PluginInsert::port_automation_node_name = "PortAutomation";
58
59 PluginInsert::PluginInsert (Session& s, boost::shared_ptr<Plugin> plug, Placement placement)
60         : Processor (s, plug->name(), placement)
61 {
62         /* the first is the master */
63
64         _plugins.push_back (plug);
65
66         init ();
67
68         {
69                 Glib::Mutex::Lock em (_session.engine().process_lock());
70                 IO::PortCountChanged (max(input_streams(), output_streams()));
71         }
72
73         ProcessorCreated (this); /* EMIT SIGNAL */
74 }
75
76 PluginInsert::PluginInsert (Session& s, const XMLNode& node)
77         : Processor (s, "unnamed plugin insert", PreFader)
78 {
79         if (set_state (node)) {
80                 throw failed_constructor();
81         }
82
83         // set_automatable ();
84
85         {
86                 Glib::Mutex::Lock em (_session.engine().process_lock());
87                 IO::PortCountChanged (max(input_streams(), output_streams()));
88         }
89 }
90
91 PluginInsert::PluginInsert (const PluginInsert& other)
92         : Processor (other._session, other._name, other.placement())
93 {
94         uint32_t count = other._plugins.size();
95
96         /* make as many copies as requested */
97         for (uint32_t n = 0; n < count; ++n) {
98                 _plugins.push_back (plugin_factory (other.plugin (n)));
99         }
100
101         init ();
102
103         ProcessorCreated (this); /* EMIT SIGNAL */
104 }
105
106 bool
107 PluginInsert::set_count (uint32_t num)
108 {
109         bool require_state = !_plugins.empty();
110
111         /* this is a bad idea.... we shouldn't do this while active.
112            only a route holding their redirect_lock should be calling this 
113         */
114
115         if (num == 0) { 
116                 return false;
117         } else if (num > _plugins.size()) {
118                 uint32_t diff = num - _plugins.size();
119
120                 for (uint32_t n = 0; n < diff; ++n) {
121                         _plugins.push_back (plugin_factory (_plugins[0]));
122
123                         if (require_state) {
124                                 /* XXX do something */
125                         }
126                 }
127
128         } else if (num < _plugins.size()) {
129                 uint32_t diff = _plugins.size() - num;
130                 for (uint32_t n= 0; n < diff; ++n) {
131                         _plugins.pop_back();
132                 }
133         }
134
135         return true;
136 }
137
138 void
139 PluginInsert::init ()
140 {
141         set_automatable ();
142 }
143
144 PluginInsert::~PluginInsert ()
145 {
146         GoingAway (); /* EMIT SIGNAL */
147 }
148
149 void
150 PluginInsert::auto_state_changed (Parameter which)
151 {
152         if (which.type() != PluginAutomation)
153                 return;
154
155         boost::shared_ptr<AutomationControl> c = control (which);
156
157         if (c && c->list()->automation_state() != Off) {
158                 _plugins[0]->set_parameter (which.id(), c->list()->eval (_session.transport_frame()));
159         }
160 }
161
162 ChanCount
163 PluginInsert::output_streams() const
164 {
165         if (_configured)
166                 return output_for_input_configuration(_configured_input);
167         else
168                 return natural_output_streams();
169 }
170
171 ChanCount
172 PluginInsert::input_streams() const
173 {
174         if (_configured)
175                 return _configured_input;
176         else
177                 return natural_input_streams();
178 }
179
180 ChanCount
181 PluginInsert::natural_output_streams() const
182 {
183         return _plugins[0]->get_info()->n_outputs;
184 }
185
186 ChanCount
187 PluginInsert::natural_input_streams() const
188 {
189         return _plugins[0]->get_info()->n_inputs;
190 }
191
192 bool
193 PluginInsert::is_generator() const
194 {
195         /* XXX more finesse is possible here. VST plugins have a
196            a specific "instrument" flag, for example.
197          */
198
199         return _plugins[0]->get_info()->n_inputs.n_audio() == 0;
200 }
201
202 void
203 PluginInsert::set_automatable ()
204 {
205         set<Parameter> a = _plugins.front()->automatable ();
206
207         Plugin::ParameterDescriptor desc;
208
209         for (set<Parameter>::iterator i = a.begin(); i != a.end(); ++i) {
210                 if (i->type() == PluginAutomation) {
211                         can_automate (*i);
212                         _plugins.front()->get_parameter_descriptor(i->id(), desc);
213                         boost::shared_ptr<AutomationList> list(new AutomationList(
214                                         *i,
215                                         //(desc.min_unbound ? FLT_MIN : desc.lower),
216                                         //(desc.max_unbound ? FLT_MAX : desc.upper),
217                                         desc.lower, desc.upper,
218                                         _plugins.front()->default_value(i->id())));
219
220                         add_control(boost::shared_ptr<AutomationControl>(
221                                         new PluginControl(*this, list)));
222                 }
223         }
224 }
225
226 void
227 PluginInsert::parameter_changed (Parameter which, float val)
228 {
229         if (which.type() != PluginAutomation)
230                 return;
231
232         vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin();
233
234         /* don't set the first plugin, just all the slaves */
235
236         if (i != _plugins.end()) {
237                 ++i;
238                 for (; i != _plugins.end(); ++i) {
239                         (*i)->set_parameter (which, val);
240                 }
241         }
242 }
243
244 void
245 PluginInsert::set_block_size (nframes_t nframes)
246 {
247         for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
248                 (*i)->set_block_size (nframes);
249         }
250 }
251
252 void
253 PluginInsert::activate ()
254 {
255         for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
256                 (*i)->activate ();
257         }
258 }
259
260 void
261 PluginInsert::deactivate ()
262 {
263         for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
264                 (*i)->deactivate ();
265         }
266 }
267
268 void
269 PluginInsert::connect_and_run (BufferSet& bufs, nframes_t nframes, nframes_t offset, bool with_auto, nframes_t now)
270 {
271         uint32_t in_index = 0;
272         uint32_t out_index = 0;
273
274         /* Note that we've already required that plugins
275            be able to handle in-place processing.
276         */
277
278         if (with_auto) {
279
280                 uint32_t n = 0;
281                 
282                 for (Controls::iterator li = _controls.begin(); li != _controls.end(); ++li, ++n) {
283                         
284                         boost::shared_ptr<AutomationControl> c = li->second;
285
286                         if (c->parameter().type() == PluginAutomation && c->list()->automation_playback()) {
287                                 bool valid;
288
289                                 const float val = c->list()->rt_safe_eval (now, valid);                         
290
291                                 if (valid) {
292                                         c->set_value(val);
293                                 }
294
295                         } 
296                 }
297         }
298
299         for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
300                 (*i)->connect_and_run (bufs, in_index, out_index, nframes, offset);
301         }
302
303         /* leave remaining channel buffers alone */
304 }
305
306 void
307 PluginInsert::silence (nframes_t nframes, nframes_t offset)
308 {
309         uint32_t in_index = 0;
310         uint32_t out_index = 0;
311
312         if (active()) {
313                 for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
314                         (*i)->connect_and_run (_session.get_silent_buffers ((*i)->get_info()->n_inputs), in_index, out_index, nframes, offset);
315                 }
316         }
317 }
318         
319 void
320 PluginInsert::run_in_place (BufferSet& bufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset)
321 {
322         if (active()) {
323
324                 if (_session.transport_rolling()) {
325                         automation_run (bufs, nframes, offset);
326                 } else {
327                         connect_and_run (bufs, nframes, offset, false);
328                 }
329         } else {
330
331                 /* FIXME: type, audio only */
332
333                 uint32_t in = _plugins[0]->get_info()->n_inputs.n_audio();
334                 uint32_t out = _plugins[0]->get_info()->n_outputs.n_audio();
335
336                 if (out > in) {
337
338                         /* not active, but something has make up for any channel count increase */
339                         
340                         for (uint32_t n = out - in; n < out; ++n) {
341                                 memcpy (bufs.get_audio(n).data(nframes, offset), bufs.get_audio(in - 1).data(nframes, offset), sizeof (Sample) * nframes);
342                         }
343                 }
344
345                 bufs.count().set_audio(out);
346         }
347 }
348
349 void
350 PluginInsert::set_parameter (Parameter param, float val)
351 {
352         if (param.type() != PluginAutomation)
353                 return;
354
355         /* the others will be set from the event triggered by this */
356
357         _plugins[0]->set_parameter (param.id(), val);
358         
359         boost::shared_ptr<AutomationControl> c = control (param);
360         
361         if (c)
362                 c->set_value(val);
363
364         _session.set_dirty();
365 }
366
367 float
368 PluginInsert::get_parameter (Parameter param)
369 {
370         if (param.type() != PluginAutomation)
371                 return 0.0;
372         else
373                 return
374                 _plugins[0]->get_parameter (param.id());
375 }
376
377 void
378 PluginInsert::automation_run (BufferSet& bufs, nframes_t nframes, nframes_t offset)
379 {
380         ControlEvent next_event (0, 0.0f);
381         nframes_t now = _session.transport_frame ();
382         nframes_t end = now + nframes;
383
384         Glib::Mutex::Lock lm (_automation_lock, Glib::TRY_LOCK);
385
386         if (!lm.locked()) {
387                 connect_and_run (bufs, nframes, offset, false);
388                 return;
389         }
390         
391         if (!find_next_event (now, end, next_event)) {
392                 
393                 /* no events have a time within the relevant range */
394                 
395                 connect_and_run (bufs, nframes, offset, true, now);
396                 return;
397         }
398         
399         while (nframes) {
400
401                 nframes_t cnt = min (((nframes_t) ceil (next_event.when) - now), nframes);
402   
403                 connect_and_run (bufs, cnt, offset, true, now);
404                 
405                 nframes -= cnt;
406                 offset += cnt;
407                 now += cnt;
408
409                 if (!find_next_event (now, end, next_event)) {
410                         break;
411                 }
412         }
413   
414         /* cleanup anything that is left to do */
415   
416         if (nframes) {
417                 connect_and_run (bufs, nframes, offset, true, now);
418         }
419 }       
420
421 float
422 PluginInsert::default_parameter_value (Parameter param)
423 {
424         if (param.type() != PluginAutomation)
425                 return 1.0;
426
427         if (_plugins.empty()) {
428                 fatal << _("programming error: ") << X_("PluginInsert::default_parameter_value() called with no plugin")
429                       << endmsg;
430                 /*NOTREACHED*/
431         }
432
433         return _plugins[0]->default_value (param.id());
434 }
435
436 boost::shared_ptr<Plugin>
437 PluginInsert::plugin_factory (boost::shared_ptr<Plugin> other)
438 {
439         boost::shared_ptr<LadspaPlugin> lp;
440 #ifdef HAVE_SLV2
441         boost::shared_ptr<LV2Plugin> lv2p;
442 #endif
443 #ifdef VST_SUPPORT
444         boost::shared_ptr<VSTPlugin> vp;
445 #endif
446 #ifdef HAVE_AUDIOUNITS
447         boost::shared_ptr<AUPlugin> ap;
448 #endif
449
450         if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
451                 return boost::shared_ptr<Plugin> (new LadspaPlugin (*lp));
452 #ifdef HAVE_SLV2
453         } else if ((lv2p = boost::dynamic_pointer_cast<LV2Plugin> (other)) != 0) {
454                 return boost::shared_ptr<Plugin> (new LV2Plugin (*lv2p));
455 #endif
456 #ifdef VST_SUPPORT
457         } else if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (other)) != 0) {
458                 return boost::shared_ptr<Plugin> (new VSTPlugin (*vp));
459 #endif
460 #ifdef HAVE_AUDIOUNITS
461         } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
462                 return boost::shared_ptr<Plugin> (new AUPlugin (*ap));
463 #endif
464         }
465
466         fatal << string_compose (_("programming error: %1"),
467                           X_("unknown plugin type in PluginInsert::plugin_factory"))
468               << endmsg;
469         /*NOTREACHED*/
470         return boost::shared_ptr<Plugin> ((Plugin*) 0);
471 }
472
473 bool
474 PluginInsert::configure_io (ChanCount in, ChanCount out)
475 {
476         ChanCount matching_out = output_for_input_configuration(out);
477         if (matching_out != out) {
478                 _configured = false;
479                 return false;
480         } else {
481                 bool success = set_count (count_for_configuration(in, out));
482                 if (success)
483                         Processor::configure_io(in, out);
484                 return success;
485         }
486 }
487
488 bool
489 PluginInsert::can_support_input_configuration (ChanCount in) const
490 {
491         ChanCount outputs = _plugins[0]->get_info()->n_outputs;
492         ChanCount inputs = _plugins[0]->get_info()->n_inputs;
493
494         /* see output_for_input_configuration below */
495         if ((inputs.n_total() == 0)
496                         || (inputs.n_total() == 1 && outputs == inputs)
497                         || (inputs.n_total() == 1 && outputs == inputs
498                                 && ((inputs.n_audio() == 0 && in.n_audio() == 0)
499                                         || (inputs.n_midi() == 0 && in.n_midi() == 0)))
500                         || (inputs == in)) {
501                 return true;
502         }
503
504         bool can_replicate = true;
505
506         /* if number of inputs is a factor of the requested input
507            configuration for every type, we can replicate.
508         */
509         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
510                 if (inputs.get(*t) >= in.get(*t) || (inputs.get(*t) % in.get(*t) != 0)) {
511                         can_replicate = false;
512                         break;
513                 }
514         }
515
516         if (can_replicate && (in.n_total() % inputs.n_total() == 0)) {
517                 return true;
518         } else {
519                 return false;
520         }
521 }
522
523 ChanCount
524 PluginInsert::output_for_input_configuration (ChanCount in) const
525 {
526         ChanCount outputs = _plugins[0]->get_info()->n_outputs;
527         ChanCount inputs = _plugins[0]->get_info()->n_inputs;
528
529         if (inputs.n_total() == 0) {
530                 /* instrument plugin, always legal, but throws away any existing streams */
531                 return outputs;
532         }
533
534         if (inputs.n_total() == 1 && outputs == inputs
535                         && ((inputs.n_audio() == 0 && in.n_audio() == 0)
536                                 || (inputs.n_midi() == 0 && in.n_midi() == 0))) {
537                 /* mono plugin, replicate as needed to match in */
538                 return in;
539         }
540
541         if (inputs == in) {
542                 /* exact match */
543                 return outputs;
544         }
545
546         bool can_replicate = true;
547
548         /* if number of inputs is a factor of the requested input
549            configuration for every type, we can replicate.
550         */
551         for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
552                 if (inputs.get(*t) >= in.get(*t) || (in.get(*t) % inputs.get(*t) != 0)) {
553                         can_replicate = false;
554                         break;
555                 }
556         }
557
558         if (can_replicate && (inputs.n_total() % in.n_total() == 0)) {
559                 ChanCount output;
560                 
561                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
562                         output.set(*t, outputs.get(*t) * (in.get(*t) / inputs.get(*t)));
563                 }
564
565                 return output;
566         }
567
568         /* sorry */
569         return ChanCount();
570 }
571
572 /* Number of plugin instances required to support a given channel configuration.
573  * (private helper)
574  */
575 int32_t
576 PluginInsert::count_for_configuration (ChanCount in, ChanCount out) const
577 {
578         // FIXME: take 'out' into consideration
579         
580         ChanCount outputs = _plugins[0]->get_info()->n_outputs;
581         ChanCount inputs = _plugins[0]->get_info()->n_inputs;
582
583         if (inputs.n_total() == 0) {
584                 /* instrument plugin, always legal, but throws away any existing streams */
585                 return 1;
586         }
587
588         if (inputs.n_total() == 1 && outputs == inputs
589                         && ((inputs.n_audio() == 0 && in.n_audio() == 0)
590                                 || (inputs.n_midi() == 0 && in.n_midi() == 0))) {
591                 /* mono plugin, replicate as needed to match in */
592                 return in.n_total();
593         }
594
595         if (inputs == in) {
596                 /* exact match */
597                 return 1;
598         }
599
600         // assumes in is valid, so we must be replicating
601         if (inputs.n_total() < in.n_total()
602                         && (in.n_total() % inputs.n_total() == 0)) {
603
604                 return in.n_total() / inputs.n_total();
605         }
606
607         /* err... */
608         return 0;
609 }
610
611 XMLNode&
612 PluginInsert::get_state(void)
613 {
614         return state (true);
615 }
616
617 XMLNode&
618 PluginInsert::state (bool full)
619 {
620         XMLNode& node = Processor::state (full);
621
622         node.add_property ("type", _plugins[0]->state_node_name());
623         node.add_property("unique-id", _plugins[0]->unique_id());
624         node.add_property("count", string_compose("%1", _plugins.size()));
625         node.add_child_nocopy (_plugins[0]->get_state());
626
627         /* add port automation state */
628         XMLNode *autonode = new XMLNode(port_automation_node_name);
629         set<Parameter> automatable = _plugins[0]->automatable();
630         
631         for (set<Parameter>::iterator x = automatable.begin(); x != automatable.end(); ++x) {
632                 
633                 /*XMLNode* child = new XMLNode("port");
634                 snprintf(buf, sizeof(buf), "%" PRIu32, *x);
635                 child->add_property("number", string(buf));
636                 
637                 child->add_child_nocopy (automation_list (*x).state (full));
638                 autonode->add_child_nocopy (*child);
639                 */
640                 autonode->add_child_nocopy (control(*x)->list()->state (full));
641         }
642
643         node.add_child_nocopy (*autonode);
644         
645         return node;
646 }
647
648 int
649 PluginInsert::set_state(const XMLNode& node)
650 {
651         XMLNodeList nlist = node.children();
652         XMLNodeIterator niter;
653         XMLPropertyList plist;
654         const XMLProperty *prop;
655         ARDOUR::PluginType type;
656
657         if ((prop = node.property ("type")) == 0) {
658                 error << _("XML node describing insert is missing the `type' field") << endmsg;
659                 return -1;
660         }
661
662         if (prop->value() == X_("ladspa") || prop->value() == X_("Ladspa")) { /* handle old school sessions */
663                 type = ARDOUR::LADSPA;
664         } else if (prop->value() == X_("lv2")) {
665                 type = ARDOUR::LV2;
666         } else if (prop->value() == X_("vst")) {
667                 type = ARDOUR::VST;
668         } else {
669                 error << string_compose (_("unknown plugin type %1 in plugin insert state"),
670                                   prop->value())
671                       << endmsg;
672                 return -1;
673         }
674         
675         prop = node.property ("unique-id");
676         if (prop == 0) {
677                 error << _("Plugin has no unique ID field") << endmsg;
678                 return -1;
679         }
680
681         boost::shared_ptr<Plugin> plugin;
682         
683         plugin = find_plugin (_session, prop->value(), type);   
684
685         if (plugin == 0) {
686                 error << string_compose(_("Found a reference to a plugin (\"%1\") that is unknown.\n"
687                                    "Perhaps it was removed or moved since it was last used."), prop->value()) 
688                       << endmsg;
689                 return -1;
690         }
691
692         uint32_t count = 1;
693
694         if ((prop = node.property ("count")) != 0) {
695                 sscanf (prop->value().c_str(), "%u", &count);
696         }
697
698         if (_plugins.size() != count) {
699                 
700                 _plugins.push_back (plugin);
701                 
702                 for (uint32_t n=1; n < count; ++n) {
703                         _plugins.push_back (plugin_factory (plugin));
704                 }
705         }
706         
707         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
708                 if ((*niter)->name() == plugin->state_node_name()) {
709                         for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
710                                 (*i)->set_state (**niter);
711                         }
712                         break;
713                 }
714         } 
715
716         const XMLNode* insert_node = &node;
717
718         // legacy sessions: search for child IOProcessor node
719         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
720                 if ((*niter)->name() == "IOProcessor") {
721                         insert_node = *niter;
722                         break;
723                 }
724         }
725         
726         Processor::set_state (*insert_node);
727
728         /* look for port automation node */
729         
730         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
731
732                 if ((*niter)->name() != port_automation_node_name) {
733                         continue;
734                 }
735
736                 XMLNodeList cnodes;
737                 XMLProperty *cprop;
738                 XMLNodeConstIterator iter;
739                 XMLNode *child;
740                 const char *port;
741                 uint32_t port_id;
742                 
743                 cnodes = (*niter)->children ("port");
744                 
745                 for(iter = cnodes.begin(); iter != cnodes.end(); ++iter){
746                         
747                         child = *iter;
748                         
749                         if ((cprop = child->property("number")) != 0) {
750                                 port = cprop->value().c_str();
751                         } else {
752                                 warning << _("PluginInsert: Auto: no ladspa port number") << endmsg;
753                                 continue;
754                         }
755                         
756                         sscanf (port, "%" PRIu32, &port_id);
757                         
758                         if (port_id >= _plugins[0]->parameter_count()) {
759                                 warning << _("PluginInsert: Auto: port id out of range") << endmsg;
760                                 continue;
761                         }
762
763                         if (!child->children().empty()) {
764                                 control (Parameter(PluginAutomation, port_id), true)->list()->set_state (*child->children().front());
765                         } else {
766                                 if ((cprop = child->property("auto")) != 0) {
767                                         
768                                         /* old school */
769
770                                         int x;
771                                         sscanf (cprop->value().c_str(), "0x%x", &x);
772                                         control (Parameter(PluginAutomation, port_id), true)->list()->set_automation_state (AutoState (x));
773
774                                 } else {
775                                         
776                                         /* missing */
777                                         
778                                         control (Parameter(PluginAutomation, port_id), true)->list()->set_automation_state (Off);
779                                 }
780                         }
781
782                 }
783
784                 /* done */
785
786                 break;
787         } 
788
789         if (niter == nlist.end()) {
790                 warning << string_compose(_("XML node describing a port automation is missing the `%1' information"), port_automation_node_name) << endmsg;
791         }
792         
793         // The name of the PluginInsert comes from the plugin, nothing else
794         _name = plugin->get_info()->name;
795         
796         return 0;
797 }
798
799 string
800 PluginInsert::describe_parameter (Parameter param)
801 {
802         if (param.type() != PluginAutomation)
803                 return Automatable::describe_parameter(param);
804
805         return _plugins[0]->describe_parameter (param);
806 }
807
808 ARDOUR::nframes_t 
809 PluginInsert::signal_latency() const
810 {
811         if (_user_latency) {
812                 return _user_latency;
813         }
814
815         return _plugins[0]->signal_latency ();
816 }
817
818 ARDOUR::PluginType
819 PluginInsert::type ()
820 {
821         boost::shared_ptr<LadspaPlugin> lp;
822 #ifdef VST_SUPPORT
823         boost::shared_ptr<VSTPlugin> vp;
824 #endif
825 #ifdef HAVE_AUDIOUNITS
826         boost::shared_ptr<AUPlugin> ap;
827 #endif
828         
829         PluginPtr other = plugin ();
830
831         if ((lp = boost::dynamic_pointer_cast<LadspaPlugin> (other)) != 0) {
832                 return ARDOUR::LADSPA;
833 #ifdef VST_SUPPORT
834         } else if ((vp = boost::dynamic_pointer_cast<VSTPlugin> (other)) != 0) {
835                 return ARDOUR::VST;
836 #endif
837 #ifdef HAVE_AUDIOUNITS
838         } else if ((ap = boost::dynamic_pointer_cast<AUPlugin> (other)) != 0) {
839                 return ARDOUR::AudioUnit;
840 #endif
841         } else {
842                 /* NOT REACHED */
843                 return (ARDOUR::PluginType) 0;
844         }
845 }
846
847 PluginInsert::PluginControl::PluginControl (PluginInsert& p, boost::shared_ptr<AutomationList> list)
848         : AutomationControl (p.session(), list, p.describe_parameter(list->parameter()))
849         , _plugin (p)
850         , _list (list)
851 {
852         Plugin::ParameterDescriptor desc;
853         p.plugin(0)->get_parameter_descriptor (list->parameter().id(), desc);
854         _logarithmic = desc.logarithmic;
855         _toggled = desc.toggled;
856 }
857          
858 void
859 PluginInsert::PluginControl::set_value (float val)
860 {
861         /* FIXME: probably should be taking out some lock here.. */
862         
863         if (_toggled) {
864                 if (val > 0.5) {
865                         val = 1.0;
866                 } else {
867                         val = 0.0;
868                 }
869         } else {
870                         
871                 /*const float range = _list->get_max_y() - _list->get_min_y();
872                 const float lower = _list->get_min_y();
873
874                 if (!_logarithmic) {
875                         val = lower + (range * val);
876                 } else {
877                         float log_lower = 0.0f;
878                         if (lower > 0.0f) {
879                                 log_lower = log(lower);
880                         }
881
882                         val = exp(log_lower + log(range) * val);
883                 }*/
884
885         }
886
887         for (vector<boost::shared_ptr<Plugin> >::iterator i = _plugin._plugins.begin();
888                         i != _plugin._plugins.end(); ++i) {
889                 (*i)->set_parameter (_list->parameter().id(), val);
890         }
891
892         AutomationControl::set_value(val);
893 }
894
895 float
896 PluginInsert::PluginControl::get_value (void) const
897 {
898         /* FIXME: probably should be taking out some lock here.. */
899         
900         float val = _plugin.get_parameter (_list->parameter());
901
902         return val;
903
904         /*if (_toggled) {
905                 
906                 return val;
907                 
908         } else {
909                 
910                 if (_logarithmic) {
911                         val = log(val);
912                 }
913                 
914                 return ((val - lower) / range);
915         }*/
916 }
917