1 /*****************************************************************************
2 * normvol.c : volume normalizer
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
7 * Authors: Clément Stenac <zorglub@videolan.org>
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.
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.
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 *****************************************************************************/
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 /*****************************************************************************
33 *****************************************************************************/
34 #include <stdlib.h> /* malloc(), free() */
37 #include <errno.h> /* ENOMEM */
48 #include <aout_internal.h>
50 /*****************************************************************************
52 *****************************************************************************/
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 *);
59 typedef struct aout_filter_sys_t
66 /*****************************************************************************
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 high " \
73 "power but will make it less sensitive to short variations." )
75 #define LEVEL_TEXT N_("Max 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." )
82 set_description( _("Volume normalizer") );
83 set_category( CAT_AUDIO );
84 set_subcategory( SUBCAT_AUDIO_AFILTER );
85 add_shortcut( "volnorm" );
86 add_integer( "norm-buff-size", 20 ,NULL ,BUFF_TEXT, BUFF_LONGTEXT,
88 add_float( "norm-max-level", 2.0, NULL, LEVEL_TEXT,
89 LEVEL_LONGTEXT, VLC_TRUE );
90 set_capability( "audio filter", 0 );
91 set_callbacks( Open, Close );
94 /*****************************************************************************
95 * Open: initialize and create stuff
96 *****************************************************************************/
97 static int Open( vlc_object_t *p_this )
99 aout_filter_t *p_filter = (aout_filter_t*)p_this;
101 aout_filter_sys_t *p_sys = p_filter->p_sys =
102 malloc( sizeof( aout_filter_sys_t ) );
104 if( p_filter->input.i_format != VLC_FOURCC('f','l','3','2' ) ||
105 p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )
107 msg_Warn( p_filter, "Bad input or output format" );
111 if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )
113 msg_Warn( p_filter, "input and output formats are not similar" );
117 p_filter->pf_do_work = DoWork;
118 p_filter->b_in_place = VLC_TRUE;
120 i_channels = aout_FormatNbChannels( &p_filter->input );
122 p_sys->i_nb = var_CreateGetInteger( p_filter->p_parent, "norm-buff-size" );
123 p_sys->f_max = var_CreateGetFloat( p_filter->p_parent, "norm-max-level" );
125 if( p_sys->f_max <= 0 ) p_sys->f_max = 0.01;
127 /* We need to store (nb_buffers+1)*nb_channels floats */
128 p_sys->p_last = malloc( sizeof( float ) * (i_channels) *
129 (p_filter->p_sys->i_nb + 2) );
130 memset( p_sys->p_last, 0 ,sizeof( float ) * (i_channels) *
131 (p_filter->p_sys->i_nb + 2) );
135 /*****************************************************************************
136 * DoWork : normalizes and sends a buffer
137 *****************************************************************************/
138 static void DoWork( aout_instance_t *p_aout, aout_filter_t *p_filter,
139 aout_buffer_t *p_in_buf, aout_buffer_t *p_out_buf )
146 int i_samples = p_in_buf->i_nb_samples;
147 int i_channels = aout_FormatNbChannels( &p_filter->input );
148 float *p_out = (float*)p_out_buf->p_buffer;
149 float *p_in = (float*)p_in_buf->p_buffer;
151 struct aout_filter_sys_t *p_sys = p_filter->p_sys;
153 pf_sum = (float *)malloc( sizeof(float) * i_channels );
154 memset( pf_sum, 0, sizeof(float) * i_channels );
156 pf_gain = (float *)malloc( sizeof(float) * i_channels );
158 p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;
159 p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes;
161 /* Calculate the average power level on this buffer */
162 for( i = 0 ; i < i_samples; i++ )
164 for( i_chan = 0; i_chan < i_channels; i_chan++ )
166 float f_sample = p_in[i_chan];
167 float f_square = pow( f_sample, 2 );
168 pf_sum[i_chan] += f_square;
173 /* sum now contains for each channel the sigma(value²) */
174 for( i_chan = 0; i_chan < i_channels; i_chan++ )
176 /* Shift our lastbuff */
177 memmove( &p_sys->p_last[ i_chan * p_sys->i_nb],
178 &p_sys->p_last[i_chan * p_sys->i_nb + 1],
179 (p_sys->i_nb-1) * sizeof( float ) );
181 /* Insert the new average : sqrt(sigma(value²)) */
182 p_sys->p_last[ i_chan * p_sys->i_nb + p_sys->i_nb - 1] =
183 sqrt( pf_sum[i_chan] );
187 /* Get the average power on the lastbuff */
189 for( i = 0; i < p_sys->i_nb ; i++)
191 f_average += p_sys->p_last[ i_chan * p_sys->i_nb + i ];
193 f_average = f_average / p_sys->i_nb;
195 /* Seuil arbitraire */
196 p_sys->f_max = var_GetFloat( p_aout, "norm-max-level" );
198 //fprintf(stderr,"Average %f, max %f\n", f_average, p_sys->f_max );
199 if( f_average > p_sys->f_max )
201 pf_gain[i_chan] = f_average / p_sys->f_max;
210 for( i = 0; i < i_samples; i++)
212 for( i_chan = 0; i_chan < i_channels; i_chan++ )
214 p_out[i_chan] /= pf_gain[i_chan];
225 /**********************************************************************
227 **********************************************************************/
228 static void Close( vlc_object_t *p_this )
230 aout_filter_t *p_filter = (aout_filter_t*)p_this;
231 aout_filter_sys_t *p_sys = p_filter->p_sys;
235 if( p_sys->p_last) free( p_sys->p_last );