]> git.sesse.net Git - vlc/blob - modules/audio_filter/normvol.c
Fixed norm-max-level description
[vlc] / modules / audio_filter / normvol.c
1 /*****************************************************************************
2  * normvol.c :  volume normalizer
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: equalizer.c,v 1.21 2003/07/31 23:44:49 zorglub Exp $
6  *
7  * Authors: Clément Stenac <zorglub@videolan.org>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*
25  * TODO:
26  *
27  * We should detect fast power increases and react faster to these
28  * This way, we can increase the buffer size to get a more stable filter */
29
30
31 /*****************************************************************************
32  * Preamble
33  *****************************************************************************/
34 #include <stdlib.h>                                      /* malloc(), free() */
35 #include <string.h>
36
37 #include <errno.h>                                                 /* ENOMEM */
38 #include <stdio.h>
39 #include <ctype.h>
40 #include <signal.h>
41
42 #include <math.h>
43
44
45 #include <vlc/vlc.h>
46 #include <vlc/aout.h>
47
48 #include <aout_internal.h>
49
50 /*****************************************************************************
51  * Local prototypes
52  *****************************************************************************/
53
54 static int  Open     ( vlc_object_t * );
55 static void Close    ( vlc_object_t * );
56 static void DoWork   ( aout_instance_t * , aout_filter_t *,
57                 aout_buffer_t * , aout_buffer_t *);
58
59 struct aout_filter_sys_t {
60     int i_nb;
61     float *p_last;
62     float f_max;
63 };
64
65 /*****************************************************************************
66  * Module descriptor
67  *****************************************************************************/
68 #define BUFF_TEXT N_("Number of audio buffers" )
69 #define BUFF_LONGTEXT N_("This is the number of audio buffers on which the " \
70                 "power measurement is made. A higher number of buffers will " \
71                 "increase the response time of the filter to a high " \
72                 "power but will make it less sensitive to short variations " )
73
74 #define LEVEL_TEXT N_("Max level" )
75 #define LEVEL_LONGTEXT N_("If the average power over the last N buffers " \
76                "is higher than this value, the volume will be normalized." \
77                "This value is a positive floating point number. A value " \
78                "between 0.5 and 10 seems sensible." )
79
80 vlc_module_begin();
81     set_description( _("Volume normalizer") );
82     add_shortcut("volnorm");
83     add_integer("norm-buff-size" , 20 , NULL , BUFF_TEXT, BUFF_LONGTEXT,
84                 VLC_TRUE);
85     add_float("norm-max-level",2.0,NULL,LEVEL_TEXT,LEVEL_LONGTEXT,VLC_TRUE);
86     set_capability( "audio filter", 0 );
87     set_callbacks( Open, Close );
88 vlc_module_end();
89
90 /*****************************************************************************
91  * Open: initialize and create stuff
92  *****************************************************************************/
93 static int Open( vlc_object_t *p_this )
94 {
95     aout_filter_t *p_filter = (aout_filter_t*)p_this;
96
97     struct aout_filter_sys_t *p_sys = p_filter->p_sys =
98             malloc( sizeof( struct aout_filter_sys_t ) );
99
100     if( p_filter->input.i_format != VLC_FOURCC('f','l','3','2' ) ||
101         p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
102     {
103             msg_Warn( p_filter, "Bad input or output format" );
104             return VLC_EGENERIC;
105     }
106
107     if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
108     {
109         msg_Warn( p_filter, "input and output formats are not similar" );
110         return VLC_EGENERIC;
111     }
112
113     p_filter->pf_do_work = DoWork;
114     p_filter->b_in_place = VLC_TRUE;
115
116     int i_channels = aout_FormatNbChannels( &p_filter->input );
117
118     p_sys->i_nb =  var_CreateGetInteger( p_filter->p_parent, "norm-buff-size" );
119     p_sys->f_max = var_CreateGetFloat( p_filter->p_parent, "norm-max-level" );
120
121     if( p_sys->f_max <= 0 )
122     {
123         p_sys->f_max = 0.01;
124     }
125
126     /* We need to store (nb_buffers+1)*nb_channels floats */
127     p_sys->p_last = malloc( sizeof( float ) * (i_channels) *
128                             (p_filter->p_sys->i_nb + 2) );
129     memset( p_sys->p_last, 0 , sizeof( float ) * (i_channels) *
130                            (p_filter->p_sys->i_nb + 2) );
131     return VLC_SUCCESS;
132 }
133
134 /*****************************************************************************
135  * DoWork : normalizes and sends a buffer
136  *****************************************************************************/
137  static void DoWork( aout_instance_t *p_aout, aout_filter_t *p_filter,
138                      aout_buffer_t *p_in_buf, aout_buffer_t *p_out_buf )
139 {
140     float *pf_sum;
141     float *pf_gain;
142     float f_average = 0;
143     int i, i_chan;
144
145     int i_samples = p_in_buf->i_nb_samples;
146     int i_channels = aout_FormatNbChannels( &p_filter->input );
147     float *p_out = (float*)p_out_buf->p_buffer;
148     float *p_in =  (float*)p_in_buf->p_buffer;
149
150     struct aout_filter_sys_t *p_sys = p_filter->p_sys;
151
152     pf_sum = (float *)malloc( sizeof(float) * i_channels );
153     memset( pf_sum, 0, sizeof(float) * i_channels );
154
155     pf_gain = (float *)malloc( sizeof(float) * i_channels );
156
157     p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
158     p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes;
159
160    /* Calculate the average power level on this buffer */
161    for( i = 0 ; i < i_samples; i++ )
162    {
163         for( i_chan = 0; i_chan < i_channels; i_chan++ )
164         {
165             float f_sample = p_in[i_chan];
166             float f_square = pow( f_sample, 2 );
167             pf_sum[i_chan] += f_square;
168         }
169         p_in += i_channels;
170    }
171
172    /* sum now contains for each channel the sigma(value²) */
173    for( i_chan = 0; i_chan < i_channels; i_chan++ )
174    {
175         /* Shift our lastbuff */
176         memmove( &p_sys->p_last[ i_chan * p_sys->i_nb],
177                         &p_sys->p_last[i_chan * p_sys->i_nb + 1],
178                  (p_sys->i_nb-1) * sizeof( float ) );
179
180         /* Insert the new average : sqrt(sigma(value²)) */
181         p_sys->p_last[ i_chan * p_sys->i_nb + p_sys->i_nb - 1] =
182                 sqrt( pf_sum[i_chan] );
183
184         pf_sum[i_chan] = 0;
185
186         /* Get the average power on the lastbuff */
187         f_average = 0;
188         for( i = 0; i < p_sys->i_nb ; i++)
189         {
190             f_average += p_sys->p_last[ i_chan * p_sys->i_nb + i ];
191         }
192         f_average = f_average / p_sys->i_nb;
193
194         /* Seuil arbitraire */
195         p_sys->f_max = var_GetFloat( p_aout, "norm-max-level" );
196
197 //        fprintf(stderr,"Average %f, max %f\n", f_average, p_sys->f_max );
198         if( f_average > p_sys->f_max )
199         {
200              pf_gain[i_chan] = f_average / p_sys->f_max;
201         }
202         else
203         {
204            pf_gain[i_chan] = 1;
205         }
206     }
207
208     /* Apply gain */
209     for( i = 0 ; i < i_samples ; i++)
210     {
211         for( i_chan = 0; i_chan < i_channels; i_chan++ )
212         {
213             p_out[i_chan] /= pf_gain[i_chan];
214         }
215         p_out += i_channels;
216     }
217
218     free( pf_sum );
219     free( pf_gain );
220
221     return;
222 }
223
224 /**********************************************************************
225  * Close
226  **********************************************************************/
227 static void Close( vlc_object_t *p_this )
228 {
229     aout_filter_t *p_filter = (aout_filter_t*)p_this;
230     struct aout_filter_sys_t *p_sys = p_filter->p_sys;
231
232     if( p_sys )
233     {
234         if( p_sys->p_last)
235         {
236             free( p_sys->p_last );
237         }
238         free( p_sys );
239     }
240 }