input: add an input_item_t arg to input_CreateFilename()
[vlc.git] / modules / audio_filter / normvol.c
blob52c400b09ed9e88aaac3dd0a8514acbc9c28f0bc
1 /*****************************************************************************
2 * normvol.c: volume normalizer
3 *****************************************************************************
4 * Copyright (C) 2001, 2006 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
25 * TODO:
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 */
31 /*****************************************************************************
32 * Preamble
33 *****************************************************************************/
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
39 #include <math.h>
41 #include <vlc_common.h>
42 #include <vlc_plugin.h>
44 #include <vlc_aout.h>
45 #include <vlc_filter.h>
47 /*****************************************************************************
48 * Local prototypes
49 *****************************************************************************/
51 static int Open ( vlc_object_t * );
52 static void Close ( vlc_object_t * );
53 static block_t *DoWork( filter_t *, block_t * );
55 typedef struct
57 int i_nb;
58 float *p_last;
59 float f_max;
60 } filter_sys_t;
62 /*****************************************************************************
63 * Module descriptor
64 *****************************************************************************/
65 #define BUFF_TEXT N_("Number of audio buffers" )
66 #define BUFF_LONGTEXT N_("This is the number of audio buffers on which the " \
67 "power measurement is made. A higher number of buffers will " \
68 "increase the response time of the filter to a spike " \
69 "but will make it less sensitive to short variations." )
71 #define LEVEL_TEXT N_("Maximal volume level" )
72 #define LEVEL_LONGTEXT N_("If the average power over the last N buffers " \
73 "is higher than this value, the volume will be normalized. " \
74 "This value is a positive floating point number. A value " \
75 "between 0.5 and 10 seems sensible." )
77 vlc_module_begin ()
78 set_description( N_("Volume normalizer") )
79 set_shortname( N_("Volume normalizer") )
80 set_category( CAT_AUDIO )
81 set_subcategory( SUBCAT_AUDIO_AFILTER )
82 add_shortcut( "volnorm" )
83 add_integer( "norm-buff-size", 20 ,BUFF_TEXT, BUFF_LONGTEXT,
84 true )
85 add_float( "norm-max-level", 2.0, LEVEL_TEXT,
86 LEVEL_LONGTEXT, true )
87 set_capability( "audio filter", 0 )
88 set_callbacks( Open, Close )
89 vlc_module_end ()
91 /*****************************************************************************
92 * Open: initialize and create stuff
93 *****************************************************************************/
94 static int Open( vlc_object_t *p_this )
96 filter_t *p_filter = (filter_t*)p_this;
97 unsigned i_channels;
98 filter_sys_t *p_sys;
100 i_channels = aout_FormatNbChannels( &p_filter->fmt_in.audio );
102 p_sys = p_filter->p_sys = malloc( sizeof( *p_sys ) );
103 if( !p_sys )
104 return VLC_ENOMEM;
105 p_sys->i_nb = var_CreateGetInteger( p_filter->obj.parent,
106 "norm-buff-size" );
107 p_sys->f_max = var_CreateGetFloat( p_filter->obj.parent,
108 "norm-max-level" );
110 if( p_sys->f_max <= 0 ) p_sys->f_max = 0.01;
112 /* We need to store (nb_buffers+1)*nb_channels floats */
113 p_sys->p_last = calloc( i_channels * (p_sys->i_nb + 2), sizeof(float) );
114 if( !p_sys->p_last )
116 free( p_sys );
117 return VLC_ENOMEM;
120 p_filter->fmt_in.audio.i_format = VLC_CODEC_FL32;
121 aout_FormatPrepare(&p_filter->fmt_in.audio);
122 p_filter->fmt_out.audio = p_filter->fmt_in.audio;
123 p_filter->pf_audio_filter = DoWork;
125 return VLC_SUCCESS;
128 /*****************************************************************************
129 * DoWork : normalizes and sends a buffer
130 *****************************************************************************/
131 static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
133 float *pf_sum;
134 float *pf_gain;
135 float f_average = 0;
136 int i, i_chan;
138 int i_samples = p_in_buf->i_nb_samples;
139 int i_channels = aout_FormatNbChannels( &p_filter->fmt_in.audio );
140 float *p_out = (float*)p_in_buf->p_buffer;
141 float *p_in = (float*)p_in_buf->p_buffer;
143 filter_sys_t *p_sys = p_filter->p_sys;
145 pf_sum = calloc( i_channels, sizeof(float) );
146 if( !pf_sum )
147 goto out;
149 pf_gain = vlc_alloc( i_channels, sizeof(float) );
150 if( !pf_gain )
152 free( pf_sum );
153 goto out;
156 /* Calculate the average power level on this buffer */
157 for( i = 0 ; i < i_samples; i++ )
159 for( i_chan = 0; i_chan < i_channels; i_chan++ )
161 float f_sample = p_in[i_chan];
162 pf_sum[i_chan] += f_sample * f_sample;
164 p_in += i_channels;
167 /* sum now contains for each channel the sigma(value²) */
168 for( i_chan = 0; i_chan < i_channels; i_chan++ )
170 /* Shift our lastbuff */
171 memmove( &p_sys->p_last[ i_chan * p_sys->i_nb],
172 &p_sys->p_last[i_chan * p_sys->i_nb + 1],
173 (p_sys->i_nb-1) * sizeof( float ) );
175 /* Insert the new average : sqrt(sigma(value²)) */
176 p_sys->p_last[ i_chan * p_sys->i_nb + p_sys->i_nb - 1] =
177 sqrt( pf_sum[i_chan] );
179 pf_sum[i_chan] = 0;
181 /* Get the average power on the lastbuff */
182 f_average = 0;
183 for( i = 0; i < p_sys->i_nb ; i++)
185 f_average += p_sys->p_last[ i_chan * p_sys->i_nb + i ];
187 f_average = f_average / p_sys->i_nb;
189 /* Seuil arbitraire */
190 p_sys->f_max = var_GetFloat( p_filter->obj.parent,
191 "norm-max-level" );
193 //fprintf(stderr,"Average %f, max %f\n", f_average, p_sys->f_max );
194 if( f_average > p_sys->f_max )
196 pf_gain[i_chan] = f_average / p_sys->f_max;
198 else
200 pf_gain[i_chan] = 1;
204 /* Apply gain */
205 for( i = 0; i < i_samples; i++)
207 for( i_chan = 0; i_chan < i_channels; i_chan++ )
209 p_out[i_chan] /= pf_gain[i_chan];
211 p_out += i_channels;
214 free( pf_sum );
215 free( pf_gain );
217 return p_in_buf;
218 out:
219 block_Release( p_in_buf );
220 return NULL;
223 /**********************************************************************
224 * Close
225 **********************************************************************/
226 static void Close( vlc_object_t *p_this )
228 filter_t *p_filter = (filter_t*)p_this;
229 filter_sys_t *p_sys = p_filter->p_sys;
231 free( p_sys->p_last );
232 free( p_sys );