ViPERFX_RE/src/viper/utils/AdaptiveBuffer.cpp

118 lines
3.1 KiB
C++
Raw Normal View History

2022-09-13 02:14:03 +02:00
#include <cstring>
#include "AdaptiveBuffer.h"
2022-09-18 03:39:40 +02:00
AdaptiveBuffer::AdaptiveBuffer(uint32_t channels, uint32_t length) {
2022-09-13 02:14:03 +02:00
this->channels = channels;
this->buffer = nullptr;
this->length = 0;
this->offset = 0;
if (channels != 0) {
this->buffer = new float[channels * length];
this->length = length;
}
}
2022-09-18 03:39:40 +02:00
AdaptiveBuffer::~AdaptiveBuffer() {
2022-09-13 02:14:03 +02:00
delete this->buffer;
}
2022-09-18 03:39:40 +02:00
void AdaptiveBuffer::FlushBuffer() {
2022-09-13 02:14:03 +02:00
this->offset = 0;
}
2022-09-18 03:39:40 +02:00
uint32_t AdaptiveBuffer::GetBufferLength() const {
2022-09-13 02:14:03 +02:00
return this->length;
}
2022-09-18 03:39:40 +02:00
uint32_t AdaptiveBuffer::GetBufferOffset() const {
2022-09-13 02:14:03 +02:00
return this->offset;
}
2022-10-06 03:37:22 +02:00
float *AdaptiveBuffer::GetBuffer() const {
2022-09-13 02:14:03 +02:00
return this->buffer;
}
2022-09-18 03:39:40 +02:00
uint32_t AdaptiveBuffer::GetChannels() const {
2022-09-13 02:14:03 +02:00
return this->channels;
}
2022-09-18 03:39:40 +02:00
void AdaptiveBuffer::PanFrames(float left, float right) {
2022-09-13 02:14:03 +02:00
if (this->buffer != nullptr && this->channels == 2) {
2022-10-11 00:36:38 +02:00
for (uint32_t i = 0; i < this->offset * this->channels; i++) {
2022-09-13 02:14:03 +02:00
if (i % 2 == 0) {
this->buffer[i] = this->buffer[i] * left;
} else {
this->buffer[i] = this->buffer[i] * right;
}
}
}
}
2022-09-18 03:39:40 +02:00
int AdaptiveBuffer::PopFrames(float *frames, uint32_t length) {
2022-09-13 02:14:03 +02:00
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) {
2022-10-11 00:36:38 +02:00
memmove(this->buffer, this->buffer + (length * this->channels), this->offset * this->channels * sizeof(float));
2022-09-13 02:14:03 +02:00
}
}
return 1;
}
2022-09-18 03:39:40 +02:00
int AdaptiveBuffer::PushFrames(const float *frames, uint32_t length) {
2022-09-13 02:14:03 +02:00
if (this->buffer == nullptr) {
return 0;
}
if (length != 0) {
if (this->offset + length > this->length) {
auto tmp = new float[(this->offset + length) * this->channels];
2022-09-16 03:16:58 +02:00
memcpy(tmp, this->buffer, this->offset * this->channels * sizeof(float));
2022-09-13 02:14:03 +02:00
delete this->buffer;
this->buffer = tmp;
this->length = this->offset + length;
}
2022-10-11 00:36:38 +02:00
memcpy(this->buffer + (this->offset * this->channels), frames, length * this->channels * sizeof(float));
2022-09-13 02:14:03 +02:00
this->offset = this->offset + length;
}
return 1;
}
2022-09-18 03:39:40 +02:00
int AdaptiveBuffer::PushZero(uint32_t length) {
2022-09-13 02:14:03 +02:00
if (this->buffer == nullptr) {
return 0;
}
if (this->offset + length > this->length) {
auto tmp = new float[(this->offset + length) * this->channels];
2022-09-16 03:16:58 +02:00
memcpy(tmp, this->buffer, this->offset * this->channels * sizeof(float));
2022-09-13 02:14:03 +02:00
delete this->buffer;
this->buffer = tmp;
this->length = this->offset + length;
}
2022-10-11 00:36:38 +02:00
memset(this->buffer + (this->offset * this->channels), 0, length * this->channels * sizeof(float));
2022-09-13 02:14:03 +02:00
this->offset = this->offset + length;
return 1;
}
2022-09-18 03:39:40 +02:00
void AdaptiveBuffer::ScaleFrames(float scale) {
2022-09-13 02:14:03 +02:00
if (this->buffer != nullptr) {
for (uint32_t i = 0; i < this->offset * this->channels; i++) {
this->buffer[i] = this->buffer[i] * scale;
}
}
}
2022-09-18 03:39:40 +02:00
void AdaptiveBuffer::SetBufferOffset(uint32_t offset) {
2022-09-13 02:14:03 +02:00
this->offset = offset;
}