From 5c0d69d97024841292c08c751551c2be14a3330b Mon Sep 17 00:00:00 2001 From: Iscle Date: Sun, 21 May 2023 18:46:20 +0200 Subject: [PATCH] Fix ViPER DDC, add architecture info --- src/ViPER4Android.h | 1 + src/ViperContext.cpp | 8 ++ src/viper/ViPER.cpp | 2 +- src/viper/constants.h | 15 +++ src/viper/effects/ViPERDDC.cpp | 215 ++++++++++++-------------------- src/viper/effects/ViPERDDC.h | 28 +++-- src/viper/utils/PConvSingle.cpp | 42 ++++--- src/viper/utils/PConvSingle.h | 8 +- 8 files changed, 149 insertions(+), 170 deletions(-) diff --git a/src/ViPER4Android.h b/src/ViPER4Android.h index 0d6ceda..4b0ee1e 100644 --- a/src/ViPER4Android.h +++ b/src/ViPER4Android.h @@ -19,6 +19,7 @@ extern "C" { #define PARAM_GET_DISABLE_REASON 8 #define PARAM_GET_DISABLE_REASON_MESSAGE 9 #define PARAM_GET_CONFIG 10 +#define PARAM_GET_ARCHITECTURE 11 // Param set #define PARAM_SET_UPDATE_STATUS 0x9002 diff --git a/src/ViperContext.cpp b/src/ViperContext.cpp index 7fab920..8ce67b1 100644 --- a/src/ViperContext.cpp +++ b/src/ViperContext.cpp @@ -4,6 +4,7 @@ #include #include "ViperContext.h" #include "log.h" +#include "viper/constants.h" #define SET(type, ptr, value) (*(type *) (ptr) = (value)) @@ -280,6 +281,13 @@ int32_t ViperContext::handleGetParam(effect_param_t *pCmdParam, effect_param_t * *pReplySize = sizeof(effect_param_t) + pReplyParam->psize + vOffset + pReplyParam->vsize; return 0; } + case PARAM_GET_ARCHITECTURE: { + pReplyParam->status = 0; + pReplyParam->vsize = sizeof(VIPER_ARCHITECTURE) - 1; // Exclude null terminator + memcpy(pReplyParam->data + vOffset, VIPER_ARCHITECTURE, pReplyParam->vsize); + *pReplySize = sizeof(effect_param_t) + pReplyParam->psize + vOffset + pReplyParam->vsize; + return 0; + } default: { return -EINVAL; } diff --git a/src/viper/ViPER.cpp b/src/viper/ViPER.cpp index 425dc34..e301f3b 100644 --- a/src/viper/ViPER.cpp +++ b/src/viper/ViPER.cpp @@ -228,7 +228,7 @@ void ViPER::DispatchCommand(int param, int val1, int val2, int val3, int val4, u break; } // 0x1000A case PARAM_DDC_COEFFICIENTS: { - this->viperDdc.SetCoeffs(arrSize, (float *) arr, (float *) (arr + arrSize * 4)); + this->viperDdc.SetCoeffs(arrSize, (float *) arr, (float *) (arr + arrSize * sizeof(float))); break; } // 0x1000B case PARAM_SPECTRUM_EXTENSION_ENABLE: { diff --git a/src/viper/constants.h b/src/viper/constants.h index 1650301..371ddd2 100644 --- a/src/viper/constants.h +++ b/src/viper/constants.h @@ -8,6 +8,21 @@ #include "../log.h" // TODO: Remove this dependency +#if defined(__arm__) +#define VIPER_ARCHITECTURE "ARM" +#elif defined(__aarch64__) +#define VIPER_ARCHITECTURE "ARM64" +#elif defined(__i386__) +#define VIPER_ARCHITECTURE "x86" +#elif defined(__x86_64__) +#define VIPER_ARCHITECTURE "x86_64" +#else +#error "Unknown architecture" +// Note from the developer: +// There's no architecture dependent code in ViPER4Android, this is just for debugging purposes. +// Feel free to add your architecture if it's not listed here. +#endif + #define VIPER_NAME "ViPER4Android" #define VIPER_AUTHORS "viper.WYF, Martmists, Iscle" #define VIPER_DEFAULT_SAMPLING_RATE 44100 \ No newline at end of file diff --git a/src/viper/effects/ViPERDDC.cpp b/src/viper/effects/ViPERDDC.cpp index 996732f..d59354c 100644 --- a/src/viper/effects/ViPERDDC.cpp +++ b/src/viper/effects/ViPERDDC.cpp @@ -1,41 +1,27 @@ #include "ViPERDDC.h" #include "../../log.h" +#include "../constants.h" #include -ViPERDDC::ViPERDDC() { - this->enable = false; - this->samplingRate = 44100; - this->setCoeffsOk = false; - this->arrSize = 0; - this->coeffsArr44100 = nullptr; - this->coeffsArr48000 = nullptr; - this->x2R = nullptr; - this->x2L = nullptr; - this->x1R = nullptr; - this->x1L = nullptr; - this->y2R = nullptr; - this->y2L = nullptr; - this->y1R = nullptr; - this->y1L = nullptr; -} - -ViPERDDC::~ViPERDDC() { - ReleaseResources(); -} +ViPERDDC::ViPERDDC() : + enable(false), + setCoeffsOk(false), + samplingRate(VIPER_DEFAULT_SAMPLING_RATE), + arrSize(0) {} void ViPERDDC::Process(float *samples, uint32_t size) { - if (!this->setCoeffsOk) return; + if (!this->setCoeffsOk || this->arrSize == 0) return; if (!this->enable) return; - float **coeffsArr; + std::vector> *coeffsArr; switch (this->samplingRate) { case 44100: { - coeffsArr = this->coeffsArr44100; + coeffsArr = &this->coeffsArr44100; break; } case 48000: { - coeffsArr = this->coeffsArr48000; + coeffsArr = &this->coeffsArr48000; break; } default: { @@ -45,145 +31,108 @@ void ViPERDDC::Process(float *samples, uint32_t size) { } for (uint32_t i = 0; i < size * 2; i += 2) { - if (this->arrSize == 0) { - samples[i] = 0.0; - samples[i + 1] = 0.0; - } else { - float sample = samples[i]; - for (uint32_t j = 0; j < this->arrSize; j++) { - float *coeffs = coeffsArr[j]; + float sampleL = samples[i]; + float sampleR = samples[i + 1]; - float b0 = coeffs[0]; - float b1 = coeffs[1]; - float b2 = coeffs[2]; - float a1 = coeffs[3]; - float a2 = coeffs[4]; + for (uint32_t j = 0; j < this->arrSize; j++) { + std::array *coeffs = &(*coeffsArr)[j]; - float out = - sample * b0 + - x1L[j] * b1 + - x2L[j] * b2 + - y1L[j] * a1 + - y2L[j] * a2; + float b0 = (*coeffs)[0]; + float b1 = (*coeffs)[1]; + float b2 = (*coeffs)[2]; + float a1 = (*coeffs)[3]; + float a2 = (*coeffs)[4]; - x2L[j] = x1L[j]; - x1L[j] = sample; - y2L[j] = y1L[j]; - y1L[j] = out; + float outL = + sampleL * b0 + + x1L[j] * b1 + + x2L[j] * b2 + + y1L[j] * a1 + + y2L[j] * a2; - sample = out; - } - samples[i] = sample; + x2L[j] = x1L[j]; + x1L[j] = sampleL; + y2L[j] = y1L[j]; + y1L[j] = outL; - sample = samples[i + 1]; - for (uint32_t j = 0; j < this->arrSize; j++) { - float *coeffs = coeffsArr[j]; + sampleL = outL; - float b0 = coeffs[0]; - float b1 = coeffs[1]; - float b2 = coeffs[2]; - float a1 = coeffs[3]; - float a2 = coeffs[4]; + float outR = + sampleR * b0 + + x1R[j] * b1 + + x2R[j] * b2 + + y1R[j] * a1 + + y2R[j] * a2; - float out = - sample * b0 + - x1R[j] * b1 + - x2R[j] * b2 + - y1R[j] * a1 + - y2R[j] * a2; + x2R[j] = x1R[j]; + x1R[j] = sampleR; + y2R[j] = y1R[j]; + y1R[j] = outR; - x2R[j] = x1R[j]; - x1R[j] = sample; - y2R[j] = y1R[j]; - y1R[j] = out; - - sample = out; - } - samples[i + 1] = sample; + sampleR = outR; } + + samples[i] = sampleL; + samples[i + 1] = sampleR; } } void ViPERDDC::ReleaseResources() { - for (uint32_t i = 0; i < this->arrSize; i++) { - delete[] this->coeffsArr44100[i]; - delete[] this->coeffsArr48000[i]; - } - - delete[] this->coeffsArr44100; - this->coeffsArr44100 = nullptr; - - delete[] this->coeffsArr48000; - this->coeffsArr48000 = nullptr; - - delete[] this->x1L; - this->x1L = nullptr; - - delete[] this->x1R; - this->x1R = nullptr; - - delete[] this->x2L; - this->x2L = nullptr; - - delete[] this->x2R; - this->x2R = nullptr; - - delete[] this->y1L; - this->y1L = nullptr; - - delete[] this->y1R; - this->y1R = nullptr; - - delete[] this->y2L; - this->y2L = nullptr; - - delete[] this->y2R; - this->y2R = nullptr; - this->setCoeffsOk = false; + + this->coeffsArr44100.resize(0); + this->coeffsArr48000.resize(0); + + this->x1L.resize(0); + this->x1R.resize(0); + this->x2L.resize(0); + this->x2R.resize(0); + this->y1L.resize(0); + this->y1R.resize(0); + this->y2L.resize(0); + this->y2R.resize(0); } void ViPERDDC::Reset() { if (!this->setCoeffsOk) return; if (this->arrSize == 0) return; - memset(this->x1L, 0, this->arrSize * 4); - memset(this->x1R, 0, this->arrSize * 4); + memset(this->x1L.data(), 0, this->arrSize * sizeof(float)); + memset(this->x1R.data(), 0, this->arrSize * sizeof(float)); } -void ViPERDDC::SetCoeffs(uint32_t param_1, float *param_2, float *param_3) { +void ViPERDDC::SetCoeffs(uint32_t newCoeffsSize, float *newCoeffs44100, float *newCoeffs48000) { ReleaseResources(); - if (param_1 == 0) return; + if (newCoeffsSize == 0) return; - this->arrSize = param_1 / 5; - this->coeffsArr44100 = new float *[this->arrSize](); - this->coeffsArr48000 = new float *[this->arrSize](); + this->arrSize = newCoeffsSize / 5; + this->coeffsArr44100.resize(this->arrSize); + this->coeffsArr48000.resize(this->arrSize); for (uint32_t i = 0; i < this->arrSize; i++) { - this->coeffsArr44100[i] = new float[5]; - this->coeffsArr44100[i][0] = param_2[i * 5]; - this->coeffsArr44100[i][1] = param_2[i * 5 + 1]; - this->coeffsArr44100[i][2] = param_2[i * 5 + 2]; - this->coeffsArr44100[i][3] = param_2[i * 5 + 3]; - this->coeffsArr44100[i][4] = param_2[i * 5 + 4]; + this->coeffsArr44100[i][0] = newCoeffs44100[i * 5]; + this->coeffsArr44100[i][1] = newCoeffs44100[i * 5 + 1]; + this->coeffsArr44100[i][2] = newCoeffs44100[i * 5 + 2]; + this->coeffsArr44100[i][3] = newCoeffs44100[i * 5 + 3]; + this->coeffsArr44100[i][4] = newCoeffs44100[i * 5 + 4]; - this->coeffsArr48000[i] = new float[5]; - this->coeffsArr48000[i][0] = param_3[i * 5]; - this->coeffsArr48000[i][1] = param_3[i * 5 + 1]; - this->coeffsArr48000[i][2] = param_3[i * 5 + 2]; - this->coeffsArr48000[i][3] = param_3[i * 5 + 3]; - this->coeffsArr48000[i][4] = param_3[i * 5 + 4]; + this->coeffsArr48000[i][0] = newCoeffs48000[i * 5]; + this->coeffsArr48000[i][1] = newCoeffs48000[i * 5 + 1]; + this->coeffsArr48000[i][2] = newCoeffs48000[i * 5 + 2]; + this->coeffsArr48000[i][3] = newCoeffs48000[i * 5 + 3]; + this->coeffsArr48000[i][4] = newCoeffs48000[i * 5 + 4]; } - this->x1L = new float[this->arrSize](); - this->x1R = new float[this->arrSize](); - this->x2L = new float[this->arrSize](); - this->x2R = new float[this->arrSize](); - this->y1L = new float[this->arrSize](); - this->y1R = new float[this->arrSize](); - this->y2L = new float[this->arrSize](); - this->y2R = new float[this->arrSize](); + this->x1L.resize(this->arrSize); + this->x1R.resize(this->arrSize); + this->x2L.resize(this->arrSize); + this->x2R.resize(this->arrSize); + this->y1L.resize(this->arrSize); + this->y1R.resize(this->arrSize); + this->y2L.resize(this->arrSize); + this->y2R.resize(this->arrSize); + this->setCoeffsOk = true; } diff --git a/src/viper/effects/ViPERDDC.h b/src/viper/effects/ViPERDDC.h index 7ab2a3f..65b9c54 100644 --- a/src/viper/effects/ViPERDDC.h +++ b/src/viper/effects/ViPERDDC.h @@ -1,16 +1,16 @@ #pragma once #include +#include +#include class ViPERDDC { public: ViPERDDC(); - ~ViPERDDC(); void Process(float *samples, uint32_t size); - void ReleaseResources(); void Reset(); - void SetCoeffs(uint32_t param_1, float *param_2, float *param_3); + void SetCoeffs(uint32_t newCoeffsSize, float *newCoeffs44100, float *newCoeffs48000); void SetEnable(bool enable); void SetSamplingRate(uint32_t samplingRate); @@ -19,16 +19,18 @@ private: bool setCoeffsOk; uint32_t samplingRate; uint32_t arrSize; - float **coeffsArr44100; - float **coeffsArr48000; - float *x1L; - float *x1R; - float *x2L; - float *x2R; - float *y1L; - float *y1R; - float *y2L; - float *y2R; + std::vector> coeffsArr44100; + std::vector> coeffsArr48000; + std::vector x1L; + std::vector x1R; + std::vector x2L; + std::vector x2R; + std::vector y1L; + std::vector y1R; + std::vector y2L; + std::vector y2R; + + void ReleaseResources(); }; diff --git a/src/viper/utils/PConvSingle.cpp b/src/viper/utils/PConvSingle.cpp index 9d18f84..ab1ceb2 100644 --- a/src/viper/utils/PConvSingle.cpp +++ b/src/viper/utils/PConvSingle.cpp @@ -22,7 +22,11 @@ void PConvSingle::ConvolveInterleaved(float *buffer, int channel) { } void PConvSingle::ConvSegment(float *buffer, bool interleaved, int channel) { - // TODO + if (!interleaved) { + + } else { + // TODO + } } int PConvSingle::GetFFTSize() { @@ -41,13 +45,13 @@ bool PConvSingle::InstanceUsable() { return this->instanceUsable; } -int PConvSingle::LoadKernel(float *buf, int param_2, int segmentSize) { - if (buf != nullptr && param_2 > 0 && segmentSize > 0 && segmentSize % 2 == 0) { +int PConvSingle::LoadKernel(const float *kernel, int kernelSize, int segmentSize) { + if (kernel != nullptr && kernelSize >= 2 && segmentSize >= 2 && (segmentSize & (segmentSize - 1)) == 0) { this->instanceUsable = false; ReleaseResources(); this->data = new PConvData(); //(PConvData *) malloc(0x140); // TODO: Sizeof this->segmentSize = segmentSize; - int n = ProcessKernel(buf, param_2, 1); + int n = ProcessKernel(kernel, kernelSize, 1); if (n != 0) { this->instanceUsable = true; return n; @@ -57,28 +61,28 @@ int PConvSingle::LoadKernel(float *buf, int param_2, int segmentSize) { return 0; } -int PConvSingle::LoadKernel(const float *param_2,float param_3,int param_4,int param_5) { -// if (buf != nullptr && param_5 > 0 && segmentSize > 0 && segmentSize % 2 == 0) { -// this->enable = false; -// ReleaseResources(); -//// this->data = new PConvData(); //(PConvData *) malloc(0x140); // TODO: Sizeof -// this->segmentSize = segmentSize; -// int n = ProcessKernel(1, param_2, param_4, param_5); -// if (n != 0) { -// this->enable = true; -// return n; -// } -// ReleaseResources(); -// } +int PConvSingle::LoadKernel(const float *kernel, float param_3, int kernelSize, int segmentSize) { + if (kernel != nullptr && kernelSize >= 2 && segmentSize >= 2 && (segmentSize & (segmentSize - 1)) == 0) { + this->instanceUsable = false; + ReleaseResources(); + this->data = new PConvData(); //(PConvData *) malloc(0x140); // TODO: Sizeof + this->segmentSize = segmentSize; + int n = ProcessKernel(kernel, param_3, kernelSize, 1); + if (n != 0) { + this->instanceUsable = true; + return n; + } + ReleaseResources(); + } return 0; } -int PConvSingle::ProcessKernel(float *param_1, int param_2, int param_3) { +int PConvSingle::ProcessKernel(const float *kernel, int kernelSize, int param_4) { // TODO return 0; } -int PConvSingle::ProcessKernel(int param_2, float *param_3, int param_4, int param_5) { +int PConvSingle::ProcessKernel(const float *kernel, float param_3, int kernelSize, int param_5) { // TODO return 0; } diff --git a/src/viper/utils/PConvSingle.h b/src/viper/utils/PConvSingle.h index 3e4610e..31ebc08 100644 --- a/src/viper/utils/PConvSingle.h +++ b/src/viper/utils/PConvSingle.h @@ -25,13 +25,13 @@ public: void ConvSegment(float *buffer, bool interleaved, int channel); - int LoadKernel(float *buf, int param_2, int segmentSize); + int LoadKernel(const float *kernel, int kernelSize, int segmentSize); - int LoadKernel(const float *param_2,float param_3,int param_4,int param_5); + int LoadKernel(const float *kernel, float param_3, int kernelSize, int segmentSize); - int ProcessKernel(float *param_1, int param_2, int param_3); + int ProcessKernel(const float *kernel, int kernelSize, int param_4); - int ProcessKernel(int param_2, float *param_3, int param_4, int param_5); + int ProcessKernel(const float *kernel, float param_3, int kernelSize, int param_5); void ReleaseResources();