Mercurial > mplayer.hg
annotate libaf/af_equalizer.c @ 36395:2b9bc3c2933d
Remove some macros and switch to libavutil equivalents.
author | reimar |
---|---|
date | Sat, 26 Oct 2013 09:36:23 +0000 |
parents | a93891202051 |
children |
rev | line source |
---|---|
28229
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24900
diff
changeset
|
1 /* |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24900
diff
changeset
|
2 * Equalizer filter, implementation of a 10 band time domain graphic |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24900
diff
changeset
|
3 * equalizer using IIR filters. The IIR filters are implemented using a |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24900
diff
changeset
|
4 * Direct Form II approach, but has been modified (b1 == 0 always) to |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24900
diff
changeset
|
5 * save computation. |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24900
diff
changeset
|
6 * |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24900
diff
changeset
|
7 * Copyright (C) 2001 Anders Johansson ajh@atri.curtin.edu.au |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24900
diff
changeset
|
8 * |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24900
diff
changeset
|
9 * This file is part of MPlayer. |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24900
diff
changeset
|
10 * |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24900
diff
changeset
|
11 * MPlayer is free software; you can redistribute it and/or modify |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24900
diff
changeset
|
12 * it under the terms of the GNU General Public License as published by |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24900
diff
changeset
|
13 * the Free Software Foundation; either version 2 of the License, or |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24900
diff
changeset
|
14 * (at your option) any later version. |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24900
diff
changeset
|
15 * |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24900
diff
changeset
|
16 * MPlayer is distributed in the hope that it will be useful, |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24900
diff
changeset
|
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24900
diff
changeset
|
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24900
diff
changeset
|
19 * GNU General Public License for more details. |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24900
diff
changeset
|
20 * |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24900
diff
changeset
|
21 * You should have received a copy of the GNU General Public License along |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24900
diff
changeset
|
22 * with MPlayer; if not, write to the Free Software Foundation, Inc., |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24900
diff
changeset
|
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
72d0b1444141
Replace informal license notices by standard license header
diego
parents:
24900
diff
changeset
|
24 */ |
8073 | 25 |
26 #include <stdio.h> | |
27 #include <stdlib.h> | |
28 | |
29 #include <inttypes.h> | |
30 #include <math.h> | |
31 | |
36395
2b9bc3c2933d
Remove some macros and switch to libavutil equivalents.
reimar
parents:
34174
diff
changeset
|
32 #include "libavutil/common.h" |
34174
a93891202051
Add missing mp_msg.h #includes, remove some unnecessary ones.
diego
parents:
32537
diff
changeset
|
33 #include "mp_msg.h" |
8073 | 34 #include "af.h" |
35 | |
8167 | 36 #define L 2 // Storage for filter taps |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
37 #define KM 10 // Max number of bands |
8073 | 38 |
39 #define Q 1.2247449 /* Q value for band-pass filters 1.2247=(3/2)^(1/2) | |
40 gives 4dB suppression @ Fc*2 and Fc/2 */ | |
41 | |
8607 | 42 /* Center frequencies for band-pass filters |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
43 The different frequency bands are: |
8607 | 44 nr. center frequency |
45 0 31.25 Hz | |
46 1 62.50 Hz | |
47 2 125.0 Hz | |
48 3 250.0 Hz | |
49 4 500.0 Hz | |
50 5 1.000 kHz | |
51 6 2.000 kHz | |
52 7 4.000 kHz | |
53 8 8.000 kHz | |
54 9 16.00 kHz | |
55 */ | |
8073 | 56 #define CF {31.25,62.5,125,250,500,1000,2000,4000,8000,16000} |
57 | |
58 // Maximum and minimum gain for the bands | |
59 #define G_MAX +12.0 | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
60 #define G_MIN -12.0 |
8073 | 61 |
62 // Data for specific instances of this filter | |
63 typedef struct af_equalizer_s | |
64 { | |
8607 | 65 float a[KM][L]; // A weights |
66 float b[KM][L]; // B weights | |
67 float wq[AF_NCH][KM][L]; // Circular buffer for W data | |
68 float g[AF_NCH][KM]; // Gain factor for each channel and band | |
69 int K; // Number of used eq bands | |
70 int channels; // Number of channels | |
17812
1ade5769c97b
added dinamically calculated gain factor at output stage to avoid clipping on sane ranges
reynaldo
parents:
17774
diff
changeset
|
71 float gain_factor; // applied at output to avoid clipping |
8073 | 72 } af_equalizer_t; |
73 | |
74 // 2nd order Band-pass Filter design | |
75 static void bp2(float* a, float* b, float fc, float q){ | |
76 double th= 2.0 * M_PI * fc; | |
77 double C = (1.0 - tan(th*q/2.0))/(1.0 + tan(th*q/2.0)); | |
78 | |
79 a[0] = (1.0 + C) * cos(th); | |
80 a[1] = -1 * C; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
81 |
8073 | 82 b[0] = (1.0 - C)/2.0; |
83 b[1] = -1.0050; | |
84 } | |
85 | |
86 // Initialization and runtime control | |
87 static int control(struct af_instance_s* af, int cmd, void* arg) | |
88 { | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
89 af_equalizer_t* s = (af_equalizer_t*)af->setup; |
8073 | 90 |
91 switch(cmd){ | |
92 case AF_CONTROL_REINIT:{ | |
17812
1ade5769c97b
added dinamically calculated gain factor at output stage to avoid clipping on sane ranges
reynaldo
parents:
17774
diff
changeset
|
93 int k =0, i =0; |
8073 | 94 float F[KM] = CF; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
95 |
17812
1ade5769c97b
added dinamically calculated gain factor at output stage to avoid clipping on sane ranges
reynaldo
parents:
17774
diff
changeset
|
96 s->gain_factor=0.0; |
1ade5769c97b
added dinamically calculated gain factor at output stage to avoid clipping on sane ranges
reynaldo
parents:
17774
diff
changeset
|
97 |
8073 | 98 // Sanity check |
99 if(!arg) return AF_ERROR; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
100 |
8073 | 101 af->data->rate = ((af_data_t*)arg)->rate; |
102 af->data->nch = ((af_data_t*)arg)->nch; | |
14245 | 103 af->data->format = AF_FORMAT_FLOAT_NE; |
8607 | 104 af->data->bps = 4; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
105 |
8073 | 106 // Calculate number of active filters |
107 s->K=KM; | |
8167 | 108 while(F[s->K-1] > (float)af->data->rate/2.2) |
8073 | 109 s->K--; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
110 |
8167 | 111 if(s->K != KM) |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
112 mp_msg(MSGT_AFILTER, MSGL_INFO, "[equalizer] Limiting the number of filters to" |
8607 | 113 " %i due to low sample rate.\n",s->K); |
8073 | 114 |
115 // Generate filter taps | |
116 for(k=0;k<s->K;k++) | |
117 bp2(s->a[k],s->b[k],F[k]/((float)af->data->rate),Q); | |
118 | |
119 // Calculate how much this plugin adds to the overall time delay | |
24900 | 120 af->delay = 2 * af->data->nch * af->data->bps; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
121 |
17812
1ade5769c97b
added dinamically calculated gain factor at output stage to avoid clipping on sane ranges
reynaldo
parents:
17774
diff
changeset
|
122 // Calculate gain factor to prevent clipping at output |
1ade5769c97b
added dinamically calculated gain factor at output stage to avoid clipping on sane ranges
reynaldo
parents:
17774
diff
changeset
|
123 for(k=0;k<AF_NCH;k++) |
1ade5769c97b
added dinamically calculated gain factor at output stage to avoid clipping on sane ranges
reynaldo
parents:
17774
diff
changeset
|
124 { |
1ade5769c97b
added dinamically calculated gain factor at output stage to avoid clipping on sane ranges
reynaldo
parents:
17774
diff
changeset
|
125 for(i=0;i<KM;i++) |
1ade5769c97b
added dinamically calculated gain factor at output stage to avoid clipping on sane ranges
reynaldo
parents:
17774
diff
changeset
|
126 { |
1ade5769c97b
added dinamically calculated gain factor at output stage to avoid clipping on sane ranges
reynaldo
parents:
17774
diff
changeset
|
127 if(s->gain_factor < s->g[k][i]) s->gain_factor=s->g[k][i]; |
1ade5769c97b
added dinamically calculated gain factor at output stage to avoid clipping on sane ranges
reynaldo
parents:
17774
diff
changeset
|
128 } |
1ade5769c97b
added dinamically calculated gain factor at output stage to avoid clipping on sane ranges
reynaldo
parents:
17774
diff
changeset
|
129 } |
8073 | 130 |
17812
1ade5769c97b
added dinamically calculated gain factor at output stage to avoid clipping on sane ranges
reynaldo
parents:
17774
diff
changeset
|
131 s->gain_factor=log10(s->gain_factor + 1.0) * 20.0; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
132 |
17812
1ade5769c97b
added dinamically calculated gain factor at output stage to avoid clipping on sane ranges
reynaldo
parents:
17774
diff
changeset
|
133 if(s->gain_factor > 0.0) |
1ade5769c97b
added dinamically calculated gain factor at output stage to avoid clipping on sane ranges
reynaldo
parents:
17774
diff
changeset
|
134 { |
1ade5769c97b
added dinamically calculated gain factor at output stage to avoid clipping on sane ranges
reynaldo
parents:
17774
diff
changeset
|
135 s->gain_factor=0.1+(s->gain_factor/12.0); |
1ade5769c97b
added dinamically calculated gain factor at output stage to avoid clipping on sane ranges
reynaldo
parents:
17774
diff
changeset
|
136 }else{ |
1ade5769c97b
added dinamically calculated gain factor at output stage to avoid clipping on sane ranges
reynaldo
parents:
17774
diff
changeset
|
137 s->gain_factor=1; |
1ade5769c97b
added dinamically calculated gain factor at output stage to avoid clipping on sane ranges
reynaldo
parents:
17774
diff
changeset
|
138 } |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
139 |
8607 | 140 return af_test_output(af,arg); |
8073 | 141 } |
142 case AF_CONTROL_COMMAND_LINE:{ | |
143 float g[10]={0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}; | |
144 int i,j; | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
145 sscanf((char*)arg,"%f:%f:%f:%f:%f:%f:%f:%f:%f:%f", &g[0], &g[1], |
8073 | 146 &g[2], &g[3], &g[4], &g[5], &g[6], &g[7], &g[8] ,&g[9]); |
8607 | 147 for(i=0;i<AF_NCH;i++){ |
8073 | 148 for(j=0;j<KM;j++){ |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
149 ((af_equalizer_t*)af->setup)->g[i][j] = |
36395
2b9bc3c2933d
Remove some macros and switch to libavutil equivalents.
reimar
parents:
34174
diff
changeset
|
150 pow(10.0,av_clipf(g[j],G_MIN,G_MAX)/20.0)-1.0; |
8073 | 151 } |
152 } | |
153 return AF_OK; | |
154 } | |
8607 | 155 case AF_CONTROL_EQUALIZER_GAIN | AF_CONTROL_SET:{ |
156 float* gain = ((af_control_ext_t*)arg)->arg; | |
157 int ch = ((af_control_ext_t*)arg)->ch; | |
158 int k; | |
17774
30fa0638b78d
We start with 0, so ch (channel number) == AF_NCH is invalid, too (not just >)
reimar
parents:
17068
diff
changeset
|
159 if(ch >= AF_NCH || ch < 0) |
8073 | 160 return AF_ERROR; |
8607 | 161 |
162 for(k = 0 ; k<KM ; k++) | |
36395
2b9bc3c2933d
Remove some macros and switch to libavutil equivalents.
reimar
parents:
34174
diff
changeset
|
163 s->g[ch][k] = pow(10.0,av_clipf(gain[k],G_MIN,G_MAX)/20.0)-1.0; |
8607 | 164 |
8073 | 165 return AF_OK; |
166 } | |
8607 | 167 case AF_CONTROL_EQUALIZER_GAIN | AF_CONTROL_GET:{ |
168 float* gain = ((af_control_ext_t*)arg)->arg; | |
169 int ch = ((af_control_ext_t*)arg)->ch; | |
170 int k; | |
17774
30fa0638b78d
We start with 0, so ch (channel number) == AF_NCH is invalid, too (not just >)
reimar
parents:
17068
diff
changeset
|
171 if(ch >= AF_NCH || ch < 0) |
8073 | 172 return AF_ERROR; |
8607 | 173 |
174 for(k = 0 ; k<KM ; k++) | |
175 gain[k] = log10(s->g[ch][k]+1.0) * 20.0; | |
176 | |
8073 | 177 return AF_OK; |
178 } | |
179 } | |
180 return AF_UNKNOWN; | |
181 } | |
182 | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
183 // Deallocate memory |
8073 | 184 static void uninit(struct af_instance_s* af) |
185 { | |
186 free(af->data); | |
187 free(af->setup); | |
188 } | |
189 | |
190 // Filter data through filter | |
191 static af_data_t* play(struct af_instance_s* af, af_data_t* data) | |
192 { | |
193 af_data_t* c = data; // Current working data | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
194 af_equalizer_t* s = (af_equalizer_t*)af->setup; // Setup |
8073 | 195 uint32_t ci = af->data->nch; // Index for channels |
196 uint32_t nch = af->data->nch; // Number of channels | |
197 | |
198 while(ci--){ | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
199 float* g = s->g[ci]; // Gain factor |
8607 | 200 float* in = ((float*)c->audio)+ci; |
201 float* out = ((float*)c->audio)+ci; | |
202 float* end = in + c->len/4; // Block loop end | |
8073 | 203 |
204 while(in < end){ | |
14245 | 205 register int k = 0; // Frequency band index |
8607 | 206 register float yt = *in; // Current input sample |
8073 | 207 in+=nch; |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
208 |
8073 | 209 // Run the filters |
210 for(;k<s->K;k++){ | |
211 // Pointer to circular buffer wq | |
212 register float* wq = s->wq[ci][k]; | |
213 // Calculate output from AR part of current filter | |
214 register float w=yt*s->b[k][0] + wq[0]*s->a[k][0] + wq[1]*s->a[k][1]; | |
215 // Calculate output form MA part of current filter | |
216 yt+=(w + wq[1]*s->b[k][1])*g[k]; | |
217 // Update circular buffer | |
218 wq[1] = wq[0]; | |
219 wq[0] = w; | |
220 } | |
29263
0f1b5b68af32
whitespace cosmetics: Remove all trailing whitespace.
diego
parents:
29049
diff
changeset
|
221 // Calculate output |
17812
1ade5769c97b
added dinamically calculated gain factor at output stage to avoid clipping on sane ranges
reynaldo
parents:
17774
diff
changeset
|
222 *out=yt*s->gain_factor; |
8073 | 223 out+=nch; |
224 } | |
225 } | |
226 return c; | |
227 } | |
228 | |
229 // Allocate memory and set function pointers | |
22746
fd6f824ef894
Rename open to af_open so as not to conflict with a previous header definition.
diego
parents:
17812
diff
changeset
|
230 static int af_open(af_instance_t* af){ |
8073 | 231 af->control=control; |
232 af->uninit=uninit; | |
233 af->play=play; | |
24888 | 234 af->mul=1; |
8073 | 235 af->data=calloc(1,sizeof(af_data_t)); |
236 af->setup=calloc(1,sizeof(af_equalizer_t)); | |
237 if(af->data == NULL || af->setup == NULL) | |
238 return AF_ERROR; | |
239 return AF_OK; | |
240 } | |
241 | |
242 // Description of this filter | |
243 af_info_t af_info_equalizer = { | |
244 "Equalizer audio filter", | |
245 "equalizer", | |
246 "Anders", | |
247 "", | |
248 AF_FLAGS_NOT_REENTRANT, | |
22746
fd6f824ef894
Rename open to af_open so as not to conflict with a previous header definition.
diego
parents:
17812
diff
changeset
|
249 af_open |
8073 | 250 }; |