]> code.delx.au - pulseaudio/blob - src/modules/module-ladspa-sink.c
Merge commit 'origin/master-tx'
[pulseaudio] / src / modules / module-ladspa-sink.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2008 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 /* TODO: Some plugins cause latency, and some even report it by using a control
23 out port. We don't currently use the latency information. */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <pulse/xmalloc.h>
30 #include <pulse/i18n.h>
31
32 #include <pulsecore/core-error.h>
33 #include <pulsecore/namereg.h>
34 #include <pulsecore/sink.h>
35 #include <pulsecore/module.h>
36 #include <pulsecore/core-util.h>
37 #include <pulsecore/modargs.h>
38 #include <pulsecore/log.h>
39 #include <pulsecore/thread.h>
40 #include <pulsecore/thread-mq.h>
41 #include <pulsecore/rtpoll.h>
42 #include <pulsecore/sample-util.h>
43 #include <pulsecore/ltdl-helper.h>
44
45 #include "module-ladspa-sink-symdef.h"
46 #include "ladspa.h"
47
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 "sink_properties=<properties for the sink> "
55 "master=<name of sink to filter> "
56 "format=<sample format> "
57 "rate=<sample rate> "
58 "channels=<number of channels> "
59 "channel_map=<channel map> "
60 "plugin=<ladspa plugin name> "
61 "label=<ladspa plugin label> "
62 "control=<comma seperated list of input control values>"));
63
64 #define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
65
66 struct userdata {
67 pa_module *module;
68
69 pa_sink *sink;
70 pa_sink_input *sink_input;
71
72 const LADSPA_Descriptor *descriptor;
73 unsigned channels;
74 LADSPA_Handle handle[PA_CHANNELS_MAX];
75 LADSPA_Data *input, *output;
76 size_t block_size;
77 unsigned long input_port, output_port;
78 LADSPA_Data *control;
79
80 /* This is a dummy buffer. Every port must be connected, but we don't care
81 about control out ports. We connect them all to this single buffer. */
82 LADSPA_Data control_out;
83
84 pa_memblockq *memblockq;
85
86 pa_bool_t auto_desc;
87 };
88
89 static const char* const valid_modargs[] = {
90 "sink_name",
91 "sink_properties",
92 "master",
93 "format",
94 "rate",
95 "channels",
96 "channel_map",
97 "plugin",
98 "label",
99 "control",
100 NULL
101 };
102
103 /* Called from I/O thread context */
104 static int sink_process_msg_cb(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
105 struct userdata *u = PA_SINK(o)->userdata;
106
107 switch (code) {
108
109 case PA_SINK_MESSAGE_GET_LATENCY:
110
111 /* The sink is _put() before the sink input is, so let's
112 * make sure we don't access it in that time. Also, the
113 * sink input is first shut down, the sink second. */
114 if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
115 !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state)) {
116 *((pa_usec_t*) data) = 0;
117 return 0;
118 }
119
120 *((pa_usec_t*) data) =
121
122 /* Get the latency of the master sink */
123 pa_sink_get_latency_within_thread(u->sink_input->sink) +
124
125 /* Add the latency internal to our sink input on top */
126 pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->sink_input->sink->sample_spec);
127
128 return 0;
129 }
130
131 return pa_sink_process_msg(o, code, data, offset, chunk);
132 }
133
134 /* Called from main context */
135 static int sink_set_state_cb(pa_sink *s, pa_sink_state_t state) {
136 struct userdata *u;
137
138 pa_sink_assert_ref(s);
139 pa_assert_se(u = s->userdata);
140
141 if (!PA_SINK_IS_LINKED(state) ||
142 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
143 return 0;
144
145 pa_sink_input_cork(u->sink_input, state == PA_SINK_SUSPENDED);
146 return 0;
147 }
148
149 /* Called from I/O thread context */
150 static void sink_request_rewind_cb(pa_sink *s) {
151 struct userdata *u;
152
153 pa_sink_assert_ref(s);
154 pa_assert_se(u = s->userdata);
155
156 if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
157 !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
158 return;
159
160 /* Just hand this one over to the master sink */
161 pa_sink_input_request_rewind(u->sink_input, s->thread_info.rewind_nbytes + pa_memblockq_get_length(u->memblockq), TRUE, FALSE, FALSE);
162 }
163
164 /* Called from I/O thread context */
165 static void sink_update_requested_latency_cb(pa_sink *s) {
166 struct userdata *u;
167
168 pa_sink_assert_ref(s);
169 pa_assert_se(u = s->userdata);
170
171 if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
172 !PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state))
173 return;
174
175 /* Just hand this one over to the master sink */
176 pa_sink_input_set_requested_latency_within_thread(
177 u->sink_input,
178 pa_sink_get_requested_latency_within_thread(s));
179 }
180
181 /* Called from main context */
182 static void sink_set_volume_cb(pa_sink *s) {
183 struct userdata *u;
184
185 pa_sink_assert_ref(s);
186 pa_assert_se(u = s->userdata);
187
188 if (!PA_SINK_IS_LINKED(pa_sink_get_state(s)) ||
189 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
190 return;
191
192 pa_sink_input_set_volume(u->sink_input, &s->real_volume, s->save_volume, TRUE);
193 }
194
195 /* Called from main context */
196 static void sink_set_mute_cb(pa_sink *s) {
197 struct userdata *u;
198
199 pa_sink_assert_ref(s);
200 pa_assert_se(u = s->userdata);
201
202 if (!PA_SINK_IS_LINKED(pa_sink_get_state(s)) ||
203 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u->sink_input)))
204 return;
205
206 pa_sink_input_set_mute(u->sink_input, s->muted, s->save_muted);
207 }
208
209 /* Called from I/O thread context */
210 static int sink_input_pop_cb(pa_sink_input *i, size_t nbytes, pa_memchunk *chunk) {
211 struct userdata *u;
212 float *src, *dst;
213 size_t fs;
214 unsigned n, c;
215 pa_memchunk tchunk;
216
217 pa_sink_input_assert_ref(i);
218 pa_assert(chunk);
219 pa_assert_se(u = i->userdata);
220
221 /* Hmm, process any rewind request that might be queued up */
222 pa_sink_process_rewind(u->sink, 0);
223
224 while (pa_memblockq_peek(u->memblockq, &tchunk) < 0) {
225 pa_memchunk nchunk;
226
227 pa_sink_render(u->sink, nbytes, &nchunk);
228 pa_memblockq_push(u->memblockq, &nchunk);
229 pa_memblock_unref(nchunk.memblock);
230 }
231
232 tchunk.length = PA_MIN(nbytes, tchunk.length);
233 pa_assert(tchunk.length > 0);
234
235 fs = pa_frame_size(&i->sample_spec);
236 n = (unsigned) (PA_MIN(tchunk.length, u->block_size) / fs);
237
238 pa_assert(n > 0);
239
240 chunk->index = 0;
241 chunk->length = n*fs;
242 chunk->memblock = pa_memblock_new(i->sink->core->mempool, chunk->length);
243
244 pa_memblockq_drop(u->memblockq, chunk->length);
245
246 src = (float*) ((uint8_t*) pa_memblock_acquire(tchunk.memblock) + tchunk.index);
247 dst = (float*) pa_memblock_acquire(chunk->memblock);
248
249 for (c = 0; c < u->channels; c++) {
250 pa_sample_clamp(PA_SAMPLE_FLOAT32NE, u->input, sizeof(float), src+c, u->channels*sizeof(float), n);
251 u->descriptor->run(u->handle[c], n);
252 pa_sample_clamp(PA_SAMPLE_FLOAT32NE, dst+c, u->channels*sizeof(float), u->output, sizeof(float), n);
253 }
254
255 pa_memblock_release(tchunk.memblock);
256 pa_memblock_release(chunk->memblock);
257
258 pa_memblock_unref(tchunk.memblock);
259
260 return 0;
261 }
262
263 /* Called from I/O thread context */
264 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
265 struct userdata *u;
266 size_t amount = 0;
267
268 pa_sink_input_assert_ref(i);
269 pa_assert_se(u = i->userdata);
270
271 if (u->sink->thread_info.rewind_nbytes > 0) {
272 size_t max_rewrite;
273
274 max_rewrite = nbytes + pa_memblockq_get_length(u->memblockq);
275 amount = PA_MIN(u->sink->thread_info.rewind_nbytes, max_rewrite);
276 u->sink->thread_info.rewind_nbytes = 0;
277
278 if (amount > 0) {
279 unsigned c;
280
281 pa_memblockq_seek(u->memblockq, - (int64_t) amount, PA_SEEK_RELATIVE, TRUE);
282
283 pa_log_debug("Resetting plugin");
284
285 /* Reset the plugin */
286 if (u->descriptor->deactivate)
287 for (c = 0; c < u->channels; c++)
288 u->descriptor->deactivate(u->handle[c]);
289 if (u->descriptor->activate)
290 for (c = 0; c < u->channels; c++)
291 u->descriptor->activate(u->handle[c]);
292 }
293 }
294
295 pa_sink_process_rewind(u->sink, amount);
296 pa_memblockq_rewind(u->memblockq, nbytes);
297 }
298
299 /* Called from I/O thread context */
300 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
301 struct userdata *u;
302
303 pa_sink_input_assert_ref(i);
304 pa_assert_se(u = i->userdata);
305
306 pa_memblockq_set_maxrewind(u->memblockq, nbytes);
307 pa_sink_set_max_rewind_within_thread(u->sink, nbytes);
308 }
309
310 /* Called from I/O thread context */
311 static void sink_input_update_max_request_cb(pa_sink_input *i, size_t nbytes) {
312 struct userdata *u;
313
314 pa_sink_input_assert_ref(i);
315 pa_assert_se(u = i->userdata);
316
317 pa_sink_set_max_request_within_thread(u->sink, nbytes);
318 }
319
320 /* Called from I/O thread context */
321 static void sink_input_update_sink_latency_range_cb(pa_sink_input *i) {
322 struct userdata *u;
323
324 pa_sink_input_assert_ref(i);
325 pa_assert_se(u = i->userdata);
326
327 pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
328 }
329
330 /* Called from I/O thread context */
331 static void sink_input_update_sink_fixed_latency_cb(pa_sink_input *i) {
332 struct userdata *u;
333
334 pa_sink_input_assert_ref(i);
335 pa_assert_se(u = i->userdata);
336
337 pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
338 }
339
340 /* Called from I/O thread context */
341 static void sink_input_detach_cb(pa_sink_input *i) {
342 struct userdata *u;
343
344 pa_sink_input_assert_ref(i);
345 pa_assert_se(u = i->userdata);
346
347 pa_sink_detach_within_thread(u->sink);
348
349 pa_sink_set_rtpoll(u->sink, NULL);
350 }
351
352 /* Called from I/O thread context */
353 static void sink_input_attach_cb(pa_sink_input *i) {
354 struct userdata *u;
355
356 pa_sink_input_assert_ref(i);
357 pa_assert_se(u = i->userdata);
358
359 pa_sink_set_rtpoll(u->sink, i->sink->thread_info.rtpoll);
360 pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
361 pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
362 pa_sink_set_max_request_within_thread(u->sink, pa_sink_input_get_max_request(i));
363 pa_sink_set_max_rewind_within_thread(u->sink, pa_sink_input_get_max_rewind(i));
364
365 pa_sink_attach_within_thread(u->sink);
366 }
367
368 /* Called from main context */
369 static void sink_input_kill_cb(pa_sink_input *i) {
370 struct userdata *u;
371
372 pa_sink_input_assert_ref(i);
373 pa_assert_se(u = i->userdata);
374
375 /* The order here matters! We first kill the sink input, followed
376 * by the sink. That means the sink callbacks must be protected
377 * against an unconnected sink input! */
378 pa_sink_input_unlink(u->sink_input);
379 pa_sink_unlink(u->sink);
380
381 pa_sink_input_unref(u->sink_input);
382 u->sink_input = NULL;
383
384 pa_sink_unref(u->sink);
385 u->sink = NULL;
386
387 pa_module_unload_request(u->module, TRUE);
388 }
389
390 /* Called from IO thread context */
391 static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
392 struct userdata *u;
393
394 pa_sink_input_assert_ref(i);
395 pa_assert_se(u = i->userdata);
396
397 /* If we are added for the first time, ask for a rewinding so that
398 * we are heard right-away. */
399 if (PA_SINK_INPUT_IS_LINKED(state) &&
400 i->thread_info.state == PA_SINK_INPUT_INIT) {
401 pa_log_debug("Requesting rewind due to state change.");
402 pa_sink_input_request_rewind(i, 0, FALSE, TRUE, TRUE);
403 }
404 }
405
406 /* Called from main context */
407 static pa_bool_t sink_input_may_move_to_cb(pa_sink_input *i, pa_sink *dest) {
408 struct userdata *u;
409
410 pa_sink_input_assert_ref(i);
411 pa_assert_se(u = i->userdata);
412
413 return u->sink != dest;
414 }
415
416 /* Called from main context */
417 static void sink_input_moving_cb(pa_sink_input *i, pa_sink *dest) {
418 struct userdata *u;
419
420 pa_sink_input_assert_ref(i);
421 pa_assert_se(u = i->userdata);
422
423 if (dest) {
424 pa_sink_set_asyncmsgq(u->sink, dest->asyncmsgq);
425 pa_sink_update_flags(u->sink, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY, dest->flags);
426 } else
427 pa_sink_set_asyncmsgq(u->sink, NULL);
428
429 if (u->auto_desc && dest) {
430 const char *z;
431 pa_proplist *pl;
432
433 pl = pa_proplist_new();
434 z = pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_DESCRIPTION);
435 pa_proplist_setf(pl, PA_PROP_DEVICE_DESCRIPTION, "LADSPA Plugin %s on %s",
436 pa_proplist_gets(u->sink->proplist, "device.ladspa.name"), z ? z : dest->name);
437
438 pa_sink_update_proplist(u->sink, PA_UPDATE_REPLACE, pl);
439 pa_proplist_free(pl);
440 }
441 }
442
443 /* Called from main context */
444 static void sink_input_volume_changed_cb(pa_sink_input *i) {
445 struct userdata *u;
446
447 pa_sink_input_assert_ref(i);
448 pa_assert_se(u = i->userdata);
449
450 pa_sink_volume_changed(u->sink, &i->volume);
451 }
452
453 /* Called from main context */
454 static void sink_input_mute_changed_cb(pa_sink_input *i) {
455 struct userdata *u;
456
457 pa_sink_input_assert_ref(i);
458 pa_assert_se(u = i->userdata);
459
460 pa_sink_mute_changed(u->sink, i->muted);
461 }
462
463 int pa__init(pa_module*m) {
464 struct userdata *u;
465 pa_sample_spec ss;
466 pa_channel_map map;
467 pa_modargs *ma;
468 char *t;
469 pa_sink *master;
470 pa_sink_input_new_data sink_input_data;
471 pa_sink_new_data sink_data;
472 const char *plugin, *label;
473 LADSPA_Descriptor_Function descriptor_func;
474 const char *e, *cdata;
475 const LADSPA_Descriptor *d;
476 unsigned long input_port, output_port, p, j, n_control;
477 unsigned c;
478 pa_bool_t *use_default = NULL;
479
480 pa_assert(m);
481
482 pa_assert_cc(sizeof(LADSPA_Data) == sizeof(float));
483
484 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
485 pa_log("Failed to parse module arguments.");
486 goto fail;
487 }
488
489 if (!(master = pa_namereg_get(m->core, pa_modargs_get_value(ma, "master", NULL), PA_NAMEREG_SINK))) {
490 pa_log("Master sink not found");
491 goto fail;
492 }
493
494 ss = master->sample_spec;
495 ss.format = PA_SAMPLE_FLOAT32;
496 map = master->channel_map;
497 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
498 pa_log("Invalid sample format specification or channel map");
499 goto fail;
500 }
501
502 if (!(plugin = pa_modargs_get_value(ma, "plugin", NULL))) {
503 pa_log("Missing LADSPA plugin name");
504 goto fail;
505 }
506
507 if (!(label = pa_modargs_get_value(ma, "label", NULL))) {
508 pa_log("Missing LADSPA plugin label");
509 goto fail;
510 }
511
512 cdata = pa_modargs_get_value(ma, "control", NULL);
513
514 u = pa_xnew0(struct userdata, 1);
515 u->module = m;
516 m->userdata = u;
517 u->memblockq = pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH, 0, pa_frame_size(&ss), 1, 1, 0, NULL);
518
519 if (!(e = getenv("LADSPA_PATH")))
520 e = LADSPA_PATH;
521
522 /* FIXME: This is not exactly thread safe */
523 t = pa_xstrdup(lt_dlgetsearchpath());
524 lt_dlsetsearchpath(e);
525 m->dl = lt_dlopenext(plugin);
526 lt_dlsetsearchpath(t);
527 pa_xfree(t);
528
529 if (!m->dl) {
530 pa_log("Failed to load LADSPA plugin: %s", lt_dlerror());
531 goto fail;
532 }
533
534 if (!(descriptor_func = (LADSPA_Descriptor_Function) pa_load_sym(m->dl, NULL, "ladspa_descriptor"))) {
535 pa_log("LADSPA module lacks ladspa_descriptor() symbol.");
536 goto fail;
537 }
538
539 for (j = 0;; j++) {
540
541 if (!(d = descriptor_func(j))) {
542 pa_log("Failed to find plugin label '%s' in plugin '%s'.", label, plugin);
543 goto fail;
544 }
545
546 if (strcmp(d->Label, label) == 0)
547 break;
548 }
549
550 u->descriptor = d;
551
552 pa_log_debug("Module: %s", plugin);
553 pa_log_debug("Label: %s", d->Label);
554 pa_log_debug("Unique ID: %lu", d->UniqueID);
555 pa_log_debug("Name: %s", d->Name);
556 pa_log_debug("Maker: %s", d->Maker);
557 pa_log_debug("Copyright: %s", d->Copyright);
558
559 input_port = output_port = (unsigned long) -1;
560 n_control = 0;
561
562 for (p = 0; p < d->PortCount; p++) {
563
564 if (LADSPA_IS_PORT_INPUT(d->PortDescriptors[p]) && LADSPA_IS_PORT_AUDIO(d->PortDescriptors[p])) {
565
566 if (strcmp(d->PortNames[p], "Input") == 0) {
567 pa_assert(input_port == (unsigned long) -1);
568 input_port = p;
569 } else {
570 pa_log("Found audio input port on plugin we cannot handle: %s", d->PortNames[p]);
571 goto fail;
572 }
573
574 } else if (LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p]) && LADSPA_IS_PORT_AUDIO(d->PortDescriptors[p])) {
575
576 if (strcmp(d->PortNames[p], "Output") == 0) {
577 pa_assert(output_port == (unsigned long) -1);
578 output_port = p;
579 } else {
580 pa_log("Found audio output port on plugin we cannot handle: %s", d->PortNames[p]);
581 goto fail;
582 }
583
584 } else if (LADSPA_IS_PORT_INPUT(d->PortDescriptors[p]) && LADSPA_IS_PORT_CONTROL(d->PortDescriptors[p]))
585 n_control++;
586 else {
587 pa_assert(LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p]) && LADSPA_IS_PORT_CONTROL(d->PortDescriptors[p]));
588 pa_log_debug("Ignored control output port \"%s\".", d->PortNames[p]);
589 }
590 }
591
592 if ((input_port == (unsigned long) -1) || (output_port == (unsigned long) -1)) {
593 pa_log("Failed to identify input and output ports. "
594 "Right now this module can only deal with plugins which provide an 'Input' and an 'Output' audio port. "
595 "Patches welcome!");
596 goto fail;
597 }
598
599 u->block_size = pa_frame_align(pa_mempool_block_size_max(m->core->mempool), &ss);
600
601 u->input = (LADSPA_Data*) pa_xnew(uint8_t, (unsigned) u->block_size);
602 if (LADSPA_IS_INPLACE_BROKEN(d->Properties))
603 u->output = (LADSPA_Data*) pa_xnew(uint8_t, (unsigned) u->block_size);
604 else
605 u->output = u->input;
606
607 u->channels = ss.channels;
608
609 for (c = 0; c < ss.channels; c++) {
610 if (!(u->handle[c] = d->instantiate(d, ss.rate))) {
611 pa_log("Failed to instantiate plugin %s with label %s for channel %i", plugin, d->Label, c);
612 goto fail;
613 }
614
615 d->connect_port(u->handle[c], input_port, u->input);
616 d->connect_port(u->handle[c], output_port, u->output);
617 }
618
619 if (!cdata && n_control > 0) {
620 pa_log("This plugin requires specification of %lu control parameters.", n_control);
621 goto fail;
622 }
623
624 if (n_control > 0) {
625 const char *state = NULL;
626 char *k;
627 unsigned long h;
628
629 u->control = pa_xnew(LADSPA_Data, (unsigned) n_control);
630 use_default = pa_xnew(pa_bool_t, (unsigned) n_control);
631 p = 0;
632
633 while ((k = pa_split(cdata, ",", &state)) && p < n_control) {
634 double f;
635
636 if (*k == 0) {
637 use_default[p++] = TRUE;
638 pa_xfree(k);
639 continue;
640 }
641
642 if (pa_atod(k, &f) < 0) {
643 pa_log("Failed to parse control value '%s'", k);
644 pa_xfree(k);
645 goto fail;
646 }
647
648 pa_xfree(k);
649
650 use_default[p] = FALSE;
651 u->control[p++] = (LADSPA_Data) f;
652 }
653
654 /* The previous loop doesn't take the last control value into account
655 if it is left empty, so we do it here. */
656 if (*cdata == 0 || cdata[strlen(cdata) - 1] == ',') {
657 if (p < n_control)
658 use_default[p] = TRUE;
659 p++;
660 }
661
662 if (p > n_control || k) {
663 pa_log("Too many control values passed, %lu expected.", n_control);
664 pa_xfree(k);
665 goto fail;
666 }
667
668 if (p < n_control) {
669 pa_log("Not enough control values passed, %lu expected, %lu passed.", n_control, p);
670 goto fail;
671 }
672
673 h = 0;
674 for (p = 0; p < d->PortCount; p++) {
675 LADSPA_PortRangeHintDescriptor hint = d->PortRangeHints[p].HintDescriptor;
676
677 if (!LADSPA_IS_PORT_CONTROL(d->PortDescriptors[p]))
678 continue;
679
680 if (LADSPA_IS_PORT_OUTPUT(d->PortDescriptors[p])) {
681 for (c = 0; c < ss.channels; c++)
682 d->connect_port(u->handle[c], p, &u->control_out);
683 continue;
684 }
685
686 pa_assert(h < n_control);
687
688 if (use_default[h]) {
689 LADSPA_Data lower, upper;
690
691 if (!LADSPA_IS_HINT_HAS_DEFAULT(hint)) {
692 pa_log("Control port value left empty but plugin defines no default.");
693 goto fail;
694 }
695
696 lower = d->PortRangeHints[p].LowerBound;
697 upper = d->PortRangeHints[p].UpperBound;
698
699 if (LADSPA_IS_HINT_SAMPLE_RATE(hint)) {
700 lower *= (LADSPA_Data) ss.rate;
701 upper *= (LADSPA_Data) ss.rate;
702 }
703
704 switch (hint & LADSPA_HINT_DEFAULT_MASK) {
705
706 case LADSPA_HINT_DEFAULT_MINIMUM:
707 u->control[h] = lower;
708 break;
709
710 case LADSPA_HINT_DEFAULT_MAXIMUM:
711 u->control[h] = upper;
712 break;
713
714 case LADSPA_HINT_DEFAULT_LOW:
715 if (LADSPA_IS_HINT_LOGARITHMIC(hint))
716 u->control[h] = (LADSPA_Data) exp(log(lower) * 0.75 + log(upper) * 0.25);
717 else
718 u->control[h] = (LADSPA_Data) (lower * 0.75 + upper * 0.25);
719 break;
720
721 case LADSPA_HINT_DEFAULT_MIDDLE:
722 if (LADSPA_IS_HINT_LOGARITHMIC(hint))
723 u->control[h] = (LADSPA_Data) exp(log(lower) * 0.5 + log(upper) * 0.5);
724 else
725 u->control[h] = (LADSPA_Data) (lower * 0.5 + upper * 0.5);
726 break;
727
728 case LADSPA_HINT_DEFAULT_HIGH:
729 if (LADSPA_IS_HINT_LOGARITHMIC(hint))
730 u->control[h] = (LADSPA_Data) exp(log(lower) * 0.25 + log(upper) * 0.75);
731 else
732 u->control[h] = (LADSPA_Data) (lower * 0.25 + upper * 0.75);
733 break;
734
735 case LADSPA_HINT_DEFAULT_0:
736 u->control[h] = 0;
737 break;
738
739 case LADSPA_HINT_DEFAULT_1:
740 u->control[h] = 1;
741 break;
742
743 case LADSPA_HINT_DEFAULT_100:
744 u->control[h] = 100;
745 break;
746
747 case LADSPA_HINT_DEFAULT_440:
748 u->control[h] = 440;
749 break;
750
751 default:
752 pa_assert_not_reached();
753 }
754 }
755
756 if (LADSPA_IS_HINT_INTEGER(hint))
757 u->control[h] = roundf(u->control[h]);
758
759 pa_log_debug("Binding %f to port %s", u->control[h], d->PortNames[p]);
760
761 for (c = 0; c < ss.channels; c++)
762 d->connect_port(u->handle[c], p, &u->control[h]);
763
764 h++;
765 }
766
767 pa_assert(h == n_control);
768 }
769
770 if (d->activate)
771 for (c = 0; c < u->channels; c++)
772 d->activate(u->handle[c]);
773
774 /* Create sink */
775 pa_sink_new_data_init(&sink_data);
776 sink_data.driver = __FILE__;
777 sink_data.module = m;
778 if (!(sink_data.name = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
779 sink_data.name = pa_sprintf_malloc("%s.ladspa", master->name);
780 pa_sink_new_data_set_sample_spec(&sink_data, &ss);
781 pa_sink_new_data_set_channel_map(&sink_data, &map);
782 pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, master->name);
783 pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_CLASS, "filter");
784 pa_proplist_sets(sink_data.proplist, "device.ladspa.module", plugin);
785 pa_proplist_sets(sink_data.proplist, "device.ladspa.label", d->Label);
786 pa_proplist_sets(sink_data.proplist, "device.ladspa.name", d->Name);
787 pa_proplist_sets(sink_data.proplist, "device.ladspa.maker", d->Maker);
788 pa_proplist_sets(sink_data.proplist, "device.ladspa.copyright", d->Copyright);
789 pa_proplist_setf(sink_data.proplist, "device.ladspa.unique_id", "%lu", (unsigned long) d->UniqueID);
790
791 if (pa_modargs_get_proplist(ma, "sink_properties", sink_data.proplist, PA_UPDATE_REPLACE) < 0) {
792 pa_log("Invalid properties");
793 pa_sink_new_data_done(&sink_data);
794 goto fail;
795 }
796
797 if ((u->auto_desc = !pa_proplist_contains(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION))) {
798 const char *z;
799
800 z = pa_proplist_gets(master->proplist, PA_PROP_DEVICE_DESCRIPTION);
801 pa_proplist_setf(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "LADSPA Plugin %s on %s", d->Name, z ? z : master->name);
802 }
803
804 u->sink = pa_sink_new(m->core, &sink_data,
805 PA_SINK_HW_MUTE_CTRL|PA_SINK_HW_VOLUME_CTRL|PA_SINK_DECIBEL_VOLUME|
806 (master->flags & (PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY)));
807 pa_sink_new_data_done(&sink_data);
808
809 if (!u->sink) {
810 pa_log("Failed to create sink.");
811 goto fail;
812 }
813
814 u->sink->parent.process_msg = sink_process_msg_cb;
815 u->sink->set_state = sink_set_state_cb;
816 u->sink->update_requested_latency = sink_update_requested_latency_cb;
817 u->sink->request_rewind = sink_request_rewind_cb;
818 u->sink->set_volume = sink_set_volume_cb;
819 u->sink->set_mute = sink_set_mute_cb;
820 u->sink->userdata = u;
821
822 pa_sink_set_asyncmsgq(u->sink, master->asyncmsgq);
823
824 /* Create sink input */
825 pa_sink_input_new_data_init(&sink_input_data);
826 sink_input_data.driver = __FILE__;
827 sink_input_data.module = m;
828 sink_input_data.sink = master;
829 pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_NAME, "LADSPA Stream");
830 pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, "filter");
831 pa_sink_input_new_data_set_sample_spec(&sink_input_data, &ss);
832 pa_sink_input_new_data_set_channel_map(&sink_input_data, &map);
833
834 pa_sink_input_new(&u->sink_input, m->core, &sink_input_data);
835 pa_sink_input_new_data_done(&sink_input_data);
836
837 if (!u->sink_input)
838 goto fail;
839
840 u->sink_input->pop = sink_input_pop_cb;
841 u->sink_input->process_rewind = sink_input_process_rewind_cb;
842 u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
843 u->sink_input->update_max_request = sink_input_update_max_request_cb;
844 u->sink_input->update_sink_latency_range = sink_input_update_sink_latency_range_cb;
845 u->sink_input->update_sink_fixed_latency = sink_input_update_sink_fixed_latency_cb;
846 u->sink_input->kill = sink_input_kill_cb;
847 u->sink_input->attach = sink_input_attach_cb;
848 u->sink_input->detach = sink_input_detach_cb;
849 u->sink_input->state_change = sink_input_state_change_cb;
850 u->sink_input->may_move_to = sink_input_may_move_to_cb;
851 u->sink_input->moving = sink_input_moving_cb;
852 u->sink_input->volume_changed = sink_input_volume_changed_cb;
853 u->sink_input->mute_changed = sink_input_mute_changed_cb;
854 u->sink_input->userdata = u;
855
856 pa_sink_put(u->sink);
857 pa_sink_input_put(u->sink_input);
858
859 pa_modargs_free(ma);
860
861 pa_xfree(use_default);
862
863 return 0;
864
865 fail:
866 if (ma)
867 pa_modargs_free(ma);
868
869 pa_xfree(use_default);
870
871 pa__done(m);
872
873 return -1;
874 }
875
876 int pa__get_n_used(pa_module *m) {
877 struct userdata *u;
878
879 pa_assert(m);
880 pa_assert_se(u = m->userdata);
881
882 return pa_sink_linked_by(u->sink);
883 }
884
885 void pa__done(pa_module*m) {
886 struct userdata *u;
887 unsigned c;
888
889 pa_assert(m);
890
891 if (!(u = m->userdata))
892 return;
893
894 /* See comments in sink_input_kill_cb() above regarding
895 * destruction order! */
896
897 if (u->sink_input)
898 pa_sink_input_unlink(u->sink_input);
899
900 if (u->sink)
901 pa_sink_unlink(u->sink);
902
903 if (u->sink_input)
904 pa_sink_input_unref(u->sink_input);
905
906 if (u->sink)
907 pa_sink_unref(u->sink);
908
909 for (c = 0; c < u->channels; c++)
910 if (u->handle[c]) {
911 if (u->descriptor->deactivate)
912 u->descriptor->deactivate(u->handle[c]);
913 u->descriptor->cleanup(u->handle[c]);
914 }
915
916 if (u->output != u->input)
917 pa_xfree(u->output);
918
919 if (u->memblockq)
920 pa_memblockq_free(u->memblockq);
921
922 pa_xfree(u->input);
923
924 pa_xfree(u->control);
925
926 pa_xfree(u);
927 }