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
;
83 static const char* const valid_modargs
[] = {
96 /* Called from I/O thread context */
97 static int sink_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
98 struct userdata
*u
= PA_SINK(o
)->userdata
;
102 case PA_SINK_MESSAGE_GET_LATENCY
: {
105 if (PA_MSGOBJECT(u
->master
)->process_msg(PA_MSGOBJECT(u
->master
), PA_SINK_MESSAGE_GET_LATENCY
, &usec
, 0, NULL
) < 0)
108 *((pa_usec_t
*) data
) = usec
/* + pa_bytes_to_usec(u->memchunk.length, &u->sink->sample_spec) */;
113 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
116 /* Called from main context */
117 static int sink_set_state(pa_sink
*s
, pa_sink_state_t state
) {
120 pa_sink_assert_ref(s
);
121 pa_assert_se(u
= s
->userdata
);
123 if (PA_SINK_LINKED(state
) && u
->sink_input
&& PA_SINK_INPUT_LINKED(pa_sink_input_get_state(u
->sink_input
)))
124 pa_sink_input_cork(u
->sink_input
, state
== PA_SINK_SUSPENDED
);
129 /* Called from I/O thread context */
130 static void sink_request_rewind(pa_sink
*s
) {
133 pa_sink_assert_ref(s
);
134 pa_assert_se(u
= s
->userdata
);
136 /* Just hand this one over to the master sink */
137 pa_sink_input_request_rewind(u
->sink_input
, s
->thread_info
.rewind_nbytes
, FALSE
);
140 /* Called from I/O thread context */
141 static void sink_update_requested_latency(pa_sink
*s
) {
144 pa_sink_assert_ref(s
);
145 pa_assert_se(u
= s
->userdata
);
147 /* Just hand this one over to the master sink */
148 u
->sink_input
->thread_info
.requested_sink_latency
= pa_sink_get_requested_latency_within_thread(s
);
149 pa_sink_invalidate_requested_latency(u
->master
);
152 /* Called from I/O thread context */
153 static int sink_input_process_msg(pa_msgobject
*o
, int code
, void *data
, int64_t offset
, pa_memchunk
*chunk
) {
154 struct userdata
*u
= PA_SINK_INPUT(o
)->userdata
;
157 case PA_SINK_INPUT_MESSAGE_GET_LATENCY
:
158 *((pa_usec_t
*) data
) = 0 /*pa_bytes_to_usec(u->memchunk.length, &u->sink_input->sample_spec)*/;
160 /* Fall through, the default handler will add in the extra
161 * latency added by the resampler */
165 return pa_sink_input_process_msg(o
, code
, data
, offset
, chunk
);
168 /* Called from I/O thread context */
169 static int sink_input_pop_cb(pa_sink_input
*i
, size_t nbytes
, pa_memchunk
*chunk
) {
176 pa_sink_input_assert_ref(i
);
178 pa_assert_se(u
= i
->userdata
);
180 pa_sink_render(u
->sink
, nbytes
, &tchunk
);
182 fs
= pa_frame_size(&i
->sample_spec
);
183 n
= tchunk
.length
/ fs
;
187 chunk
->memblock
= pa_memblock_new(i
->sink
->core
->mempool
, tchunk
.length
);
189 chunk
->length
= tchunk
.length
;
191 src
= (float*) ((uint8_t*) pa_memblock_acquire(tchunk
.memblock
) + tchunk
.index
);
192 dst
= (float*) pa_memblock_acquire(chunk
->memblock
);
194 for (c
= 0; c
< u
->channels
; c
++) {
200 for (j
= 0; j
< n
; j
++, p
+= u
->channels
, q
++)
201 *q
= PA_CLAMP_UNLIKELY(*p
, -1.0, 1.0);
203 u
->descriptor
->run(u
->handle
[c
], n
);
207 for (j
= 0; j
< n
; j
++, q
++, p
+= u
->channels
)
208 *p
= PA_CLAMP_UNLIKELY(*q
, -1.0, 1.0);
211 pa_memblock_release(tchunk
.memblock
);
212 pa_memblock_release(chunk
->memblock
);
214 pa_memblock_unref(tchunk
.memblock
);
219 /* Called from I/O thread context */
220 static void sink_input_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
223 pa_sink_input_assert_ref(i
);
224 pa_assert_se(u
= i
->userdata
);
225 pa_assert(nbytes
> 0);
227 pa_sink_process_rewind(u
->sink
, nbytes
);
230 /* Called from I/O thread context */
231 static void sink_input_set_max_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
234 pa_sink_input_assert_ref(i
);
235 pa_assert_se(u
= i
->userdata
);
237 pa_sink_set_max_rewind(u
->sink
, nbytes
);
240 /* Called from I/O thread context */
241 static void sink_input_detach_cb(pa_sink_input
*i
) {
244 pa_sink_input_assert_ref(i
);
245 pa_assert_se(u
= i
->userdata
);
247 pa_sink_detach_within_thread(u
->sink
);
250 /* Called from I/O thread context */
251 static void sink_input_attach_cb(pa_sink_input
*i
) {
254 pa_sink_input_assert_ref(i
);
255 pa_assert_se(u
= i
->userdata
);
257 pa_sink_set_asyncmsgq(u
->sink
, i
->sink
->asyncmsgq
);
258 pa_sink_set_rtpoll(u
->sink
, i
->sink
->rtpoll
);
260 pa_sink_attach_within_thread(u
->sink
);
263 /* Called from main context */
264 static void sink_input_kill_cb(pa_sink_input
*i
) {
267 pa_sink_input_assert_ref(i
);
268 pa_assert_se(u
= i
->userdata
);
270 pa_sink_input_unlink(u
->sink_input
);
271 pa_sink_input_unref(u
->sink_input
);
272 u
->sink_input
= NULL
;
274 pa_sink_unlink(u
->sink
);
275 pa_sink_unref(u
->sink
);
278 pa_module_unload_request(u
->module
);
281 int pa__init(pa_module
*m
) {
289 pa_sink_input_new_data sink_input_data
;
290 pa_sink_new_data sink_data
;
291 const char *plugin
, *label
;
292 LADSPA_Descriptor_Function descriptor_func
;
293 const char *e
, *cdata
;
294 const LADSPA_Descriptor
*d
;
295 unsigned long input_port
, output_port
, p
, j
, n_control
;
297 pa_bool_t
*use_default
= NULL
;
301 pa_assert(sizeof(LADSPA_Data
) == sizeof(float));
303 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
304 pa_log("Failed to parse module arguments.");
308 if (!(master
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "master", NULL
), PA_NAMEREG_SINK
, 1))) {
309 pa_log("Master sink not found");
313 ss
= master
->sample_spec
;
314 ss
.format
= PA_SAMPLE_FLOAT32
;
315 map
= master
->channel_map
;
316 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
, PA_CHANNEL_MAP_DEFAULT
) < 0) {
317 pa_log("Invalid sample format specification or channel map");
321 if (!(plugin
= pa_modargs_get_value(ma
, "plugin", NULL
))) {
322 pa_log("Missing LADSPA plugin name");
326 if (!(label
= pa_modargs_get_value(ma
, "label", NULL
))) {
327 pa_log("Missing LADSPA plugin label");
331 cdata
= pa_modargs_get_value(ma
, "control", NULL
);
333 u
= pa_xnew0(struct userdata
, 1);
339 u
->sink_input
= NULL
;
341 if (!(e
= getenv("LADSPA_PATH")))
344 /* FIXME: This is not exactly thread safe */
345 t
= pa_xstrdup(lt_dlgetsearchpath());
346 lt_dlsetsearchpath(e
);
347 m
->dl
= lt_dlopenext(plugin
);
348 lt_dlsetsearchpath(t
);
352 pa_log("Failed to load LADSPA plugin: %s", lt_dlerror());
356 if (!(descriptor_func
= (LADSPA_Descriptor_Function
) lt_dlsym(m
->dl
, "ladspa_descriptor"))) {
357 pa_log("LADSPA module lacks ladspa_descriptor() symbol.");
363 if (!(d
= descriptor_func(j
))) {
364 pa_log("Failed to find plugin label '%s' in plugin '%s'.", plugin
, label
);
368 if (strcmp(d
->Label
, label
) == 0)
374 pa_log_debug("Module: %s", plugin
);
375 pa_log_debug("Label: %s", d
->Label
);
376 pa_log_debug("Unique ID: %lu", d
->UniqueID
);
377 pa_log_debug("Name: %s", d
->Name
);
378 pa_log_debug("Maker: %s", d
->Maker
);
379 pa_log_debug("Copyright: %s", d
->Copyright
);
381 input_port
= output_port
= (unsigned long) -1;
384 for (p
= 0; p
< d
->PortCount
; p
++) {
386 if (LADSPA_IS_PORT_INPUT(d
->PortDescriptors
[p
]) && LADSPA_IS_PORT_AUDIO(d
->PortDescriptors
[p
])) {
388 if (strcmp(d
->PortNames
[p
], "Input") == 0) {
389 pa_assert(input_port
== (unsigned long) -1);
392 pa_log("Found audio input port on plugin we cannot handle: %s", d
->PortNames
[p
]);
396 } else if (LADSPA_IS_PORT_OUTPUT(d
->PortDescriptors
[p
]) && LADSPA_IS_PORT_AUDIO(d
->PortDescriptors
[p
])) {
398 if (strcmp(d
->PortNames
[p
], "Output") == 0) {
399 pa_assert(output_port
== (unsigned long) -1);
402 pa_log("Found audio output port on plugin we cannot handle: %s", d
->PortNames
[p
]);
406 } else if (LADSPA_IS_PORT_INPUT(d
->PortDescriptors
[p
]) && LADSPA_IS_PORT_CONTROL(d
->PortDescriptors
[p
]))
409 pa_assert(LADSPA_IS_PORT_OUTPUT(d
->PortDescriptors
[p
]) && LADSPA_IS_PORT_CONTROL(d
->PortDescriptors
[p
]));
410 pa_log_debug("Ignored control output port \"%s\".", d
->PortNames
[p
]);
414 if ((input_port
== (unsigned long) -1) || (output_port
== (unsigned long) -1)) {
415 pa_log("Failed to identify input and output ports. "
416 "Right now this module can only deal with plugins which provide an 'Input' and an 'Output' audio port. "
421 u
->block_size
= pa_frame_align(pa_mempool_block_size_max(m
->core
->mempool
), &ss
);
423 u
->input
= (LADSPA_Data
*) pa_xnew(uint8_t, u
->block_size
);
424 if (LADSPA_IS_INPLACE_BROKEN(d
->Properties
))
425 u
->output
= (LADSPA_Data
*) pa_xnew(uint8_t, u
->block_size
);
427 u
->output
= u
->input
;
429 u
->channels
= ss
.channels
;
431 for (c
= 0; c
< ss
.channels
; c
++) {
432 if (!(u
->handle
[c
] = d
->instantiate(d
, ss
.rate
))) {
433 pa_log("Failed to instantiate plugin %s with label %s for channel %i", plugin
, d
->Label
, c
);
437 d
->connect_port(u
->handle
[c
], input_port
, u
->input
);
438 d
->connect_port(u
->handle
[c
], output_port
, u
->output
);
441 if (!cdata
&& n_control
> 0) {
442 pa_log("This plugin requires specification of %lu control parameters.", n_control
);
447 const char *state
= NULL
;
451 u
->control
= pa_xnew(LADSPA_Data
, n_control
);
452 use_default
= pa_xnew(pa_bool_t
, n_control
);
455 while ((k
= pa_split(cdata
, ",", &state
)) && p
< n_control
) {
459 use_default
[p
++] = TRUE
;
464 if (pa_atof(k
, &f
) < 0) {
465 pa_log("Failed to parse control value '%s'", k
);
472 use_default
[p
] = FALSE
;
476 /* The previous loop doesn't take the last control value into account
477 if it is left empty, so we do it here. */
478 if (*cdata
== 0 || cdata
[strlen(cdata
) - 1] == ',') {
480 use_default
[p
] = TRUE
;
484 if (p
> n_control
|| k
) {
485 pa_log("Too many control values passed, %lu expected.", n_control
);
491 pa_log("Not enough control values passed, %lu expected, %lu passed.", n_control
, p
);
496 for (p
= 0; p
< d
->PortCount
; p
++) {
497 LADSPA_PortRangeHintDescriptor hint
= d
->PortRangeHints
[p
].HintDescriptor
;
499 if (!LADSPA_IS_PORT_CONTROL(d
->PortDescriptors
[p
]))
502 if (LADSPA_IS_PORT_OUTPUT(d
->PortDescriptors
[p
])) {
503 for (c
= 0; c
< ss
.channels
; c
++)
504 d
->connect_port(u
->handle
[c
], p
, &u
->control_out
);
508 pa_assert(h
< n_control
);
510 if (use_default
[h
]) {
511 LADSPA_Data lower
, upper
;
513 if (!LADSPA_IS_HINT_HAS_DEFAULT(hint
)) {
514 pa_log("Control port value left empty but plugin defines no default.");
518 lower
= d
->PortRangeHints
[p
].LowerBound
;
519 upper
= d
->PortRangeHints
[p
].UpperBound
;
521 if (LADSPA_IS_HINT_SAMPLE_RATE(hint
)) {
526 switch (hint
& LADSPA_HINT_DEFAULT_MASK
) {
528 case LADSPA_HINT_DEFAULT_MINIMUM
:
529 u
->control
[h
] = lower
;
532 case LADSPA_HINT_DEFAULT_MAXIMUM
:
533 u
->control
[h
] = upper
;
536 case LADSPA_HINT_DEFAULT_LOW
:
537 if (LADSPA_IS_HINT_LOGARITHMIC(hint
))
538 u
->control
[h
] = exp(log(lower
) * 0.75 + log(upper
) * 0.25);
540 u
->control
[h
] = lower
* 0.75 + upper
* 0.25;
543 case LADSPA_HINT_DEFAULT_MIDDLE
:
544 if (LADSPA_IS_HINT_LOGARITHMIC(hint
))
545 u
->control
[h
] = exp(log(lower
) * 0.5 + log(upper
) * 0.5);
547 u
->control
[h
] = lower
* 0.5 + upper
* 0.5;
550 case LADSPA_HINT_DEFAULT_HIGH
:
551 if (LADSPA_IS_HINT_LOGARITHMIC(hint
))
552 u
->control
[h
] = exp(log(lower
) * 0.25 + log(upper
) * 0.75);
554 u
->control
[h
] = lower
* 0.25 + upper
* 0.75;
557 case LADSPA_HINT_DEFAULT_0
:
561 case LADSPA_HINT_DEFAULT_1
:
565 case LADSPA_HINT_DEFAULT_100
:
569 case LADSPA_HINT_DEFAULT_440
:
574 pa_assert_not_reached();
578 if (LADSPA_IS_HINT_INTEGER(hint
))
579 u
->control
[h
] = roundf(u
->control
[h
]);
581 pa_log_debug("Binding %f to port %s", u
->control
[h
], d
->PortNames
[p
]);
583 for (c
= 0; c
< ss
.channels
; c
++)
584 d
->connect_port(u
->handle
[c
], p
, &u
->control
[h
]);
589 pa_assert(h
== n_control
);
593 for (c
= 0; c
< u
->channels
; c
++)
594 d
->activate(u
->handle
[c
]);
597 pa_sink_new_data_init(&sink_data
);
598 sink_data
.driver
= __FILE__
;
599 sink_data
.module
= m
;
600 if (!(sink_data
.name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink_name", NULL
))))
601 sink_data
.name
= pa_sprintf_malloc("%s.ladspa", master
->name
);
602 sink_data
.namereg_fail
= FALSE
;
603 pa_sink_new_data_set_sample_spec(&sink_data
, &ss
);
604 pa_sink_new_data_set_channel_map(&sink_data
, &map
);
605 z
= pa_proplist_gets(master
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
606 pa_proplist_sets(sink_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, t
= pa_sprintf_malloc("LADSPA Plugin %s on %s", label
, z
? z
: master
->name
));
608 pa_proplist_sets(sink_data
.proplist
, PA_PROP_DEVICE_MASTER_DEVICE
, master
->name
);
609 pa_proplist_sets(sink_data
.proplist
, "device.ladspa.module", plugin
);
610 pa_proplist_sets(sink_data
.proplist
, "device.ladspa.label", d
->Label
);
611 pa_proplist_sets(sink_data
.proplist
, "device.ladspa.name", d
->Name
);
612 pa_proplist_sets(sink_data
.proplist
, "device.ladspa.maker", d
->Maker
);
613 pa_proplist_sets(sink_data
.proplist
, "device.ladspa.copyright", d
->Copyright
);
614 pa_proplist_sets(sink_data
.proplist
, "device.ladspa.unique_id", t
= pa_sprintf_malloc("%lu", (unsigned long) d
->UniqueID
));
617 u
->sink
= pa_sink_new(m
->core
, &sink_data
, 0);
618 pa_sink_new_data_done(&sink_data
);
621 pa_log("Failed to create sink.");
625 u
->sink
->parent
.process_msg
= sink_process_msg
;
626 u
->sink
->set_state
= sink_set_state
;
627 u
->sink
->update_requested_latency
= sink_update_requested_latency
;
628 u
->sink
->request_rewind
= sink_request_rewind
;
629 u
->sink
->userdata
= u
;
630 u
->sink
->flags
= PA_SINK_LATENCY
;
632 pa_sink_set_asyncmsgq(u
->sink
, master
->asyncmsgq
);
633 pa_sink_set_rtpoll(u
->sink
, master
->rtpoll
);
635 /* Create sink input */
636 pa_sink_input_new_data_init(&sink_input_data
);
637 sink_input_data
.driver
= __FILE__
;
638 sink_input_data
.module
= m
;
639 sink_input_data
.sink
= u
->master
;
640 pa_proplist_sets(sink_input_data
.proplist
, PA_PROP_MEDIA_NAME
, "LADSPA Stream");
641 pa_proplist_sets(sink_input_data
.proplist
, PA_PROP_MEDIA_ROLE
, "routing");
642 pa_sink_input_new_data_set_sample_spec(&sink_input_data
, &ss
);
643 pa_sink_input_new_data_set_channel_map(&sink_input_data
, &map
);
645 u
->sink_input
= pa_sink_input_new(m
->core
, &sink_input_data
, PA_SINK_INPUT_DONT_MOVE
);
646 pa_sink_input_new_data_done(&sink_input_data
);
651 u
->sink_input
->parent
.process_msg
= sink_input_process_msg
;
652 u
->sink_input
->pop
= sink_input_pop_cb
;
653 u
->sink_input
->rewind
= sink_input_rewind_cb
;
654 u
->sink_input
->set_max_rewind
= sink_input_set_max_rewind_cb
;
655 u
->sink_input
->kill
= sink_input_kill_cb
;
656 u
->sink_input
->attach
= sink_input_attach_cb
;
657 u
->sink_input
->detach
= sink_input_detach_cb
;
658 u
->sink_input
->userdata
= u
;
660 pa_sink_put(u
->sink
);
661 pa_sink_input_put(u
->sink_input
);
665 pa_xfree(use_default
);
673 pa_xfree(use_default
);
680 void pa__done(pa_module
*m
) {
686 if (!(u
= m
->userdata
))
690 pa_sink_input_unlink(u
->sink_input
);
691 pa_sink_input_unref(u
->sink_input
);
695 pa_sink_unlink(u
->sink
);
696 pa_sink_unref(u
->sink
);
699 for (c
= 0; c
< u
->channels
; c
++)
701 if (u
->descriptor
->deactivate
)
702 u
->descriptor
->deactivate(u
->handle
[c
]);
703 u
->descriptor
->cleanup(u
->handle
[c
]);
706 if (u
->output
!= u
->input
)
711 pa_xfree(u
->control
);