10l
[mplayer.git] / libaf / af_ladspa.c
blob320c341f874f6315c3d609a1778ba48d432504f5
1 /* ------------------------------------------------------------------------- */
3 /*
4 * af_ladspa.c, LADSPA plugin loader
6 * Written by Ivo van Poorten <ivop@euronet.nl>
7 * Copyright (C) 2004, 2005
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * Changelog
26 * 2005-06-21 Replaced erroneous use of mp_msg by af_msg
27 * 2004-12-23 Added to CVS
28 * 2004-12-22 Cleaned up cosmetics
29 * Made conversion loops in play() more cache-friendly
30 * 2004-12-20 Fixed bug for stereo effect on mono signal
31 * (trivial >1 to >=1 change; would segfault otherwise :-) )
32 * Removed trailing whitespace and fixed warn/err messages
33 * Have CONTROL_REINIT return a proper value
34 * 2004-12-13 More Doxygen comments
35 * 2004-12-12 Made af_ladspa optional (updated configure, af.c, etc.)
36 * 2004-12-11 Added deactivate and cleanup to uninit.
37 * Finished Doxygen comments.
38 * Moved translatable messages to help_mp-en.h
39 * 2004-12-10 Added ranges to list of controls for ease of use.
40 * Fixed sig11 bug. Implemented (dummy) outputcontrols. Some
41 * perfectly normal audio processing filters also have output
42 * controls.
43 * 2004-12-08 Added support for generators (no input, one output)
44 * Added support for stereo effects
45 * Added LADSPA_PATH support!
46 * 2004-12-07 Fixed changing buffersize. Now it's really working, also in
47 * real-time.
48 * 2004-12-06 First working version, mono-effects (1 input --> 1 output) only
49 * 2004-12-05 Started, Loading of plugin/label, Check inputs/outputs/controls
50 * Due to lack of documentation, I studied the ladspa_sdk source
51 * code and the loader code of Audacity (by Dominic Mazzoni). So,
52 * certain similarities in (small) pieces of code are not
53 * coincidental :-) No C&P jobs though!
57 /* ------------------------------------------------------------------------- */
59 /* Global Includes */
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
65 #include <unistd.h>
66 #include <inttypes.h>
67 #include <math.h>
68 #include <limits.h>
70 #include <dlfcn.h>
71 #include <ladspa.h>
73 /* ------------------------------------------------------------------------- */
75 /* Local Includes */
77 #include "af.h"
78 #include "../help_mp.h"
80 /* ------------------------------------------------------------------------- */
82 /* Filter specific data */
84 typedef struct af_ladspa_s
86 int status; /**< Status of the filter.
87 * Either AF_OK or AF_ERROR
88 * Because MPlayer re-inits audio filters that
89 * _clearly_ returned AF_ERROR anyway, I use this
90 * in play() to skip the processing and return
91 * the data unchanged.
94 int activated; /**< 0 or 1. Activate LADSPA filters only once, even
95 * if the buffers get resized, to avoid a stuttering
96 * filter.
99 char *file;
100 char *label;
102 char *myname; /**< It's easy to have a concatenation of file and label */
104 void *libhandle;
105 const LADSPA_Descriptor *plugin_descriptor;
107 int nports;
109 int ninputs;
110 int *inputs;
112 int noutputs;
113 int *outputs;
115 int ninputcontrols;
116 int *inputcontrolsmap; /**< Map input port number [0-] to actual port */
117 float *inputcontrols;
119 int noutputcontrols;
120 int *outputcontrolsmap;
121 float *outputcontrols;
123 int nch; /**< number of channels */
124 int bufsize;
125 float **inbufs;
126 float **outbufs;
127 LADSPA_Handle *chhandles;
129 } af_ladspa_t;
131 /* ------------------------------------------------------------------------- */
133 static int open(af_instance_t *af);
134 static int af_ladspa_malloc_failed(char*);
136 /* ------------------------------------------------------------------------- */
138 /* Description */
140 af_info_t af_info_ladspa = {
141 "LADSPA plugin loader",
142 "ladspa",
143 "Ivo van Poorten",
145 AF_FLAGS_REENTRANT,
146 open
149 /* ------------------------------------------------------------------------- */
151 /* By lack of a better word (in my vocabulary) this is called 'parse'.
152 * Feel free to suggest an alternative.
155 /** \brief Check for inputs, outputs and controls of a given filter.
157 * This function counts and checks all input, output and control ports
158 * of the filter that was loaded. If it turns out to be a valid
159 * filter for MPlayer use, it prints out a list of all controls and
160 * the corresponding range of its value at message level MSGL_V.
162 * \param setup Current setup of the filter. Must have its
163 * plugin_descriptor set!
165 * \return Returns AF_OK if it has a valid input/output/controls
166 * configuration. Else, it returns AF_ERROR.
169 static int af_ladspa_parse_plugin(af_ladspa_t *setup) {
170 int p, i;
171 const LADSPA_Descriptor *pdes = setup->plugin_descriptor;
172 LADSPA_PortDescriptor d;
173 LADSPA_PortRangeHint hint;
175 if (!setup->libhandle)
176 return AF_ERROR; /* only call parse after a succesful load */
177 if (!setup->plugin_descriptor)
178 return AF_ERROR; /* same as above */
180 /* let's do it */
182 setup->nports = pdes->PortCount;
184 /* allocate memory for all inputs/outputs/controls */
186 setup->inputs = calloc(setup->nports, sizeof(int));
187 if (!setup->inputs) return af_ladspa_malloc_failed(setup->myname);
189 setup->outputs = calloc(setup->nports, sizeof(int));
190 if (!setup->outputs) return af_ladspa_malloc_failed(setup->myname);
192 setup->inputcontrolsmap = calloc(setup->nports, sizeof(int));
193 if (!setup->inputcontrolsmap) return af_ladspa_malloc_failed(setup->myname);
195 setup->inputcontrols = calloc(setup->nports, sizeof(float));
196 if (!setup->inputcontrols) return af_ladspa_malloc_failed(setup->myname);
198 setup->outputcontrolsmap = calloc(setup->nports, sizeof(int));
199 if (!setup->outputcontrolsmap) return af_ladspa_malloc_failed(setup->myname);
201 setup->outputcontrols = calloc(setup->nports, sizeof(float));
202 if (!setup->outputcontrols) return af_ladspa_malloc_failed(setup->myname);
204 /* set counts to zero */
206 setup->ninputs = 0;
207 setup->noutputs = 0;
208 setup->ninputcontrols = 0;
209 setup->noutputcontrols = 0;
211 /* check all ports, see what type it is and set variables according to
212 * what we have found
215 for (p=0; p<setup->nports; p++) {
216 d = pdes->PortDescriptors[p];
218 if (LADSPA_IS_PORT_AUDIO(d)) {
219 if (LADSPA_IS_PORT_INPUT(d)) {
220 setup->inputs[setup->ninputs] = p;
221 setup->ninputs++;
222 } else if (LADSPA_IS_PORT_OUTPUT(d)) {
223 setup->outputs[setup->noutputs] = p;
224 setup->noutputs++;
228 if (LADSPA_IS_PORT_CONTROL(d)) {
229 if (LADSPA_IS_PORT_INPUT(d)) {
230 setup->inputcontrolsmap[setup->ninputcontrols] = p;
231 setup->ninputcontrols++;
232 /* set control to zero. set values after reading the rest
233 * of the suboptions and check LADSPA_?_HINT's later.
235 setup->inputcontrols[p] = 0.0f;
236 } else if (LADSPA_IS_PORT_OUTPUT(d)) {
237 /* read and handle these too, otherwise filters that have them
238 * will sig11
240 setup->outputcontrolsmap[setup->noutputcontrols]=p;
241 setup->noutputcontrols++;
242 setup->outputcontrols[p] = 0.0f;
248 if (setup->ninputs == 0) {
249 af_msg(AF_MSG_WARN, "%s: %s\n", setup->myname,
250 MSGTR_AF_LADSPA_WarnNoInputs);
251 } else if (setup->ninputs == 1) {
252 af_msg(AF_MSG_VERBOSE, "%s: this is a mono effect\n", setup->myname);
253 } else if (setup->ninputs == 2) {
254 af_msg(AF_MSG_VERBOSE, "%s: this is a stereo effect\n", setup->myname);
257 if (setup->ninputs > 2) {
258 af_msg(AF_MSG_ERROR, "%s: %s\n", setup->myname,
259 MSGTR_AF_LADSPA_ErrMultiChannel);
260 return AF_ERROR;
263 if (setup->noutputs == 0) {
264 af_msg(AF_MSG_ERROR, "%s: %s\n", setup->myname,
265 MSGTR_AF_LADSPA_ErrNoOutputs);
266 return AF_ERROR;
269 if (setup->noutputs != setup->ninputs ) {
270 af_msg(AF_MSG_ERROR, "%s: %s\n", setup->myname,
271 MSGTR_AF_LADSPA_ErrInOutDiff);
272 return AF_ERROR;
275 af_msg(AF_MSG_VERBOSE, "%s: this plugin has %d input control(s)\n",
276 setup->myname, setup->ninputcontrols);
278 /* Print list of controls and its range of values it accepts */
280 for (i=0; i<setup->ninputcontrols; i++) {
281 p = setup->inputcontrolsmap[i];
282 hint = pdes->PortRangeHints[p];
283 af_msg(AF_MSG_VERBOSE, " --- %d %s [", i, pdes->PortNames[p]);
285 if (LADSPA_IS_HINT_BOUNDED_BELOW(hint.HintDescriptor)) {
286 af_msg(AF_MSG_VERBOSE, "%0.2f , ", hint.LowerBound);
287 } else {
288 af_msg(AF_MSG_VERBOSE, "... , ");
291 if (LADSPA_IS_HINT_BOUNDED_ABOVE(hint.HintDescriptor)) {
292 af_msg(AF_MSG_VERBOSE, "%0.2f]\n", hint.UpperBound);
293 } else {
294 af_msg(AF_MSG_VERBOSE, "...]\n");
299 return AF_OK;
302 /* ------------------------------------------------------------------------- */
304 /* This function might "slightly" look like dlopenLADSPA in the LADSPA SDK :-)
305 * But, I changed a few things, because imho it was broken. It did not support
306 * relative paths, only absolute paths that start with a /
307 * I think ../../some/dir/foobar.so is just as valid. And if one wants to call
308 * his library '...somename...so' he's crazy, but it should be allowed.
309 * So, search the path first, try plain *filename later.
310 * Also, try adding .so first! I like the recursion the SDK did, but it's
311 * better the other way around. -af ladspa=cmt:amp_stereo:0.5 is easier to type
312 * than -af ladspa=cmt.so:amp_stereo:0.5 :-))
315 /** \brief dlopen() wrapper
317 * This is a wrapper around dlopen(). It tries various variations of the
318 * filename (with or without the addition of the .so extension) in various
319 * directories specified by the LADSPA_PATH environment variable. If all fails
320 * it tries the filename directly as an absolute path to the library.
322 * \param filename filename of the library to load.
323 * \param flag see dlopen(3) for a description of the flags.
325 * \return returns a pointer to the loaded library on success, or
326 * NULL if it fails to load.
329 static void* mydlopen(const char *filename, int flag) {
330 char *buf;
331 const char *end, *start, *ladspapath;
332 int endsinso, needslash;
333 size_t filenamelen;
334 void *result = NULL;
336 # ifdef WIN32 /* for windows there's only absolute path support.
337 * if you have a windows machine, feel free to fix
338 * this. (path separator, shared objects extension,
339 * et cetera).
341 af_msg(AF_MSG_VERBOSE, "\ton windows, only absolute pathnames "
342 "are supported\n");
343 af_msg(AF_MSG_VERBOSE, "\ttrying %s\n", filename);
344 return dlopen(filename, flag);
345 # endif
347 filenamelen = strlen(filename);
349 endsinso = 0;
350 if (filenamelen > 3)
351 endsinso = (strcmp(filename+filenamelen-3, ".so") == 0);
352 if (!endsinso) {
353 buf=malloc(filenamelen+4);
354 strcpy(buf, filename);
355 strcat(buf, ".so");
356 result=mydlopen(buf, flag);
357 free(buf);
360 if (result)
361 return result;
363 ladspapath=getenv("LADSPA_PATH");
365 if (ladspapath) {
367 start=ladspapath;
368 while (*start != '\0') {
369 end=start;
370 while ( (*end != ':') && (*end != '\0') )
371 end++;
373 buf=malloc(filenamelen + 2 + (end-start) );
374 if (end > start)
375 strncpy(buf, start, end-start);
376 needslash=0;
377 if (end > start)
378 if (*(end-1) != '/') {
379 needslash = 1;
380 buf[end-start] = '/';
382 strcpy(buf+needslash+(end-start), filename);
384 af_msg(AF_MSG_VERBOSE, "\ttrying %s\n", buf);
385 result=dlopen(buf, flag);
387 free(buf);
388 if (result)
389 return result;
391 start = end;
392 if (*start == ':')
393 start++;
394 } /* end while there's still more in the path */
395 } /* end if there's a ladspapath */
397 /* last resort, just open it again, so the dlerror() message is correct */
398 af_msg(AF_MSG_VERBOSE, "\ttrying %s\n", filename);
399 return dlopen(filename,flag);
402 /* ------------------------------------------------------------------------- */
404 /** \brief Load a LADSPA Plugin
406 * This function loads the LADSPA plugin specified by the file and label
407 * that are present in the setup variable. First, it loads the library.
408 * If it fails, it returns AF_ERROR. If not, it continues to look for the
409 * specified label. If it finds it, it sets the plugin_descriptor inside
410 * setup and returns AF_OK. If it doesn't, it returns AF_ERROR. Special case
411 * is a label called 'help'. In that case, it prints a list of all available
412 * labels (filters) in the library specified by file.
414 * \param setup Current setup of the filter. Contains filename and label.
416 * \return Either AF_ERROR or AF_OK, depending on the success of the operation.
419 static int af_ladspa_load_plugin(af_ladspa_t *setup) {
420 const LADSPA_Descriptor *ladspa_descriptor;
421 LADSPA_Descriptor_Function descriptor_function;
422 int i;
424 /* load library */
425 af_msg(AF_MSG_VERBOSE, "%s: loading ladspa plugin library %s\n",
426 setup->myname, setup->file);
428 setup->libhandle = mydlopen(setup->file, RTLD_NOW);
430 if (!setup->libhandle) {
431 af_msg(AF_MSG_ERROR, "%s: %s %s\n\t%s\n", setup->myname,
432 MSGTR_AF_LADSPA_ErrFailedToLoad, setup->file, dlerror() );
433 return AF_ERROR;
436 af_msg(AF_MSG_VERBOSE, "%s: library found.\n", setup->myname);
438 /* find descriptor function */
439 dlerror();
440 descriptor_function = (LADSPA_Descriptor_Function) dlsym (setup->libhandle,
441 "ladspa_descriptor");
443 if (!descriptor_function) {
444 af_msg(AF_MSG_ERROR, "%s: %s\n\t%s\n", setup->myname,
445 MSGTR_AF_LADSPA_ErrNoDescriptor, dlerror());
446 return AF_ERROR;
449 /* if label == help, list all labels in library and exit */
451 if (strcmp(setup->label, "help") == 0) {
452 af_msg(AF_MSG_INFO, "%s: %s %s:\n", setup->myname,
453 MSGTR_AF_LADSPA_AvailableLabels, setup->file);
454 for (i=0; ; i++) {
455 ladspa_descriptor = descriptor_function(i);
456 if (ladspa_descriptor == NULL) {
457 return AF_ERROR;
459 af_msg(AF_MSG_INFO, " %-16s - %s (%lu)\n",
460 ladspa_descriptor->Label,
461 ladspa_descriptor->Name,
462 ladspa_descriptor->UniqueID);
466 af_msg(AF_MSG_VERBOSE, "%s: looking for label\n", setup->myname);
468 /* find label in library */
469 for (i=0; ; i++) {
470 ladspa_descriptor = descriptor_function(i);
471 if (ladspa_descriptor == NULL) {
472 af_msg(AF_MSG_ERROR, "%s: %s\n", setup->myname,
473 MSGTR_AF_LADSPA_ErrLabelNotFound);
474 return AF_ERROR;
476 if (strcmp(ladspa_descriptor->Label, setup->label) == 0) {
477 setup->plugin_descriptor = ladspa_descriptor;
478 af_msg(AF_MSG_VERBOSE, "%s: %s found\n", setup->myname,
479 setup->label);
480 return AF_OK;
484 return AF_OK;
487 /* ------------------------------------------------------------------------- */
489 /** \brief Print a malloc() failed error message.
491 * Generic function which can be called if a call to malloc(), calloc(),
492 * strdup(), et cetera, failed. It prints a message to the console and
493 * returns AF_ERROR.
495 * \return AF_ERROR
498 static int af_ladspa_malloc_failed(char *myname) {
499 af_msg(AF_MSG_ERROR, "%s: %s\n", myname, MSGTR_MemAllocFailed);
500 return AF_ERROR;
503 /* ------------------------------------------------------------------------- */
505 /** \brief Controls the filter.
507 * Control the behaviour of the filter.
509 * Commands:
510 * CONTROL_REINIT Sets the af structure with proper values for number
511 * of channels, rate, format, et cetera.
512 * CONTROL_COMMAND_LINE Parses the suboptions given to this filter
513 * through arg. It first parses the filename and
514 * the label. After that, it loads the filter
515 * and finds out its proprties. Then in continues
516 * parsing the controls given on the commandline,
517 * if any are needed.
519 * \param af Audio filter instance
520 * \param cmd The command to execute
521 * \param arg Arguments to the command
523 * \return Either AF_ERROR or AF_OK, depending on the succes of the
524 * operation.
527 static int control(struct af_instance_s *af, int cmd, void *arg) {
528 af_ladspa_t *setup = (af_ladspa_t*) af->setup;
529 int i, r;
530 float val;
532 switch(cmd) {
533 case AF_CONTROL_REINIT:
534 af_msg(AF_MSG_VERBOSE, "%s: (re)init\n", setup->myname);
536 if (!arg) return AF_ERROR;
538 /* for now, only accept 16 bit signed int */
540 af->data->rate = ((af_data_t*)arg)->rate;
541 af->data->nch = ((af_data_t*)arg)->nch;
542 af->data->format = AF_FORMAT_S16_NE;
543 af->data->bps = 2;
545 /* arg->len is not set here yet, so init of buffers and connecting the
546 * filter, has to be done in play() :-/
549 return af_test_output(af, (af_data_t*)arg);
550 case AF_CONTROL_COMMAND_LINE: {
551 char *buf;
553 af_msg(AF_MSG_VERBOSE, "%s: parse suboptions\n", setup->myname);
555 /* suboption parser here!
556 * format is (ladspa=)file:label:controls....
559 if (!arg) {
560 af_msg(AF_MSG_ERROR, "%s: %s\n", setup->myname,
561 MSGTR_AF_LADSPA_ErrNoSuboptions);
562 return AF_ERROR;
565 buf = malloc(strlen(arg)+1);
566 if (!buf) return af_ladspa_malloc_failed(setup->myname);
568 /* file... */
569 buf[0] = '\0';
570 sscanf(arg, "%[^:]", buf);
571 if (buf[0] == '\0') {
572 af_msg(AF_MSG_ERROR, "%s: %s\n", setup->myname,
573 MSGTR_AF_LADSPA_ErrNoLibFile);
574 free(buf);
575 return AF_ERROR;
577 arg += strlen(buf);
578 setup->file = strdup(buf);
579 if (!setup->file) return af_ladspa_malloc_failed(setup->myname);
580 af_msg(AF_MSG_VERBOSE, "%s: file --> %s\n", setup->myname,
581 setup->file);
582 if (*(char*)arg != '\0') arg++; /* read ':' */
584 /* label... */
585 buf[0] = '\0';
586 sscanf(arg, "%[^:]", buf);
587 if (buf[0] == '\0') {
588 af_msg(AF_MSG_ERROR, "%s: %s\n", setup->myname,
589 MSGTR_AF_LADSPA_ErrNoLabel);
590 free(buf);
591 return AF_ERROR;
593 arg += strlen(buf);
594 setup->label = strdup(buf);
595 if (!setup->label) return af_ladspa_malloc_failed(setup->myname);
596 af_msg(AF_MSG_VERBOSE, "%s: label --> %s\n", setup->myname,
597 setup->label);
598 /* if (*(char*)arg != '0') arg++; */ /* read ':' */
600 free(buf); /* no longer needed */
602 /* set new setup->myname */
604 if(setup->myname) free(setup->myname);
605 setup->myname = calloc(strlen(af_info_ladspa.name)+strlen(setup->file)+
606 strlen(setup->label)+6, 1);
607 snprintf(setup->myname, strlen(af_info_ladspa.name)+
608 strlen(setup->file)+strlen(setup->label)+6, "%s: (%s:%s)",
609 af_info_ladspa.name, setup->file, setup->label);
611 /* load plugin :) */
613 if ( af_ladspa_load_plugin(setup) != AF_OK )
614 return AF_ERROR;
616 /* see what inputs, outputs and controls this plugin has */
617 if ( af_ladspa_parse_plugin(setup) != AF_OK )
618 return AF_ERROR;
620 /* ninputcontrols is set by now, read control values from arg */
622 for(i=0; i<setup->ninputcontrols; i++) {
623 if (!arg || (*(char*)arg != ':') ) {
624 af_msg(AF_MSG_ERROR, "%s: %s\n", setup->myname,
625 MSGTR_AF_LADSPA_ErrNotEnoughControls);
626 return AF_ERROR;
628 arg++;
629 r = sscanf(arg, "%f", &val);
630 if (r!=1) {
631 af_msg(AF_MSG_ERROR, "%s: %s\n", setup->myname,
632 MSGTR_AF_LADSPA_ErrNotEnoughControls);
633 return AF_ERROR;
635 setup->inputcontrols[setup->inputcontrolsmap[i]] = val;
636 arg = strchr(arg, ':');
639 af_msg(AF_MSG_VERBOSE, "%s: input controls: ", setup->myname);
640 for(i=0; i<setup->ninputcontrols; i++) {
641 af_msg(AF_MSG_VERBOSE, "%0.4f ",
642 setup->inputcontrols[setup->inputcontrolsmap[i]]);
644 af_msg(AF_MSG_VERBOSE, "\n");
646 /* check boundaries of inputcontrols */
648 af_msg(AF_MSG_VERBOSE, "%s: checking boundaries of input controls\n",
649 setup->myname);
650 for(i=0; i<setup->ninputcontrols; i++) {
651 int p = setup->inputcontrolsmap[i];
652 LADSPA_PortRangeHint hint =
653 setup->plugin_descriptor->PortRangeHints[p];
654 val = setup->inputcontrols[p];
656 if (LADSPA_IS_HINT_BOUNDED_BELOW(hint.HintDescriptor) &&
657 val < hint.LowerBound) {
658 af_msg(AF_MSG_ERROR, MSGTR_AF_LADSPA_ErrControlBelow,
659 setup->myname, i, hint.LowerBound);
660 return AF_ERROR;
662 if (LADSPA_IS_HINT_BOUNDED_ABOVE(hint.HintDescriptor) &&
663 val > hint.UpperBound) {
664 af_msg(AF_MSG_ERROR, MSGTR_AF_LADSPA_ErrControlAbove,
665 setup->myname, i, hint.UpperBound);
666 return AF_ERROR;
669 af_msg(AF_MSG_VERBOSE, "%s: all controls have sane values\n",
670 setup->myname);
672 /* All is well! */
673 setup->status = AF_OK;
675 return AF_OK; }
678 return AF_UNKNOWN;
681 /* ------------------------------------------------------------------------- */
683 /** \brief Uninitialise the LADSPA Plugin Loader filter.
685 * This function deactivates the plugin(s), cleans up, frees all allocated
686 * memory and exits.
688 * \return No return value.
691 static void uninit(struct af_instance_s *af) {
692 int i;
694 if (af->data)
695 free(af->data);
696 if (af->setup) {
697 af_ladspa_t *setup = (af_ladspa_t*) af->setup;
698 const LADSPA_Descriptor *pdes = setup->plugin_descriptor;
700 if (setup->myname) {
701 af_msg(AF_MSG_VERBOSE, "%s: cleaning up\n", setup->myname);
702 free(setup->myname);
705 if (setup->chhandles) {
706 for(i=0; i<setup->nch; i++) {
707 if ( (setup->ninputs == 2) && (i & 1) ) { /* stereo effect */
708 i++;
709 continue;
711 if (pdes->deactivate) pdes->deactivate(setup->chhandles[i]);
712 if (pdes->cleanup) pdes->cleanup(setup->chhandles[i]);
714 free(setup->chhandles);
717 if (setup->file)
718 free(setup->file);
719 if (setup->label)
720 free(setup->label);
721 if (setup->inputcontrolsmap)
722 free(setup->inputcontrolsmap);
723 if (setup->inputcontrols)
724 free(setup->inputcontrols);
725 if (setup->outputcontrolsmap)
726 free(setup->outputcontrolsmap);
727 if (setup->outputcontrols)
728 free(setup->outputcontrols);
729 if (setup->inputs)
730 free(setup->inputs);
731 if (setup->outputs)
732 free(setup->outputs);
734 if (setup->inbufs) {
735 for(i=0; i<setup->nch; i++) {
736 if (setup->inbufs[i])
737 free(setup->inbufs[i]);
739 free(setup->inbufs);
742 if (setup->outbufs) {
743 for(i=0; i<setup->nch; i++) {
744 if (setup->outbufs[i])
745 free(setup->outbufs[i]);
747 free(setup->outbufs);
750 if (setup->libhandle)
751 dlclose(setup->libhandle);
753 free(setup);
754 setup = NULL;
758 /* ------------------------------------------------------------------------- */
760 /** \brief Process chunk of audio data through the selected LADSPA Plugin.
762 * \param af Pointer to audio filter instance
763 * \param data Pointer to chunk of audio data
765 * \return Either AF_ERROR or AF_OK
768 static af_data_t* play(struct af_instance_s *af, af_data_t *data) {
769 af_ladspa_t *setup = af->setup;
770 const LADSPA_Descriptor *pdes = setup->plugin_descriptor;
771 int16_t *audio = (int16_t*)data->audio;
772 int nsamples = data->len/2; /* /2 because it's int16_t */
773 int nch = data->nch;
774 int rate = data->rate;
775 int i, p;
776 float v;
778 if (setup->status !=AF_OK)
779 return data;
781 /* See if it's the first call. If so, setup inbufs/outbufs, instantiate
782 * plugin, connect ports and activate plugin
785 /* 2004-12-07: Also check if the buffersize has to be changed!
786 * data->len is not constant per se! re-init buffers.
789 if ( (setup->bufsize != nsamples/nch) || (setup->nch != nch) ) {
791 /* if setup->nch==0, it's the first call, if not, something has
792 * changed and all previous mallocs have to be freed
795 if (setup->nch != 0) {
796 af_msg(AF_MSG_DEBUG1, "%s: bufsize change; free old buffer\n",
797 setup->myname);
799 if(setup->inbufs) {
800 for(i=0; i<setup->nch; i++) {
801 if(setup->inbufs[i])
802 free(setup->inbufs[i]);
804 free(setup->inbufs);
806 if(setup->outbufs) {
807 for(i=0; i<setup->nch; i++) {
808 if(setup->outbufs[i])
809 free(setup->outbufs[i]);
811 free(setup->outbufs);
813 } /* everything is freed */
815 setup->bufsize = nsamples/nch;
816 setup->nch = nch;
818 setup->inbufs = calloc(nch, sizeof(float*));
819 setup->outbufs = calloc(nch, sizeof(float*));
821 af_msg(AF_MSG_DEBUG1, "%s: bufsize = %d\n",
822 setup->myname, setup->bufsize);
824 for(i=0; i<nch; i++) {
825 setup->inbufs[i] = calloc(setup->bufsize, sizeof(float));
826 setup->outbufs[i] = calloc(setup->bufsize, sizeof(float));
829 /* only on the first call, there are no handles. */
831 if (!setup->chhandles) {
832 setup->chhandles = calloc(nch, sizeof(LADSPA_Handle));
834 /* create handles
835 * for stereo effects, create one handle for two channels
838 for(i=0; i<nch; i++) {
840 if ( (setup->ninputs == 2) && (i & 1) ) { /* stereo effect */
841 /* copy the handle from previous channel */
842 setup->chhandles[i] = setup->chhandles[i-1];
843 continue;
846 setup->chhandles[i] = pdes->instantiate(pdes, rate);
850 /* connect input/output ports for each channel/filter instance
852 * always (re)connect ports
855 for(i=0; i<nch; i++) {
856 pdes->connect_port(setup->chhandles[i],
857 setup->inputs[ (setup->ninputs==2) ? i&1 : 0 ],
858 setup->inbufs[i]);
859 pdes->connect_port(setup->chhandles[i],
860 setup->outputs[ (setup->ninputs==2) ? i&1 : 0 ],
861 setup->outbufs[i]);
863 /* connect (input) controls */
865 for (p=0; p<setup->nports; p++) {
866 LADSPA_PortDescriptor d = pdes->PortDescriptors[p];
867 if (LADSPA_IS_PORT_CONTROL(d)) {
868 if (LADSPA_IS_PORT_INPUT(d)) {
869 pdes->connect_port(setup->chhandles[i], p,
870 &(setup->inputcontrols[p]) );
871 } else {
872 pdes->connect_port(setup->chhandles[i], p,
873 &(setup->outputcontrols[p]) );
878 /* Activate filter (if it isn't already :) ) */
880 if ( (pdes->activate) && (setup->activated == 0) ) {
881 pdes->activate(setup->chhandles[i]);
882 setup->activated = 1;
885 } /* All channels/filters done! except for... */
887 /* Stereo effect with one channel left. Use same buffer for left
888 * and right. connect it to the second port.
891 if( (setup->ninputs == 2) && (i&1) && (i >= 1) ) {
892 pdes->connect_port(setup->chhandles[i-1],
893 setup->inputs[ (setup->ninputs==2) ? i&1 : 0 ],
894 setup->inbufs[i-1]);
895 pdes->connect_port(setup->chhandles[i-1],
896 setup->outputs[ (setup->ninputs==2) ? i&1 : 0 ],
897 setup->outbufs[i-1]);
898 } /* done! */
900 } /* setup for first call/change of bufsize is done.
901 * normal playing routine follows...
904 /* Right now, I use a separate input and output buffer.
905 * I could change this to in-place processing (inbuf==outbuf), but some
906 * ladspa filters are broken and are not able to handle that. This seems
907 * fast enough, so unless somebody complains, it stays this way :)
910 /* Fill inbufs */
912 for (p=0; p<setup->bufsize; p++) {
913 for (i=0; i<nch; i++) {
914 setup->inbufs[i][p] = ( (float) audio[p*nch + i] ) / 32768.0f;
918 /* Run filter(s) */
920 for (i=0; i<nch; i++) {
921 pdes->run(setup->chhandles[i], setup->bufsize);
922 if (setup->ninputs==2) // stereo effect just ran
923 i++;
926 /* Extract outbufs, hard clipping in case the filter exceeded [-1.0,1.0] */
928 for (p=0; p<setup->bufsize; p++) {
929 for (i=0; i<nch; i++) {
930 v = setup->outbufs[i][p];
931 v *= 32768.0f;
932 v = (v > 32767.0f ? 32767.0f : v);
933 v = (v < -32768.0f ? -32768.0f : v);
934 audio[p*nch + i] = (int16_t) v;
938 /* done */
940 return data;
943 /* ------------------------------------------------------------------------- */
945 /** \brief Open LADSPA Plugin Loader Filter
947 * \param af Audio Filter instance
949 * \return Either AF_ERROR or AF_OK
952 static int open(af_instance_t *af) {
954 af->control=control;
955 af->uninit=uninit;
956 af->play=play;
957 af->mul.n=1;
958 af->mul.d=1;
960 af->data = calloc(1, sizeof(af_data_t));
961 if (af->data == NULL)
962 return af_ladspa_malloc_failed((char*)af_info_ladspa.name);
964 af->setup = calloc(1, sizeof(af_ladspa_t));
965 if (af->setup == NULL) {
966 free(af->data);
967 af->data=NULL;
968 return af_ladspa_malloc_failed((char*)af_info_ladspa.name);
971 ((af_ladspa_t*)af->setup)->status = AF_ERROR; /* will be set to AF_OK if
972 * all went OK and play()
973 * should proceed.
976 ((af_ladspa_t*)af->setup)->myname = strdup(af_info_ladspa.name);
977 if (!((af_ladspa_t*)af->setup)->myname)
978 return af_ladspa_malloc_failed((char*)af_info_ladspa.name);
980 return AF_OK;
983 /* ------------------------------------------------------------------------- */