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