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