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