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