another round of '-' escapes
[mplayer/greg.git] / libaf / af_ladspa.c
blob199faabfbbcf16126841a34e3c79166c9adfbb61
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Changelog
26 * 2005-06-21 Replaced erroneous use of mp_msg by af_msg
27 * 2005-05-30 Removed int16 to float conversion; leave that to af_format
28 * 2004-12-23 Added to CVS
29 * 2004-12-22 Cleaned up cosmetics
30 * Made conversion loops in play() more cache-friendly
31 * 2004-12-20 Fixed bug for stereo effect on mono signal
32 * (trivial >1 to >=1 change; would segfault otherwise :-) )
33 * Removed trailing whitespace and fixed warn/err messages
34 * Have CONTROL_REINIT return a proper value
35 * 2004-12-13 More Doxygen comments
36 * 2004-12-12 Made af_ladspa optional (updated configure, af.c, etc.)
37 * 2004-12-11 Added deactivate and cleanup to uninit.
38 * Finished Doxygen comments.
39 * Moved translatable messages to help_mp-en.h
40 * 2004-12-10 Added ranges to list of controls for ease of use.
41 * Fixed sig11 bug. Implemented (dummy) outputcontrols. Some
42 * perfectly normal audio processing filters also have output
43 * controls.
44 * 2004-12-08 Added support for generators (no input, one output)
45 * Added support for stereo effects
46 * Added LADSPA_PATH support!
47 * 2004-12-07 Fixed changing buffersize. Now it's really working, also in
48 * real-time.
49 * 2004-12-06 First working version, mono-effects (1 input --> 1 output) only
50 * 2004-12-05 Started, Loading of plugin/label, Check inputs/outputs/controls
51 * Due to lack of documentation, I studied the ladspa_sdk source
52 * code and the loader code of Audacity (by Dominic Mazzoni). So,
53 * certain similarities in (small) pieces of code are not
54 * coincidental :-) No C&P jobs though!
58 /* ------------------------------------------------------------------------- */
60 /* Global Includes */
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.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 af_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 af_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);
255 } else {
256 af_msg(AF_MSG_VERBOSE, "%s: this is a %i-channel effect, "
257 "support is experimental\n", setup->myname, setup->ninputs);
260 if (setup->noutputs == 0) {
261 af_msg(AF_MSG_ERROR, "%s: %s\n", setup->myname,
262 MSGTR_AF_LADSPA_ErrNoOutputs);
263 return AF_ERROR;
266 if (setup->noutputs != setup->ninputs ) {
267 af_msg(AF_MSG_ERROR, "%s: %s\n", setup->myname,
268 MSGTR_AF_LADSPA_ErrInOutDiff);
269 return AF_ERROR;
272 af_msg(AF_MSG_VERBOSE, "%s: this plugin has %d input control(s)\n",
273 setup->myname, setup->ninputcontrols);
275 /* Print list of controls and its range of values it accepts */
277 for (i=0; i<setup->ninputcontrols; i++) {
278 p = setup->inputcontrolsmap[i];
279 hint = pdes->PortRangeHints[p];
280 af_msg(AF_MSG_VERBOSE, " --- %d %s [", i, pdes->PortNames[p]);
282 if (LADSPA_IS_HINT_BOUNDED_BELOW(hint.HintDescriptor)) {
283 af_msg(AF_MSG_VERBOSE, "%0.2f , ", hint.LowerBound);
284 } else {
285 af_msg(AF_MSG_VERBOSE, "... , ");
288 if (LADSPA_IS_HINT_BOUNDED_ABOVE(hint.HintDescriptor)) {
289 af_msg(AF_MSG_VERBOSE, "%0.2f]\n", hint.UpperBound);
290 } else {
291 af_msg(AF_MSG_VERBOSE, "...]\n");
296 return AF_OK;
299 /* ------------------------------------------------------------------------- */
301 /* This function might "slightly" look like dlopenLADSPA in the LADSPA SDK :-)
302 * But, I changed a few things, because imho it was broken. It did not support
303 * relative paths, only absolute paths that start with a /
304 * I think ../../some/dir/foobar.so is just as valid. And if one wants to call
305 * his library '...somename...so' he's crazy, but it should be allowed.
306 * So, search the path first, try plain *filename later.
307 * Also, try adding .so first! I like the recursion the SDK did, but it's
308 * better the other way around. -af ladspa=cmt:amp_stereo:0.5 is easier to type
309 * than -af ladspa=cmt.so:amp_stereo:0.5 :-))
312 /** \brief dlopen() wrapper
314 * This is a wrapper around dlopen(). It tries various variations of the
315 * filename (with or without the addition of the .so extension) in various
316 * directories specified by the LADSPA_PATH environment variable. If all fails
317 * it tries the filename directly as an absolute path to the library.
319 * \param filename filename of the library to load.
320 * \param flag see dlopen(3) for a description of the flags.
322 * \return returns a pointer to the loaded library on success, or
323 * NULL if it fails to load.
326 static void* mydlopen(const char *filename, int flag) {
327 char *buf;
328 const char *end, *start, *ladspapath;
329 int endsinso, needslash;
330 size_t filenamelen;
331 void *result = NULL;
333 # ifdef WIN32 /* for windows there's only absolute path support.
334 * if you have a windows machine, feel free to fix
335 * this. (path separator, shared objects extension,
336 * et cetera).
338 af_msg(AF_MSG_VERBOSE, "\ton windows, only absolute pathnames "
339 "are supported\n");
340 af_msg(AF_MSG_VERBOSE, "\ttrying %s\n", filename);
341 return dlopen(filename, flag);
342 # endif
344 filenamelen = strlen(filename);
346 endsinso = 0;
347 if (filenamelen > 3)
348 endsinso = (strcmp(filename+filenamelen-3, ".so") == 0);
349 if (!endsinso) {
350 buf=malloc(filenamelen+4);
351 strcpy(buf, filename);
352 strcat(buf, ".so");
353 result=mydlopen(buf, flag);
354 free(buf);
357 if (result)
358 return result;
360 ladspapath=getenv("LADSPA_PATH");
362 if (ladspapath) {
364 start=ladspapath;
365 while (*start != '\0') {
366 end=start;
367 while ( (*end != ':') && (*end != '\0') )
368 end++;
370 buf=malloc(filenamelen + 2 + (end-start) );
371 if (end > start)
372 strncpy(buf, start, end-start);
373 needslash=0;
374 if (end > start)
375 if (*(end-1) != '/') {
376 needslash = 1;
377 buf[end-start] = '/';
379 strcpy(buf+needslash+(end-start), filename);
381 af_msg(AF_MSG_VERBOSE, "\ttrying %s\n", buf);
382 result=dlopen(buf, flag);
384 free(buf);
385 if (result)
386 return result;
388 start = end;
389 if (*start == ':')
390 start++;
391 } /* end while there's still more in the path */
392 } /* end if there's a ladspapath */
394 /* last resort, just open it again, so the dlerror() message is correct */
395 af_msg(AF_MSG_VERBOSE, "\ttrying %s\n", filename);
396 return dlopen(filename,flag);
399 /* ------------------------------------------------------------------------- */
401 /** \brief Load a LADSPA Plugin
403 * This function loads the LADSPA plugin specified by the file and label
404 * that are present in the setup variable. First, it loads the library.
405 * If it fails, it returns AF_ERROR. If not, it continues to look for the
406 * specified label. If it finds it, it sets the plugin_descriptor inside
407 * setup and returns AF_OK. If it doesn't, it returns AF_ERROR. Special case
408 * is a label called 'help'. In that case, it prints a list of all available
409 * labels (filters) in the library specified by file.
411 * \param setup Current setup of the filter. Contains filename and label.
413 * \return Either AF_ERROR or AF_OK, depending on the success of the operation.
416 static int af_ladspa_load_plugin(af_ladspa_t *setup) {
417 const LADSPA_Descriptor *ladspa_descriptor;
418 LADSPA_Descriptor_Function descriptor_function;
419 int i;
421 /* load library */
422 af_msg(AF_MSG_VERBOSE, "%s: loading ladspa plugin library %s\n",
423 setup->myname, setup->file);
425 setup->libhandle = mydlopen(setup->file, RTLD_NOW);
427 if (!setup->libhandle) {
428 af_msg(AF_MSG_ERROR, "%s: %s %s\n\t%s\n", setup->myname,
429 MSGTR_AF_LADSPA_ErrFailedToLoad, setup->file, dlerror() );
430 return AF_ERROR;
433 af_msg(AF_MSG_VERBOSE, "%s: library found.\n", setup->myname);
435 /* find descriptor function */
436 dlerror();
437 descriptor_function = (LADSPA_Descriptor_Function) dlsym (setup->libhandle,
438 "ladspa_descriptor");
440 if (!descriptor_function) {
441 af_msg(AF_MSG_ERROR, "%s: %s\n\t%s\n", setup->myname,
442 MSGTR_AF_LADSPA_ErrNoDescriptor, dlerror());
443 return AF_ERROR;
446 /* if label == help, list all labels in library and exit */
448 if (strcmp(setup->label, "help") == 0) {
449 af_msg(AF_MSG_INFO, "%s: %s %s:\n", setup->myname,
450 MSGTR_AF_LADSPA_AvailableLabels, setup->file);
451 for (i=0; ; i++) {
452 ladspa_descriptor = descriptor_function(i);
453 if (ladspa_descriptor == NULL) {
454 return AF_ERROR;
456 af_msg(AF_MSG_INFO, " %-16s - %s (%lu)\n",
457 ladspa_descriptor->Label,
458 ladspa_descriptor->Name,
459 ladspa_descriptor->UniqueID);
463 af_msg(AF_MSG_VERBOSE, "%s: looking for label\n", setup->myname);
465 /* find label in library */
466 for (i=0; ; i++) {
467 ladspa_descriptor = descriptor_function(i);
468 if (ladspa_descriptor == NULL) {
469 af_msg(AF_MSG_ERROR, "%s: %s\n", setup->myname,
470 MSGTR_AF_LADSPA_ErrLabelNotFound);
471 return AF_ERROR;
473 if (strcmp(ladspa_descriptor->Label, setup->label) == 0) {
474 setup->plugin_descriptor = ladspa_descriptor;
475 af_msg(AF_MSG_VERBOSE, "%s: %s found\n", setup->myname,
476 setup->label);
477 return AF_OK;
481 return AF_OK;
484 /* ------------------------------------------------------------------------- */
486 /** \brief Print a malloc() failed error message.
488 * Generic function which can be called if a call to malloc(), calloc(),
489 * strdup(), et cetera, failed. It prints a message to the console and
490 * returns AF_ERROR.
492 * \return AF_ERROR
495 static int af_ladspa_malloc_failed(char *myname) {
496 af_msg(AF_MSG_ERROR, "%s: %s", myname, MSGTR_MemAllocFailed);
497 return AF_ERROR;
500 /* ------------------------------------------------------------------------- */
502 /** \brief Controls the filter.
504 * Control the behaviour of the filter.
506 * Commands:
507 * CONTROL_REINIT Sets the af structure with proper values for number
508 * of channels, rate, format, et cetera.
509 * CONTROL_COMMAND_LINE Parses the suboptions given to this filter
510 * through arg. It first parses the filename and
511 * the label. After that, it loads the filter
512 * and finds out its proprties. Then in continues
513 * parsing the controls given on the commandline,
514 * if any are needed.
516 * \param af Audio filter instance
517 * \param cmd The command to execute
518 * \param arg Arguments to the command
520 * \return Either AF_ERROR or AF_OK, depending on the succes of the
521 * operation.
524 static int control(struct af_instance_s *af, int cmd, void *arg) {
525 af_ladspa_t *setup = (af_ladspa_t*) af->setup;
526 int i, r;
527 float val;
529 switch(cmd) {
530 case AF_CONTROL_REINIT:
531 af_msg(AF_MSG_VERBOSE, "%s: (re)init\n", setup->myname);
533 if (!arg) return AF_ERROR;
535 /* accept FLOAT, let af_format do conversion */
537 af->data->rate = ((af_data_t*)arg)->rate;
538 af->data->nch = ((af_data_t*)arg)->nch;
539 af->data->format = AF_FORMAT_FLOAT_NE;
540 af->data->bps = 4;
542 /* arg->len is not set here yet, so init of buffers and connecting the
543 * filter, has to be done in play() :-/
546 return af_test_output(af, (af_data_t*)arg);
547 case AF_CONTROL_COMMAND_LINE: {
548 char *buf;
550 af_msg(AF_MSG_VERBOSE, "%s: parse suboptions\n", setup->myname);
552 /* suboption parser here!
553 * format is (ladspa=)file:label:controls....
556 if (!arg) {
557 af_msg(AF_MSG_ERROR, "%s: %s\n", setup->myname,
558 MSGTR_AF_LADSPA_ErrNoSuboptions);
559 return AF_ERROR;
562 buf = malloc(strlen(arg)+1);
563 if (!buf) return af_ladspa_malloc_failed(setup->myname);
565 /* file... */
566 buf[0] = '\0';
567 sscanf(arg, "%[^:]", buf);
568 if (buf[0] == '\0') {
569 af_msg(AF_MSG_ERROR, "%s: %s\n", setup->myname,
570 MSGTR_AF_LADSPA_ErrNoLibFile);
571 free(buf);
572 return AF_ERROR;
574 arg += strlen(buf);
575 setup->file = strdup(buf);
576 if (!setup->file) return af_ladspa_malloc_failed(setup->myname);
577 af_msg(AF_MSG_VERBOSE, "%s: file --> %s\n", setup->myname,
578 setup->file);
579 if (*(char*)arg != '\0') arg++; /* read ':' */
581 /* label... */
582 buf[0] = '\0';
583 sscanf(arg, "%[^:]", buf);
584 if (buf[0] == '\0') {
585 af_msg(AF_MSG_ERROR, "%s: %s\n", setup->myname,
586 MSGTR_AF_LADSPA_ErrNoLabel);
587 free(buf);
588 return AF_ERROR;
590 arg += strlen(buf);
591 setup->label = strdup(buf);
592 if (!setup->label) return af_ladspa_malloc_failed(setup->myname);
593 af_msg(AF_MSG_VERBOSE, "%s: label --> %s\n", setup->myname,
594 setup->label);
595 /* if (*(char*)arg != '0') arg++; */ /* read ':' */
597 free(buf); /* no longer needed */
599 /* set new setup->myname */
601 if(setup->myname) free(setup->myname);
602 setup->myname = calloc(strlen(af_info_ladspa.name)+strlen(setup->file)+
603 strlen(setup->label)+6, 1);
604 snprintf(setup->myname, strlen(af_info_ladspa.name)+
605 strlen(setup->file)+strlen(setup->label)+6, "%s: (%s:%s)",
606 af_info_ladspa.name, setup->file, setup->label);
608 /* load plugin :) */
610 if ( af_ladspa_load_plugin(setup) != AF_OK )
611 return AF_ERROR;
613 /* see what inputs, outputs and controls this plugin has */
614 if ( af_ladspa_parse_plugin(setup) != AF_OK )
615 return AF_ERROR;
617 /* ninputcontrols is set by now, read control values from arg */
619 for(i=0; i<setup->ninputcontrols; i++) {
620 if (!arg || (*(char*)arg != ':') ) {
621 af_msg(AF_MSG_ERROR, "%s: %s\n", setup->myname,
622 MSGTR_AF_LADSPA_ErrNotEnoughControls);
623 return AF_ERROR;
625 arg++;
626 r = sscanf(arg, "%f", &val);
627 if (r!=1) {
628 af_msg(AF_MSG_ERROR, "%s: %s\n", setup->myname,
629 MSGTR_AF_LADSPA_ErrNotEnoughControls);
630 return AF_ERROR;
632 setup->inputcontrols[setup->inputcontrolsmap[i]] = val;
633 arg = strchr(arg, ':');
636 af_msg(AF_MSG_VERBOSE, "%s: input controls: ", setup->myname);
637 for(i=0; i<setup->ninputcontrols; i++) {
638 af_msg(AF_MSG_VERBOSE, "%0.4f ",
639 setup->inputcontrols[setup->inputcontrolsmap[i]]);
641 af_msg(AF_MSG_VERBOSE, "\n");
643 /* check boundaries of inputcontrols */
645 af_msg(AF_MSG_VERBOSE, "%s: checking boundaries of input controls\n",
646 setup->myname);
647 for(i=0; i<setup->ninputcontrols; i++) {
648 int p = setup->inputcontrolsmap[i];
649 LADSPA_PortRangeHint hint =
650 setup->plugin_descriptor->PortRangeHints[p];
651 val = setup->inputcontrols[p];
653 if (LADSPA_IS_HINT_BOUNDED_BELOW(hint.HintDescriptor) &&
654 val < hint.LowerBound) {
655 af_msg(AF_MSG_ERROR, MSGTR_AF_LADSPA_ErrControlBelow,
656 setup->myname, i, hint.LowerBound);
657 return AF_ERROR;
659 if (LADSPA_IS_HINT_BOUNDED_ABOVE(hint.HintDescriptor) &&
660 val > hint.UpperBound) {
661 af_msg(AF_MSG_ERROR, MSGTR_AF_LADSPA_ErrControlAbove,
662 setup->myname, i, hint.UpperBound);
663 return AF_ERROR;
666 af_msg(AF_MSG_VERBOSE, "%s: all controls have sane values\n",
667 setup->myname);
669 /* All is well! */
670 setup->status = AF_OK;
672 return AF_OK; }
675 return AF_UNKNOWN;
678 /* ------------------------------------------------------------------------- */
680 /** \brief Uninitialise the LADSPA Plugin Loader filter.
682 * This function deactivates the plugin(s), cleans up, frees all allocated
683 * memory and exits.
685 * \return No return value.
688 static void uninit(struct af_instance_s *af) {
689 int i;
691 if (af->data)
692 free(af->data);
693 if (af->setup) {
694 af_ladspa_t *setup = (af_ladspa_t*) af->setup;
695 const LADSPA_Descriptor *pdes = setup->plugin_descriptor;
697 if (setup->myname) {
698 af_msg(AF_MSG_VERBOSE, "%s: cleaning up\n", setup->myname);
699 free(setup->myname);
702 if (setup->chhandles) {
703 for(i=0; i<setup->nch; i+=setup->ninputs) {
704 if (pdes->deactivate) pdes->deactivate(setup->chhandles[i]);
705 if (pdes->cleanup) pdes->cleanup(setup->chhandles[i]);
707 free(setup->chhandles);
710 if (setup->file)
711 free(setup->file);
712 if (setup->label)
713 free(setup->label);
714 if (setup->inputcontrolsmap)
715 free(setup->inputcontrolsmap);
716 if (setup->inputcontrols)
717 free(setup->inputcontrols);
718 if (setup->outputcontrolsmap)
719 free(setup->outputcontrolsmap);
720 if (setup->outputcontrols)
721 free(setup->outputcontrols);
722 if (setup->inputs)
723 free(setup->inputs);
724 if (setup->outputs)
725 free(setup->outputs);
727 if (setup->inbufs) {
728 for(i=0; i<setup->nch; i++) {
729 if (setup->inbufs[i])
730 free(setup->inbufs[i]);
732 free(setup->inbufs);
735 if (setup->outbufs) {
736 for(i=0; i<setup->nch; i++) {
737 if (setup->outbufs[i])
738 free(setup->outbufs[i]);
740 free(setup->outbufs);
743 if (setup->libhandle)
744 dlclose(setup->libhandle);
746 free(setup);
747 setup = NULL;
751 /* ------------------------------------------------------------------------- */
753 /** \brief Process chunk of audio data through the selected LADSPA Plugin.
755 * \param af Pointer to audio filter instance
756 * \param data Pointer to chunk of audio data
758 * \return Either AF_ERROR or AF_OK
761 static af_data_t* play(struct af_instance_s *af, af_data_t *data) {
762 af_ladspa_t *setup = af->setup;
763 const LADSPA_Descriptor *pdes = setup->plugin_descriptor;
764 float *audio = (float*)data->audio;
765 int nsamples = data->len/4; /* /4 because it's 32-bit float */
766 int nch = data->nch;
767 int rate = data->rate;
768 int i, p;
770 if (setup->status !=AF_OK)
771 return data;
773 /* See if it's the first call. If so, setup inbufs/outbufs, instantiate
774 * plugin, connect ports and activate plugin
777 /* 2004-12-07: Also check if the buffersize has to be changed!
778 * data->len is not constant per se! re-init buffers.
781 if ( (setup->bufsize != nsamples/nch) || (setup->nch != nch) ) {
783 /* if setup->nch==0, it's the first call, if not, something has
784 * changed and all previous mallocs have to be freed
787 if (setup->nch != 0) {
788 af_msg(AF_MSG_DEBUG1, "%s: bufsize change; free old buffer\n",
789 setup->myname);
791 if(setup->inbufs) {
792 for(i=0; i<setup->nch; i++) {
793 if(setup->inbufs[i])
794 free(setup->inbufs[i]);
796 free(setup->inbufs);
798 if(setup->outbufs) {
799 for(i=0; i<setup->nch; i++) {
800 if(setup->outbufs[i])
801 free(setup->outbufs[i]);
803 free(setup->outbufs);
805 } /* everything is freed */
807 setup->bufsize = nsamples/nch;
808 setup->nch = nch;
810 setup->inbufs = calloc(nch, sizeof(float*));
811 setup->outbufs = calloc(nch, sizeof(float*));
813 af_msg(AF_MSG_DEBUG1, "%s: bufsize = %d\n",
814 setup->myname, setup->bufsize);
816 for(i=0; i<nch; i++) {
817 setup->inbufs[i] = calloc(setup->bufsize, sizeof(float));
818 setup->outbufs[i] = calloc(setup->bufsize, sizeof(float));
821 /* only on the first call, there are no handles. */
823 if (!setup->chhandles) {
824 setup->chhandles = calloc(nch, sizeof(LADSPA_Handle));
826 /* create handles
827 * for stereo effects, create one handle for two channels
830 for(i=0; i<nch; i++) {
832 if (i % setup->ninputs) { /* stereo effect */
833 /* copy the handle from previous channel */
834 setup->chhandles[i] = setup->chhandles[i-1];
835 continue;
838 setup->chhandles[i] = pdes->instantiate(pdes, rate);
842 /* connect input/output ports for each channel/filter instance
844 * always (re)connect ports
847 for(i=0; i<nch; i++) {
848 pdes->connect_port(setup->chhandles[i],
849 setup->inputs[i % setup->ninputs],
850 setup->inbufs[i]);
851 pdes->connect_port(setup->chhandles[i],
852 setup->outputs[i % setup->ninputs],
853 setup->outbufs[i]);
855 /* connect (input) controls */
857 for (p=0; p<setup->nports; p++) {
858 LADSPA_PortDescriptor d = pdes->PortDescriptors[p];
859 if (LADSPA_IS_PORT_CONTROL(d)) {
860 if (LADSPA_IS_PORT_INPUT(d)) {
861 pdes->connect_port(setup->chhandles[i], p,
862 &(setup->inputcontrols[p]) );
863 } else {
864 pdes->connect_port(setup->chhandles[i], p,
865 &(setup->outputcontrols[p]) );
870 /* Activate filter (if it isn't already :) ) */
872 if ( (pdes->activate) && (setup->activated == 0) ) {
873 pdes->activate(setup->chhandles[i]);
874 setup->activated = 1;
877 } /* All channels/filters done! except for... */
879 /* Stereo effect with one channel left. Use same buffer for left
880 * and right. connect it to the second port.
883 for (p = i; p % setup->ninputs; p++) {
884 pdes->connect_port(setup->chhandles[i-1],
885 setup->inputs[p % setup->ninputs],
886 setup->inbufs[i-1]);
887 pdes->connect_port(setup->chhandles[i-1],
888 setup->outputs[p % setup->ninputs],
889 setup->outbufs[i-1]);
890 } /* done! */
892 } /* setup for first call/change of bufsize is done.
893 * normal playing routine follows...
896 /* Right now, I use a separate input and output buffer.
897 * I could change this to in-place processing (inbuf==outbuf), but some
898 * ladspa filters are broken and are not able to handle that. This seems
899 * fast enough, so unless somebody complains, it stays this way :)
902 /* Fill inbufs */
904 for (p=0; p<setup->bufsize; p++) {
905 for (i=0; i<nch; i++) {
906 setup->inbufs[i][p] = audio[p*nch + i];
910 /* Run filter(s) */
912 for (i=0; i<nch; i+=setup->ninputs) {
913 pdes->run(setup->chhandles[i], setup->bufsize);
916 /* Extract outbufs */
918 for (p=0; p<setup->bufsize; p++) {
919 for (i=0; i<nch; i++) {
920 audio[p*nch + i] = setup->outbufs[i][p];
924 /* done */
926 return data;
929 /* ------------------------------------------------------------------------- */
931 /** \brief Open LADSPA Plugin Loader Filter
933 * \param af Audio Filter instance
935 * \return Either AF_ERROR or AF_OK
938 static int af_open(af_instance_t *af) {
940 af->control=control;
941 af->uninit=uninit;
942 af->play=play;
943 af->mul=1;
945 af->data = calloc(1, sizeof(af_data_t));
946 if (af->data == NULL)
947 return af_ladspa_malloc_failed((char*)af_info_ladspa.name);
949 af->setup = calloc(1, sizeof(af_ladspa_t));
950 if (af->setup == NULL) {
951 free(af->data);
952 af->data=NULL;
953 return af_ladspa_malloc_failed((char*)af_info_ladspa.name);
956 ((af_ladspa_t*)af->setup)->status = AF_ERROR; /* will be set to AF_OK if
957 * all went OK and play()
958 * should proceed.
961 ((af_ladspa_t*)af->setup)->myname = strdup(af_info_ladspa.name);
962 if (!((af_ladspa_t*)af->setup)->myname)
963 return af_ladspa_malloc_failed((char*)af_info_ladspa.name);
965 return AF_OK;
968 /* ------------------------------------------------------------------------- */