LV2 support.
[ardour.git] / libs / ardour / lv2_plugin.cc
1 /*
2     Copyright (C) 2008 Paul Davis 
3     Author: Dave Robillard
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include <vector>
22 #include <string>
23
24 #include <cstdlib>
25 #include <cmath>
26
27 #include <pbd/compose.h>
28 #include <pbd/error.h>
29 #include <pbd/pathscanner.h>
30 #include <pbd/xml++.h>
31
32 #include <ardour/ardour.h>
33 #include <ardour/session.h>
34 #include <ardour/audioengine.h>
35 #include <ardour/lv2_plugin.h>
36
37 #include <pbd/stl_delete.h>
38
39 #include "i18n.h"
40 #include <locale.h>
41
42 using namespace std;
43 using namespace ARDOUR;
44 using namespace PBD;
45
46 LV2Plugin::LV2Plugin (AudioEngine& e, Session& session, SLV2Plugin plugin, nframes_t rate)
47         : Plugin (e, session)
48 {
49         init (plugin, rate);
50 }
51
52 LV2Plugin::LV2Plugin (const LV2Plugin &other)
53         : Plugin (other)
54 {
55         init (other._plugin, other._sample_rate);
56
57         for (uint32_t i = 0; i < parameter_count(); ++i) {
58                 _control_data[i] = other._shadow_data[i];
59                 _shadow_data[i] = other._shadow_data[i];
60         }
61 }
62
63 void
64 LV2Plugin::init (SLV2Plugin plugin, nframes_t rate)
65 {
66         _plugin = plugin;
67         _template = slv2_plugin_get_template(plugin);
68         _control_data = 0;
69         _shadow_data = 0;
70         _latency_control_port = 0;
71         _was_activated = false;
72
73         _instance = slv2_plugin_instantiate(plugin, rate, NULL);
74
75         if (_instance == 0) {
76                 error << _("LV2: Failed to instantiate plugin ") << slv2_plugin_get_uri(plugin) << endl;
77                 throw failed_constructor();
78         }
79
80         if (slv2_plugin_has_feature(plugin, "http://lv2plug.in/ns/lv2core#inPlaceBroken")) {
81                 error << string_compose(_("LV2: \"%1\" cannot be used, since it cannot do inplace processing"), slv2_plugin_get_name(plugin)) << endmsg;
82                 throw failed_constructor();
83         }
84
85         _sample_rate = rate;
86
87         const uint32_t num_ports = slv2_plugin_get_num_ports(plugin);
88
89         _control_data = new float[num_ports];
90         _shadow_data = new float[num_ports];
91
92         const bool latent = slv2_plugin_has_latency(plugin);
93         uint32_t latency_port = (latent ? slv2_plugin_get_latency_port(plugin) : 0);
94
95         for (uint32_t i = 0; i < num_ports; ++i) {
96                 if (parameter_is_control(i)) {
97                         slv2_instance_connect_port (_instance, i, &_control_data[i]);
98                         
99                         if (latent && i == latency_port) {
100                                 _latency_control_port = &_control_data[i];
101                                 *_latency_control_port = 0;
102                         }
103
104                         if (parameter_is_input(i)) {
105                                 _shadow_data[i] = default_value (i);
106                         }
107                 }
108         }
109
110         Plugin::setup_controls ();
111
112         latency_compute_run ();
113 }
114
115 LV2Plugin::~LV2Plugin ()
116 {
117         deactivate ();
118         cleanup ();
119
120         GoingAway (); /* EMIT SIGNAL */
121         
122         slv2_instance_free(_instance);
123
124         if (_control_data) {
125                 delete [] _control_data;
126         }
127
128         if (_shadow_data) {
129                 delete [] _shadow_data;
130         }
131 }
132
133 string
134 LV2Plugin::unique_id() const
135 {
136         return slv2_plugin_get_uri(_plugin);
137 }
138
139
140 float
141 LV2Plugin::default_value (uint32_t port)
142 {
143         return slv2_port_get_default_value(_plugin,
144                         slv2_plugin_get_port_by_index(_plugin, port));
145 }       
146
147 void
148 LV2Plugin::set_parameter (uint32_t which, float val)
149 {
150         if (which < slv2_plugin_get_num_ports(_plugin)) {
151                 _shadow_data[which] = val;
152                 ParameterChanged (which, val); /* EMIT SIGNAL */
153
154                 if (which < parameter_count() && controls[which]) {
155                         controls[which]->Changed ();
156                 }
157                 
158         } else {
159                 warning << string_compose (_("Illegal parameter number used with plugin \"%1\"."
160                                 "This is a bug in either Ardour or the LV2 plugin (%2)"),
161                                 name(), unique_id()) << endmsg;
162         }
163 }
164
165 float
166 LV2Plugin::get_parameter (uint32_t which) const
167 {
168         if (parameter_is_input(which)) {
169                 return (float) _shadow_data[which];
170         } else {
171                 return (float) _control_data[which];
172         }
173         return 0.0f;
174 }
175
176 uint32_t
177 LV2Plugin::nth_parameter (uint32_t n, bool& ok) const
178 {
179         uint32_t x, c;
180
181         ok = false;
182
183         for (c = 0, x = 0; x < slv2_plugin_get_num_ports(_plugin); ++x) {
184                 if (parameter_is_control (x)) {
185                         if (c++ == n) {
186                                 ok = true;
187                                 return x;
188                         }
189                 }
190         }
191         
192         return 0;
193 }
194
195 XMLNode&
196 LV2Plugin::get_state()
197 {
198         XMLNode *root = new XMLNode(state_node_name());
199         XMLNode *child;
200         char buf[16];
201         LocaleGuard lg (X_("POSIX"));
202
203         for (uint32_t i = 0; i < parameter_count(); ++i){
204
205                 if (parameter_is_input(i) && parameter_is_control(i)) {
206                         child = new XMLNode("port");
207                         snprintf(buf, sizeof(buf), "%u", i);
208                         child->add_property("number", string(buf));
209                         snprintf(buf, sizeof(buf), "%+f", _shadow_data[i]);
210                         child->add_property("value", string(buf));
211                         root->add_child_nocopy (*child);
212
213                         if (i < controls.size() && controls[i]) {
214                                 root->add_child_nocopy (controls[i]->get_state());
215                         }
216                 }
217         }
218
219         return *root;
220 }
221
222 bool
223 LV2Plugin::save_preset (string name)
224 {
225         return Plugin::save_preset (name, "lv2");
226 }
227
228 int
229 LV2Plugin::set_state(const XMLNode& node)
230 {
231         XMLNodeList nodes;
232         XMLProperty *prop;
233         XMLNodeConstIterator iter;
234         XMLNode *child;
235         const char *port;
236         const char *data;
237         uint32_t port_id;
238         LocaleGuard lg (X_("POSIX"));
239
240         if (node.name() != state_node_name()) {
241                 error << _("Bad node sent to LV2Plugin::set_state") << endmsg;
242                 return -1;
243         }
244
245         nodes = node.children ("port");
246
247         for(iter = nodes.begin(); iter != nodes.end(); ++iter){
248
249                 child = *iter;
250
251                 if ((prop = child->property("number")) != 0) {
252                         port = prop->value().c_str();
253                 } else {
254                         warning << _("LV2: no lv2 port number") << endmsg;
255                         continue;
256                 }
257
258                 if ((prop = child->property("value")) != 0) {
259                         data = prop->value().c_str();
260                 } else {
261                         warning << _("LV2: no lv2 port data") << endmsg;
262                         continue;
263                 }
264
265                 sscanf (port, "%" PRIu32, &port_id);
266                 set_parameter (port_id, atof(data));
267         }
268         
269         latency_compute_run ();
270
271         return 0;
272 }
273
274 int
275 LV2Plugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& desc) const
276 {
277         SLV2Port port = slv2_plugin_get_port_by_index(_plugin, which);
278
279         #define LV2_URI "http://lv2plug.in/ns/lv2core#"
280         
281     desc.integer_step = slv2_port_has_property(_plugin, port, LV2_URI "integer");
282     desc.toggled = slv2_port_has_property(_plugin, port, LV2_URI "toggled");
283     desc.logarithmic = false; // TODO (LV2 extension)
284     desc.sr_dependent = slv2_port_has_property(_plugin, port, LV2_URI "sampleRate");
285     desc.label = slv2_port_get_name(_plugin, port);
286     desc.lower = slv2_port_get_minimum_value(_plugin, port);
287     desc.upper = slv2_port_get_maximum_value(_plugin, port);
288     desc.min_unbound = false; // TODO (LV2 extension)
289     desc.max_unbound = false; // TODO (LV2 extension)
290         
291         if (desc.integer_step) {
292                 desc.step = 1.0;
293                 desc.smallstep = 0.1;
294                 desc.largestep = 10.0;
295         } else {
296                 const float delta = desc.upper - desc.lower;
297                 desc.step = delta / 1000.0f;
298                 desc.smallstep = delta / 10000.0f;
299                 desc.largestep = delta/10.0f;
300         }
301
302         return 0;
303 }
304
305
306 string
307 LV2Plugin::describe_parameter (uint32_t which)
308 {
309         if (which < parameter_count()) {
310                 return slv2_port_get_name(_plugin,
311                                 slv2_plugin_get_port_by_index(_plugin, which));
312         } else {
313                 return "??";
314         }
315 }
316
317 nframes_t
318 LV2Plugin::latency () const
319 {
320         if (_latency_control_port) {
321                 return (nframes_t) floor (*_latency_control_port);
322         } else {
323                 return 0;
324         }
325 }
326
327 set<uint32_t>
328 LV2Plugin::automatable () const
329 {
330         set<uint32_t> ret;
331
332         for (uint32_t i = 0; i < parameter_count(); ++i){
333                 if (parameter_is_input(i) && parameter_is_control(i)) {
334                         ret.insert (ret.end(), i);
335                 }
336         }
337
338         return ret;
339 }
340
341 int
342 LV2Plugin::connect_and_run (vector<Sample*>& bufs, uint32_t nbufs, int32_t& in_index, int32_t& out_index, nframes_t nframes, nframes_t offset)
343 {
344         uint32_t port_index;
345         cycles_t then, now;
346
347         port_index = 0;
348
349         then = get_cycles ();
350
351         while (port_index < parameter_count()) {
352                 if (parameter_is_audio(port_index)) {
353                         if (parameter_is_input(port_index)) {
354                                 slv2_instance_connect_port(_instance, port_index,
355                                                 bufs[min((uint32_t)in_index, nbufs - 1)] + offset);
356                                 in_index++;
357                         } else if (parameter_is_output(port_index)) {
358                                 slv2_instance_connect_port(_instance, port_index,
359                                                 bufs[min((uint32_t)out_index, nbufs - 1)] + offset);
360                                 out_index++;
361                         }
362                 }
363                 port_index++;
364         }
365         
366         run (nframes);
367         now = get_cycles ();
368         set_cycles ((uint32_t) (now - then));
369
370         return 0;
371 }
372
373 bool
374 LV2Plugin::parameter_is_control (uint32_t param) const
375 {
376         SLV2PortSignature sig = slv2_template_get_port(_template, param);
377         return (slv2_port_signature_get_type(sig) == SLV2_PORT_DATA_TYPE_CONTROL);
378 }
379
380 bool
381 LV2Plugin::parameter_is_audio (uint32_t param) const
382 {
383         SLV2PortSignature sig = slv2_template_get_port(_template, param);
384         return (slv2_port_signature_get_type(sig) == SLV2_PORT_DATA_TYPE_AUDIO);
385 }
386
387 bool
388 LV2Plugin::parameter_is_output (uint32_t param) const
389 {
390         SLV2PortSignature sig = slv2_template_get_port(_template, param);
391         return (slv2_port_signature_get_direction(sig) == SLV2_PORT_DIRECTION_OUTPUT);
392 }
393
394 bool
395 LV2Plugin::parameter_is_input (uint32_t param) const
396 {
397         SLV2PortSignature sig = slv2_template_get_port(_template, param);
398         return (slv2_port_signature_get_direction(sig) == SLV2_PORT_DIRECTION_INPUT);
399 }
400
401 void
402 LV2Plugin::print_parameter (uint32_t param, char *buf, uint32_t len) const
403 {
404         if (buf && len) {
405                 if (param < parameter_count()) {
406                         snprintf (buf, len, "%.3f", get_parameter (param));
407                 } else {
408                         strcat (buf, "0");
409                 }
410         }
411 }
412
413 void
414 LV2Plugin::run (nframes_t nframes)
415 {
416         for (uint32_t i = 0; i < parameter_count(); ++i) {
417                 SLV2PortSignature sig = slv2_template_get_port(_template, i);
418                 if (slv2_port_signature_get_type(sig) == SLV2_PORT_DATA_TYPE_CONTROL
419                                 && slv2_port_signature_get_direction(sig) == SLV2_PORT_DIRECTION_INPUT) {
420                         _control_data[i] = _shadow_data[i];
421                 }
422         }
423
424         slv2_instance_run(_instance, nframes);
425 }
426
427 void
428 LV2Plugin::latency_compute_run ()
429 {
430         if (!_latency_control_port) {
431                 return;
432         }
433
434         /* we need to run the plugin so that it can set its latency
435            parameter.
436         */
437         
438         activate ();
439         
440         uint32_t port_index = 0;
441         uint32_t in_index = 0;
442         uint32_t out_index = 0;
443         const nframes_t bufsize = 1024;
444         float buffer[bufsize];
445
446         memset(buffer,0,sizeof(float)*bufsize);
447                 
448         /* Note that we've already required that plugins
449            be able to handle in-place processing.
450         */
451         
452         port_index = 0;
453         
454         while (port_index < parameter_count()) {
455                 if (parameter_is_audio (port_index)) {
456                         if (parameter_is_input (port_index)) {
457                                 slv2_instance_connect_port (_instance, port_index, buffer);
458                                 in_index++;
459                         } else if (parameter_is_output (port_index)) {
460                                 slv2_instance_connect_port (_instance, port_index, buffer);
461                                 out_index++;
462                         }
463                 }
464                 port_index++;
465         }
466         
467         run (bufsize);
468         deactivate ();
469 }
470
471 LV2PluginInfo::LV2PluginInfo (void* slv2_plugin)
472         : _slv2_plugin(slv2_plugin)
473 {
474 }
475
476 LV2PluginInfo::~LV2PluginInfo()
477 {
478 }
479
480 PluginPtr
481 LV2PluginInfo::load (Session& session)
482 {
483         SLV2Plugin p = (SLV2Plugin)_slv2_plugin;
484         
485         try {
486                 PluginPtr plugin;
487
488                 plugin.reset (new LV2Plugin (session.engine(), session, p, session.frame_rate()));
489
490                 plugin->set_info(PluginInfoPtr(new LV2PluginInfo(*this)));
491                 return plugin;
492         }
493
494         catch (failed_constructor &err) {
495                 return PluginPtr ((Plugin*) 0);
496         }       
497         
498         return PluginPtr();
499 }
500
501 PluginInfoList
502 LV2PluginInfo::discover (void* slv2_world)
503 {
504         PluginInfoList plugs;
505         
506         SLV2Plugins plugins = slv2_world_get_all_plugins((SLV2World)slv2_world);
507
508         for (unsigned i=0; i < slv2_plugins_size(plugins); ++i) {
509                 SLV2Plugin p = slv2_plugins_get_at(plugins, i);
510                 LV2PluginInfoPtr info (new LV2PluginInfo(p));
511
512                 info->name = slv2_plugin_get_name(p);
513
514                 SLV2PluginClass pclass = slv2_plugin_get_class(p);
515                 info->category = slv2_plugin_class_get_label(pclass);
516
517                 char* author_name = slv2_plugin_get_author_name(p);
518                 info->creator = author_name ? string(author_name) : "Unknown";
519                 free(author_name);
520
521                 info->path = "/NOPATH"; // Meaningless for LV2
522
523                 SLV2Template io = slv2_plugin_get_template(p);
524
525                 info->n_inputs = slv2_template_get_num_ports_of_type(io,
526                                 SLV2_PORT_DIRECTION_INPUT, SLV2_PORT_DATA_TYPE_AUDIO);
527                 
528                 info->n_outputs = slv2_template_get_num_ports_of_type(io,
529                                 SLV2_PORT_DIRECTION_OUTPUT, SLV2_PORT_DATA_TYPE_AUDIO);
530
531                 info->unique_id = slv2_plugin_get_uri(p);
532                 info->index = 0; // Meaningless for LV2
533                 
534                 plugs.push_back (info);
535         }
536
537         return plugs;
538 }
539