]> git.sesse.net Git - vlc/blob - modules/audio_filter/equalizer.c
FSF address change.
[vlc] / modules / audio_filter / equalizer.c
1 /*****************************************************************************
2  * equalizer.c:
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <math.h>
29
30 #include <vlc/vlc.h>
31
32 #include <vlc/aout.h>
33 #include "aout_internal.h"
34
35 #include "equalizer_presets.h"
36 /* TODO:
37  *  - add tables for other rates ( 22500, 11250, ...)
38  *  - optimize a bit (you can hardly do slower ;)
39  *  - add tables for more bands (15 and 32 would be cool), maybe with auto coeffs
40  *  computation (not too hard once the Q is found).
41  *  - support for external preset
42  *  - callback to handle preset changes on the fly
43  *  - ...
44  */
45
46 /*****************************************************************************
47  * Module descriptor
48  *****************************************************************************/
49 static int  Open ( vlc_object_t * );
50 static void Close( vlc_object_t * );
51
52 #define PRESET_TEXT N_( "Equalizer preset" )
53 #define PRESET_LONGTEXT PRESET_TEXT
54
55 #define BANDS_TEXT N_( "Bands gain")
56 #define BANDS_LONGTEXT N_( "Override preset bands gain in dB (-20 ... 20)" )
57
58 #define TWOPASS_TEXT N_( "Two pass" )
59 #define TWOPASS_LONGTEXT N_( "Filter twice the audio" )
60
61 #define PREAMP_TEXT N_("Global gain" )
62 #define PREAMP_LONGTEXT N_("Set the global gain in dB (-20 ... 20)" )
63
64 vlc_module_begin();
65     set_description( _("Equalizer 10 bands") );
66     set_shortname( N_("Equalizer" ) );
67     set_capability( "audio filter", 0 );
68     set_category( CAT_AUDIO );
69     set_subcategory( SUBCAT_AUDIO_AFILTER );
70
71     add_string( "equalizer-preset", "flat", NULL, PRESET_TEXT,
72                 PRESET_LONGTEXT, VLC_FALSE );
73         change_string_list( preset_list, preset_list_text, 0 );
74     add_string( "equalizer-bands", NULL, NULL, BANDS_TEXT,
75                 BANDS_LONGTEXT, VLC_TRUE );
76     add_bool( "equalizer-2pass", 0, NULL, TWOPASS_TEXT,
77               TWOPASS_LONGTEXT, VLC_TRUE );
78     add_float( "equalizer-preamp", 12.0, NULL, PREAMP_TEXT,
79                PREAMP_LONGTEXT, VLC_TRUE );
80     set_callbacks( Open, Close );
81     add_shortcut( "equalizer" );
82 vlc_module_end();
83
84 /*****************************************************************************
85  * Local prototypes
86  *****************************************************************************/
87 typedef struct aout_filter_sys_t
88 {
89     /* Filter static config */
90     int i_band;
91     float *f_alpha;
92     float *f_beta;
93     float *f_gamma;
94
95     float f_newpreamp;
96     char *psz_newbands;
97     vlc_bool_t b_first;
98
99     /* Filter dyn config */
100     float *f_amp;   /* Per band amp */
101     float f_gamp;   /* Global preamp */
102     vlc_bool_t b_2eqz;
103
104     /* Filter state */
105     float x[32][2];
106     float y[32][128][2];
107
108     /* Second filter state */
109     float x2[32][2];
110     float y2[32][128][2];
111
112 } aout_filter_sys_t;
113
114 static void DoWork( aout_instance_t *, aout_filter_t *,
115                     aout_buffer_t *, aout_buffer_t * );
116
117 #define EQZ_IN_FACTOR (0.25)
118 static int  EqzInit( aout_filter_t *, int );
119 static void EqzFilter( aout_instance_t *,aout_filter_t *, float *, float *,
120                         int, int );
121 static void EqzClean( aout_filter_t * );
122
123 static int PresetCallback( vlc_object_t *, char const *,
124                                            vlc_value_t, vlc_value_t, void * );
125 static int PreampCallback( vlc_object_t *, char const *,
126                                            vlc_value_t, vlc_value_t, void * );
127 static int BandsCallback ( vlc_object_t *, char const *,
128                                            vlc_value_t, vlc_value_t, void * );
129
130
131
132 /*****************************************************************************
133  * Open:
134  *****************************************************************************/
135 static int Open( vlc_object_t *p_this )
136 {
137     aout_filter_t     *p_filter = (aout_filter_t *)p_this;
138     aout_filter_sys_t *p_sys;
139     vlc_bool_t         b_fit = VLC_TRUE;
140
141     if( p_filter->input.i_format != VLC_FOURCC('f','l','3','2' ) ||
142         p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
143     {
144         b_fit = VLC_FALSE;
145         p_filter->input.i_format = VLC_FOURCC('f','l','3','2');
146         p_filter->output.i_format = VLC_FOURCC('f','l','3','2');
147         msg_Warn( p_filter, "Bad input or output format" );
148     }
149     if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
150     {
151         b_fit = VLC_FALSE;
152         memcpy( &p_filter->output, &p_filter->input,
153                 sizeof(audio_sample_format_t) );
154         msg_Warn( p_filter, "input and output formats are not similar" );
155     }
156
157     if ( ! b_fit )
158     {
159         return VLC_EGENERIC;
160     }
161
162     p_filter->pf_do_work = DoWork;
163     p_filter->b_in_place = VLC_TRUE;
164
165     /* Allocate structure */
166     p_sys = p_filter->p_sys = malloc( sizeof( aout_filter_sys_t ) );
167
168     if( EqzInit( p_filter, p_filter->input.i_rate ) )
169         return VLC_EGENERIC;
170
171     return VLC_SUCCESS;
172 }
173
174 /*****************************************************************************
175  * Close: close the plugin
176  *****************************************************************************/
177 static void Close( vlc_object_t *p_this )
178 {
179     aout_filter_t     *p_filter = (aout_filter_t *)p_this;
180     aout_filter_sys_t *p_sys = p_filter->p_sys;
181
182     EqzClean( p_filter );
183     free( p_sys );
184 }
185
186 /*****************************************************************************
187  * DoWork: process samples buffer
188  *****************************************************************************
189  *
190  *****************************************************************************/
191 static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,
192                     aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )
193 {
194     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
195     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes;
196
197     EqzFilter( p_aout, p_filter, (float*)p_out_buf->p_buffer,
198                (float*)p_in_buf->p_buffer, p_in_buf->i_nb_samples,
199                aout_FormatNbChannels( &p_filter->input ) );
200 }
201
202 /*****************************************************************************
203  * Equalizer stuff
204  *****************************************************************************/
205 typedef struct
206 {
207     int   i_band;
208
209     struct
210     {
211         float f_frequency;
212         float f_alpha;
213         float f_beta;
214         float f_gamma;
215     } band[EQZ_BANDS_MAX];
216
217 } eqz_config_t;
218
219 /* Value from equ-xmms */
220 static const eqz_config_t eqz_config_44100_10b =
221 {
222     10,
223     {
224         {    60, 0.003013, 0.993973, 1.993901 },
225         {   170, 0.008490, 0.983019, 1.982437 },
226         {   310, 0.015374, 0.969252, 1.967331 },
227         {   600, 0.029328, 0.941343, 1.934254 },
228         {  1000, 0.047918, 0.904163, 1.884869 },
229         {  3000, 0.130408, 0.739184, 1.582718 },
230         {  6000, 0.226555, 0.546889, 1.015267 },
231         { 12000, 0.344937, 0.310127, -0.181410 },
232         { 14000, 0.366438, 0.267123, -0.521151 },
233         { 16000, 0.379009, 0.241981, -0.808451 },
234     }
235 };
236 static const eqz_config_t eqz_config_48000_10b =
237 {
238     10,
239     {
240         {    60, 0.002769, 0.994462, 1.994400 },
241         {   170, 0.007806, 0.984388, 1.983897 },
242         {   310, 0.014143, 0.971714, 1.970091 },
243         {   600, 0.027011, 0.945978, 1.939979 },
244         {  1000, 0.044203, 0.911595, 1.895241 },
245         {  3000, 0.121223, 0.757553, 1.623767 },
246         {  6000, 0.212888, 0.574224, 1.113145 },
247         { 12000, 0.331347, 0.337307, 0.000000 },
248         { 14000, 0.355263, 0.289473, -0.333740 },
249         { 16000, 0.371900, 0.256201, -0.628100 }
250     }
251 };
252
253 static inline float EqzConvertdB( float db )
254 {
255     /* Map it to gain,
256      * (we do as if the input of iir is /EQZ_IN_FACTOR, but in fact it's the non iir data that is *EQZ_IN_FACTOR)
257      * db = 20*log( out / in ) with out = in + amp*iir(i/EQZ_IN_FACTOR)
258      * or iir(i) == i for the center freq so
259      * db = 20*log( 1 + amp/EQZ_IN_FACTOR )
260      * -> amp = EQZ_IN_FACTOR*(10^(db/20) - 1)
261      **/
262
263     if( db < -20.0 )
264         db = -20.0;
265     else if(  db > 20.0 )
266         db = 20.0;
267     return EQZ_IN_FACTOR * ( pow( 10, db / 20.0 ) - 1.0 );
268 }
269
270 static int EqzInit( aout_filter_t *p_filter, int i_rate )
271 {
272     aout_filter_sys_t *p_sys = p_filter->p_sys;
273     const eqz_config_t *p_cfg;
274     int i, ch;
275     vlc_value_t val1, val2, val3;
276     aout_instance_t *p_aout = (aout_instance_t *)p_filter->p_parent;
277
278     /* Select the config */
279     if( i_rate == 48000 )
280     {
281         p_cfg = &eqz_config_48000_10b;
282     }
283     else if( i_rate == 44100 )
284     {
285         p_cfg = &eqz_config_44100_10b;
286     }
287     else
288     {
289         /* TODO compute the coeffs on the fly */
290         msg_Err( p_filter, "unsupported rate" );
291         return VLC_EGENERIC;
292     }
293
294     /* Create the static filter config */
295     p_sys->i_band = p_cfg->i_band;
296     p_sys->f_alpha = malloc( p_sys->i_band * sizeof(float) );
297     p_sys->f_beta  = malloc( p_sys->i_band * sizeof(float) );
298     p_sys->f_gamma = malloc( p_sys->i_band * sizeof(float) );
299     for( i = 0; i < p_sys->i_band; i++ )
300     {
301         p_sys->f_alpha[i] = p_cfg->band[i].f_alpha;
302         p_sys->f_beta[i]  = p_cfg->band[i].f_beta;
303         p_sys->f_gamma[i] = p_cfg->band[i].f_gamma;
304     }
305
306     /* Filter dyn config */
307     p_sys->b_2eqz = VLC_FALSE;
308     p_sys->f_gamp = 1.0;
309     p_sys->f_amp   = malloc( p_sys->i_band * sizeof(float) );
310     for( i = 0; i < p_sys->i_band; i++ )
311     {
312         p_sys->f_amp[i] = 0.0;
313     }
314
315     /* Filter state */
316     for( ch = 0; ch < 32; ch++ )
317     {
318         p_sys->x[ch][0]  =
319         p_sys->x[ch][1]  =
320         p_sys->x2[ch][0] =
321         p_sys->x2[ch][1] = 0.0;
322
323         for( i = 0; i < p_sys->i_band; i++ )
324         {
325             p_sys->y[ch][i][0]  =
326             p_sys->y[ch][i][1]  =
327             p_sys->y2[ch][i][0] =
328             p_sys->y2[ch][i][1] = 0.0;
329         }
330     }
331
332     var_CreateGetString( p_aout,"equalizer-bands" );
333     var_CreateGetString( p_aout, "equalizer-preset" );
334
335     p_sys->b_2eqz = var_CreateGetBool( p_aout, "equalizer-2pass" );
336
337     var_CreateGetFloat( p_aout, "equalizer-preamp" );
338
339     /* Get initial values */
340     var_Get( p_aout, "equalizer-preset", &val1 );
341     var_Get( p_aout, "equalizer-bands", &val2 );
342     var_Get( p_aout, "equalizer-preamp", &val3 );
343
344     p_sys->b_first = VLC_TRUE;
345     PresetCallback( VLC_OBJECT( p_aout ), NULL, val1, val1, p_sys );
346     BandsCallback(  VLC_OBJECT( p_aout ), NULL, val2, val2, p_sys );
347     PreampCallback( VLC_OBJECT( p_aout ), NULL, val3, val3, p_sys );
348     p_sys->b_first = VLC_FALSE;
349
350     /* Register preset bands (for intf) if : */
351     /* We have no bands info --> the preset info must be given to the intf */
352     /* or The bands info matches the preset */
353     if (p_sys->psz_newbands == NULL)
354     {
355         msg_Err(p_filter, "No preset selected");
356         return (VLC_EGENERIC);
357     }
358     if( ( *(val2.psz_string) &&
359         strstr( p_sys->psz_newbands, val2.psz_string ) ) || !*val2.psz_string )
360     {
361         var_SetString( p_aout, "equalizer-bands", p_sys->psz_newbands );
362         if( p_sys->f_newpreamp == p_sys->f_gamp )
363             var_SetFloat( p_aout, "equalizer-preamp", p_sys->f_newpreamp );
364     }
365
366     /* Add our own callbacks */
367     var_AddCallback( p_aout, "equalizer-preset", PresetCallback, p_sys );
368     var_AddCallback( p_aout, "equalizer-bands", BandsCallback, p_sys );
369     var_AddCallback( p_aout, "equalizer-preamp", PreampCallback, p_sys );
370
371     msg_Dbg( p_filter, "equalizer loaded for %d Hz with %d bands %d pass",
372                         i_rate, p_sys->i_band, p_sys->b_2eqz ? 2 : 1 );
373     for( i = 0; i < p_sys->i_band; i++ )
374     {
375         msg_Dbg( p_filter, "   %d Hz -> factor:%f alpha:%f beta:%f gamma:%f",
376                  (int)p_cfg->band[i].f_frequency, p_sys->f_amp[i],
377                  p_sys->f_alpha[i], p_sys->f_beta[i], p_sys->f_gamma[i]);
378     }
379     return VLC_SUCCESS;
380 }
381
382 static void EqzFilter( aout_instance_t *p_aout,
383                        aout_filter_t *p_filter, float *out, float *in,
384                        int i_samples, int i_channels )
385 {
386     aout_filter_sys_t *p_sys = p_filter->p_sys;
387     int i, ch, j;
388
389     for( i = 0; i < i_samples; i++ )
390     {
391         for( ch = 0; ch < i_channels; ch++ )
392         {
393             const float x = in[ch];
394             float o = 0.0;
395
396             for( j = 0; j < p_sys->i_band; j++ )
397             {
398                 float y = p_sys->f_alpha[j] * ( x - p_sys->x[ch][1] ) +
399                           p_sys->f_gamma[j] * p_sys->y[ch][j][0] -
400                           p_sys->f_beta[j]  * p_sys->y[ch][j][1];
401
402                 p_sys->y[ch][j][1] = p_sys->y[ch][j][0];
403                 p_sys->y[ch][j][0] = y;
404
405                 o += y * p_sys->f_amp[j];
406             }
407             p_sys->x[ch][1] = p_sys->x[ch][0];
408             p_sys->x[ch][0] = x;
409
410             /* Second filter */
411             if( p_sys->b_2eqz )
412             {
413                 const float x2 = EQZ_IN_FACTOR * x + o;
414                 o = 0.0;
415                 for( j = 0; j < p_sys->i_band; j++ )
416                 {
417                     float y = p_sys->f_alpha[j] * ( x2 - p_sys->x2[ch][1] ) +
418                               p_sys->f_gamma[j] * p_sys->y2[ch][j][0] -
419                               p_sys->f_beta[j]  * p_sys->y2[ch][j][1];
420
421                     p_sys->y2[ch][j][1] = p_sys->y2[ch][j][0];
422                     p_sys->y2[ch][j][0] = y;
423
424                     o += y * p_sys->f_amp[j];
425                 }
426                 p_sys->x2[ch][1] = p_sys->x2[ch][0];
427                 p_sys->x2[ch][0] = x2;
428
429                 /* We add source PCM + filtered PCM */
430                 out[ch] = p_sys->f_gamp *( EQZ_IN_FACTOR * x2 + o );
431             }
432             else
433             {
434                 /* We add source PCM + filtered PCM */
435                 out[ch] = p_sys->f_gamp *( EQZ_IN_FACTOR * x + o );
436             }
437         }
438
439         in  += i_channels;
440         out += i_channels;
441     }
442 }
443
444 static void EqzClean( aout_filter_t *p_filter )
445 {
446     aout_filter_sys_t *p_sys = p_filter->p_sys;
447
448     var_DelCallback( (aout_instance_t *)p_filter->p_parent,
449                         "equalizer-bands", BandsCallback, p_sys );
450     var_DelCallback( (aout_instance_t *)p_filter->p_parent,
451                         "equalizer-preset", PresetCallback, p_sys );
452     var_DelCallback( (aout_instance_t *)p_filter->p_parent,
453                         "equalizer-preamp", PreampCallback, p_sys );
454
455     free( p_sys->f_alpha );
456     free( p_sys->f_beta );
457     free( p_sys->f_gamma );
458
459     free( p_sys->f_amp );
460 }
461
462
463 static int PresetCallback( vlc_object_t *p_this, char const *psz_cmd,
464                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
465 {
466     aout_filter_sys_t *p_sys = (aout_filter_sys_t *)p_data;
467     aout_instance_t *p_aout = (aout_instance_t *)p_this;
468
469     char *psz_preset = newval.psz_string;
470     char psz_newbands[120];
471
472     memset( psz_newbands, 0, 120 );
473
474     if( *psz_preset && p_sys->i_band == 10 )
475     {
476         int i;
477         /* */
478         for( i = 0; eqz_preset_10b[i] != NULL; i++ )
479         {
480             if( !strcasecmp( eqz_preset_10b[i]->psz_name, psz_preset ) )
481             {
482                 int j;
483                 p_sys->f_gamp *= pow( 10, eqz_preset_10b[i]->f_preamp / 20.0 );
484                 for( j = 0; j < p_sys->i_band; j++ )
485                 {
486                     p_sys->f_amp[j] = EqzConvertdB(
487                                         eqz_preset_10b[i]->f_amp[j] );
488                     sprintf( psz_newbands, "%s %f", psz_newbands,
489                                          eqz_preset_10b[i]->f_amp[j] );
490                 }
491                 if( p_sys->b_first == VLC_FALSE )
492                 {
493                     var_SetString( p_aout, "equalizer-bands", psz_newbands );
494                     var_SetFloat( p_aout, "equalizer-preamp",
495                                     eqz_preset_10b[i]->f_preamp );
496                 }
497                 else
498                 {
499                     p_sys->psz_newbands = strdup( psz_newbands );
500                     p_sys->f_newpreamp = eqz_preset_10b[i]->f_preamp;
501                 }
502                 break;
503             }
504         }
505         if( eqz_preset_10b[i] == NULL )
506         {
507             msg_Err( p_aout, "equalizer preset '%s' not found", psz_preset );
508             msg_Dbg( p_aout, "full list:" );
509             for( i = 0; eqz_preset_10b[i] != NULL; i++ )
510                 msg_Dbg( p_aout, "  - '%s'", eqz_preset_10b[i]->psz_name );
511         }
512     }
513     return VLC_SUCCESS;
514 }
515
516 static int PreampCallback( vlc_object_t *p_this, char const *psz_cmd,
517                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
518 {
519     aout_filter_sys_t *p_sys = (aout_filter_sys_t *)p_data;
520
521     if( newval.f_float < -20.0 )
522         newval.f_float = -20.0;
523     else if( newval.f_float > 20.0 )
524         newval.f_float = 20.0;
525     p_sys->f_gamp = pow( 10, newval.f_float /20.0);
526
527     return VLC_SUCCESS;
528 }
529
530 static int BandsCallback( vlc_object_t *p_this, char const *psz_cmd,
531                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
532 {
533     aout_filter_sys_t *p_sys = (aout_filter_sys_t *)p_data;
534     char *psz_bands = newval.psz_string;
535
536     /* Same thing for bands */
537     if( *psz_bands )
538     {
539         char *p = psz_bands, *p_next;
540         int i;
541
542         for( i = 0; i < p_sys->i_band; i++ )
543         {
544             /* Read dB -20/20 */
545 #ifdef HAVE_STRTOF
546             float f = strtof( p, &p_next );
547 #else
548             float f = (float) strtod( p, &p_next );
549 #endif
550             if( !p_next || p_next == p ) break; /* strtof() failed */
551
552             p_sys->f_amp[i] = EqzConvertdB( f );
553
554             if( !*p ) break; /* end of line */
555             p=p_next+1;
556         }
557     }
558
559     return VLC_SUCCESS;
560 }