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
);
51 PA_MODULE_LOAD_ONCE(FALSE
);
53 "sink_name=<name for the sink> "
54 "master=<name of sink to remap> "
55 "format=<sample format> "
56 "channels=<number of channels> "
58 "channel_map=<channel map> "
59 "plugin=<ladspa plugin name> "
60 "label=<ladspa plugin label> "
61 "control=<comma seperated list of input control values>");
67 pa_sink
*sink
, *master
;
68 pa_sink_input
*sink_input
;
70 const LADSPA_Descriptor
*descriptor
;
72 LADSPA_Handle handle
[PA_CHANNELS_MAX
];
73 LADSPA_Data
*input
, *output
;
75 unsigned long input_port
, output_port
;
78 /* This is a dummy buffer. Every port must be connected, but we don't care
79 about control out ports. We connect them all to this single buffer. */
80 LADSPA_Data control_out
;
85 static const char* const valid_modargs
[] = {
98 /* Called from I/O thread context */
99 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
100 struct userdata
*u
= PA_SINK(o
)->userdata
;
104 case PA_SINK_MESSAGE_GET_LATENCY
: {
107 if (PA_MSGOBJECT(u
->master
)->process_msg(PA_MSGOBJECT(u
->master
), PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) < 0)
110 *((pa_usec_t
*) data
) = usec
+ pa_bytes_to_usec(u
->memchunk
.length
, &u
->sink
->sample_spec
);
115 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
118 /* Called from main context */
119 static int sink_set_state(pa_sink
*s
, pa_sink_state_t state
) {
122 pa_sink_assert_ref(s
);
123 pa_assert_se(u
= s
->userdata
);
125 if (PA_SINK_LINKED(state
) && u
->sink_input
&& PA_SINK_INPUT_LINKED(pa_sink_input_get_state(u
->sink_input
)))
126 pa_sink_input_cork(u
->sink_input
, state
== PA_SINK_SUSPENDED
);
131 /* Called from I/O thread context */
132 static int sink_input_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
133 struct userdata
*u
= PA_SINK_INPUT(o
)->userdata
;
136 case PA_SINK_INPUT_MESSAGE_GET_LATENCY
:
137 *((pa_usec_t
*) data
) = pa_bytes_to_usec(u
->memchunk
.length
, &u
->sink_input
->sample_spec
);
139 /* Fall through, the default handler will add in the extra
140 * latency added by the resampler */
144 return pa_sink_input_process_msg(o
, code
, data
, offset
, chunk
);
147 /* Called from I/O thread context */
148 static int sink_input_peek_cb(pa_sink_input
*i
, size_t length
, pa_memchunk
*chunk
) {
151 pa_sink_input_assert_ref(i
);
152 pa_assert_se(u
= i
->userdata
);
154 if (!u
->memchunk
.memblock
) {
160 pa_sink_render(u
->sink
, length
, &tchunk
);
162 fs
= pa_frame_size(&i
->sample_spec
);
163 n
= tchunk
.length
/ fs
;
167 u
->memchunk
.memblock
= pa_memblock_new(i
->sink
->core
->mempool
, tchunk
.length
);
168 u
->memchunk
.index
= 0;
169 u
->memchunk
.length
= tchunk
.length
;
171 src
= (float*) ((uint8_t*) pa_memblock_acquire(tchunk
.memblock
) + tchunk
.index
);
172 dst
= (float*) pa_memblock_acquire(u
->memchunk
.memblock
);
174 for (c
= 0; c
< u
->channels
; c
++) {
180 for (j
= 0; j
< n
; j
++, p
+= u
->channels
, q
++)
181 *q
= PA_CLAMP_UNLIKELY(*p
, -1.0, 1.0);
183 u
->descriptor
->run(u
->handle
[c
], n
);
187 for (j
= 0; j
< n
; j
++, q
++, p
+= u
->channels
)
188 *p
= PA_CLAMP_UNLIKELY(*q
, -1.0, 1.0);
191 pa_memblock_release(tchunk
.memblock
);
192 pa_memblock_release(u
->memchunk
.memblock
);
194 pa_memblock_unref(tchunk
.memblock
);
197 pa_assert(u
->memchunk
.length
> 0);
198 pa_assert(u
->memchunk
.memblock
);
200 *chunk
= u
->memchunk
;
201 pa_memblock_ref(chunk
->memblock
);
206 /* Called from I/O thread context */
207 static void sink_input_drop_cb(pa_sink_input
*i
, size_t length
) {
210 pa_sink_input_assert_ref(i
);
211 pa_assert_se(u
= i
->userdata
);
212 pa_assert(length
> 0);
214 if (u
->memchunk
.memblock
) {
216 if (length
< u
->memchunk
.length
) {
217 u
->memchunk
.index
+= length
;
218 u
->memchunk
.length
-= length
;
222 pa_memblock_unref(u
->memchunk
.memblock
);
223 length
-= u
->memchunk
.length
;
224 pa_memchunk_reset(&u
->memchunk
);
228 pa_sink_skip(u
->sink
, length
);
231 /* Called from I/O thread context */
232 static void sink_input_detach_cb(pa_sink_input
*i
) {
235 pa_sink_input_assert_ref(i
);
236 pa_assert_se(u
= i
->userdata
);
238 pa_sink_detach_within_thread(u
->sink
);
241 /* Called from I/O thread context */
242 static void sink_input_attach_cb(pa_sink_input
*i
) {
245 pa_sink_input_assert_ref(i
);
246 pa_assert_se(u
= i
->userdata
);
248 pa_sink_set_asyncmsgq(u
->sink
, i
->sink
->asyncmsgq
);
249 pa_sink_set_rtpoll(u
->sink
, i
->sink
->rtpoll
);
251 pa_sink_attach_within_thread(u
->sink
);
254 /* Called from main context */
255 static void sink_input_kill_cb(pa_sink_input
*i
) {
258 pa_sink_input_assert_ref(i
);
259 pa_assert_se(u
= i
->userdata
);
261 pa_sink_input_unlink(u
->sink_input
);
262 pa_sink_input_unref(u
->sink_input
);
263 u
->sink_input
= NULL
;
265 pa_sink_unlink(u
->sink
);
266 pa_sink_unref(u
->sink
);
269 pa_module_unload_request(u
->module
);
272 int pa__init(pa_module
*m
) {
279 pa_sink_input_new_data data
;
280 const char *plugin
, *label
;
281 LADSPA_Descriptor_Function descriptor_func
;
282 const char *e
, *cdata
;
283 const LADSPA_Descriptor
*d
;
284 unsigned long input_port
, output_port
, p
, j
, n_control
;
286 pa_bool_t
*use_default
= NULL
;
287 char *default_sink_name
= NULL
;
291 pa_assert(sizeof(LADSPA_Data
) == sizeof(float));
293 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
294 pa_log("Failed to parse module arguments.");
298 if (!(master
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "master", NULL
), PA_NAMEREG_SINK
, 1))) {
299 pa_log("Master sink not found");
303 ss
= master
->sample_spec
;
304 ss
.format
= PA_SAMPLE_FLOAT32
;
305 map
= master
->channel_map
;
306 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
, PA_CHANNEL_MAP_DEFAULT
) < 0) {
307 pa_log("Invalid sample format specification or channel map");
311 if (!(plugin
= pa_modargs_get_value(ma
, "plugin", NULL
))) {
312 pa_log("Missing LADSPA plugin name");
316 if (!(label
= pa_modargs_get_value(ma
, "label", NULL
))) {
317 pa_log("Missing LADSPA plugin label");
321 cdata
= pa_modargs_get_value(ma
, "control", NULL
);
323 u
= pa_xnew0(struct userdata
, 1);
328 pa_memchunk_reset(&u
->memchunk
);
330 if (!(e
= getenv("LADSPA_PATH")))
333 /* FIXME: This is not exactly thread safe */
334 t
= pa_xstrdup(lt_dlgetsearchpath());
335 lt_dlsetsearchpath(e
);
336 m
->dl
= lt_dlopenext(plugin
);
337 lt_dlsetsearchpath(t
);
341 pa_log("Failed to load LADSPA plugin: %s", lt_dlerror());
345 if (!(descriptor_func
= (LADSPA_Descriptor_Function
) lt_dlsym(m
->dl
, "ladspa_descriptor"))) {
346 pa_log("LADSPA module lacks ladspa_descriptor() symbol.");
352 if (!(d
= descriptor_func(j
))) {
353 pa_log("Failed to find plugin label '%s' in plugin '%s'.", plugin
, label
);
357 if (strcmp(d
->Label
, label
) == 0)
363 pa_log_debug("Module: %s", plugin
);
364 pa_log_debug("Label: %s", d
->Label
);
365 pa_log_debug("Unique ID: %lu", d
->UniqueID
);
366 pa_log_debug("Name: %s", d
->Name
);
367 pa_log_debug("Maker: %s", d
->Maker
);
368 pa_log_debug("Copyright: %s", d
->Copyright
);
370 input_port
= output_port
= (unsigned long) -1;
373 for (p
= 0; p
< d
->PortCount
; p
++) {
375 if (LADSPA_IS_PORT_INPUT(d
->PortDescriptors
[p
]) && LADSPA_IS_PORT_AUDIO(d
->PortDescriptors
[p
])) {
377 if (strcmp(d
->PortNames
[p
], "Input") == 0) {
378 pa_assert(input_port
== (unsigned long) -1);
381 pa_log("Found audio input port on plugin we cannot handle: %s", d
->PortNames
[p
]);
385 } else if (LADSPA_IS_PORT_OUTPUT(d
->PortDescriptors
[p
]) && LADSPA_IS_PORT_AUDIO(d
->PortDescriptors
[p
])) {
387 if (strcmp(d
->PortNames
[p
], "Output") == 0) {
388 pa_assert(output_port
== (unsigned long) -1);
391 pa_log("Found audio output port on plugin we cannot handle: %s", d
->PortNames
[p
]);
395 } else if (LADSPA_IS_PORT_INPUT(d
->PortDescriptors
[p
]) && LADSPA_IS_PORT_CONTROL(d
->PortDescriptors
[p
]))
398 pa_assert(LADSPA_IS_PORT_OUTPUT(d
->PortDescriptors
[p
]) && LADSPA_IS_PORT_CONTROL(d
->PortDescriptors
[p
]));
399 pa_log_debug("Ignored control output port \"%s\".", d
->PortNames
[p
]);
403 if ((input_port
== (unsigned long) -1) || (output_port
== (unsigned long) -1)) {
404 pa_log("Failed to identify input and output ports. "
405 "Right now this module can only deal with plugins which provide an 'Input' and an 'Output' audio port. "
410 u
->block_size
= pa_frame_align(pa_mempool_block_size_max(m
->core
->mempool
), &ss
);
412 u
->input
= (LADSPA_Data
*) pa_xnew(uint8_t, u
->block_size
);
413 if (LADSPA_IS_INPLACE_BROKEN(d
->Properties
))
414 u
->output
= (LADSPA_Data
*) pa_xnew(uint8_t, u
->block_size
);
416 u
->output
= u
->input
;
418 u
->channels
= ss
.channels
;
420 for (c
= 0; c
< ss
.channels
; c
++) {
421 if (!(u
->handle
[c
] = d
->instantiate(d
, ss
.rate
))) {
422 pa_log("Failed to instantiate plugin %s with label %s for channel %i", plugin
, d
->Label
, c
);
426 d
->connect_port(u
->handle
[c
], input_port
, u
->input
);
427 d
->connect_port(u
->handle
[c
], output_port
, u
->output
);
430 if (!cdata
&& n_control
> 0) {
431 pa_log("This plugin requires specification of %lu control parameters.", n_control
);
436 const char *state
= NULL
;
440 u
->control
= pa_xnew(LADSPA_Data
, n_control
);
441 use_default
= pa_xnew(pa_bool_t
, n_control
);
444 while ((k
= pa_split(cdata
, ",", &state
))) {
448 use_default
[p
++] = TRUE
;
453 if (pa_atof(k
, &f
) < 0) {
454 pa_log("Failed to parse control value '%s'", k
);
461 if (p
>= n_control
) {
462 pa_log("Too many control values passed, %lu expected.", n_control
);
466 use_default
[p
] = FALSE
;
471 pa_log("Not enough control values passed, %lu expected, %lu passed.", n_control
, p
);
476 for (p
= 0; p
< d
->PortCount
; p
++) {
477 LADSPA_PortRangeHintDescriptor hint
= d
->PortRangeHints
[p
].HintDescriptor
;
479 if (!LADSPA_IS_PORT_CONTROL(d
->PortDescriptors
[p
]))
482 if (LADSPA_IS_PORT_OUTPUT(d
->PortDescriptors
[p
])) {
483 for (c
= 0; c
< ss
.channels
; c
++)
484 d
->connect_port(u
->handle
[c
], p
, &u
->control_out
);
488 pa_assert(h
< n_control
);
490 if (use_default
[h
]) {
491 LADSPA_Data lower
, upper
;
493 if (!LADSPA_IS_HINT_HAS_DEFAULT(hint
)) {
494 pa_log("Control port value left empty but plugin defines no default.");
498 lower
= d
->PortRangeHints
[p
].LowerBound
;
499 upper
= d
->PortRangeHints
[p
].UpperBound
;
501 if (LADSPA_IS_HINT_SAMPLE_RATE(hint
)) {
506 switch (hint
& LADSPA_HINT_DEFAULT_MASK
) {
508 case LADSPA_HINT_DEFAULT_MINIMUM
:
509 u
->control
[h
] = lower
;
512 case LADSPA_HINT_DEFAULT_MAXIMUM
:
513 u
->control
[h
] = upper
;
516 case LADSPA_HINT_DEFAULT_LOW
:
517 if (LADSPA_IS_HINT_LOGARITHMIC(hint
))
518 u
->control
[h
] = exp(log(lower
) * 0.75 + log(upper
) * 0.25);
520 u
->control
[h
] = lower
* 0.75 + upper
* 0.25;
523 case LADSPA_HINT_DEFAULT_MIDDLE
:
524 if (LADSPA_IS_HINT_LOGARITHMIC(hint
))
525 u
->control
[h
] = exp(log(lower
) * 0.5 + log(upper
) * 0.5);
527 u
->control
[h
] = lower
* 0.5 + upper
* 0.5;
530 case LADSPA_HINT_DEFAULT_HIGH
:
531 if (LADSPA_IS_HINT_LOGARITHMIC(hint
))
532 u
->control
[h
] = exp(log(lower
) * 0.25 + log(upper
) * 0.75);
534 u
->control
[h
] = lower
* 0.25 + upper
* 0.75;
537 case LADSPA_HINT_DEFAULT_0
:
541 case LADSPA_HINT_DEFAULT_1
:
545 case LADSPA_HINT_DEFAULT_100
:
549 case LADSPA_HINT_DEFAULT_440
:
554 pa_assert_not_reached();
558 if (LADSPA_IS_HINT_INTEGER(hint
))
559 u
->control
[h
] = roundf(u
->control
[h
]);
561 pa_log_debug("Binding %f to port %s", u
->control
[h
], d
->PortNames
[p
]);
563 for (c
= 0; c
< ss
.channels
; c
++)
564 d
->connect_port(u
->handle
[c
], p
, &u
->control
[h
]);
569 pa_assert(h
== n_control
);
573 for (c
= 0; c
< u
->channels
; c
++)
574 d
->activate(u
->handle
[c
]);
576 default_sink_name
= pa_sprintf_malloc("%s.ladspa", master
->name
);
579 if (!(u
->sink
= pa_sink_new(m
->core
, __FILE__
, pa_modargs_get_value(ma
, "sink_name", default_sink_name
), 0, &ss
, &map
))) {
580 pa_log("Failed to create sink.");
584 u
->sink
->parent
.process_msg
= sink_process_msg
;
585 u
->sink
->set_state
= sink_set_state
;
586 u
->sink
->userdata
= u
;
587 u
->sink
->flags
= PA_SINK_LATENCY
;
589 pa_sink_set_module(u
->sink
, m
);
590 pa_sink_set_description(u
->sink
, t
= pa_sprintf_malloc("LADSPA plugin '%s' on '%s'", label
, master
->description
));
592 pa_sink_set_asyncmsgq(u
->sink
, master
->asyncmsgq
);
593 pa_sink_set_rtpoll(u
->sink
, master
->rtpoll
);
595 /* Create sink input */
596 pa_sink_input_new_data_init(&data
);
597 data
.sink
= u
->master
;
598 data
.driver
= __FILE__
;
599 data
.name
= "LADSPA Stream";
600 pa_sink_input_new_data_set_sample_spec(&data
, &ss
);
601 pa_sink_input_new_data_set_channel_map(&data
, &map
);
604 if (!(u
->sink_input
= pa_sink_input_new(m
->core
, &data
, PA_SINK_INPUT_DONT_MOVE
)))
607 u
->sink_input
->parent
.process_msg
= sink_input_process_msg
;
608 u
->sink_input
->peek
= sink_input_peek_cb
;
609 u
->sink_input
->drop
= sink_input_drop_cb
;
610 u
->sink_input
->kill
= sink_input_kill_cb
;
611 u
->sink_input
->attach
= sink_input_attach_cb
;
612 u
->sink_input
->detach
= sink_input_detach_cb
;
613 u
->sink_input
->userdata
= u
;
615 pa_sink_put(u
->sink
);
616 pa_sink_input_put(u
->sink_input
);
620 pa_xfree(use_default
);
621 pa_xfree(default_sink_name
);
629 pa_xfree(use_default
);
630 pa_xfree(default_sink_name
);
637 void pa__done(pa_module
*m
) {
643 if (!(u
= m
->userdata
))
647 pa_sink_input_unlink(u
->sink_input
);
648 pa_sink_input_unref(u
->sink_input
);
652 pa_sink_unlink(u
->sink
);
653 pa_sink_unref(u
->sink
);
656 if (u
->memchunk
.memblock
)
657 pa_memblock_unref(u
->memchunk
.memblock
);
659 for (c
= 0; c
< u
->channels
; c
++)
661 if (u
->descriptor
->deactivate
)
662 u
->descriptor
->deactivate(u
->handle
[c
]);
663 u
->descriptor
->cleanup(u
->handle
[c
]);
666 if (u
->output
!= u
->input
)
671 pa_xfree(u
->control
);