]> code.delx.au - pulseaudio/blob - src/pulse/volume.c
add new API call pa_cvolume_compatible()
[pulseaudio] / src / pulse / volume.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 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 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 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <string.h>
28
29 #include <pulse/i18n.h>
30 #include <pulsecore/core-util.h>
31 #include <pulsecore/macro.h>
32
33 #include "volume.h"
34
35 int pa_cvolume_equal(const pa_cvolume *a, const pa_cvolume *b) {
36 int i;
37 pa_assert(a);
38 pa_assert(b);
39
40 if (a->channels != b->channels)
41 return 0;
42
43 for (i = 0; i < a->channels; i++)
44 if (a->values[i] != b->values[i])
45 return 0;
46
47 return 1;
48 }
49
50 pa_cvolume* pa_cvolume_init(pa_cvolume *a) {
51 unsigned c;
52
53 pa_assert(a);
54
55 a->channels = 0;
56
57 for (c = 0; c < PA_CHANNELS_MAX; c++)
58 a->values[c] = (pa_volume_t) -1;
59
60 return a;
61 }
62
63 pa_cvolume* pa_cvolume_set(pa_cvolume *a, unsigned channels, pa_volume_t v) {
64 int i;
65
66 pa_assert(a);
67 pa_assert(channels > 0);
68 pa_assert(channels <= PA_CHANNELS_MAX);
69
70 a->channels = (uint8_t) channels;
71
72 for (i = 0; i < a->channels; i++)
73 a->values[i] = v;
74
75 return a;
76 }
77
78 pa_volume_t pa_cvolume_avg(const pa_cvolume *a) {
79 uint64_t sum = 0;
80 int i;
81 pa_assert(a);
82
83 for (i = 0; i < a->channels; i++)
84 sum += a->values[i];
85
86 sum /= a->channels;
87
88 return (pa_volume_t) sum;
89 }
90
91 pa_volume_t pa_cvolume_max(const pa_cvolume *a) {
92 pa_volume_t m = 0;
93 int i;
94 pa_assert(a);
95
96 for (i = 0; i < a->channels; i++)
97 if (a->values[i] > m)
98 m = a->values[i];
99
100 return m;
101 }
102
103 pa_volume_t pa_sw_volume_multiply(pa_volume_t a, pa_volume_t b) {
104 return pa_sw_volume_from_linear(pa_sw_volume_to_linear(a) * pa_sw_volume_to_linear(b));
105 }
106
107 #define USER_DECIBEL_RANGE 60
108
109 pa_volume_t pa_sw_volume_from_dB(double dB) {
110 if (isinf(dB) < 0 || dB <= -USER_DECIBEL_RANGE)
111 return PA_VOLUME_MUTED;
112
113 return (pa_volume_t) lrint((dB/USER_DECIBEL_RANGE+1)*PA_VOLUME_NORM);
114 }
115
116 double pa_sw_volume_to_dB(pa_volume_t v) {
117 if (v == PA_VOLUME_MUTED)
118 return PA_DECIBEL_MININFTY;
119
120 return ((double) v/PA_VOLUME_NORM-1)*USER_DECIBEL_RANGE;
121 }
122
123 pa_volume_t pa_sw_volume_from_linear(double v) {
124
125 if (v <= 0)
126 return PA_VOLUME_MUTED;
127
128 if (v > .999 && v < 1.001)
129 return PA_VOLUME_NORM;
130
131 return pa_sw_volume_from_dB(20*log10(v));
132 }
133
134 double pa_sw_volume_to_linear(pa_volume_t v) {
135
136 if (v == PA_VOLUME_MUTED)
137 return 0;
138
139 return pow(10.0, pa_sw_volume_to_dB(v)/20.0);
140 }
141
142 char *pa_cvolume_snprint(char *s, size_t l, const pa_cvolume *c) {
143 unsigned channel;
144 pa_bool_t first = TRUE;
145 char *e;
146
147 pa_assert(s);
148 pa_assert(l > 0);
149 pa_assert(c);
150
151 pa_init_i18n();
152
153 if (!pa_cvolume_valid(c)) {
154 pa_snprintf(s, l, _("(invalid)"));
155 return s;
156 }
157
158 *(e = s) = 0;
159
160 for (channel = 0; channel < c->channels && l > 1; channel++) {
161 l -= pa_snprintf(e, l, "%s%u: %3u%%",
162 first ? "" : " ",
163 channel,
164 (c->values[channel]*100)/PA_VOLUME_NORM);
165
166 e = strchr(e, 0);
167 first = FALSE;
168 }
169
170 return s;
171 }
172
173 char *pa_cvolume_snprint_dB(char *s, size_t l, const pa_cvolume *c) {
174 unsigned channel;
175 pa_bool_t first = TRUE;
176 char *e;
177
178 pa_assert(s);
179 pa_assert(l > 0);
180 pa_assert(c);
181
182 pa_init_i18n();
183
184 if (!pa_cvolume_valid(c)) {
185 pa_snprintf(s, l, _("(invalid)"));
186 return s;
187 }
188
189 *(e = s) = 0;
190
191 for (channel = 0; channel < c->channels && l > 1; channel++) {
192 l -= pa_snprintf(e, l, "%s%u: %0.2f dB",
193 first ? "" : " ",
194 channel,
195 pa_sw_volume_to_dB(c->values[channel]));
196
197 e = strchr(e, 0);
198 first = FALSE;
199 }
200
201 return s;
202 }
203
204 /** Return non-zero if the volume of all channels is equal to the specified value */
205 int pa_cvolume_channels_equal_to(const pa_cvolume *a, pa_volume_t v) {
206 unsigned c;
207 pa_assert(a);
208
209 for (c = 0; c < a->channels; c++)
210 if (a->values[c] != v)
211 return 0;
212
213 return 1;
214 }
215
216 pa_cvolume *pa_sw_cvolume_multiply(pa_cvolume *dest, const pa_cvolume *a, const pa_cvolume *b) {
217 unsigned i;
218
219 pa_assert(dest);
220 pa_assert(a);
221 pa_assert(b);
222
223 for (i = 0; i < a->channels && i < b->channels && i < PA_CHANNELS_MAX; i++) {
224
225 dest->values[i] = pa_sw_volume_multiply(
226 i < a->channels ? a->values[i] : PA_VOLUME_NORM,
227 i < b->channels ? b->values[i] : PA_VOLUME_NORM);
228 }
229
230 dest->channels = (uint8_t) i;
231
232 return dest;
233 }
234
235 int pa_cvolume_valid(const pa_cvolume *v) {
236 pa_assert(v);
237
238 if (v->channels <= 0 || v->channels > PA_CHANNELS_MAX)
239 return 0;
240
241 return 1;
242 }
243
244 static pa_bool_t on_left(pa_channel_position_t p) {
245
246 return
247 p == PA_CHANNEL_POSITION_FRONT_LEFT ||
248 p == PA_CHANNEL_POSITION_REAR_LEFT ||
249 p == PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER ||
250 p == PA_CHANNEL_POSITION_SIDE_LEFT ||
251 p == PA_CHANNEL_POSITION_TOP_FRONT_LEFT ||
252 p == PA_CHANNEL_POSITION_TOP_REAR_LEFT;
253 }
254
255 static pa_bool_t on_right(pa_channel_position_t p) {
256
257 return
258 p == PA_CHANNEL_POSITION_FRONT_RIGHT ||
259 p == PA_CHANNEL_POSITION_REAR_RIGHT ||
260 p == PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER ||
261 p == PA_CHANNEL_POSITION_SIDE_RIGHT ||
262 p == PA_CHANNEL_POSITION_TOP_FRONT_RIGHT ||
263 p == PA_CHANNEL_POSITION_TOP_REAR_RIGHT;
264 }
265
266 static pa_bool_t on_center(pa_channel_position_t p) {
267
268 return
269 p == PA_CHANNEL_POSITION_FRONT_CENTER ||
270 p == PA_CHANNEL_POSITION_REAR_CENTER ||
271 p == PA_CHANNEL_POSITION_TOP_CENTER ||
272 p == PA_CHANNEL_POSITION_TOP_FRONT_CENTER ||
273 p == PA_CHANNEL_POSITION_TOP_REAR_CENTER;
274 }
275
276 static pa_bool_t on_lfe(pa_channel_position_t p) {
277 return
278 p == PA_CHANNEL_POSITION_LFE;
279 }
280
281 pa_cvolume *pa_cvolume_remap(pa_cvolume *v, pa_channel_map *from, pa_channel_map *to) {
282 int a, b;
283 pa_cvolume result;
284
285 pa_assert(v);
286 pa_assert(from);
287 pa_assert(to);
288 pa_assert(v->channels == from->channels);
289
290 if (pa_channel_map_equal(from, to))
291 return v;
292
293 result.channels = to->channels;
294
295 for (b = 0; b < to->channels; b++) {
296 pa_volume_t k = 0;
297 int n = 0;
298
299 for (a = 0; a < from->channels; a++)
300 if (from->map[a] == to->map[b]) {
301 k += v->values[a];
302 n ++;
303 }
304
305 if (n <= 0) {
306 for (a = 0; a < from->channels; a++)
307 if ((on_left(from->map[a]) && on_left(to->map[b])) ||
308 (on_right(from->map[a]) && on_right(to->map[b])) ||
309 (on_center(from->map[a]) && on_center(to->map[b])) ||
310 (on_lfe(from->map[a]) && on_lfe(to->map[b]))) {
311
312 k += v->values[a];
313 n ++;
314 }
315 }
316
317 if (n <= 0)
318 k = pa_cvolume_avg(v);
319 else
320 k /= n;
321
322 result.values[b] = k;
323 }
324
325 *v = result;
326 return v;
327 }
328
329 int pa_cvolume_compatible(const pa_cvolume *v, const pa_sample_spec *ss) {
330
331 pa_assert(v);
332 pa_assert(ss);
333
334 if (!pa_cvolume_valid(v))
335 return 0;
336
337 if (!pa_sample_spec_valid(ss))
338 return 0;
339
340 return v->channels == ss->channels;
341 }