From a2f768cbd64b92c4e8aebea6d49a64f6b76843fc Mon Sep 17 00:00:00 2001 From: Vadim Zubkov Date: Wed, 17 Jan 2024 20:03:07 +0300 Subject: [PATCH] reafactor: impove input normalization --- src/CSoundInput.cpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/CSoundInput.cpp b/src/CSoundInput.cpp index a2d1fb9..7d5eb9a 100644 --- a/src/CSoundInput.cpp +++ b/src/CSoundInput.cpp @@ -292,22 +292,32 @@ void CSoundInput::Normalize(void* buffer, DWORD sampleCount) } // Update normalizeMax using a running average or directly based on conditions - if (normalizeMax == 0.f || maxFrame > normalizeMax || normalizeMax / maxFrame < 0.5) { - normalizeMax = maxFrame; + bool forceReset = false; + if (normalizeMax == 0.f || maxFrame > normalizeMax) + { + forceReset = true; + normalizeMax = maxFrame * 1.5; } - else { + else + { + // Probably this smoothing is not needed because of the smoothing below normalizeMax = (normalizeMax * (NORMALIZE_FRAME_COUNT - 1) + maxFrame) / NORMALIZE_FRAME_COUNT; } - if (normalizeMax <= 1.f) { + if (normalizeMax <= 1.f) + { return; } float desiredGain = MAXSHORT / normalizeMax / 2; - desiredGain = std::fmin(desiredGain, 10.0f); + desiredGain = std::fmin(desiredGain, 3.0f); // Smooth the transition of gain - currentNormalizationGain = (1 - NORMALIZE_SMOOTHING_FACTOR) * currentNormalizationGain + NORMALIZE_SMOOTHING_FACTOR * desiredGain; + currentNormalizationGain = !forceReset + ? (1 - NORMALIZE_SMOOTHING_FACTOR) * currentNormalizationGain + NORMALIZE_SMOOTHING_FACTOR * desiredGain + : desiredGain; + + // std::cout << "MaxFrame: " << normalizeMax << ", Desired gain: " << desiredGain << ", Current gain: " << currentNormalizationGain << std::endl; auto shortSamples = static_cast(buffer);