| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
#ifdef HAVE_CONFIG_H |
|---|
| 36 |
# include "config.h" |
|---|
| 37 |
#endif |
|---|
| 38 |
|
|---|
| 39 |
#include <errno.h> |
|---|
| 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 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
static int Open ( vlc_object_t * ); |
|---|
| 56 |
static void Close ( vlc_object_t * ); |
|---|
| 57 |
static void DoWork ( aout_instance_t * , aout_filter_t *, |
|---|
| 58 |
aout_buffer_t * , aout_buffer_t *); |
|---|
| 59 |
|
|---|
| 60 |
typedef struct aout_filter_sys_t |
|---|
| 61 |
{ |
|---|
| 62 |
int i_nb; |
|---|
| 63 |
float *p_last; |
|---|
| 64 |
float f_max; |
|---|
| 65 |
} aout_filter_sys_t; |
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 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 |
|
|---|
| 98 |
|
|---|
| 99 |
static int Open( vlc_object_t *p_this ) |
|---|
| 100 |
{ |
|---|
| 101 |
aout_filter_t *p_filter = (aout_filter_t*)p_this; |
|---|
| 102 |
bool b_fit = true; |
|---|
| 103 |
int i_channels; |
|---|
| 104 |
aout_filter_sys_t *p_sys; |
|---|
| 105 |
|
|---|
| 106 |
if( p_filter->input.i_format != VLC_FOURCC('f','l','3','2' ) || |
|---|
| 107 |
p_filter->output.i_format != VLC_FOURCC('f','l','3','2') ) |
|---|
| 108 |
{ |
|---|
| 109 |
b_fit = false; |
|---|
| 110 |
p_filter->input.i_format = VLC_FOURCC('f','l','3','2'); |
|---|
| 111 |
p_filter->output.i_format = VLC_FOURCC('f','l','3','2'); |
|---|
| 112 |
msg_Warn( p_filter, "bad input or output format" ); |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) ) |
|---|
| 116 |
{ |
|---|
| 117 |
b_fit = false; |
|---|
| 118 |
memcpy( &p_filter->output, &p_filter->input, |
|---|
| 119 |
sizeof(audio_sample_format_t) ); |
|---|
| 120 |
msg_Warn( p_filter, "input and output formats are not similar" ); |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
if ( ! b_fit ) |
|---|
| 124 |
{ |
|---|
| 125 |
return VLC_EGENERIC; |
|---|
| 126 |
} |
|---|
| 127 |
|
|---|
| 128 |
p_filter->pf_do_work = DoWork; |
|---|
| 129 |
p_filter->b_in_place = true; |
|---|
| 130 |
|
|---|
| 131 |
i_channels = aout_FormatNbChannels( &p_filter->input ); |
|---|
| 132 |
|
|---|
| 133 |
p_sys = p_filter->p_sys = malloc( sizeof( aout_filter_sys_t ) ); |
|---|
| 134 |
if( !p_sys ) |
|---|
| 135 |
return VLC_ENOMEM; |
|---|
| 136 |
p_sys->i_nb = var_CreateGetInteger( p_filter->p_parent, "norm-buff-size" ); |
|---|
| 137 |
p_sys->f_max = var_CreateGetFloat( p_filter->p_parent, "norm-max-level" ); |
|---|
| 138 |
|
|---|
| 139 |
if( p_sys->f_max <= 0 ) p_sys->f_max = 0.01; |
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
p_sys->p_last = malloc( sizeof( float ) * (i_channels) * |
|---|
| 143 |
(p_filter->p_sys->i_nb + 2) ); |
|---|
| 144 |
if( !p_sys->p_last ) |
|---|
| 145 |
{ |
|---|
| 146 |
free( p_sys ); |
|---|
| 147 |
return VLC_ENOMEM; |
|---|
| 148 |
} |
|---|
| 149 |
memset( p_sys->p_last, 0 ,sizeof( float ) * (i_channels) * |
|---|
| 150 |
(p_filter->p_sys->i_nb + 2) ); |
|---|
| 151 |
return VLC_SUCCESS; |
|---|
| 152 |
} |
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
static void DoWork( aout_instance_t *p_aout, aout_filter_t *p_filter, |
|---|
| 158 |
aout_buffer_t *p_in_buf, aout_buffer_t *p_out_buf ) |
|---|
| 159 |
{ |
|---|
| 160 |
float *pf_sum; |
|---|
| 161 |
float *pf_gain; |
|---|
| 162 |
float f_average = 0; |
|---|
| 163 |
int i, i_chan; |
|---|
| 164 |
|
|---|
| 165 |
int i_samples = p_in_buf->i_nb_samples; |
|---|
| 166 |
int i_channels = aout_FormatNbChannels( &p_filter->input ); |
|---|
| 167 |
float *p_out = (float*)p_out_buf->p_buffer; |
|---|
| 168 |
float *p_in = (float*)p_in_buf->p_buffer; |
|---|
| 169 |
|
|---|
| 170 |
struct aout_filter_sys_t *p_sys = p_filter->p_sys; |
|---|
| 171 |
|
|---|
| 172 |
pf_sum = malloc( sizeof(float) * i_channels ); |
|---|
| 173 |
if( !pf_sum ) |
|---|
| 174 |
return; |
|---|
| 175 |
memset( pf_sum, 0, sizeof(float) * i_channels ); |
|---|
| 176 |
|
|---|
| 177 |
pf_gain = malloc( sizeof(float) * i_channels ); |
|---|
| 178 |
if( !pf_gain ) |
|---|
| 179 |
{ |
|---|
| 180 |
free( pf_sum ); |
|---|
| 181 |
return; |
|---|
| 182 |
} |
|---|
| 183 |
|
|---|
| 184 |
p_out_buf->i_nb_samples = p_in_buf->i_nb_samples; |
|---|
| 185 |
p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes; |
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
for( i = 0 ; i < i_samples; i++ ) |
|---|
| 189 |
{ |
|---|
| 190 |
for( i_chan = 0; i_chan < i_channels; i_chan++ ) |
|---|
| 191 |
{ |
|---|
| 192 |
float f_sample = p_in[i_chan]; |
|---|
| 193 |
float f_square = pow( f_sample, 2 ); |
|---|
| 194 |
pf_sum[i_chan] += f_square; |
|---|
| 195 |
} |
|---|
| 196 |
p_in += i_channels; |
|---|
| 197 |
} |
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 |
for( i_chan = 0; i_chan < i_channels; i_chan++ ) |
|---|
| 201 |
{ |
|---|
| 202 |
|
|---|
| 203 |
memmove( &p_sys->p_last[ i_chan * p_sys->i_nb], |
|---|
| 204 |
&p_sys->p_last[i_chan * p_sys->i_nb + 1], |
|---|
| 205 |
(p_sys->i_nb-1) * sizeof( float ) ); |
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
p_sys->p_last[ i_chan * p_sys->i_nb + p_sys->i_nb - 1] = |
|---|
| 209 |
sqrt( pf_sum[i_chan] ); |
|---|
| 210 |
|
|---|
| 211 |
pf_sum[i_chan] = 0; |
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
f_average = 0; |
|---|
| 215 |
for( i = 0; i < p_sys->i_nb ; i++) |
|---|
| 216 |
{ |
|---|
| 217 |
f_average += p_sys->p_last[ i_chan * p_sys->i_nb + i ]; |
|---|
| 218 |
} |
|---|
| 219 |
f_average = f_average / p_sys->i_nb; |
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
p_sys->f_max = var_GetFloat( p_aout, "norm-max-level" ); |
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 |
if( f_average > p_sys->f_max ) |
|---|
| 226 |
{ |
|---|
| 227 |
pf_gain[i_chan] = f_average / p_sys->f_max; |
|---|
| 228 |
} |
|---|
| 229 |
else |
|---|
| 230 |
{ |
|---|
| 231 |
pf_gain[i_chan] = 1; |
|---|
| 232 |
} |
|---|
| 233 |
} |
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
for( i = 0; i < i_samples; i++) |
|---|
| 237 |
{ |
|---|
| 238 |
for( i_chan = 0; i_chan < i_channels; i_chan++ ) |
|---|
| 239 |
{ |
|---|
| 240 |
p_out[i_chan] /= pf_gain[i_chan]; |
|---|
| 241 |
} |
|---|
| 242 |
p_out += i_channels; |
|---|
| 243 |
} |
|---|
| 244 |
|
|---|
| 245 |
free( pf_sum ); |
|---|
| 246 |
free( pf_gain ); |
|---|
| 247 |
|
|---|
| 248 |
return; |
|---|
| 249 |
} |
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
static void Close( vlc_object_t *p_this ) |
|---|
| 255 |
{ |
|---|
| 256 |
aout_filter_t *p_filter = (aout_filter_t*)p_this; |
|---|
| 257 |
aout_filter_sys_t *p_sys = p_filter->p_sys; |
|---|
| 258 |
|
|---|
| 259 |
if( p_sys ) |
|---|
| 260 |
{ |
|---|
| 261 |
free( p_sys->p_last ); |
|---|
| 262 |
free( p_sys ); |
|---|
| 263 |
} |
|---|
| 264 |
} |
|---|