2 * filter_volume.c -- adjust audio volume
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "filter_volume.h"
23 #include <framework/mlt_frame.h>
31 #define MAX_CHANNELS 6
32 #define EPSILON 0.00001
34 /* The normalise functions come from the normalize utility:
35 Copyright (C) 1999--2002 Chris Vaill */
40 # define ROUND(x) floor((x) + 0.5)
43 #define DBFSTOAMP(x) pow(10,(x)/20.0)
45 /** Return nonzero if the two strings are equal, ignoring case, up to
46 the first n characters.
48 int strncaseeq(const char *s1, const char *s2, size_t n)
52 if (tolower(*s1++) != tolower(*s2++))
60 / tanh((x + lev) / (1-lev)) * (1-lev) - lev (for x < -lev)
62 x' = | x (for |x| <= lev)
64 \ tanh((x - lev) / (1-lev)) * (1-lev) + lev (for x > lev)
66 With limiter level = 0, this is equivalent to a tanh() function;
67 with limiter level = 1, this is equivalent to clipping.
69 static inline double limiter( double x, double lmtr_lvl )
74 xp = tanh((x + lmtr_lvl) / (1-lmtr_lvl)) * (1-lmtr_lvl) - lmtr_lvl;
75 else if (x > lmtr_lvl)
76 xp = tanh((x - lmtr_lvl) / (1-lmtr_lvl)) * (1-lmtr_lvl) + lmtr_lvl;
79 // fprintf( stderr, "filter_volume: sample %f limited %f\n", x, xp );
85 /** Takes a full smoothing window, and returns the value of the center
88 Currently, just does a mean filter, but we could do a median or
89 gaussian filter here instead.
91 static inline double get_smoothed_data( double *buf, int count )
96 for ( i = 0, j = 0; i < count; i++ )
98 if ( buf[ i ] != -1.0 )
100 smoothed += buf[ i ];
105 // fprintf( stderr, "smoothed over %d values, result %f\n", j, smoothed );
110 /** Get the max power level (using RMS) and peak level of the audio segment.
112 double signal_max_power( int16_t *buffer, int channels, int samples, int16_t *peak )
114 // Determine numeric limits
115 int bytes_per_samp = (samp_width - 1) / 8 + 1;
116 int16_t max = (1 << (bytes_per_samp * 8 - 1)) - 1;
117 int16_t min = -max - 1;
119 double *sums = (double *) calloc( channels, sizeof(double) );
122 double pow, maxpow = 0;
124 /* initialize peaks to effectively -inf and +inf */
125 int16_t max_sample = min;
126 int16_t min_sample = max;
128 for ( i = 0; i < samples; i++ )
130 for ( c = 0; c < channels; c++ )
133 sums[ c ] += (double) sample * (double) sample;
136 if ( sample > max_sample )
138 else if ( sample < min_sample )
142 for ( c = 0; c < channels; c++ )
144 pow = sums[ c ] / (double) samples;
151 /* scale the pow value to be in the range 0.0 -- 1.0 */
152 maxpow /= ( (double) min * (double) min);
154 if ( -min_sample > max_sample )
155 *peak = min_sample / (double) min;
157 *peak = max_sample / (double) max;
159 return sqrt( maxpow );
162 /* ------ End normalize functions --------------------------------------- */
167 static int filter_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
169 // Get the properties of the a frame
170 mlt_properties properties = mlt_frame_properties( frame );
171 double gain = mlt_properties_get_double( properties, "volume.gain" );
172 double max_gain = mlt_properties_get_double( properties, "volume.max_gain" );
173 double limiter_level = 0.5; /* -6 dBFS */
174 int normalise = mlt_properties_get_int( properties, "volume.normalise" );
175 double amplitude = mlt_properties_get_double( properties, "volume.amplitude" );
180 // Get the filter from the frame
181 mlt_filter this = mlt_properties_get_data( properties, "filter_volume", NULL );
183 // Get the properties from the filter
184 mlt_properties filter_props = mlt_filter_properties( this );
186 if ( mlt_properties_get( properties, "volume.limiter" ) != NULL )
187 limiter_level = mlt_properties_get_double( properties, "volume.limiter" );
189 // Restore the original get_audio
190 frame->get_audio = mlt_properties_get_data( properties, "volume.get_audio", NULL );
192 // Get the producer's audio
193 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
194 // fprintf( stderr, "filter_volume: frequency %d\n", *frequency );
196 // Determine numeric limits
197 int bytes_per_samp = (samp_width - 1) / 8 + 1;
198 int samplemax = (1 << (bytes_per_samp * 8 - 1)) - 1;
199 int samplemin = -samplemax - 1;
203 int window = mlt_properties_get_int( filter_props, "window" );
204 double *smooth_buffer = mlt_properties_get_data( filter_props, "smooth_buffer", NULL );
205 int *smooth_index = mlt_properties_get_data( filter_props, "smooth_index", NULL );
207 if ( window > 0 && smooth_buffer != NULL )
209 // Compute the signal power and put into smoothing buffer
210 smooth_buffer[ *smooth_index ] = signal_max_power( *buffer, *channels, *samples, &peak );
211 // fprintf( stderr, "filter_volume: raw power %f ", smooth_buffer[ *smooth_index ] );
212 if ( smooth_buffer[ *smooth_index ] > EPSILON )
214 *smooth_index = ( *smooth_index + 1 ) % window;
216 // Smooth the data and compute the gain
217 // fprintf( stderr, "smoothed %f over %d frames\n", get_smoothed_data( smooth_buffer, window ), window );
218 gain *= amplitude / get_smoothed_data( smooth_buffer, window );
223 gain *= amplitude / signal_max_power( *buffer, *channels, *samples, &peak );
227 // if ( gain > 1.0 && normalise )
228 // fprintf(stderr, "filter_volume: limiter level %f gain %f\n", limiter_level, gain );
230 if ( max_gain > 0 && gain > max_gain )
233 // Initialise filter's previous gain value to prevent an inadvertant jump from 0
234 if ( mlt_properties_get( filter_props, "previous_gain" ) == NULL )
235 mlt_properties_set_double( filter_props, "previous_gain", gain );
237 // Start the gain out at the previous
238 double previous_gain = mlt_properties_get_double( filter_props, "previous_gain" );
240 // Determine ramp increment
241 double gain_step = ( gain - previous_gain ) / *samples;
242 // fprintf( stderr, "filter_volume: previous gain %f current gain %f step %f\n", previous_gain, gain, gain_step );
244 // Save the current gain for the next iteration
245 mlt_properties_set_double( filter_props, "previous_gain", gain );
247 // Ramp from the previous gain to the current
248 gain = previous_gain;
250 int16_t *p = *buffer;
253 for ( i = 0; i < *samples; i++ )
255 for ( j = 0; j < *channels; j++ )
258 *p = ROUND( sample );
262 /* use limiter function instead of clipping */
264 *p = ROUND( samplemax * limiter( sample / (double) samplemax, limiter_level ) );
266 /* perform clipping */
267 else if ( sample > samplemax )
269 else if ( sample < samplemin )
280 /** Filter processing.
283 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
285 mlt_properties properties = mlt_frame_properties( frame );
286 mlt_properties filter_props = mlt_filter_properties( this );
288 // Parse the gain property
289 if ( mlt_properties_get( properties, "gain" ) == NULL )
291 double gain = 1.0; // no adjustment
293 if ( mlt_properties_get( filter_props, "gain" ) != NULL )
295 char *p = mlt_properties_get( filter_props, "gain" );
297 if ( strncaseeq( p, "normalise", 9 ) )
298 mlt_properties_set( filter_props, "normalise", "" );
301 if ( strcmp( p, "" ) != 0 )
302 gain = fabs( strtod( p, &p) );
304 while ( isspace( *p ) )
307 /* check if "dB" is given after number */
308 if ( strncaseeq( p, "db", 2 ) )
309 gain = DBFSTOAMP( gain );
311 // If there is an end adjust gain to the range
312 if ( mlt_properties_get( filter_props, "end" ) != NULL )
314 // Determine the time position of this frame in the transition duration
315 mlt_position in = mlt_filter_get_in( this );
316 mlt_position out = mlt_filter_get_out( this );
317 mlt_position time = mlt_frame_get_position( frame );
318 double position = ( double )( time - in ) / ( double )( out - in + 1 );
321 char *p = mlt_properties_get( filter_props, "end" );
322 if ( strcmp( p, "" ) != 0 )
323 end = fabs( strtod( p, &p) );
325 while ( isspace( *p ) )
328 /* check if "dB" is given after number */
329 if ( strncaseeq( p, "db", 2 ) )
330 end = DBFSTOAMP( gain );
333 gain += ( end - gain ) * position;
337 mlt_properties_set_double( properties, "volume.gain", gain );
340 // Parse the maximum gain property
341 if ( mlt_properties_get( filter_props, "max_gain" ) != NULL )
343 char *p = mlt_properties_get( filter_props, "max_gain" );
344 double gain = fabs( strtod( p, &p) ); // 0 = no max
346 while ( isspace( *p ) )
349 /* check if "dB" is given after number */
350 if ( strncaseeq( p, "db", 2 ) )
351 gain = DBFSTOAMP( gain );
353 mlt_properties_set_double( properties, "volume.max_gain", gain );
356 // Parse the limiter property
357 if ( mlt_properties_get( filter_props, "limiter" ) != NULL )
359 char *p = mlt_properties_get( filter_props, "limiter" );
360 double level = 0.5; /* -6dBFS */
361 if ( strcmp( p, "" ) != 0 )
362 level = strtod( p, &p);
364 while ( isspace( *p ) )
367 /* check if "dB" is given after number */
368 if ( strncaseeq( p, "db", 2 ) )
372 level = DBFSTOAMP( level );
379 mlt_properties_set_double( properties, "volume.limiter", level );
382 // Parse the normalise property
383 if ( mlt_properties_get( filter_props, "normalise" ) != NULL )
385 char *p = mlt_properties_get( filter_props, "normalise" );
386 double amplitude = 0.2511886431509580; /* -12dBFS */
387 if ( strcmp( p, "" ) != 0 )
388 amplitude = strtod( p, &p);
390 while ( isspace( *p ) )
393 /* check if "dB" is given after number */
394 if ( strncaseeq( p, "db", 2 ) )
397 amplitude = -amplitude;
398 amplitude = DBFSTOAMP( amplitude );
403 amplitude = -amplitude;
404 if ( amplitude > 1.0 )
408 // If there is an end adjust gain to the range
409 if ( mlt_properties_get( filter_props, "end" ) != NULL )
411 // Determine the time position of this frame in the transition duration
412 mlt_position in = mlt_filter_get_in( this );
413 mlt_position out = mlt_filter_get_out( this );
414 mlt_position time = mlt_frame_get_position( frame );
415 double position = ( double )( time - in ) / ( double )( out - in + 1 );
416 amplitude *= position;
418 mlt_properties_set_int( properties, "volume.normalise", 1 );
419 mlt_properties_set_double( properties, "volume.amplitude", amplitude );
422 // Parse the window property and allocate smoothing buffer if needed
423 int window = mlt_properties_get_int( filter_props, "window" );
424 if ( mlt_properties_get( filter_props, "smooth_buffer" ) == NULL && window > 1 )
426 // Create a smoothing buffer for the calculated "max power" of frame of audio used in normalisation
427 double *smooth_buffer = (double*) calloc( window, sizeof( double ) );
429 for ( i = 0; i < window; i++ )
430 smooth_buffer[ i ] = -1.0;
431 mlt_properties_set_data( filter_props, "smooth_buffer", smooth_buffer, 0, free, NULL );
432 int *smooth_index = calloc( 1, sizeof( int ) );
434 mlt_properties_set_data( filter_props, "smooth_index", smooth_index, 0, free, NULL );
437 // Put a filter reference onto the frame
438 mlt_properties_set_data( properties, "filter_volume", this, 0, NULL, NULL );
440 // Backup the original get_audio (it's still needed)
441 mlt_properties_set_data( properties, "volume.get_audio", frame->get_audio, 0, NULL, NULL );
443 // Override the get_audio method
444 frame->get_audio = filter_get_audio;
449 /** Constructor for the filter.
452 mlt_filter filter_volume_init( char *arg )
454 mlt_filter this = calloc( sizeof( struct mlt_filter_s ), 1 );
455 if ( this != NULL && mlt_filter_init( this, NULL ) == 0 )
457 mlt_properties properties = mlt_filter_properties( this );
458 this->process = filter_process;
460 mlt_properties_set( properties, "gain", arg );
462 mlt_properties_set_int( properties, "window", 75 );
463 mlt_properties_set( properties, "max_gain", "20dB" );