qt: playlist: use item title if available
[vlc.git] / modules / audio_filter / normvol.c
blob2e8796655c1cf1f323bbfd5e3dfb5552568a5c87
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 *****************************************************************************/
24 * TODO:
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 /*****************************************************************************
31 * Preamble
32 *****************************************************************************/
34 #ifdef HAVE_CONFIG_H
35 # include "config.h"
36 #endif
38 #include <math.h>
40 #include <vlc_common.h>
41 #include <vlc_plugin.h>
43 #include <vlc_aout.h>
44 #include <vlc_filter.h>
46 /*****************************************************************************
47 * Local prototypes
48 *****************************************************************************/
50 static int Open ( vlc_object_t * );
51 static void Close ( filter_t * );
52 static block_t *DoWork( filter_t *, block_t * );
54 typedef struct
56 int i_nb;
57 float *p_last;
58 float f_max;
59 } filter_sys_t;
61 /*****************************************************************************
62 * Module descriptor
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." )
76 vlc_module_begin ()
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,
83 true )
84 add_float( "norm-max-level", 2.0, LEVEL_TEXT,
85 LEVEL_LONGTEXT, true )
86 set_capability( "audio filter", 0 )
87 set_callback( Open )
88 vlc_module_end ()
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;
96 unsigned i_channels;
97 filter_sys_t *p_sys;
99 i_channels = aout_FormatNbChannels( &p_filter->fmt_in.audio );
101 p_sys = p_filter->p_sys = malloc( sizeof( *p_sys ) );
102 if( !p_sys )
103 return VLC_ENOMEM;
104 p_sys->i_nb = var_CreateGetInteger( vlc_object_parent(p_filter),
105 "norm-buff-size" );
106 p_sys->f_max = var_CreateGetFloat( vlc_object_parent(p_filter),
107 "norm-max-level" );
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) );
113 if( !p_sys->p_last )
115 free( p_sys );
116 return VLC_ENOMEM;
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;
128 return VLC_SUCCESS;
131 /*****************************************************************************
132 * DoWork : normalizes and sends a buffer
133 *****************************************************************************/
134 static block_t *DoWork( filter_t *p_filter, block_t *p_in_buf )
136 float *pf_sum;
137 float *pf_gain;
138 float f_average = 0;
139 int i, i_chan;
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) );
149 if( !pf_sum )
150 goto out;
152 pf_gain = vlc_alloc( i_channels, sizeof(float) );
153 if( !pf_gain )
155 free( pf_sum );
156 goto out;
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;
167 p_in += i_channels;
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] );
182 pf_sum[i_chan] = 0;
184 /* Get the average power on the lastbuff */
185 f_average = 0;
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),
194 "norm-max-level" );
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;
201 else
203 pf_gain[i_chan] = 1;
207 /* Apply gain */
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];
214 p_out += i_channels;
217 free( pf_sum );
218 free( pf_gain );
220 return p_in_buf;
221 out:
222 block_Release( p_in_buf );
223 return NULL;
226 /**********************************************************************
227 * Close
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 );
234 free( p_sys );