C++ hackery

This commit is contained in:
Iscle 2023-05-12 03:50:26 +02:00
parent 3082bf6af7
commit 3decda5e82
17 changed files with 114 additions and 212 deletions

View File

@ -153,12 +153,12 @@ static int32_t ViPERInterfaceProcess(effect_handle_t self, audio_buffer_t *inBuf
static int handleSetConfig(ViPERContext *pContext, effect_config_t *newConfig) {
VIPER_LOGI("Checking input and output configuration ...");
VIPER_LOGI("Input buffer frame count: %d", newConfig->inputCfg.buffer.frameCount);
VIPER_LOGI("Input buffer frame count: %ld", newConfig->inputCfg.buffer.frameCount);
VIPER_LOGI("Input sampling rate: %d", newConfig->inputCfg.samplingRate);
VIPER_LOGI("Input channels: %d", newConfig->inputCfg.channels);
VIPER_LOGI("Input format: %d", newConfig->inputCfg.format);
VIPER_LOGI("Input access mode: %d", newConfig->inputCfg.accessMode);
VIPER_LOGI("Output buffer frame count: %d", newConfig->outputCfg.buffer.frameCount);
VIPER_LOGI("Output buffer frame count: %ld", newConfig->outputCfg.buffer.frameCount);
VIPER_LOGI("Output sampling rate: %d", newConfig->outputCfg.samplingRate);
VIPER_LOGI("Output channels: %d", newConfig->outputCfg.channels);
VIPER_LOGI("Output format: %d", newConfig->outputCfg.format);
@ -169,7 +169,7 @@ static int handleSetConfig(ViPERContext *pContext, effect_config_t *newConfig) {
pContext->buffer = nullptr;
if (newConfig->inputCfg.buffer.frameCount != newConfig->outputCfg.buffer.frameCount) {
VIPER_LOGE("ViPER4Android disabled, reason [in.FC = %d, out.FC = %d]",
VIPER_LOGE("ViPER4Android disabled, reason [in.FC = %ld, out.FC = %ld]",
newConfig->inputCfg.buffer.frameCount, newConfig->outputCfg.buffer.frameCount);
// pContext->disableReason = "Invalid frame count";
return 0;
@ -371,7 +371,7 @@ static int32_t ViPERInterfaceCommand(effect_handle_t self,
switch (cmdCode) {
case EFFECT_CMD_INIT: {
if (GET_REPLY_SIZE(replySize) != sizeof(int32_t) || pReplyData == nullptr) {
VIPER_LOGE("EFFECT_CMD_INIT called with invalid replySize = %d, pReplyData = %p, expected replySize = %d", GET_REPLY_SIZE(replySize), pReplyData, sizeof(int32_t));
VIPER_LOGE("EFFECT_CMD_INIT called with invalid replySize = %d, pReplyData = %p, expected replySize = %lu", GET_REPLY_SIZE(replySize), pReplyData, sizeof(int32_t));
return -EINVAL;
}
SET_INT32(pReplyData, 0);
@ -379,7 +379,7 @@ static int32_t ViPERInterfaceCommand(effect_handle_t self,
}
case EFFECT_CMD_SET_CONFIG: {
if (cmdSize < sizeof(effect_config_t) || pCmdData == nullptr || GET_REPLY_SIZE(replySize) != sizeof(int32_t) || pReplyData == nullptr) {
VIPER_LOGE("EFFECT_CMD_SET_CONFIG called with invalid cmdSize = %d, pCmdData = %p, replySize = %d, pReplyData = %p, expected cmdSize = %d, replySize = %d", cmdSize, pCmdData, GET_REPLY_SIZE(replySize), pReplyData, sizeof(effect_config_t), sizeof(int32_t));
VIPER_LOGE("EFFECT_CMD_SET_CONFIG called with invalid cmdSize = %d, pCmdData = %p, replySize = %d, pReplyData = %p, expected cmdSize = %lu, replySize = %lu", cmdSize, pCmdData, GET_REPLY_SIZE(replySize), pReplyData, sizeof(effect_config_t), sizeof(int32_t));
return -EINVAL;
}
SET_INT32(pReplyData, handleSetConfig(context, (effect_config_t *) pCmdData));
@ -387,7 +387,7 @@ static int32_t ViPERInterfaceCommand(effect_handle_t self,
}
case EFFECT_CMD_RESET: {
if (GET_REPLY_SIZE(replySize) != sizeof(int32_t) || pReplyData == nullptr) {
VIPER_LOGE("EFFECT_CMD_RESET called with invalid replySize = %d, pReplyData = %p, expected replySize = %d", GET_REPLY_SIZE(replySize), pReplyData, sizeof(int32_t));
VIPER_LOGE("EFFECT_CMD_RESET called with invalid replySize = %d, pReplyData = %p, expected replySize = %lu", GET_REPLY_SIZE(replySize), pReplyData, sizeof(int32_t));
return -EINVAL;
}
context->viper->ResetAllEffects();
@ -396,7 +396,7 @@ static int32_t ViPERInterfaceCommand(effect_handle_t self,
}
case EFFECT_CMD_ENABLE: {
if (GET_REPLY_SIZE(replySize) != sizeof(int32_t) || pReplyData == nullptr) {
VIPER_LOGE("EFFECT_CMD_ENABLE called with invalid replySize = %d, pReplyData = %p, expected replySize = %d", GET_REPLY_SIZE(replySize), pReplyData, sizeof(int32_t));
VIPER_LOGE("EFFECT_CMD_ENABLE called with invalid replySize = %d, pReplyData = %p, expected replySize = %lu", GET_REPLY_SIZE(replySize), pReplyData, sizeof(int32_t));
return -EINVAL;
}
context->viper->ResetAllEffects();
@ -406,7 +406,7 @@ static int32_t ViPERInterfaceCommand(effect_handle_t self,
}
case EFFECT_CMD_DISABLE: {
if (GET_REPLY_SIZE(replySize) != sizeof(int32_t) || pReplyData == nullptr) {
VIPER_LOGE("EFFECT_CMD_DISABLE called with invalid replySize = %d, pReplyData = %p, expected replySize = %d", GET_REPLY_SIZE(replySize), pReplyData, sizeof(int32_t));
VIPER_LOGE("EFFECT_CMD_DISABLE called with invalid replySize = %d, pReplyData = %p, expected replySize = %lu", GET_REPLY_SIZE(replySize), pReplyData, sizeof(int32_t));
return -EINVAL;
}
context->enabled = false;
@ -415,21 +415,21 @@ static int32_t ViPERInterfaceCommand(effect_handle_t self,
}
case EFFECT_CMD_SET_PARAM: {
if (cmdSize < sizeof(effect_param_t) || pCmdData == nullptr || GET_REPLY_SIZE(replySize) != sizeof(int32_t) || pReplyData == nullptr) {
VIPER_LOGE("EFFECT_CMD_SET_PARAM called with invalid cmdSize = %d, pCmdData = %p, replySize = %d, pReplyData = %p, expected cmdSize = %d, replySize = %d", cmdSize, pCmdData, GET_REPLY_SIZE(replySize), pReplyData, sizeof(effect_param_t), sizeof(int32_t));
VIPER_LOGE("EFFECT_CMD_SET_PARAM called with invalid cmdSize = %d, pCmdData = %p, replySize = %d, pReplyData = %p, expected cmdSize = %lu, replySize = %lu", cmdSize, pCmdData, GET_REPLY_SIZE(replySize), pReplyData, sizeof(effect_param_t), sizeof(int32_t));
return -EINVAL;
}
return handleSetParam(context, (effect_param_t *) pCmdData, pReplyData);
}
case EFFECT_CMD_GET_PARAM: {
if (cmdSize < sizeof(effect_param_t) || pCmdData == nullptr || GET_REPLY_SIZE(replySize) < sizeof(effect_param_t) || pReplyData == nullptr) {
VIPER_LOGE("EFFECT_CMD_GET_PARAM called with invalid cmdSize = %d, pCmdData = %p, replySize = %d, pReplyData = %p, expected cmdSize = %d, replySize = %d", cmdSize, pCmdData, GET_REPLY_SIZE(replySize), pReplyData, sizeof(effect_param_t), sizeof(effect_param_t));
VIPER_LOGE("EFFECT_CMD_GET_PARAM called with invalid cmdSize = %d, pCmdData = %p, replySize = %d, pReplyData = %p, expected cmdSize = %lu, replySize = %lu", cmdSize, pCmdData, GET_REPLY_SIZE(replySize), pReplyData, sizeof(effect_param_t), sizeof(effect_param_t));
return -EINVAL;
}
return handleGetParam(context, (effect_param_t *) pCmdData, (effect_param_t *) pReplyData, replySize);
}
case EFFECT_CMD_GET_CONFIG: {
if (GET_REPLY_SIZE(replySize) != sizeof(effect_config_t) || pReplyData == nullptr) {
VIPER_LOGE("EFFECT_CMD_GET_CONFIG called with invalid replySize = %d, pReplyData = %p, expected replySize = %d", GET_REPLY_SIZE(replySize), pReplyData, sizeof(effect_config_t));
VIPER_LOGE("EFFECT_CMD_GET_CONFIG called with invalid replySize = %d, pReplyData = %p, expected replySize = %lu", GET_REPLY_SIZE(replySize), pReplyData, sizeof(effect_config_t));
return -EINVAL;
}
*(effect_config_t *) pReplyData = context->config;

View File

@ -3,17 +3,9 @@
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 = std::vector<float>(channels * length);
this->offset = 0;
}
void AdaptiveBuffer::FlushBuffer() {
@ -28,8 +20,8 @@ uint32_t AdaptiveBuffer::GetBufferOffset() const {
return this->offset;
}
float *AdaptiveBuffer::GetBuffer() const {
return this->buffer;
float *AdaptiveBuffer::GetBuffer() {
return this->buffer.data();
}
uint32_t AdaptiveBuffer::GetChannels() const {
@ -37,7 +29,7 @@ uint32_t AdaptiveBuffer::GetChannels() const {
}
void AdaptiveBuffer::PanFrames(float left, float right) {
if (this->buffer != nullptr && this->channels == 2) {
if (this->channels == 2) {
for (uint32_t i = 0; i < this->offset * this->channels; i++) {
if (i % 2 == 0) {
this->buffer[i] = this->buffer[i] * left;
@ -49,15 +41,15 @@ void AdaptiveBuffer::PanFrames(float left, float right) {
}
int AdaptiveBuffer::PopFrames(float *frames, uint32_t length) {
if (this->buffer == nullptr || this->offset < length) {
if (this->offset < length) {
return 0;
}
if (length != 0) {
memcpy(frames, this->buffer, length * this->channels * sizeof(*frames));
memcpy(frames, this->buffer.data(), length * this->channels * sizeof(float));
this->offset = this->offset - length;
if (this->offset != 0) {
memmove(this->buffer, this->buffer + (length * this->channels), this->offset * this->channels * sizeof(float));
memmove(this->buffer.data(), this->buffer.data() + (length * this->channels), this->offset * this->channels * sizeof(float));
}
}
@ -65,20 +57,13 @@ int AdaptiveBuffer::PopFrames(float *frames, uint32_t length) {
}
int AdaptiveBuffer::PushFrames(const 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(float));
delete this->buffer;
this->buffer = tmp;
buffer.resize((this->offset + length) * this->channels);
this->length = this->offset + length;
}
memcpy(this->buffer + (this->offset * this->channels), frames, length * this->channels * sizeof(float));
memcpy(this->buffer.data() + (this->offset * this->channels), frames, length * this->channels * sizeof(float));
this->offset = this->offset + length;
}
@ -86,30 +71,21 @@ int AdaptiveBuffer::PushFrames(const float *frames, uint32_t length) {
}
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(float));
delete this->buffer;
this->buffer = tmp;
buffer.resize((this->offset + length) * this->channels);
this->length = this->offset + length;
}
memset(this->buffer + (this->offset * this->channels), 0, length * this->channels * sizeof(float));
memset(this->buffer.data() + (this->offset * this->channels), 0, length * this->channels * sizeof(float));
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) {

View File

@ -1,16 +1,16 @@
#pragma once
#include <cstdint>
#include <vector>
class AdaptiveBuffer {
public:
AdaptiveBuffer(uint32_t channels, uint32_t length);
~AdaptiveBuffer();
void FlushBuffer();
uint32_t GetBufferLength() const;
uint32_t GetBufferOffset() const;
float *GetBuffer() const;
float *GetBuffer();
uint32_t GetChannels() const;
void PanFrames(float left, float right);
int PopFrames(float *frames, uint32_t length);
@ -20,7 +20,7 @@ public:
void SetBufferOffset(uint32_t offset);
private:
float *buffer;
std::vector<float> buffer;
uint32_t length;
uint32_t offset;
uint32_t channels;

View File

@ -2,20 +2,11 @@
#include "FIR.h"
FIR::FIR() {
this->offsetBlock = nullptr;
this->coeffs = nullptr;
this->block = nullptr;
this->coeffsSize = 0;
this->blockLength = 0;
this->hasCoefficients = false;
}
FIR::~FIR() {
delete[] this->offsetBlock;
delete[] this->coeffs;
delete[] this->block;
}
void FIR::FilterSamples(float *samples, uint32_t size) {
this->FilterSamplesInterleaved(samples, size, 1);
}
@ -28,10 +19,10 @@ void FIR::FilterSamplesInterleaved(float *samples, uint32_t size, uint32_t chann
}
if (this->blockLength > size) {
memset(this->block + size, 0, (this->blockLength - size) * sizeof(float));
memset(this->block.data() + size, 0, (this->blockLength - size) * sizeof(float));
}
memcpy(this->offsetBlock + this->coeffsSize - 1, this->block, this->blockLength * sizeof(float));
memcpy(this->offsetBlock.data() + this->coeffsSize - 1, this->block.data(), this->blockLength * sizeof(float));
for (uint32_t i = 0; i < this->blockLength; i++) {
float sample = 0.0f;
@ -46,9 +37,9 @@ void FIR::FilterSamplesInterleaved(float *samples, uint32_t size, uint32_t chann
}
if (this->coeffsSize > 1) {
float *pfVar1 = this->block;
float *pfVar1 = this->block.data();
float *pfVar6 = pfVar1 + this->blockLength;
float *pfVar2 = this->offsetBlock + this->coeffsSize;
float *pfVar2 = this->offsetBlock.data() + this->coeffsSize;
do {
pfVar6 = pfVar6 - 1;
pfVar2[-2] = *pfVar6;
@ -64,18 +55,14 @@ uint32_t FIR::GetBlockLength() {
int FIR::LoadCoefficients(const float *coeffs, uint32_t coeffsSize, uint32_t blockLength) {
if (coeffs == nullptr || coeffsSize == 0 || blockLength == 0) return 0;
delete[] this->offsetBlock;
delete[] this->coeffs;
delete[] this->block;
this->offsetBlock = new float[coeffsSize + blockLength + 1];
this->coeffs = new float[coeffsSize];
this->block = new float[blockLength];
this->offsetBlock = std::vector<float>(coeffsSize + blockLength + 1);
this->coeffs = std::vector<float>(coeffsSize);
this->block = std::vector<float>(blockLength);
this->coeffsSize = coeffsSize;
this->blockLength = blockLength;
memcpy(this->coeffs, coeffs, coeffsSize * sizeof(float));
memcpy(this->coeffs.data(), coeffs, coeffsSize * sizeof(float));
Reset();
this->hasCoefficients = true;
@ -84,7 +71,7 @@ int FIR::LoadCoefficients(const float *coeffs, uint32_t coeffsSize, uint32_t blo
}
void FIR::Reset() {
if (this->offsetBlock != nullptr && this->coeffsSize + this->blockLength > 0) {
memset(this->offsetBlock, 0, (this->coeffsSize + this->blockLength + 1) * sizeof(float));
if (this->coeffsSize + this->blockLength > 0) {
memset(this->offsetBlock.data(), 0, (this->coeffsSize + this->blockLength + 1) * sizeof(float));
}
}

View File

@ -1,11 +1,11 @@
#pragma once
#include <cstdint>
#include <vector>
class FIR {
public:
FIR();
~FIR();
void FilterSamples(float *samples, uint32_t size);
void FilterSamplesInterleaved(float *samples, uint32_t size, uint32_t channels);
@ -14,9 +14,9 @@ public:
void Reset();
private:
float *offsetBlock;
float *coeffs;
float *block;
std::vector<float> offsetBlock;
std::vector<float> coeffs;
std::vector<float> block;
uint32_t coeffsSize;
uint32_t blockLength;
bool hasCoefficients;

View File

@ -1,8 +1,8 @@
#include "IIR_NOrder_BW_BP.h"
IIR_NOrder_BW_BP::IIR_NOrder_BW_BP(uint32_t order) {
this->lowpass = new IIR_1st[order];
this->highpass = new IIR_1st[order];
this->lowpass = std::vector<IIR_1st>(order);
this->highpass = std::vector<IIR_1st>(order);
this->order = order;
for (uint32_t x = 0; x < order; x++) {
@ -11,11 +11,6 @@ IIR_NOrder_BW_BP::IIR_NOrder_BW_BP(uint32_t order) {
}
}
IIR_NOrder_BW_BP::~IIR_NOrder_BW_BP() {
delete[] this->lowpass;
delete[] this->highpass;
}
void IIR_NOrder_BW_BP::Mute() {
for (uint32_t x = 0; x < this->order; x++) {
this->lowpass[x].Mute();

View File

@ -1,19 +1,18 @@
#pragma once
#include "IIR_1st.h"
#include <vector>
class IIR_NOrder_BW_BP {
public:
explicit IIR_NOrder_BW_BP(uint32_t order);
~IIR_NOrder_BW_BP();
void Mute();
void setBPF(float highCut, float lowCut, uint32_t samplingRate);
IIR_1st *lowpass;
IIR_1st *highpass;
std::vector<IIR_1st> lowpass;
std::vector<IIR_1st> highpass;
uint32_t order;
};

View File

@ -1,7 +1,7 @@
#include "IIR_NOrder_BW_LH.h"
IIR_NOrder_BW_LH::IIR_NOrder_BW_LH(uint32_t order) {
this->filters = new IIR_1st[order];
this->filters = std::vector<IIR_1st>(order);
this->order = order;
for (uint32_t x = 0; x < order; x++) {
@ -9,10 +9,6 @@ IIR_NOrder_BW_LH::IIR_NOrder_BW_LH(uint32_t order) {
}
}
IIR_NOrder_BW_LH::~IIR_NOrder_BW_LH() {
delete[] this->filters;
}
void IIR_NOrder_BW_LH::Mute() {
for (uint32_t x = 0; x < this->order; x++) {
this->filters[x].Mute();

View File

@ -1,19 +1,18 @@
#pragma once
#include "IIR_1st.h"
#include <vector>
class IIR_NOrder_BW_LH {
public:
explicit IIR_NOrder_BW_LH(uint32_t order);
~IIR_NOrder_BW_LH();
void Mute();
void setLPF(float frequency, uint32_t samplingRate);
void setHPF(float frequency, uint32_t samplingRate);
IIR_1st *filters;
std::vector<IIR_1st> filters;
uint32_t order;
};
@ -23,3 +22,10 @@ inline float do_filter_lh(IIR_NOrder_BW_LH *filt, float sample) {
}
return sample;
}
inline float do_filter_lh(IIR_NOrder_BW_LH& filt, float sample) {
for (uint32_t idx = 0; idx < filt.order; idx++) {
sample = do_filter(&filt.filters[idx], sample);
}
return sample;
}

View File

@ -1,22 +1,16 @@
#include "PassFilter.h"
#include "../constants.h"
PassFilter::PassFilter() {
this->filters[0] = new IIR_NOrder_BW_LH(3);
this->filters[1] = new IIR_NOrder_BW_LH(3);
this->filters[2] = new IIR_NOrder_BW_LH(1);
this->filters[3] = new IIR_NOrder_BW_LH(1);
PassFilter::PassFilter() : filters({
IIR_NOrder_BW_LH(3),
IIR_NOrder_BW_LH(3),
IIR_NOrder_BW_LH(1),
IIR_NOrder_BW_LH(1)
}) {
this->samplingRate = VIPER_DEFAULT_SAMPLING_RATE;
Reset();
}
PassFilter::~PassFilter() {
delete this->filters[0];
delete this->filters[1];
delete this->filters[2];
delete this->filters[3];
}
void PassFilter::ProcessFrames(float *buffer, uint32_t size) {
for (uint32_t x = 0; x < size; x++) {
float left = buffer[2 * x];
@ -40,15 +34,15 @@ void PassFilter::Reset() {
cutoff = 18000.0;
}
this->filters[0]->setLPF(cutoff, this->samplingRate);
this->filters[1]->setLPF(cutoff, this->samplingRate);
this->filters[2]->setHPF(10.0, this->samplingRate);
this->filters[3]->setHPF(10.0, this->samplingRate);
this->filters[0].setLPF(cutoff, this->samplingRate);
this->filters[1].setLPF(cutoff, this->samplingRate);
this->filters[2].setHPF(10.0, this->samplingRate);
this->filters[3].setHPF(10.0, this->samplingRate);
this->filters[0]->Mute();
this->filters[1]->Mute();
this->filters[2]->Mute();
this->filters[3]->Mute();
this->filters[0].Mute();
this->filters[1].Mute();
this->filters[2].Mute();
this->filters[3].Mute();
}
void PassFilter::SetSamplingRate(uint32_t samplingRate) {

View File

@ -2,17 +2,17 @@
#include <cstdint>
#include "IIR_NOrder_BW_LH.h"
#include <array>
class PassFilter {
public:
PassFilter();
~PassFilter();
void Reset();
void ProcessFrames(float *buffer, uint32_t size);
void SetSamplingRate(uint32_t samplingRate);
private:
IIR_NOrder_BW_LH *filters[4];
std::array<IIR_NOrder_BW_LH, 4> filters;
uint32_t samplingRate;
};

View File

@ -133,11 +133,8 @@ static const float POLYPHASE_COEFFICIENTS_OTHER[] = {
-0.032919
};
Polyphase::Polyphase(int param_1) {
Polyphase::Polyphase(int param_1) : waveBuffer1(2, 0x1000), waveBuffer2(2, 0x1000) {
this->samplingRate = VIPER_DEFAULT_SAMPLING_RATE;
this->waveBuffer1 = new WaveBuffer(2, 0x1000);
this->waveBuffer2 = new WaveBuffer(2, 0x1000);
this->buffer = new float[0x7e0];
if (param_1 == 2) {
this->fir1.LoadCoefficients(POLYPHASE_COEFFICIENTS_2, 63, 1008);
@ -148,31 +145,25 @@ Polyphase::Polyphase(int param_1) {
}
}
Polyphase::~Polyphase() {
delete this->waveBuffer1;
delete this->waveBuffer2;
delete[] this->buffer;
}
uint32_t Polyphase::GetLatency() {
return 63;
}
uint32_t Polyphase::Process(float *samples, uint32_t size) {
if (this->waveBuffer1->PushSamples(samples, size)) {
while (this->waveBuffer1->GetBufferOffset() >= 1008) {
if (this->waveBuffer1->PopSamples(this->buffer, 1008, false) == 1008) {
if (this->waveBuffer1.PushSamples(samples, size)) {
while (this->waveBuffer1.GetBufferOffset() >= 1008) {
if (this->waveBuffer1.PopSamples(this->buffer, 1008, false) == 1008) {
this->fir1.FilterSamplesInterleaved(this->buffer, 1008, 2);
this->fir2.FilterSamplesInterleaved(this->buffer + 1, 1008, 2);
this->waveBuffer2->PushSamples(this->buffer, 1008);
this->waveBuffer2.PushSamples(this->buffer, 1008);
}
}
if (this->waveBuffer2->GetBufferOffset() < size) {
if (this->waveBuffer2.GetBufferOffset() < size) {
return 0;
}
this->waveBuffer2->PopSamples(samples, size, true);
this->waveBuffer2.PopSamples(samples, size, true);
}
return size;
@ -181,8 +172,8 @@ uint32_t Polyphase::Process(float *samples, uint32_t size) {
void Polyphase::Reset() {
this->fir1.Reset();
this->fir2.Reset();
this->waveBuffer1->Reset();
this->waveBuffer2->Reset();
this->waveBuffer1.Reset();
this->waveBuffer2.Reset();
}
void Polyphase::SetSamplingRate(uint32_t samplingRate) {

View File

@ -7,7 +7,6 @@
class Polyphase {
public:
Polyphase(int param_1);
~Polyphase();
uint32_t GetLatency();
uint32_t Process(float *samples, uint32_t size);
@ -17,9 +16,9 @@ public:
private:
FIR fir1;
FIR fir2;
WaveBuffer *waveBuffer1;
WaveBuffer *waveBuffer2;
float *buffer;
WaveBuffer waveBuffer1;
WaveBuffer waveBuffer2;
float buffer[0x7e0];
uint32_t samplingRate;
};

View File

@ -2,28 +2,19 @@
#include <cstdlib>
TimeConstDelay::TimeConstDelay() {
this->samples = nullptr;
this->offset = 0;
this->sampleCount = 0;
}
TimeConstDelay::~TimeConstDelay() {
delete[] this->samples;
}
float TimeConstDelay::ProcessSample(float sample) {
if (this->samples != nullptr) {
float val = this->samples[this->offset];
this->samples[this->offset] = sample;
this->offset = (uint32_t) modf((float) this->offset + 1, (float *) &this->sampleCount); // TODO: check if this is correct
return val;
}
return 0.0;
}
void TimeConstDelay::SetParameters(uint32_t samplingRate, float delay) {
this->sampleCount = (uint32_t) ((float) samplingRate * delay);
delete[] this->samples;
this->samples = new float[this->sampleCount]();
this->samples.resize(this->sampleCount);
this->offset = 0;
}

View File

@ -1,17 +1,17 @@
#pragma once
#include <cstdint>
#include <vector>
class TimeConstDelay {
public:
TimeConstDelay();
~TimeConstDelay();
float ProcessSample(float sample);
void SetParameters(uint32_t samplingRate, float delay);
private:
float *samples;
std::vector<float> samples;
uint32_t offset;
uint32_t sampleCount;
};

View File

@ -3,13 +3,8 @@
WaveBuffer::WaveBuffer(uint32_t channels, uint32_t length) {
this->channels = channels;
this->size = length * channels;
this->index = 0;
this->buffer = new float[this->size];
}
WaveBuffer::~WaveBuffer() {
delete[] this->buffer;
this->buffer = std::vector<float>(length * channels);
}
uint32_t WaveBuffer::GetBufferOffset() {
@ -17,21 +12,21 @@ uint32_t WaveBuffer::GetBufferOffset() {
}
uint32_t WaveBuffer::GetBufferSize() {
return this->size / this->channels;
return this->buffer.size() / this->channels;
}
float *WaveBuffer::GetBuffer() {
return this->buffer;
return this->buffer.data();
}
uint32_t WaveBuffer::PopSamples(uint32_t size, bool resetIndex) {
if (this->buffer == nullptr || this->size == 0) {
if (this->buffer.empty()) {
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));
memmove(this->buffer.data(), this->buffer.data() + this->channels * size, this->index * sizeof(float));
return size;
}
@ -45,20 +40,20 @@ uint32_t WaveBuffer::PopSamples(uint32_t size, bool resetIndex) {
}
uint32_t WaveBuffer::PopSamples(float *dest, uint32_t size, bool resetIndex) {
if (this->buffer == nullptr || this->size == 0 || dest == nullptr) {
if (this->buffer.empty() || dest == nullptr) {
return 0;
}
if (this->channels * size <= this->index) {
memcpy(dest, this->buffer, this->channels * size * sizeof(float));
memcpy(dest, this->buffer.data(), this->channels * size * sizeof(float));
this->index -= this->channels * size;
memmove(this->buffer, this->buffer + this->channels * size, this->index * sizeof(float));
memmove(this->buffer.data(), this->buffer.data() + this->channels * size, this->index * sizeof(float));
return size;
}
if (resetIndex) {
uint32_t ret = this->index / this->channels;
memcpy(dest, this->buffer, this->index * sizeof(float));
memcpy(dest, this->buffer.data(), this->index * sizeof(float));
this->index = 0;
return ret;
}
@ -67,20 +62,12 @@ uint32_t WaveBuffer::PopSamples(float *dest, uint32_t size, bool resetIndex) {
}
int WaveBuffer::PushSamples(float *source, uint32_t size) {
if (this->buffer == nullptr) {
return 0;
}
if (size > 0) {
uint32_t requiredSize = this->channels * size + this->index;
if (this->size < requiredSize) {
auto *newBuffer = new float[requiredSize];
memcpy(newBuffer, this->buffer, this->index * sizeof(float));
delete[] this->buffer;
this->buffer = newBuffer;
this->size = requiredSize;
if (requiredSize > this->buffer.size()) {
this->buffer.resize(requiredSize);
}
memcpy(this->buffer + this->index, source, this->channels * size * sizeof(float));
memcpy(this->buffer.data() + this->index, source, this->channels * size * sizeof(float));
this->index += this->channels * size;
}
@ -88,20 +75,12 @@ int WaveBuffer::PushSamples(float *source, uint32_t size) {
}
int WaveBuffer::PushZeros(uint32_t size) {
if (this->buffer == nullptr) {
return 0;
}
if (size > 0) {
uint32_t requiredSize = this->channels * size + this->index;
if (this->size < requiredSize) {
auto *newBuffer = new float[requiredSize];
memcpy(newBuffer, this->buffer, this->index * sizeof(float));
delete[] this->buffer;
this->buffer = newBuffer;
this->size = requiredSize;
if (requiredSize > this->buffer.size()) {
this->buffer.resize(requiredSize);
}
memset(this->buffer + this->index, 0, this->channels * size * sizeof(float));
memset(this->buffer.data() + this->index, 0, this->channels * size * sizeof(float));
this->index += this->channels * size;
}
@ -109,26 +88,18 @@ int WaveBuffer::PushZeros(uint32_t size) {
}
float *WaveBuffer::PushZerosGetBuffer(uint32_t size) {
if (this->buffer == nullptr) {
return nullptr;
}
uint32_t oldIndex = this->index;
if (size > 0) {
uint32_t requiredSize = this->channels * size + this->index;
if (this->size < requiredSize) {
auto *newBuffer = new float[requiredSize];
memcpy(newBuffer, this->buffer, this->index * sizeof(float));
delete[] this->buffer;
this->buffer = newBuffer;
this->size = requiredSize;
if (requiredSize > this->buffer.size()) {
this->buffer.resize(requiredSize);
}
memset(this->buffer + this->index, 0, this->channels * size * sizeof(float));
memset(this->buffer.data() + this->index, 0, this->channels * size * sizeof(float));
this->index += this->channels * size;
}
return this->buffer + oldIndex;
return this->buffer.data() + oldIndex;
}
void WaveBuffer::Reset() {
@ -136,7 +107,7 @@ void WaveBuffer::Reset() {
}
void WaveBuffer::SetBufferOffset(uint32_t offset) {
uint32_t maxOffset = this->size / this->channels;
uint32_t maxOffset = this->buffer.size() / this->channels;
if (offset <= maxOffset) {
this->index = offset * this->channels;
}

View File

@ -1,14 +1,12 @@
#pragma once
#include <cstdint>
#include <vector>
class WaveBuffer {
public:
WaveBuffer(uint32_t channels, uint32_t length);
~WaveBuffer();
void Reset();
uint32_t GetBufferOffset();
uint32_t GetBufferSize();
@ -21,8 +19,7 @@ public:
void SetBufferOffset(uint32_t offset);
private:
float *buffer;
uint32_t size;
std::vector<float> buffer;
uint32_t index;
uint32_t channels;
};