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