]> git.sesse.net Git - vlc/blob - modules/audio_filter/param_eq.c
89ff80e37ef029b80c14250315ec76e1620e9658
[vlc] / modules / audio_filter / param_eq.c
1 /*****************************************************************************
2  * param_eq.c:
3  *****************************************************************************
4  * Copyright © 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antti Huovilainen
8  *          Sigmund A. Helberg <dnumgis@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <math.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc_aout.h>
32
33 /*****************************************************************************
34  * Module descriptor
35  *****************************************************************************/
36 static int  Open ( vlc_object_t * );
37 static void Close( vlc_object_t * );
38 static void CalcPeakEQCoeffs( float, float, float, float, float * );
39 static void CalcShelfEQCoeffs( float, float, float, int, float, float * );
40 static void ProcessEQ( float *, float *, float *, unsigned, unsigned, float *, unsigned );
41 static void DoWork( aout_instance_t *, aout_filter_t *,
42                     aout_buffer_t *, aout_buffer_t * );
43
44 vlc_module_begin();
45     set_description( _("Parametric Equalizer") );
46     set_shortname( _("Parametric Equalizer" ) );
47     set_capability( "audio filter", 0 );
48     set_category( CAT_AUDIO );
49     set_subcategory( SUBCAT_AUDIO_AFILTER );
50
51     add_float( "param-eq-lowf", 100, NULL, N_("Low freq (Hz)"),"", VLC_FALSE );
52     add_float_with_range( "param-eq-lowgain", 0, -20.0, 20.0, NULL,
53                           N_("Low freq gain (dB)"), "",VLC_FALSE );
54     add_float( "param-eq-highf", 10000, NULL, N_("High freq (Hz)"),"", VLC_FALSE );
55     add_float_with_range( "param-eq-highgain", 0, -20.0, 20.0, NULL,
56                           N_("High freq gain (dB)"),"",VLC_FALSE );
57     add_float( "param-eq-f1", 300, NULL, N_("Freq 1 (Hz)"),"", VLC_FALSE );
58     add_float_with_range( "param-eq-gain1", 0, -20.0, 20.0, NULL,
59                           N_("Freq 1 gain (dB)"), "",VLC_FALSE );
60     add_float_with_range( "param-eq-q1", 3, 0.1, 100.0, NULL,
61                           N_("Freq 1 Q"), "",VLC_FALSE );
62     add_float( "param-eq-f2", 1000, NULL, N_("Freq 2 (Hz)"),"", VLC_FALSE );
63     add_float_with_range( "param-eq-gain2", 0, -20.0, 20.0, NULL,
64                           N_("Freq 2 gain (dB)"),"",VLC_FALSE );
65     add_float_with_range( "param-eq-q2", 3, 0.1, 100.0, NULL,
66                           N_("Freq 2 Q"),"",VLC_FALSE );
67     add_float( "param-eq-f3", 3000, NULL, N_("Freq 3 (Hz)"),"", VLC_FALSE );
68     add_float_with_range( "param-eq-gain3", 0, -20.0, 20.0, NULL,
69                           N_("Freq 3 gain (dB)"),"",VLC_FALSE );
70     add_float_with_range( "param-eq-q3", 3, 0.1, 100.0, NULL,
71                           N_("Freq 3 Q"),"",VLC_FALSE );
72
73     set_callbacks( Open, Close );
74 vlc_module_end();
75
76 /*****************************************************************************
77  * Local prototypes
78  *****************************************************************************/
79 typedef struct aout_filter_sys_t
80 {
81     /* Filter static config */
82     float   f_lowf, f_lowgain;
83     float   f_f1, f_Q1, f_gain1;
84     float   f_f2, f_Q2, f_gain2;
85     float   f_f3, f_Q3, f_gain3;
86     float   f_highf, f_highgain;
87     /* Filter computed coeffs */
88     float   coeffs[5*5];
89     /* State */
90     float  *p_state;
91        
92 } aout_filter_sys_t;
93
94
95
96
97 /*****************************************************************************
98  * Open:
99  *****************************************************************************/
100 static int Open( vlc_object_t *p_this )
101 {
102     aout_filter_t     *p_filter = (aout_filter_t *)p_this;
103     aout_filter_sys_t *p_sys;
104     vlc_bool_t         b_fit = VLC_TRUE;
105     int                i_samplerate;
106
107     if( p_filter->input.i_format != VLC_FOURCC('f','l','3','2' ) ||
108         p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
109     {
110         b_fit = VLC_FALSE;
111         p_filter->input.i_format = VLC_FOURCC('f','l','3','2');
112         p_filter->output.i_format = VLC_FOURCC('f','l','3','2');
113         msg_Warn( p_filter, "bad input or output format" );
114     }
115     if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
116     {
117         b_fit = VLC_FALSE;
118         memcpy( &p_filter->output, &p_filter->input,
119                 sizeof(audio_sample_format_t) );
120         msg_Warn( p_filter, "input and output formats are not similar" );
121     }
122
123     if ( ! b_fit )
124     {
125         return VLC_EGENERIC;
126     }
127
128     p_filter->pf_do_work = DoWork;
129     p_filter->b_in_place = VLC_TRUE;
130
131     /* Allocate structure */
132     p_sys = p_filter->p_sys = malloc( sizeof( aout_filter_sys_t ) );
133
134     p_sys->f_lowf = config_GetFloat( p_this, "param-eq-lowf");
135     p_sys->f_lowgain = config_GetFloat( p_this, "param-eq-lowgain");
136     p_sys->f_highf = config_GetFloat( p_this, "param-eq-highf");
137     p_sys->f_highgain = config_GetFloat( p_this, "param-eq-highgain");
138     
139     p_sys->f_f1 = config_GetFloat( p_this, "param-eq-f1");
140     p_sys->f_Q1 = config_GetFloat( p_this, "param-eq-q1");
141     p_sys->f_gain1 = config_GetFloat( p_this, "param-eq-gain1");
142     
143     p_sys->f_f2 = config_GetFloat( p_this, "param-eq-f2");
144     p_sys->f_Q2 = config_GetFloat( p_this, "param-eq-q2");
145     p_sys->f_gain2 = config_GetFloat( p_this, "param-eq-gain2");
146
147     p_sys->f_f3 = config_GetFloat( p_this, "param-eq-f3");
148     p_sys->f_Q3 = config_GetFloat( p_this, "param-eq-q3");
149     p_sys->f_gain3 = config_GetFloat( p_this, "param-eq-gain3");
150   
151
152     i_samplerate = p_filter->input.i_rate;
153     CalcPeakEQCoeffs(p_sys->f_f1, p_sys->f_Q1, p_sys->f_gain1,
154                      i_samplerate, p_sys->coeffs+0*5);
155     CalcPeakEQCoeffs(p_sys->f_f2, p_sys->f_Q2, p_sys->f_gain2,
156                      i_samplerate, p_sys->coeffs+1*5);
157     CalcPeakEQCoeffs(p_sys->f_f3, p_sys->f_Q3, p_sys->f_gain3,
158                      i_samplerate, p_sys->coeffs+2*5);
159     CalcShelfEQCoeffs(p_sys->f_lowf, 1, p_sys->f_lowgain, 0,
160                       i_samplerate, p_sys->coeffs+3*5);
161     CalcShelfEQCoeffs(p_sys->f_highf, 1, p_sys->f_highgain, 0,
162                       i_samplerate, p_sys->coeffs+4*5);
163     p_sys->p_state = (float*)calloc( p_filter->input.i_channels*5*4,
164                                      sizeof(float) );
165
166     return VLC_SUCCESS;
167 }
168
169 static void Close( vlc_object_t *p_this )
170 {
171     aout_filter_t *p_filter = (aout_filter_t *)p_this;
172     free( p_filter->p_sys->p_state );
173     free( p_filter->p_sys );
174 }
175
176 /*****************************************************************************
177  * DoWork: process samples buffer
178  *****************************************************************************
179  *
180  *****************************************************************************/
181 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
182                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
183 {
184     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
185     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes;
186
187     ProcessEQ( (float*)p_in_buf->p_buffer, (float*)p_out_buf->p_buffer,
188                p_filter->p_sys->p_state, 
189                p_filter->input.i_channels, p_in_buf->i_nb_samples,
190                p_filter->p_sys->coeffs, 5 );
191 }
192
193 /*
194  * Calculate direct form IIR coefficients for peaking EQ
195  * coeffs[0] = b0
196  * coeffs[1] = b1
197  * coeffs[2] = b2
198  * coeffs[3] = a1
199  * coeffs[4] = a2
200  *
201  * Equations taken from RBJ audio EQ cookbook 
202  * (http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt)
203  */
204 static void CalcPeakEQCoeffs( float f0, float Q, float gainDB, float Fs,
205                               float *coeffs )
206 {
207     float A;
208     float w0;
209     float alpha;
210     float b0, b1, b2;
211     float a0, a1, a2;
212
213     // Provide sane limits to avoid overflow
214     if (Q < 0.1f) Q = 0.1f;   
215     if (Q > 100) Q = 100;
216     if (f0 > Fs/2*0.95f) f0 = Fs/2*0.95f;
217     if (gainDB < -40) gainDB = -40;
218     if (gainDB > 40) gainDB = 40;
219     
220     A = pow(10, gainDB/40);
221     w0 = 2*3.141593f*f0/Fs;
222     alpha = sin(w0)/(2*Q);
223     
224     b0 = 1 + alpha*A;
225     b1 = -2*cos(w0);
226     b2 = 1 - alpha*A;
227     a0 = 1 + alpha/A;
228     a1 = -2*cos(w0);
229     a2 = 1 - alpha/A;
230     
231     // Store values to coeffs and normalize by 1/a0
232     coeffs[0] = b0/a0;
233     coeffs[1] = b1/a0;
234     coeffs[2] = b2/a0;
235     coeffs[3] = a1/a0;
236     coeffs[4] = a2/a0;
237 }
238
239 /*
240  * Calculate direct form IIR coefficients for low/high shelf EQ
241  * coeffs[0] = b0
242  * coeffs[1] = b1
243  * coeffs[2] = b2
244  * coeffs[3] = a1
245  * coeffs[4] = a2
246  *
247  * Equations taken from RBJ audio EQ cookbook 
248  * (http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt)
249  */
250 static void CalcShelfEQCoeffs( float f0, float slope, float gainDB, int high,
251                                float Fs, float *coeffs )
252 {
253     float A;
254     float w0;
255     float alpha;
256     float b0, b1, b2;
257     float a0, a1, a2;
258
259     // Provide sane limits to avoid overflow
260     if (f0 > Fs/2*0.95f) f0 = Fs/2*0.95f;
261     if (gainDB < -40) gainDB = -40;
262     if (gainDB > 40) gainDB = 40;
263
264     A = pow(10, gainDB/40);
265     w0 = 2*3.141593f*f0/Fs;
266     alpha = sin(w0)/2 * sqrt( (A + 1/A)*(1/slope - 1) + 2 );
267
268     if (high)
269     {
270         b0 =    A*( (A+1) + (A-1)*cos(w0) + 2*sqrt(A)*alpha );
271         b1 = -2*A*( (A-1) + (A+1)*cos(w0) );
272         b2 =    A*( (A+1) + (A-1)*cos(w0) - 2*sqrt(A)*alpha );
273         a0 =        (A+1) - (A-1)*cos(w0) + 2*sqrt(A)*alpha;
274         a1 =    2*( (A-1) - (A+1)*cos(w0) );
275         a2 =        (A+1) - (A-1)*cos(w0) - 2*sqrt(A)*alpha;
276     }
277     else
278     {
279         b0 =    A*( (A+1) - (A-1)*cos(w0) + 2*sqrt(A)*alpha );
280         b1 =  2*A*( (A-1) - (A+1)*cos(w0));
281         b2 =    A*( (A+1) - (A-1)*cos(w0) - 2*sqrt(A)*alpha );
282         a0 =        (A+1) + (A-1)*cos(w0) + 2*sqrt(A)*alpha;
283         a1 =   -2*( (A-1) + (A+1)*cos(w0));
284         a2 =        (A+1) + (A-1)*cos(w0) - 2*sqrt(A)*alpha;
285     }
286     // Store values to coeffs and normalize by 1/a0
287     coeffs[0] = b0/a0;
288     coeffs[1] = b1/a0;
289     coeffs[2] = b2/a0;
290     coeffs[3] = a1/a0;
291     coeffs[4] = a2/a0;
292 }
293
294 /*
295   src is assumed to be interleaved
296   dest is assumed to be interleaved
297   size of state is 4*channels*eqCount
298   samples is not premultiplied by channels
299   size of coeffs is 5*eqCount
300 */
301 void ProcessEQ( float *src, float *dest, float *state, 
302                 unsigned channels, unsigned samples, float *coeffs, 
303                 unsigned eqCount )
304 {
305     unsigned i, chn, eq;
306     float   b0, b1, b2, a1, a2;
307     float   x, y = 0;
308     float   *src1, *dest1;
309     float   *coeffs1, *state1;
310     src1 = src;
311     dest1 = dest;
312     for (i = 0; i < samples; i++)
313     {
314         state1 = state;
315         for (chn = 0; chn < channels; chn++)
316         {
317             coeffs1 = coeffs;
318             x = *src1++;
319             /* Direct form 1 IIRs */
320             for (eq = 0; eq < eqCount; eq++)
321             {
322                 b0 = coeffs1[0];
323                 b1 = coeffs1[1];
324                 b2 = coeffs1[2];
325                 a1 = coeffs1[3];
326                 a2 = coeffs1[4];
327                 coeffs1 += 5;
328                 y = x*b0 + state1[0]*b1 + state1[1]*b2 - state1[2]*a1 - state1[3]*a2;
329                 state1[1] = state1[0];
330                 state1[0] = x;
331                 state1[3] = state1[2];
332                 state1[2] = y;
333                 x = y;
334                 state1 += 4;
335             }
336             *dest1++ = y;
337         }
338     }
339 }
340