add new function pa_yes_no()
[pulseaudio.git] / src / modules / module-ladspa-sink.c
blob0265d9713eab4d6281fcf55b1cf4474a0e8194c4
1 /* $Id$ */
3 /***
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
21 USA.
22 ***/
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. */
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
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"
46 #include "ladspa.h"
48 PA_MODULE_AUTHOR("Lennart Poettering")
49 PA_MODULE_DESCRIPTION("Virtual LADSPA sink")
50 PA_MODULE_VERSION(PACKAGE_VERSION)
51 PA_MODULE_USAGE(
52 "sink_name=<name for the sink> "
53 "master=<name of sink to remap> "
54 "format=<sample format> "
55 "channels=<number of channels> "
56 "rate=<sample rate> "
57 "channel_map=<channel map> "
58 "plugin=<ladspa plugin name> "
59 "label=<ladspa plugin label> "
60 "control=<comma seperated list of input control values>")
62 struct userdata {
63 pa_core *core;
64 pa_module *module;
66 pa_sink *sink, *master;
67 pa_sink_input *sink_input;
69 const LADSPA_Descriptor *descriptor;
70 unsigned channels;
71 LADSPA_Handle handle[PA_CHANNELS_MAX];
72 LADSPA_Data *input, *output;
73 size_t block_size;
74 unsigned long input_port, output_port;
75 LADSPA_Data *control;
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;
81 pa_memchunk memchunk;
84 static const char* const valid_modargs[] = {
85 "sink_name",
86 "master",
87 "format",
88 "channels",
89 "rate",
90 "channel_map",
91 "plugin",
92 "label",
93 "control",
94 NULL
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;
101 switch (code) {
103 case PA_SINK_MESSAGE_GET_LATENCY: {
104 pa_usec_t usec = 0;
106 if (PA_MSGOBJECT(u->master)->process_msg(PA_MSGOBJECT(u->master), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
107 usec = 0;
109 *((pa_usec_t*) data) = usec + pa_bytes_to_usec(u->memchunk.length, &u->sink->sample_spec);
110 return 0;
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) {
119 struct userdata *u;
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);
127 return 0;
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;
134 switch (code) {
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 */
140 break;
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) {
148 struct userdata *u;
150 pa_sink_input_assert_ref(i);
151 pa_assert_se(u = i->userdata);
153 if (!u->memchunk.memblock) {
154 pa_memchunk tchunk;
155 float *src, *dst;
156 size_t fs;
157 unsigned n, c;
159 pa_sink_render(u->sink, length, &tchunk);
161 fs = pa_frame_size(&i->sample_spec);
162 n = tchunk.length / fs;
164 pa_assert(n > 0);
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++) {
174 unsigned j;
175 float *p, *q;
177 p = src + c;
178 q = u->input;
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);
184 q = u->output;
185 p = dst + c;
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);
202 return 0;
205 /* Called from I/O thread context */
206 static void sink_input_drop_cb(pa_sink_input *i, size_t length) {
207 struct userdata *u;
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;
218 return;
221 pa_memblock_unref(u->memchunk.memblock);
222 length -= u->memchunk.length;
223 pa_memchunk_reset(&u->memchunk);
226 if (length > 0)
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) {
232 struct userdata *u;
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) {
242 struct userdata *u;
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) {
255 struct userdata *u;
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);
266 u->sink = NULL;
268 pa_module_unload_request(u->module);
271 int pa__init(pa_module*m) {
272 struct userdata *u;
273 pa_sample_spec ss;
274 pa_channel_map map;
275 pa_modargs *ma;
276 char *t;
277 pa_sink *master;
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;
284 unsigned c;
285 pa_bool_t *use_default = NULL;
286 char *default_sink_name = NULL;
288 pa_assert(m);
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.");
294 goto fail;
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");
299 goto fail;
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");
307 goto fail;
310 if (!(plugin = pa_modargs_get_value(ma, "plugin", NULL))) {
311 pa_log("Missing LADSPA plugin name");
312 goto fail;
315 if (!(label = pa_modargs_get_value(ma, "label", NULL))) {
316 pa_log("Missing LADSPA plugin label");
317 goto fail;
320 cdata = pa_modargs_get_value(ma, "control", NULL);
322 u = pa_xnew0(struct userdata, 1);
323 u->core = m->core;
324 u->module = m;
325 m->userdata = u;
326 u->master = master;
327 pa_memchunk_reset(&u->memchunk);
329 if (!(e = getenv("LADSPA_PATH")))
330 e = 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);
337 pa_xfree(t);
339 if (!m->dl) {
340 pa_log("Failed to load LADSPA plugin: %s", lt_dlerror());
341 goto fail;
344 if (!(descriptor_func = (LADSPA_Descriptor_Function) lt_dlsym(m->dl, "ladspa_descriptor"))) {
345 pa_log("LADSPA module lacks ladspa_descriptor() symbol.");
346 goto fail;
349 for (j = 0;; j++) {
351 if (!(d = descriptor_func(j))) {
352 pa_log("Failed to find plugin label '%s' in plugin '%s'.", plugin, label);
353 goto fail;
356 if (strcmp(d->Label, label) == 0)
357 break;
360 u->descriptor = d;
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;
370 n_control = 0;
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);
378 input_port = p;
379 } else {
380 pa_log("Found audio input port on plugin we cannot handle: %s", d->PortNames[p]);
381 goto fail;
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);
388 output_port = p;
389 } else {
390 pa_log("Found audio output port on plugin we cannot handle: %s", d->PortNames[p]);
391 goto fail;
394 } else if (LADSPA_IS_PORT_INPUT(d->PortDescriptors[p]) && LADSPA_IS_PORT_CONTROL(d->PortDescriptors[p]))
395 n_control++;
396 else {
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. "
405 "Patches welcome!");
406 goto fail;
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);
414 else
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);
422 goto fail;
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);
431 goto fail;
434 if (n_control > 0) {
435 const char *state = NULL;
436 char *k;
437 unsigned long h;
439 u->control = pa_xnew(LADSPA_Data, n_control);
440 use_default = pa_xnew(pa_bool_t, n_control);
441 p = 0;
443 while ((k = pa_split(cdata, ",", &state))) {
444 float f;
446 if (*k == 0) {
447 use_default[p++] = TRUE;
448 pa_xfree(k);
449 continue;
452 if (pa_atof(k, &f) < 0) {
453 pa_log("Failed to parse control value '%s'", k);
454 pa_xfree(k);
455 goto fail;
458 pa_xfree(k);
460 if (p >= n_control) {
461 pa_log("Too many control values passed, %lu expected.", n_control);
462 goto fail;
465 use_default[p] = FALSE;
466 u->control[p++] = f;
469 if (p < n_control) {
470 pa_log("Not enough control values passed, %lu expected, %lu passed.", n_control, p);
471 goto fail;
474 h = 0;
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]))
479 continue;
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);
484 continue;
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.");
494 goto fail;
497 lower = d->PortRangeHints[p].LowerBound;
498 upper = d->PortRangeHints[p].UpperBound;
500 if (LADSPA_IS_HINT_SAMPLE_RATE(hint)) {
501 lower *= ss.rate;
502 upper *= ss.rate;
505 switch (hint & LADSPA_HINT_DEFAULT_MASK) {
507 case LADSPA_HINT_DEFAULT_MINIMUM:
508 u->control[h] = lower;
509 break;
511 case LADSPA_HINT_DEFAULT_MAXIMUM:
512 u->control[h] = upper;
513 break;
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);
518 else
519 u->control[h] = lower * 0.75 + upper * 0.25;
520 break;
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);
525 else
526 u->control[h] = lower * 0.5 + upper * 0.5;
527 break;
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);
532 else
533 u->control[h] = lower * 0.25 + upper * 0.75;
534 break;
536 case LADSPA_HINT_DEFAULT_0:
537 u->control[h] = 0;
538 break;
540 case LADSPA_HINT_DEFAULT_1:
541 u->control[h] = 1;
542 break;
544 case LADSPA_HINT_DEFAULT_100:
545 u->control[h] = 100;
546 break;
548 case LADSPA_HINT_DEFAULT_440:
549 u->control[h] = 440;
550 break;
552 default:
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]);
565 h++;
568 pa_assert(h == n_control);
571 if (d->activate)
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);
577 /* Create sink */
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.");
580 goto fail;
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));
590 pa_xfree(t);
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);
601 data.module = m;
603 if (!(u->sink_input = pa_sink_input_new(m->core, &data, PA_SINK_INPUT_DONT_MOVE)))
604 goto fail;
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);
617 pa_modargs_free(ma);
619 pa_xfree(use_default);
620 pa_xfree(default_sink_name);
622 return 0;
624 fail:
625 if (ma)
626 pa_modargs_free(ma);
628 pa_xfree(use_default);
629 pa_xfree(default_sink_name);
631 pa__done(m);
633 return -1;
636 void pa__done(pa_module*m) {
637 struct userdata *u;
638 unsigned c;
640 pa_assert(m);
642 if (!(u = m->userdata))
643 return;
645 if (u->sink_input) {
646 pa_sink_input_unlink(u->sink_input);
647 pa_sink_input_unref(u->sink_input);
650 if (u->sink) {
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++)
659 if (u->handle[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)
666 pa_xfree(u->output);
668 pa_xfree(u->input);
670 pa_xfree(u->control);
672 pa_xfree(u);