ViPERFX_RE/src/viper/utils/WaveBuffer.cpp

144 lines
3.9 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-10-11 03:07:11 +02:00
WaveBuffer::WaveBuffer(uint32_t channels, uint32_t length) {
2021-07-31 01:15:40 +02:00
this->channels = channels;
2022-10-11 03:07:11 +02:00
this->size = length * channels;
2021-07-31 01:15:40 +02:00
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;
2022-10-11 03:07:11 +02:00
memmove(this->buffer, this->buffer + this->channels * size, this->index * sizeof(float));
2021-07-31 01:15:40 +02:00
return size;
}
if (resetIndex) {
2022-10-11 03:07:11 +02:00
uint32_t ret = this->index / this->channels;
2021-07-31 01:15:40 +02:00
this->index = 0;
2022-10-11 03:07:11 +02:00
return ret;
2021-07-31 01:15:40 +02:00
}
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;
2022-10-11 03:07:11 +02:00
memmove(this->buffer, this->buffer + this->channels * size, this->index * sizeof(float));
2021-07-31 01:15:40 +02:00
return size;
}
if (resetIndex) {
2022-10-11 03:07:11 +02:00
uint32_t ret = this->index / this->channels;
2021-07-31 01:15:40 +02:00
memcpy(dest, this->buffer, this->index * sizeof(float));
this->index = 0;
2022-10-11 03:07:11 +02:00
return ret;
2021-07-31 01:15:40 +02:00
}
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) {
2022-10-11 03:07:11 +02:00
auto *newBuffer = new float[requiredSize];
memcpy(newBuffer, this->buffer, this->index * sizeof(float));
2022-10-06 03:37:22 +02:00
delete[] this->buffer;
2022-10-11 03:07:11 +02:00
this->buffer = newBuffer;
2022-09-18 03:38:22 +02:00
this->size = requiredSize;
2021-07-31 01:15:40 +02:00
}
2022-10-11 03:07:11 +02:00
memcpy(this->buffer + this->index, source, this->channels * size * sizeof(float));
2021-07-31 01:15:40 +02:00
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) {
2022-10-11 03:07:11 +02:00
auto *newBuffer = new float[requiredSize];
memcpy(newBuffer, this->buffer, this->index * sizeof(float));
2022-10-06 03:37:22 +02:00
delete[] this->buffer;
2022-10-11 03:07:11 +02:00
this->buffer = newBuffer;
2022-09-18 03:38:22 +02:00
this->size = requiredSize;
2021-07-31 01:15:40 +02:00
}
2022-10-11 03:07:11 +02:00
memset(this->buffer + this->index, 0, this->channels * size * sizeof(float));
2021-07-31 01:15:40 +02:00
this->index += this->channels * size;
}
return 1;
}
2022-09-18 03:38:22 +02:00
float *WaveBuffer::PushZerosGetBuffer(uint32_t size) {
2021-07-31 01:15:40 +02:00
if (this->buffer == nullptr) {
return nullptr;
}
2022-10-11 03:07:11 +02:00
uint32_t oldIndex = this->index;
2021-07-31 01:15:40 +02:00
if (size > 0) {
2022-09-18 03:38:22 +02:00
uint32_t requiredSize = this->channels * size + this->index;
if (this->size < requiredSize) {
2022-10-11 03:07:11 +02:00
auto *newBuffer = new float[requiredSize];
memcpy(newBuffer, this->buffer, this->index * sizeof(float));
2022-10-06 03:37:22 +02:00
delete[] this->buffer;
2022-10-11 03:07:11 +02:00
this->buffer = newBuffer;
2022-09-18 03:38:22 +02:00
this->size = requiredSize;
2021-07-31 01:15:40 +02:00
}
2022-10-11 03:07:11 +02:00
memset(this->buffer + this->index, 0, this->channels * size * sizeof(float));
2021-07-31 01:15:40 +02:00
this->index += this->channels * size;
}
2022-10-11 03:07:11 +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
}