2 * Equalizer filter, implementation of a 10 band time domain graphic
3 * equalizer using IIR filters. The IIR filters are implemented using a
4 * Direct Form II approach, but has been modified (b1 == 0 always) to
7 * Copyright (C) 2001 Anders Johansson ajh@atri.curtin.edu.au
9 * This file is part of MPlayer.
11 * MPlayer is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * MPlayer is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
34 #define L 2 // Storage for filter taps
35 #define KM 10 // Max number of bands
37 #define Q 1.2247449 /* Q value for band-pass filters 1.2247=(3/2)^(1/2)
38 gives 4dB suppression @ Fc*2 and Fc/2 */
40 /* Center frequencies for band-pass filters
41 The different frequency bands are:
54 #define CF {31.25,62.5,125,250,500,1000,2000,4000,8000,16000}
56 // Maximum and minimum gain for the bands
60 // Data for specific instances of this filter
61 typedef struct af_equalizer_s
63 float a
[KM
][L
]; // A weights
64 float b
[KM
][L
]; // B weights
65 float wq
[AF_NCH
][KM
][L
]; // Circular buffer for W data
66 float g
[AF_NCH
][KM
]; // Gain factor for each channel and band
67 int K
; // Number of used eq bands
68 int channels
; // Number of channels
69 float gain_factor
; // applied at output to avoid clipping
72 // 2nd order Band-pass Filter design
73 static void bp2(float* a
, float* b
, float fc
, float q
){
74 double th
= 2.0 * M_PI
* fc
;
75 double C
= (1.0 - tan(th
*q
/2.0))/(1.0 + tan(th
*q
/2.0));
77 a
[0] = (1.0 + C
) * cos(th
);
84 // Initialization and runtime control
85 static int control(struct af_instance_s
* af
, int cmd
, void* arg
)
87 af_equalizer_t
* s
= (af_equalizer_t
*)af
->setup
;
90 case AF_CONTROL_REINIT
:{
97 if(!arg
) return AF_ERROR
;
99 af
->data
->rate
= ((af_data_t
*)arg
)->rate
;
100 af
->data
->nch
= ((af_data_t
*)arg
)->nch
;
101 af
->data
->format
= AF_FORMAT_FLOAT_NE
;
104 // Calculate number of active filters
106 while(F
[s
->K
-1] > (float)af
->data
->rate
/2.2)
110 mp_msg(MSGT_AFILTER
, MSGL_INFO
, "[equalizer] Limiting the number of filters to"
111 " %i due to low sample rate.\n",s
->K
);
113 // Generate filter taps
115 bp2(s
->a
[k
],s
->b
[k
],F
[k
]/((float)af
->data
->rate
),Q
);
117 // Calculate how much this plugin adds to the overall time delay
118 af
->delay
= 2 * af
->data
->nch
* af
->data
->bps
;
120 // Calculate gain factor to prevent clipping at output
121 for(k
=0;k
<AF_NCH
;k
++)
125 if(s
->gain_factor
< s
->g
[k
][i
]) s
->gain_factor
=s
->g
[k
][i
];
129 s
->gain_factor
=log10(s
->gain_factor
+ 1.0) * 20.0;
131 if(s
->gain_factor
> 0.0)
133 s
->gain_factor
=0.1+(s
->gain_factor
/12.0);
138 return af_test_output(af
,arg
);
140 case AF_CONTROL_COMMAND_LINE
:{
141 float g
[10]={0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};
143 sscanf((char*)arg
,"%f:%f:%f:%f:%f:%f:%f:%f:%f:%f", &g
[0], &g
[1],
144 &g
[2], &g
[3], &g
[4], &g
[5], &g
[6], &g
[7], &g
[8] ,&g
[9]);
145 for(i
=0;i
<AF_NCH
;i
++){
147 ((af_equalizer_t
*)af
->setup
)->g
[i
][j
] =
148 pow(10.0,clamp(g
[j
],G_MIN
,G_MAX
)/20.0)-1.0;
153 case AF_CONTROL_EQUALIZER_GAIN
| AF_CONTROL_SET
:{
154 float* gain
= ((af_control_ext_t
*)arg
)->arg
;
155 int ch
= ((af_control_ext_t
*)arg
)->ch
;
157 if(ch
>= AF_NCH
|| ch
< 0)
160 for(k
= 0 ; k
<KM
; k
++)
161 s
->g
[ch
][k
] = pow(10.0,clamp(gain
[k
],G_MIN
,G_MAX
)/20.0)-1.0;
165 case AF_CONTROL_EQUALIZER_GAIN
| AF_CONTROL_GET
:{
166 float* gain
= ((af_control_ext_t
*)arg
)->arg
;
167 int ch
= ((af_control_ext_t
*)arg
)->ch
;
169 if(ch
>= AF_NCH
|| ch
< 0)
172 for(k
= 0 ; k
<KM
; k
++)
173 gain
[k
] = log10(s
->g
[ch
][k
]+1.0) * 20.0;
182 static void uninit(struct af_instance_s
* af
)
190 // Filter data through filter
191 static af_data_t
* play(struct af_instance_s
* af
, af_data_t
* data
)
193 af_data_t
* c
= data
; // Current working data
194 af_equalizer_t
* s
= (af_equalizer_t
*)af
->setup
; // Setup
195 uint32_t ci
= af
->data
->nch
; // Index for channels
196 uint32_t nch
= af
->data
->nch
; // Number of channels
199 float* g
= s
->g
[ci
]; // Gain factor
200 float* in
= ((float*)c
->audio
)+ci
;
201 float* out
= ((float*)c
->audio
)+ci
;
202 float* end
= in
+ c
->len
/4; // Block loop end
205 register int k
= 0; // Frequency band index
206 register float yt
= *in
; // Current input sample
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
222 *out
=yt
*s
->gain_factor
;
229 // Allocate memory and set function pointers
230 static int af_open(af_instance_t
* af
){
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
)
242 // Description of this filter
243 af_info_t af_info_equalizer
= {
244 "Equalizer audio filter",
248 AF_FLAGS_NOT_REENTRANT
,