1 /* ------------------------------------------------------------------------- */
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.
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
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
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 /* ------------------------------------------------------------------------- */
73 /* ------------------------------------------------------------------------- */
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
94 int activated
; /**< 0 or 1. Activate LADSPA filters only once, even
95 * if the buffers get resized, to avoid a stuttering
102 char *myname
; /**< It's easy to have a concatenation of file and label */
105 const LADSPA_Descriptor
*plugin_descriptor
;
116 int *inputcontrolsmap
; /**< Map input port number [0-] to actual port */
117 float *inputcontrols
;
120 int *outputcontrolsmap
;
121 float *outputcontrols
;
123 int nch
; /**< number of channels */
127 LADSPA_Handle
*chhandles
;
131 /* ------------------------------------------------------------------------- */
133 static int open(af_instance_t
*af
);
134 static int af_ladspa_malloc_failed(char*);
136 /* ------------------------------------------------------------------------- */
140 af_info_t af_info_ladspa
= {
141 "LADSPA plugin loader",
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
) {
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 */
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 */
208 setup
->ninputcontrols
= 0;
209 setup
->noutputcontrols
= 0;
211 /* check all ports, see what type it is and set variables according to
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
;
222 } else if (LADSPA_IS_PORT_OUTPUT(d
)) {
223 setup
->outputs
[setup
->noutputs
] = p
;
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
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
);
263 if (setup
->noutputs
== 0) {
264 af_msg(AF_MSG_ERROR
, "%s: %s\n", setup
->myname
,
265 MSGTR_AF_LADSPA_ErrNoOutputs
);
269 if (setup
->noutputs
!= setup
->ninputs
) {
270 af_msg(AF_MSG_ERROR
, "%s: %s\n", setup
->myname
,
271 MSGTR_AF_LADSPA_ErrInOutDiff
);
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
);
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
);
294 af_msg(AF_MSG_VERBOSE
, "...]\n");
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
) {
331 const char *end
, *start
, *ladspapath
;
332 int endsinso
, needslash
;
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,
341 af_msg(AF_MSG_VERBOSE
, "\ton windows, only absolute pathnames "
343 af_msg(AF_MSG_VERBOSE
, "\ttrying %s\n", filename
);
344 return dlopen(filename
, flag
);
347 filenamelen
= strlen(filename
);
351 endsinso
= (strcmp(filename
+filenamelen
-3, ".so") == 0);
353 buf
=malloc(filenamelen
+4);
354 strcpy(buf
, filename
);
356 result
=mydlopen(buf
, flag
);
363 ladspapath
=getenv("LADSPA_PATH");
368 while (*start
!= '\0') {
370 while ( (*end
!= ':') && (*end
!= '\0') )
373 buf
=malloc(filenamelen
+ 2 + (end
-start
) );
375 strncpy(buf
, start
, end
-start
);
378 if (*(end
-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
);
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
;
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() );
436 af_msg(AF_MSG_VERBOSE
, "%s: library found.\n", setup
->myname
);
438 /* find descriptor function */
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());
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
);
455 ladspa_descriptor
= descriptor_function(i
);
456 if (ladspa_descriptor
== NULL
) {
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 */
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
);
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
,
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
498 static int af_ladspa_malloc_failed(char *myname
) {
499 af_msg(AF_MSG_ERROR
, "%s: %s\n", myname
, MSGTR_MemAllocFailed
);
503 /* ------------------------------------------------------------------------- */
505 /** \brief Controls the filter.
507 * Control the behaviour of the filter.
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,
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
527 static int control(struct af_instance_s
*af
, int cmd
, void *arg
) {
528 af_ladspa_t
*setup
= (af_ladspa_t
*) af
->setup
;
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
;
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
: {
553 af_msg(AF_MSG_VERBOSE
, "%s: parse suboptions\n", setup
->myname
);
555 /* suboption parser here!
556 * format is (ladspa=)file:label:controls....
560 af_msg(AF_MSG_ERROR
, "%s: %s\n", setup
->myname
,
561 MSGTR_AF_LADSPA_ErrNoSuboptions
);
565 buf
= malloc(strlen(arg
)+1);
566 if (!buf
) return af_ladspa_malloc_failed(setup
->myname
);
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
);
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
,
582 if (*(char*)arg
!= '\0') arg
++; /* read ':' */
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
);
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
,
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
);
613 if ( af_ladspa_load_plugin(setup
) != AF_OK
)
616 /* see what inputs, outputs and controls this plugin has */
617 if ( af_ladspa_parse_plugin(setup
) != AF_OK
)
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
);
629 r
= sscanf(arg
, "%f", &val
);
631 af_msg(AF_MSG_ERROR
, "%s: %s\n", setup
->myname
,
632 MSGTR_AF_LADSPA_ErrNotEnoughControls
);
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",
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
);
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
);
669 af_msg(AF_MSG_VERBOSE
, "%s: all controls have sane values\n",
673 setup
->status
= AF_OK
;
681 /* ------------------------------------------------------------------------- */
683 /** \brief Uninitialise the LADSPA Plugin Loader filter.
685 * This function deactivates the plugin(s), cleans up, frees all allocated
688 * \return No return value.
691 static void uninit(struct af_instance_s
*af
) {
697 af_ladspa_t
*setup
= (af_ladspa_t
*) af
->setup
;
698 const LADSPA_Descriptor
*pdes
= setup
->plugin_descriptor
;
701 af_msg(AF_MSG_VERBOSE
, "%s: cleaning up\n", setup
->myname
);
705 if (setup
->chhandles
) {
706 for(i
=0; i
<setup
->nch
; i
++) {
707 if ( (setup
->ninputs
== 2) && (i
& 1) ) { /* stereo effect */
711 if (pdes
->deactivate
) pdes
->deactivate(setup
->chhandles
[i
]);
712 if (pdes
->cleanup
) pdes
->cleanup(setup
->chhandles
[i
]);
714 free(setup
->chhandles
);
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
);
732 free(setup
->outputs
);
735 for(i
=0; i
<setup
->nch
; i
++) {
736 if (setup
->inbufs
[i
])
737 free(setup
->inbufs
[i
]);
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
);
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 */
774 int rate
= data
->rate
;
778 if (setup
->status
!=AF_OK
)
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",
800 for(i
=0; i
<setup
->nch
; i
++) {
802 free(setup
->inbufs
[i
]);
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
;
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
));
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];
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 ],
859 pdes
->connect_port(setup
->chhandles
[i
],
860 setup
->outputs
[ (setup
->ninputs
==2) ? i
&1 : 0 ],
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
]) );
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 ],
895 pdes
->connect_port(setup
->chhandles
[i
-1],
896 setup
->outputs
[ (setup
->ninputs
==2) ? i
&1 : 0 ],
897 setup
->outbufs
[i
-1]);
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 :)
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
;
920 for (i
=0; i
<nch
; i
++) {
921 pdes
->run(setup
->chhandles
[i
], setup
->bufsize
);
922 if (setup
->ninputs
==2) // stereo effect just ran
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
];
932 v
= (v
> 32767.0f
? 32767.0f
: v
);
933 v
= (v
< -32768.0f
? -32768.0f
: v
);
934 audio
[p
*nch
+ i
] = (int16_t) v
;
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
) {
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
) {
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()
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
);
983 /* ------------------------------------------------------------------------- */