2 This file is part of PulseAudio.
4 Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
5 Copyright 2004-2006, 2008 Lennart Poettering
7 Contact: Marc-Andre Lureau <marc-andre.lureau@nokia.com>
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
32 #include <sys/types.h>
37 #include <pulse/xmalloc.h>
39 #include <pulsecore/core-error.h>
40 #include <pulsecore/module.h>
41 #include <pulsecore/core-util.h>
42 #include <pulsecore/log.h>
43 #include <pulsecore/sink-input.h>
44 #include <pulsecore/core-util.h>
45 #include <pulsecore/macro.h>
47 #include "module-flat-volume-symdef.h"
49 PA_MODULE_AUTHOR("Marc-Andre Lureau");
50 PA_MODULE_DESCRIPTION("Flat volume");
51 PA_MODULE_VERSION(PACKAGE_VERSION
);
52 PA_MODULE_LOAD_ONCE(TRUE
);
56 pa_subscription
*subscription
;
57 pa_hook_slot
*sink_input_set_volume_hook_slot
;
58 pa_hook_slot
*sink_input_fixate_hook_slot
;
61 static void process_input_volume_change(
62 pa_cvolume
*dest_volume
,
63 const pa_cvolume
*dest_virtual_volume
,
64 pa_channel_map
*dest_channel_map
,
70 pa_cvolume max_volume
, sink_volume
;
72 pa_assert(dest_volume
);
73 pa_assert(dest_virtual_volume
);
74 pa_assert(dest_channel_map
);
77 if (!(sink
->flags
& PA_SINK_DECIBEL_VOLUME
))
80 pa_log_debug("Sink input volume changed");
82 max_volume
= *dest_virtual_volume
;
83 pa_cvolume_remap(&max_volume
, dest_channel_map
, &sink
->channel_map
);
85 for (i
= PA_SINK_INPUT(pa_idxset_first(sink
->inputs
, &idx
)); i
; i
= PA_SINK_INPUT(pa_idxset_next(sink
->inputs
, &idx
))) {
86 /* skip this sink-input if we are processing a volume change request */
87 if (this && this == i
)
90 if (pa_cvolume_max(&i
->virtual_volume
) > pa_cvolume_max(&max_volume
)) {
91 max_volume
= i
->virtual_volume
;
92 pa_cvolume_remap(&max_volume
, &i
->channel_map
, &sink
->channel_map
);
96 /* Set the master volume, and normalize inputs */
97 if (!pa_cvolume_equal(&max_volume
, pa_sink_get_volume(sink
, TRUE
))) {
99 pa_sink_set_volume(sink
, &max_volume
);
101 pa_log_debug("sink = %.2f (changed)", (double)pa_cvolume_avg(pa_sink_get_volume(sink
, TRUE
))/PA_VOLUME_NORM
);
103 /* Now, normalize each of the internal volume (client sink-input volume / sink master volume) */
104 for (i
= PA_SINK_INPUT(pa_idxset_first(sink
->inputs
, &idx
)); i
; i
= PA_SINK_INPUT(pa_idxset_next(sink
->inputs
, &idx
))) {
105 /* skip this sink-input if we are processing a volume change request */
106 if (this && this == i
)
109 sink_volume
= max_volume
;
110 pa_cvolume_remap(&sink_volume
, &sink
->channel_map
, &i
->channel_map
);
111 pa_sw_cvolume_divide(&i
->volume
, &i
->virtual_volume
, &sink_volume
);
112 pa_log_debug("sink input { id = %d, flat = %.2f, true = %.2f }",
114 (double)pa_cvolume_avg(&i
->virtual_volume
)/PA_VOLUME_NORM
,
115 (double)pa_cvolume_avg(&i
->volume
)/PA_VOLUME_NORM
);
116 pa_asyncmsgq_post(i
->sink
->asyncmsgq
, PA_MSGOBJECT(i
), PA_SINK_INPUT_MESSAGE_SET_VOLUME
, pa_xnewdup(struct pa_cvolume
, &i
->volume
, 1), 0, NULL
, pa_xfree
);
119 pa_log_debug("sink = %.2f", (double)pa_cvolume_avg(pa_sink_get_volume(sink
, TRUE
))/PA_VOLUME_NORM
);
123 sink_volume
= max_volume
;
124 pa_cvolume_remap(&sink_volume
, &sink
->channel_map
, dest_channel_map
);
125 pa_sw_cvolume_divide(dest_volume
, dest_virtual_volume
, &sink_volume
);
126 pa_log_debug("caller sink input: { id = %d, flat = %.2f, true = %.2f }",
127 this ? (int)this->index
: -1,
128 (double)pa_cvolume_avg(dest_virtual_volume
)/PA_VOLUME_NORM
,
129 (double)pa_cvolume_avg(dest_volume
)/PA_VOLUME_NORM
);
132 static pa_hook_result_t
sink_input_set_volume_hook_callback(pa_core
*c
, pa_sink_input_set_volume_data
*this, struct userdata
*u
) {
134 pa_assert(this->sink_input
);
136 process_input_volume_change(&this->volume
, &this->virtual_volume
, &this->sink_input
->channel_map
,
137 this->sink_input
, this->sink_input
->sink
);
142 static pa_hook_result_t
sink_input_fixate_hook_callback(pa_core
*core
, pa_sink_input_new_data
*this, struct userdata
*u
) {
144 pa_assert(this->sink
);
146 process_input_volume_change(&this->volume
, &this->virtual_volume
, &this->channel_map
,
152 static void subscribe_callback(pa_core
*core
, pa_subscription_event_type_t t
, uint32_t idx
, void *userdata
) {
153 struct userdata
*u
= userdata
;
157 pa_cvolume sink_volume
;
162 if (t
!= (PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_NEW
) &&
163 t
!= (PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
))
166 if (!(sink
= pa_idxset_get_by_index(core
->sinks
, idx
)))
169 if (!(sink
->flags
& PA_SINK_DECIBEL_VOLUME
))
172 pa_log_debug("Sink volume changed");
173 pa_log_debug("sink = %.2f", (double)pa_cvolume_avg(pa_sink_get_volume(sink
, TRUE
)) / PA_VOLUME_NORM
);
175 sink_volume
= *pa_sink_get_volume(sink
, TRUE
);
177 for (i
= PA_SINK_INPUT(pa_idxset_first(sink
->inputs
, &iidx
)); i
; i
= PA_SINK_INPUT(pa_idxset_next(sink
->inputs
, &iidx
))) {
178 pa_cvolume si_volume
;
180 si_volume
= sink_volume
;
181 pa_cvolume_remap(&si_volume
, &sink
->channel_map
, &i
->channel_map
);
182 pa_sw_cvolume_multiply(&i
->virtual_volume
, &i
->volume
, &si_volume
);
183 pa_log_debug("sink input = { id = %d, flat = %.2f, true = %.2f }",
185 (double)pa_cvolume_avg(&i
->virtual_volume
)/PA_VOLUME_NORM
,
186 (double)pa_cvolume_avg(&i
->volume
)/PA_VOLUME_NORM
);
187 pa_subscription_post(i
->sink
->core
, PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
, i
->index
);
191 int pa__init(pa_module
*m
) {
196 u
= pa_xnew(struct userdata
, 1);
199 u
->sink_input_fixate_hook_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_FIXATE
], PA_HOOK_LATE
, (pa_hook_cb_t
) sink_input_fixate_hook_callback
, u
);
200 u
->sink_input_set_volume_hook_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_SET_VOLUME
], PA_HOOK_LATE
, (pa_hook_cb_t
) sink_input_set_volume_hook_callback
, u
);
202 u
->subscription
= pa_subscription_new(m
->core
, PA_SUBSCRIPTION_MASK_SINK
, subscribe_callback
, u
);
207 void pa__done(pa_module
*m
) {
212 if (!(u
= m
->userdata
))
216 pa_subscription_free(u
->subscription
);
218 if (u
->sink_input_set_volume_hook_slot
)
219 pa_hook_slot_free(u
->sink_input_set_volume_hook_slot
);
220 if (u
->sink_input_fixate_hook_slot
)
221 pa_hook_slot_free(u
->sink_input_fixate_hook_slot
);