Downgraded the priority of a message, because it's really quite uninteresting.
[pulseaudio.git] / src / modules / module-ladspa-sink.c
blobd4351b2f0cbecb571b19e552415b8f9c831b17ca
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_LOAD_ONCE(FALSE);
52 PA_MODULE_USAGE(
53 "sink_name=<name for the sink> "
54 "master=<name of sink to remap> "
55 "format=<sample format> "
56 "channels=<number of channels> "
57 "rate=<sample rate> "
58 "channel_map=<channel map> "
59 "plugin=<ladspa plugin name> "
60 "label=<ladspa plugin label> "
61 "control=<comma seperated list of input control values>");
63 struct userdata {
64 pa_core *core;
65 pa_module *module;
67 pa_sink *sink, *master;
68 pa_sink_input *sink_input;
70 const LADSPA_Descriptor *descriptor;
71 unsigned channels;
72 LADSPA_Handle handle[PA_CHANNELS_MAX];
73 LADSPA_Data *input, *output;
74 size_t block_size;
75 unsigned long input_port, output_port;
76 LADSPA_Data *control;
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;
82 pa_memchunk memchunk;
85 static const char* const valid_modargs[] = {
86 "sink_name",
87 "master",
88 "format",
89 "channels",
90 "rate",
91 "channel_map",
92 "plugin",
93 "label",
94 "control",
95 NULL
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;
102 switch (code) {
104 case PA_SINK_MESSAGE_GET_LATENCY: {
105 pa_usec_t usec = 0;
107 if (PA_MSGOBJECT(u->master)->process_msg(PA_MSGOBJECT(u->master), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
108 usec = 0;
110 *((pa_usec_t*) data) = usec + pa_bytes_to_usec(u->memchunk.length, &u->sink->sample_spec);
111 return 0;
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) {
120 struct userdata *u;
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);
128 return 0;
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;
135 switch (code) {
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 */
141 break;
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) {
149 struct userdata *u;
151 pa_sink_input_assert_ref(i);
152 pa_assert_se(u = i->userdata);
154 if (!u->memchunk.memblock) {
155 pa_memchunk tchunk;
156 float *src, *dst;
157 size_t fs;
158 unsigned n, c;
160 pa_sink_render(u->sink, length, &tchunk);
162 fs = pa_frame_size(&i->sample_spec);
163 n = tchunk.length / fs;
165 pa_assert(n > 0);
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++) {
175 unsigned j;
176 float *p, *q;
178 p = src + c;
179 q = u->input;
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);
185 q = u->output;
186 p = dst + c;
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);
203 return 0;
206 /* Called from I/O thread context */
207 static void sink_input_drop_cb(pa_sink_input *i, size_t length) {
208 struct userdata *u;
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;
219 return;
222 pa_memblock_unref(u->memchunk.memblock);
223 length -= u->memchunk.length;
224 pa_memchunk_reset(&u->memchunk);
227 if (length > 0)
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) {
233 struct userdata *u;
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) {
243 struct userdata *u;
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) {
256 struct userdata *u;
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);
267 u->sink = NULL;
269 pa_module_unload_request(u->module);
272 int pa__init(pa_module*m) {
273 struct userdata *u;
274 pa_sample_spec ss;
275 pa_channel_map map;
276 pa_modargs *ma;
277 char *t;
278 pa_sink *master;
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;
285 unsigned c;
286 pa_bool_t *use_default = NULL;
287 char *default_sink_name = NULL;
289 pa_assert(m);
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.");
295 goto fail;
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");
300 goto fail;
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");
308 goto fail;
311 if (!(plugin = pa_modargs_get_value(ma, "plugin", NULL))) {
312 pa_log("Missing LADSPA plugin name");
313 goto fail;
316 if (!(label = pa_modargs_get_value(ma, "label", NULL))) {
317 pa_log("Missing LADSPA plugin label");
318 goto fail;
321 cdata = pa_modargs_get_value(ma, "control", NULL);
323 u = pa_xnew0(struct userdata, 1);
324 u->core = m->core;
325 u->module = m;
326 m->userdata = u;
327 u->master = master;
328 pa_memchunk_reset(&u->memchunk);
330 if (!(e = getenv("LADSPA_PATH")))
331 e = 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);
338 pa_xfree(t);
340 if (!m->dl) {
341 pa_log("Failed to load LADSPA plugin: %s", lt_dlerror());
342 goto fail;
345 if (!(descriptor_func = (LADSPA_Descriptor_Function) lt_dlsym(m->dl, "ladspa_descriptor"))) {
346 pa_log("LADSPA module lacks ladspa_descriptor() symbol.");
347 goto fail;
350 for (j = 0;; j++) {
352 if (!(d = descriptor_func(j))) {
353 pa_log("Failed to find plugin label '%s' in plugin '%s'.", plugin, label);
354 goto fail;
357 if (strcmp(d->Label, label) == 0)
358 break;
361 u->descriptor = d;
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;
371 n_control = 0;
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);
379 input_port = p;
380 } else {
381 pa_log("Found audio input port on plugin we cannot handle: %s", d->PortNames[p]);
382 goto fail;
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);
389 output_port = p;
390 } else {
391 pa_log("Found audio output port on plugin we cannot handle: %s", d->PortNames[p]);
392 goto fail;
395 } else if (LADSPA_IS_PORT_INPUT(d->PortDescriptors[p]) && LADSPA_IS_PORT_CONTROL(d->PortDescriptors[p]))
396 n_control++;
397 else {
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. "
406 "Patches welcome!");
407 goto fail;
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);
415 else
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);
423 goto fail;
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);
432 goto fail;
435 if (n_control > 0) {
436 const char *state = NULL;
437 char *k;
438 unsigned long h;
440 u->control = pa_xnew(LADSPA_Data, n_control);
441 use_default = pa_xnew(pa_bool_t, n_control);
442 p = 0;
444 while ((k = pa_split(cdata, ",", &state))) {
445 float f;
447 if (*k == 0) {
448 use_default[p++] = TRUE;
449 pa_xfree(k);
450 continue;
453 if (pa_atof(k, &f) < 0) {
454 pa_log("Failed to parse control value '%s'", k);
455 pa_xfree(k);
456 goto fail;
459 pa_xfree(k);
461 if (p >= n_control) {
462 pa_log("Too many control values passed, %lu expected.", n_control);
463 goto fail;
466 use_default[p] = FALSE;
467 u->control[p++] = f;
470 if (p < n_control) {
471 pa_log("Not enough control values passed, %lu expected, %lu passed.", n_control, p);
472 goto fail;
475 h = 0;
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]))
480 continue;
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);
485 continue;
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.");
495 goto fail;
498 lower = d->PortRangeHints[p].LowerBound;
499 upper = d->PortRangeHints[p].UpperBound;
501 if (LADSPA_IS_HINT_SAMPLE_RATE(hint)) {
502 lower *= ss.rate;
503 upper *= ss.rate;
506 switch (hint & LADSPA_HINT_DEFAULT_MASK) {
508 case LADSPA_HINT_DEFAULT_MINIMUM:
509 u->control[h] = lower;
510 break;
512 case LADSPA_HINT_DEFAULT_MAXIMUM:
513 u->control[h] = upper;
514 break;
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);
519 else
520 u->control[h] = lower * 0.75 + upper * 0.25;
521 break;
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);
526 else
527 u->control[h] = lower * 0.5 + upper * 0.5;
528 break;
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);
533 else
534 u->control[h] = lower * 0.25 + upper * 0.75;
535 break;
537 case LADSPA_HINT_DEFAULT_0:
538 u->control[h] = 0;
539 break;
541 case LADSPA_HINT_DEFAULT_1:
542 u->control[h] = 1;
543 break;
545 case LADSPA_HINT_DEFAULT_100:
546 u->control[h] = 100;
547 break;
549 case LADSPA_HINT_DEFAULT_440:
550 u->control[h] = 440;
551 break;
553 default:
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]);
566 h++;
569 pa_assert(h == n_control);
572 if (d->activate)
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);
578 /* Create sink */
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.");
581 goto fail;
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));
591 pa_xfree(t);
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);
602 data.module = m;
604 if (!(u->sink_input = pa_sink_input_new(m->core, &data, PA_SINK_INPUT_DONT_MOVE)))
605 goto fail;
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);
618 pa_modargs_free(ma);
620 pa_xfree(use_default);
621 pa_xfree(default_sink_name);
623 return 0;
625 fail:
626 if (ma)
627 pa_modargs_free(ma);
629 pa_xfree(use_default);
630 pa_xfree(default_sink_name);
632 pa__done(m);
634 return -1;
637 void pa__done(pa_module*m) {
638 struct userdata *u;
639 unsigned c;
641 pa_assert(m);
643 if (!(u = m->userdata))
644 return;
646 if (u->sink_input) {
647 pa_sink_input_unlink(u->sink_input);
648 pa_sink_input_unref(u->sink_input);
651 if (u->sink) {
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++)
660 if (u->handle[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)
667 pa_xfree(u->output);
669 pa_xfree(u->input);
671 pa_xfree(u->control);
673 pa_xfree(u);