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