ViPERFX_RE/src/cpp/viper/utils/WaveBuffer.cpp

144 lines
3.8 KiB
C++
Raw Normal View History

2022-09-18 03:38:22 +02:00
#include "WaveBuffer.h"
2021-07-31 01:15:40 +02:00
#include <cstring>
2022-09-21 03:43:03 +02:00
WaveBuffer::WaveBuffer(uint32_t channels, uint32_t size) {
2021-07-31 01:15:40 +02:00
this->channels = channels;
this->size = size * channels;
this->index = 0;
this->buffer = new float[this->size];
2021-07-31 01:15:40 +02:00
}
2022-09-18 03:38:22 +02:00
WaveBuffer::~WaveBuffer() {
2022-10-06 03:37:22 +02:00
delete[] this->buffer;
2021-07-31 01:15:40 +02:00
}
2022-09-18 03:38:22 +02:00
uint32_t WaveBuffer::GetBufferOffset() {
2021-07-31 01:15:40 +02:00
return this->index / this->channels;
}
2022-09-18 03:38:22 +02:00
uint32_t WaveBuffer::GetBufferSize() {
2021-07-31 01:15:40 +02:00
return this->size / this->channels;
}
2022-09-18 03:38:22 +02:00
float *WaveBuffer::GetBuffer() {
2021-07-31 01:15:40 +02:00
return this->buffer;
}
2022-09-18 03:38:22 +02:00
uint32_t WaveBuffer::PopSamples(uint32_t size, bool resetIndex) {
2021-07-31 01:15:40 +02:00
if (this->buffer == nullptr || this->size == 0) {
return 0;
}
if (this->channels * size <= this->index) {
this->index -= this->channels * size;
memmove(this->buffer, &this->buffer[this->channels * size], this->index * sizeof(float));
2021-07-31 01:15:40 +02:00
return size;
}
if (resetIndex) {
uint32_t idx = this->index;
this->index = 0;
return idx / this->channels;
}
return 0;
}
2022-09-18 03:38:22 +02:00
uint32_t WaveBuffer::PopSamples(float *dest, uint32_t size, bool resetIndex) {
2021-07-31 01:15:40 +02:00
if (this->buffer == nullptr || this->size == 0 || dest == nullptr) {
return 0;
}
if (this->channels * size <= this->index) {
2022-09-18 03:38:22 +02:00
memcpy(dest, this->buffer, this->channels * size * sizeof(float));
2021-07-31 01:15:40 +02:00
this->index -= this->channels * size;
memmove(this->buffer, &this->buffer[this->channels * size], this->index * sizeof(float));
2021-07-31 01:15:40 +02:00
return size;
}
if (resetIndex) {
uint32_t idx = this->index;
memcpy(dest, this->buffer, this->index * sizeof(float));
this->index = 0;
return idx / this->channels;
}
return 0;
}
2022-09-18 03:38:22 +02:00
int WaveBuffer::PushSamples(float *source, uint32_t size) {
2021-07-31 01:15:40 +02:00
if (this->buffer == nullptr) {
return 0;
}
if (size > 0) {
2022-09-18 03:38:22 +02:00
uint32_t requiredSize = this->channels * size + this->index;
if (this->size < requiredSize) {
auto *buf = new float[requiredSize];
2021-07-31 01:15:40 +02:00
memcpy(buf, this->buffer, this->index * sizeof(float));
2022-10-06 03:37:22 +02:00
delete[] this->buffer;
2021-07-31 01:15:40 +02:00
this->buffer = buf;
2022-09-18 03:38:22 +02:00
this->size = requiredSize;
2021-07-31 01:15:40 +02:00
}
memcpy(&this->buffer[this->index], source, this->channels * size * sizeof(float));
this->index += this->channels * size;
}
return 1;
}
2022-09-18 03:38:22 +02:00
int WaveBuffer::PushZeros(uint32_t size) {
2021-07-31 01:15:40 +02:00
if (this->buffer == nullptr) {
return 0;
}
if (size > 0) {
2022-09-18 03:38:22 +02:00
uint32_t requiredSize = this->channels * size + this->index;
if (this->size < requiredSize) {
auto *buf = new float[requiredSize];
2021-07-31 01:15:40 +02:00
memcpy(buf, this->buffer, this->index * sizeof(float));
2022-10-06 03:37:22 +02:00
delete[] this->buffer;
2021-07-31 01:15:40 +02:00
this->buffer = buf;
2022-09-18 03:38:22 +02:00
this->size = requiredSize;
2021-07-31 01:15:40 +02:00
}
memset(&this->buffer[this->index], 0, this->channels * size * sizeof(float));
this->index += this->channels * size;
}
return 1;
}
2022-09-18 03:38:22 +02:00
float *WaveBuffer::PushZerosGetBuffer(uint32_t size) {
uint32_t oldIndex = this->index;
2021-07-31 01:15:40 +02:00
if (this->buffer == nullptr) {
return nullptr;
}
if (size > 0) {
2022-09-18 03:38:22 +02:00
uint32_t requiredSize = this->channels * size + this->index;
if (this->size < requiredSize) {
auto *buf = new float[requiredSize];
2021-07-31 01:15:40 +02:00
memcpy(buf, this->buffer, this->index * sizeof(float));
2022-10-06 03:37:22 +02:00
delete[] this->buffer;
2021-07-31 01:15:40 +02:00
this->buffer = buf;
2022-09-18 03:38:22 +02:00
this->size = requiredSize;
2021-07-31 01:15:40 +02:00
}
memset(&this->buffer[this->index], 0, this->channels * size * sizeof(float));
this->index += this->channels * size;
}
2022-09-18 03:38:22 +02:00
return &this->buffer[oldIndex];
2021-07-31 01:15:40 +02:00
}
2022-09-18 03:38:22 +02:00
void WaveBuffer::Reset() {
this->index = 0;
}
void WaveBuffer::SetBufferOffset(uint32_t offset) {
uint32_t maxOffset = this->size / this->channels;
if (offset <= maxOffset) {
this->index = offset * this->channels;
}
2021-07-31 01:15:40 +02:00
}