Skip to content

Commit

Permalink
readd normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
martonp96 committed Aug 15, 2023
1 parent 27329e8 commit 5693da0
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions examples/devicetests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ int main()
soundInput->SelectDeviceByUID(nullptr);
soundInput->SetStreamEnabled(true);
soundInput->SetNoiseSuppressionEnabled(true);
soundInput->SetNormalizatonEnabled(true);
std::cout << soundInput->GetCurrentDeviceUID() << std::endl;

for(;;)
Expand Down
3 changes: 3 additions & 0 deletions include/ISoundInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@ class ISoundInput
virtual void SetStreamEnabled(bool enabled) = 0;
virtual void SetNoiseSuppressionEnabled(bool enabled) = 0;
[[nodiscard]] virtual bool IsNoiseSuppressionEnabled() const = 0;

virtual void SetNormalizatonEnabled(bool enabled) = 0;
[[nodiscard]] virtual bool IsNormalizationEnabled() const = 0;
};
41 changes: 41 additions & 0 deletions src/CSoundInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,44 @@ void CSoundInput::NoiseSuppressionProcess(void* buffer, DWORD length)
}
}

void CSoundInput::SetNormalizatonEnabled(bool enabled)
{
normalizationEnabled = enabled;
}

bool CSoundInput::IsNormalizationEnabled() const
{
return normalizationEnabled;
}

void CSoundInput::Normalize(short* buffer, DWORD length)
{
if(normalizationEnabled)
{
short maxFrame = 0;
for (int i = 0; i < length; ++i)
{
short s = abs(buffer[i]);
if (s > maxFrame)
maxFrame = s;
}

if (normalizeMax == 0.f || maxFrame > normalizeMax || normalizeMax / maxFrame < 0.5)
normalizeMax = maxFrame;
else
normalizeMax = (normalizeMax * (NORMALIZE_FRAME_COUNT - 1) + maxFrame) / NORMALIZE_FRAME_COUNT;

if (normalizeMax <= 1.f)
return;

float gain = MAXSHORT / normalizeMax / 2;
gain = std::fmin<float, float>(gain, 10);

for (int i = 0; i < length; ++i)
buffer[i] *= gain;
}
}

void CSoundInput::SoundFrameCaptured(HRECORD handle, const void* buffer, DWORD length)
{
// Create new buffer on stack because buffer was marked as const in API
Expand All @@ -250,6 +288,9 @@ void CSoundInput::SoundFrameCaptured(HRECORD handle, const void* buffer, DWORD l
// Apply noise suppression
NoiseSuppressionProcess(writableBuffer, FRAME_SIZE_SAMPLES);

// Apply normalization
Normalize(writableBuffer, FRAME_SIZE_SAMPLES);

// Get current microphone noise level
const DWORD currentMicLevel = BASS_ChannelGetLevel(handle);

Expand Down
10 changes: 9 additions & 1 deletion src/CSoundInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class CSoundInput : public ISoundInput
{
static constexpr int RNNoiseFrameSize = 480;
static constexpr float MaxShortFloatValue = 32768.0f;
static constexpr int NORMALIZE_FRAME_COUNT = 20;

HRECORD recordChannel = 0;
COpusEncoder* encoder = nullptr;
Expand All @@ -20,6 +21,9 @@ class CSoundInput : public ISoundInput
float micLevel = 0;
bool noiseSuppressionEnabled = false;

bool normalizationEnabled = false;
float normalizeMax = 0.f;

HFX VolumeChangeFX;
DenoiseState* denoiser;

Expand Down Expand Up @@ -56,8 +60,12 @@ class CSoundInput : public ISoundInput
void RegisterCallback(OnVoiceCallback callback) override;

void SetNoiseSuppressionEnabled(bool enabled) override;
void NoiseSuppressionProcess(void* buffer, DWORD length);
[[nodiscard]] bool IsNoiseSuppressionEnabled() const override;
void NoiseSuppressionProcess(void* buffer, DWORD length);

void SetNormalizatonEnabled(bool enabled) override;
[[nodiscard]] bool IsNormalizationEnabled() const override;
void Normalize(short* buffer, DWORD length);

void SoundFrameCaptured(HRECORD handle, const void* buffer, DWORD length);
static BOOL OnSoundFrame(HRECORD handle, const void* buffer, DWORD length, void* user);
Expand Down

0 comments on commit 5693da0

Please sign in to comment.