]> code.delx.au - pulseaudio/blob - src/modules/module-virtual-surround-sink.c
i18n: Update Greek translation
[pulseaudio] / src / modules / module-virtual-surround-sink.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2010 Intel Corporation
5 Contributor: Pierre-Louis Bossart <pierre-louis.bossart@intel.com>
6 Copyright 2012 Niels Ole Salscheider <niels_ole@salscheider-online.de>
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2.1 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <pulse/gccmacro.h>
29 #include <pulse/xmalloc.h>
30
31 #include <pulsecore/i18n.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/rtpoll.h>
39 #include <pulsecore/sample-util.h>
40 #include <pulsecore/ltdl-helper.h>
41 #include <pulsecore/sound-file.h>
42 #include <pulsecore/resampler.h>
43
44 #include <math.h>
45
46 #include "module-virtual-surround-sink-symdef.h"
47
48 PA_MODULE_AUTHOR("Niels Ole Salscheider");
49 PA_MODULE_DESCRIPTION(_("Virtual surround 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 "use_volume_sharing=<yes or no> "
61 "force_flat_volume=<yes or no> "
62 "hrir=/path/to/left_hrir.wav "
63 ));
64
65 #define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
66
67 struct userdata {
68 pa_module *module;
69
70 /* FIXME: Uncomment this and take "autoloaded" as a modarg if this is a filter */
71 /* bool autoloaded; */
72
73 pa_sink *sink;
74 pa_sink_input *sink_input;
75
76 pa_memblockq *memblockq;
77
78 bool auto_desc;
79 unsigned channels;
80 unsigned hrir_channels;
81
82 unsigned fs, sink_fs;
83
84 unsigned *mapping_left;
85 unsigned *mapping_right;
86
87 unsigned hrir_samples;
88 float *hrir_data;
89
90 float *input_buffer;
91 int input_buffer_offset;
92 };
93
94 static const char* const valid_modargs[] = {
95 "sink_name",
96 "sink_properties",
97 "master",
98 "format",
99 "rate",
100 "channels",
101 "channel_map",
102 "use_volume_sharing",
103 "force_flat_volume",
104 "hrir",
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 unsigned n;
221 pa_memchunk tchunk;
222
223 unsigned j, k, l;
224 float sum_right, sum_left;
225 float current_sample;
226
227 pa_sink_input_assert_ref(i);
228 pa_assert(chunk);
229 pa_assert_se(u = i->userdata);
230
231 /* Hmm, process any rewind request that might be queued up */
232 pa_sink_process_rewind(u->sink, 0);
233
234 while (pa_memblockq_peek(u->memblockq, &tchunk) < 0) {
235 pa_memchunk nchunk;
236
237 pa_sink_render(u->sink, nbytes * u->sink_fs / u->fs, &nchunk);
238 pa_memblockq_push(u->memblockq, &nchunk);
239 pa_memblock_unref(nchunk.memblock);
240 }
241
242 tchunk.length = PA_MIN(nbytes * u->sink_fs / u->fs, tchunk.length);
243 pa_assert(tchunk.length > 0);
244
245 n = (unsigned) (tchunk.length / u->sink_fs);
246
247 pa_assert(n > 0);
248
249 chunk->index = 0;
250 chunk->length = n * u->fs;
251 chunk->memblock = pa_memblock_new(i->sink->core->mempool, chunk->length);
252
253 pa_memblockq_drop(u->memblockq, n * u->sink_fs);
254
255 src = pa_memblock_acquire_chunk(&tchunk);
256 dst = pa_memblock_acquire(chunk->memblock);
257
258 for (l = 0; l < n; l++) {
259 memcpy(((char*) u->input_buffer) + u->input_buffer_offset * u->sink_fs, ((char *) src) + l * u->sink_fs, u->sink_fs);
260
261 sum_right = 0;
262 sum_left = 0;
263
264 /* fold the input buffer with the impulse response */
265 for (j = 0; j < u->hrir_samples; j++) {
266 for (k = 0; k < u->channels; k++) {
267 current_sample = u->input_buffer[((u->input_buffer_offset + j) % u->hrir_samples) * u->channels + k];
268
269 sum_left += current_sample * u->hrir_data[j * u->hrir_channels + u->mapping_left[k]];
270 sum_right += current_sample * u->hrir_data[j * u->hrir_channels + u->mapping_right[k]];
271 }
272 }
273
274 dst[2 * l] = PA_CLAMP_UNLIKELY(sum_left, -1.0f, 1.0f);
275 dst[2 * l + 1] = PA_CLAMP_UNLIKELY(sum_right, -1.0f, 1.0f);
276
277 u->input_buffer_offset--;
278 if (u->input_buffer_offset < 0)
279 u->input_buffer_offset += u->hrir_samples;
280 }
281
282 pa_memblock_release(tchunk.memblock);
283 pa_memblock_release(chunk->memblock);
284
285 pa_memblock_unref(tchunk.memblock);
286
287 return 0;
288 }
289
290 /* Called from I/O thread context */
291 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
292 struct userdata *u;
293 size_t amount = 0;
294
295 pa_sink_input_assert_ref(i);
296 pa_assert_se(u = i->userdata);
297
298 if (u->sink->thread_info.rewind_nbytes > 0) {
299 size_t max_rewrite;
300
301 max_rewrite = nbytes * u->sink_fs / u->fs + pa_memblockq_get_length(u->memblockq);
302 amount = PA_MIN(u->sink->thread_info.rewind_nbytes * u->sink_fs / u->fs, max_rewrite);
303 u->sink->thread_info.rewind_nbytes = 0;
304
305 if (amount > 0) {
306 pa_memblockq_seek(u->memblockq, - (int64_t) amount, PA_SEEK_RELATIVE, true);
307
308 /* Reset the input buffer */
309 memset(u->input_buffer, 0, u->hrir_samples * u->sink_fs);
310 u->input_buffer_offset = 0;
311 }
312 }
313
314 pa_sink_process_rewind(u->sink, amount);
315 pa_memblockq_rewind(u->memblockq, nbytes * u->sink_fs / u->fs);
316 }
317
318 /* Called from I/O thread context */
319 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
320 struct userdata *u;
321
322 pa_sink_input_assert_ref(i);
323 pa_assert_se(u = i->userdata);
324
325 /* FIXME: Too small max_rewind:
326 * https://bugs.freedesktop.org/show_bug.cgi?id=53709 */
327 pa_memblockq_set_maxrewind(u->memblockq, nbytes * u->sink_fs / u->fs);
328 pa_sink_set_max_rewind_within_thread(u->sink, nbytes * u->sink_fs / u->fs);
329 }
330
331 /* Called from I/O thread context */
332 static void sink_input_update_max_request_cb(pa_sink_input *i, size_t nbytes) {
333 struct userdata *u;
334
335 pa_sink_input_assert_ref(i);
336 pa_assert_se(u = i->userdata);
337
338 pa_sink_set_max_request_within_thread(u->sink, nbytes * u->sink_fs / u->fs);
339 }
340
341 /* Called from I/O thread context */
342 static void sink_input_update_sink_latency_range_cb(pa_sink_input *i) {
343 struct userdata *u;
344
345 pa_sink_input_assert_ref(i);
346 pa_assert_se(u = i->userdata);
347
348 pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
349 }
350
351 /* Called from I/O thread context */
352 static void sink_input_update_sink_fixed_latency_cb(pa_sink_input *i) {
353 struct userdata *u;
354
355 pa_sink_input_assert_ref(i);
356 pa_assert_se(u = i->userdata);
357
358 pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
359 }
360
361 /* Called from I/O thread context */
362 static void sink_input_detach_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_detach_within_thread(u->sink);
369
370 pa_sink_set_rtpoll(u->sink, NULL);
371 }
372
373 /* Called from I/O thread context */
374 static void sink_input_attach_cb(pa_sink_input *i) {
375 struct userdata *u;
376
377 pa_sink_input_assert_ref(i);
378 pa_assert_se(u = i->userdata);
379
380 pa_sink_set_rtpoll(u->sink, i->sink->thread_info.rtpoll);
381 pa_sink_set_latency_range_within_thread(u->sink, i->sink->thread_info.min_latency, i->sink->thread_info.max_latency);
382
383 pa_sink_set_fixed_latency_within_thread(u->sink, i->sink->thread_info.fixed_latency);
384
385 pa_sink_set_max_request_within_thread(u->sink, pa_sink_input_get_max_request(i) * u->sink_fs / u->fs);
386
387 /* FIXME: Too small max_rewind:
388 * https://bugs.freedesktop.org/show_bug.cgi?id=53709 */
389 pa_sink_set_max_rewind_within_thread(u->sink, pa_sink_input_get_max_rewind(i) * u->sink_fs / u->fs);
390
391 pa_sink_attach_within_thread(u->sink);
392 }
393
394 /* Called from main context */
395 static void sink_input_kill_cb(pa_sink_input *i) {
396 struct userdata *u;
397
398 pa_sink_input_assert_ref(i);
399 pa_assert_se(u = i->userdata);
400
401 /* The order here matters! We first kill the sink input, followed
402 * by the sink. That means the sink callbacks must be protected
403 * against an unconnected sink input! */
404 pa_sink_input_unlink(u->sink_input);
405 pa_sink_unlink(u->sink);
406
407 pa_sink_input_unref(u->sink_input);
408 u->sink_input = NULL;
409
410 pa_sink_unref(u->sink);
411 u->sink = NULL;
412
413 pa_module_unload_request(u->module, true);
414 }
415
416 /* Called from IO thread context */
417 static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
418 struct userdata *u;
419
420 pa_sink_input_assert_ref(i);
421 pa_assert_se(u = i->userdata);
422
423 /* If we are added for the first time, ask for a rewinding so that
424 * we are heard right-away. */
425 if (PA_SINK_INPUT_IS_LINKED(state) &&
426 i->thread_info.state == PA_SINK_INPUT_INIT) {
427 pa_log_debug("Requesting rewind due to state change.");
428 pa_sink_input_request_rewind(i, 0, false, true, true);
429 }
430 }
431
432 /* Called from main context */
433 static void sink_input_moving_cb(pa_sink_input *i, pa_sink *dest) {
434 struct userdata *u;
435
436 pa_sink_input_assert_ref(i);
437 pa_assert_se(u = i->userdata);
438
439 if (dest) {
440 pa_sink_set_asyncmsgq(u->sink, dest->asyncmsgq);
441 pa_sink_update_flags(u->sink, PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY, dest->flags);
442 } else
443 pa_sink_set_asyncmsgq(u->sink, NULL);
444
445 if (u->auto_desc && dest) {
446 const char *z;
447 pa_proplist *pl;
448
449 pl = pa_proplist_new();
450 z = pa_proplist_gets(dest->proplist, PA_PROP_DEVICE_DESCRIPTION);
451 pa_proplist_setf(pl, PA_PROP_DEVICE_DESCRIPTION, "Virtual Surround Sink %s on %s",
452 pa_proplist_gets(u->sink->proplist, "device.vsurroundsink.name"), z ? z : dest->name);
453
454 pa_sink_update_proplist(u->sink, PA_UPDATE_REPLACE, pl);
455 pa_proplist_free(pl);
456 }
457 }
458
459 /* Called from main context */
460 static void sink_input_volume_changed_cb(pa_sink_input *i) {
461 struct userdata *u;
462
463 pa_sink_input_assert_ref(i);
464 pa_assert_se(u = i->userdata);
465
466 pa_sink_volume_changed(u->sink, &i->volume);
467 }
468
469 /* Called from main context */
470 static void sink_input_mute_changed_cb(pa_sink_input *i) {
471 struct userdata *u;
472
473 pa_sink_input_assert_ref(i);
474 pa_assert_se(u = i->userdata);
475
476 pa_sink_mute_changed(u->sink, i->muted);
477 }
478
479 static pa_channel_position_t mirror_channel(pa_channel_position_t channel) {
480 switch (channel) {
481 case PA_CHANNEL_POSITION_FRONT_LEFT:
482 return PA_CHANNEL_POSITION_FRONT_RIGHT;
483
484 case PA_CHANNEL_POSITION_FRONT_RIGHT:
485 return PA_CHANNEL_POSITION_FRONT_LEFT;
486
487 case PA_CHANNEL_POSITION_REAR_LEFT:
488 return PA_CHANNEL_POSITION_REAR_RIGHT;
489
490 case PA_CHANNEL_POSITION_REAR_RIGHT:
491 return PA_CHANNEL_POSITION_REAR_LEFT;
492
493 case PA_CHANNEL_POSITION_SIDE_LEFT:
494 return PA_CHANNEL_POSITION_SIDE_RIGHT;
495
496 case PA_CHANNEL_POSITION_SIDE_RIGHT:
497 return PA_CHANNEL_POSITION_SIDE_LEFT;
498
499 case PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER:
500 return PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER;
501
502 case PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER:
503 return PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER;
504
505 case PA_CHANNEL_POSITION_TOP_FRONT_LEFT:
506 return PA_CHANNEL_POSITION_TOP_FRONT_RIGHT;
507
508 case PA_CHANNEL_POSITION_TOP_FRONT_RIGHT:
509 return PA_CHANNEL_POSITION_TOP_FRONT_LEFT;
510
511 case PA_CHANNEL_POSITION_TOP_REAR_LEFT:
512 return PA_CHANNEL_POSITION_TOP_REAR_RIGHT;
513
514 case PA_CHANNEL_POSITION_TOP_REAR_RIGHT:
515 return PA_CHANNEL_POSITION_TOP_REAR_LEFT;
516
517 default:
518 return channel;
519 }
520 }
521
522 static void normalize_hrir(struct userdata *u) {
523 /* normalize hrir to avoid audible clipping
524 *
525 * The following heuristic tries to avoid audible clipping. It cannot avoid
526 * clipping in the worst case though, because the scaling factor would
527 * become too large resulting in a too quiet signal.
528 * The idea of the heuristic is to avoid clipping when a single click is
529 * played back on all channels. The scaling factor describes the additional
530 * factor that is necessary to avoid clipping for "normal" signals.
531 *
532 * This algorithm doesn't pretend to be perfect, it's just something that
533 * appears to work (not too quiet, no audible clipping) on the material that
534 * it has been tested on. If you find a real-world example where this
535 * algorithm results in audible clipping, please write a patch that adjusts
536 * the scaling factor constants or improves the algorithm (or if you can't
537 * write a patch, at least report the problem to the PulseAudio mailing list
538 * or bug tracker). */
539
540 const float scaling_factor = 2.5;
541
542 float hrir_sum, hrir_max;
543 unsigned i, j;
544
545 hrir_max = 0;
546 for (i = 0; i < u->hrir_samples; i++) {
547 hrir_sum = 0;
548 for (j = 0; j < u->hrir_channels; j++)
549 hrir_sum += fabs(u->hrir_data[i * u->hrir_channels + j]);
550
551 if (hrir_sum > hrir_max)
552 hrir_max = hrir_sum;
553 }
554
555 for (i = 0; i < u->hrir_samples; i++) {
556 for (j = 0; j < u->hrir_channels; j++)
557 u->hrir_data[i * u->hrir_channels + j] /= hrir_max * scaling_factor;
558 }
559 }
560
561 int pa__init(pa_module*m) {
562 struct userdata *u;
563 pa_sample_spec ss, sink_input_ss;
564 pa_channel_map map, sink_input_map;
565 pa_modargs *ma;
566 pa_sink *master=NULL;
567 pa_sink_input_new_data sink_input_data;
568 pa_sink_new_data sink_data;
569 bool use_volume_sharing = true;
570 bool force_flat_volume = false;
571 pa_memchunk silence;
572
573 const char *hrir_file;
574 unsigned i, j, found_channel_left, found_channel_right;
575 float *hrir_data;
576
577 pa_sample_spec hrir_ss;
578 pa_channel_map hrir_map;
579
580 pa_sample_spec hrir_temp_ss;
581 pa_memchunk hrir_temp_chunk, hrir_temp_chunk_resampled;
582 pa_resampler *resampler;
583
584 size_t hrir_copied_length, hrir_total_length;
585
586 hrir_temp_chunk.memblock = NULL;
587 hrir_temp_chunk_resampled.memblock = NULL;
588
589 pa_assert(m);
590
591 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
592 pa_log("Failed to parse module arguments.");
593 goto fail;
594 }
595
596 if (!(master = pa_namereg_get(m->core, pa_modargs_get_value(ma, "master", NULL), PA_NAMEREG_SINK))) {
597 pa_log("Master sink not found");
598 goto fail;
599 }
600
601 pa_assert(master);
602
603 u = pa_xnew0(struct userdata, 1);
604 u->module = m;
605 m->userdata = u;
606
607 /* Initialize hrir and input buffer */
608 /* this is the hrir file for the left ear! */
609 if (!(hrir_file = pa_modargs_get_value(ma, "hrir", NULL))) {
610 pa_log("The mandatory 'hrir' module argument is missing.");
611 goto fail;
612 }
613
614 if (pa_sound_file_load(master->core->mempool, hrir_file, &hrir_temp_ss, &hrir_map, &hrir_temp_chunk, NULL) < 0) {
615 pa_log("Cannot load hrir file.");
616 goto fail;
617 }
618
619 /* sample spec / map of hrir */
620 hrir_ss.format = PA_SAMPLE_FLOAT32;
621 hrir_ss.rate = master->sample_spec.rate;
622 hrir_ss.channels = hrir_temp_ss.channels;
623
624 /* sample spec of sink */
625 ss = hrir_ss;
626 map = hrir_map;
627 if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) {
628 pa_log("Invalid sample format specification or channel map");
629 goto fail;
630 }
631 ss.format = PA_SAMPLE_FLOAT32;
632 hrir_ss.rate = ss.rate;
633 u->channels = ss.channels;
634
635 if (pa_modargs_get_value_boolean(ma, "use_volume_sharing", &use_volume_sharing) < 0) {
636 pa_log("use_volume_sharing= expects a boolean argument");
637 goto fail;
638 }
639
640 if (pa_modargs_get_value_boolean(ma, "force_flat_volume", &force_flat_volume) < 0) {
641 pa_log("force_flat_volume= expects a boolean argument");
642 goto fail;
643 }
644
645 if (use_volume_sharing && force_flat_volume) {
646 pa_log("Flat volume can't be forced when using volume sharing.");
647 goto fail;
648 }
649
650 /* sample spec / map of sink input */
651 pa_channel_map_init_stereo(&sink_input_map);
652 sink_input_ss.channels = 2;
653 sink_input_ss.format = PA_SAMPLE_FLOAT32;
654 sink_input_ss.rate = ss.rate;
655
656 u->sink_fs = pa_frame_size(&ss);
657 u->fs = pa_frame_size(&sink_input_ss);
658
659 /* Create sink */
660 pa_sink_new_data_init(&sink_data);
661 sink_data.driver = __FILE__;
662 sink_data.module = m;
663 if (!(sink_data.name = pa_xstrdup(pa_modargs_get_value(ma, "sink_name", NULL))))
664 sink_data.name = pa_sprintf_malloc("%s.vsurroundsink", master->name);
665 pa_sink_new_data_set_sample_spec(&sink_data, &ss);
666 pa_sink_new_data_set_channel_map(&sink_data, &map);
667 pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_MASTER_DEVICE, master->name);
668 pa_proplist_sets(sink_data.proplist, PA_PROP_DEVICE_CLASS, "filter");
669 pa_proplist_sets(sink_data.proplist, "device.vsurroundsink.name", sink_data.name);
670
671 if (pa_modargs_get_proplist(ma, "sink_properties", sink_data.proplist, PA_UPDATE_REPLACE) < 0) {
672 pa_log("Invalid properties");
673 pa_sink_new_data_done(&sink_data);
674 goto fail;
675 }
676
677 if ((u->auto_desc = !pa_proplist_contains(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION))) {
678 const char *z;
679
680 z = pa_proplist_gets(master->proplist, PA_PROP_DEVICE_DESCRIPTION);
681 pa_proplist_setf(sink_data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Virtual Surround Sink %s on %s", sink_data.name, z ? z : master->name);
682 }
683
684 u->sink = pa_sink_new(m->core, &sink_data, (master->flags & (PA_SINK_LATENCY|PA_SINK_DYNAMIC_LATENCY))
685 | (use_volume_sharing ? PA_SINK_SHARE_VOLUME_WITH_MASTER : 0));
686 pa_sink_new_data_done(&sink_data);
687
688 if (!u->sink) {
689 pa_log("Failed to create sink.");
690 goto fail;
691 }
692
693 u->sink->parent.process_msg = sink_process_msg_cb;
694 u->sink->set_state = sink_set_state_cb;
695 u->sink->update_requested_latency = sink_update_requested_latency_cb;
696 u->sink->request_rewind = sink_request_rewind_cb;
697 pa_sink_set_set_mute_callback(u->sink, sink_set_mute_cb);
698 if (!use_volume_sharing) {
699 pa_sink_set_set_volume_callback(u->sink, sink_set_volume_cb);
700 pa_sink_enable_decibel_volume(u->sink, true);
701 }
702 /* Normally this flag would be enabled automatically be we can force it. */
703 if (force_flat_volume)
704 u->sink->flags |= PA_SINK_FLAT_VOLUME;
705 u->sink->userdata = u;
706
707 pa_sink_set_asyncmsgq(u->sink, master->asyncmsgq);
708
709 /* Create sink input */
710 pa_sink_input_new_data_init(&sink_input_data);
711 sink_input_data.driver = __FILE__;
712 sink_input_data.module = m;
713 pa_sink_input_new_data_set_sink(&sink_input_data, master, false);
714 sink_input_data.origin_sink = u->sink;
715 pa_proplist_setf(sink_input_data.proplist, PA_PROP_MEDIA_NAME, "Virtual Surround Sink Stream from %s", pa_proplist_gets(u->sink->proplist, PA_PROP_DEVICE_DESCRIPTION));
716 pa_proplist_sets(sink_input_data.proplist, PA_PROP_MEDIA_ROLE, "filter");
717 pa_sink_input_new_data_set_sample_spec(&sink_input_data, &sink_input_ss);
718 pa_sink_input_new_data_set_channel_map(&sink_input_data, &sink_input_map);
719
720 pa_sink_input_new(&u->sink_input, m->core, &sink_input_data);
721 pa_sink_input_new_data_done(&sink_input_data);
722
723 if (!u->sink_input)
724 goto fail;
725
726 u->sink_input->pop = sink_input_pop_cb;
727 u->sink_input->process_rewind = sink_input_process_rewind_cb;
728 u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
729 u->sink_input->update_max_request = sink_input_update_max_request_cb;
730 u->sink_input->update_sink_latency_range = sink_input_update_sink_latency_range_cb;
731 u->sink_input->update_sink_fixed_latency = sink_input_update_sink_fixed_latency_cb;
732 u->sink_input->kill = sink_input_kill_cb;
733 u->sink_input->attach = sink_input_attach_cb;
734 u->sink_input->detach = sink_input_detach_cb;
735 u->sink_input->state_change = sink_input_state_change_cb;
736 u->sink_input->moving = sink_input_moving_cb;
737 u->sink_input->volume_changed = use_volume_sharing ? NULL : sink_input_volume_changed_cb;
738 u->sink_input->mute_changed = sink_input_mute_changed_cb;
739 u->sink_input->userdata = u;
740
741 u->sink->input_to_master = u->sink_input;
742
743 pa_sink_input_get_silence(u->sink_input, &silence);
744 u->memblockq = pa_memblockq_new("module-virtual-surround-sink memblockq", 0, MEMBLOCKQ_MAXLENGTH, 0, &sink_input_ss, 1, 1, 0, &silence);
745 pa_memblock_unref(silence.memblock);
746
747 /* resample hrir */
748 resampler = pa_resampler_new(u->sink->core->mempool, &hrir_temp_ss, &hrir_map, &hrir_ss, &hrir_map,
749 PA_RESAMPLER_SRC_SINC_BEST_QUALITY, PA_RESAMPLER_NO_REMAP);
750
751 u->hrir_samples = hrir_temp_chunk.length / pa_frame_size(&hrir_temp_ss) * hrir_ss.rate / hrir_temp_ss.rate;
752 if (u->hrir_samples > 64) {
753 u->hrir_samples = 64;
754 pa_log("The (resampled) hrir contains more than 64 samples. Only the first 64 samples will be used to limit processor usage.");
755 }
756
757 hrir_total_length = u->hrir_samples * pa_frame_size(&hrir_ss);
758 u->hrir_channels = hrir_ss.channels;
759
760 u->hrir_data = (float *) pa_xmalloc(hrir_total_length);
761 hrir_copied_length = 0;
762
763 /* add silence to the hrir until we get enough samples out of the resampler */
764 while (hrir_copied_length < hrir_total_length) {
765 pa_resampler_run(resampler, &hrir_temp_chunk, &hrir_temp_chunk_resampled);
766 if (hrir_temp_chunk.memblock != hrir_temp_chunk_resampled.memblock) {
767 /* Silence input block */
768 pa_silence_memblock(hrir_temp_chunk.memblock, &hrir_temp_ss);
769 }
770
771 if (hrir_temp_chunk_resampled.memblock) {
772 /* Copy hrir data */
773 hrir_data = (float *) pa_memblock_acquire(hrir_temp_chunk_resampled.memblock);
774
775 if (hrir_total_length - hrir_copied_length >= hrir_temp_chunk_resampled.length) {
776 memcpy(u->hrir_data + hrir_copied_length, hrir_data, hrir_temp_chunk_resampled.length);
777 hrir_copied_length += hrir_temp_chunk_resampled.length;
778 } else {
779 memcpy(u->hrir_data + hrir_copied_length, hrir_data, hrir_total_length - hrir_copied_length);
780 hrir_copied_length = hrir_total_length;
781 }
782
783 pa_memblock_release(hrir_temp_chunk_resampled.memblock);
784 pa_memblock_unref(hrir_temp_chunk_resampled.memblock);
785 hrir_temp_chunk_resampled.memblock = NULL;
786 }
787 }
788
789 pa_resampler_free(resampler);
790
791 pa_memblock_unref(hrir_temp_chunk.memblock);
792 hrir_temp_chunk.memblock = NULL;
793
794 if (hrir_map.channels < map.channels) {
795 pa_log("hrir file does not have enough channels!");
796 goto fail;
797 }
798
799 normalize_hrir(u);
800
801 /* create mapping between hrir and input */
802 u->mapping_left = (unsigned *) pa_xnew0(unsigned, u->channels);
803 u->mapping_right = (unsigned *) pa_xnew0(unsigned, u->channels);
804 for (i = 0; i < map.channels; i++) {
805 found_channel_left = 0;
806 found_channel_right = 0;
807
808 for (j = 0; j < hrir_map.channels; j++) {
809 if (hrir_map.map[j] == map.map[i]) {
810 u->mapping_left[i] = j;
811 found_channel_left = 1;
812 }
813
814 if (hrir_map.map[j] == mirror_channel(map.map[i])) {
815 u->mapping_right[i] = j;
816 found_channel_right = 1;
817 }
818 }
819
820 if (!found_channel_left) {
821 pa_log("Cannot find mapping for channel %s", pa_channel_position_to_string(map.map[i]));
822 goto fail;
823 }
824 if (!found_channel_right) {
825 pa_log("Cannot find mapping for channel %s", pa_channel_position_to_string(mirror_channel(map.map[i])));
826 goto fail;
827 }
828 }
829
830 u->input_buffer = pa_xmalloc0(u->hrir_samples * u->sink_fs);
831 u->input_buffer_offset = 0;
832
833 pa_sink_put(u->sink);
834 pa_sink_input_put(u->sink_input);
835
836 pa_modargs_free(ma);
837 return 0;
838
839 fail:
840 if (hrir_temp_chunk.memblock)
841 pa_memblock_unref(hrir_temp_chunk.memblock);
842
843 if (hrir_temp_chunk_resampled.memblock)
844 pa_memblock_unref(hrir_temp_chunk_resampled.memblock);
845
846 if (ma)
847 pa_modargs_free(ma);
848
849 pa__done(m);
850
851 return -1;
852 }
853
854 int pa__get_n_used(pa_module *m) {
855 struct userdata *u;
856
857 pa_assert(m);
858 pa_assert_se(u = m->userdata);
859
860 return pa_sink_linked_by(u->sink);
861 }
862
863 void pa__done(pa_module*m) {
864 struct userdata *u;
865
866 pa_assert(m);
867
868 if (!(u = m->userdata))
869 return;
870
871 /* See comments in sink_input_kill_cb() above regarding
872 * destruction order! */
873
874 if (u->sink_input)
875 pa_sink_input_unlink(u->sink_input);
876
877 if (u->sink)
878 pa_sink_unlink(u->sink);
879
880 if (u->sink_input)
881 pa_sink_input_unref(u->sink_input);
882
883 if (u->sink)
884 pa_sink_unref(u->sink);
885
886 if (u->memblockq)
887 pa_memblockq_free(u->memblockq);
888
889 if (u->hrir_data)
890 pa_xfree(u->hrir_data);
891
892 if (u->input_buffer)
893 pa_xfree(u->input_buffer);
894
895 if (u->mapping_left)
896 pa_xfree(u->mapping_left);
897 if (u->mapping_right)
898 pa_xfree(u->mapping_right);
899
900 pa_xfree(u);
901 }