]> code.delx.au - pulseaudio/blob - src/pulse/volume.c
rename pa_cvolume_snprint_dB to pa_sw_cvolume_snprint_dB since it is
[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_sw_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 unsigned c;
237
238 pa_assert(v);
239
240 if (v->channels <= 0 || v->channels > PA_CHANNELS_MAX)
241 return 0;
242
243 for (c = 0; c < v->channels; c++)
244 if (v->values[c] == (pa_volume_t) -1)
245 return 0;
246
247 return 1;
248 }
249
250 static pa_bool_t on_left(pa_channel_position_t p) {
251
252 return
253 p == PA_CHANNEL_POSITION_FRONT_LEFT ||
254 p == PA_CHANNEL_POSITION_REAR_LEFT ||
255 p == PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER ||
256 p == PA_CHANNEL_POSITION_SIDE_LEFT ||
257 p == PA_CHANNEL_POSITION_TOP_FRONT_LEFT ||
258 p == PA_CHANNEL_POSITION_TOP_REAR_LEFT;
259 }
260
261 static pa_bool_t on_right(pa_channel_position_t p) {
262
263 return
264 p == PA_CHANNEL_POSITION_FRONT_RIGHT ||
265 p == PA_CHANNEL_POSITION_REAR_RIGHT ||
266 p == PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER ||
267 p == PA_CHANNEL_POSITION_SIDE_RIGHT ||
268 p == PA_CHANNEL_POSITION_TOP_FRONT_RIGHT ||
269 p == PA_CHANNEL_POSITION_TOP_REAR_RIGHT;
270 }
271
272 static pa_bool_t on_center(pa_channel_position_t p) {
273
274 return
275 p == PA_CHANNEL_POSITION_FRONT_CENTER ||
276 p == PA_CHANNEL_POSITION_REAR_CENTER ||
277 p == PA_CHANNEL_POSITION_TOP_CENTER ||
278 p == PA_CHANNEL_POSITION_TOP_FRONT_CENTER ||
279 p == PA_CHANNEL_POSITION_TOP_REAR_CENTER;
280 }
281
282 static pa_bool_t on_lfe(pa_channel_position_t p) {
283 return
284 p == PA_CHANNEL_POSITION_LFE;
285 }
286
287 pa_cvolume *pa_cvolume_remap(pa_cvolume *v, pa_channel_map *from, pa_channel_map *to) {
288 int a, b;
289 pa_cvolume result;
290
291 pa_assert(v);
292 pa_assert(from);
293 pa_assert(to);
294 pa_assert(v->channels == from->channels);
295
296 if (pa_channel_map_equal(from, to))
297 return v;
298
299 result.channels = to->channels;
300
301 for (b = 0; b < to->channels; b++) {
302 pa_volume_t k = 0;
303 int n = 0;
304
305 for (a = 0; a < from->channels; a++)
306 if (from->map[a] == to->map[b]) {
307 k += v->values[a];
308 n ++;
309 }
310
311 if (n <= 0) {
312 for (a = 0; a < from->channels; a++)
313 if ((on_left(from->map[a]) && on_left(to->map[b])) ||
314 (on_right(from->map[a]) && on_right(to->map[b])) ||
315 (on_center(from->map[a]) && on_center(to->map[b])) ||
316 (on_lfe(from->map[a]) && on_lfe(to->map[b]))) {
317
318 k += v->values[a];
319 n ++;
320 }
321 }
322
323 if (n <= 0)
324 k = pa_cvolume_avg(v);
325 else
326 k /= n;
327
328 result.values[b] = k;
329 }
330
331 *v = result;
332 return v;
333 }
334
335 int pa_cvolume_compatible(const pa_cvolume *v, const pa_sample_spec *ss) {
336
337 pa_assert(v);
338 pa_assert(ss);
339
340 if (!pa_cvolume_valid(v))
341 return 0;
342
343 if (!pa_sample_spec_valid(ss))
344 return 0;
345
346 return v->channels == ss->channels;
347 }