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