1 /*
2 * filter_volume.c -- adjust audio volume
3 * Copyright (C) 2003-2020 Meltytech, LLC
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20 #include <framework/mlt_filter.h>
21 #include <framework/mlt_frame.h>
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <math.h>
26 #include <ctype.h>
27 #include <string.h>
28
29 #define EPSILON 0.00001
30
31 /* The following normalise functions come from the normalize utility:
32 Copyright (C) 1999--2002 Chris Vaill */
33
34 #define samp_width 16
35
36 #ifndef ROUND
37 # define ROUND(x) floor((x) + 0.5)
38 #endif
39
40 #define DBFSTOAMP(x) pow(10,(x)/20.0)
41
42 /** Return nonzero if the two strings are equal, ignoring case, up to
43 the first n characters.
44 */
strncaseeq(const char * s1,const char * s2,size_t n)45 int strncaseeq(const char *s1, const char *s2, size_t n)
46 {
47 for ( ; n > 0; n--)
48 {
49 if (tolower(*s1++) != tolower(*s2++))
50 return 0;
51 }
52 return 1;
53 }
54
55 /** Limiter function.
56
57 / tanh((x + lev) / (1-lev)) * (1-lev) - lev (for x < -lev)
58 |
59 x' = | x (for |x| <= lev)
60 |
61 \ tanh((x - lev) / (1-lev)) * (1-lev) + lev (for x > lev)
62
63 With limiter level = 0, this is equivalent to a tanh() function;
64 with limiter level = 1, this is equivalent to clipping.
65 */
limiter(double x,double lmtr_lvl)66 static inline double limiter( double x, double lmtr_lvl )
67 {
68 double xp = x;
69
70 if (x < -lmtr_lvl)
71 xp = tanh((x + lmtr_lvl) / (1-lmtr_lvl)) * (1-lmtr_lvl) - lmtr_lvl;
72 else if (x > lmtr_lvl)
73 xp = tanh((x - lmtr_lvl) / (1-lmtr_lvl)) * (1-lmtr_lvl) + lmtr_lvl;
74
75 return xp;
76 }
77
78
79 /** Takes a full smoothing window, and returns the value of the center
80 element, smoothed.
81
82 Currently, just does a mean filter, but we could do a median or
83 gaussian filter here instead.
84 */
get_smoothed_data(double * buf,int count)85 static inline double get_smoothed_data( double *buf, int count )
86 {
87 int i, j;
88 double smoothed = 0;
89
90 for ( i = 0, j = 0; i < count; i++ )
91 {
92 if ( buf[ i ] != -1.0 )
93 {
94 smoothed += buf[ i ];
95 j++;
96 }
97 }
98 if (j) smoothed /= j;
99
100 return smoothed;
101 }
102
103 /** Get the max power level (using RMS) and peak level of the audio segment.
104 */
signal_max_power(int16_t * buffer,int channels,int samples,int16_t * peak)105 double signal_max_power( int16_t *buffer, int channels, int samples, int16_t *peak )
106 {
107 // Determine numeric limits
108 int bytes_per_samp = (samp_width - 1) / 8 + 1;
109 int16_t max = (1 << (bytes_per_samp * 8 - 1)) - 1;
110 int16_t min = -max - 1;
111
112 double *sums = (double *) calloc( channels, sizeof(double) );
113 int c, i;
114 int16_t sample;
115 double pow, maxpow = 0;
116
117 /* initialize peaks to effectively -inf and +inf */
118 int16_t max_sample = min;
119 int16_t min_sample = max;
120
121 for ( i = 0; i < samples; i++ )
122 {
123 for ( c = 0; c < channels; c++ )
124 {
125 sample = *buffer++;
126 sums[ c ] += (double) sample * (double) sample;
127
128 /* track peak */
129 if ( sample > max_sample )
130 max_sample = sample;
131 else if ( sample < min_sample )
132 min_sample = sample;
133 }
134 }
135 for ( c = 0; c < channels; c++ )
136 {
137 pow = sums[ c ] / (double) samples;
138 if ( pow > maxpow )
139 maxpow = pow;
140 }
141
142 free( sums );
143
144 /* scale the pow value to be in the range 0.0 -- 1.0 */
145 maxpow /= ( (double) min * (double) min);
146
147 if ( -min_sample > max_sample )
148 *peak = min_sample / (double) min;
149 else
150 *peak = max_sample / (double) max;
151
152 return sqrt( maxpow );
153 }
154
155 /* ------ End normalize functions --------------------------------------- */
156
157 /** Get the audio.
158 */
159
filter_get_audio(mlt_frame frame,void ** buffer,mlt_audio_format * format,int * frequency,int * channels,int * samples)160 static int filter_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
161 {
162 // Get the filter from the frame
163 mlt_filter filter = mlt_frame_pop_audio( frame );
164
165 // Get the properties from the filter
166 mlt_properties filter_props = MLT_FILTER_PROPERTIES( filter );
167
168 // Get the frame's filter instance properties
169 mlt_properties instance_props = mlt_frame_unique_properties( frame, MLT_FILTER_SERVICE( filter ) );
170
171 // Get the parameters
172 double gain = mlt_properties_get_double( instance_props, "gain" );
173 double max_gain = mlt_properties_get_double( instance_props, "max_gain" );
174 double limiter_level = 0.5; /* -6 dBFS */
175 int normalise = mlt_properties_get_int( instance_props, "normalise" );
176 double amplitude = mlt_properties_get_double( instance_props, "amplitude" );
177 int i, j;
178 double sample;
179 int16_t peak;
180
181 // Use animated value for gain if "level" property is set
182 char* level_property = mlt_properties_get( filter_props, "level" );
183 if ( level_property != NULL )
184 {
185 mlt_position position = mlt_filter_get_position( filter, frame );
186 mlt_position length = mlt_filter_get_length2( filter, frame );
187 gain = mlt_properties_anim_get_double( filter_props, "level", position, length );
188 gain = DBFSTOAMP( gain );
189 }
190
191 if ( mlt_properties_get( instance_props, "limiter" ) != NULL )
192 limiter_level = mlt_properties_get_double( instance_props, "limiter" );
193
194 // Get the producer's audio
195 *format = normalise? mlt_audio_s16 : mlt_audio_f32le;
196 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
197
198 mlt_service_lock( MLT_FILTER_SERVICE( filter ) );
199
200 if ( normalise )
201 {
202 int window = mlt_properties_get_int( filter_props, "window" );
203 double *smooth_buffer = mlt_properties_get_data( filter_props, "smooth_buffer", NULL );
204
205 if ( window > 0 && smooth_buffer != NULL )
206 {
207 int smooth_index = mlt_properties_get_int( filter_props, "_smooth_index" );
208
209 // Compute the signal power and put into smoothing buffer
210 smooth_buffer[ smooth_index ] = signal_max_power( *buffer, *channels, *samples, &peak );
211
212 if ( smooth_buffer[ smooth_index ] > EPSILON )
213 {
214 mlt_properties_set_int( filter_props, "_smooth_index", ( smooth_index + 1 ) % window );
215
216 // Smooth the data and compute the gain
217 gain *= amplitude / get_smoothed_data( smooth_buffer, window );
218 }
219 }
220 else
221 {
222 gain *= amplitude / signal_max_power( *buffer, *channels, *samples, &peak );
223 }
224 }
225
226 if ( max_gain > 0 && gain > max_gain )
227 gain = max_gain;
228
229 // Initialise filter's previous gain value to prevent an inadvertent jump from 0
230 mlt_position last_position = mlt_properties_get_position( filter_props, "_last_position" );
231 mlt_position current_position = mlt_frame_get_position( frame );
232 if ( mlt_properties_get( filter_props, "_previous_gain" ) == NULL
233 || current_position != last_position + 1 )
234 mlt_properties_set_double( filter_props, "_previous_gain", gain );
235
236 // Start the gain out at the previous
237 double previous_gain = mlt_properties_get_double( filter_props, "_previous_gain" );
238
239 // Determine ramp increment
240 double gain_step = ( gain - previous_gain ) / *samples;
241
242 // Save the current gain for the next iteration
243 mlt_properties_set_double( filter_props, "_previous_gain", gain );
244 mlt_properties_set_position( filter_props, "_last_position", current_position );
245
246 mlt_service_unlock( MLT_FILTER_SERVICE( filter ) );
247
248 // Ramp from the previous gain to the current
249 gain = previous_gain;
250
251 // Apply the gain
252 if ( normalise )
253 {
254 int16_t *p = *buffer;
255 // Determine numeric limits
256 int bytes_per_samp = (samp_width - 1) / 8 + 1;
257 int samplemax = (1 << (bytes_per_samp * 8 - 1)) - 1;
258
259 for ( i = 0; i < *samples; i++, gain += gain_step ) {
260 for ( j = 0; j < *channels; j++ ) {
261 sample = *p * gain;
262 *p = ROUND( sample );
263 if ( gain > 1.0 && normalise ) {
264 /* use limiter function instead of clipping */
265 *p = ROUND( samplemax * limiter( sample / (double) samplemax, limiter_level ) );
266 }
267 p++;
268 }
269 }
270 }
271 else
272 {
273 float *p = *buffer;
274 for ( i = 0; i < *samples; i++, gain += gain_step ) {
275 for ( j = 0; j < *channels; j++, p++ ) {
276 p[0] *= gain;
277 }
278 }
279 }
280 return 0;
281 }
282
283 /** Filter processing.
284 */
285
filter_process(mlt_filter filter,mlt_frame frame)286 static mlt_frame filter_process( mlt_filter filter, mlt_frame frame )
287 {
288 mlt_properties filter_props = MLT_FILTER_PROPERTIES( filter );
289 mlt_properties instance_props = mlt_frame_unique_properties( frame, MLT_FILTER_SERVICE( filter ) );
290
291 double gain = 1.0; // no adjustment
292 char *gain_str = mlt_properties_get( filter_props, "gain" );
293
294 // Parse the gain property
295 if ( gain_str )
296 {
297 char *p_orig = strdup( gain_str );
298 char *p = p_orig;
299
300 if ( strncaseeq( p, "normalise", 9 ) )
301 mlt_properties_set( filter_props, "normalise", "" );
302 else
303 {
304 if ( strcmp( p, "" ) != 0 )
305 gain = strtod( p, &p );
306
307 while ( isspace( *p ) )
308 p++;
309
310 /* check if "dB" is given after number */
311 if ( strncaseeq( p, "db", 2 ) )
312 gain = DBFSTOAMP( gain );
313 else
314 gain = fabs( gain );
315
316 // If there is an end adjust gain to the range
317 if ( mlt_properties_get( filter_props, "end" ) != NULL )
318 {
319 double end = -1;
320 char *p = mlt_properties_get( filter_props, "end" );
321 if ( strcmp( p, "" ) != 0 )
322 end = strtod( p, &p );
323
324 while ( isspace( *p ) )
325 p++;
326
327 /* check if "dB" is given after number */
328 if ( strncaseeq( p, "db", 2 ) )
329 end = DBFSTOAMP( end );
330 else
331 end = fabs( end );
332
333 if ( end != -1 )
334 gain += ( end - gain ) * mlt_filter_get_progress( filter, frame );
335 }
336 }
337 free( p_orig );
338 }
339 mlt_properties_set_double( instance_props, "gain", gain );
340
341 // Parse the maximum gain property
342 if ( mlt_properties_get( filter_props, "max_gain" ) != NULL )
343 {
344 char *p = mlt_properties_get( filter_props, "max_gain" );
345 double gain = strtod( p, &p ); // 0 = no max
346
347 while ( isspace( *p ) )
348 p++;
349
350 /* check if "dB" is given after number */
351 if ( strncaseeq( p, "db", 2 ) )
352 gain = DBFSTOAMP( gain );
353 else
354 gain = fabs( gain );
355
356 mlt_properties_set_double( instance_props, "max_gain", gain );
357 }
358
359 // Parse the limiter property
360 if ( mlt_properties_get( filter_props, "limiter" ) != NULL )
361 {
362 char *p = mlt_properties_get( filter_props, "limiter" );
363 double level = 0.5; /* -6dBFS */
364 if ( strcmp( p, "" ) != 0 )
365 level = strtod( p, &p);
366
367 while ( isspace( *p ) )
368 p++;
369
370 /* check if "dB" is given after number */
371 if ( strncaseeq( p, "db", 2 ) )
372 {
373 if ( level > 0 )
374 level = -level;
375 level = DBFSTOAMP( level );
376 }
377 else
378 {
379 if ( level < 0 )
380 level = -level;
381 }
382 mlt_properties_set_double( instance_props, "limiter", level );
383 }
384
385 // Parse the normalise property
386 if ( mlt_properties_get( filter_props, "normalise" ) != NULL )
387 {
388 char *p = mlt_properties_get( filter_props, "normalise" );
389 double amplitude = 0.2511886431509580; /* -12dBFS */
390 if ( strcmp( p, "" ) != 0 )
391 amplitude = strtod( p, &p);
392
393 while ( isspace( *p ) )
394 p++;
395
396 /* check if "dB" is given after number */
397 if ( strncaseeq( p, "db", 2 ) )
398 {
399 if ( amplitude > 0 )
400 amplitude = -amplitude;
401 amplitude = DBFSTOAMP( amplitude );
402 }
403 else
404 {
405 if ( amplitude < 0 )
406 amplitude = -amplitude;
407 if ( amplitude > 1.0 )
408 amplitude = 1.0;
409 }
410
411 // If there is an end adjust gain to the range
412 if ( mlt_properties_get( filter_props, "end" ) != NULL )
413 {
414 amplitude *= mlt_filter_get_progress( filter, frame );
415 }
416 mlt_properties_set_int( instance_props, "normalise", 1 );
417 mlt_properties_set_double( instance_props, "amplitude", amplitude );
418 }
419
420 // Parse the window property and allocate smoothing buffer if needed
421 int window = mlt_properties_get_int( filter_props, "window" );
422 if ( mlt_properties_get( filter_props, "smooth_buffer" ) == NULL && window > 1 )
423 {
424 // Create a smoothing buffer for the calculated "max power" of frame of audio used in normalisation
425 double *smooth_buffer = (double*) calloc( window, sizeof( double ) );
426 int i;
427 for ( i = 0; i < window; i++ )
428 smooth_buffer[ i ] = -1.0;
429 mlt_properties_set_data( filter_props, "smooth_buffer", smooth_buffer, 0, free, NULL );
430 }
431
432 // Push the filter onto the stack
433 mlt_frame_push_audio( frame, filter );
434
435 // Override the get_audio method
436 mlt_frame_push_audio( frame, filter_get_audio );
437
438 return frame;
439 }
440
441 /** Constructor for the filter.
442 */
443
filter_volume_init(mlt_profile profile,mlt_service_type type,const char * id,char * arg)444 mlt_filter filter_volume_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
445 {
446 mlt_filter filter = calloc( 1, sizeof( struct mlt_filter_s ) );
447 if ( filter != NULL && mlt_filter_init( filter, NULL ) == 0 )
448 {
449 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
450 filter->process = filter_process;
451 if ( arg != NULL )
452 mlt_properties_set( properties, "gain", arg );
453
454 mlt_properties_set_int( properties, "window", 75 );
455 mlt_properties_set( properties, "max_gain", "20dB" );
456
457 mlt_properties_set( properties, "level", NULL );
458 }
459 return filter;
460 }
461