This commit is contained in:
Iscle 2022-10-11 03:07:11 +02:00
parent 39aef22062
commit f0e899ab3a
9 changed files with 77 additions and 67 deletions

View File

@ -23,28 +23,33 @@ IIRFilter::IIRFilter(uint32_t bands) {
void IIRFilter::Process(float *samples, uint32_t size) {
if (!this->enable) return;
float *coeffs = this->minPhaseIirCoeffs.GetCoefficients();
double *coeffs = this->minPhaseIirCoeffs.GetCoefficients();
if (coeffs == nullptr || size == 0) return;
for (uint32_t i = 0; i < size; i++) {
for (uint32_t j = 0; j < 2; j++) {
float sample = samples[i * 2 + j];
float tmp = 0.0;
double sample = samples[i * 2 + j];
double accumulated = 0.0;
for (uint32_t k = 0; k < this->bands; k++) {
uint32_t bufIdx = this->unknown2 + j * 8 + k * 16;
this->buf[bufIdx] = sample;
float coeff1 = coeffs[k * 4];
float coeff2 = coeffs[k * 4 + 1];
float coeff3 = coeffs[k * 4 + 2];
double coeff1 = coeffs[k * 4];
double coeff2 = coeffs[k * 4 + 1];
double coeff3 = coeffs[k * 4 + 2];
float tmp2 = ((coeff3 * this->buf[bufIdx + ((this->unknown3 + 3) - this->unknown2)] + (sample - this->buf[bufIdx + (unknown4 - unknown2)]) * coeff2) - coeff1 * this->buf[bufIdx + ((unknown4 - unknown2) + 3)]);
double a = coeff3 * this->buf[bufIdx + ((this->unknown3 + 3) - this->unknown2)];
double b = coeff2 * (sample - this->buf[bufIdx + (unknown4 - unknown2)]);
double c = coeff1 * this->buf[bufIdx + ((unknown4 - unknown2) + 3)];
this->buf[bufIdx + 3] = tmp2;
tmp += tmp2 * this->bandLevelsWithQ[k];
double tmp = (a + b) - c;
this->buf[bufIdx + 3] = tmp;
accumulated += tmp * this->bandLevelsWithQ[k];
}
samples[2 * i + j] = tmp;
samples[i * 2 + j] = (float) accumulated;
}
this->unknown2 = (this->unknown2 + 1) % 3;
@ -54,9 +59,9 @@ void IIRFilter::Process(float *samples, uint32_t size) {
}
void IIRFilter::Reset() {
memset(this->buf,0,sizeof(buf)); // size should be 0x7c0
this->unknown3 = 1;
memset(this->buf,0,sizeof(buf));
this->unknown2 = 2;
this->unknown3 = 1;
this->unknown4 = 0;
}

View File

@ -18,7 +18,7 @@ private:
uint32_t samplingRate;
bool enable;
MinPhaseIIRCoeffs minPhaseIirCoeffs;
float buf[496];
double buf[496];
uint32_t unknown2;
uint32_t unknown3;
uint32_t unknown4;

View File

@ -37,16 +37,25 @@ void FIR::FilterSamplesInterleaved(float *samples, uint32_t size, uint32_t chann
float sample = 0.0f;
for (uint32_t j = 0; j < this->coeffsSize; j++) {
sample += this->coeffs[j] * this->offsetBlock[this->coeffsSize + i - 1 - j];
sample += this->coeffs[j] * this->offsetBlock[this->coeffsSize + i - j - 1];
}
if (i < size) {
samples[channels * i] = sample;
samples[i * channels] = sample;
}
}
if (this->coeffsSize > 1) {
memcpy(this->offsetBlock + this->coeffsSize - 2, this->block + this->blockLength - 1, this->blockLength - (this->coeffsSize - 1) * sizeof(float));
// TODO: Replace this with memcpy
float *pfVar1 = this->block;
float *pfVar6 = pfVar1 + blockLength;
float *pfVar2 = this->offsetBlock + this->coeffsSize;
do {
pfVar6 = pfVar6 + -1;
pfVar2[-2] = *pfVar6;
pfVar2 = pfVar2 + -1;
} while (pfVar6 != pfVar1 + blockLength + (1 - this->coeffsSize));
//memcpy(this->offsetBlock + this->coeffsSize - 2 - (this->coeffsSize - 1), this->block + this->blockLength - 1 - (this->coeffsSize - 1), (this->coeffsSize - 1) * sizeof(float));
}
}
@ -70,7 +79,7 @@ int FIR::LoadCoefficients(const float *coeffs, uint32_t coeffsSize, uint32_t blo
memcpy(this->coeffs, coeffs, coeffsSize * sizeof(float));
this->Reset();
Reset();
this->hasCoefficients = true;
return 1;
@ -78,6 +87,6 @@ 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) * sizeof(float));
memset(this->offsetBlock, 0, (this->coeffsSize + this->blockLength + 1) * sizeof(float));
}
}

View File

@ -111,7 +111,7 @@ void MinPhaseIIRCoeffs::Find_F1_F2(double param_2, double param_3, double *param
*param_4 = param_2 * x;
}
float *MinPhaseIIRCoeffs::GetCoefficients() {
double *MinPhaseIIRCoeffs::GetCoefficients() {
return this->coeffs;
}
@ -159,7 +159,7 @@ int MinPhaseIIRCoeffs::UpdateCoeffs(uint32_t bands, uint32_t samplingRate) {
this->samplingRate = samplingRate;
delete[] this->coeffs;
this->coeffs = new float[bands * 4](); // TODO: Check this array size
this->coeffs = new double[bands * 4](); // TODO: Check this array size, original type: float
const float *coeffsArray;
double tmp;
@ -208,9 +208,9 @@ int MinPhaseIIRCoeffs::UpdateCoeffs(uint32_t bands, uint32_t samplingRate) {
double f = ((pow(cosX, 2.0) * 0.125 - cosX * cosY * 0.25) + 0.125) - c * 0.25;
if (SolveRoot(d, e, f, &ret1) == 0) {
this->coeffs[4 * i] = (float) (ret1 * 2.0);
this->coeffs[4 * i + 1] = (float) (((0.5 - ret1) * 0.5) * 2.0);
this->coeffs[4 * i + 2] = (float) (((ret1 + 0.5) * cosX) * 2.0);
this->coeffs[4 * i] = ret1 * 2.0;
this->coeffs[4 * i + 1] = ((0.5 - ret1) * 0.5) * 2.0;
this->coeffs[4 * i + 2] = ((ret1 + 0.5) * cosX) * 2.0;
}
}

View File

@ -8,13 +8,13 @@ public:
~MinPhaseIIRCoeffs();
void Find_F1_F2(double param_2, double param_3, double *param_4, double *param_5);
float *GetCoefficients();
double *GetCoefficients();
float GetIndexFrequency(uint32_t param_1);
int SolveRoot(double param_2, double param_3, double param_4, double *param_5);
int UpdateCoeffs(uint32_t bands, uint32_t samplingRate);
private:
float *coeffs;
double *coeffs;
uint32_t samplingRate;
uint32_t bands;
};

View File

@ -133,26 +133,22 @@ static const float POLYPHASE_COEFFICIENTS_OTHER[] = {
-0.032919
};
Polyphase::Polyphase(int unknown1) {
Polyphase::Polyphase(int param_1) {
this->samplingRate = DEFAULT_SAMPLERATE;
this->fir1 = new FIR();
this->fir2 = new FIR();
this->waveBuffer1 = new WaveBuffer(2, 0x1000);
this->waveBuffer2 = new WaveBuffer(2, 0x1000);
this->buffer = new float[0x7e0];
if (unknown1 == 2) {
this->fir1->LoadCoefficients(POLYPHASE_COEFFICIENTS_2, sizeof(POLYPHASE_COEFFICIENTS_2) / sizeof(float), 1008);
this->fir2->LoadCoefficients(POLYPHASE_COEFFICIENTS_2, sizeof(POLYPHASE_COEFFICIENTS_2) / sizeof(float), 1008);
} else { // if (unknown1 < 2)
this->fir1->LoadCoefficients(POLYPHASE_COEFFICIENTS_OTHER, sizeof(POLYPHASE_COEFFICIENTS_OTHER) / sizeof(float), 1008);
this->fir2->LoadCoefficients(POLYPHASE_COEFFICIENTS_OTHER, sizeof(POLYPHASE_COEFFICIENTS_OTHER) / sizeof(float), 1008);
if (param_1 == 2) {
this->fir1.LoadCoefficients(POLYPHASE_COEFFICIENTS_2, 63, 1008);
this->fir2.LoadCoefficients(POLYPHASE_COEFFICIENTS_2, 63, 1008);
} else { // if (param_1 < 2)
this->fir1.LoadCoefficients(POLYPHASE_COEFFICIENTS_OTHER, 63, 1008);
this->fir2.LoadCoefficients(POLYPHASE_COEFFICIENTS_OTHER, 63, 1008);
}
}
Polyphase::~Polyphase() {
delete this->fir1;
delete this->fir2;
delete this->waveBuffer1;
delete this->waveBuffer2;
delete[] this->buffer;
@ -166,8 +162,8 @@ 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) {
this->fir1->FilterSamplesInterleaved(this->buffer, 1008, 2);
this->fir2->FilterSamplesInterleaved(this->buffer + 1, 1008, 2);
this->fir1.FilterSamplesInterleaved(this->buffer, 1008, 2);
this->fir2.FilterSamplesInterleaved(this->buffer + 1, 1008, 2);
this->waveBuffer2->PushSamples(this->buffer, 1008);
}
}
@ -183,8 +179,8 @@ uint32_t Polyphase::Process(float *samples, uint32_t size) {
}
void Polyphase::Reset() {
this->fir1->Reset();
this->fir2->Reset();
this->fir1.Reset();
this->fir2.Reset();
this->waveBuffer1->Reset();
this->waveBuffer2->Reset();
}

View File

@ -6,7 +6,7 @@
class Polyphase {
public:
Polyphase(int unknown1);
Polyphase(int param_1);
~Polyphase();
uint32_t GetLatency();
@ -15,8 +15,8 @@ public:
void SetSamplingRate(uint32_t samplingRate);
private:
FIR *fir1;
FIR *fir2;
FIR fir1;
FIR fir2;
WaveBuffer *waveBuffer1;
WaveBuffer *waveBuffer2;
float *buffer;

View File

@ -1,9 +1,9 @@
#include "WaveBuffer.h"
#include <cstring>
WaveBuffer::WaveBuffer(uint32_t channels, uint32_t size) {
WaveBuffer::WaveBuffer(uint32_t channels, uint32_t length) {
this->channels = channels;
this->size = size * channels;
this->size = length * channels;
this->index = 0;
this->buffer = new float[this->size];
}
@ -31,14 +31,14 @@ uint32_t WaveBuffer::PopSamples(uint32_t size, bool resetIndex) {
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, this->buffer + this->channels * size, this->index * sizeof(float));
return size;
}
if (resetIndex) {
uint32_t idx = this->index;
uint32_t ret = this->index / this->channels;
this->index = 0;
return idx / this->channels;
return ret;
}
return 0;
@ -52,15 +52,15 @@ uint32_t WaveBuffer::PopSamples(float *dest, uint32_t size, bool resetIndex) {
if (this->channels * size <= this->index) {
memcpy(dest, this->buffer, 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, this->buffer + this->channels * size, this->index * sizeof(float));
return size;
}
if (resetIndex) {
uint32_t idx = this->index;
uint32_t ret = this->index / this->channels;
memcpy(dest, this->buffer, this->index * sizeof(float));
this->index = 0;
return idx / this->channels;
return ret;
}
return 0;
@ -74,13 +74,13 @@ int WaveBuffer::PushSamples(float *source, uint32_t size) {
if (size > 0) {
uint32_t requiredSize = this->channels * size + this->index;
if (this->size < requiredSize) {
auto *buf = new float[requiredSize];
memcpy(buf, this->buffer, this->index * sizeof(float));
auto *newBuffer = new float[requiredSize];
memcpy(newBuffer, this->buffer, this->index * sizeof(float));
delete[] this->buffer;
this->buffer = buf;
this->buffer = newBuffer;
this->size = requiredSize;
}
memcpy(&this->buffer[this->index], source, this->channels * size * sizeof(float));
memcpy(this->buffer + this->index, source, this->channels * size * sizeof(float));
this->index += this->channels * size;
}
@ -95,13 +95,13 @@ int WaveBuffer::PushZeros(uint32_t size) {
if (size > 0) {
uint32_t requiredSize = this->channels * size + this->index;
if (this->size < requiredSize) {
auto *buf = new float[requiredSize];
memcpy(buf, this->buffer, this->index * sizeof(float));
auto *newBuffer = new float[requiredSize];
memcpy(newBuffer, this->buffer, this->index * sizeof(float));
delete[] this->buffer;
this->buffer = buf;
this->buffer = newBuffer;
this->size = requiredSize;
}
memset(&this->buffer[this->index], 0, this->channels * size * sizeof(float));
memset(this->buffer + this->index, 0, this->channels * size * sizeof(float));
this->index += this->channels * size;
}
@ -109,26 +109,26 @@ int WaveBuffer::PushZeros(uint32_t size) {
}
float *WaveBuffer::PushZerosGetBuffer(uint32_t size) {
uint32_t oldIndex = this->index;
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 *buf = new float[requiredSize];
memcpy(buf, this->buffer, this->index * sizeof(float));
auto *newBuffer = new float[requiredSize];
memcpy(newBuffer, this->buffer, this->index * sizeof(float));
delete[] this->buffer;
this->buffer = buf;
this->buffer = newBuffer;
this->size = requiredSize;
}
memset(&this->buffer[this->index], 0, this->channels * size * sizeof(float));
memset(this->buffer + this->index, 0, this->channels * size * sizeof(float));
this->index += this->channels * size;
}
return &this->buffer[oldIndex];
return this->buffer + oldIndex;
}
void WaveBuffer::Reset() {

View File

@ -5,7 +5,7 @@
class WaveBuffer {
public:
WaveBuffer(uint32_t channels, uint32_t size);
WaveBuffer(uint32_t channels, uint32_t length);
~WaveBuffer();