4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 /* TODO: Some plugins cause latency, and some even report it by using a control
25 out port. We don't currently use the latency information. */
31 #include <pulse/xmalloc.h>
33 #include <pulsecore/core-error.h>
34 #include <pulsecore/namereg.h>
35 #include <pulsecore/sink.h>
36 #include <pulsecore/module.h>
37 #include <pulsecore/core-util.h>
38 #include <pulsecore/modargs.h>
39 #include <pulsecore/log.h>
40 #include <pulsecore/thread.h>
41 #include <pulsecore/thread-mq.h>
42 #include <pulsecore/rtpoll.h>
43 #include <pulsecore/sample-util.h>
45 #include "module-ladspa-sink-symdef.h"
48 PA_MODULE_AUTHOR("Lennart Poettering")
49 PA_MODULE_DESCRIPTION("Virtual LADSPA sink")
50 PA_MODULE_VERSION(PACKAGE_VERSION
)
52 "sink_name=<name for the sink> "
53 "master=<name of sink to remap> "
54 "format=<sample format> "
55 "channels=<number of channels> "
57 "channel_map=<channel map> "
58 "plugin=<ladspa plugin name> "
59 "label=<ladspa plugin label> "
60 "control=<comma seperated list of input control values>")
66 pa_sink
*sink
, *master
;
67 pa_sink_input
*sink_input
;
69 const LADSPA_Descriptor
*descriptor
;
71 LADSPA_Handle handle
[PA_CHANNELS_MAX
];
72 LADSPA_Data
*input
, *output
;
74 unsigned long input_port
, output_port
;
77 /* This is a dummy buffer. Every port must be connected, but we don't care
78 about control out ports. We connect them all to this single buffer. */
79 LADSPA_Data control_out
;
84 static const char* const valid_modargs
[] = {
97 /* Called from I/O thread context */
98 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
99 struct userdata
*u
= PA_SINK(o
)->userdata
;
103 case PA_SINK_MESSAGE_GET_LATENCY
: {
106 if (PA_MSGOBJECT(u
->master
)->process_msg(PA_MSGOBJECT(u
->master
), PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) < 0)
109 *((pa_usec_t
*) data
) = usec
+ pa_bytes_to_usec(u
->memchunk
.length
, &u
->sink
->sample_spec
);
114 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
117 /* Called from main context */
118 static int sink_set_state(pa_sink
*s
, pa_sink_state_t state
) {
121 pa_sink_assert_ref(s
);
122 pa_assert_se(u
= s
->userdata
);
124 if (PA_SINK_LINKED(state
) && u
->sink_input
&& PA_SINK_INPUT_LINKED(pa_sink_input_get_state(u
->sink_input
)))
125 pa_sink_input_cork(u
->sink_input
, state
== PA_SINK_SUSPENDED
);
130 /* Called from I/O thread context */
131 static int sink_input_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
132 struct userdata
*u
= PA_SINK_INPUT(o
)->userdata
;
135 case PA_SINK_INPUT_MESSAGE_GET_LATENCY
:
136 *((pa_usec_t
*) data
) = pa_bytes_to_usec(u
->memchunk
.length
, &u
->sink_input
->sample_spec
);
138 /* Fall through, the default handler will add in the extra
139 * latency added by the resampler */
143 return pa_sink_input_process_msg(o
, code
, data
, offset
, chunk
);
146 /* Called from I/O thread context */
147 static int sink_input_peek_cb(pa_sink_input
*i
, size_t length
, pa_memchunk
*chunk
) {
150 pa_sink_input_assert_ref(i
);
151 pa_assert_se(u
= i
->userdata
);
153 if (!u
->memchunk
.memblock
) {
159 pa_sink_render(u
->sink
, length
, &tchunk
);
161 fs
= pa_frame_size(&i
->sample_spec
);
162 n
= tchunk
.length
/ fs
;
166 u
->memchunk
.memblock
= pa_memblock_new(i
->sink
->core
->mempool
, tchunk
.length
);
167 u
->memchunk
.index
= 0;
168 u
->memchunk
.length
= tchunk
.length
;
170 src
= (float*) ((uint8_t*) pa_memblock_acquire(tchunk
.memblock
) + tchunk
.index
);
171 dst
= (float*) pa_memblock_acquire(u
->memchunk
.memblock
);
173 for (c
= 0; c
< u
->channels
; c
++) {
179 for (j
= 0; j
< n
; j
++, p
+= u
->channels
, q
++)
180 *q
= CLAMP(*p
, -1.0, 1.0);
182 u
->descriptor
->run(u
->handle
[c
], n
);
186 for (j
= 0; j
< n
; j
++, q
++, p
+= u
->channels
)
187 *p
= CLAMP(*q
, -1.0, 1.0);
190 pa_memblock_release(tchunk
.memblock
);
191 pa_memblock_release(u
->memchunk
.memblock
);
193 pa_memblock_unref(tchunk
.memblock
);
196 pa_assert(u
->memchunk
.length
> 0);
197 pa_assert(u
->memchunk
.memblock
);
199 *chunk
= u
->memchunk
;
200 pa_memblock_ref(chunk
->memblock
);
205 /* Called from I/O thread context */
206 static void sink_input_drop_cb(pa_sink_input
*i
, size_t length
) {
209 pa_sink_input_assert_ref(i
);
210 pa_assert_se(u
= i
->userdata
);
211 pa_assert(length
> 0);
213 if (u
->memchunk
.memblock
) {
215 if (length
< u
->memchunk
.length
) {
216 u
->memchunk
.index
+= length
;
217 u
->memchunk
.length
-= length
;
221 pa_memblock_unref(u
->memchunk
.memblock
);
222 length
-= u
->memchunk
.length
;
223 pa_memchunk_reset(&u
->memchunk
);
227 pa_sink_skip(u
->sink
, length
);
230 /* Called from I/O thread context */
231 static void sink_input_detach_cb(pa_sink_input
*i
) {
234 pa_sink_input_assert_ref(i
);
235 pa_assert_se(u
= i
->userdata
);
237 pa_sink_detach_within_thread(u
->sink
);
240 /* Called from I/O thread context */
241 static void sink_input_attach_cb(pa_sink_input
*i
) {
244 pa_sink_input_assert_ref(i
);
245 pa_assert_se(u
= i
->userdata
);
247 pa_sink_set_asyncmsgq(u
->sink
, i
->sink
->asyncmsgq
);
248 pa_sink_set_rtpoll(u
->sink
, i
->sink
->rtpoll
);
250 pa_sink_attach_within_thread(u
->sink
);
253 /* Called from main context */
254 static void sink_input_kill_cb(pa_sink_input
*i
) {
257 pa_sink_input_assert_ref(i
);
258 pa_assert_se(u
= i
->userdata
);
260 pa_sink_input_unlink(u
->sink_input
);
261 pa_sink_input_unref(u
->sink_input
);
262 u
->sink_input
= NULL
;
264 pa_sink_unlink(u
->sink
);
265 pa_sink_unref(u
->sink
);
268 pa_module_unload_request(u
->module
);
271 int pa__init(pa_module
*m
) {
278 pa_sink_input_new_data data
;
279 const char *plugin
, *label
;
280 LADSPA_Descriptor_Function descriptor_func
;
281 const char *e
, *cdata
;
282 const LADSPA_Descriptor
*d
;
283 unsigned long input_port
, output_port
, p
, j
, n_control
;
285 pa_bool_t
*use_default
= NULL
;
286 char *default_sink_name
= NULL
;
290 pa_assert(sizeof(LADSPA_Data
) == sizeof(float));
292 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
293 pa_log("Failed to parse module arguments.");
297 if (!(master
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "master", NULL
), PA_NAMEREG_SINK
, 1))) {
298 pa_log("Master sink not found");
302 ss
= master
->sample_spec
;
303 ss
.format
= PA_SAMPLE_FLOAT32
;
304 map
= master
->channel_map
;
305 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
, PA_CHANNEL_MAP_DEFAULT
) < 0) {
306 pa_log("Invalid sample format specification or channel map");
310 if (!(plugin
= pa_modargs_get_value(ma
, "plugin", NULL
))) {
311 pa_log("Missing LADSPA plugin name");
315 if (!(label
= pa_modargs_get_value(ma
, "label", NULL
))) {
316 pa_log("Missing LADSPA plugin label");
320 cdata
= pa_modargs_get_value(ma
, "control", NULL
);
322 u
= pa_xnew0(struct userdata
, 1);
327 pa_memchunk_reset(&u
->memchunk
);
329 if (!(e
= getenv("LADSPA_PATH")))
332 /* FIXME: This is not exactly thread safe */
333 t
= pa_xstrdup(lt_dlgetsearchpath());
334 lt_dlsetsearchpath(e
);
335 m
->dl
= lt_dlopenext(plugin
);
336 lt_dlsetsearchpath(t
);
340 pa_log("Failed to load LADSPA plugin: %s", lt_dlerror());
344 if (!(descriptor_func
= (LADSPA_Descriptor_Function
) lt_dlsym(m
->dl
, "ladspa_descriptor"))) {
345 pa_log("LADSPA module lacks ladspa_descriptor() symbol.");
351 if (!(d
= descriptor_func(j
))) {
352 pa_log("Failed to find plugin label '%s' in plugin '%s'.", plugin
, label
);
356 if (strcmp(d
->Label
, label
) == 0)
362 pa_log_debug("Module: %s", plugin
);
363 pa_log_debug("Label: %s", d
->Label
);
364 pa_log_debug("Unique ID: %lu", d
->UniqueID
);
365 pa_log_debug("Name: %s", d
->Name
);
366 pa_log_debug("Maker: %s", d
->Maker
);
367 pa_log_debug("Copyright: %s", d
->Copyright
);
369 input_port
= output_port
= (unsigned long) -1;
372 for (p
= 0; p
< d
->PortCount
; p
++) {
374 if (LADSPA_IS_PORT_INPUT(d
->PortDescriptors
[p
]) && LADSPA_IS_PORT_AUDIO(d
->PortDescriptors
[p
])) {
376 if (strcmp(d
->PortNames
[p
], "Input") == 0) {
377 pa_assert(input_port
== (unsigned long) -1);
380 pa_log("Found audio input port on plugin we cannot handle: %s", d
->PortNames
[p
]);
384 } else if (LADSPA_IS_PORT_OUTPUT(d
->PortDescriptors
[p
]) && LADSPA_IS_PORT_AUDIO(d
->PortDescriptors
[p
])) {
386 if (strcmp(d
->PortNames
[p
], "Output") == 0) {
387 pa_assert(output_port
== (unsigned long) -1);
390 pa_log("Found audio output port on plugin we cannot handle: %s", d
->PortNames
[p
]);
394 } else if (LADSPA_IS_PORT_INPUT(d
->PortDescriptors
[p
]) && LADSPA_IS_PORT_CONTROL(d
->PortDescriptors
[p
]))
397 pa_assert(LADSPA_IS_PORT_OUTPUT(d
->PortDescriptors
[p
]) && LADSPA_IS_PORT_CONTROL(d
->PortDescriptors
[p
]));
398 pa_log_info("Ignored port \"%s\", because we ignore all control out ports.", d
->PortNames
[p
]);
402 if ((input_port
== (unsigned long) -1) || (output_port
== (unsigned long) -1)) {
403 pa_log("Failed to identify input and output ports. "
404 "Right now this module can only deal with plugins which provide an 'Input' and an 'Output' audio port. "
409 u
->block_size
= pa_frame_align(pa_mempool_block_size_max(m
->core
->mempool
), &ss
);
411 u
->input
= (LADSPA_Data
*) pa_xnew(uint8_t, u
->block_size
);
412 if (LADSPA_IS_INPLACE_BROKEN(d
->Properties
))
413 u
->output
= (LADSPA_Data
*) pa_xnew(uint8_t, u
->block_size
);
415 u
->output
= u
->input
;
417 u
->channels
= ss
.channels
;
419 for (c
= 0; c
< ss
.channels
; c
++) {
420 if (!(u
->handle
[c
] = d
->instantiate(d
, ss
.rate
))) {
421 pa_log("Failed to instantiate plugin %s with label %s for channel %i", plugin
, d
->Label
, c
);
425 d
->connect_port(u
->handle
[c
], input_port
, u
->input
);
426 d
->connect_port(u
->handle
[c
], output_port
, u
->output
);
429 if (!cdata
&& n_control
> 0) {
430 pa_log("This plugin requires specification of %lu control parameters.", n_control
);
435 const char *state
= NULL
;
439 u
->control
= pa_xnew(LADSPA_Data
, n_control
);
440 use_default
= pa_xnew(pa_bool_t
, n_control
);
443 while ((k
= pa_split(cdata
, ",", &state
))) {
447 use_default
[p
++] = TRUE
;
452 if (pa_atof(k
, &f
) < 0) {
453 pa_log("Failed to parse control value '%s'", k
);
460 if (p
>= n_control
) {
461 pa_log("Too many control values passed, %lu expected.", n_control
);
465 use_default
[p
] = FALSE
;
470 pa_log("Not enough control values passed, %lu expected, %lu passed.", n_control
, p
);
475 for (p
= 0; p
< d
->PortCount
; p
++) {
476 LADSPA_PortRangeHintDescriptor hint
= d
->PortRangeHints
[p
].HintDescriptor
;
478 if (!LADSPA_IS_PORT_CONTROL(d
->PortDescriptors
[p
]))
481 if (LADSPA_IS_PORT_OUTPUT(d
->PortDescriptors
[p
])) {
482 for (c
= 0; c
< ss
.channels
; c
++)
483 d
->connect_port(u
->handle
[c
], p
, &u
->control_out
);
487 pa_assert(h
< n_control
);
489 if (use_default
[h
]) {
490 LADSPA_Data lower
, upper
;
492 if (!LADSPA_IS_HINT_HAS_DEFAULT(hint
)) {
493 pa_log("Control port value left empty but plugin defines no default.");
497 lower
= d
->PortRangeHints
[p
].LowerBound
;
498 upper
= d
->PortRangeHints
[p
].UpperBound
;
500 if (LADSPA_IS_HINT_SAMPLE_RATE(hint
)) {
505 switch (hint
& LADSPA_HINT_DEFAULT_MASK
) {
507 case LADSPA_HINT_DEFAULT_MINIMUM
:
508 u
->control
[h
] = lower
;
511 case LADSPA_HINT_DEFAULT_MAXIMUM
:
512 u
->control
[h
] = upper
;
515 case LADSPA_HINT_DEFAULT_LOW
:
516 if (LADSPA_IS_HINT_LOGARITHMIC(hint
))
517 u
->control
[h
] = exp(log(lower
) * 0.75 + log(upper
) * 0.25);
519 u
->control
[h
] = lower
* 0.75 + upper
* 0.25;
522 case LADSPA_HINT_DEFAULT_MIDDLE
:
523 if (LADSPA_IS_HINT_LOGARITHMIC(hint
))
524 u
->control
[h
] = exp(log(lower
) * 0.5 + log(upper
) * 0.5);
526 u
->control
[h
] = lower
* 0.5 + upper
* 0.5;
529 case LADSPA_HINT_DEFAULT_HIGH
:
530 if (LADSPA_IS_HINT_LOGARITHMIC(hint
))
531 u
->control
[h
] = exp(log(lower
) * 0.25 + log(upper
) * 0.75);
533 u
->control
[h
] = lower
* 0.25 + upper
* 0.75;
536 case LADSPA_HINT_DEFAULT_0
:
540 case LADSPA_HINT_DEFAULT_1
:
544 case LADSPA_HINT_DEFAULT_100
:
548 case LADSPA_HINT_DEFAULT_440
:
553 pa_assert_not_reached();
557 if (LADSPA_IS_HINT_INTEGER(hint
))
558 u
->control
[h
] = roundf(u
->control
[h
]);
560 pa_log_debug("Binding %f to port %s", u
->control
[h
], d
->PortNames
[p
]);
562 for (c
= 0; c
< ss
.channels
; c
++)
563 d
->connect_port(u
->handle
[c
], p
, &u
->control
[h
]);
568 pa_assert(h
== n_control
);
572 for (c
= 0; c
< u
->channels
; c
++)
573 d
->activate(u
->handle
[c
]);
575 default_sink_name
= pa_sprintf_malloc("%s.ladspa", master
->name
);
578 if (!(u
->sink
= pa_sink_new(m
->core
, __FILE__
, pa_modargs_get_value(ma
, "sink_name", default_sink_name
), 0, &ss
, &map
))) {
579 pa_log("Failed to create sink.");
583 u
->sink
->parent
.process_msg
= sink_process_msg
;
584 u
->sink
->set_state
= sink_set_state
;
585 u
->sink
->userdata
= u
;
586 u
->sink
->flags
= PA_SINK_LATENCY
;
588 pa_sink_set_module(u
->sink
, m
);
589 pa_sink_set_description(u
->sink
, t
= pa_sprintf_malloc("LADSPA plugin '%s' on '%s'", label
, master
->description
));
591 pa_sink_set_asyncmsgq(u
->sink
, master
->asyncmsgq
);
592 pa_sink_set_rtpoll(u
->sink
, master
->rtpoll
);
594 /* Create sink input */
595 pa_sink_input_new_data_init(&data
);
596 data
.sink
= u
->master
;
597 data
.driver
= __FILE__
;
598 data
.name
= "LADSPA Stream";
599 pa_sink_input_new_data_set_sample_spec(&data
, &ss
);
600 pa_sink_input_new_data_set_channel_map(&data
, &map
);
603 if (!(u
->sink_input
= pa_sink_input_new(m
->core
, &data
, PA_SINK_INPUT_DONT_MOVE
)))
606 u
->sink_input
->parent
.process_msg
= sink_input_process_msg
;
607 u
->sink_input
->peek
= sink_input_peek_cb
;
608 u
->sink_input
->drop
= sink_input_drop_cb
;
609 u
->sink_input
->kill
= sink_input_kill_cb
;
610 u
->sink_input
->attach
= sink_input_attach_cb
;
611 u
->sink_input
->detach
= sink_input_detach_cb
;
612 u
->sink_input
->userdata
= u
;
614 pa_sink_put(u
->sink
);
615 pa_sink_input_put(u
->sink_input
);
619 pa_xfree(use_default
);
620 pa_xfree(default_sink_name
);
628 pa_xfree(use_default
);
629 pa_xfree(default_sink_name
);
636 void pa__done(pa_module
*m
) {
642 if (!(u
= m
->userdata
))
646 pa_sink_input_unlink(u
->sink_input
);
647 pa_sink_input_unref(u
->sink_input
);
651 pa_sink_unlink(u
->sink
);
652 pa_sink_unref(u
->sink
);
655 if (u
->memchunk
.memblock
)
656 pa_memblock_unref(u
->memchunk
.memblock
);
658 for (c
= 0; c
< u
->channels
; c
++)
660 if (u
->descriptor
->deactivate
)
661 u
->descriptor
->deactivate(u
->handle
[c
]);
662 u
->descriptor
->cleanup(u
->handle
[c
]);
665 if (u
->output
!= u
->input
)
670 pa_xfree(u
->control
);