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