diff --git a/CMakeLists.txt b/CMakeLists.txt index b11924f..4582119 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,7 +43,7 @@ set(FILES src/cpp/viper/effects/ViPERDDC.cpp # Utils - src/cpp/viper/utils/AdaptiveBuffer_F32.cpp + src/cpp/viper/utils/AdaptiveBuffer.cpp src/cpp/viper/utils/CAllpassFilter.cpp src/cpp/viper/utils/CCombFilter.cpp src/cpp/viper/utils/CRevModel.cpp diff --git a/src/cpp/viper/utils/AdaptiveBuffer.cpp b/src/cpp/viper/utils/AdaptiveBuffer.cpp new file mode 100644 index 0000000..d24030b --- /dev/null +++ b/src/cpp/viper/utils/AdaptiveBuffer.cpp @@ -0,0 +1,118 @@ +#include +#include "AdaptiveBuffer.h" + +AdaptiveBuffer::AdaptiveBuffer(uint32_t channels, uint32_t length) { + this->channels = channels; + this->buffer = nullptr; + this->length = 0; + this->offset = 0; + if (channels != 0) { + this->buffer = new float[channels * length]; + this->length = length; + } +} + +AdaptiveBuffer::~AdaptiveBuffer() { + delete this->buffer; + this->buffer = nullptr; +} + +void AdaptiveBuffer::FlushBuffer() { + this->offset = 0; +} + +uint32_t AdaptiveBuffer::GetBufferLength() { + return this->length; +} + +uint32_t AdaptiveBuffer::GetBufferOffset() { + return this->offset; +} + +float *AdaptiveBuffer::GetBufferPointer() { + return this->buffer; +} + +uint32_t AdaptiveBuffer::GetChannels() { + return this->channels; +} + +void AdaptiveBuffer::PanFrames(float left, float right) { + if (this->buffer != nullptr && this->channels == 2) { + for (int i = 0; i < this->offset * this->channels; i++) { + if (i % 2 == 0) { + this->buffer[i] = this->buffer[i] * left; + } else { + this->buffer[i] = this->buffer[i] * right; + } + } + } +} + +int AdaptiveBuffer::PopFrames(float *frames, uint32_t length) { + if (this->buffer == nullptr || this->offset < length) { + return 0; + } + + if (length != 0) { + memcpy(frames, this->buffer, length * this->channels * sizeof(*frames)); + this->offset = this->offset - length; + if (this->offset != 0) { + memmove(this->buffer, &this->buffer[length * this->channels], this->offset * this->channels * sizeof(*this->buffer)); + } + } + + return 1; +} + +int AdaptiveBuffer::PushFrames(float *frames, uint32_t length) { + if (this->buffer == nullptr) { + return 0; + } + + if (length != 0) { + if (this->offset + length > this->length) { + auto tmp = new float[(this->offset + length) * this->channels]; + memcpy(tmp, this->buffer, this->offset * this->channels * sizeof(*this->buffer)); + delete this->buffer; + this->buffer = tmp; + this->length = this->offset + length; + } + + memcpy(&this->buffer[this->offset * this->channels], frames, length * this->channels * sizeof(*frames)); + this->offset = this->offset + length; + } + + return 1; +} + +int AdaptiveBuffer::PushZero(uint32_t length) { + if (this->buffer == nullptr) { + return 0; + } + + if (this->offset + length > this->length) { + auto tmp = new float[(this->offset + length) * this->channels]; + memcpy(tmp, this->buffer, this->offset * this->channels * sizeof(*this->buffer)); + delete this->buffer; + this->buffer = tmp; + this->length = this->offset + length; + } + + memset(&this->buffer[this->offset * this->channels], 0, length * this->channels * sizeof(*this->buffer)); + this->offset = this->offset + length; + + return 1; +} + +void AdaptiveBuffer::ScaleFrames(float scale) { + if (this->buffer != nullptr) { + for (uint32_t i = 0; i < this->offset * this->channels; i++) { + this->buffer[i] = this->buffer[i] * scale; + } + } +} + +void AdaptiveBuffer::SetBufferOffset(uint32_t offset) { + this->offset = offset; +} diff --git a/src/cpp/viper/utils/AdaptiveBuffer.h b/src/cpp/viper/utils/AdaptiveBuffer.h new file mode 100644 index 0000000..8f3cda5 --- /dev/null +++ b/src/cpp/viper/utils/AdaptiveBuffer.h @@ -0,0 +1,29 @@ +#pragma once + +#include + +class AdaptiveBuffer { +public: + AdaptiveBuffer(uint32_t channels, uint32_t length); + ~AdaptiveBuffer(); + + void FlushBuffer(); + uint32_t GetBufferLength(); + uint32_t GetBufferOffset(); + float *GetBufferPointer(); + uint32_t GetChannels(); + void PanFrames(float left, float right); + int PopFrames(float *frames, uint32_t length); + int PushFrames(float *frames, uint32_t length); + int PushZero(uint32_t length); + void ScaleFrames(float scale); + void SetBufferOffset(uint32_t offset); + + float *buffer; + uint32_t length; + uint32_t offset; + uint32_t channels; + +}; + + diff --git a/src/cpp/viper/utils/AdaptiveBuffer_F32.cpp b/src/cpp/viper/utils/AdaptiveBuffer_F32.cpp deleted file mode 100644 index 9b0478d..0000000 --- a/src/cpp/viper/utils/AdaptiveBuffer_F32.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include "AdaptiveBuffer_F32.h" - -AdaptiveBuffer_F32::AdaptiveBuffer_F32(int channels, uint32_t size) { - // TODO -} - -AdaptiveBuffer_F32::~AdaptiveBuffer_F32() { - -} - -void AdaptiveBuffer_F32::FlushBuffer() { - -} - -uint32_t AdaptiveBuffer_F32::GetBufferLength() { - return 0; -} - -int32_t AdaptiveBuffer_F32::GetBufferOffset() { - return 0; -} - -float *AdaptiveBuffer_F32::GetBufferPointer() { - return nullptr; -} - -uint32_t AdaptiveBuffer_F32::GetChannels() { - return 0; -} - -void AdaptiveBuffer_F32::PanFrames(int32_t param_1, int32_t param_2) { - -} - -int32_t AdaptiveBuffer_F32::PopFrames(int16_t param_1, uint32_t param_2) { - return 0; -} - -int32_t AdaptiveBuffer_F32::PushFrames(int16_t param_1, uint32_t param_2) { - return 0; -} - -int32_t AdaptiveBuffer_F32::PushFrames(int32_t *param_1, uint32_t param_2) { - return 0; -} - -int32_t AdaptiveBuffer_F32::PushZero(uint32_t param_1) { - return 0; -} - -void AdaptiveBuffer_F32::ScaleFrames(int32_t param_1) { - -} - -void AdaptiveBuffer_F32::SetBufferOffset(uint32_t param_1) { - -} diff --git a/src/cpp/viper/utils/AdaptiveBuffer_F32.h b/src/cpp/viper/utils/AdaptiveBuffer_F32.h deleted file mode 100644 index fadd931..0000000 --- a/src/cpp/viper/utils/AdaptiveBuffer_F32.h +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once - -#include - -class AdaptiveBuffer_F32 { -public: - AdaptiveBuffer_F32(int channels, uint32_t size); - ~AdaptiveBuffer_F32(); - - void FlushBuffer(); - uint32_t GetBufferLength(); - int32_t GetBufferOffset(); - float *GetBufferPointer(); - uint32_t GetChannels(); - void PanFrames(int32_t param_1, int32_t param_2); - int32_t PopFrames(int16_t param_1, uint32_t param_2); - int32_t PushFrames(int16_t param_1, uint32_t param_2); - int32_t PushFrames(int32_t *param_1, uint32_t param_2); - int32_t PushZero(uint32_t param_1); - void ScaleFrames(int32_t param_1); - void SetBufferOffset(uint32_t param_1); -}; - -