From 9ffa048b24009a867196d57bfbafef201922f34d Mon Sep 17 00:00:00 2001 From: Alexis Murzeau Date: Sat, 24 Aug 2024 14:48:11 +0200 Subject: [PATCH] damc: compressor: fix unwanted gain reset when a sample is 0 Instead, only reset when a full buffer is 0 (1ms). --- damc/damc_audio_processing/CompressorFilter.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/damc/damc_audio_processing/CompressorFilter.cpp b/damc/damc_audio_processing/CompressorFilter.cpp index 3148efd..528e185 100644 --- a/damc/damc_audio_processing/CompressorFilter.cpp +++ b/damc/damc_audio_processing/CompressorFilter.cpp @@ -61,6 +61,7 @@ void CompressorFilter::reset() { void CompressorFilter::processSamples(float** samples, size_t count) { if(enablePeak || enableLoudness) { + bool samplesAreAllZeroes = true; float staticGain = gainComputer(0) + makeUpGain; for(size_t i = 0; i < count; i++) { float levelPeak = 0; @@ -76,6 +77,9 @@ void CompressorFilter::processSamples(float** samples, size_t count) { } } + if(levelPeak != 0) + samplesAreAllZeroes = false; + float dbGain = doCompression(levelPeak, levelLoudness); // db to ratio @@ -85,6 +89,13 @@ void CompressorFilter::processSamples(float** samples, size_t count) { samples[channel][i] = compressionRatio * samples[channel][i]; } } + + if(samplesAreAllZeroes) { + // When no sound, avoid big amplification that would cause high volume sound when some sound is played + // Make the gain assume the volume is 0dBFS. + + y1 = yL = gainComputer(0.f); + } } } @@ -101,12 +112,6 @@ float CompressorFilter::doCompression(float levelPeak, float levelLoudness) { dbSample = std::max(dbSample, lufsRealtimeLevel - lufsTarget.get()); } - if(dbSample == -INFINITY) { - // When no sound, avoid big amplification that would cause high volume sound when some sound is played - // Make the gain assume the volume is 0dBFS. - dbSample = 0; - } - levelDetectorSmoothing(gainComputer(dbSample)); return -yL; }