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 <framework/mlt_filter.h>
22 #include <framework/mlt_frame.h>
30 #define MAX_CHANNELS 6
31 #define EPSILON 0.00001
33 /* The following normalise functions come from the normalize utility:
34 Copyright (C) 1999--2002 Chris Vaill */
39 # define ROUND(x) floor((x) + 0.5)
42 #define DBFSTOAMP(x) pow(10,(x)/20.0)
44 /** Return nonzero if the two strings are equal, ignoring case, up to
45 the first n characters.
47 int strncaseeq(const char *s1, const char *s2, size_t n)
51 if (tolower(*s1++) != tolower(*s2++))
59 / tanh((x + lev) / (1-lev)) * (1-lev) - lev (for x < -lev)
61 x' = | x (for |x| <= lev)
63 \ tanh((x - lev) / (1-lev)) * (1-lev) + lev (for x > lev)
65 With limiter level = 0, this is equivalent to a tanh() function;
66 with limiter level = 1, this is equivalent to clipping.
68 static inline double limiter( double x, double lmtr_lvl )
73 xp = tanh((x + lmtr_lvl) / (1-lmtr_lvl)) * (1-lmtr_lvl) - lmtr_lvl;
74 else if (x > lmtr_lvl)
75 xp = tanh((x - lmtr_lvl) / (1-lmtr_lvl)) * (1-lmtr_lvl) + lmtr_lvl;
78 // fprintf( stderr, "filter_volume: sample %f limited %f\n", x, xp );
84 /** Takes a full smoothing window, and returns the value of the center
87 Currently, just does a mean filter, but we could do a median or
88 gaussian filter here instead.
90 static inline double get_smoothed_data( double *buf, int count )
95 for ( i = 0, j = 0; i < count; i++ )
97 if ( buf[ i ] != -1.0 )
104 // fprintf( stderr, "smoothed over %d values, result %f\n", j, smoothed );
109 /** Get the max power level (using RMS) and peak level of the audio segment.
111 double signal_max_power( int16_t *buffer, int channels, int samples, int16_t *peak )
113 // Determine numeric limits
114 int bytes_per_samp = (samp_width - 1) / 8 + 1;
115 int16_t max = (1 << (bytes_per_samp * 8 - 1)) - 1;
116 int16_t min = -max - 1;
118 double *sums = (double *) calloc( channels, sizeof(double) );
121 double pow, maxpow = 0;
123 /* initialize peaks to effectively -inf and +inf */
124 int16_t max_sample = min;
125 int16_t min_sample = max;
127 for ( i = 0; i < samples; i++ )
129 for ( c = 0; c < channels; c++ )
132 sums[ c ] += (double) sample * (double) sample;
135 if ( sample > max_sample )
137 else if ( sample < min_sample )
141 for ( c = 0; c < channels; c++ )
143 pow = sums[ c ] / (double) samples;
150 /* scale the pow value to be in the range 0.0 -- 1.0 */
151 maxpow /= ( (double) min * (double) min);
153 if ( -min_sample > max_sample )
154 *peak = min_sample / (double) min;
156 *peak = max_sample / (double) max;
158 return sqrt( maxpow );
161 /* ------ End normalize functions --------------------------------------- */
166 static int filter_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
168 // Get the properties of the a frame
169 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
170 double gain = mlt_properties_get_double( properties, "volume.gain" );
171 double max_gain = mlt_properties_get_double( properties, "volume.max_gain" );
172 double limiter_level = 0.5; /* -6 dBFS */
173 int normalise = mlt_properties_get_int( properties, "volume.normalise" );
174 double amplitude = mlt_properties_get_double( properties, "volume.amplitude" );
179 // Get the filter from the frame
180 mlt_filter this = mlt_properties_get_data( properties, "filter_volume", NULL );
182 // Get the properties from the filter
183 mlt_properties filter_props = MLT_FILTER_PROPERTIES( this );
185 if ( mlt_properties_get( properties, "volume.limiter" ) != NULL )
186 limiter_level = mlt_properties_get_double( properties, "volume.limiter" );
188 // Get the producer's audio
189 *format = mlt_audio_s16;
190 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
191 // fprintf( stderr, "filter_volume: frequency %d\n", *frequency );
193 // Determine numeric limits
194 int bytes_per_samp = (samp_width - 1) / 8 + 1;
195 int samplemax = (1 << (bytes_per_samp * 8 - 1)) - 1;
196 int samplemin = -samplemax - 1;
200 int window = mlt_properties_get_int( filter_props, "window" );
201 double *smooth_buffer = mlt_properties_get_data( filter_props, "smooth_buffer", NULL );
203 if ( window > 0 && smooth_buffer != NULL )
205 int smooth_index = mlt_properties_get_int( filter_props, "_smooth_index" );
207 // Compute the signal power and put into smoothing buffer
208 smooth_buffer[ smooth_index ] = signal_max_power( *buffer, *channels, *samples, &peak );
209 // fprintf( stderr, "filter_volume: raw power %f ", smooth_buffer[ smooth_index ] );
210 if ( smooth_buffer[ smooth_index ] > EPSILON )
212 mlt_properties_set_int( filter_props, "_smooth_index", ( smooth_index + 1 ) % window );
214 // Smooth the data and compute the gain
215 // fprintf( stderr, "smoothed %f over %d frames\n", get_smoothed_data( smooth_buffer, window ), window );
216 gain *= amplitude / get_smoothed_data( smooth_buffer, window );
221 gain *= amplitude / signal_max_power( (int16_t*) *buffer, *channels, *samples, &peak );
225 // if ( gain > 1.0 && normalise )
226 // fprintf(stderr, "filter_volume: limiter level %f gain %f\n", limiter_level, gain );
228 if ( max_gain > 0 && gain > max_gain )
231 // Initialise filter's previous gain value to prevent an inadvertant jump from 0
232 mlt_position last_position = mlt_properties_get_position( filter_props, "_last_position" );
233 mlt_position current_position = mlt_frame_get_position( frame );
234 if ( mlt_properties_get( filter_props, "_previous_gain" ) == NULL
235 || current_position != last_position + 1 )
236 mlt_properties_set_double( filter_props, "_previous_gain", gain );
238 // Start the gain out at the previous
239 double previous_gain = mlt_properties_get_double( filter_props, "_previous_gain" );
241 // Determine ramp increment
242 double gain_step = ( gain - previous_gain ) / *samples;
243 // fprintf( stderr, "filter_volume: previous gain %f current gain %f step %f\n", previous_gain, gain, gain_step );
245 // Save the current gain for the next iteration
246 mlt_properties_set_double( filter_props, "_previous_gain", gain );
247 mlt_properties_set_position( filter_props, "_last_position", current_position );
249 // Ramp from the previous gain to the current
250 gain = previous_gain;
252 int16_t *p = (int16_t*) *buffer;
255 for ( i = 0; i < *samples; i++ )
257 for ( j = 0; j < *channels; j++ )
260 *p = ROUND( sample );
264 /* use limiter function instead of clipping */
266 *p = ROUND( samplemax * limiter( sample / (double) samplemax, limiter_level ) );
268 /* perform clipping */
269 else if ( sample > samplemax )
271 else if ( sample < samplemin )
282 /** Filter processing.
285 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
287 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
288 mlt_properties filter_props = MLT_FILTER_PROPERTIES( this );
290 // Parse the gain property
291 if ( mlt_properties_get( properties, "gain" ) == NULL )
293 double gain = 1.0; // no adjustment
295 if ( mlt_properties_get( filter_props, "gain" ) != NULL )
297 char *p = mlt_properties_get( filter_props, "gain" );
299 if ( strncaseeq( p, "normalise", 9 ) )
300 mlt_properties_set( filter_props, "normalise", "" );
303 if ( strcmp( p, "" ) != 0 )
304 gain = fabs( strtod( p, &p) );
306 while ( isspace( *p ) )
309 /* check if "dB" is given after number */
310 if ( strncaseeq( p, "db", 2 ) )
311 gain = DBFSTOAMP( gain );
313 // If there is an end adjust gain to the range
314 if ( mlt_properties_get( filter_props, "end" ) != NULL )
316 // Determine the time position of this frame in the transition duration
317 mlt_position in = mlt_filter_get_in( this );
318 mlt_position out = mlt_filter_get_out( this );
319 mlt_position time = mlt_frame_get_position( frame );
320 double position = ( double )( time - in ) / ( double )( out - in + 1 );
323 char *p = mlt_properties_get( filter_props, "end" );
324 if ( strcmp( p, "" ) != 0 )
325 end = fabs( strtod( p, &p) );
327 while ( isspace( *p ) )
330 /* check if "dB" is given after number */
331 if ( strncaseeq( p, "db", 2 ) )
332 end = DBFSTOAMP( gain );
335 gain += ( end - gain ) * position;
339 mlt_properties_set_double( properties, "volume.gain", gain );
342 // Parse the maximum gain property
343 if ( mlt_properties_get( filter_props, "max_gain" ) != NULL )
345 char *p = mlt_properties_get( filter_props, "max_gain" );
346 double gain = fabs( strtod( p, &p) ); // 0 = no max
348 while ( isspace( *p ) )
351 /* check if "dB" is given after number */
352 if ( strncaseeq( p, "db", 2 ) )
353 gain = DBFSTOAMP( gain );
355 mlt_properties_set_double( properties, "volume.max_gain", gain );
358 // Parse the limiter property
359 if ( mlt_properties_get( filter_props, "limiter" ) != NULL )
361 char *p = mlt_properties_get( filter_props, "limiter" );
362 double level = 0.5; /* -6dBFS */
363 if ( strcmp( p, "" ) != 0 )
364 level = strtod( p, &p);
366 while ( isspace( *p ) )
369 /* check if "dB" is given after number */
370 if ( strncaseeq( p, "db", 2 ) )
374 level = DBFSTOAMP( level );
381 mlt_properties_set_double( properties, "volume.limiter", level );
384 // Parse the normalise property
385 if ( mlt_properties_get( filter_props, "normalise" ) != NULL )
387 char *p = mlt_properties_get( filter_props, "normalise" );
388 double amplitude = 0.2511886431509580; /* -12dBFS */
389 if ( strcmp( p, "" ) != 0 )
390 amplitude = strtod( p, &p);
392 while ( isspace( *p ) )
395 /* check if "dB" is given after number */
396 if ( strncaseeq( p, "db", 2 ) )
399 amplitude = -amplitude;
400 amplitude = DBFSTOAMP( amplitude );
405 amplitude = -amplitude;
406 if ( amplitude > 1.0 )
410 // If there is an end adjust gain to the range
411 if ( mlt_properties_get( filter_props, "end" ) != NULL )
413 // Determine the time position of this frame in the transition duration
414 mlt_position in = mlt_filter_get_in( this );
415 mlt_position out = mlt_filter_get_out( this );
416 mlt_position time = mlt_frame_get_position( frame );
417 double position = ( double )( time - in ) / ( double )( out - in + 1 );
418 amplitude *= position;
420 mlt_properties_set_int( properties, "volume.normalise", 1 );
421 mlt_properties_set_double( properties, "volume.amplitude", amplitude );
424 // Parse the window property and allocate smoothing buffer if needed
425 int window = mlt_properties_get_int( filter_props, "window" );
426 if ( mlt_properties_get( filter_props, "smooth_buffer" ) == NULL && window > 1 )
428 // Create a smoothing buffer for the calculated "max power" of frame of audio used in normalisation
429 double *smooth_buffer = (double*) calloc( window, sizeof( double ) );
431 for ( i = 0; i < window; i++ )
432 smooth_buffer[ i ] = -1.0;
433 mlt_properties_set_data( filter_props, "smooth_buffer", smooth_buffer, 0, free, NULL );
436 // Put a filter reference onto the frame
437 mlt_properties_set_data( properties, "filter_volume", this, 0, NULL, NULL );
439 // Override the get_audio method
440 mlt_frame_push_audio( frame, filter_get_audio );
445 /** Constructor for the filter.
448 mlt_filter filter_volume_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
450 mlt_filter this = calloc( sizeof( struct mlt_filter_s ), 1 );
451 if ( this != NULL && mlt_filter_init( this, NULL ) == 0 )
453 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
454 this->process = filter_process;
456 mlt_properties_set( properties, "gain", arg );
458 mlt_properties_set_int( properties, "window", 75 );
459 mlt_properties_set( properties, "max_gain", "20dB" );