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