From ea123e4425539043b831c31bbc91ab74426aa311 Mon Sep 17 00:00:00 2001 From: SaucySoliton Date: Fri, 6 Jan 2017 07:30:06 +0000 Subject: [PATCH] Audio compressor for more volume --- src/fm_mpx.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/fm_mpx.c b/src/fm_mpx.c index 37bde66..8783dca 100644 --- a/src/fm_mpx.c +++ b/src/fm_mpx.c @@ -65,6 +65,7 @@ float fir_buffer_left[FIR_SIZE] = {0}; float fir_buffer_right[FIR_SIZE] = {0}; int fir_index = 0; int channels; +float left_max=1, right_max=1; // start compressor with low gain SNDFILE *inf; @@ -250,7 +251,51 @@ int fm_mpx_get_samples(float *mpx_buffer) { out_left+=low_pass_fir[ iphase + (FIR_PHASES*fi) ] * fir_buffer_left[(fir_index-fi)&(FIR_SIZE-1)]; } } - + + // Simple broadcast compressor + // + // The goal is to get the loudest sounding audio while + // keeping the deviation within legal limits, and + // without degrading the audio quality significantly. + // Don't expect this simple code to match the + // performance of commercial broadcast equipment. + float left_abs, right_abs; + float compressor_decay=0.999995; + float compressor_attack=1.0; + // Setting attack to anything other than 1.0 could cause overshoot. + float compressor_max_gain_recip=0.01; + left_abs=fabsf(out_left); + if( left_abs>left_max ) + { + left_max+= (left_abs-left_max)*compressor_attack; + } + else + { + left_max*=compressor_decay; + } + + if( channels > 1 ) + { + right_abs=fabsf(out_right); + if( right_abs>right_max ) + { + right_max+= (right_abs-right_max)*compressor_attack; + } + else + { + right_max*=compressor_decay; + } + if( 1 )// Experimental joint compressor mode + { + if( left_max > right_max ) + right_max=left_max; + else if( left_max < right_max ) + left_max=right_max; + } + out_right=out_right/(right_max+compressor_max_gain_recip); + } + out_left= out_left/(left_max+compressor_max_gain_recip); // Adjust volume with limited maximum gain + // Generate the stereo mpx if( channels > 1 ) { mpx_buffer[i] += 4.05*(out_left+out_right) + // Stereo sum signal