1 /*****************************************************************************
2 * normvol.c: volume normalizer
3 *****************************************************************************
4 * Copyright (C) 2001, 2006 VLC authors and VideoLAN
6 * Authors: Clément Stenac <zorglub@videolan.org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
26 * We should detect fast power increases and react faster to these
27 * This way, we can increase the buffer size to get a more stable filter */
30 /*****************************************************************************
32 *****************************************************************************/
40 #include <vlc_common.h>
41 #include <vlc_plugin.h>
44 #include <vlc_filter.h>
46 /*****************************************************************************
48 *****************************************************************************/
50 static int Open ( vlc_object_t
* );
51 static void Close ( filter_t
* );
52 static block_t
*DoWork( filter_t
*, block_t
* );
61 /*****************************************************************************
63 *****************************************************************************/
64 #define BUFF_TEXT N_("Number of audio buffers" )
65 #define BUFF_LONGTEXT N_("This is the number of audio buffers on which the " \
66 "power measurement is made. A higher number of buffers will " \
67 "increase the response time of the filter to a spike " \
68 "but will make it less sensitive to short variations." )
70 #define LEVEL_TEXT N_("Maximal volume level" )
71 #define LEVEL_LONGTEXT N_("If the average power over the last N buffers " \
72 "is higher than this value, the volume will be normalized. " \
73 "This value is a positive floating point number. A value " \
74 "between 0.5 and 10 seems sensible." )
77 set_description( N_("Volume normalizer") )
78 set_shortname( N_("Volume normalizer") )
79 set_category( CAT_AUDIO
)
80 set_subcategory( SUBCAT_AUDIO_AFILTER
)
81 add_shortcut( "volnorm" )
82 add_integer( "norm-buff-size", 20 ,BUFF_TEXT
, BUFF_LONGTEXT
,
84 add_float( "norm-max-level", 2.0, LEVEL_TEXT
,
85 LEVEL_LONGTEXT
, true )
86 set_capability( "audio filter", 0 )
90 /*****************************************************************************
91 * Open: initialize and create stuff
92 *****************************************************************************/
93 static int Open( vlc_object_t
*p_this
)
95 filter_t
*p_filter
= (filter_t
*)p_this
;
99 i_channels
= aout_FormatNbChannels( &p_filter
->fmt_in
.audio
);
101 p_sys
= p_filter
->p_sys
= malloc( sizeof( *p_sys
) );
104 p_sys
->i_nb
= var_CreateGetInteger( vlc_object_parent(p_filter
),
106 p_sys
->f_max
= var_CreateGetFloat( vlc_object_parent(p_filter
),
109 if( p_sys
->f_max
<= 0 ) p_sys
->f_max
= 0.01;
111 /* We need to store (nb_buffers+1)*nb_channels floats */
112 p_sys
->p_last
= calloc( i_channels
* (p_sys
->i_nb
+ 2), sizeof(float) );
119 p_filter
->fmt_in
.audio
.i_format
= VLC_CODEC_FL32
;
120 aout_FormatPrepare(&p_filter
->fmt_in
.audio
);
121 p_filter
->fmt_out
.audio
= p_filter
->fmt_in
.audio
;
122 static const struct vlc_filter_operations filter_ops
=
124 .filter_audio
= DoWork
, .close
= Close
,
126 p_filter
->ops
= &filter_ops
;
131 /*****************************************************************************
132 * DoWork : normalizes and sends a buffer
133 *****************************************************************************/
134 static block_t
*DoWork( filter_t
*p_filter
, block_t
*p_in_buf
)
141 int i_samples
= p_in_buf
->i_nb_samples
;
142 int i_channels
= aout_FormatNbChannels( &p_filter
->fmt_in
.audio
);
143 float *p_out
= (float*)p_in_buf
->p_buffer
;
144 float *p_in
= (float*)p_in_buf
->p_buffer
;
146 filter_sys_t
*p_sys
= p_filter
->p_sys
;
148 pf_sum
= calloc( i_channels
, sizeof(float) );
152 pf_gain
= vlc_alloc( i_channels
, sizeof(float) );
159 /* Calculate the average power level on this buffer */
160 for( i
= 0 ; i
< i_samples
; i
++ )
162 for( i_chan
= 0; i_chan
< i_channels
; i_chan
++ )
164 float f_sample
= p_in
[i_chan
];
165 pf_sum
[i_chan
] += f_sample
* f_sample
;
170 /* sum now contains for each channel the sigma(value²) */
171 for( i_chan
= 0; i_chan
< i_channels
; i_chan
++ )
173 /* Shift our lastbuff */
174 memmove( &p_sys
->p_last
[ i_chan
* p_sys
->i_nb
],
175 &p_sys
->p_last
[i_chan
* p_sys
->i_nb
+ 1],
176 (p_sys
->i_nb
-1) * sizeof( float ) );
178 /* Insert the new average : sqrt(sigma(value²)) */
179 p_sys
->p_last
[ i_chan
* p_sys
->i_nb
+ p_sys
->i_nb
- 1] =
180 sqrt( pf_sum
[i_chan
] );
184 /* Get the average power on the lastbuff */
186 for( i
= 0; i
< p_sys
->i_nb
; i
++)
188 f_average
+= p_sys
->p_last
[ i_chan
* p_sys
->i_nb
+ i
];
190 f_average
= f_average
/ p_sys
->i_nb
;
192 /* Seuil arbitraire */
193 p_sys
->f_max
= var_GetFloat( vlc_object_parent(p_filter
),
196 //fprintf(stderr,"Average %f, max %f\n", f_average, p_sys->f_max );
197 if( f_average
> p_sys
->f_max
)
199 pf_gain
[i_chan
] = f_average
/ p_sys
->f_max
;
208 for( i
= 0; i
< i_samples
; i
++)
210 for( i_chan
= 0; i_chan
< i_channels
; i_chan
++ )
212 p_out
[i_chan
] /= pf_gain
[i_chan
];
222 block_Release( p_in_buf
);
226 /**********************************************************************
228 **********************************************************************/
229 static void Close( filter_t
*p_filter
)
231 filter_sys_t
*p_sys
= p_filter
->p_sys
;
233 free( p_sys
->p_last
);