Skip to content

Commit

Permalink
damc: compressor: fix unwanted gain reset when a sample is 0
Browse files Browse the repository at this point in the history
Instead, only reset when a full buffer is 0 (1ms).
  • Loading branch information
amurzeau committed Aug 25, 2024
1 parent 52113b4 commit 9ffa048
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions damc/damc_audio_processing/CompressorFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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);
}
}
}

Expand All @@ -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;
}
Expand Down

0 comments on commit 9ffa048

Please sign in to comment.