if a volume or channel map is invalid show so when printing it
[profile/ivi/pulseaudio-panda.git] / 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_set(pa_cvolume *a, unsigned channels, pa_volume_t v) {
51     int i;
52
53     pa_assert(a);
54     pa_assert(channels > 0);
55     pa_assert(channels <= PA_CHANNELS_MAX);
56
57     a->channels = (uint8_t) channels;
58
59     for (i = 0; i < a->channels; i++)
60         a->values[i] = v;
61
62     return a;
63 }
64
65 pa_volume_t pa_cvolume_avg(const pa_cvolume *a) {
66     uint64_t sum = 0;
67     int i;
68     pa_assert(a);
69
70     for (i = 0; i < a->channels; i++)
71         sum += a->values[i];
72
73     sum /= a->channels;
74
75     return (pa_volume_t) sum;
76 }
77
78 pa_volume_t pa_cvolume_max(const pa_cvolume *a) {
79     pa_volume_t m = 0;
80     int i;
81     pa_assert(a);
82
83     for (i = 0; i < a->channels; i++)
84         if (a->values[i] > m)
85             m = a->values[i];
86
87     return m;
88 }
89
90 pa_volume_t pa_sw_volume_multiply(pa_volume_t a, pa_volume_t b) {
91     return pa_sw_volume_from_linear(pa_sw_volume_to_linear(a)* pa_sw_volume_to_linear(b));
92 }
93
94 #define USER_DECIBEL_RANGE 60
95
96 pa_volume_t pa_sw_volume_from_dB(double dB) {
97     if (isinf(dB) < 0 || dB <= -USER_DECIBEL_RANGE)
98         return PA_VOLUME_MUTED;
99
100     return (pa_volume_t) lrint((dB/USER_DECIBEL_RANGE+1)*PA_VOLUME_NORM);
101 }
102
103 double pa_sw_volume_to_dB(pa_volume_t v) {
104     if (v == PA_VOLUME_MUTED)
105         return PA_DECIBEL_MININFTY;
106
107     return ((double) v/PA_VOLUME_NORM-1)*USER_DECIBEL_RANGE;
108 }
109
110 pa_volume_t pa_sw_volume_from_linear(double v) {
111
112     if (v <= 0)
113         return PA_VOLUME_MUTED;
114
115     if (v > .999 && v < 1.001)
116         return PA_VOLUME_NORM;
117
118     return pa_sw_volume_from_dB(20*log10(v));
119 }
120
121 double pa_sw_volume_to_linear(pa_volume_t v) {
122
123     if (v == PA_VOLUME_MUTED)
124         return 0;
125
126     return pow(10.0, pa_sw_volume_to_dB(v)/20.0);
127 }
128
129 char *pa_cvolume_snprint(char *s, size_t l, const pa_cvolume *c) {
130     unsigned channel;
131     int first = 1;
132     char *e;
133
134     pa_assert(s);
135     pa_assert(l > 0);
136     pa_assert(c);
137
138     pa_init_i18n();
139
140     if (!pa_cvolume_valid(c)) {
141         pa_snprintf(s, l, _("(invalid)"));
142         return s;
143     }
144
145     *(e = s) = 0;
146
147     for (channel = 0; channel < c->channels && l > 1; channel++) {
148         l -= pa_snprintf(e, l, "%s%u: %3u%%",
149                       first ? "" : " ",
150                       channel,
151                       (c->values[channel]*100)/PA_VOLUME_NORM);
152
153         e = strchr(e, 0);
154         first = 0;
155     }
156
157     return s;
158 }
159
160 /** Return non-zero if the volume of all channels is equal to the specified value */
161 int pa_cvolume_channels_equal_to(const pa_cvolume *a, pa_volume_t v) {
162     unsigned c;
163     pa_assert(a);
164
165     for (c = 0; c < a->channels; c++)
166         if (a->values[c] != v)
167             return 0;
168
169     return 1;
170 }
171
172 pa_cvolume *pa_sw_cvolume_multiply(pa_cvolume *dest, const pa_cvolume *a, const pa_cvolume *b) {
173     unsigned i;
174
175     pa_assert(dest);
176     pa_assert(a);
177     pa_assert(b);
178
179     for (i = 0; i < a->channels && i < b->channels && i < PA_CHANNELS_MAX; i++) {
180
181         dest->values[i] = pa_sw_volume_multiply(
182             i < a->channels ? a->values[i] : PA_VOLUME_NORM,
183             i < b->channels ? b->values[i] : PA_VOLUME_NORM);
184     }
185
186     dest->channels = (uint8_t) i;
187
188     return dest;
189 }
190
191 int pa_cvolume_valid(const pa_cvolume *v) {
192     pa_assert(v);
193
194     if (v->channels <= 0 || v->channels > PA_CHANNELS_MAX)
195         return 0;
196
197     return 1;
198 }
199
200 static pa_bool_t on_left(pa_channel_position_t p) {
201
202     return
203         p == PA_CHANNEL_POSITION_FRONT_LEFT ||
204         p == PA_CHANNEL_POSITION_REAR_LEFT ||
205         p == PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER ||
206         p == PA_CHANNEL_POSITION_SIDE_LEFT ||
207         p == PA_CHANNEL_POSITION_TOP_FRONT_LEFT ||
208         p == PA_CHANNEL_POSITION_TOP_REAR_LEFT;
209 }
210
211 static pa_bool_t on_right(pa_channel_position_t p) {
212
213     return
214         p == PA_CHANNEL_POSITION_FRONT_RIGHT ||
215         p == PA_CHANNEL_POSITION_REAR_RIGHT ||
216         p == PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER ||
217         p == PA_CHANNEL_POSITION_SIDE_RIGHT ||
218         p == PA_CHANNEL_POSITION_TOP_FRONT_RIGHT ||
219         p == PA_CHANNEL_POSITION_TOP_REAR_RIGHT;
220 }
221
222 static pa_bool_t on_center(pa_channel_position_t p) {
223
224     return
225         p == PA_CHANNEL_POSITION_FRONT_CENTER ||
226         p == PA_CHANNEL_POSITION_REAR_CENTER ||
227         p == PA_CHANNEL_POSITION_TOP_CENTER ||
228         p == PA_CHANNEL_POSITION_TOP_FRONT_CENTER ||
229         p == PA_CHANNEL_POSITION_TOP_REAR_CENTER;
230 }
231
232 static pa_bool_t on_lfe(pa_channel_position_t p) {
233     return
234         p == PA_CHANNEL_POSITION_LFE;
235 }
236
237 pa_cvolume *pa_cvolume_remap(pa_cvolume *v, pa_channel_map *from, pa_channel_map *to) {
238     int a, b;
239     pa_cvolume result;
240
241     pa_assert(v);
242     pa_assert(from);
243     pa_assert(to);
244     pa_assert(v->channels == from->channels);
245
246     if (pa_channel_map_equal(from, to))
247         return v;
248
249     result.channels = to->channels;
250
251     for (b = 0; b < to->channels; b++) {
252         pa_volume_t k = 0;
253         int n = 0;
254
255         for (a = 0; a < from->channels; a++)
256             if (from->map[a] == to->map[b]) {
257                 k += v->values[a];
258                 n ++;
259             }
260
261         if (n <= 0) {
262             for (a = 0; a < from->channels; a++)
263                 if ((on_left(from->map[a]) && on_left(to->map[b])) ||
264                     (on_right(from->map[a]) && on_right(to->map[b])) ||
265                     (on_center(from->map[a]) && on_center(to->map[b])) ||
266                     (on_lfe(from->map[a]) && on_lfe(to->map[b]))) {
267
268                     k += v->values[a];
269                     n ++;
270                 }
271         }
272
273         if (n <= 0)
274             k = pa_cvolume_avg(v);
275         else
276             k /= n;
277
278         result.values[b] = k;
279     }
280
281     *v = result;
282     return v;
283 }