AdaptiveBuffer: Initial implementation

This commit is contained in:
Iscle 2022-09-13 02:14:03 +02:00
parent 30e8bd545c
commit c6390aceb7
5 changed files with 148 additions and 82 deletions

View File

@ -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

View File

@ -0,0 +1,118 @@
#include <cstring>
#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;
}

View File

@ -0,0 +1,29 @@
#pragma once
#include <cstdint>
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;
};

View File

@ -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) {
}

View File

@ -1,24 +0,0 @@
#pragma once
#include <cstdint>
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);
};