1 /* GStreamer LADSPA plugin
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 * Copyright (C) 2013 Juan Manuel Borges CaƱo <juanmabcmail@gmail.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
24 * SECTION:element-ladspa
26 * @short_description: bridge for LADSPA (Linux Audio Developer's Simple Plugin API)
27 * @see_also: #GstAudioConvert #GstAudioResample, #GstAudioTestSrc, #GstAutoAudioSink
29 * The LADSPA (Linux Audio Developer's Simple Plugin API) element is a bridge
30 * for plugins using the [LADSPA](http://www.ladspa.org/) API.
32 * It scans all installed LADSPA plugins and registers them as gstreamer
33 * elements. If available it can also parse LRDF files and use the metadata for
34 * element classification. The functionality you get depends on the LADSPA plugins
37 * ## Example LADSPA line without this plugins
40 * (padsp) analyseplugin cmt.so amp_mono
41 * gst-launch-1.0 -e filesrc location="$myfile" ! decodebin ! audioconvert ! audioresample ! "audio/x-raw,format=S16LE,rate=48000,channels=1" ! wavenc ! filesink location="testin.wav"
42 * (padsp) applyplugin testin.wav testout.wav cmt.so amp_mono 2
43 * gst-launch-1.0 playbin uri=file://"$PWD"/testout.wav
44 * ]| Decode any audio file into wav with the format expected for the specific ladspa plugin to be applied, apply the ladspa filter and play it.
46 * Now with this plugin:
48 * ## Example LADSPA line with this plugins
50 * gst-launch-1.0 autoaudiosrc ! ladspa-cmt-so-amp-mono gain=2 ! ladspa-caps-so-plate ! ladspa-tap-echo-so-tap-stereo-echo l-delay=500 r-haas-delay=500 ! tee name=myT myT. ! queue ! autoaudiosink myT. ! queue ! audioconvert ! goom ! videoconvert ! xvimagesink pixel-aspect-ratio=3/4
51 * ]| Get audio input, filter it through CAPS Plate and TAP Stereo Echo, play it and show a visualization (recommended hearphones).
53 * In case you wonder the plugin naming scheme, quoting ladspa.h:
54 * "Plugin types should be identified by file and label rather than by
55 * index or plugin name, which may be changed in new plugin versions."
56 * This is really the best way then, and so it is less prone to conflicts.
58 * Also it is worth noting that LADSPA provides a control in and out interface,
59 * on top of the audio in and out one, so some parameters are readable too.
61 * You can see the listing of plugins available with:
63 * ## Inspecting the plugins list
66 * ]| List available LADSPA plugins on gstreamer.
68 * You can see the parameters of any plugin with:
70 * ## Inspecting the plugins
72 * gst-inspect ladspa-retro-flange-1208-so-retroflange
73 * ]| List details of the plugin, parameters, range and defaults included.
75 * The elements categorize in:
77 * * Filter/Effect/Audio/LADSPA:
79 * ## Example Filter/Effect/Audio/LADSPA line with this plugins
81 * gst-launch-1.0 filesrc location="$myfile" ! decodebin ! audioconvert ! audioresample ! ladspa-calf-so-reverb decay-time=15 high-frq-damp=20000 room-size=5 diffusion=1 wet-amount=2 dry-amount=2 pre-delay=50 bass-cut=20000 treble-cut=20000 ! ladspa-tap-echo-so-tap-stereo-echo l-delay=500 r-haas-delay=500 ! autoaudiosink
82 * ]| Decode any audio file, filter it through Calf Reverb LADSPA then TAP Stereo Echo, and play it.
84 * * Source/Audio/LADSPA:
86 * ## Example Source/Audio/LADSPA line with this plugins
88 * gst-launch-1.0 ladspasrc-sine-so-sine-fcac frequency=220 amplitude=100 ! audioconvert ! autoaudiosink
89 * ]| Generate a sine wave with Sine Oscillator (Freq:control, Amp:control) and play it.
91 * ## Example Source/Audio/LADSPA line with this plugins
93 * gst-launch-1.0 ladspasrc-caps-so-click bpm=240 volume=1 ! autoaudiosink
94 * ]| Generate clicks with CAPS Click - Metronome at 240 beats per minute and play it.
96 * ## Example Source/Audio/LADSPA line with this plugins
98 * gst-launch-1.0 ladspasrc-random-1661-so-random-fcsc-oa ! ladspa-cmt-so-amp-mono gain=1.5 ! ladspa-caps-so-plate ! tee name=myT myT. ! queue ! autoaudiosink myT. ! queue ! audioconvert ! wavescope ! videoconvert ! autovideosink
99 * ]| Generate random wave, filter it trhough Mono Amplifier and Versatile Plate Reverb, and play, while showing, it.
101 * * Sink/Audio/LADSPA:
103 * ## Example Sink/Audio/LADSPA line with this plugins
105 * gst-launch-1.0 autoaudiosrc ! ladspa-cmt-so-amp-mono gain=2 ! ladspa-caps-so-plate ! ladspa-tap-echo-so-tap-stereo-echo l-delay=500 r-haas-delay=500 ! tee name=myT myT. ! audioconvert ! audioresample ! queue ! ladspasink-cmt-so-null-ai myT. ! audioconvert ! audioresample ! queue ! goom ! videoconvert ! xvimagesink pixel-aspect-ratio=3/4
106 * ]| Get audio input, filter it trhough Mono Amplifier, CAPS Plate LADSPA and TAP Stereo Echo, explicitly anulate audio with Null (Audio Output), and play a visualization (recommended hearphones).
114 #include "gstladspautils.h"
115 #include "gstladspafilter.h"
116 #include "gstladspasource.h"
117 #include "gstladspasink.h"
118 #include <gst/gst-i18n-plugin.h>
126 GST_DEBUG_CATEGORY (ladspa_debug);
127 #define GST_CAT_DEFAULT ladspa_debug
130 * 1.0 and the 1.1 preliminary headers don't define a version, but
133 #ifndef LADSPA_VERSION
134 #define LADSPA_VERSION "1.0"
137 #if defined (G_OS_WIN32)
138 #define GST_LADSPA_ENVVARS "APPDATA/LADSPA:COMMONPROGRAMFILES/LADSPA"
139 #define GST_LADSPA_DEFAULT_PATH ""
140 #elif defined (HAVE_OSX)
141 #define GST_LADSPA_ENVVARS "HOME/Library/Audio/Plug-Ins/LADSPA:HOME/.ladspa"
142 #define GST_LADSPA_DEFAULT_PATH \
143 "/usr/local/lib/ladspa:/usr/lib/ladspa:/Library/Audio/Plug-Ins/LADSPA"
144 #elif defined (G_OS_UNIX)
145 #define GST_LADSPA_ENVVARS "HOME/.ladspa"
146 #define GST_LADSPA_DEFAULT_PATH \
148 "/usr/lib64/ladspa:" \
149 "/usr/local/lib/ladspa:" \
150 "/usr/local/lib64/ladspa:" \
153 #error "Unsupported OS"
156 GstStructure *ladspa_meta_all = NULL;
159 ladspa_plugin_register_element (GstPlugin * plugin, GstStructure * ladspa_meta)
161 guint audio_in, audio_out;
163 gst_structure_get_uint (ladspa_meta, "audio-in", &audio_in);
164 gst_structure_get_uint (ladspa_meta, "audio-out", &audio_out);
167 ladspa_register_source_element (plugin, ladspa_meta);
168 } else if (audio_out == 0) {
169 ladspa_register_sink_element (plugin, ladspa_meta);
171 ladspa_register_filter_element (plugin, ladspa_meta);
176 ladspa_count_ports (const LADSPA_Descriptor * descriptor,
177 guint * audio_in, guint * audio_out, guint * control_in,
182 *audio_in = *audio_out = *control_in = *control_out = 0;
184 for (i = 0; i < descriptor->PortCount; i++) {
185 LADSPA_PortDescriptor p = descriptor->PortDescriptors[i];
187 if (LADSPA_IS_PORT_AUDIO (p)) {
188 if (LADSPA_IS_PORT_INPUT (p))
192 } else if (LADSPA_IS_PORT_CONTROL (p)) {
193 if (LADSPA_IS_PORT_INPUT (p))
202 ladspa_describe_plugin (const gchar * file_name, const gchar * entry_name,
203 LADSPA_Descriptor_Function descriptor_function)
205 const LADSPA_Descriptor *desc;
208 /* walk through all the plugins in this plugin library */
209 for (i = 0; (desc = descriptor_function (i)); i++) {
210 GstStructure *ladspa_meta = NULL;
211 GValue value = { 0, };
214 guint audio_in, audio_out, control_in, control_out;
216 /* count ports of this plugin */
217 ladspa_count_ports (desc, &audio_in, &audio_out, &control_in, &control_out);
220 if (audio_in == 0 && audio_out == 0) {
221 GST_WARNING ("Skipping control only element (%s:%lu/%s)",
222 entry_name, desc->UniqueID, desc->Label);
224 } else if (audio_in == 0) {
225 tmp = g_strdup_printf ("ladspasrc-%s-%s", entry_name, desc->Label);
226 } else if (audio_out == 0) {
227 tmp = g_strdup_printf ("ladspasink-%s-%s", entry_name, desc->Label);
229 tmp = g_strdup_printf ("ladspa-%s-%s", entry_name, desc->Label);
231 type_name = g_ascii_strdown (tmp, -1);
233 g_strcanon (type_name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-+", '-');
235 /* check if the type is already registered */
236 if (g_type_from_name (type_name)) {
237 GST_WARNING ("Plugin identifier collision for %s (%s:%lu/%s)", type_name,
238 entry_name, desc->UniqueID, desc->Label);
243 ladspa_meta = gst_structure_new ("ladspa",
244 "plugin-filename", G_TYPE_STRING, file_name,
245 "element-ix", G_TYPE_UINT, i,
246 "element-type-name", G_TYPE_STRING, type_name,
247 "audio-in", G_TYPE_UINT, audio_in,
248 "audio-out", G_TYPE_UINT, audio_out,
249 "control-in", G_TYPE_UINT, control_in,
250 "control-out", G_TYPE_UINT, control_out, NULL);
252 g_value_init (&value, GST_TYPE_STRUCTURE);
253 g_value_set_boxed (&value, ladspa_meta);
254 gst_structure_set_value (ladspa_meta_all, type_name, &value);
255 g_value_unset (&value);
261 ladspa_rdf_directory_search (const char *dir_name)
264 gchar *file_name, *file_uri;
265 const gchar *entry_name;
268 GST_INFO ("scanning directory for rdfs \"%s\"", dir_name);
270 dir = g_dir_open (dir_name, 0, NULL);
274 while ((entry_name = g_dir_read_name (dir))) {
275 file_name = g_build_filename (dir_name, entry_name, NULL);
276 file_uri = g_strconcat ("file://", file_name, NULL);
277 ok = lrdf_read_file (file_uri);
278 GST_INFO ("read %s : %d", file_uri, ok);
288 /* search just the one directory */
290 ladspa_plugin_directory_search (GstPlugin * ladspa_plugin, const char *dir_name)
294 const gchar *entry_name;
295 LADSPA_Descriptor_Function descriptor_function;
299 GST_INFO ("scanning directory for plugins \"%s\"", dir_name);
301 dir = g_dir_open (dir_name, 0, NULL);
305 while ((entry_name = g_dir_read_name (dir))) {
306 /* Only attempt to open files with the module suffixes */
307 if (!g_str_has_suffix (entry_name, "." G_MODULE_SUFFIX)
308 #ifdef GST_EXTRA_MODULE_SUFFIX
309 && !g_str_has_suffix (entry_name, GST_EXTRA_MODULE_SUFFIX)
312 GST_TRACE ("Ignoring file %s as it has the wrong suffix for a plugin",
317 file_name = g_build_filename (dir_name, entry_name, NULL);
318 GST_LOG ("Probing file %s as a LADSPA plugin", file_name);
320 g_module_open (file_name, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
322 /* the file is a shared library */
323 if (g_module_symbol (plugin, "ladspa_descriptor",
324 (gpointer *) & descriptor_function)) {
325 /* we've found a ladspa_descriptor function, now introspect it. */
326 GST_INFO ("Found LADSPA descriptor in %s", file_name);
327 ladspa_describe_plugin (file_name, entry_name, descriptor_function);
330 /* it was a library, but not a LADSPA one. Unload it. */
331 g_module_close (plugin);
341 /* search the plugin path */
343 ladspa_plugin_path_search (GstPlugin * plugin)
345 const gchar *search_path, *path;
346 GString *ladspa_path;
348 gint i, j, path_entries;
349 gboolean res = FALSE, skip;
351 gchar *pos, *prefix, *rdf_path;
354 ladspa_path = g_string_new (NULL);
356 search_path = g_getenv ("LADSPA_PATH");
358 g_string_append_printf (ladspa_path,
359 "%s" G_SEARCHPATH_SEPARATOR_S GST_LADSPA_DEFAULT_PATH, search_path);
361 g_string_append (ladspa_path, GST_LADSPA_DEFAULT_PATH);
365 path = g_getenv ("APPDATA");
367 gchar *path_subdir = g_build_filename (path, "LADSPA", NULL);
368 if (ladspa_path->len)
369 g_string_append_printf (ladspa_path, G_SEARCHPATH_SEPARATOR_S "%s",
372 g_string_append (ladspa_path, path_subdir);
373 g_free (path_subdir);
376 path = g_getenv ("COMMONPROGRAMFILES");
378 gchar *path_subdir = g_build_filename (path, "LADSPA", NULL);
379 if (ladspa_path->len)
380 g_string_append_printf (ladspa_path, G_SEARCHPATH_SEPARATOR_S "%s",
383 g_string_append (ladspa_path, path_subdir);
384 g_free (path_subdir);
387 path = g_getenv ("HOME");
390 if (ladspa_path->len)
391 g_string_append_printf (ladspa_path, ":%s/.ladspa", path);
393 g_string_append_printf (ladspa_path, "%s/.ladspa", path);
395 #if defined (HAVE_IOS) || defined (HAVE_OSX)
396 g_string_append_printf (ladspa_path, ":%s/Library/Audio/Plug-Ins/LADSPA",
402 paths = g_strsplit (ladspa_path->str, G_SEARCHPATH_SEPARATOR_S, 0);
403 path_entries = g_strv_length (paths);
404 GST_INFO ("%d dirs in search paths \"%s\"", path_entries, ladspa_path->str);
407 for (i = 0; i < path_entries; i++) {
409 for (j = 0; j < i; j++) {
410 if (!strcmp (paths[i], paths[j])) {
418 * transform path: /usr/lib/ladspa -> /usr/share/ladspa/rdf/
419 * yes, this is ugly, but lrdf has not searchpath
421 if ((pos = strstr (paths[i], "/lib/ladspa"))) {
422 prefix = g_strndup (paths[i], (pos - paths[i]));
423 rdf_path = g_build_filename (prefix, "share", "ladspa", "rdf", NULL);
424 ladspa_rdf_directory_search (rdf_path);
431 for (i = 0; i < path_entries; i++) {
433 for (j = 0; j < i; j++) {
434 if (!strcmp (paths[i], paths[j])) {
441 res |= ladspa_plugin_directory_search (plugin, paths[i]);
445 g_string_free (ladspa_path, TRUE);
451 plugin_init (GstPlugin * plugin)
453 gboolean res = FALSE;
456 GST_DEBUG_CATEGORY_INIT (ladspa_debug, "ladspa", 0, "LADSPA plugins");
459 GST_DEBUG_OBJECT (plugin, "binding text domain %s to locale dir %s",
460 GETTEXT_PACKAGE, LOCALEDIR);
461 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
462 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
465 gst_plugin_add_dependency_simple (plugin,
466 "LADSPA_PATH:" GST_LADSPA_ENVVARS,
467 GST_LADSPA_DEFAULT_PATH, NULL, GST_PLUGIN_DEPENDENCY_FLAG_NONE);
473 ladspa_meta_all = (GstStructure *) gst_plugin_get_cache_data (plugin);
474 if (ladspa_meta_all) {
475 n = gst_structure_n_fields (ladspa_meta_all);
477 GST_INFO_OBJECT (plugin, "%d entries in cache", n);
479 ladspa_meta_all = gst_structure_new_empty ("ladspa");
480 if ((res = ladspa_plugin_path_search (plugin))) {
481 n = gst_structure_n_fields (ladspa_meta_all);
482 GST_INFO_OBJECT (plugin, "%d entries after scanning", n);
483 gst_plugin_set_cache_data (plugin, ladspa_meta_all);
494 GST_INFO_OBJECT (plugin, "register types");
496 for (i = 0; i < n; i++) {
497 name = gst_structure_nth_field_name (ladspa_meta_all, i);
498 value = gst_structure_get_value (ladspa_meta_all, name);
499 if (G_VALUE_TYPE (value) == GST_TYPE_STRUCTURE) {
500 GstStructure *ladspa_meta = g_value_get_boxed (value);
502 ladspa_plugin_register_element (plugin, ladspa_meta);
508 GST_WARNING_OBJECT (plugin, "no LADSPA plugins found, check LADSPA_PATH");
511 /* we don't want to fail, even if there are no elements registered */
515 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
519 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)