2 This file is part of PulseAudio.
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>
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.
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.
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
28 #include <pulse/gccmacro.h>
29 #include <pulse/xmalloc.h>
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>
46 #include "module-virtual-surround-sink-symdef.h"
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
);
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> "
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 "
65 #define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
70 /* FIXME: Uncomment this and take "autoloaded" as a modarg if this is a filter */
71 /* pa_bool_t autoloaded; */
74 pa_sink_input
*sink_input
;
76 pa_memblockq
*memblockq
;
80 unsigned hrir_channels
;
84 unsigned *mapping_left
;
85 unsigned *mapping_right
;
87 unsigned hrir_samples
;
91 int input_buffer_offset
;
94 static const char* const valid_modargs
[] = {
102 "use_volume_sharing",
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
;
114 case PA_SINK_MESSAGE_GET_LATENCY
:
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;
125 *((pa_usec_t
*) data
) =
127 /* Get the latency of the master sink */
128 pa_sink_get_latency_within_thread(u
->sink_input
->sink
) +
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
);
136 return pa_sink_process_msg(o
, code
, data
, offset
, chunk
);
139 /* Called from main context */
140 static int sink_set_state_cb(pa_sink
*s
, pa_sink_state_t state
) {
143 pa_sink_assert_ref(s
);
144 pa_assert_se(u
= s
->userdata
);
146 if (!PA_SINK_IS_LINKED(state
) ||
147 !PA_SINK_INPUT_IS_LINKED(pa_sink_input_get_state(u
->sink_input
)))
150 pa_sink_input_cork(u
->sink_input
, state
== PA_SINK_SUSPENDED
);
154 /* Called from I/O thread context */
155 static void sink_request_rewind_cb(pa_sink
*s
) {
158 pa_sink_assert_ref(s
);
159 pa_assert_se(u
= s
->userdata
);
161 if (!PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
) ||
162 !PA_SINK_INPUT_IS_LINKED(u
->sink_input
->thread_info
.state
))
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
);
171 /* Called from I/O thread context */
172 static void sink_update_requested_latency_cb(pa_sink
*s
) {
175 pa_sink_assert_ref(s
);
176 pa_assert_se(u
= s
->userdata
);
178 if (!PA_SINK_IS_LINKED(u
->sink
->thread_info
.state
) ||
179 !PA_SINK_INPUT_IS_LINKED(u
->sink_input
->thread_info
.state
))
182 /* Just hand this one over to the master sink */
183 pa_sink_input_set_requested_latency_within_thread(
185 pa_sink_get_requested_latency_within_thread(s
));
188 /* Called from main context */
189 static void sink_set_volume_cb(pa_sink
*s
) {
192 pa_sink_assert_ref(s
);
193 pa_assert_se(u
= s
->userdata
);
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
)))
199 pa_sink_input_set_volume(u
->sink_input
, &s
->real_volume
, s
->save_volume
, TRUE
);
202 /* Called from main context */
203 static void sink_set_mute_cb(pa_sink
*s
) {
206 pa_sink_assert_ref(s
);
207 pa_assert_se(u
= s
->userdata
);
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
)))
213 pa_sink_input_set_mute(u
->sink_input
, s
->muted
, s
->save_muted
);
216 /* Called from I/O thread context */
217 static int sink_input_pop_cb(pa_sink_input
*i
, size_t nbytes
, pa_memchunk
*chunk
) {
224 float sum_right
, sum_left
;
225 float current_sample
;
227 pa_sink_input_assert_ref(i
);
229 pa_assert_se(u
= i
->userdata
);
231 /* Hmm, process any rewind request that might be queued up */
232 pa_sink_process_rewind(u
->sink
, 0);
234 while (pa_memblockq_peek(u
->memblockq
, &tchunk
) < 0) {
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
);
242 tchunk
.length
= PA_MIN(nbytes
* u
->sink_fs
/ u
->fs
, tchunk
.length
);
243 pa_assert(tchunk
.length
> 0);
245 n
= (unsigned) (tchunk
.length
/ u
->sink_fs
);
250 chunk
->length
= n
* u
->fs
;
251 chunk
->memblock
= pa_memblock_new(i
->sink
->core
->mempool
, chunk
->length
);
253 pa_memblockq_drop(u
->memblockq
, n
* u
->sink_fs
);
255 src
= (float*) ((uint8_t*) pa_memblock_acquire(tchunk
.memblock
) + tchunk
.index
);
256 dst
= (float*) pa_memblock_acquire(chunk
->memblock
);
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
);
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
];
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
]];
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
);
277 u
->input_buffer_offset
--;
278 if (u
->input_buffer_offset
< 0)
279 u
->input_buffer_offset
+= u
->hrir_samples
;
282 pa_memblock_release(tchunk
.memblock
);
283 pa_memblock_release(chunk
->memblock
);
285 pa_memblock_unref(tchunk
.memblock
);
290 /* Called from I/O thread context */
291 static void sink_input_process_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
295 pa_sink_input_assert_ref(i
);
296 pa_assert_se(u
= i
->userdata
);
298 if (u
->sink
->thread_info
.rewind_nbytes
> 0) {
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;
306 pa_memblockq_seek(u
->memblockq
, - (int64_t) amount
, PA_SEEK_RELATIVE
, TRUE
);
308 /* Reset the input buffer */
309 memset(u
->input_buffer
, 0, u
->hrir_samples
* u
->sink_fs
);
310 u
->input_buffer_offset
= 0;
314 pa_sink_process_rewind(u
->sink
, amount
);
315 pa_memblockq_rewind(u
->memblockq
, nbytes
* u
->sink_fs
/ u
->fs
);
318 /* Called from I/O thread context */
319 static void sink_input_update_max_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
322 pa_sink_input_assert_ref(i
);
323 pa_assert_se(u
= i
->userdata
);
325 pa_memblockq_set_maxrewind(u
->memblockq
, nbytes
* u
->sink_fs
/ u
->fs
);
326 pa_sink_set_max_rewind_within_thread(u
->sink
, nbytes
* u
->sink_fs
/ u
->fs
);
329 /* Called from I/O thread context */
330 static void sink_input_update_max_request_cb(pa_sink_input
*i
, size_t nbytes
) {
333 pa_sink_input_assert_ref(i
);
334 pa_assert_se(u
= i
->userdata
);
336 pa_sink_set_max_request_within_thread(u
->sink
, nbytes
* u
->sink_fs
/ u
->fs
);
339 /* Called from I/O thread context */
340 static void sink_input_update_sink_latency_range_cb(pa_sink_input
*i
) {
343 pa_sink_input_assert_ref(i
);
344 pa_assert_se(u
= i
->userdata
);
346 pa_sink_set_latency_range_within_thread(u
->sink
, i
->sink
->thread_info
.min_latency
, i
->sink
->thread_info
.max_latency
);
349 /* Called from I/O thread context */
350 static void sink_input_update_sink_fixed_latency_cb(pa_sink_input
*i
) {
353 pa_sink_input_assert_ref(i
);
354 pa_assert_se(u
= i
->userdata
);
356 pa_sink_set_fixed_latency_within_thread(u
->sink
, i
->sink
->thread_info
.fixed_latency
);
359 /* Called from I/O thread context */
360 static void sink_input_detach_cb(pa_sink_input
*i
) {
363 pa_sink_input_assert_ref(i
);
364 pa_assert_se(u
= i
->userdata
);
366 pa_sink_detach_within_thread(u
->sink
);
368 pa_sink_set_rtpoll(u
->sink
, NULL
);
371 /* Called from I/O thread context */
372 static void sink_input_attach_cb(pa_sink_input
*i
) {
375 pa_sink_input_assert_ref(i
);
376 pa_assert_se(u
= i
->userdata
);
378 pa_sink_set_rtpoll(u
->sink
, i
->sink
->thread_info
.rtpoll
);
379 pa_sink_set_latency_range_within_thread(u
->sink
, i
->sink
->thread_info
.min_latency
, i
->sink
->thread_info
.max_latency
);
381 pa_sink_set_fixed_latency_within_thread(u
->sink
, i
->sink
->thread_info
.fixed_latency
);
383 pa_sink_set_max_request_within_thread(u
->sink
, pa_sink_input_get_max_request(i
));
384 pa_sink_set_max_rewind_within_thread(u
->sink
, pa_sink_input_get_max_rewind(i
));
386 pa_sink_attach_within_thread(u
->sink
);
389 /* Called from main context */
390 static void sink_input_kill_cb(pa_sink_input
*i
) {
393 pa_sink_input_assert_ref(i
);
394 pa_assert_se(u
= i
->userdata
);
396 /* The order here matters! We first kill the sink input, followed
397 * by the sink. That means the sink callbacks must be protected
398 * against an unconnected sink input! */
399 pa_sink_input_unlink(u
->sink_input
);
400 pa_sink_unlink(u
->sink
);
402 pa_sink_input_unref(u
->sink_input
);
403 u
->sink_input
= NULL
;
405 pa_sink_unref(u
->sink
);
408 pa_module_unload_request(u
->module
, TRUE
);
411 /* Called from IO thread context */
412 static void sink_input_state_change_cb(pa_sink_input
*i
, pa_sink_input_state_t state
) {
415 pa_sink_input_assert_ref(i
);
416 pa_assert_se(u
= i
->userdata
);
418 /* If we are added for the first time, ask for a rewinding so that
419 * we are heard right-away. */
420 if (PA_SINK_INPUT_IS_LINKED(state
) &&
421 i
->thread_info
.state
== PA_SINK_INPUT_INIT
) {
422 pa_log_debug("Requesting rewind due to state change.");
423 pa_sink_input_request_rewind(i
, 0, FALSE
, TRUE
, TRUE
);
427 /* Called from main context */
428 static pa_bool_t
sink_input_may_move_to_cb(pa_sink_input
*i
, pa_sink
*dest
) {
431 pa_sink_input_assert_ref(i
);
432 pa_assert_se(u
= i
->userdata
);
434 return u
->sink
!= dest
;
437 /* Called from main context */
438 static void sink_input_moving_cb(pa_sink_input
*i
, pa_sink
*dest
) {
441 pa_sink_input_assert_ref(i
);
442 pa_assert_se(u
= i
->userdata
);
445 pa_sink_set_asyncmsgq(u
->sink
, dest
->asyncmsgq
);
446 pa_sink_update_flags(u
->sink
, PA_SINK_LATENCY
|PA_SINK_DYNAMIC_LATENCY
, dest
->flags
);
448 pa_sink_set_asyncmsgq(u
->sink
, NULL
);
450 if (u
->auto_desc
&& dest
) {
454 pl
= pa_proplist_new();
455 z
= pa_proplist_gets(dest
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
456 pa_proplist_setf(pl
, PA_PROP_DEVICE_DESCRIPTION
, "Virtual Surround Sink %s on %s",
457 pa_proplist_gets(u
->sink
->proplist
, "device.vsurroundsink.name"), z
? z
: dest
->name
);
459 pa_sink_update_proplist(u
->sink
, PA_UPDATE_REPLACE
, pl
);
460 pa_proplist_free(pl
);
464 /* Called from main context */
465 static void sink_input_volume_changed_cb(pa_sink_input
*i
) {
468 pa_sink_input_assert_ref(i
);
469 pa_assert_se(u
= i
->userdata
);
471 pa_sink_volume_changed(u
->sink
, &i
->volume
);
474 /* Called from main context */
475 static void sink_input_mute_changed_cb(pa_sink_input
*i
) {
478 pa_sink_input_assert_ref(i
);
479 pa_assert_se(u
= i
->userdata
);
481 pa_sink_mute_changed(u
->sink
, i
->muted
);
484 static pa_channel_position_t
mirror_channel(pa_channel_position_t channel
) {
486 case PA_CHANNEL_POSITION_FRONT_LEFT
:
487 return PA_CHANNEL_POSITION_FRONT_RIGHT
;
489 case PA_CHANNEL_POSITION_FRONT_RIGHT
:
490 return PA_CHANNEL_POSITION_FRONT_LEFT
;
492 case PA_CHANNEL_POSITION_REAR_LEFT
:
493 return PA_CHANNEL_POSITION_REAR_RIGHT
;
495 case PA_CHANNEL_POSITION_REAR_RIGHT
:
496 return PA_CHANNEL_POSITION_REAR_LEFT
;
498 case PA_CHANNEL_POSITION_SIDE_LEFT
:
499 return PA_CHANNEL_POSITION_SIDE_RIGHT
;
501 case PA_CHANNEL_POSITION_SIDE_RIGHT
:
502 return PA_CHANNEL_POSITION_SIDE_LEFT
;
504 case PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
:
505 return PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
;
507 case PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
:
508 return PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
;
510 case PA_CHANNEL_POSITION_TOP_FRONT_LEFT
:
511 return PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
;
513 case PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
:
514 return PA_CHANNEL_POSITION_TOP_FRONT_LEFT
;
516 case PA_CHANNEL_POSITION_TOP_REAR_LEFT
:
517 return PA_CHANNEL_POSITION_TOP_REAR_RIGHT
;
519 case PA_CHANNEL_POSITION_TOP_REAR_RIGHT
:
520 return PA_CHANNEL_POSITION_TOP_REAR_LEFT
;
527 int pa__init(pa_module
*m
) {
529 pa_sample_spec ss
, sink_input_ss
;
530 pa_channel_map map
, sink_input_map
;
532 pa_sink
*master
=NULL
;
533 pa_sink_input_new_data sink_input_data
;
534 pa_sink_new_data sink_data
;
535 pa_bool_t use_volume_sharing
= TRUE
;
536 pa_bool_t force_flat_volume
= FALSE
;
539 const char *hrir_file
;
540 unsigned i
, j
, found_channel_left
, found_channel_right
;
541 float hrir_sum
, hrir_max
;
544 pa_sample_spec hrir_ss
;
545 pa_channel_map hrir_map
;
547 pa_sample_spec hrir_temp_ss
;
548 pa_memchunk hrir_temp_chunk
;
549 pa_resampler
*resampler
;
551 hrir_temp_chunk
.memblock
= NULL
;
555 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
556 pa_log("Failed to parse module arguments.");
560 if (!(master
= pa_namereg_get(m
->core
, pa_modargs_get_value(ma
, "master", NULL
), PA_NAMEREG_SINK
))) {
561 pa_log("Master sink not found");
567 u
= pa_xnew0(struct userdata
, 1);
571 /* Initialize hrir and input buffer */
572 /* this is the hrir file for the left ear! */
573 hrir_file
= pa_modargs_get_value(ma
, "hrir", NULL
);
575 if (!(hrir_file
= pa_modargs_get_value(ma
, "hrir", NULL
))) {
576 pa_log("The mandatory 'hrir' module argument is missing.");
580 if (pa_sound_file_load(master
->core
->mempool
, hrir_file
, &hrir_temp_ss
, &hrir_map
, &hrir_temp_chunk
, NULL
) < 0) {
581 pa_log("Cannot load hrir file.");
585 /* sample spec / map of hrir */
586 hrir_ss
.format
= PA_SAMPLE_FLOAT32
;
587 hrir_ss
.rate
= master
->sample_spec
.rate
;
588 hrir_ss
.channels
= hrir_temp_ss
.channels
;
590 /* sample spec of sink */
593 if (pa_modargs_get_sample_spec_and_channel_map(ma
, &ss
, &map
, PA_CHANNEL_MAP_DEFAULT
) < 0) {
594 pa_log("Invalid sample format specification or channel map");
597 ss
.format
= PA_SAMPLE_FLOAT32
;
598 hrir_ss
.rate
= ss
.rate
;
599 u
->channels
= ss
.channels
;
601 if (pa_modargs_get_value_boolean(ma
, "use_volume_sharing", &use_volume_sharing
) < 0) {
602 pa_log("use_volume_sharing= expects a boolean argument");
606 if (pa_modargs_get_value_boolean(ma
, "force_flat_volume", &force_flat_volume
) < 0) {
607 pa_log("force_flat_volume= expects a boolean argument");
611 if (use_volume_sharing
&& force_flat_volume
) {
612 pa_log("Flat volume can't be forced when using volume sharing.");
616 /* sample spec / map of sink input */
617 pa_channel_map_init_stereo(&sink_input_map
);
618 sink_input_ss
.channels
= 2;
619 sink_input_ss
.format
= PA_SAMPLE_FLOAT32
;
620 sink_input_ss
.rate
= ss
.rate
;
622 u
->sink_fs
= pa_frame_size(&ss
);
623 u
->fs
= pa_frame_size(&sink_input_ss
);
626 pa_sink_new_data_init(&sink_data
);
627 sink_data
.driver
= __FILE__
;
628 sink_data
.module
= m
;
629 if (!(sink_data
.name
= pa_xstrdup(pa_modargs_get_value(ma
, "sink_name", NULL
))))
630 sink_data
.name
= pa_sprintf_malloc("%s.vsurroundsink", master
->name
);
631 pa_sink_new_data_set_sample_spec(&sink_data
, &ss
);
632 pa_sink_new_data_set_channel_map(&sink_data
, &map
);
633 pa_proplist_sets(sink_data
.proplist
, PA_PROP_DEVICE_MASTER_DEVICE
, master
->name
);
634 pa_proplist_sets(sink_data
.proplist
, PA_PROP_DEVICE_CLASS
, "filter");
635 pa_proplist_sets(sink_data
.proplist
, "device.vsurroundsink.name", sink_data
.name
);
637 if (pa_modargs_get_proplist(ma
, "sink_properties", sink_data
.proplist
, PA_UPDATE_REPLACE
) < 0) {
638 pa_log("Invalid properties");
639 pa_sink_new_data_done(&sink_data
);
643 if ((u
->auto_desc
= !pa_proplist_contains(sink_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
))) {
646 z
= pa_proplist_gets(master
->proplist
, PA_PROP_DEVICE_DESCRIPTION
);
647 pa_proplist_setf(sink_data
.proplist
, PA_PROP_DEVICE_DESCRIPTION
, "Virtual Surround Sink %s on %s", sink_data
.name
, z
? z
: master
->name
);
650 u
->sink
= pa_sink_new(m
->core
, &sink_data
, (master
->flags
& (PA_SINK_LATENCY
|PA_SINK_DYNAMIC_LATENCY
))
651 | (use_volume_sharing
? PA_SINK_SHARE_VOLUME_WITH_MASTER
: 0));
652 pa_sink_new_data_done(&sink_data
);
655 pa_log("Failed to create sink.");
659 u
->sink
->parent
.process_msg
= sink_process_msg_cb
;
660 u
->sink
->set_state
= sink_set_state_cb
;
661 u
->sink
->update_requested_latency
= sink_update_requested_latency_cb
;
662 u
->sink
->request_rewind
= sink_request_rewind_cb
;
663 pa_sink_set_set_mute_callback(u
->sink
, sink_set_mute_cb
);
664 if (!use_volume_sharing
) {
665 pa_sink_set_set_volume_callback(u
->sink
, sink_set_volume_cb
);
666 pa_sink_enable_decibel_volume(u
->sink
, TRUE
);
668 /* Normally this flag would be enabled automatically be we can force it. */
669 if (force_flat_volume
)
670 u
->sink
->flags
|= PA_SINK_FLAT_VOLUME
;
671 u
->sink
->userdata
= u
;
673 pa_sink_set_asyncmsgq(u
->sink
, master
->asyncmsgq
);
675 /* Create sink input */
676 pa_sink_input_new_data_init(&sink_input_data
);
677 sink_input_data
.driver
= __FILE__
;
678 sink_input_data
.module
= m
;
679 pa_sink_input_new_data_set_sink(&sink_input_data
, master
, FALSE
);
680 sink_input_data
.origin_sink
= u
->sink
;
681 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
));
682 pa_proplist_sets(sink_input_data
.proplist
, PA_PROP_MEDIA_ROLE
, "filter");
683 pa_sink_input_new_data_set_sample_spec(&sink_input_data
, &sink_input_ss
);
684 pa_sink_input_new_data_set_channel_map(&sink_input_data
, &sink_input_map
);
686 pa_sink_input_new(&u
->sink_input
, m
->core
, &sink_input_data
);
687 pa_sink_input_new_data_done(&sink_input_data
);
692 u
->sink_input
->pop
= sink_input_pop_cb
;
693 u
->sink_input
->process_rewind
= sink_input_process_rewind_cb
;
694 u
->sink_input
->update_max_rewind
= sink_input_update_max_rewind_cb
;
695 u
->sink_input
->update_max_request
= sink_input_update_max_request_cb
;
696 u
->sink_input
->update_sink_latency_range
= sink_input_update_sink_latency_range_cb
;
697 u
->sink_input
->update_sink_fixed_latency
= sink_input_update_sink_fixed_latency_cb
;
698 u
->sink_input
->kill
= sink_input_kill_cb
;
699 u
->sink_input
->attach
= sink_input_attach_cb
;
700 u
->sink_input
->detach
= sink_input_detach_cb
;
701 u
->sink_input
->state_change
= sink_input_state_change_cb
;
702 u
->sink_input
->may_move_to
= sink_input_may_move_to_cb
;
703 u
->sink_input
->moving
= sink_input_moving_cb
;
704 u
->sink_input
->volume_changed
= use_volume_sharing
? NULL
: sink_input_volume_changed_cb
;
705 u
->sink_input
->mute_changed
= sink_input_mute_changed_cb
;
706 u
->sink_input
->userdata
= u
;
708 u
->sink
->input_to_master
= u
->sink_input
;
710 pa_sink_input_get_silence(u
->sink_input
, &silence
);
711 u
->memblockq
= pa_memblockq_new("module-virtual-surround-sink memblockq", 0, MEMBLOCKQ_MAXLENGTH
, 0, &sink_input_ss
, 1, 1, 0, &silence
);
712 pa_memblock_unref(silence
.memblock
);
715 resampler
= pa_resampler_new(u
->sink
->core
->mempool
, &hrir_temp_ss
, &hrir_map
, &hrir_ss
, &hrir_map
,
716 PA_RESAMPLER_SRC_SINC_BEST_QUALITY
, PA_RESAMPLER_NO_REMAP
);
717 pa_resampler_run(resampler
, &hrir_temp_chunk
, &hrir_temp_chunk
);
718 pa_resampler_free(resampler
);
720 u
->hrir_samples
= hrir_temp_chunk
.length
/ pa_frame_size(&hrir_ss
);
721 u
->hrir_channels
= hrir_ss
.channels
;
724 hrir_data
= (float *) pa_memblock_acquire(hrir_temp_chunk
.memblock
);
725 u
->hrir_data
= (float *) pa_xmalloc(hrir_temp_chunk
.length
);
726 memcpy(u
->hrir_data
, hrir_data
, hrir_temp_chunk
.length
);
727 pa_memblock_release(hrir_temp_chunk
.memblock
);
728 pa_memblock_unref(hrir_temp_chunk
.memblock
);
729 hrir_temp_chunk
.memblock
= NULL
;
731 if (hrir_map
.channels
< map
.channels
) {
732 pa_log("hrir file does not have enough channels!");
736 /* normalize hrir to avoid clipping */
738 for (i
= 0; i
< u
->hrir_samples
; i
++) {
740 for (j
= 0; j
< u
->hrir_channels
; j
++)
741 hrir_sum
+= fabs(u
->hrir_data
[i
* u
->hrir_channels
+ j
]);
743 if (hrir_sum
> hrir_max
)
747 for (i
= 0; i
< u
->hrir_samples
; i
++) {
748 for (j
= 0; j
< u
->hrir_channels
; j
++)
749 u
->hrir_data
[i
* u
->hrir_channels
+ j
] /= hrir_max
* 1.2;
753 /* create mapping between hrir and input */
754 u
->mapping_left
= (unsigned *) pa_xnew0(unsigned, u
->channels
);
755 u
->mapping_right
= (unsigned *) pa_xnew0(unsigned, u
->channels
);
756 for (i
= 0; i
< map
.channels
; i
++) {
757 found_channel_left
= 0;
758 found_channel_right
= 0;
760 for (j
= 0; j
< hrir_map
.channels
; j
++) {
761 if (hrir_map
.map
[j
] == map
.map
[i
]) {
762 u
->mapping_left
[i
] = j
;
763 found_channel_left
= 1;
766 if (hrir_map
.map
[j
] == mirror_channel(map
.map
[i
])) {
767 u
->mapping_right
[i
] = j
;
768 found_channel_right
= 1;
772 if (!found_channel_left
) {
773 pa_log("Cannot find mapping for channel %s", pa_channel_position_to_string(map
.map
[i
]));
776 if (!found_channel_right
) {
777 pa_log("Cannot find mapping for channel %s", pa_channel_position_to_string(mirror_channel(map
.map
[i
])));
782 u
->input_buffer
= pa_xmalloc0(u
->hrir_samples
* u
->sink_fs
);
783 u
->input_buffer_offset
= 0;
785 pa_sink_put(u
->sink
);
786 pa_sink_input_put(u
->sink_input
);
792 if (hrir_temp_chunk
.memblock
)
793 pa_memblock_unref(hrir_temp_chunk
.memblock
);
803 int pa__get_n_used(pa_module
*m
) {
807 pa_assert_se(u
= m
->userdata
);
809 return pa_sink_linked_by(u
->sink
);
812 void pa__done(pa_module
*m
) {
817 if (!(u
= m
->userdata
))
820 /* See comments in sink_input_kill_cb() above regarding
821 * destruction order! */
824 pa_sink_input_unlink(u
->sink_input
);
827 pa_sink_unlink(u
->sink
);
830 pa_sink_input_unref(u
->sink_input
);
833 pa_sink_unref(u
->sink
);
836 pa_memblockq_free(u
->memblockq
);
839 pa_xfree(u
->hrir_data
);
842 pa_xfree(u
->input_buffer
);
845 pa_xfree(u
->mapping_left
);
846 if (u
->mapping_right
)
847 pa_xfree(u
->mapping_right
);