Audio compressor for more volume

This commit is contained in:
SaucySoliton 2017-01-06 07:30:06 +00:00
parent 6d192bbbe0
commit ea123e4425
1 changed files with 46 additions and 1 deletions

View File

@ -65,6 +65,7 @@ float fir_buffer_left[FIR_SIZE] = {0};
float fir_buffer_right[FIR_SIZE] = {0}; float fir_buffer_right[FIR_SIZE] = {0};
int fir_index = 0; int fir_index = 0;
int channels; int channels;
float left_max=1, right_max=1; // start compressor with low gain
SNDFILE *inf; SNDFILE *inf;
@ -251,6 +252,50 @@ int fm_mpx_get_samples(float *mpx_buffer) {
} }
} }
// 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 // Generate the stereo mpx
if( channels > 1 ) { if( channels > 1 ) {
mpx_buffer[i] += 4.05*(out_left+out_right) + // Stereo sum signal mpx_buffer[i] += 4.05*(out_left+out_right) + // Stereo sum signal