comparison libaf/af_volnorm.c @ 29263:0f1b5b68af32

whitespace cosmetics: Remove all trailing whitespace.
author diego
date Wed, 13 May 2009 02:58:57 +0000
parents 72d0b1444141
children 8fa2f43cb760
comparison
equal deleted inserted replaced
29262:7d545a6b8aff 29263:0f1b5b68af32
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */ 19 */
20 20
21 #include <stdio.h> 21 #include <stdio.h>
22 #include <stdlib.h> 22 #include <stdlib.h>
23 #include <string.h> 23 #include <string.h>
24 24
25 #include <inttypes.h> 25 #include <inttypes.h>
26 #include <math.h> 26 #include <math.h>
27 #include <limits.h> 27 #include <limits.h>
28 28
78 }af_volnorm_t; 78 }af_volnorm_t;
79 79
80 // Initialization and runtime control 80 // Initialization and runtime control
81 static int control(struct af_instance_s* af, int cmd, void* arg) 81 static int control(struct af_instance_s* af, int cmd, void* arg)
82 { 82 {
83 af_volnorm_t* s = (af_volnorm_t*)af->setup; 83 af_volnorm_t* s = (af_volnorm_t*)af->setup;
84 84
85 switch(cmd){ 85 switch(cmd){
86 case AF_CONTROL_REINIT: 86 case AF_CONTROL_REINIT:
87 // Sanity check 87 // Sanity check
88 if(!arg) return AF_ERROR; 88 if(!arg) return AF_ERROR;
89 89
90 af->data->rate = ((af_data_t*)arg)->rate; 90 af->data->rate = ((af_data_t*)arg)->rate;
91 af->data->nch = ((af_data_t*)arg)->nch; 91 af->data->nch = ((af_data_t*)arg)->nch;
92 92
93 if(((af_data_t*)arg)->format == (AF_FORMAT_S16_NE)){ 93 if(((af_data_t*)arg)->format == (AF_FORMAT_S16_NE)){
94 af->data->format = AF_FORMAT_S16_NE; 94 af->data->format = AF_FORMAT_S16_NE;
95 af->data->bps = 2; 95 af->data->bps = 2;
96 }else{ 96 }else{
97 af->data->format = AF_FORMAT_FLOAT_NE; 97 af->data->format = AF_FORMAT_FLOAT_NE;
111 } 111 }
112 } 112 }
113 return AF_UNKNOWN; 113 return AF_UNKNOWN;
114 } 114 }
115 115
116 // Deallocate memory 116 // Deallocate memory
117 static void uninit(struct af_instance_s* af) 117 static void uninit(struct af_instance_s* af)
118 { 118 {
119 if(af->data) 119 if(af->data)
120 free(af->data); 120 free(af->data);
121 if(af->setup) 121 if(af->setup)
127 register int i = 0; 127 register int i = 0;
128 int16_t *data = (int16_t*)c->audio; // Audio data 128 int16_t *data = (int16_t*)c->audio; // Audio data
129 int len = c->len/2; // Number of samples 129 int len = c->len/2; // Number of samples
130 float curavg = 0.0, newavg, neededmul; 130 float curavg = 0.0, newavg, neededmul;
131 int tmp; 131 int tmp;
132 132
133 for (i = 0; i < len; i++) 133 for (i = 0; i < len; i++)
134 { 134 {
135 tmp = data[i]; 135 tmp = data[i];
136 curavg += tmp * tmp; 136 curavg += tmp * tmp;
137 } 137 }
138 curavg = sqrt(curavg / (float) len); 138 curavg = sqrt(curavg / (float) len);
139 139
140 // Evaluate an adequate 'mul' coefficient based on previous state, current 140 // Evaluate an adequate 'mul' coefficient based on previous state, current
141 // samples level, etc 141 // samples level, etc
142 142
143 if (curavg > SIL_S16) 143 if (curavg > SIL_S16)
144 { 144 {
145 neededmul = s->mid_s16 / (curavg * s->mul); 145 neededmul = s->mid_s16 / (curavg * s->mul);
146 s->mul = (1.0 - SMOOTH_MUL) * s->mul + SMOOTH_MUL * neededmul; 146 s->mul = (1.0 - SMOOTH_MUL) * s->mul + SMOOTH_MUL * neededmul;
147 147
148 // clamp the mul coefficient 148 // clamp the mul coefficient
149 s->mul = clamp(s->mul, MUL_MIN, MUL_MAX); 149 s->mul = clamp(s->mul, MUL_MIN, MUL_MAX);
150 } 150 }
151 151
152 // Scale & clamp the samples 152 // Scale & clamp the samples
153 for (i = 0; i < len; i++) 153 for (i = 0; i < len; i++)
154 { 154 {
155 tmp = s->mul * data[i]; 155 tmp = s->mul * data[i];
156 tmp = clamp(tmp, SHRT_MIN, SHRT_MAX); 156 tmp = clamp(tmp, SHRT_MIN, SHRT_MAX);
157 data[i] = tmp; 157 data[i] = tmp;
158 } 158 }
159 159
160 // Evaulation of newavg (not 100% accurate because of values clamping) 160 // Evaulation of newavg (not 100% accurate because of values clamping)
161 newavg = s->mul * curavg; 161 newavg = s->mul * curavg;
162 162
163 // Stores computed values for future smoothing 163 // Stores computed values for future smoothing
164 s->lastavg = (1.0 - SMOOTH_LASTAVG) * s->lastavg + SMOOTH_LASTAVG * newavg; 164 s->lastavg = (1.0 - SMOOTH_LASTAVG) * s->lastavg + SMOOTH_LASTAVG * newavg;
165 } 165 }
166 166
167 static void method1_float(af_volnorm_t *s, af_data_t *c) 167 static void method1_float(af_volnorm_t *s, af_data_t *c)
168 { 168 {
169 register int i = 0; 169 register int i = 0;
170 float *data = (float*)c->audio; // Audio data 170 float *data = (float*)c->audio; // Audio data
171 int len = c->len/4; // Number of samples 171 int len = c->len/4; // Number of samples
172 float curavg = 0.0, newavg, neededmul, tmp; 172 float curavg = 0.0, newavg, neededmul, tmp;
173 173
174 for (i = 0; i < len; i++) 174 for (i = 0; i < len; i++)
175 { 175 {
176 tmp = data[i]; 176 tmp = data[i];
177 curavg += tmp * tmp; 177 curavg += tmp * tmp;
178 } 178 }
179 curavg = sqrt(curavg / (float) len); 179 curavg = sqrt(curavg / (float) len);
180 180
181 // Evaluate an adequate 'mul' coefficient based on previous state, current 181 // Evaluate an adequate 'mul' coefficient based on previous state, current
182 // samples level, etc 182 // samples level, etc
183 183
184 if (curavg > SIL_FLOAT) // FIXME 184 if (curavg > SIL_FLOAT) // FIXME
185 { 185 {
186 neededmul = s->mid_float / (curavg * s->mul); 186 neededmul = s->mid_float / (curavg * s->mul);
187 s->mul = (1.0 - SMOOTH_MUL) * s->mul + SMOOTH_MUL * neededmul; 187 s->mul = (1.0 - SMOOTH_MUL) * s->mul + SMOOTH_MUL * neededmul;
188 188
189 // clamp the mul coefficient 189 // clamp the mul coefficient
190 s->mul = clamp(s->mul, MUL_MIN, MUL_MAX); 190 s->mul = clamp(s->mul, MUL_MIN, MUL_MAX);
191 } 191 }
192 192
193 // Scale & clamp the samples 193 // Scale & clamp the samples
194 for (i = 0; i < len; i++) 194 for (i = 0; i < len; i++)
195 data[i] *= s->mul; 195 data[i] *= s->mul;
196 196
197 // Evaulation of newavg (not 100% accurate because of values clamping) 197 // Evaulation of newavg (not 100% accurate because of values clamping)
198 newavg = s->mul * curavg; 198 newavg = s->mul * curavg;
199 199
200 // Stores computed values for future smoothing 200 // Stores computed values for future smoothing
201 s->lastavg = (1.0 - SMOOTH_LASTAVG) * s->lastavg + SMOOTH_LASTAVG * newavg; 201 s->lastavg = (1.0 - SMOOTH_LASTAVG) * s->lastavg + SMOOTH_LASTAVG * newavg;
202 } 202 }
203 203
204 static void method2_int16(af_volnorm_t *s, af_data_t *c) 204 static void method2_int16(af_volnorm_t *s, af_data_t *c)
206 register int i = 0; 206 register int i = 0;
207 int16_t *data = (int16_t*)c->audio; // Audio data 207 int16_t *data = (int16_t*)c->audio; // Audio data
208 int len = c->len/2; // Number of samples 208 int len = c->len/2; // Number of samples
209 float curavg = 0.0, newavg, avg = 0.0; 209 float curavg = 0.0, newavg, avg = 0.0;
210 int tmp, totallen = 0; 210 int tmp, totallen = 0;
211 211
212 for (i = 0; i < len; i++) 212 for (i = 0; i < len; i++)
213 { 213 {
214 tmp = data[i]; 214 tmp = data[i];
215 curavg += tmp * tmp; 215 curavg += tmp * tmp;
216 } 216 }
217 curavg = sqrt(curavg / (float) len); 217 curavg = sqrt(curavg / (float) len);
218 218
219 // Evaluate an adequate 'mul' coefficient based on previous state, current 219 // Evaluate an adequate 'mul' coefficient based on previous state, current
220 // samples level, etc 220 // samples level, etc
221 for (i = 0; i < NSAMPLES; i++) 221 for (i = 0; i < NSAMPLES; i++)
222 { 222 {
223 avg += s->mem[i].avg * (float)s->mem[i].len; 223 avg += s->mem[i].avg * (float)s->mem[i].len;
224 totallen += s->mem[i].len; 224 totallen += s->mem[i].len;
225 } 225 }
226 226
227 if (totallen > MIN_SAMPLE_SIZE) 227 if (totallen > MIN_SAMPLE_SIZE)
228 { 228 {
229 avg /= (float)totallen; 229 avg /= (float)totallen;
230 if (avg >= SIL_S16) 230 if (avg >= SIL_S16)
231 { 231 {
232 s->mul = s->mid_s16 / avg; 232 s->mul = s->mid_s16 / avg;
233 s->mul = clamp(s->mul, MUL_MIN, MUL_MAX); 233 s->mul = clamp(s->mul, MUL_MIN, MUL_MAX);
234 } 234 }
235 } 235 }
236 236
237 // Scale & clamp the samples 237 // Scale & clamp the samples
238 for (i = 0; i < len; i++) 238 for (i = 0; i < len; i++)
239 { 239 {
240 tmp = s->mul * data[i]; 240 tmp = s->mul * data[i];
241 tmp = clamp(tmp, SHRT_MIN, SHRT_MAX); 241 tmp = clamp(tmp, SHRT_MIN, SHRT_MAX);
242 data[i] = tmp; 242 data[i] = tmp;
243 } 243 }
244 244
245 // Evaulation of newavg (not 100% accurate because of values clamping) 245 // Evaulation of newavg (not 100% accurate because of values clamping)
246 newavg = s->mul * curavg; 246 newavg = s->mul * curavg;
247 247
248 // Stores computed values for future smoothing 248 // Stores computed values for future smoothing
249 s->mem[s->idx].len = len; 249 s->mem[s->idx].len = len;
250 s->mem[s->idx].avg = newavg; 250 s->mem[s->idx].avg = newavg;
251 s->idx = (s->idx + 1) % NSAMPLES; 251 s->idx = (s->idx + 1) % NSAMPLES;
252 } 252 }
256 register int i = 0; 256 register int i = 0;
257 float *data = (float*)c->audio; // Audio data 257 float *data = (float*)c->audio; // Audio data
258 int len = c->len/4; // Number of samples 258 int len = c->len/4; // Number of samples
259 float curavg = 0.0, newavg, avg = 0.0, tmp; 259 float curavg = 0.0, newavg, avg = 0.0, tmp;
260 int totallen = 0; 260 int totallen = 0;
261 261
262 for (i = 0; i < len; i++) 262 for (i = 0; i < len; i++)
263 { 263 {
264 tmp = data[i]; 264 tmp = data[i];
265 curavg += tmp * tmp; 265 curavg += tmp * tmp;
266 } 266 }
267 curavg = sqrt(curavg / (float) len); 267 curavg = sqrt(curavg / (float) len);
268 268
269 // Evaluate an adequate 'mul' coefficient based on previous state, current 269 // Evaluate an adequate 'mul' coefficient based on previous state, current
270 // samples level, etc 270 // samples level, etc
271 for (i = 0; i < NSAMPLES; i++) 271 for (i = 0; i < NSAMPLES; i++)
272 { 272 {
273 avg += s->mem[i].avg * (float)s->mem[i].len; 273 avg += s->mem[i].avg * (float)s->mem[i].len;
274 totallen += s->mem[i].len; 274 totallen += s->mem[i].len;
275 } 275 }
276 276
277 if (totallen > MIN_SAMPLE_SIZE) 277 if (totallen > MIN_SAMPLE_SIZE)
278 { 278 {
279 avg /= (float)totallen; 279 avg /= (float)totallen;
280 if (avg >= SIL_FLOAT) 280 if (avg >= SIL_FLOAT)
281 { 281 {
282 s->mul = s->mid_float / avg; 282 s->mul = s->mid_float / avg;
283 s->mul = clamp(s->mul, MUL_MIN, MUL_MAX); 283 s->mul = clamp(s->mul, MUL_MIN, MUL_MAX);
284 } 284 }
285 } 285 }
286 286
287 // Scale & clamp the samples 287 // Scale & clamp the samples
288 for (i = 0; i < len; i++) 288 for (i = 0; i < len; i++)
289 data[i] *= s->mul; 289 data[i] *= s->mul;
290 290
291 // Evaulation of newavg (not 100% accurate because of values clamping) 291 // Evaulation of newavg (not 100% accurate because of values clamping)
292 newavg = s->mul * curavg; 292 newavg = s->mul * curavg;
293 293
294 // Stores computed values for future smoothing 294 // Stores computed values for future smoothing
295 s->mem[s->idx].len = len; 295 s->mem[s->idx].len = len;
296 s->mem[s->idx].avg = newavg; 296 s->mem[s->idx].avg = newavg;
297 s->idx = (s->idx + 1) % NSAMPLES; 297 s->idx = (s->idx + 1) % NSAMPLES;
298 } 298 }
308 method2_int16(s, data); 308 method2_int16(s, data);
309 else 309 else
310 method1_int16(s, data); 310 method1_int16(s, data);
311 } 311 }
312 else if(af->data->format == (AF_FORMAT_FLOAT_NE)) 312 else if(af->data->format == (AF_FORMAT_FLOAT_NE))
313 { 313 {
314 if (s->method) 314 if (s->method)
315 method2_float(s, data); 315 method2_float(s, data);
316 else 316 else
317 method1_float(s, data); 317 method1_float(s, data);
318 } 318 }