From cf134277867ce0076c6068e694b1881d97dc20fd Mon Sep 17 00:00:00 2001 From: Yauhen Pahrabniak Date: Thu, 26 Oct 2023 14:56:51 +0200 Subject: [PATCH] Fixed normalization --- src/CSoundInput.cpp | 8 +++----- src/CSoundInput.h | 3 ++- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/CSoundInput.cpp b/src/CSoundInput.cpp index 4c744d0..8cc2101 100644 --- a/src/CSoundInput.cpp +++ b/src/CSoundInput.cpp @@ -314,15 +314,13 @@ void CSoundInput::Normalize(void* buffer, DWORD sampleCount) desiredGain = std::fmin(desiredGain, 10.0f); // Smooth the transition of gain - static float currentGain = 1.0f; - float smoothingFactor = 0.05f; - currentNormalizationGain = (1 - smoothingFactor) * currentNormalizationGain + smoothingFactor * desiredGain; + currentNormalizationGain = (1 - NORMALIZE_SMOOTHING_FACTOR) * currentNormalizationGain + NORMALIZE_SMOOTHING_FACTOR * desiredGain; - const auto shortSamples = static_cast(buffer); + auto shortSamples = static_cast(buffer); for (int i = 0; i < sampleCount; ++i) { - float processedSample = (float)shortSamples[i] * currentGain; + float processedSample = (float)shortSamples[i] * currentNormalizationGain; shortSamples[i] = static_cast(clampFloatSample(processedSample)); } } diff --git a/src/CSoundInput.h b/src/CSoundInput.h index ea6936e..a850174 100644 --- a/src/CSoundInput.h +++ b/src/CSoundInput.h @@ -12,7 +12,8 @@ class CSoundInput : public ISoundInput { static constexpr int RNNoiseFrameSize = 480; static constexpr float MaxShortFloatValue = 32768.0f; - static constexpr int NORMALIZE_FRAME_COUNT = 20; + static constexpr int NORMALIZE_FRAME_COUNT = 50; + static constexpr float NORMALIZE_SMOOTHING_FACTOR = 0.05f; HRECORD recordChannel = 0; HSTREAM levelChannel = 0;