This commit is contained in:
Iscle 2022-09-25 02:02:07 +02:00
parent e9857b11db
commit 96563a651d
4 changed files with 68 additions and 32 deletions

View File

@ -1,7 +1,23 @@
#include <cmath>
#include <cstring>
#include "IIRFilter.h" #include "IIRFilter.h"
#include "../constants.h"
IIRFilter::IIRFilter() { IIRFilter::IIRFilter(uint32_t bands) {
this->enable = false;
this->samplingRate = DEFAULT_SAMPLERATE;
if (bands == 10 || bands == 15 || bands == 25 || bands == 31) {
this->bands = bands;
this->coeffs.UpdateCoeffs(bands,this->samplingRate);
} else {
this->bands = 0;
}
for (auto &bandLevelWithQ : this->bandLevelsWithQ) {
bandLevelWithQ = 0.636;
}
this->Reset();
} }
IIRFilter::~IIRFilter() { IIRFilter::~IIRFilter() {
@ -13,17 +29,27 @@ void IIRFilter::Process(float *samples, uint32_t size) {
} }
void IIRFilter::Reset() { void IIRFilter::Reset() {
memset(this->buf,0,0x7c0);
// this->unknown3 = 1;
// this->unknown2 = 2;
// this->unknown4 = 0;
} }
void IIRFilter::SetBandLevel() { void IIRFilter::SetBandLevel(uint32_t band, float level) {
this->bandLevelsWithQ[band] = (float) (pow(10.0, level / 20.0) * 0.636);
} }
void IIRFilter::SetEnable(bool enable) { void IIRFilter::SetEnable(bool enable) {
this->enable = enable;
if (enable) {
Reset();
}
} }
void IIRFilter::SetSamplingRate(unsigned int i) { void IIRFilter::SetSamplingRate(uint32_t samplingRate) {
this->samplingRate = samplingRate;
if (this->bands != 0) {
this->coeffs->UpdateCoeffs(bands, samplingRate);
}
this->Reset();
} }

View File

@ -1,17 +1,27 @@
#pragma once #pragma once
#include <cstdint> #include <cstdint>
#include "../utils/MinPhaseIIRCoeffs.h"
class IIRFilter { class IIRFilter {
public: public:
IIRFilter(); IIRFilter(uint32_t bands);
~IIRFilter(); ~IIRFilter();
void Process(float *samples, uint32_t size); void Process(float *samples, uint32_t size);
void Reset(); void Reset();
void SetBandLevel(); void SetBandLevel(uint32_t band, float level);
void SetEnable(bool enable); void SetEnable(bool enable);
void SetSamplingRate(unsigned int i); void SetSamplingRate(uint32_t samplingRate);
private:
uint32_t bands;
uint32_t samplingRate;
bool enable;
MinPhaseIIRCoeffs coeffs;
float buf[496];
// 3 unknown
float bandLevelsWithQ[31];
}; };

View File

@ -2,7 +2,7 @@
#include "../constants.h" #include "../constants.h"
#include <cmath> #include <cmath>
static const float MIN_PHASE_IIR_COEFFS_FREQ_10[] = { static const float MIN_PHASE_IIR_COEFFS_FREQ_10BANDS[] = {
31.0, 31.0,
62.0, 62.0,
125.0, 125.0,
@ -15,7 +15,7 @@ static const float MIN_PHASE_IIR_COEFFS_FREQ_10[] = {
16000.0 16000.0
}; };
static const float MIN_PHASE_IIR_COEFFS_FREQ_15[] = { static const float MIN_PHASE_IIR_COEFFS_FREQ_15BANDS[] = {
25.0, 25.0,
40.0, 40.0,
63.0, 63.0,
@ -33,7 +33,7 @@ static const float MIN_PHASE_IIR_COEFFS_FREQ_15[] = {
16000.0 16000.0
}; };
static const float MIN_PHASE_IIR_COEFFS_FREQ_25[] = { static const float MIN_PHASE_IIR_COEFFS_FREQ_25BANDS[] = {
20.0, 20.0,
31.5, 31.5,
40.0, 40.0,
@ -61,7 +61,7 @@ static const float MIN_PHASE_IIR_COEFFS_FREQ_25[] = {
20000.0 20000.0
}; };
static const float MIN_PHASE_IIR_COEFFS_FREQ_31[] = { static const float MIN_PHASE_IIR_COEFFS_FREQ_31BANDS[] = {
20.0, 20.0,
25.0, 25.0,
31.5, 31.5,
@ -98,7 +98,7 @@ static const float MIN_PHASE_IIR_COEFFS_FREQ_31[] = {
MinPhaseIIRCoeffs::MinPhaseIIRCoeffs() { MinPhaseIIRCoeffs::MinPhaseIIRCoeffs() {
this->coeffs = nullptr; this->coeffs = nullptr;
this->samplingRate = DEFAULT_SAMPLERATE; this->samplingRate = DEFAULT_SAMPLERATE;
this->freqs = 0; this->bands = 0;
} }
MinPhaseIIRCoeffs::~MinPhaseIIRCoeffs() { MinPhaseIIRCoeffs::~MinPhaseIIRCoeffs() {
@ -116,15 +116,15 @@ float *MinPhaseIIRCoeffs::GetCoefficients() {
} }
float MinPhaseIIRCoeffs::GetIndexFrequency(uint32_t index) { float MinPhaseIIRCoeffs::GetIndexFrequency(uint32_t index) {
switch (this->freqs) { switch (this->bands) {
case 10: case 10:
return MIN_PHASE_IIR_COEFFS_FREQ_10[index]; return MIN_PHASE_IIR_COEFFS_FREQ_10BANDS[index];
case 15: case 15:
return MIN_PHASE_IIR_COEFFS_FREQ_15[index]; return MIN_PHASE_IIR_COEFFS_FREQ_15BANDS[index];
case 25: case 25:
return MIN_PHASE_IIR_COEFFS_FREQ_25[index]; return MIN_PHASE_IIR_COEFFS_FREQ_25BANDS[index];
case 31: case 31:
return MIN_PHASE_IIR_COEFFS_FREQ_31[index]; return MIN_PHASE_IIR_COEFFS_FREQ_31BANDS[index];
default: default:
return 0.0; return 0.0;
} }
@ -150,40 +150,40 @@ int MinPhaseIIRCoeffs::SolveRoot(double param_2, double param_3, double param_4,
return 0; return 0;
} }
int MinPhaseIIRCoeffs::UpdateCoeffs(uint32_t freqs, int samplingRate) { int MinPhaseIIRCoeffs::UpdateCoeffs(uint32_t bands, uint32_t samplingRate) {
if ((freqs != 10 && freqs != 15 && freqs != 25 && freqs != 31) || samplingRate != 44100) { if ((bands != 10 && bands != 15 && bands != 25 && bands != 31) || samplingRate != 44100) {
return 0; return 0;
} }
this->freqs = freqs; this->bands = bands;
this->samplingRate = samplingRate; this->samplingRate = samplingRate;
delete this->coeffs; delete this->coeffs;
this->coeffs = new float[freqs * 4](); this->coeffs = new float[bands * 4]();
const float *coeffsArray; const float *coeffsArray;
double tmp; double tmp;
switch (freqs) { switch (bands) {
case 10: case 10:
coeffsArray = MIN_PHASE_IIR_COEFFS_FREQ_10; coeffsArray = MIN_PHASE_IIR_COEFFS_FREQ_10BANDS;
tmp = 3.0 / 3.0; tmp = 3.0 / 3.0;
break; break;
case 15: case 15:
coeffsArray = MIN_PHASE_IIR_COEFFS_FREQ_15; coeffsArray = MIN_PHASE_IIR_COEFFS_FREQ_15BANDS;
tmp = 2.0 / 3.0; tmp = 2.0 / 3.0;
break; break;
case 25: case 25:
coeffsArray = MIN_PHASE_IIR_COEFFS_FREQ_25; coeffsArray = MIN_PHASE_IIR_COEFFS_FREQ_25BANDS;
tmp = 1.0 / 3.0; tmp = 1.0 / 3.0;
break; break;
case 31: case 31:
coeffsArray = MIN_PHASE_IIR_COEFFS_FREQ_31; coeffsArray = MIN_PHASE_IIR_COEFFS_FREQ_31BANDS;
tmp = 1.0 / 3.0; tmp = 1.0 / 3.0;
break; break;
} }
for (uint32_t i = 0; i < freqs; i++) { for (uint32_t i = 0; i < bands; i++) {
double ret1; double ret1;
double ret2; double ret2;
this->Find_F1_F2(coeffsArray[i], tmp, &ret1, &ret2); this->Find_F1_F2(coeffsArray[i], tmp, &ret1, &ret2);

View File

@ -11,10 +11,10 @@ public:
float *GetCoefficients(); float *GetCoefficients();
float GetIndexFrequency(uint32_t param_1); float GetIndexFrequency(uint32_t param_1);
int SolveRoot(double param_2, double param_3, double param_4, double *param_5); int SolveRoot(double param_2, double param_3, double param_4, double *param_5);
int UpdateCoeffs(uint32_t freqs, int samplingRate); int UpdateCoeffs(uint32_t bands, uint32_t samplingRate);
private: private:
float *coeffs; float *coeffs;
uint32_t samplingRate; uint32_t samplingRate;
uint32_t freqs; uint32_t bands;
}; };