mirror of
https://github.com/AndroidAudioMods/ViPERFX_RE.git
synced 2025-06-08 02:29:40 +08:00
C++ hackery
This commit is contained in:
parent
3082bf6af7
commit
3decda5e82
@ -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) {
|
static int handleSetConfig(ViPERContext *pContext, effect_config_t *newConfig) {
|
||||||
VIPER_LOGI("Checking input and output configuration ...");
|
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 sampling rate: %d", newConfig->inputCfg.samplingRate);
|
||||||
VIPER_LOGI("Input channels: %d", newConfig->inputCfg.channels);
|
VIPER_LOGI("Input channels: %d", newConfig->inputCfg.channels);
|
||||||
VIPER_LOGI("Input format: %d", newConfig->inputCfg.format);
|
VIPER_LOGI("Input format: %d", newConfig->inputCfg.format);
|
||||||
VIPER_LOGI("Input access mode: %d", newConfig->inputCfg.accessMode);
|
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 sampling rate: %d", newConfig->outputCfg.samplingRate);
|
||||||
VIPER_LOGI("Output channels: %d", newConfig->outputCfg.channels);
|
VIPER_LOGI("Output channels: %d", newConfig->outputCfg.channels);
|
||||||
VIPER_LOGI("Output format: %d", newConfig->outputCfg.format);
|
VIPER_LOGI("Output format: %d", newConfig->outputCfg.format);
|
||||||
@ -169,7 +169,7 @@ static int handleSetConfig(ViPERContext *pContext, effect_config_t *newConfig) {
|
|||||||
pContext->buffer = nullptr;
|
pContext->buffer = nullptr;
|
||||||
|
|
||||||
if (newConfig->inputCfg.buffer.frameCount != newConfig->outputCfg.buffer.frameCount) {
|
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);
|
newConfig->inputCfg.buffer.frameCount, newConfig->outputCfg.buffer.frameCount);
|
||||||
// pContext->disableReason = "Invalid frame count";
|
// pContext->disableReason = "Invalid frame count";
|
||||||
return 0;
|
return 0;
|
||||||
@ -371,7 +371,7 @@ static int32_t ViPERInterfaceCommand(effect_handle_t self,
|
|||||||
switch (cmdCode) {
|
switch (cmdCode) {
|
||||||
case EFFECT_CMD_INIT: {
|
case EFFECT_CMD_INIT: {
|
||||||
if (GET_REPLY_SIZE(replySize) != sizeof(int32_t) || pReplyData == nullptr) {
|
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;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
SET_INT32(pReplyData, 0);
|
SET_INT32(pReplyData, 0);
|
||||||
@ -379,7 +379,7 @@ static int32_t ViPERInterfaceCommand(effect_handle_t self,
|
|||||||
}
|
}
|
||||||
case EFFECT_CMD_SET_CONFIG: {
|
case EFFECT_CMD_SET_CONFIG: {
|
||||||
if (cmdSize < sizeof(effect_config_t) || pCmdData == nullptr || GET_REPLY_SIZE(replySize) != sizeof(int32_t) || pReplyData == nullptr) {
|
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;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
SET_INT32(pReplyData, handleSetConfig(context, (effect_config_t *) pCmdData));
|
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: {
|
case EFFECT_CMD_RESET: {
|
||||||
if (GET_REPLY_SIZE(replySize) != sizeof(int32_t) || pReplyData == nullptr) {
|
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;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
context->viper->ResetAllEffects();
|
context->viper->ResetAllEffects();
|
||||||
@ -396,7 +396,7 @@ static int32_t ViPERInterfaceCommand(effect_handle_t self,
|
|||||||
}
|
}
|
||||||
case EFFECT_CMD_ENABLE: {
|
case EFFECT_CMD_ENABLE: {
|
||||||
if (GET_REPLY_SIZE(replySize) != sizeof(int32_t) || pReplyData == nullptr) {
|
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;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
context->viper->ResetAllEffects();
|
context->viper->ResetAllEffects();
|
||||||
@ -406,7 +406,7 @@ static int32_t ViPERInterfaceCommand(effect_handle_t self,
|
|||||||
}
|
}
|
||||||
case EFFECT_CMD_DISABLE: {
|
case EFFECT_CMD_DISABLE: {
|
||||||
if (GET_REPLY_SIZE(replySize) != sizeof(int32_t) || pReplyData == nullptr) {
|
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;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
context->enabled = false;
|
context->enabled = false;
|
||||||
@ -415,21 +415,21 @@ static int32_t ViPERInterfaceCommand(effect_handle_t self,
|
|||||||
}
|
}
|
||||||
case EFFECT_CMD_SET_PARAM: {
|
case EFFECT_CMD_SET_PARAM: {
|
||||||
if (cmdSize < sizeof(effect_param_t) || pCmdData == nullptr || GET_REPLY_SIZE(replySize) != sizeof(int32_t) || pReplyData == nullptr) {
|
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 -EINVAL;
|
||||||
}
|
}
|
||||||
return handleSetParam(context, (effect_param_t *) pCmdData, pReplyData);
|
return handleSetParam(context, (effect_param_t *) pCmdData, pReplyData);
|
||||||
}
|
}
|
||||||
case EFFECT_CMD_GET_PARAM: {
|
case EFFECT_CMD_GET_PARAM: {
|
||||||
if (cmdSize < sizeof(effect_param_t) || pCmdData == nullptr || GET_REPLY_SIZE(replySize) < sizeof(effect_param_t) || pReplyData == nullptr) {
|
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 -EINVAL;
|
||||||
}
|
}
|
||||||
return handleGetParam(context, (effect_param_t *) pCmdData, (effect_param_t *) pReplyData, replySize);
|
return handleGetParam(context, (effect_param_t *) pCmdData, (effect_param_t *) pReplyData, replySize);
|
||||||
}
|
}
|
||||||
case EFFECT_CMD_GET_CONFIG: {
|
case EFFECT_CMD_GET_CONFIG: {
|
||||||
if (GET_REPLY_SIZE(replySize) != sizeof(effect_config_t) || pReplyData == nullptr) {
|
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;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
*(effect_config_t *) pReplyData = context->config;
|
*(effect_config_t *) pReplyData = context->config;
|
||||||
|
@ -3,17 +3,9 @@
|
|||||||
|
|
||||||
AdaptiveBuffer::AdaptiveBuffer(uint32_t channels, uint32_t length) {
|
AdaptiveBuffer::AdaptiveBuffer(uint32_t channels, uint32_t length) {
|
||||||
this->channels = channels;
|
this->channels = channels;
|
||||||
this->buffer = nullptr;
|
|
||||||
this->length = 0;
|
|
||||||
this->offset = 0;
|
|
||||||
if (channels != 0) {
|
|
||||||
this->buffer = new float[channels * length];
|
|
||||||
this->length = length;
|
this->length = length;
|
||||||
}
|
this->buffer = std::vector<float>(channels * length);
|
||||||
}
|
this->offset = 0;
|
||||||
|
|
||||||
AdaptiveBuffer::~AdaptiveBuffer() {
|
|
||||||
delete this->buffer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AdaptiveBuffer::FlushBuffer() {
|
void AdaptiveBuffer::FlushBuffer() {
|
||||||
@ -28,8 +20,8 @@ uint32_t AdaptiveBuffer::GetBufferOffset() const {
|
|||||||
return this->offset;
|
return this->offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
float *AdaptiveBuffer::GetBuffer() const {
|
float *AdaptiveBuffer::GetBuffer() {
|
||||||
return this->buffer;
|
return this->buffer.data();
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t AdaptiveBuffer::GetChannels() const {
|
uint32_t AdaptiveBuffer::GetChannels() const {
|
||||||
@ -37,7 +29,7 @@ uint32_t AdaptiveBuffer::GetChannels() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void AdaptiveBuffer::PanFrames(float left, float right) {
|
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++) {
|
for (uint32_t i = 0; i < this->offset * this->channels; i++) {
|
||||||
if (i % 2 == 0) {
|
if (i % 2 == 0) {
|
||||||
this->buffer[i] = this->buffer[i] * left;
|
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) {
|
int AdaptiveBuffer::PopFrames(float *frames, uint32_t length) {
|
||||||
if (this->buffer == nullptr || this->offset < length) {
|
if (this->offset < length) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (length != 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;
|
this->offset = this->offset - length;
|
||||||
if (this->offset != 0) {
|
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) {
|
int AdaptiveBuffer::PushFrames(const float *frames, uint32_t length) {
|
||||||
if (this->buffer == nullptr) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (length != 0) {
|
if (length != 0) {
|
||||||
if (this->offset + length > this->length) {
|
if (this->offset + length > this->length) {
|
||||||
auto tmp = new float[(this->offset + length) * this->channels];
|
buffer.resize((this->offset + length) * this->channels);
|
||||||
memcpy(tmp, this->buffer, this->offset * this->channels * sizeof(float));
|
|
||||||
delete this->buffer;
|
|
||||||
this->buffer = tmp;
|
|
||||||
this->length = this->offset + length;
|
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;
|
this->offset = this->offset + length;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,31 +71,22 @@ int AdaptiveBuffer::PushFrames(const float *frames, uint32_t length) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int AdaptiveBuffer::PushZero(uint32_t length) {
|
int AdaptiveBuffer::PushZero(uint32_t length) {
|
||||||
if (this->buffer == nullptr) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this->offset + length > this->length) {
|
if (this->offset + length > this->length) {
|
||||||
auto tmp = new float[(this->offset + length) * this->channels];
|
buffer.resize((this->offset + length) * this->channels);
|
||||||
memcpy(tmp, this->buffer, this->offset * this->channels * sizeof(float));
|
|
||||||
delete this->buffer;
|
|
||||||
this->buffer = tmp;
|
|
||||||
this->length = this->offset + length;
|
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;
|
this->offset = this->offset + length;
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AdaptiveBuffer::ScaleFrames(float scale) {
|
void AdaptiveBuffer::ScaleFrames(float scale) {
|
||||||
if (this->buffer != nullptr) {
|
|
||||||
for (uint32_t i = 0; i < this->offset * this->channels; i++) {
|
for (uint32_t i = 0; i < this->offset * this->channels; i++) {
|
||||||
this->buffer[i] = this->buffer[i] * scale;
|
this->buffer[i] = this->buffer[i] * scale;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void AdaptiveBuffer::SetBufferOffset(uint32_t offset) {
|
void AdaptiveBuffer::SetBufferOffset(uint32_t offset) {
|
||||||
this->offset = offset;
|
this->offset = offset;
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
class AdaptiveBuffer {
|
class AdaptiveBuffer {
|
||||||
public:
|
public:
|
||||||
AdaptiveBuffer(uint32_t channels, uint32_t length);
|
AdaptiveBuffer(uint32_t channels, uint32_t length);
|
||||||
~AdaptiveBuffer();
|
|
||||||
|
|
||||||
void FlushBuffer();
|
void FlushBuffer();
|
||||||
uint32_t GetBufferLength() const;
|
uint32_t GetBufferLength() const;
|
||||||
uint32_t GetBufferOffset() const;
|
uint32_t GetBufferOffset() const;
|
||||||
float *GetBuffer() const;
|
float *GetBuffer();
|
||||||
uint32_t GetChannels() const;
|
uint32_t GetChannels() const;
|
||||||
void PanFrames(float left, float right);
|
void PanFrames(float left, float right);
|
||||||
int PopFrames(float *frames, uint32_t length);
|
int PopFrames(float *frames, uint32_t length);
|
||||||
@ -20,7 +20,7 @@ public:
|
|||||||
void SetBufferOffset(uint32_t offset);
|
void SetBufferOffset(uint32_t offset);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
float *buffer;
|
std::vector<float> buffer;
|
||||||
uint32_t length;
|
uint32_t length;
|
||||||
uint32_t offset;
|
uint32_t offset;
|
||||||
uint32_t channels;
|
uint32_t channels;
|
||||||
|
@ -2,20 +2,11 @@
|
|||||||
#include "FIR.h"
|
#include "FIR.h"
|
||||||
|
|
||||||
FIR::FIR() {
|
FIR::FIR() {
|
||||||
this->offsetBlock = nullptr;
|
|
||||||
this->coeffs = nullptr;
|
|
||||||
this->block = nullptr;
|
|
||||||
this->coeffsSize = 0;
|
this->coeffsSize = 0;
|
||||||
this->blockLength = 0;
|
this->blockLength = 0;
|
||||||
this->hasCoefficients = false;
|
this->hasCoefficients = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
FIR::~FIR() {
|
|
||||||
delete[] this->offsetBlock;
|
|
||||||
delete[] this->coeffs;
|
|
||||||
delete[] this->block;
|
|
||||||
}
|
|
||||||
|
|
||||||
void FIR::FilterSamples(float *samples, uint32_t size) {
|
void FIR::FilterSamples(float *samples, uint32_t size) {
|
||||||
this->FilterSamplesInterleaved(samples, size, 1);
|
this->FilterSamplesInterleaved(samples, size, 1);
|
||||||
}
|
}
|
||||||
@ -28,10 +19,10 @@ void FIR::FilterSamplesInterleaved(float *samples, uint32_t size, uint32_t chann
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this->blockLength > size) {
|
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++) {
|
for (uint32_t i = 0; i < this->blockLength; i++) {
|
||||||
float sample = 0.0f;
|
float sample = 0.0f;
|
||||||
@ -46,9 +37,9 @@ void FIR::FilterSamplesInterleaved(float *samples, uint32_t size, uint32_t chann
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this->coeffsSize > 1) {
|
if (this->coeffsSize > 1) {
|
||||||
float *pfVar1 = this->block;
|
float *pfVar1 = this->block.data();
|
||||||
float *pfVar6 = pfVar1 + this->blockLength;
|
float *pfVar6 = pfVar1 + this->blockLength;
|
||||||
float *pfVar2 = this->offsetBlock + this->coeffsSize;
|
float *pfVar2 = this->offsetBlock.data() + this->coeffsSize;
|
||||||
do {
|
do {
|
||||||
pfVar6 = pfVar6 - 1;
|
pfVar6 = pfVar6 - 1;
|
||||||
pfVar2[-2] = *pfVar6;
|
pfVar2[-2] = *pfVar6;
|
||||||
@ -64,18 +55,14 @@ uint32_t FIR::GetBlockLength() {
|
|||||||
int FIR::LoadCoefficients(const float *coeffs, uint32_t coeffsSize, uint32_t blockLength) {
|
int FIR::LoadCoefficients(const float *coeffs, uint32_t coeffsSize, uint32_t blockLength) {
|
||||||
if (coeffs == nullptr || coeffsSize == 0 || blockLength == 0) return 0;
|
if (coeffs == nullptr || coeffsSize == 0 || blockLength == 0) return 0;
|
||||||
|
|
||||||
delete[] this->offsetBlock;
|
this->offsetBlock = std::vector<float>(coeffsSize + blockLength + 1);
|
||||||
delete[] this->coeffs;
|
this->coeffs = std::vector<float>(coeffsSize);
|
||||||
delete[] this->block;
|
this->block = std::vector<float>(blockLength);
|
||||||
|
|
||||||
this->offsetBlock = new float[coeffsSize + blockLength + 1];
|
|
||||||
this->coeffs = new float[coeffsSize];
|
|
||||||
this->block = new float[blockLength];
|
|
||||||
|
|
||||||
this->coeffsSize = coeffsSize;
|
this->coeffsSize = coeffsSize;
|
||||||
this->blockLength = blockLength;
|
this->blockLength = blockLength;
|
||||||
|
|
||||||
memcpy(this->coeffs, coeffs, coeffsSize * sizeof(float));
|
memcpy(this->coeffs.data(), coeffs, coeffsSize * sizeof(float));
|
||||||
|
|
||||||
Reset();
|
Reset();
|
||||||
this->hasCoefficients = true;
|
this->hasCoefficients = true;
|
||||||
@ -84,7 +71,7 @@ int FIR::LoadCoefficients(const float *coeffs, uint32_t coeffsSize, uint32_t blo
|
|||||||
}
|
}
|
||||||
|
|
||||||
void FIR::Reset() {
|
void FIR::Reset() {
|
||||||
if (this->offsetBlock != nullptr && this->coeffsSize + this->blockLength > 0) {
|
if (this->coeffsSize + this->blockLength > 0) {
|
||||||
memset(this->offsetBlock, 0, (this->coeffsSize + this->blockLength + 1) * sizeof(float));
|
memset(this->offsetBlock.data(), 0, (this->coeffsSize + this->blockLength + 1) * sizeof(float));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
class FIR {
|
class FIR {
|
||||||
public:
|
public:
|
||||||
FIR();
|
FIR();
|
||||||
~FIR();
|
|
||||||
|
|
||||||
void FilterSamples(float *samples, uint32_t size);
|
void FilterSamples(float *samples, uint32_t size);
|
||||||
void FilterSamplesInterleaved(float *samples, uint32_t size, uint32_t channels);
|
void FilterSamplesInterleaved(float *samples, uint32_t size, uint32_t channels);
|
||||||
@ -14,9 +14,9 @@ public:
|
|||||||
void Reset();
|
void Reset();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
float *offsetBlock;
|
std::vector<float> offsetBlock;
|
||||||
float *coeffs;
|
std::vector<float> coeffs;
|
||||||
float *block;
|
std::vector<float> block;
|
||||||
uint32_t coeffsSize;
|
uint32_t coeffsSize;
|
||||||
uint32_t blockLength;
|
uint32_t blockLength;
|
||||||
bool hasCoefficients;
|
bool hasCoefficients;
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
#include "IIR_NOrder_BW_BP.h"
|
#include "IIR_NOrder_BW_BP.h"
|
||||||
|
|
||||||
IIR_NOrder_BW_BP::IIR_NOrder_BW_BP(uint32_t order) {
|
IIR_NOrder_BW_BP::IIR_NOrder_BW_BP(uint32_t order) {
|
||||||
this->lowpass = new IIR_1st[order];
|
this->lowpass = std::vector<IIR_1st>(order);
|
||||||
this->highpass = new IIR_1st[order];
|
this->highpass = std::vector<IIR_1st>(order);
|
||||||
this->order = order;
|
this->order = order;
|
||||||
|
|
||||||
for (uint32_t x = 0; x < order; x++) {
|
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() {
|
void IIR_NOrder_BW_BP::Mute() {
|
||||||
for (uint32_t x = 0; x < this->order; x++) {
|
for (uint32_t x = 0; x < this->order; x++) {
|
||||||
this->lowpass[x].Mute();
|
this->lowpass[x].Mute();
|
||||||
|
@ -1,19 +1,18 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "IIR_1st.h"
|
#include "IIR_1st.h"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
class IIR_NOrder_BW_BP {
|
class IIR_NOrder_BW_BP {
|
||||||
public:
|
public:
|
||||||
explicit IIR_NOrder_BW_BP(uint32_t order);
|
explicit IIR_NOrder_BW_BP(uint32_t order);
|
||||||
|
|
||||||
~IIR_NOrder_BW_BP();
|
|
||||||
|
|
||||||
void Mute();
|
void Mute();
|
||||||
|
|
||||||
void setBPF(float highCut, float lowCut, uint32_t samplingRate);
|
void setBPF(float highCut, float lowCut, uint32_t samplingRate);
|
||||||
|
|
||||||
IIR_1st *lowpass;
|
std::vector<IIR_1st> lowpass;
|
||||||
IIR_1st *highpass;
|
std::vector<IIR_1st> highpass;
|
||||||
uint32_t order;
|
uint32_t order;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#include "IIR_NOrder_BW_LH.h"
|
#include "IIR_NOrder_BW_LH.h"
|
||||||
|
|
||||||
IIR_NOrder_BW_LH::IIR_NOrder_BW_LH(uint32_t order) {
|
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;
|
this->order = order;
|
||||||
|
|
||||||
for (uint32_t x = 0; x < order; x++) {
|
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() {
|
void IIR_NOrder_BW_LH::Mute() {
|
||||||
for (uint32_t x = 0; x < this->order; x++) {
|
for (uint32_t x = 0; x < this->order; x++) {
|
||||||
this->filters[x].Mute();
|
this->filters[x].Mute();
|
||||||
|
@ -1,19 +1,18 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "IIR_1st.h"
|
#include "IIR_1st.h"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
class IIR_NOrder_BW_LH {
|
class IIR_NOrder_BW_LH {
|
||||||
public:
|
public:
|
||||||
explicit IIR_NOrder_BW_LH(uint32_t order);
|
explicit IIR_NOrder_BW_LH(uint32_t order);
|
||||||
|
|
||||||
~IIR_NOrder_BW_LH();
|
|
||||||
|
|
||||||
void Mute();
|
void Mute();
|
||||||
void setLPF(float frequency, uint32_t samplingRate);
|
void setLPF(float frequency, uint32_t samplingRate);
|
||||||
void setHPF(float frequency, uint32_t samplingRate);
|
void setHPF(float frequency, uint32_t samplingRate);
|
||||||
|
|
||||||
|
|
||||||
IIR_1st *filters;
|
std::vector<IIR_1st> filters;
|
||||||
uint32_t order;
|
uint32_t order;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -23,3 +22,10 @@ inline float do_filter_lh(IIR_NOrder_BW_LH *filt, float sample) {
|
|||||||
}
|
}
|
||||||
return 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;
|
||||||
|
}
|
||||||
|
@ -1,22 +1,16 @@
|
|||||||
#include "PassFilter.h"
|
#include "PassFilter.h"
|
||||||
#include "../constants.h"
|
#include "../constants.h"
|
||||||
|
|
||||||
PassFilter::PassFilter() {
|
PassFilter::PassFilter() : filters({
|
||||||
this->filters[0] = new IIR_NOrder_BW_LH(3);
|
IIR_NOrder_BW_LH(3),
|
||||||
this->filters[1] = new IIR_NOrder_BW_LH(3);
|
IIR_NOrder_BW_LH(3),
|
||||||
this->filters[2] = new IIR_NOrder_BW_LH(1);
|
IIR_NOrder_BW_LH(1),
|
||||||
this->filters[3] = new IIR_NOrder_BW_LH(1);
|
IIR_NOrder_BW_LH(1)
|
||||||
|
}) {
|
||||||
this->samplingRate = VIPER_DEFAULT_SAMPLING_RATE;
|
this->samplingRate = VIPER_DEFAULT_SAMPLING_RATE;
|
||||||
Reset();
|
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) {
|
void PassFilter::ProcessFrames(float *buffer, uint32_t size) {
|
||||||
for (uint32_t x = 0; x < size; x++) {
|
for (uint32_t x = 0; x < size; x++) {
|
||||||
float left = buffer[2 * x];
|
float left = buffer[2 * x];
|
||||||
@ -40,15 +34,15 @@ void PassFilter::Reset() {
|
|||||||
cutoff = 18000.0;
|
cutoff = 18000.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->filters[0]->setLPF(cutoff, this->samplingRate);
|
this->filters[0].setLPF(cutoff, this->samplingRate);
|
||||||
this->filters[1]->setLPF(cutoff, this->samplingRate);
|
this->filters[1].setLPF(cutoff, this->samplingRate);
|
||||||
this->filters[2]->setHPF(10.0, this->samplingRate);
|
this->filters[2].setHPF(10.0, this->samplingRate);
|
||||||
this->filters[3]->setHPF(10.0, this->samplingRate);
|
this->filters[3].setHPF(10.0, this->samplingRate);
|
||||||
|
|
||||||
this->filters[0]->Mute();
|
this->filters[0].Mute();
|
||||||
this->filters[1]->Mute();
|
this->filters[1].Mute();
|
||||||
this->filters[2]->Mute();
|
this->filters[2].Mute();
|
||||||
this->filters[3]->Mute();
|
this->filters[3].Mute();
|
||||||
}
|
}
|
||||||
|
|
||||||
void PassFilter::SetSamplingRate(uint32_t samplingRate) {
|
void PassFilter::SetSamplingRate(uint32_t samplingRate) {
|
||||||
|
@ -2,17 +2,17 @@
|
|||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include "IIR_NOrder_BW_LH.h"
|
#include "IIR_NOrder_BW_LH.h"
|
||||||
|
#include <array>
|
||||||
|
|
||||||
class PassFilter {
|
class PassFilter {
|
||||||
public:
|
public:
|
||||||
PassFilter();
|
PassFilter();
|
||||||
~PassFilter();
|
|
||||||
|
|
||||||
void Reset();
|
void Reset();
|
||||||
void ProcessFrames(float *buffer, uint32_t size);
|
void ProcessFrames(float *buffer, uint32_t size);
|
||||||
void SetSamplingRate(uint32_t samplingRate);
|
void SetSamplingRate(uint32_t samplingRate);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
IIR_NOrder_BW_LH *filters[4];
|
std::array<IIR_NOrder_BW_LH, 4> filters;
|
||||||
uint32_t samplingRate;
|
uint32_t samplingRate;
|
||||||
};
|
};
|
||||||
|
@ -133,11 +133,8 @@ static const float POLYPHASE_COEFFICIENTS_OTHER[] = {
|
|||||||
-0.032919
|
-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->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) {
|
if (param_1 == 2) {
|
||||||
this->fir1.LoadCoefficients(POLYPHASE_COEFFICIENTS_2, 63, 1008);
|
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() {
|
uint32_t Polyphase::GetLatency() {
|
||||||
return 63;
|
return 63;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t Polyphase::Process(float *samples, uint32_t size) {
|
uint32_t Polyphase::Process(float *samples, uint32_t size) {
|
||||||
if (this->waveBuffer1->PushSamples(samples, size)) {
|
if (this->waveBuffer1.PushSamples(samples, size)) {
|
||||||
while (this->waveBuffer1->GetBufferOffset() >= 1008) {
|
while (this->waveBuffer1.GetBufferOffset() >= 1008) {
|
||||||
if (this->waveBuffer1->PopSamples(this->buffer, 1008, false) == 1008) {
|
if (this->waveBuffer1.PopSamples(this->buffer, 1008, false) == 1008) {
|
||||||
this->fir1.FilterSamplesInterleaved(this->buffer, 1008, 2);
|
this->fir1.FilterSamplesInterleaved(this->buffer, 1008, 2);
|
||||||
this->fir2.FilterSamplesInterleaved(this->buffer + 1, 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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->waveBuffer2->PopSamples(samples, size, true);
|
this->waveBuffer2.PopSamples(samples, size, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
return size;
|
return size;
|
||||||
@ -181,8 +172,8 @@ uint32_t Polyphase::Process(float *samples, uint32_t size) {
|
|||||||
void Polyphase::Reset() {
|
void Polyphase::Reset() {
|
||||||
this->fir1.Reset();
|
this->fir1.Reset();
|
||||||
this->fir2.Reset();
|
this->fir2.Reset();
|
||||||
this->waveBuffer1->Reset();
|
this->waveBuffer1.Reset();
|
||||||
this->waveBuffer2->Reset();
|
this->waveBuffer2.Reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Polyphase::SetSamplingRate(uint32_t samplingRate) {
|
void Polyphase::SetSamplingRate(uint32_t samplingRate) {
|
||||||
|
@ -7,7 +7,6 @@
|
|||||||
class Polyphase {
|
class Polyphase {
|
||||||
public:
|
public:
|
||||||
Polyphase(int param_1);
|
Polyphase(int param_1);
|
||||||
~Polyphase();
|
|
||||||
|
|
||||||
uint32_t GetLatency();
|
uint32_t GetLatency();
|
||||||
uint32_t Process(float *samples, uint32_t size);
|
uint32_t Process(float *samples, uint32_t size);
|
||||||
@ -17,9 +16,9 @@ public:
|
|||||||
private:
|
private:
|
||||||
FIR fir1;
|
FIR fir1;
|
||||||
FIR fir2;
|
FIR fir2;
|
||||||
WaveBuffer *waveBuffer1;
|
WaveBuffer waveBuffer1;
|
||||||
WaveBuffer *waveBuffer2;
|
WaveBuffer waveBuffer2;
|
||||||
float *buffer;
|
float buffer[0x7e0];
|
||||||
uint32_t samplingRate;
|
uint32_t samplingRate;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -2,28 +2,19 @@
|
|||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
|
||||||
TimeConstDelay::TimeConstDelay() {
|
TimeConstDelay::TimeConstDelay() {
|
||||||
this->samples = nullptr;
|
|
||||||
this->offset = 0;
|
this->offset = 0;
|
||||||
this->sampleCount = 0;
|
this->sampleCount = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
TimeConstDelay::~TimeConstDelay() {
|
|
||||||
delete[] this->samples;
|
|
||||||
}
|
|
||||||
|
|
||||||
float TimeConstDelay::ProcessSample(float sample) {
|
float TimeConstDelay::ProcessSample(float sample) {
|
||||||
if (this->samples != nullptr) {
|
|
||||||
float val = this->samples[this->offset];
|
float val = this->samples[this->offset];
|
||||||
this->samples[this->offset] = sample;
|
this->samples[this->offset] = sample;
|
||||||
this->offset = (uint32_t) modf((float) this->offset + 1, (float *) &this->sampleCount); // TODO: check if this is correct
|
this->offset = (uint32_t) modf((float) this->offset + 1, (float *) &this->sampleCount); // TODO: check if this is correct
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
return 0.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void TimeConstDelay::SetParameters(uint32_t samplingRate, float delay) {
|
void TimeConstDelay::SetParameters(uint32_t samplingRate, float delay) {
|
||||||
this->sampleCount = (uint32_t) ((float) samplingRate * delay);
|
this->sampleCount = (uint32_t) ((float) samplingRate * delay);
|
||||||
delete[] this->samples;
|
this->samples.resize(this->sampleCount);
|
||||||
this->samples = new float[this->sampleCount]();
|
|
||||||
this->offset = 0;
|
this->offset = 0;
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
class TimeConstDelay {
|
class TimeConstDelay {
|
||||||
public:
|
public:
|
||||||
TimeConstDelay();
|
TimeConstDelay();
|
||||||
~TimeConstDelay();
|
|
||||||
|
|
||||||
float ProcessSample(float sample);
|
float ProcessSample(float sample);
|
||||||
void SetParameters(uint32_t samplingRate, float delay);
|
void SetParameters(uint32_t samplingRate, float delay);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
float *samples;
|
std::vector<float> samples;
|
||||||
uint32_t offset;
|
uint32_t offset;
|
||||||
uint32_t sampleCount;
|
uint32_t sampleCount;
|
||||||
};
|
};
|
||||||
|
@ -3,13 +3,8 @@
|
|||||||
|
|
||||||
WaveBuffer::WaveBuffer(uint32_t channels, uint32_t length) {
|
WaveBuffer::WaveBuffer(uint32_t channels, uint32_t length) {
|
||||||
this->channels = channels;
|
this->channels = channels;
|
||||||
this->size = length * channels;
|
|
||||||
this->index = 0;
|
this->index = 0;
|
||||||
this->buffer = new float[this->size];
|
this->buffer = std::vector<float>(length * channels);
|
||||||
}
|
|
||||||
|
|
||||||
WaveBuffer::~WaveBuffer() {
|
|
||||||
delete[] this->buffer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t WaveBuffer::GetBufferOffset() {
|
uint32_t WaveBuffer::GetBufferOffset() {
|
||||||
@ -17,21 +12,21 @@ uint32_t WaveBuffer::GetBufferOffset() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
uint32_t WaveBuffer::GetBufferSize() {
|
uint32_t WaveBuffer::GetBufferSize() {
|
||||||
return this->size / this->channels;
|
return this->buffer.size() / this->channels;
|
||||||
}
|
}
|
||||||
|
|
||||||
float *WaveBuffer::GetBuffer() {
|
float *WaveBuffer::GetBuffer() {
|
||||||
return this->buffer;
|
return this->buffer.data();
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t WaveBuffer::PopSamples(uint32_t size, bool resetIndex) {
|
uint32_t WaveBuffer::PopSamples(uint32_t size, bool resetIndex) {
|
||||||
if (this->buffer == nullptr || this->size == 0) {
|
if (this->buffer.empty()) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this->channels * size <= this->index) {
|
if (this->channels * size <= this->index) {
|
||||||
this->index -= this->channels * size;
|
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;
|
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) {
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this->channels * size <= this->index) {
|
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;
|
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;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (resetIndex) {
|
if (resetIndex) {
|
||||||
uint32_t ret = this->index / this->channels;
|
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;
|
this->index = 0;
|
||||||
return ret;
|
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) {
|
int WaveBuffer::PushSamples(float *source, uint32_t size) {
|
||||||
if (this->buffer == nullptr) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (size > 0) {
|
if (size > 0) {
|
||||||
uint32_t requiredSize = this->channels * size + this->index;
|
uint32_t requiredSize = this->channels * size + this->index;
|
||||||
if (this->size < requiredSize) {
|
if (requiredSize > this->buffer.size()) {
|
||||||
auto *newBuffer = new float[requiredSize];
|
this->buffer.resize(requiredSize);
|
||||||
memcpy(newBuffer, this->buffer, this->index * sizeof(float));
|
|
||||||
delete[] this->buffer;
|
|
||||||
this->buffer = newBuffer;
|
|
||||||
this->size = 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;
|
this->index += this->channels * size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,20 +75,12 @@ int WaveBuffer::PushSamples(float *source, uint32_t size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int WaveBuffer::PushZeros(uint32_t size) {
|
int WaveBuffer::PushZeros(uint32_t size) {
|
||||||
if (this->buffer == nullptr) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (size > 0) {
|
if (size > 0) {
|
||||||
uint32_t requiredSize = this->channels * size + this->index;
|
uint32_t requiredSize = this->channels * size + this->index;
|
||||||
if (this->size < requiredSize) {
|
if (requiredSize > this->buffer.size()) {
|
||||||
auto *newBuffer = new float[requiredSize];
|
this->buffer.resize(requiredSize);
|
||||||
memcpy(newBuffer, this->buffer, this->index * sizeof(float));
|
|
||||||
delete[] this->buffer;
|
|
||||||
this->buffer = newBuffer;
|
|
||||||
this->size = 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;
|
this->index += this->channels * size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,26 +88,18 @@ int WaveBuffer::PushZeros(uint32_t size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
float *WaveBuffer::PushZerosGetBuffer(uint32_t size) {
|
float *WaveBuffer::PushZerosGetBuffer(uint32_t size) {
|
||||||
if (this->buffer == nullptr) {
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t oldIndex = this->index;
|
uint32_t oldIndex = this->index;
|
||||||
|
|
||||||
if (size > 0) {
|
if (size > 0) {
|
||||||
uint32_t requiredSize = this->channels * size + this->index;
|
uint32_t requiredSize = this->channels * size + this->index;
|
||||||
if (this->size < requiredSize) {
|
if (requiredSize > this->buffer.size()) {
|
||||||
auto *newBuffer = new float[requiredSize];
|
this->buffer.resize(requiredSize);
|
||||||
memcpy(newBuffer, this->buffer, this->index * sizeof(float));
|
|
||||||
delete[] this->buffer;
|
|
||||||
this->buffer = newBuffer;
|
|
||||||
this->size = 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;
|
this->index += this->channels * size;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this->buffer + oldIndex;
|
return this->buffer.data() + oldIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WaveBuffer::Reset() {
|
void WaveBuffer::Reset() {
|
||||||
@ -136,7 +107,7 @@ void WaveBuffer::Reset() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void WaveBuffer::SetBufferOffset(uint32_t offset) {
|
void WaveBuffer::SetBufferOffset(uint32_t offset) {
|
||||||
uint32_t maxOffset = this->size / this->channels;
|
uint32_t maxOffset = this->buffer.size() / this->channels;
|
||||||
if (offset <= maxOffset) {
|
if (offset <= maxOffset) {
|
||||||
this->index = offset * this->channels;
|
this->index = offset * this->channels;
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,12 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
class WaveBuffer {
|
class WaveBuffer {
|
||||||
public:
|
public:
|
||||||
WaveBuffer(uint32_t channels, uint32_t length);
|
WaveBuffer(uint32_t channels, uint32_t length);
|
||||||
|
|
||||||
~WaveBuffer();
|
|
||||||
|
|
||||||
void Reset();
|
void Reset();
|
||||||
uint32_t GetBufferOffset();
|
uint32_t GetBufferOffset();
|
||||||
uint32_t GetBufferSize();
|
uint32_t GetBufferSize();
|
||||||
@ -21,8 +19,7 @@ public:
|
|||||||
void SetBufferOffset(uint32_t offset);
|
void SetBufferOffset(uint32_t offset);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
float *buffer;
|
std::vector<float> buffer;
|
||||||
uint32_t size;
|
|
||||||
uint32_t index;
|
uint32_t index;
|
||||||
uint32_t channels;
|
uint32_t channels;
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user