]> code.delx.au - pulseaudio/blob - src/modules/module-ladspa-sink.c
drop the PA_SOURCE_CAN_SUSPEND and PA_SINK_CAN_SUSPEND flags, since they were a bad...
[pulseaudio] / src / modules / module-ladspa-sink.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 Lennart Poettering
7
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.
12
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.
17
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 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <pulse/xmalloc.h>
29
30 #include <pulsecore/core-error.h>
31 #include <pulsecore/namereg.h>
32 #include <pulsecore/sink.h>
33 #include <pulsecore/module.h>
34 #include <pulsecore/core-util.h>
35 #include <pulsecore/modargs.h>
36 #include <pulsecore/log.h>
37 #include <pulsecore/thread.h>
38 #include <pulsecore/thread-mq.h>
39 #include <pulsecore/rtpoll.h>
40 #include <pulsecore/sample-util.h>
41
42 #include "module-ladspa-sink-symdef.h"
43 #include "ladspa.h"
44
45 PA_MODULE_AUTHOR("Lennart Poettering")
46 PA_MODULE_DESCRIPTION("Virtual LADSPA sink")
47 PA_MODULE_VERSION(PACKAGE_VERSION)
48 PA_MODULE_USAGE(
49 "sink_name=<name for the sink> "
50 "master=<name of sink to remap> "
51 "format=<sample format> "
52 "channels=<number of channels> "
53 "rate=<sample rate> "
54 "channel_map=<channel map> "
55 "plugin=<ladspa plugin name> "
56 "label=<ladspa plugin label> "
57 "control=<comma seperated list of input control values>")
58
59 struct userdata {
60 pa_core *core;
61 pa_module *module;
62
63 pa_sink *sink, *master;
64 pa_sink_input *sink_input;
65
66 const LADSPA_Descriptor *descriptor;
67 unsigned channels;
68 LADSPA_Handle handle[PA_CHANNELS_MAX];
69 LADSPA_Data *input, *output;
70 size_t block_size;
71 unsigned long input_port, output_port;
72 LADSPA_Data *control;
73
74 pa_memchunk memchunk;
75 };
76
77 static const char* const valid_modargs[] = {
78 "sink_name",
79 "master",
80 "format",
81 "channels",
82 "rate",
83 "channel_map",
84 "plugin",
85 "label",
86 "control",
87 NULL
88 };
89
90 /* Called from I/O thread context */
91 static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
92 struct userdata *u = PA_SINK(o)->userdata;
93
94 switch (code) {
95
96 case PA_SINK_MESSAGE_GET_LATENCY: {
97 pa_usec_t usec = 0;
98
99 if (PA_MSGOBJECT(u->master)->process_msg(PA_MSGOBJECT(u->master), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) < 0)
100 usec = 0;
101
102 *((pa_usec_t*) data) = usec + pa_bytes_to_usec(u->memchunk.length, &u->sink->sample_spec);
103 return 0;
104 }
105 }
106
107 return pa_sink_process_msg(o, code, data, offset, chunk);
108 }
109
110 /* Called from main context */
111 static int sink_set_state(pa_sink *s, pa_sink_state_t state) {
112 struct userdata *u;
113
114 pa_sink_assert_ref(s);
115 pa_assert_se(u = s->userdata);
116
117 if (PA_SINK_LINKED(state) && u->sink_input)
118 pa_sink_input_cork(u->sink_input, state == PA_SINK_SUSPENDED);
119
120 return 0;
121 }
122
123 /* Called from I/O thread context */
124 static int sink_input_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
125 struct userdata *u = PA_SINK_INPUT(o)->userdata;
126
127 switch (code) {
128 case PA_SINK_INPUT_MESSAGE_GET_LATENCY:
129 *((pa_usec_t*) data) = pa_bytes_to_usec(u->memchunk.length, &u->sink_input->sample_spec);
130
131 /* Fall through, the default handler will add in the extra
132 * latency added by the resampler */
133 break;
134 }
135
136 return pa_sink_input_process_msg(o, code, data, offset, chunk);
137 }
138
139 /* Called from I/O thread context */
140 static int sink_input_peek_cb(pa_sink_input *i, size_t length, pa_memchunk *chunk) {
141 struct userdata *u;
142
143 pa_sink_input_assert_ref(i);
144 pa_assert_se(u = i->userdata);
145
146 if (!u->memchunk.memblock) {
147 pa_memchunk tchunk;
148 float *src, *dst;
149 size_t fs;
150 unsigned n, c;
151
152 pa_sink_render(u->sink, length, &tchunk);
153
154 fs = pa_frame_size(&i->sample_spec);
155 n = tchunk.length / fs;
156
157 pa_assert(n > 0);
158
159 u->memchunk.memblock = pa_memblock_new(i->sink->core->mempool, tchunk.length);
160 u->memchunk.index = 0;
161 u->memchunk.length = tchunk.length;
162
163 src = (float*) ((uint8_t*) pa_memblock_acquire(tchunk.memblock) + tchunk.index);
164 dst = (float*) pa_memblock_acquire(u->memchunk.memblock);
165
166 for (c = 0; c < u->channels; c++) {
167 unsigned j;
168 float *p, *q;
169
170 p = src + c;
171 q = u->input;
172 for (j = 0; j < n; j++, p += u->channels, q++)
173 *q = CLAMP(*p, -1.0, 1.0);
174
175 u->descriptor->run(u->handle[c], n);
176
177 q = u->output;
178 p = dst + c;
179 for (j = 0; j < n; j++, q++, p += u->channels)
180 *p = CLAMP(*q, -1.0, 1.0);
181 }
182
183 pa_memblock_release(tchunk.memblock);
184 pa_memblock_release(u->memchunk.memblock);
185
186 pa_memblock_unref(tchunk.memblock);
187 }
188
189 pa_assert(u->memchunk.length > 0);
190 pa_assert(u->memchunk.memblock);
191
192 *chunk = u->memchunk;
193 pa_memblock_ref(chunk->memblock);
194
195 return 0;
196 }
197
198 /* Called from I/O thread context */
199 static void sink_input_drop_cb(pa_sink_input *i, size_t length) {
200 struct userdata *u;
201
202 pa_sink_input_assert_ref(i);
203 pa_assert_se(u = i->userdata);
204 pa_assert(length > 0);
205
206 if (u->memchunk.memblock) {
207
208 if (length < u->memchunk.length) {
209 u->memchunk.index += length;
210 u->memchunk.length -= length;
211 return;
212 }
213
214 pa_memblock_unref(u->memchunk.memblock);
215 length -= u->memchunk.length;
216 pa_memchunk_reset(&u->memchunk);
217 }
218
219 if (length > 0)
220 pa_sink_skip(u->sink, length);
221 }
222
223 /* Called from I/O thread context */
224 static void sink_input_detach_cb(pa_sink_input *i) {
225 struct userdata *u;
226
227 pa_sink_input_assert_ref(i);
228 pa_assert_se(u = i->userdata);
229
230 pa_sink_detach_within_thread(u->sink);
231 }
232
233 /* Called from I/O thread context */
234 static void sink_input_attach_cb(pa_sink_input *i) {
235 struct userdata *u;
236
237 pa_sink_input_assert_ref(i);
238 pa_assert_se(u = i->userdata);
239
240 pa_sink_set_asyncmsgq(u->sink, i->sink->asyncmsgq);
241 pa_sink_set_rtpoll(u->sink, i->sink->rtpoll);
242
243 pa_sink_attach_within_thread(u->sink);
244 }
245
246 /* Called from main context */
247 static void sink_input_kill_cb(pa_sink_input *i) {
248 struct userdata *u;
249
250 pa_sink_input_assert_ref(i);
251 pa_assert_se(u = i->userdata);
252
253 pa_sink_input_unlink(u->sink_input);
254 pa_sink_input_unref(u->sink_input);
255 u->sink_input = NULL;
256
257 pa_sink_unlink(u->sink);
258 pa_sink_unref(u->sink);
259 u->sink = NULL;
260
261 pa_module_unload_request(u->module);
262 }
263
264 int pa__init(pa_module*m) {
265 struct userdata *u;
266 pa_sample_spec ss;
267 pa_channel_map map;
268 pa_modargs *ma;
269 char *t;
270 pa_sink *master;
271 pa_sink_input_new_data data;
272 const char *plugin, *label;
273 LADSPA_Descriptor_Function descriptor_func;
274 const char *e, *cdata;
275 const LADSPA_Descriptor *d;
276 unsigned long input_port, output_port, p, j, n_control;
277 unsigned c;
278 pa_bool_t *use_default = NULL;
279 char *default_sink_name = NULL;
280
281 pa_assert(m);
282
283 pa_assert(sizeof(LADSPA_Data) == sizeof(float));
284
285 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
286 pa_log("Failed to parse module arguments.");
287 goto fail;
288 }
289
290 if (!(master = pa_namereg_get(m->core, pa_modargs_get_value(ma, "master", NULL), PA_NAMEREG_SINK, 1))) {
291 pa_log("Master sink not found");
292 goto fail;
293 }
294
295 ss = master->sample_spec;
296 ss.format = PA_SAMPLE_FLOAT32;
297 map = master->channel_map;
298 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
299 pa_log("Invalid sample format specification or channel map");
300 goto fail;
301 }
302
303 if (!(plugin = pa_modargs_get_value(ma, "plugin", NULL))) {
304 pa_log("Missing LADSPA plugin name");
305 goto fail;
306 }
307
308 if (!(label = pa_modargs_get_value(ma, "label", NULL))) {
309 pa_log("Missing LADSPA plugin label");
310 goto fail;
311 }
312
313 cdata = pa_modargs_get_value(ma, "control", NULL);
314
315 u = pa_xnew0(struct userdata, 1);
316 u->core = m->core;
317 u->module = m;
318 m->userdata = u;
319 u->master = master;
320 pa_memchunk_reset(&u->memchunk);
321
322 if (!(e = getenv("LADSPA_PATH")))
323 e = LADSPA_PATH;
324
325 /* FIXME: This is not exactly thread safe */
326 t = pa_xstrdup(lt_dlgetsearchpath());
327 lt_dlsetsearchpath(e);
328 m->dl = lt_dlopenext(plugin);
329 lt_dlsetsearchpath(t);
330 pa_xfree(t);
331
332 if (!m->dl) {
333 pa_log("Failed to load LADSPA plugin: %s", lt_dlerror());
334 goto fail;
335 }
336
337 if (!(descriptor_func = (LADSPA_Descriptor_Function) lt_dlsym(m->dl, "ladspa_descriptor"))) {
338 pa_log("LADSPA module lacks ladspa_descriptor() symbol.");
339 goto fail;
340 }
341
342 for (j = 0;; j++) {
343
344 if (!(d = descriptor_func(j))) {
345 pa_log("Failed to find plugin label '%s' in plugin '%s'.", plugin, label);
346 goto fail;
347 }
348
349 if (strcmp(d->Label, label) == 0)
350 break;
351 }
352
353 u->descriptor = d;
354
355 pa_log_debug("Module: %s", plugin);
356 pa_log_debug("Label: %s", d->Label);
357 pa_log_debug("Unique ID: %lu", d->UniqueID);
358 pa_log_debug("Name: %s", d->Name);
359 pa_log_debug("Maker: %s", d->Maker);
360 pa_log_debug("Copyright: %s", d->Copyright);
361
362 input_port = output_port = (unsigned long) -1;
363 n_control = 0;
364
365 for (p = 0; p < d->PortCount; p++) {
366
367 if (LADSPA_IS_PORT_INPUT(d->PortDescriptors[p]) && LADSPA_IS_PORT_AUDIO(d->PortDescriptors[p])) {
368
369 if (strcmp(d->PortNames[p], "Input") == 0) {
370 pa_assert(input_port == (unsigned long) -1);
371 input_port = p;
372 } else {
373 pa_log("Found audio input port on plugin we cannot handle: %s", d->PortNames[p]);
374 goto fail;
375 }
376
377 } else if (LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p]) && LADSPA_IS_PORT_AUDIO(d->PortDescriptors[p])) {
378
379 if (strcmp(d->PortNames[p], "Output") == 0) {
380 pa_assert(output_port == (unsigned long) -1);
381 output_port = p;
382 } else {
383 pa_log("Found audio output port on plugin we cannot handle: %s", d->PortNames[p]);
384 goto fail;
385 }
386
387 } else if (LADSPA_IS_PORT_INPUT(d->PortDescriptors[p]) && LADSPA_IS_PORT_CONTROL(d->PortDescriptors[p]))
388 n_control++;
389 else
390 pa_log("Cannot handle type of port %s", d->PortNames[p]);
391 }
392
393 if ((input_port == (unsigned long) -1) || (output_port == (unsigned long) -1)) {
394 pa_log("Failed to identify input and output ports. "
395 "Right now this module can only deal with plugins which provide an 'Input' and an 'Output' audio port. "
396 "Patches welcome!");
397 goto fail;
398 }
399
400 u->block_size = pa_frame_align(pa_mempool_block_size_max(m->core->mempool), &ss);
401
402 u->input = (LADSPA_Data*) pa_xnew(uint8_t, u->block_size);
403 if (LADSPA_IS_INPLACE_BROKEN(d->Properties))
404 u->output = (LADSPA_Data*) pa_xnew(uint8_t, u->block_size);
405 else
406 u->output = u->input;
407
408 u->channels = ss.channels;
409
410 for (c = 0; c < ss.channels; c++) {
411 if (!(u->handle[c] = d->instantiate(d, ss.rate))) {
412 pa_log("Failed to instantiate plugin %s with label %s for channel %i", plugin, d->Label, c);
413 goto fail;
414 }
415
416 d->connect_port(u->handle[c], input_port, u->input);
417 d->connect_port(u->handle[c], output_port, u->output);
418 }
419
420 if (!cdata && n_control > 0) {
421 pa_log("This plugin requires specification of %lu control parameters.", n_control);
422 goto fail;
423 }
424
425 if (n_control > 0) {
426 const char *state = NULL;
427 char *k;
428 unsigned long h;
429
430 u->control = pa_xnew(LADSPA_Data, n_control);
431 use_default = pa_xnew(pa_bool_t, n_control);
432 p = 0;
433
434 while ((k = pa_split(cdata, ",", &state))) {
435 float f;
436
437 if (*k == 0) {
438 use_default[p++] = TRUE;
439 pa_xfree(k);
440 continue;
441 }
442
443 if (pa_atof(k, &f) < 0) {
444 pa_log("Failed to parse control value '%s'", k);
445 pa_xfree(k);
446 goto fail;
447 }
448
449 pa_xfree(k);
450
451 if (p >= n_control) {
452 pa_log("Too many control values passed, %lu expected.", n_control);
453 goto fail;
454 }
455
456 use_default[p] = FALSE;
457 u->control[p++] = f;
458 }
459
460 if (p < n_control) {
461 pa_log("Not enough control values passed, %lu expected, %lu passed.", n_control, p);
462 goto fail;
463 }
464
465 h = 0;
466 for (p = 0; p < d->PortCount; p++) {
467 LADSPA_PortRangeHintDescriptor hint = d->PortRangeHints[p].HintDescriptor;
468
469 if (!LADSPA_IS_PORT_INPUT(d->PortDescriptors[p]) || !LADSPA_IS_PORT_CONTROL(d->PortDescriptors[p]))
470 continue;
471
472 pa_assert(h < n_control);
473
474 if (use_default[h]) {
475 LADSPA_Data lower, upper;
476
477 if (!LADSPA_IS_HINT_HAS_DEFAULT(hint)) {
478 pa_log("Control port value left empty but plugin defines no default.");
479 goto fail;
480 }
481
482 lower = d->PortRangeHints[p].LowerBound;
483 upper = d->PortRangeHints[p].UpperBound;
484
485 if (LADSPA_IS_HINT_SAMPLE_RATE(hint)) {
486 lower *= ss.rate;
487 upper *= ss.rate;
488 }
489
490 switch (hint & LADSPA_HINT_DEFAULT_MASK) {
491
492 case LADSPA_HINT_DEFAULT_MINIMUM:
493 u->control[h] = lower;
494 break;
495
496 case LADSPA_HINT_DEFAULT_MAXIMUM:
497 u->control[h] = upper;
498 break;
499
500 case LADSPA_HINT_DEFAULT_LOW:
501 if (LADSPA_IS_HINT_LOGARITHMIC(hint))
502 u->control[h] = exp(log(lower) * 0.75 + log(upper) * 0.25);
503 else
504 u->control[h] = lower * 0.75 + upper * 0.25;
505 break;
506
507 case LADSPA_HINT_DEFAULT_MIDDLE:
508 if (LADSPA_IS_HINT_LOGARITHMIC(hint))
509 u->control[h] = exp(log(lower) * 0.5 + log(upper) * 0.5);
510 else
511 u->control[h] = lower * 0.5 + upper * 0.5;
512 break;
513
514 case LADSPA_HINT_DEFAULT_HIGH:
515 if (LADSPA_IS_HINT_LOGARITHMIC(hint))
516 u->control[h] = exp(log(lower) * 0.25 + log(upper) * 0.75);
517 else
518 u->control[h] = lower * 0.25 + upper * 0.75;
519 break;
520
521 case LADSPA_HINT_DEFAULT_0:
522 u->control[h] = 0;
523 break;
524
525 case LADSPA_HINT_DEFAULT_1:
526 u->control[h] = 1;
527 break;
528
529 case LADSPA_HINT_DEFAULT_100:
530 u->control[h] = 100;
531 break;
532
533 case LADSPA_HINT_DEFAULT_440:
534 u->control[h] = 440;
535 break;
536
537 default:
538 pa_assert_not_reached();
539 }
540 }
541
542 if (LADSPA_IS_HINT_INTEGER(hint))
543 u->control[h] = roundf(u->control[h]);
544
545 pa_log_debug("Binding %f to port %s", u->control[h], d->PortNames[p]);
546
547 for (c = 0; c < ss.channels; c++)
548 d->connect_port(u->handle[c], p, &u->control[h]);
549
550 h++;
551 }
552
553 pa_assert(h == n_control);
554 }
555
556 if (d->activate)
557 for (c = 0; c < u->channels; c++)
558 d->activate(u->handle[c]);
559
560 default_sink_name = pa_sprintf_malloc("%s.ladspa", master->name);
561
562 /* Create sink */
563 if (!(u->sink = pa_sink_new(m->core, __FILE__, pa_modargs_get_value(ma, "sink_name", default_sink_name), 0, &ss, &map))) {
564 pa_log("Failed to create sink.");
565 goto fail;
566 }
567
568 u->sink->parent.process_msg = sink_process_msg;
569 u->sink->set_state = sink_set_state;
570 u->sink->userdata = u;
571 u->sink->flags = PA_SINK_LATENCY;
572
573 pa_sink_set_module(u->sink, m);
574 pa_sink_set_description(u->sink, t = pa_sprintf_malloc("LADSPA plugin '%s' on '%s'", label, master->description));
575 pa_xfree(t);
576 pa_sink_set_asyncmsgq(u->sink, master->asyncmsgq);
577 pa_sink_set_rtpoll(u->sink, master->rtpoll);
578
579 /* Create sink input */
580 pa_sink_input_new_data_init(&data);
581 data.sink = u->master;
582 data.driver = __FILE__;
583 data.name = "LADSPA Stream";
584 pa_sink_input_new_data_set_sample_spec(&data, &ss);
585 pa_sink_input_new_data_set_channel_map(&data, &map);
586 data.module = m;
587
588 if (!(u->sink_input = pa_sink_input_new(m->core, &data, PA_SINK_INPUT_DONT_MOVE)))
589 goto fail;
590
591 u->sink_input->parent.process_msg = sink_input_process_msg;
592 u->sink_input->peek = sink_input_peek_cb;
593 u->sink_input->drop = sink_input_drop_cb;
594 u->sink_input->kill = sink_input_kill_cb;
595 u->sink_input->attach = sink_input_attach_cb;
596 u->sink_input->detach = sink_input_detach_cb;
597 u->sink_input->userdata = u;
598
599 pa_sink_put(u->sink);
600 pa_sink_input_put(u->sink_input);
601
602 pa_modargs_free(ma);
603
604 pa_xfree(use_default);
605 pa_xfree(default_sink_name);
606
607 return 0;
608
609 fail:
610 if (ma)
611 pa_modargs_free(ma);
612
613 pa_xfree(use_default);
614 pa_xfree(default_sink_name);
615
616 pa__done(m);
617
618 return -1;
619 }
620
621 void pa__done(pa_module*m) {
622 struct userdata *u;
623 unsigned c;
624
625 pa_assert(m);
626
627 if (!(u = m->userdata))
628 return;
629
630 if (u->sink_input) {
631 pa_sink_input_unlink(u->sink_input);
632 pa_sink_input_unref(u->sink_input);
633 }
634
635 if (u->sink) {
636 pa_sink_unlink(u->sink);
637 pa_sink_unref(u->sink);
638 }
639
640 if (u->memchunk.memblock)
641 pa_memblock_unref(u->memchunk.memblock);
642
643 for (c = 0; c < u->channels; c++)
644 if (u->handle[c]) {
645 if (u->descriptor->deactivate)
646 u->descriptor->deactivate(u->handle[c]);
647 u->descriptor->cleanup(u->handle[c]);
648 }
649
650 if (u->output != u->input)
651 pa_xfree(u->output);
652
653 pa_xfree(u->input);
654
655 pa_xfree(u->control);
656
657 pa_xfree(u);
658 }