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