2 * filter_sox.c -- apply any number of SOX effects using libst
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <framework/mlt_filter.h>
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_tokeniser.h>
24 #include <framework/mlt_log.h>
31 // TODO: does not support multiple effects with SoX v14.1.0+
35 # define ST_EOF SOX_EOF
36 # define ST_SUCCESS SOX_SUCCESS
37 # define st_sample_t sox_sample_t
38 # define eff_t sox_effect_t*
39 # define ST_LIB_VERSION_CODE SOX_LIB_VERSION_CODE
40 # define ST_LIB_VERSION SOX_LIB_VERSION
41 # if (ST_LIB_VERSION_CODE >= ST_LIB_VERSION(14,2,0))
42 # define st_size_t size_t
44 # define st_size_t sox_size_t
46 # define ST_SIGNED_WORD_TO_SAMPLE(d,clips) SOX_SIGNED_16BIT_TO_SAMPLE(d,clips)
47 # if (ST_LIB_VERSION_CODE >= ST_LIB_VERSION(14,1,0))
48 # define ST_SSIZE_MIN SOX_SAMPLE_MIN
50 # define ST_SSIZE_MIN SOX_SSIZE_MIN
52 # define ST_SAMPLE_TO_SIGNED_WORD(d,clips) SOX_SAMPLE_TO_SIGNED_16BIT(d,clips)
57 #define BUFFER_LEN 8192
58 #define AMPLITUDE_NORM 0.2511886431509580 /* -12dBFS */
59 #define AMPLITUDE_MIN 0.00001
60 #define DBFSTOAMP(x) pow(10,(x)/20.0)
62 /** Compute the mean of a set of doubles skipping unset values flagged as -1
64 static inline double mean( double *buf, int count )
70 for ( i = 0; i < count; i++ )
72 if ( buf[ i ] != -1.0 )
84 #if (ST_LIB_VERSION_CODE >= ST_LIB_VERSION(14,1,0))
85 static void delete_effect( eff_t effp )
88 free( (void*)effp->in_encoding );
93 /** Create an effect state instance for a channels
95 static int create_effect( mlt_filter this, char *value, int count, int channel, int frequency )
97 mlt_tokeniser tokeniser = mlt_tokeniser_init();
101 // Tokenise the effect specification
102 mlt_tokeniser_parse_new( tokeniser, value, " " );
103 if ( tokeniser->count < 1 )
105 mlt_tokeniser_close( tokeniser );
110 mlt_destructor effect_destructor = mlt_pool_release;
112 //fprintf(stderr, "%s: effect %s count %d\n", __FUNCTION__, tokeniser->tokens[0], tokeniser->count );
113 #if (ST_LIB_VERSION_CODE >= ST_LIB_VERSION(14,1,0))
114 sox_effect_handler_t const *eff_handle = sox_find_effect( tokeniser->tokens[0] );
115 if (eff_handle == NULL ) return error;
116 eff_t eff = sox_create_effect( eff_handle );
117 effect_destructor = ( mlt_destructor ) delete_effect;
118 sox_encodinginfo_t *enc = calloc( 1, sizeof( sox_encodinginfo_t ) );
119 enc->encoding = SOX_ENCODING_SIGN2;
120 enc->bits_per_sample = 16;
121 eff->in_encoding = eff->out_encoding = enc;
123 eff_t eff = mlt_pool_alloc( sizeof( sox_effect_t ) );
124 sox_create_effect( eff, sox_find_effect( tokeniser->tokens[0] ) );
126 int opt_count = tokeniser->count - 1;
128 eff_t eff = mlt_pool_alloc( sizeof( struct st_effect ) );
129 int opt_count = st_geteffect_opt( eff, tokeniser->count, tokeniser->tokens );
133 if ( opt_count != ST_EOF )
135 // Supply the effect parameters
137 #if (ST_LIB_VERSION_CODE >= ST_LIB_VERSION(14,2,0))
138 if ( sox_effect_options( eff, opt_count, &tokeniser->tokens[ tokeniser->count > 1 ? 1 : 0 ] ) == ST_SUCCESS )
140 if ( ( * eff->handler.getopts )( eff, opt_count, &tokeniser->tokens[ tokeniser->count > 1 ? 1 : 0 ] ) == ST_SUCCESS )
143 if ( ( * eff->h->getopts )( eff, opt_count, &tokeniser->tokens[ tokeniser->count - opt_count ] ) == ST_SUCCESS )
146 // Set the sox signal parameters
147 #if (ST_LIB_VERSION_CODE >= ST_LIB_VERSION(14,1,0))
148 eff->in_signal.rate = frequency;
149 eff->out_signal.rate = frequency;
150 eff->in_signal.channels = 1;
151 eff->out_signal.channels = 1;
152 eff->in_signal.precision = 16;
153 eff->out_signal.precision = 16;
154 eff->in_signal.length = 0;
155 eff->out_signal.length = 0;
157 eff->ininfo.rate = frequency;
158 eff->outinfo.rate = frequency;
159 eff->ininfo.channels = 1;
160 eff->outinfo.channels = 1;
165 if ( ( * eff->handler.start )( eff ) == ST_SUCCESS )
167 if ( ( * eff->h->start )( eff ) == ST_SUCCESS )
171 sprintf( id, "_effect_%d_%d", count, channel );
173 // Save the effect state
174 mlt_properties_set_data( MLT_FILTER_PROPERTIES( this ), id, eff, 0, effect_destructor, NULL );
179 // Some error occurred so delete the temp effect state
181 effect_destructor( eff );
183 mlt_tokeniser_close( tokeniser );
191 static int filter_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
193 #if (ST_LIB_VERSION_CODE >= ST_LIB_VERSION(14,3,0))
196 // Get the filter service
197 mlt_filter filter = mlt_frame_pop_audio( frame );
199 // Get the filter properties
200 mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
202 mlt_service_lock( MLT_FILTER_SERVICE( filter ) );
204 // Get the properties
205 st_sample_t *input_buffer;// = mlt_properties_get_data( filter_properties, "input_buffer", NULL );
206 st_sample_t *output_buffer = mlt_properties_get_data( filter_properties, "output_buffer", NULL );
208 int count = mlt_properties_get_int( filter_properties, "_effect_count" );
209 int analysis = mlt_properties_get( filter_properties, "effect" ) && !strcmp( mlt_properties_get( filter_properties, "effect" ), "analysis" );
211 // Get the producer's audio
212 *format = mlt_audio_s32;
213 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
215 // Even though some effects are multi-channel aware, it is not reliable
216 // We must maintain a separate effect state for each channel
217 for ( i = 0; i < *channels; i++ )
220 sprintf( id, "_effect_0_%d", i );
222 // Get an existing effect state
223 eff_t e = mlt_properties_get_data( filter_properties, id, NULL );
225 // Validate the existing effect state
226 #if (ST_LIB_VERSION_CODE >= ST_LIB_VERSION(14,1,0))
227 if ( e != NULL && ( e->in_signal.rate != *frequency ||
228 e->out_signal.rate != *frequency ) )
230 if ( e != NULL && ( e->ininfo.rate != *frequency ||
231 e->outinfo.rate != *frequency ) )
235 // (Re)Create the effect state
243 // Loop over all properties
244 for ( j = 0; j < mlt_properties_count( filter_properties ); j ++ )
246 // Get the name of this property
247 char *name = mlt_properties_get_name( filter_properties, j );
249 // If the name does not contain a . and matches effect
250 if ( !strncmp( name, "effect", 6 ) )
252 // Get the effect specification
253 char *value = mlt_properties_get_value( filter_properties, j );
255 // Create an instance
256 if ( create_effect( filter, value, count, i, *frequency ) == 0 )
261 // Save the number of filters
262 mlt_properties_set_int( filter_properties, "_effect_count", count );
265 if ( *samples > 0 && ( count > 0 || analysis ) )
267 input_buffer = (st_sample_t*) *buffer + i * *samples;
268 st_sample_t *p = input_buffer;
269 st_size_t isamp = *samples;
270 st_size_t osamp = *samples;
271 int j = *samples + 1;
272 char *normalise = mlt_properties_get( filter_properties, "normalise" );
273 double normalised_gain = 1.0;
277 // Run analysis to compute a gain level to normalize the audio across entire filter duration
278 double max_power = mlt_properties_get_double( filter_properties, "_max_power" );
279 double peak = mlt_properties_get_double( filter_properties, "_max_peak" );
280 double use_peak = mlt_properties_get_int( filter_properties, "use_peak" );
282 int n = *samples + 1;
284 // Compute power level of samples in this channel of this frame
287 double s = fabs( *p++ );
292 mlt_properties_set_double( filter_properties, "_max_peak", peak );
297 // Track maximum power
298 if ( power > max_power )
301 mlt_properties_set_double( filter_properties, "_max_power", max_power );
304 // Complete analysis the last channel of the last frame.
305 if ( i + 1 == *channels && mlt_filter_get_position( filter, frame ) + 1
306 == mlt_filter_get_length2( filter, frame ) )
308 double rms = sqrt( max_power / ST_SSIZE_MIN / ST_SSIZE_MIN );
311 // Convert RMS or peak to gain
313 normalised_gain = ST_SSIZE_MIN / -peak;
316 double gain = DBFSTOAMP(-12); // default -12 dBFS
317 char *p = mlt_properties_get( filter_properties, "analysis_level" );
320 gain = mlt_properties_get_double( filter_properties, "analysis_level" );
321 if ( strstr( p, "dB" ) )
322 gain = DBFSTOAMP( gain );
324 normalised_gain = gain / rms;
327 // Set properties for serialization
328 snprintf( effect, sizeof(effect), "vol %f", normalised_gain );
330 mlt_properties_set( filter_properties, "effect", effect );
331 mlt_properties_set( filter_properties, "analyze", NULL );
333 // Show output comparable to normalize --no-adjust --fractions
334 mlt_properties_set_double( filter_properties, "level", rms );
335 mlt_properties_set_double( filter_properties, "gain", normalised_gain );
336 mlt_properties_set_double( filter_properties, "peak", -peak / ST_SSIZE_MIN );
339 // restore some variables
345 int window = mlt_properties_get_int( filter_properties, "window" );
346 double *smooth_buffer = mlt_properties_get_data( filter_properties, "smooth_buffer", NULL );
347 double max_gain = mlt_properties_get_double( filter_properties, "max_gain" );
350 // Default the maximum gain factor to 20dBFS
354 // Compute rms amplitude
357 rms += ( double )*p * ( double )*p;
360 rms = sqrt( rms / *samples / ST_SSIZE_MIN / ST_SSIZE_MIN );
362 // The smoothing buffer prevents radical shifts in the gain level
363 if ( window > 0 && smooth_buffer != NULL )
365 int smooth_index = mlt_properties_get_int( filter_properties, "_smooth_index" );
366 smooth_buffer[ smooth_index ] = rms;
368 // Ignore very small values that adversely affect the mean
369 if ( rms > AMPLITUDE_MIN )
370 mlt_properties_set_int( filter_properties, "_smooth_index", ( smooth_index + 1 ) % window );
372 // Smoothing is really just a mean over the past N values
373 normalised_gain = AMPLITUDE_NORM / mean( smooth_buffer, window );
377 // Determine gain to apply as current amplitude
378 normalised_gain = AMPLITUDE_NORM / rms;
381 //printf("filter_sox: rms %.3f gain %.3f\n", rms, normalised_gain );
383 // Govern the maximum gain
384 if ( normalised_gain > max_gain )
385 normalised_gain = max_gain;
389 for ( j = 0; j < count; j++ )
391 sprintf( id, "_effect_%d_%d", j, i );
392 e = mlt_properties_get_data( filter_properties, id, NULL );
394 // We better have this guy
397 float saved_gain = 1.0;
399 // XXX: hack to apply the normalised gain level to the vol effect
401 if ( normalise && strcmp( e->handler.name, "vol" ) == 0 )
403 if ( normalise && strcmp( e->name, "vol" ) == 0 )
406 float *f = ( float * )( e->priv );
408 *f = saved_gain * normalised_gain;
413 if ( ( * e->handler.flow )( e, input_buffer, output_buffer, &isamp, &osamp ) != ST_SUCCESS )
415 if ( ( * e->h->flow )( e, input_buffer, output_buffer, &isamp, &osamp ) != ST_SUCCESS )
418 mlt_log_warning( MLT_FILTER_SERVICE(filter), "effect processing failed\n" );
421 // XXX: hack to restore the original vol gain to prevent accumulation
423 if ( normalise && strcmp( e->handler.name, "vol" ) == 0 )
425 if ( normalise && strcmp( e->name, "vol" ) == 0 )
428 float *f = ( float * )( e->priv );
435 memcpy( input_buffer, output_buffer, *samples * sizeof(st_sample_t) );
439 mlt_service_unlock( MLT_FILTER_SERVICE( filter ) );
444 /** Filter processing.
447 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
449 if ( mlt_frame_is_test_audio( frame ) == 0 )
451 // Add the filter to the frame
452 mlt_frame_push_audio( frame, this );
453 mlt_frame_push_audio( frame, filter_get_audio );
455 // Parse the window property and allocate smoothing buffer if needed
456 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
457 int window = mlt_properties_get_int( properties, "window" );
458 if ( mlt_properties_get( properties, "smooth_buffer" ) == NULL && window > 1 )
460 // Create a smoothing buffer for the calculated "max power" of frame of audio used in normalisation
461 double *smooth_buffer = (double*) calloc( window, sizeof( double ) );
463 for ( i = 0; i < window; i++ )
464 smooth_buffer[ i ] = -1.0;
465 mlt_properties_set_data( properties, "smooth_buffer", smooth_buffer, 0, free, NULL );
472 /** Constructor for the filter.
475 mlt_filter filter_sox_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
477 mlt_filter this = mlt_filter_new( );
480 void *input_buffer = mlt_pool_alloc( BUFFER_LEN );
481 void *output_buffer = mlt_pool_alloc( BUFFER_LEN );
482 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
484 this->process = filter_process;
486 if ( !strncmp( id, "sox.", 4 ) )
488 char *s = malloc( strlen( id ) + ( arg? strlen( arg ) + 2 : 1 ) );
495 mlt_properties_set( properties, "effect", s );
499 mlt_properties_set( properties, "effect", arg );
500 mlt_properties_set_data( properties, "input_buffer", input_buffer, BUFFER_LEN, mlt_pool_release, NULL );
501 mlt_properties_set_data( properties, "output_buffer", output_buffer, BUFFER_LEN, mlt_pool_release, NULL );
502 mlt_properties_set_int( properties, "window", 75 );
503 mlt_properties_set( properties, "version", sox_version() );
508 // What to do when a libst internal failure occurs