Skip to content

Commit

Permalink
refactor: remove unneded buffer copies
Browse files Browse the repository at this point in the history
  • Loading branch information
vadzz-dev committed Jan 21, 2024
1 parent a2f768c commit 33a7e32
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 28 deletions.
2 changes: 1 addition & 1 deletion include/IOpusEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ class IOpusEncoder
public:
virtual ~IOpusEncoder() = default;

virtual int EncodeShort(void* pcmData, size_t size, void* output, size_t outputSize) = 0;
virtual int EncodeShort(const void* pcmData, size_t size, void* output, size_t outputSize) = 0;
};
2 changes: 1 addition & 1 deletion src/COpusEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ COpusEncoder::~COpusEncoder()
opus_encoder_destroy(encoder);
}

int COpusEncoder::EncodeShort(void* pcmData, size_t size, void* output, size_t outputSize)
int COpusEncoder::EncodeShort(const void* pcmData, size_t size, void* output, size_t outputSize)
{
const int len = opus_encode(encoder, (const opus_int16*)pcmData, size, (unsigned char*)output, outputSize);
if (len < 0 || len > outputSize)
Expand Down
2 changes: 1 addition & 1 deletion src/COpusEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ class COpusEncoder : public IOpusEncoder
COpusEncoder(int sampleRate, int channels, int bitRate);
~COpusEncoder() override;

int EncodeShort(void* pcmData, size_t size, void* output, size_t outputSize) override;
int EncodeShort(const void* pcmData, size_t size, void* output, size_t outputSize) override;
};
30 changes: 8 additions & 22 deletions src/CSoundInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ void CSoundInput::SetVolume(float gain)
{
volume = gain;
const BASS_BFX_VOLUME VolumeChangeFXParams = { BASS_BFX_CHANALL, volume * 2 };
BASS_FXSetParameters(LevelVolumeChangeFX, &VolumeChangeFXParams);
BASS_FXSetParameters(VolumeChangeFX, &VolumeChangeFXParams);
}

Expand Down Expand Up @@ -153,20 +152,14 @@ AltVoiceError CSoundInput::SelectDeviceByUID(const char* uid)
recordChannel = BASS_RecordStart(SAMPLE_RATE, AUDIO_CHANNELS, 0, OnSoundFrame, this);
BASS_ChannelSetAttribute(recordChannel, BASS_ATTRIB_GRANULE, FRAME_SIZE_SAMPLES);

levelChannel = BASS_StreamCreate(SAMPLE_RATE, AUDIO_CHANNELS, BASS_STREAM_DECODE, STREAMPROC_PUSH, this);

// Change input volume
LevelVolumeChangeFX = BASS_ChannelSetFX(levelChannel, BASS_FX_BFX_VOLUME, 0);
VolumeChangeFX = BASS_ChannelSetFX(recordChannel, BASS_FX_BFX_VOLUME, 0);

SetVolume(volume);

BASS_ChannelSetDSP(recordChannel, NoiseDSP, this, 2); //higher prio called first
BASS_ChannelSetDSP(recordChannel, NormalizeDSP, this, 1);

BASS_ChannelStart(levelChannel);
BASS_ChannelPlay(levelChannel, false);

if (!recordChannel)
return AltVoiceError::StartStream;

Expand Down Expand Up @@ -354,31 +347,24 @@ void CSoundInput::NoiseDSP(HDSP handle, DWORD channel, void* buffer, DWORD lengt

void CSoundInput::SoundFrameCaptured(HRECORD handle, const void* buffer, DWORD length)
{
// Put available data in the level channel
BASS_StreamPutData(levelChannel, buffer, length * sizeof(short));

// Get current microphone noise level from level channel
const DWORD currentMicLevel = BASS_ChannelGetLevel(levelChannel);
uint32_t numFrames = length / sizeof(short);
const int16_t* buffer2 = static_cast<const int16_t*>(buffer);

// Get left channel noise level from it (because it's mono so right = left)
const uint16_t leftChannelLevel = LOWORD(currentMicLevel);
uint16_t maxFrame = 0;
for (int i = 0; i < numFrames; ++i)
maxFrame = std::max<uint16_t>(maxFrame, abs(buffer2[i]));

// Convert to float from 0.f to 1.f
micLevel = static_cast<float>(leftChannelLevel) / MaxShortFloatValue;
micLevel = static_cast<float>(maxFrame) / MaxShortFloatValue;

// Convert level to decibels
micLevelDb = (micLevel > 0 ? 20 * log10(micLevel) : -HUGE_VAL);
micLevelDb = std::clamp(micLevelDb, -100.f, 0.f) + 100.f;

// Remove data from level channel (is there a better way to do it?)
BASS_ChannelGetData(levelChannel, writableBuffer, length * sizeof(short));

// Copy mic data to output buffer
memcpy_s(writableBuffer, FRAME_SIZE_SAMPLES * sizeof(short), buffer, length);

if (VoiceCallback)
{
const int opusBufferSize = encoder->EncodeShort(writableBuffer, FRAME_SIZE_SAMPLES, opusBuffer, bitrate);
const int opusBufferSize = encoder->EncodeShort(buffer, numFrames, opusBuffer, bitrate);
std::cout << "opusBufferSize: " << opusBufferSize << std::endl;
VoiceCallback(opusBuffer, opusBufferSize);
}
}
3 changes: 0 additions & 3 deletions src/CSoundInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class CSoundInput : public ISoundInput
static constexpr float NORMALIZE_SMOOTHING_FACTOR = 0.05f;

HRECORD recordChannel = 0;
HSTREAM levelChannel = 0;
COpusEncoder* encoder = nullptr;
int bitrate;

Expand All @@ -30,10 +29,8 @@ class CSoundInput : public ISoundInput
float normalizeMax = 0.f;

HFX VolumeChangeFX;
HFX LevelVolumeChangeFX;
DenoiseState* denoiser;

short writableBuffer[FRAME_SIZE_SAMPLES];
float floatBuffer[FRAME_SIZE_SAMPLES];
char* opusBuffer = nullptr;

Expand Down

0 comments on commit 33a7e32

Please sign in to comment.