From 33a7e32364ff6582abdae6e5f3d4b3b30228ce10 Mon Sep 17 00:00:00 2001 From: Vadim Zubkov Date: Sun, 21 Jan 2024 16:39:33 +0300 Subject: [PATCH] refactor: remove unneded buffer copies --- include/IOpusEncoder.h | 2 +- src/COpusEncoder.cpp | 2 +- src/COpusEncoder.h | 2 +- src/CSoundInput.cpp | 30 ++++++++---------------------- src/CSoundInput.h | 3 --- 5 files changed, 11 insertions(+), 28 deletions(-) diff --git a/include/IOpusEncoder.h b/include/IOpusEncoder.h index 7893e70..b6b6ddf 100644 --- a/include/IOpusEncoder.h +++ b/include/IOpusEncoder.h @@ -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; }; diff --git a/src/COpusEncoder.cpp b/src/COpusEncoder.cpp index 4f3dcbb..8390c70 100644 --- a/src/COpusEncoder.cpp +++ b/src/COpusEncoder.cpp @@ -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) diff --git a/src/COpusEncoder.h b/src/COpusEncoder.h index 7f64378..d576ec5 100644 --- a/src/COpusEncoder.h +++ b/src/COpusEncoder.h @@ -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; }; diff --git a/src/CSoundInput.cpp b/src/CSoundInput.cpp index 7d5eb9a..ea250f3 100644 --- a/src/CSoundInput.cpp +++ b/src/CSoundInput.cpp @@ -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); } @@ -153,10 +152,7 @@ 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); @@ -164,9 +160,6 @@ AltVoiceError CSoundInput::SelectDeviceByUID(const char* uid) 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; @@ -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(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(maxFrame, abs(buffer2[i])); // Convert to float from 0.f to 1.f - micLevel = static_cast(leftChannelLevel) / MaxShortFloatValue; + micLevel = static_cast(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); } } diff --git a/src/CSoundInput.h b/src/CSoundInput.h index a850174..2353f30 100644 --- a/src/CSoundInput.h +++ b/src/CSoundInput.h @@ -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; @@ -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;