Imported Upstream version 0.10.23
[profile/ivi/gst-plugins-bad.git] / ext / lv2 / gstlv2.c
1 /* GStreamer
2  * Copyright (C) 1999 Erik Walthinsen <omega@cse.ogi.edu>
3  *               2001 Steve Baker <stevebaker_org@yahoo.co.uk>
4  *               2003 Andy Wingo <wingo at pobox.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 /**
23  * SECTION:element-lv2
24  * @short_description: bridge for LV2.
25  *
26  * LV2 is a standard for plugins and matching host applications,
27  * mainly targeted at audio processing and generation.  It is intended as
28  * a successor to LADSPA (Linux Audio Developer's Simple Plugin API).
29  *
30  * The LV2 element is a bridge for plugins using the
31  * <ulink url="http://www.lv2plug.in/">LV2</ulink> API.  It scans all
32  * installed LV2 plugins and registers them as gstreamer elements.
33  */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38 #include <string.h>
39 #include <math.h>
40 #include <glib.h>
41 #include <gst/audio/audio.h>
42 #include <gst/controller/gstcontroller.h>
43 #include <gst/audio/multichannel.h>
44
45 #include "gstlv2.h"
46 #include <slv2/slv2.h>
47
48 #define GST_LV2_DEFAULT_PATH \
49   "/usr/lib/lv2" G_SEARCHPATH_SEPARATOR_S \
50   "/usr/local/lib/lv2" G_SEARCHPATH_SEPARATOR_S \
51   LIBDIR "/lv2"
52
53 static void gst_lv2_set_property (GObject * object,
54     guint prop_id, const GValue * value, GParamSpec * pspec);
55
56 static void gst_lv2_get_property (GObject * object,
57     guint prop_id, GValue * value, GParamSpec * pspec);
58
59 static gboolean gst_lv2_setup (GstSignalProcessor * sigproc, GstCaps * caps);
60 static gboolean gst_lv2_start (GstSignalProcessor * sigproc);
61 static void gst_lv2_stop (GstSignalProcessor * sigproc);
62 static void gst_lv2_cleanup (GstSignalProcessor * sigproc);
63 static void gst_lv2_process (GstSignalProcessor * sigproc, guint nframes);
64
65 static SLV2World world;
66 static SLV2Value audio_class;
67 static SLV2Value control_class;
68 static SLV2Value input_class;
69 static SLV2Value output_class;
70 static SLV2Value integer_prop;
71 static SLV2Value toggled_prop;
72 static SLV2Value in_place_broken_pred;
73 static SLV2Value in_group_pred;
74 static SLV2Value has_role_pred;
75 static SLV2Value lv2_symbol_pred;
76
77 static SLV2Value center_role;
78 static SLV2Value left_role;
79 static SLV2Value right_role;
80 static SLV2Value rear_center_role;
81 static SLV2Value rear_left_role;
82 static SLV2Value rear_right_role;
83 static SLV2Value lfe_role;
84 static SLV2Value center_left_role;
85 static SLV2Value center_right_role;
86 static SLV2Value side_left_role;
87 static SLV2Value side_right_role;
88
89 static GstSignalProcessorClass *parent_class;
90
91 static GstPlugin *gst_lv2_plugin;
92
93 GST_DEBUG_CATEGORY_STATIC (lv2_debug);
94 #define GST_CAT_DEFAULT lv2_debug
95
96 static GQuark descriptor_quark = 0;
97
98
99 /* Convert an LV2 port role to a Gst channel positon
100  * WARNING: If the group has only a single port,
101  * GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER will be returned for pg:centerRole
102  * (which is used by LV2 for mono groups), but this is not correct.  In this
103  * case the value must be changed to GST_AUDIO_CHANNEL_POSITION_FRONT_MONO
104  * (this can't be done by this function because this information isn't known
105  * at the time it is used).
106  */
107 static GstAudioChannelPosition
108 gst_lv2_role_to_position (SLV2Value role)
109 {
110   /* Front.  Mono and left/right are mututally exclusive */
111   if (slv2_value_equals (role, center_role)) {
112
113     return GST_AUDIO_CHANNEL_POSITION_FRONT_CENTER;
114   } else if (slv2_value_equals (role, left_role)) {
115     return GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT;
116   } else if (slv2_value_equals (role, right_role)) {
117     return GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT;
118
119     /* Rear. Left/right and center are mututally exclusive */
120   } else if (slv2_value_equals (role, rear_center_role)) {
121     return GST_AUDIO_CHANNEL_POSITION_REAR_CENTER;
122   } else if (slv2_value_equals (role, rear_left_role)) {
123     return GST_AUDIO_CHANNEL_POSITION_REAR_LEFT;
124   } else if (slv2_value_equals (role, rear_right_role)) {
125     return GST_AUDIO_CHANNEL_POSITION_REAR_RIGHT;
126
127     /* Subwoofer/low-frequency-effects */
128   } else if (slv2_value_equals (role, lfe_role)) {
129     return GST_AUDIO_CHANNEL_POSITION_LFE;
130
131     /* Center front speakers. Center and left/right_of_center
132      * are mutually exclusive */
133   } else if (slv2_value_equals (role, center_left_role)) {
134     return GST_AUDIO_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER;
135   } else if (slv2_value_equals (role, center_right_role)) {
136     return GST_AUDIO_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER;
137
138     /* sides */
139   } else if (slv2_value_equals (role, side_left_role)) {
140     return GST_AUDIO_CHANNEL_POSITION_SIDE_LEFT;
141   } else if (slv2_value_equals (role, side_right_role)) {
142     return GST_AUDIO_CHANNEL_POSITION_SIDE_RIGHT;
143   }
144
145   return GST_AUDIO_CHANNEL_POSITION_INVALID;
146 }
147
148 /* Find and return the group @a uri in @a groups, or NULL if not found */
149 static GstLV2Group *
150 gst_lv2_class_find_group (GArray * groups, SLV2Value uri)
151 {
152   int i = 0;
153   for (; i < groups->len; ++i)
154     if (slv2_value_equals (g_array_index (groups, GstLV2Group, i).uri, uri))
155       return &g_array_index (groups, GstLV2Group, i);
156   return NULL;
157 }
158
159 static GstAudioChannelPosition *
160 gst_lv2_build_positions (GstLV2Group * group)
161 {
162   GstAudioChannelPosition *positions = NULL;
163
164   /* don't do anything for mono */
165   if (group->ports->len > 1) {
166     gint i;
167
168     positions = g_new (GstAudioChannelPosition, group->ports->len);
169     for (i = 0; i < group->ports->len; ++i)
170       positions[i] = g_array_index (group->ports, GstLV2Port, i).position;
171   }
172   return positions;
173 }
174
175 static void
176 gst_lv2_base_init (gpointer g_class)
177 {
178   GstLV2Class *klass = (GstLV2Class *) g_class;
179   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
180   GstSignalProcessorClass *gsp_class = GST_SIGNAL_PROCESSOR_CLASS (g_class);
181   SLV2Plugin lv2plugin;
182   SLV2Value val;
183   SLV2Values values, sub_values;
184   GstLV2Group *group = NULL;
185   GstAudioChannelPosition position = GST_AUDIO_CHANNEL_POSITION_INVALID;
186   guint j, in_pad_index = 0, out_pad_index = 0;
187   const gchar *klass_tags;
188   gchar *longname, *author;
189
190   lv2plugin = (SLV2Plugin) g_type_get_qdata (G_OBJECT_CLASS_TYPE (klass),
191       descriptor_quark);
192
193   g_assert (lv2plugin);
194
195   GST_DEBUG ("base_init %p, plugin %s", g_class,
196       slv2_value_as_string (slv2_plugin_get_uri (lv2plugin)));
197
198   gsp_class->num_group_in = 0;
199   gsp_class->num_group_out = 0;
200   gsp_class->num_audio_in = 0;
201   gsp_class->num_audio_out = 0;
202   gsp_class->num_control_in = 0;
203   gsp_class->num_control_out = 0;
204
205   klass->in_groups = g_array_new (FALSE, TRUE, sizeof (GstLV2Group));
206   klass->out_groups = g_array_new (FALSE, TRUE, sizeof (GstLV2Group));
207   klass->audio_in_ports = g_array_new (FALSE, TRUE, sizeof (GstLV2Port));
208   klass->audio_out_ports = g_array_new (FALSE, TRUE, sizeof (GstLV2Port));
209   klass->control_in_ports = g_array_new (FALSE, TRUE, sizeof (GstLV2Port));
210   klass->control_out_ports = g_array_new (FALSE, TRUE, sizeof (GstLV2Port));
211
212   /* find ports and groups */
213   for (j = 0; j < slv2_plugin_get_num_ports (lv2plugin); j++) {
214     const SLV2Port port = slv2_plugin_get_port_by_index (lv2plugin, j);
215     const gboolean is_input = slv2_port_is_a (lv2plugin, port, input_class);
216     gboolean in_group = FALSE;
217     struct _GstLV2Port desc = { j, 0, };
218     values = slv2_port_get_value (lv2plugin, port, in_group_pred);
219
220     if (slv2_values_size (values) > 0) {
221       /* port is part of a group */
222       SLV2Value group_uri = slv2_values_get_at (values, 0);
223       GArray *groups = is_input ? klass->in_groups : klass->out_groups;
224       GstLV2Group *group = gst_lv2_class_find_group (groups, group_uri);
225       in_group = TRUE;
226       if (group == NULL) {
227         GstLV2Group g;
228         g.uri = slv2_value_duplicate (group_uri);
229         g.pad = is_input ? in_pad_index++ : out_pad_index++;
230         g.ports = g_array_new (FALSE, TRUE, sizeof (GstLV2Port));
231         g.has_roles = TRUE;
232         g.symbol = NULL;
233         sub_values = slv2_plugin_get_value_for_subject (lv2plugin, group_uri,
234             lv2_symbol_pred);
235         /* symbol is mandatory */
236         if (slv2_values_size (sub_values) > 0) {
237           g.symbol = slv2_value_duplicate (slv2_values_get_at (sub_values, 0));
238           if (!gst_element_class_get_pad_template (element_class,
239                   slv2_value_as_string (g.symbol))) {
240             g_array_append_val (groups, g);
241             group = &g_array_index (groups, GstLV2Group, groups->len - 1);
242             assert (group);
243             assert (slv2_value_equals (group->uri, group_uri));
244           } else {
245             GST_WARNING ("plugin %s has duplicate group symbol '%s'",
246                 slv2_value_as_string (slv2_plugin_get_uri (lv2plugin)),
247                 slv2_value_as_string (g.symbol));
248             in_group = FALSE;
249           }
250         } else {
251           GST_WARNING ("plugin %s has illegal group with no symbol",
252               slv2_value_as_string (slv2_plugin_get_uri (lv2plugin)));
253           in_group = FALSE;
254         }
255       }
256
257       if (in_group) {
258         position = GST_AUDIO_CHANNEL_POSITION_INVALID;
259         sub_values = slv2_port_get_value (lv2plugin, port, has_role_pred);
260         if (slv2_values_size (sub_values) > 0) {
261           SLV2Value role = slv2_values_get_at (sub_values, 0);
262           position = gst_lv2_role_to_position (role);
263         }
264         slv2_values_free (sub_values);
265         if (position != GST_AUDIO_CHANNEL_POSITION_INVALID) {
266           desc.position = position;
267           g_array_append_val (group->ports, desc);
268         } else {
269           in_group = FALSE;
270         }
271       }
272     }
273
274     if (!in_group) {
275       /* port is not part of a group, or it is part of a group but that group
276        * is illegal so we just ignore it */
277       if (slv2_port_is_a (lv2plugin, port, audio_class)) {
278         desc.pad = is_input ? in_pad_index++ : out_pad_index++;
279         if (is_input)
280           g_array_append_val (klass->audio_in_ports, desc);
281         else
282           g_array_append_val (klass->audio_out_ports, desc);
283       } else if (slv2_port_is_a (lv2plugin, port, control_class)) {
284         if (is_input)
285           g_array_append_val (klass->control_in_ports, desc);
286         else
287           g_array_append_val (klass->control_out_ports, desc);
288       } else {
289         /* unknown port type */
290         GST_INFO ("unhandled port %d", j);
291         continue;
292       }
293     }
294     slv2_values_free (values);
295   }
296
297   gsp_class->num_group_in = klass->in_groups->len;
298   gsp_class->num_group_out = klass->out_groups->len;
299   gsp_class->num_audio_in = klass->audio_in_ports->len;
300   gsp_class->num_audio_out = klass->audio_out_ports->len;
301   gsp_class->num_control_in = klass->control_in_ports->len;
302   gsp_class->num_control_out = klass->control_out_ports->len;
303
304   /* add input group pad templates */
305   for (j = 0; j < gsp_class->num_group_in; ++j) {
306     group = &g_array_index (klass->in_groups, GstLV2Group, j);
307
308     gst_signal_processor_class_add_pad_template (gsp_class,
309         slv2_value_as_string (group->symbol), GST_PAD_SINK, j,
310         group->ports->len);
311   }
312
313   /* add output group pad templates */
314   for (j = 0; j < gsp_class->num_group_out; ++j) {
315     group = &g_array_index (klass->out_groups, GstLV2Group, j);
316
317     gst_signal_processor_class_add_pad_template (gsp_class,
318         slv2_value_as_string (group->symbol), GST_PAD_SRC, j,
319         group->ports->len);
320   }
321
322   /* add non-grouped input port pad templates */
323   for (j = 0; j < gsp_class->num_audio_in; ++j) {
324     struct _GstLV2Port *desc =
325         &g_array_index (klass->audio_in_ports, GstLV2Port, j);
326     SLV2Port port = slv2_plugin_get_port_by_index (lv2plugin, desc->index);
327     const gchar *name =
328         slv2_value_as_string (slv2_port_get_symbol (lv2plugin, port));
329     gst_signal_processor_class_add_pad_template (gsp_class, name, GST_PAD_SINK,
330         j, 1);
331   }
332
333   /* add non-grouped output port pad templates */
334   for (j = 0; j < gsp_class->num_audio_out; ++j) {
335     struct _GstLV2Port *desc =
336         &g_array_index (klass->audio_out_ports, GstLV2Port, j);
337     SLV2Port port = slv2_plugin_get_port_by_index (lv2plugin, desc->index);
338     const gchar *name =
339         slv2_value_as_string (slv2_port_get_symbol (lv2plugin, port));
340     gst_signal_processor_class_add_pad_template (gsp_class, name, GST_PAD_SRC,
341         j, 1);
342   }
343
344   val = slv2_plugin_get_name (lv2plugin);
345   if (val) {
346     longname = g_strdup (slv2_value_as_string (val));
347     slv2_value_free (val);
348   } else {
349     longname = g_strdup ("no description available");
350   }
351   val = slv2_plugin_get_author_name (lv2plugin);
352   if (val) {
353     author = g_strdup (slv2_value_as_string (val));
354     slv2_value_free (val);
355   } else {
356     author = g_strdup ("no author available");
357   }
358
359   if (gsp_class->num_audio_in == 0)
360     klass_tags = "Source/Audio/LV2";
361   else if (gsp_class->num_audio_out == 0) {
362     if (gsp_class->num_control_out == 0)
363       klass_tags = "Sink/Audio/LV2";
364     else
365       klass_tags = "Sink/Analyzer/Audio/LV2";
366   } else
367     klass_tags = "Filter/Effect/Audio/LV2";
368
369   GST_INFO ("tags : %s", klass_tags);
370   gst_element_class_set_details_simple (element_class, longname,
371       klass_tags, longname, author);
372   g_free (longname);
373   g_free (author);
374
375   if (!slv2_plugin_has_feature (lv2plugin, in_place_broken_pred))
376     GST_SIGNAL_PROCESSOR_CLASS_SET_CAN_PROCESS_IN_PLACE (klass);
377
378   klass->plugin = lv2plugin;
379 }
380
381 static gchar *
382 gst_lv2_class_get_param_name (GstLV2Class * klass, SLV2Port port)
383 {
384   SLV2Plugin lv2plugin = klass->plugin;
385   gchar *ret;
386
387   ret = g_strdup (slv2_value_as_string (slv2_port_get_symbol (lv2plugin,
388               port)));
389
390   /* this is the same thing that param_spec_* will do */
391   g_strcanon (ret, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
392   /* satisfy glib2 (argname[0] must be [A-Za-z]) */
393   if (!((ret[0] >= 'a' && ret[0] <= 'z') || (ret[0] >= 'A' && ret[0] <= 'Z'))) {
394     gchar *tempstr = ret;
395
396     ret = g_strconcat ("param-", ret, NULL);
397     g_free (tempstr);
398   }
399
400   /* check for duplicate property names */
401   if (g_object_class_find_property (G_OBJECT_CLASS (klass), ret)) {
402     gint n = 1;
403     gchar *nret = g_strdup_printf ("%s-%d", ret, n++);
404
405     while (g_object_class_find_property (G_OBJECT_CLASS (klass), nret)) {
406       g_free (nret);
407       nret = g_strdup_printf ("%s-%d", ret, n++);
408     }
409     g_free (ret);
410     ret = nret;
411   }
412
413   GST_DEBUG ("built property name '%s' from port name '%s'", ret,
414       slv2_value_as_string (slv2_port_get_symbol (lv2plugin, port)));
415
416   return ret;
417 }
418
419 static gchar *
420 gst_lv2_class_get_param_nick (GstLV2Class * klass, SLV2Port port)
421 {
422   SLV2Plugin lv2plugin = klass->plugin;
423
424   return g_strdup (slv2_value_as_string (slv2_port_get_name (lv2plugin, port)));
425 }
426
427 static GParamSpec *
428 gst_lv2_class_get_param_spec (GstLV2Class * klass, gint portnum)
429 {
430   SLV2Plugin lv2plugin = klass->plugin;
431   SLV2Port port = slv2_plugin_get_port_by_index (lv2plugin, portnum);
432   SLV2Value lv2def, lv2min, lv2max;
433   GParamSpec *ret;
434   gchar *name, *nick;
435   gint perms;
436   gfloat lower = 0.0f, upper = 1.0f, def = 0.0f;
437
438   nick = gst_lv2_class_get_param_nick (klass, port);
439   name = gst_lv2_class_get_param_name (klass, port);
440
441   GST_DEBUG ("%s trying port %s : %s",
442       slv2_value_as_string (slv2_plugin_get_uri (lv2plugin)), name, nick);
443
444   perms = G_PARAM_READABLE;
445   if (slv2_port_is_a (lv2plugin, port, input_class))
446     perms |= G_PARAM_WRITABLE | G_PARAM_CONSTRUCT;
447   if (slv2_port_is_a (lv2plugin, port, control_class))
448     perms |= GST_PARAM_CONTROLLABLE;
449
450   if (slv2_port_has_property (lv2plugin, port, toggled_prop)) {
451     ret = g_param_spec_boolean (name, nick, nick, FALSE, perms);
452     goto done;
453   }
454
455   slv2_port_get_range (lv2plugin, port, &lv2def, &lv2min, &lv2max);
456
457   if (lv2def)
458     def = slv2_value_as_float (lv2def);
459   if (lv2min)
460     lower = slv2_value_as_float (lv2min);
461   if (lv2max)
462     upper = slv2_value_as_float (lv2max);
463
464   slv2_value_free (lv2def);
465   slv2_value_free (lv2min);
466   slv2_value_free (lv2max);
467
468   if (def < lower) {
469     GST_WARNING ("%s has lower bound %f > default %f",
470         slv2_value_as_string (slv2_plugin_get_uri (lv2plugin)), lower, def);
471     lower = def;
472   }
473
474   if (def > upper) {
475     GST_WARNING ("%s has upper bound %f < default %f",
476         slv2_value_as_string (slv2_plugin_get_uri (lv2plugin)), upper, def);
477     upper = def;
478   }
479
480   if (slv2_port_has_property (lv2plugin, port, integer_prop))
481     ret = g_param_spec_int (name, nick, nick, lower, upper, def, perms);
482   else
483     ret = g_param_spec_float (name, nick, nick, lower, upper, def, perms);
484
485 done:
486   g_free (name);
487   g_free (nick);
488
489   return ret;
490 }
491
492 static void
493 gst_lv2_class_init (GstLV2Class * klass, SLV2Plugin lv2plugin)
494 {
495   GObjectClass *gobject_class;
496   GstSignalProcessorClass *gsp_class;
497   GParamSpec *p;
498   gint i, ix;
499
500   GST_DEBUG ("class_init %p", klass);
501
502   gobject_class = (GObjectClass *) klass;
503   gobject_class->set_property = gst_lv2_set_property;
504   gobject_class->get_property = gst_lv2_get_property;
505
506   gsp_class = GST_SIGNAL_PROCESSOR_CLASS (klass);
507   gsp_class->setup = gst_lv2_setup;
508   gsp_class->start = gst_lv2_start;
509   gsp_class->stop = gst_lv2_stop;
510   gsp_class->cleanup = gst_lv2_cleanup;
511   gsp_class->process = gst_lv2_process;
512
513   klass->plugin = lv2plugin;
514
515   /* properties have an offset of 1 */
516   ix = 1;
517
518   /* register properties */
519
520   for (i = 0; i < gsp_class->num_control_in; i++, ix++) {
521     p = gst_lv2_class_get_param_spec (klass,
522         g_array_index (klass->control_in_ports, GstLV2Port, i).index);
523
524     g_object_class_install_property (gobject_class, ix, p);
525   }
526
527   for (i = 0; i < gsp_class->num_control_out; i++, ix++) {
528     p = gst_lv2_class_get_param_spec (klass,
529         g_array_index (klass->control_out_ports, GstLV2Port, i).index);
530
531     g_object_class_install_property (gobject_class, ix, p);
532   }
533 }
534
535 static void
536 gst_lv2_init (GstLV2 * lv2, GstLV2Class * klass)
537 {
538   lv2->plugin = klass->plugin;
539   lv2->instance = NULL;
540   lv2->activated = FALSE;
541 }
542
543 static void
544 gst_lv2_set_property (GObject * object, guint prop_id, const GValue * value,
545     GParamSpec * pspec)
546 {
547   GstSignalProcessor *gsp;
548   GstSignalProcessorClass *gsp_class;
549
550   gsp = GST_SIGNAL_PROCESSOR (object);
551   gsp_class = GST_SIGNAL_PROCESSOR_GET_CLASS (object);
552
553   /* remember, properties have an offset of 1 */
554   prop_id--;
555
556   /* only input ports */
557   g_return_if_fail (prop_id < gsp_class->num_control_in);
558
559   /* now see what type it is */
560   switch (pspec->value_type) {
561     case G_TYPE_BOOLEAN:
562       gsp->control_in[prop_id] = g_value_get_boolean (value) ? 0.0f : 1.0f;
563       break;
564     case G_TYPE_INT:
565       gsp->control_in[prop_id] = g_value_get_int (value);
566       break;
567     case G_TYPE_FLOAT:
568       gsp->control_in[prop_id] = g_value_get_float (value);
569       break;
570     default:
571       g_assert_not_reached ();
572   }
573 }
574
575 static void
576 gst_lv2_get_property (GObject * object, guint prop_id, GValue * value,
577     GParamSpec * pspec)
578 {
579   GstSignalProcessor *gsp;
580   GstSignalProcessorClass *gsp_class;
581   gfloat *controls;
582
583   gsp = GST_SIGNAL_PROCESSOR (object);
584   gsp_class = GST_SIGNAL_PROCESSOR_GET_CLASS (object);
585
586   /* remember, properties have an offset of 1 */
587   prop_id--;
588
589   if (prop_id < gsp_class->num_control_in) {
590     controls = gsp->control_in;
591   } else if (prop_id < gsp_class->num_control_in + gsp_class->num_control_out) {
592     controls = gsp->control_out;
593     prop_id -= gsp_class->num_control_in;
594   } else {
595     g_return_if_reached ();
596   }
597
598   /* now see what type it is */
599   switch (pspec->value_type) {
600     case G_TYPE_BOOLEAN:
601       g_value_set_boolean (value, controls[prop_id] > 0.0f);
602       break;
603     case G_TYPE_INT:
604       g_value_set_int (value, CLAMP (controls[prop_id], G_MININT, G_MAXINT));
605       break;
606     case G_TYPE_FLOAT:
607       g_value_set_float (value, controls[prop_id]);
608       break;
609     default:
610       g_return_if_reached ();
611   }
612 }
613
614 static gboolean
615 gst_lv2_setup (GstSignalProcessor * gsp, GstCaps * caps)
616 {
617   GstLV2 *lv2;
618   GstLV2Class *oclass;
619   GstSignalProcessorClass *gsp_class;
620   GstStructure *s;
621   gint i;
622   GstLV2Group *group = NULL;
623   GstAudioChannelPosition *positions = NULL;
624   GstPad *pad;
625
626   gsp_class = GST_SIGNAL_PROCESSOR_GET_CLASS (gsp);
627   lv2 = (GstLV2 *) gsp;
628   oclass = (GstLV2Class *) gsp_class;
629
630   g_return_val_if_fail (lv2->activated == FALSE, FALSE);
631
632   GST_DEBUG_OBJECT (lv2, "instantiating the plugin at %d Hz", gsp->sample_rate);
633
634   if (!(lv2->instance =
635           slv2_plugin_instantiate (oclass->plugin, gsp->sample_rate, NULL)))
636     goto no_instance;
637
638   /* connect the control ports */
639   for (i = 0; i < gsp_class->num_control_in; i++)
640     slv2_instance_connect_port (lv2->instance,
641         g_array_index (oclass->control_in_ports, GstLV2Port, i).index,
642         &(gsp->control_in[i]));
643   for (i = 0; i < gsp_class->num_control_out; i++)
644     slv2_instance_connect_port (lv2->instance,
645         g_array_index (oclass->control_out_ports, GstLV2Port, i).index,
646         &(gsp->control_out[i]));
647
648   /* set input group pad audio channel position */
649   for (i = 0; i < gsp_class->num_group_in; ++i) {
650     group = &g_array_index (oclass->in_groups, GstLV2Group, i);
651     if (group->has_roles) {
652       if ((positions = gst_lv2_build_positions (group))) {
653         if ((pad = gst_element_get_static_pad (GST_ELEMENT (gsp),
654                     slv2_value_as_string (group->symbol)))) {
655           GST_INFO_OBJECT (lv2, "set audio channel positions on sink pad %s",
656               slv2_value_as_string (group->symbol));
657           s = gst_caps_get_structure (caps, 0);
658           gst_audio_set_channel_positions (s, positions);
659           gst_object_unref (pad);
660         }
661         g_free (positions);
662         positions = NULL;
663       }
664     }
665   }
666   /* set output group pad audio channel position */
667   for (i = 0; i < gsp_class->num_group_out; ++i) {
668     group = &g_array_index (oclass->out_groups, GstLV2Group, i);
669     if (group->has_roles) {
670       if ((positions = gst_lv2_build_positions (group))) {
671         if ((pad = gst_element_get_static_pad (GST_ELEMENT (gsp),
672                     slv2_value_as_string (group->symbol)))) {
673           GST_INFO_OBJECT (lv2, "set audio channel positions on src pad %s",
674               slv2_value_as_string (group->symbol));
675           s = gst_caps_get_structure (caps, 0);
676           gst_audio_set_channel_positions (s, positions);
677           gst_object_unref (pad);
678         }
679         g_free (positions);
680         positions = NULL;
681       }
682     }
683   }
684   return TRUE;
685
686 no_instance:
687   {
688     GST_WARNING_OBJECT (gsp, "could not create instance");
689     return FALSE;
690   }
691 }
692
693 static gboolean
694 gst_lv2_start (GstSignalProcessor * gsp)
695 {
696   GstLV2 *lv2 = (GstLV2 *) gsp;
697
698   g_return_val_if_fail (lv2->activated == FALSE, FALSE);
699   g_return_val_if_fail (lv2->instance != NULL, FALSE);
700
701   GST_DEBUG_OBJECT (lv2, "activating");
702
703   slv2_instance_activate (lv2->instance);
704
705   lv2->activated = TRUE;
706
707   return TRUE;
708 }
709
710 static void
711 gst_lv2_stop (GstSignalProcessor * gsp)
712 {
713   GstLV2 *lv2 = (GstLV2 *) gsp;
714
715   g_return_if_fail (lv2->activated == TRUE);
716   g_return_if_fail (lv2->instance != NULL);
717
718   GST_DEBUG_OBJECT (lv2, "deactivating");
719
720   slv2_instance_deactivate (lv2->instance);
721
722   lv2->activated = FALSE;
723 }
724
725 static void
726 gst_lv2_cleanup (GstSignalProcessor * gsp)
727 {
728   GstLV2 *lv2 = (GstLV2 *) gsp;
729
730   g_return_if_fail (lv2->activated == FALSE);
731   g_return_if_fail (lv2->instance != NULL);
732
733   GST_DEBUG_OBJECT (lv2, "cleaning up");
734
735   slv2_instance_free (lv2->instance);
736
737   lv2->instance = NULL;
738 }
739
740 static void
741 gst_lv2_process (GstSignalProcessor * gsp, guint nframes)
742 {
743   GstSignalProcessorClass *gsp_class;
744   GstLV2 *lv2;
745   GstLV2Class *lv2_class;
746   GstLV2Group *lv2_group;
747   GstLV2Port *lv2_port;
748   GstSignalProcessorGroup *gst_group;
749   guint i, j;
750
751   gsp_class = GST_SIGNAL_PROCESSOR_GET_CLASS (gsp);
752   lv2 = (GstLV2 *) gsp;
753   lv2_class = (GstLV2Class *) gsp_class;
754
755   /* multi channel inputs */
756   for (i = 0; i < gsp_class->num_group_in; i++) {
757     lv2_group = &g_array_index (lv2_class->in_groups, GstLV2Group, i);
758     gst_group = &gsp->group_in[i];
759     for (j = 0; j < lv2_group->ports->len; ++j) {
760       lv2_port = &g_array_index (lv2_group->ports, GstLV2Port, j);
761       slv2_instance_connect_port (lv2->instance, lv2_port->index,
762           gst_group->buffer + (j * nframes));
763     }
764   }
765   /* mono inputs */
766   for (i = 0; i < gsp_class->num_audio_in; i++) {
767     lv2_port = &g_array_index (lv2_class->audio_in_ports, GstLV2Port, i);
768     slv2_instance_connect_port (lv2->instance, lv2_port->index,
769         gsp->audio_in[i]);
770   }
771   /* multi channel outputs */
772   for (i = 0; i < gsp_class->num_group_out; i++) {
773     lv2_group = &g_array_index (lv2_class->out_groups, GstLV2Group, i);
774     gst_group = &gsp->group_out[i];
775     for (j = 0; j < lv2_group->ports->len; ++j) {
776       lv2_port = &g_array_index (lv2_group->ports, GstLV2Port, j);
777       slv2_instance_connect_port (lv2->instance, lv2_port->index,
778           gst_group->buffer + (j * nframes));
779     }
780   }
781   /* mono outputs */
782   for (i = 0; i < gsp_class->num_audio_out; i++) {
783     lv2_port = &g_array_index (lv2_class->audio_out_ports, GstLV2Port, i);
784     slv2_instance_connect_port (lv2->instance, lv2_port->index,
785         gsp->audio_out[i]);
786   }
787
788   slv2_instance_run (lv2->instance, nframes);
789 }
790
791 /* search the plugin path
792  */
793 static gboolean
794 lv2_plugin_discover (void)
795 {
796   guint i, j;
797   SLV2Plugins plugins = slv2_world_get_all_plugins (world);
798
799   for (i = 0; i < slv2_plugins_size (plugins); ++i) {
800     SLV2Plugin lv2plugin = slv2_plugins_get_at (plugins, i);
801     gint num_audio_ports = 0;
802     const gchar *plugin_uri, *p;
803     gchar *type_name;
804     GTypeInfo typeinfo = {
805       sizeof (GstLV2Class),
806       (GBaseInitFunc) gst_lv2_base_init,
807       NULL,
808       (GClassInitFunc) gst_lv2_class_init,
809       NULL,
810       lv2plugin,
811       sizeof (GstLV2),
812       0,
813       (GInstanceInitFunc) gst_lv2_init,
814     };
815     GType type;
816
817     plugin_uri = slv2_value_as_uri (slv2_plugin_get_uri (lv2plugin));
818     /* construct the type name from plugin URI */
819     if ((p = strstr (plugin_uri, "://"))) {
820       /* cut off the protocol (e.g. http://) */
821       type_name = g_strdup (&p[3]);
822     } else {
823       type_name = g_strdup (plugin_uri);
824     }
825     g_strcanon (type_name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-+", '-');
826
827     /* if it's already registered, drop it */
828     if (g_type_from_name (type_name))
829       goto next;
830
831     /* check if this has any audio ports */
832     for (j = 0; j < slv2_plugin_get_num_ports (lv2plugin); j++) {
833       const SLV2Port port = slv2_plugin_get_port_by_index (lv2plugin, j);
834       if (slv2_port_is_a (lv2plugin, port, audio_class)) {
835         num_audio_ports++;
836       }
837     }
838     if (!num_audio_ports) {
839       GST_INFO ("plugin %s has no audio ports", type_name);
840       goto next;
841     }
842
843     /* create the type */
844     type =
845         g_type_register_static (GST_TYPE_SIGNAL_PROCESSOR, type_name, &typeinfo,
846         0);
847
848     /* FIXME: not needed anymore when we can add pad templates, etc in class_init
849      * as class_data contains the LADSPA_Descriptor too */
850     g_type_set_qdata (type, descriptor_quark, (gpointer) lv2plugin);
851
852     if (!gst_element_register (gst_lv2_plugin, type_name, GST_RANK_NONE, type))
853       goto next;
854
855   next:
856     g_free (type_name);
857   }
858   return TRUE;
859 }
860
861 static gboolean
862 plugin_init (GstPlugin * plugin)
863 {
864   GST_DEBUG_CATEGORY_INIT (lv2_debug, "lv2",
865       GST_DEBUG_FG_GREEN | GST_DEBUG_BG_BLACK | GST_DEBUG_BOLD, "LV2");
866
867   world = slv2_world_new ();
868   slv2_world_load_all (world);
869
870   audio_class = slv2_value_new_uri (world, SLV2_PORT_CLASS_AUDIO);
871   control_class = slv2_value_new_uri (world, SLV2_PORT_CLASS_CONTROL);
872   input_class = slv2_value_new_uri (world, SLV2_PORT_CLASS_INPUT);
873   output_class = slv2_value_new_uri (world, SLV2_PORT_CLASS_OUTPUT);
874
875 #define NS_LV2 "http://lv2plug.in/ns/lv2core#"
876 #define NS_PG  "http://lv2plug.in/ns/ext/port-groups"
877
878   integer_prop = slv2_value_new_uri (world, NS_LV2 "integer");
879   toggled_prop = slv2_value_new_uri (world, NS_LV2 "toggled");
880   in_place_broken_pred = slv2_value_new_uri (world, NS_LV2 "inPlaceBroken");
881   in_group_pred = slv2_value_new_uri (world, NS_PG "inGroup");
882   has_role_pred = slv2_value_new_uri (world, NS_PG "role");
883   lv2_symbol_pred = slv2_value_new_string (world, NS_LV2 "symbol");
884
885   center_role = slv2_value_new_uri (world, NS_PG "centerChannel");
886   left_role = slv2_value_new_uri (world, NS_PG "leftChannel");
887   right_role = slv2_value_new_uri (world, NS_PG "rightChannel");
888   rear_center_role = slv2_value_new_uri (world, NS_PG "rearCenterChannel");
889   rear_left_role = slv2_value_new_uri (world, NS_PG "rearLeftChannel");
890   rear_right_role = slv2_value_new_uri (world, NS_PG "rearRightChannel");
891   lfe_role = slv2_value_new_uri (world, NS_PG "lfeChannel");
892   center_left_role = slv2_value_new_uri (world, NS_PG "centerLeftChannel");
893   center_right_role = slv2_value_new_uri (world, NS_PG "centerRightChannel");
894   side_left_role = slv2_value_new_uri (world, NS_PG "sideLeftChannel");
895   side_right_role = slv2_value_new_uri (world, NS_PG "sideRightChannel");
896
897   /* initialize gst controller library */
898   gst_controller_init (NULL, NULL);
899
900   gst_plugin_add_dependency_simple (plugin,
901       "LV2_PATH", GST_LV2_DEFAULT_PATH, NULL, GST_PLUGIN_DEPENDENCY_FLAG_NONE);
902
903   parent_class = g_type_class_ref (GST_TYPE_SIGNAL_PROCESSOR);
904
905   gst_lv2_plugin = plugin;
906   descriptor_quark = g_quark_from_static_string ("slv2-plugin");
907
908   /* ensure GstAudioChannelPosition type is registered */
909   if (!gst_audio_channel_position_get_type ())
910     return FALSE;
911
912   if (!lv2_plugin_discover ()) {
913     GST_WARNING ("no lv2 plugins found, check LV2_PATH");
914   }
915
916   /* we don't want to fail, even if there are no elements registered */
917   return TRUE;
918 }
919
920 #ifdef __GNUC__
921 __attribute__ ((destructor))
922 #endif
923      static void plugin_cleanup (GstPlugin * plugin)
924 {
925   slv2_value_free (audio_class);
926   slv2_value_free (control_class);
927   slv2_value_free (input_class);
928   slv2_value_free (output_class);
929
930   slv2_value_free (integer_prop);
931   slv2_value_free (toggled_prop);
932   slv2_value_free (in_place_broken_pred);
933   slv2_value_free (in_group_pred);
934   slv2_value_free (has_role_pred);
935   slv2_value_free (lv2_symbol_pred);
936
937   slv2_value_free (center_role);
938   slv2_value_free (left_role);
939   slv2_value_free (right_role);
940   slv2_value_free (rear_center_role);
941   slv2_value_free (rear_left_role);
942   slv2_value_free (rear_right_role);
943   slv2_value_free (lfe_role);
944   slv2_value_free (center_left_role);
945   slv2_value_free (center_right_role);
946   slv2_value_free (side_left_role);
947   slv2_value_free (side_right_role);
948
949   slv2_world_free (world);
950 }
951
952 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
953     GST_VERSION_MINOR,
954     "lv2",
955     "All LV2 plugins",
956     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)