]> git.sesse.net Git - vlc/blob - modules/audio_filter/normvol.c
Add a bunch of help strings. Feel free to correct them, and add more
[vlc] / modules / audio_filter / normvol.c
1 /*****************************************************************************
2  * normvol.c: volume normalizer
3  *****************************************************************************
4  * Copyright (C) 2001, 2006 the VideoLAN team
5  * $Id$
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38
39 #include <errno.h>                                                 /* ENOMEM */
40 #include <ctype.h>
41
42 #include <math.h>
43
44
45 #include <vlc_common.h>
46 #include <vlc_plugin.h>
47
48 #include <vlc_aout.h>
49 #include <vlc_filter.h>
50
51 /*****************************************************************************
52  * Local prototypes
53  *****************************************************************************/
54
55 static int  Open     ( vlc_object_t * );
56 static void Close    ( vlc_object_t * );
57 static block_t *DoWork( filter_t *, block_t * );
58
59 struct filter_sys_t
60 {
61     int i_nb;
62     float *p_last;
63     float f_max;
64 };
65
66 /*****************************************************************************
67  * Module descriptor
68  *****************************************************************************/
69 #define BUFF_TEXT N_("Number of audio buffers" )
70 #define BUFF_LONGTEXT N_("This is the number of audio buffers on which the " \
71                 "power measurement is made. A higher number of buffers will " \
72                 "increase the response time of the filter to a spike " \
73                 "but will make it less sensitive to short variations." )
74
75 #define LEVEL_TEXT N_("Maximal volume level" )
76 #define LEVEL_LONGTEXT N_("If the average power over the last N buffers " \
77                "is higher than this value, the volume will be normalized. " \
78                "This value is a positive floating point number. A value " \
79                "between 0.5 and 10 seems sensible." )
80
81 vlc_module_begin ()
82     set_description( N_("Volume normalizer") )
83     set_shortname( N_("Volume normalizer") )
84     set_category( CAT_AUDIO )
85     set_subcategory( SUBCAT_AUDIO_AFILTER )
86     add_shortcut( "volnorm" )
87     add_integer( "norm-buff-size", 20 ,NULL ,BUFF_TEXT, BUFF_LONGTEXT,
88                  true )
89     add_float( "norm-max-level", 2.0, NULL, LEVEL_TEXT,
90                LEVEL_LONGTEXT, true )
91     set_capability( "audio filter", 0 )
92     set_callbacks( Open, Close )
93 vlc_module_end ()
94
95 /*****************************************************************************
96  * Open: initialize and create stuff
97  *****************************************************************************/
98 static int Open( vlc_object_t *p_this )
99 {
100     filter_t *p_filter = (filter_t*)p_this;
101     unsigned i_channels;
102     filter_sys_t *p_sys;
103
104     if( p_filter->fmt_in.audio.i_format != VLC_CODEC_FL32 ||
105         p_filter->fmt_out.audio.i_format != VLC_CODEC_FL32 )
106     {
107         p_filter->fmt_in.audio.i_format = VLC_CODEC_FL32;
108         p_filter->fmt_out.audio.i_format = VLC_CODEC_FL32;
109         msg_Warn( p_filter, "bad input or output format" );
110         return VLC_EGENERIC;
111     }
112
113     if ( !AOUT_FMTS_SIMILAR( &p_filter->fmt_in.audio, &p_filter->fmt_out.audio ) )
114     {
115         memcpy( &p_filter->fmt_out.audio, &p_filter->fmt_in.audio,
116                 sizeof(audio_sample_format_t) );
117         msg_Warn( p_filter, "input and output formats are not similar" );
118         return VLC_EGENERIC;
119     }
120
121     p_filter->pf_audio_filter = DoWork;
122
123     i_channels = aout_FormatNbChannels( &p_filter->fmt_in.audio );
124
125     p_sys = p_filter->p_sys = malloc( sizeof( *p_sys ) );
126     if( !p_sys )
127         return VLC_ENOMEM;
128     p_sys->i_nb = var_CreateGetInteger( p_filter->p_parent, "norm-buff-size" );
129     p_sys->f_max = var_CreateGetFloat( p_filter->p_parent, "norm-max-level" );
130
131     if( p_sys->f_max <= 0 ) p_sys->f_max = 0.01;
132
133     /* We need to store (nb_buffers+1)*nb_channels floats */
134     p_sys->p_last = calloc( i_channels * (p_filter->p_sys->i_nb + 2), sizeof(float) );
135     if( !p_sys->p_last )
136     {
137         free( p_sys );
138         return VLC_ENOMEM;
139     }
140
141     return VLC_SUCCESS;
142 }
143
144 /*****************************************************************************
145  * DoWork : normalizes and sends a buffer
146  *****************************************************************************/
147 static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
148 {
149     float *pf_sum;
150     float *pf_gain;
151     float f_average = 0;
152     int i, i_chan;
153
154     int i_samples = p_in_buf->i_nb_samples;
155     int i_channels = aout_FormatNbChannels( &p_filter->fmt_in.audio );
156     float *p_out = (float*)p_in_buf->p_buffer;
157     float *p_in =  (float*)p_in_buf->p_buffer;
158
159     struct filter_sys_t *p_sys = p_filter->p_sys;
160
161     pf_sum = calloc( i_channels, sizeof(float) );
162     if( !pf_sum )
163         goto out;
164
165     pf_gain = malloc( sizeof(float) * i_channels );
166     if( !pf_gain )
167     {
168         free( pf_sum );
169         goto out;
170     }
171
172     /* Calculate the average power level on this buffer */
173     for( i = 0 ; i < i_samples; i++ )
174     {
175         for( i_chan = 0; i_chan < i_channels; i_chan++ )
176         {
177             float f_sample = p_in[i_chan];
178             float f_square = pow( f_sample, 2 );
179             pf_sum[i_chan] += f_square;
180         }
181         p_in += i_channels;
182     }
183
184     /* sum now contains for each channel the sigma(value²) */
185     for( i_chan = 0; i_chan < i_channels; i_chan++ )
186     {
187         /* Shift our lastbuff */
188         memmove( &p_sys->p_last[ i_chan * p_sys->i_nb],
189                         &p_sys->p_last[i_chan * p_sys->i_nb + 1],
190                  (p_sys->i_nb-1) * sizeof( float ) );
191
192         /* Insert the new average : sqrt(sigma(value²)) */
193         p_sys->p_last[ i_chan * p_sys->i_nb + p_sys->i_nb - 1] =
194                 sqrt( pf_sum[i_chan] );
195
196         pf_sum[i_chan] = 0;
197
198         /* Get the average power on the lastbuff */
199         f_average = 0;
200         for( i = 0; i < p_sys->i_nb ; i++)
201         {
202             f_average += p_sys->p_last[ i_chan * p_sys->i_nb + i ];
203         }
204         f_average = f_average / p_sys->i_nb;
205
206         /* Seuil arbitraire */
207         p_sys->f_max = var_GetFloat( p_filter->p_parent, "norm-max-level" );
208
209         //fprintf(stderr,"Average %f, max %f\n", f_average, p_sys->f_max );
210         if( f_average > p_sys->f_max )
211         {
212              pf_gain[i_chan] = f_average / p_sys->f_max;
213         }
214         else
215         {
216            pf_gain[i_chan] = 1;
217         }
218     }
219
220     /* Apply gain */
221     for( i = 0; i < i_samples; i++)
222     {
223         for( i_chan = 0; i_chan < i_channels; i_chan++ )
224         {
225             p_out[i_chan] /= pf_gain[i_chan];
226         }
227         p_out += i_channels;
228     }
229
230     free( pf_sum );
231     free( pf_gain );
232
233     return p_in_buf;
234 out:
235     block_Release( p_in_buf );
236     return NULL;
237 }
238
239 /**********************************************************************
240  * Close
241  **********************************************************************/
242 static void Close( vlc_object_t *p_this )
243 {
244     filter_t *p_filter = (filter_t*)p_this;
245     filter_sys_t *p_sys = p_filter->p_sys;
246
247     free( p_sys->p_last );
248     free( p_sys );
249 }