UltraSPARC T1 (Niagara) support, patch by Derek E. Lewis /dlewis (gobble) solnetworks...
[mplayer/glamo.git] / libaf / af_volnorm.c
blobeb427dc0289b23a3a842375e00d00227dba9c631
1 /*=============================================================================
2 //
3 // This software has been released under the terms of the GNU General Public
4 // license. See http://www.gnu.org/copyleft/gpl.html for details.
5 //
6 // Copyright 2004 Alex Beregszaszi & Pierre Lombard
7 //
8 //=============================================================================
9 */
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
15 #include <unistd.h>
16 #include <inttypes.h>
17 #include <math.h>
18 #include <limits.h>
20 #include "af.h"
22 // Methods:
23 // 1: uses a 1 value memory and coefficients new=a*old+b*cur (with a+b=1)
24 // 2: uses several samples to smooth the variations (standard weighted mean
25 // on past samples)
27 // Size of the memory array
28 // FIXME: should depend on the frequency of the data (should be a few seconds)
29 #define NSAMPLES 128
31 // If summing all the mem[].len is lower than MIN_SAMPLE_SIZE bytes, then we
32 // choose to ignore the computed value as it's not significant enough
33 // FIXME: should depend on the frequency of the data (0.5s maybe)
34 #define MIN_SAMPLE_SIZE 32000
36 // mul is the value by which the samples are scaled
37 // and has to be in [MUL_MIN, MUL_MAX]
38 #define MUL_INIT 1.0
39 #define MUL_MIN 0.1
40 #define MUL_MAX 5.0
42 // Silence level
43 // FIXME: should be relative to the level of the samples
44 #define SIL_S16 (SHRT_MAX * 0.01)
45 #define SIL_FLOAT (INT_MAX * 0.01) // FIXME
47 // smooth must be in ]0.0, 1.0[
48 #define SMOOTH_MUL 0.06
49 #define SMOOTH_LASTAVG 0.06
51 #define DEFAULT_TARGET 0.25
53 // Data for specific instances of this filter
54 typedef struct af_volume_s
56 int method; // method used
57 float mul;
58 // method 1
59 float lastavg; // history value of the filter
60 // method 2
61 int idx;
62 struct {
63 float avg; // average level of the sample
64 int len; // sample size (weight)
65 } mem[NSAMPLES];
66 // "Ideal" level
67 float mid_s16;
68 float mid_float;
69 }af_volnorm_t;
71 // Initialization and runtime control
72 static int control(struct af_instance_s* af, int cmd, void* arg)
74 af_volnorm_t* s = (af_volnorm_t*)af->setup;
76 switch(cmd){
77 case AF_CONTROL_REINIT:
78 // Sanity check
79 if(!arg) return AF_ERROR;
81 af->data->rate = ((af_data_t*)arg)->rate;
82 af->data->nch = ((af_data_t*)arg)->nch;
84 if(((af_data_t*)arg)->format == (AF_FORMAT_S16_NE)){
85 af->data->format = AF_FORMAT_S16_NE;
86 af->data->bps = 2;
87 }else{
88 af->data->format = AF_FORMAT_FLOAT_NE;
89 af->data->bps = 4;
91 return af_test_output(af,(af_data_t*)arg);
92 case AF_CONTROL_COMMAND_LINE:{
93 int i = 0;
94 float target = DEFAULT_TARGET;
95 sscanf((char*)arg,"%d:%f", &i, &target);
96 if (i != 1 && i != 2)
97 return AF_ERROR;
98 s->method = i-1;
99 s->mid_s16 = ((float)SHRT_MAX) * target;
100 s->mid_float = ((float)INT_MAX) * target;
101 return AF_OK;
104 return AF_UNKNOWN;
107 // Deallocate memory
108 static void uninit(struct af_instance_s* af)
110 if(af->data)
111 free(af->data);
112 if(af->setup)
113 free(af->setup);
116 static void method1_int16(af_volnorm_t *s, af_data_t *c)
118 register int i = 0;
119 int16_t *data = (int16_t*)c->audio; // Audio data
120 int len = c->len/2; // Number of samples
121 float curavg = 0.0, newavg, neededmul;
122 int tmp;
124 for (i = 0; i < len; i++)
126 tmp = data[i];
127 curavg += tmp * tmp;
129 curavg = sqrt(curavg / (float) len);
131 // Evaluate an adequate 'mul' coefficient based on previous state, current
132 // samples level, etc
134 if (curavg > SIL_S16)
136 neededmul = s->mid_s16 / (curavg * s->mul);
137 s->mul = (1.0 - SMOOTH_MUL) * s->mul + SMOOTH_MUL * neededmul;
139 // clamp the mul coefficient
140 s->mul = clamp(s->mul, MUL_MIN, MUL_MAX);
143 // Scale & clamp the samples
144 for (i = 0; i < len; i++)
146 tmp = s->mul * data[i];
147 tmp = clamp(tmp, SHRT_MIN, SHRT_MAX);
148 data[i] = tmp;
151 // Evaulation of newavg (not 100% accurate because of values clamping)
152 newavg = s->mul * curavg;
154 // Stores computed values for future smoothing
155 s->lastavg = (1.0 - SMOOTH_LASTAVG) * s->lastavg + SMOOTH_LASTAVG * newavg;
158 static void method1_float(af_volnorm_t *s, af_data_t *c)
160 register int i = 0;
161 float *data = (float*)c->audio; // Audio data
162 int len = c->len/4; // Number of samples
163 float curavg = 0.0, newavg, neededmul, tmp;
165 for (i = 0; i < len; i++)
167 tmp = data[i];
168 curavg += tmp * tmp;
170 curavg = sqrt(curavg / (float) len);
172 // Evaluate an adequate 'mul' coefficient based on previous state, current
173 // samples level, etc
175 if (curavg > SIL_FLOAT) // FIXME
177 neededmul = s->mid_float / (curavg * s->mul);
178 s->mul = (1.0 - SMOOTH_MUL) * s->mul + SMOOTH_MUL * neededmul;
180 // clamp the mul coefficient
181 s->mul = clamp(s->mul, MUL_MIN, MUL_MAX);
184 // Scale & clamp the samples
185 for (i = 0; i < len; i++)
186 data[i] *= s->mul;
188 // Evaulation of newavg (not 100% accurate because of values clamping)
189 newavg = s->mul * curavg;
191 // Stores computed values for future smoothing
192 s->lastavg = (1.0 - SMOOTH_LASTAVG) * s->lastavg + SMOOTH_LASTAVG * newavg;
195 static void method2_int16(af_volnorm_t *s, af_data_t *c)
197 register int i = 0;
198 int16_t *data = (int16_t*)c->audio; // Audio data
199 int len = c->len/2; // Number of samples
200 float curavg = 0.0, newavg, avg = 0.0;
201 int tmp, totallen = 0;
203 for (i = 0; i < len; i++)
205 tmp = data[i];
206 curavg += tmp * tmp;
208 curavg = sqrt(curavg / (float) len);
210 // Evaluate an adequate 'mul' coefficient based on previous state, current
211 // samples level, etc
212 for (i = 0; i < NSAMPLES; i++)
214 avg += s->mem[i].avg * (float)s->mem[i].len;
215 totallen += s->mem[i].len;
218 if (totallen > MIN_SAMPLE_SIZE)
220 avg /= (float)totallen;
221 if (avg >= SIL_S16)
223 s->mul = s->mid_s16 / avg;
224 s->mul = clamp(s->mul, MUL_MIN, MUL_MAX);
228 // Scale & clamp the samples
229 for (i = 0; i < len; i++)
231 tmp = s->mul * data[i];
232 tmp = clamp(tmp, SHRT_MIN, SHRT_MAX);
233 data[i] = tmp;
236 // Evaulation of newavg (not 100% accurate because of values clamping)
237 newavg = s->mul * curavg;
239 // Stores computed values for future smoothing
240 s->mem[s->idx].len = len;
241 s->mem[s->idx].avg = newavg;
242 s->idx = (s->idx + 1) % NSAMPLES;
245 static void method2_float(af_volnorm_t *s, af_data_t *c)
247 register int i = 0;
248 float *data = (float*)c->audio; // Audio data
249 int len = c->len/4; // Number of samples
250 float curavg = 0.0, newavg, avg = 0.0, tmp;
251 int totallen = 0;
253 for (i = 0; i < len; i++)
255 tmp = data[i];
256 curavg += tmp * tmp;
258 curavg = sqrt(curavg / (float) len);
260 // Evaluate an adequate 'mul' coefficient based on previous state, current
261 // samples level, etc
262 for (i = 0; i < NSAMPLES; i++)
264 avg += s->mem[i].avg * (float)s->mem[i].len;
265 totallen += s->mem[i].len;
268 if (totallen > MIN_SAMPLE_SIZE)
270 avg /= (float)totallen;
271 if (avg >= SIL_FLOAT)
273 s->mul = s->mid_float / avg;
274 s->mul = clamp(s->mul, MUL_MIN, MUL_MAX);
278 // Scale & clamp the samples
279 for (i = 0; i < len; i++)
280 data[i] *= s->mul;
282 // Evaulation of newavg (not 100% accurate because of values clamping)
283 newavg = s->mul * curavg;
285 // Stores computed values for future smoothing
286 s->mem[s->idx].len = len;
287 s->mem[s->idx].avg = newavg;
288 s->idx = (s->idx + 1) % NSAMPLES;
291 // Filter data through filter
292 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
294 af_volnorm_t *s = af->setup;
296 if(af->data->format == (AF_FORMAT_S16_NE))
298 if (s->method)
299 method2_int16(s, data);
300 else
301 method1_int16(s, data);
303 else if(af->data->format == (AF_FORMAT_FLOAT_NE))
305 if (s->method)
306 method2_float(s, data);
307 else
308 method1_float(s, data);
310 return data;
313 // Allocate memory and set function pointers
314 static int open(af_instance_t* af){
315 int i = 0;
316 af->control=control;
317 af->uninit=uninit;
318 af->play=play;
319 af->mul.n=1;
320 af->mul.d=1;
321 af->data=calloc(1,sizeof(af_data_t));
322 af->setup=calloc(1,sizeof(af_volnorm_t));
323 if(af->data == NULL || af->setup == NULL)
324 return AF_ERROR;
326 ((af_volnorm_t*)af->setup)->mul = MUL_INIT;
327 ((af_volnorm_t*)af->setup)->lastavg = ((float)SHRT_MAX) * DEFAULT_TARGET;
328 ((af_volnorm_t*)af->setup)->idx = 0;
329 ((af_volnorm_t*)af->setup)->mid_s16 = ((float)SHRT_MAX) * DEFAULT_TARGET;
330 ((af_volnorm_t*)af->setup)->mid_float = ((float)INT_MAX) * DEFAULT_TARGET;
331 for (i = 0; i < NSAMPLES; i++)
333 ((af_volnorm_t*)af->setup)->mem[i].len = 0;
334 ((af_volnorm_t*)af->setup)->mem[i].avg = 0;
336 return AF_OK;
339 // Description of this filter
340 af_info_t af_info_volnorm = {
341 "Volume normalizer filter",
342 "volnorm",
343 "Alex Beregszaszi & Pierre Lombard",
345 AF_FLAGS_NOT_REENTRANT,
346 open