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