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