Skip to content

Commit

Permalink
reafactor: impove input normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
vadzz-dev committed Jan 17, 2024
1 parent 4c1589a commit a2f768c
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/CSoundInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<short*>(buffer);

Expand Down

0 comments on commit a2f768c

Please sign in to comment.