Skip to content

Commit

Permalink
fixed distortion with enabled noice suppression
Browse files Browse the repository at this point in the history
  • Loading branch information
emcifuntik committed Oct 24, 2023
1 parent 8cda5d0 commit 4457bfb
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/CSoundInput.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#define NOMINMAX

#include "CSoundInput.h"

#include <chrono>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <limits>

#include <bass_fx.h>
#include "alt-voice.h"


CSoundInput::CSoundInput(int _bitRate) : encoder(new COpusEncoder(SAMPLE_RATE, AUDIO_CHANNELS, _bitRate)), bitrate(_bitRate)
{
denoiser = rnnoise_create(nullptr);
Expand Down Expand Up @@ -261,6 +263,12 @@ bool CSoundInput::IsNoiseSuppressionEnabled() const
return noiseSuppressionEnabled;
}

// https://github.com/mumble-voip/mumble/pull/5363/files
static short clampFloatSample(float v) {
return static_cast<short>(std::min(std::max(v, static_cast<float>(std::numeric_limits<short>::min())),
static_cast<float>(std::numeric_limits<short>::max())));
}

void CSoundInput::NoiseSuppressionProcess(void* buffer, DWORD length)
{
const auto shortSamples = static_cast<short*>(buffer);
Expand All @@ -280,7 +288,7 @@ void CSoundInput::NoiseSuppressionProcess(void* buffer, DWORD length)
// Convert the floating-point samples back to 16-bit integer samples
for (int i = 0; i < FRAME_SIZE_SAMPLES; i++)
{
shortSamples[i] = static_cast<int16_t>(floatBuffer[i]);
shortSamples[i] = static_cast<int16_t>(clampFloatSample(floatBuffer[i]));
}
}

Expand Down

0 comments on commit 4457bfb

Please sign in to comment.