From 6f78cc7ff3e3021c45c09717dfd4d6941383d32f Mon Sep 17 00:00:00 2001 From: Iscle Date: Wed, 9 Nov 2022 15:23:29 +0100 Subject: [PATCH] Update --- src/viper/ViPER.cpp | 7 +- src/viper/effects/ViPERDDC.cpp | 176 ++++++++++++++++++++++----------- src/viper/effects/ViPERDDC.h | 20 ++-- 3 files changed, 133 insertions(+), 70 deletions(-) diff --git a/src/viper/ViPER.cpp b/src/viper/ViPER.cpp index 99e1f37..7515f9a 100644 --- a/src/viper/ViPER.cpp +++ b/src/viper/ViPER.cpp @@ -281,8 +281,7 @@ void ViPER::DispatchCommand(int param, int val1, int val2, int val3, int val4, u break; } // 0x1000A case PARAM_DDC_COEFFICIENTS: { - // TODO: Finish - //this->viperDdc->SetCoeffs(); + this->viperDdc->SetCoeffs(arrSize, (float *) arr, (float *) (arr + arrSize * 4)); break; } // 0x1000B case PARAM_SPECTRUM_EXTENSION_ENABLE: { @@ -302,7 +301,7 @@ void ViPER::DispatchCommand(int param, int val1, int val2, int val3, int val4, u break; } // 0x1000F case PARAM_FIR_EQUALIZER_BAND_LEVEL: { - this->iirFilter->SetBandLevel(val1, (float) val2 / 100.0f); + this->iirFilter->SetBandLevel((uint32_t) val1, (float) val2 / 100.0f); break; } // 0x10010 case PARAM_FIELD_SURROUND_ENABLE: { @@ -398,7 +397,7 @@ void ViPER::DispatchCommand(int param, int val1, int val2, int val3, int val4, u break; } // 0x10027 case PARAM_FIDELITY_BASS_FREQUENCY: { - this->viperBass->SetSpeaker(val1); + this->viperBass->SetSpeaker((uint32_t) val1); break; } // 0x10028 case PARAM_FIDELITY_BASS_GAIN: { diff --git a/src/viper/effects/ViPERDDC.cpp b/src/viper/effects/ViPERDDC.cpp index 0fada82..996732f 100644 --- a/src/viper/effects/ViPERDDC.cpp +++ b/src/viper/effects/ViPERDDC.cpp @@ -7,16 +7,16 @@ ViPERDDC::ViPERDDC() { this->samplingRate = 44100; this->setCoeffsOk = false; this->arrSize = 0; - this->arrPtr44100 = nullptr; - this->arrPtr48000 = nullptr; - this->arr4 = nullptr; - this->arr3 = nullptr; - this->arr2 = nullptr; - this->arr1 = nullptr; - this->arr8 = nullptr; - this->arr7 = nullptr; - this->arr6 = nullptr; - this->arr5 = nullptr; + 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() { @@ -27,54 +27,118 @@ void ViPERDDC::Process(float *samples, uint32_t size) { if (!this->setCoeffsOk) return; if (!this->enable) return; + float **coeffsArr; + switch (this->samplingRate) { case 44100: { + coeffsArr = this->coeffsArr44100; break; } case 48000: { + coeffsArr = this->coeffsArr48000; break; } default: { VIPER_LOGD("ViPERDDC::Process() -> Invalid sampling rate: %d", this->samplingRate); + return; + } + } + + 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 b0 = coeffs[0]; + float b1 = coeffs[1]; + float b2 = coeffs[2]; + float a1 = coeffs[3]; + float a2 = coeffs[4]; + + float out = + sample * b0 + + x1L[j] * b1 + + x2L[j] * b2 + + y1L[j] * a1 + + y2L[j] * a2; + + x2L[j] = x1L[j]; + x1L[j] = sample; + y2L[j] = y1L[j]; + y1L[j] = out; + + sample = out; + } + samples[i] = sample; + + sample = samples[i + 1]; + for (uint32_t j = 0; j < this->arrSize; j++) { + float *coeffs = coeffsArr[j]; + + float b0 = coeffs[0]; + float b1 = coeffs[1]; + float b2 = coeffs[2]; + float a1 = coeffs[3]; + float a2 = coeffs[4]; + + float out = + sample * b0 + + x1R[j] * b1 + + x2R[j] * b2 + + y1R[j] * a1 + + y2R[j] * a2; + + x2R[j] = x1R[j]; + x1R[j] = sample; + y2R[j] = y1R[j]; + y1R[j] = out; + + sample = out; + } + samples[i + 1] = sample; } } } void ViPERDDC::ReleaseResources() { for (uint32_t i = 0; i < this->arrSize; i++) { - delete[] this->arrPtr44100[i]; - delete[] this->arrPtr48000[i]; + delete[] this->coeffsArr44100[i]; + delete[] this->coeffsArr48000[i]; } - delete[] this->arrPtr44100; - this->arrPtr44100 = nullptr; + delete[] this->coeffsArr44100; + this->coeffsArr44100 = nullptr; - delete[] this->arrPtr48000; - this->arrPtr48000 = nullptr; + delete[] this->coeffsArr48000; + this->coeffsArr48000 = nullptr; - delete[] this->arr1; - this->arr1 = nullptr; + delete[] this->x1L; + this->x1L = nullptr; - delete[] this->arr2; - this->arr2 = nullptr; + delete[] this->x1R; + this->x1R = nullptr; - delete[] this->arr3; - this->arr3 = nullptr; + delete[] this->x2L; + this->x2L = nullptr; - delete[] this->arr4; - this->arr4 = nullptr; + delete[] this->x2R; + this->x2R = nullptr; - delete[] this->arr5; - this->arr5 = nullptr; + delete[] this->y1L; + this->y1L = nullptr; - delete[] this->arr6; - this->arr6 = nullptr; + delete[] this->y1R; + this->y1R = nullptr; - delete[] this->arr7; - this->arr7 = nullptr; + delete[] this->y2L; + this->y2L = nullptr; - delete[] this->arr8; - this->arr8 = nullptr; + delete[] this->y2R; + this->y2R = nullptr; this->setCoeffsOk = false; } @@ -83,8 +147,8 @@ void ViPERDDC::Reset() { if (!this->setCoeffsOk) return; if (this->arrSize == 0) return; - memset(this->arr1, 0, this->arrSize * 4); - memset(this->arr2, 0, this->arrSize * 4); + memset(this->x1L, 0, this->arrSize * 4); + memset(this->x1R, 0, this->arrSize * 4); } void ViPERDDC::SetCoeffs(uint32_t param_1, float *param_2, float *param_3) { @@ -93,33 +157,33 @@ void ViPERDDC::SetCoeffs(uint32_t param_1, float *param_2, float *param_3) { if (param_1 == 0) return; this->arrSize = param_1 / 5; - this->arrPtr44100 = new float *[this->arrSize](); - this->arrPtr48000 = new float *[this->arrSize](); + this->coeffsArr44100 = new float *[this->arrSize](); + this->coeffsArr48000 = new float *[this->arrSize](); for (uint32_t i = 0; i < this->arrSize; i++) { - this->arrPtr44100[i] = new float[5]; - this->arrPtr44100[i][0] = param_2[i * 5]; - this->arrPtr44100[i][1] = param_2[i * 5 + 1]; - this->arrPtr44100[i][2] = param_2[i * 5 + 2]; - this->arrPtr44100[i][3] = param_2[i * 5 + 3]; - this->arrPtr44100[i][4] = param_2[i * 5 + 4]; + 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->arrPtr48000[i] = new float[5]; - this->arrPtr48000[i][0] = param_3[i * 5]; - this->arrPtr48000[i][1] = param_3[i * 5 + 1]; - this->arrPtr48000[i][2] = param_3[i * 5 + 2]; - this->arrPtr48000[i][3] = param_3[i * 5 + 3]; - this->arrPtr48000[i][4] = param_3[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->arr1 = new float[this->arrSize](); - this->arr2 = new float[this->arrSize](); - this->arr3 = new float[this->arrSize](); - this->arr4 = new float[this->arrSize](); - this->arr5 = new float[this->arrSize](); - this->arr6 = new float[this->arrSize](); - this->arr7 = new float[this->arrSize](); - this->arr8 = new float[this->arrSize](); + 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->setCoeffsOk = true; } diff --git a/src/viper/effects/ViPERDDC.h b/src/viper/effects/ViPERDDC.h index 31ecb29..7ab2a3f 100644 --- a/src/viper/effects/ViPERDDC.h +++ b/src/viper/effects/ViPERDDC.h @@ -19,16 +19,16 @@ private: bool setCoeffsOk; uint32_t samplingRate; uint32_t arrSize; - float **arrPtr44100; - float **arrPtr48000; - float *arr1; - float *arr2; - float *arr3; - float *arr4; - float *arr5; - float *arr6; - float *arr7; - float *arr8; + float **coeffsArr44100; + float **coeffsArr48000; + float *x1L; + float *x1R; + float *x2L; + float *x2R; + float *y1L; + float *y1R; + float *y2L; + float *y2R; };