This commit is contained in:
Iscle 2022-09-21 03:43:03 +02:00
parent 9e81c8b1d0
commit 420c09be29
9 changed files with 108 additions and 38 deletions

View File

@ -28,8 +28,8 @@ void Biquad::Reset() {
this->y_2 = 0; this->y_2 = 0;
} }
void Biquad::SetBandPassParameter(double frequency, double samplingRate, float qFactor) { void Biquad::SetBandPassParameter(float frequency, uint32_t samplingRate, float qFactor) {
double x = (2.0 * M_PI * frequency) / samplingRate; double x = (2.0 * M_PI * (double) frequency) / (double) samplingRate;
double sinX = sin(x); double sinX = sin(x);
double cosX = cos(x); double cosX = cos(x);
double y = sinX / ((double) qFactor * 2.0); double y = sinX / ((double) qFactor * 2.0);
@ -56,8 +56,8 @@ void Biquad::SetCoeffs(double a0, double a1, double a2, double b0, double b1, do
} }
void void
Biquad::SetHighPassParameter(double frequency, double samplingRate, double param_4, float qFactor, double param_6) { Biquad::SetHighPassParameter(float frequency, uint32_t samplingRate, double param_4, float qFactor, double param_6) {
double x = (2.0 * M_PI * frequency) / samplingRate; double x = (2.0 * M_PI * (double) frequency) / (double) samplingRate;
double sinX = sin(x); double sinX = sin(x);
double cosX = cos(x); double cosX = cos(x);
@ -81,8 +81,8 @@ Biquad::SetHighPassParameter(double frequency, double samplingRate, double param
this->SetCoeffs(a0, a1, a2, b0, b1, b2); this->SetCoeffs(a0, a1, a2, b0, b1, b2);
} }
void Biquad::SetLowPassParameter(double frequency, double samplingRate, float qFactor) { void Biquad::SetLowPassParameter(float frequency, uint32_t samplingRate, float qFactor) {
double x = (2.0 * M_PI * frequency) / samplingRate; double x = (2.0 * M_PI * (double) frequency) / (double) samplingRate;
double sinX = sin(x); double sinX = sin(x);
double y = sinX / ((double) qFactor * 2.0); double y = sinX / ((double) qFactor * 2.0);
double cosX = cos(x); double cosX = cos(x);

View File

@ -8,10 +8,10 @@ public:
double ProcessSample(double sample); double ProcessSample(double sample);
void Reset(); void Reset();
void SetBandPassParameter(double frequency, double samplingRate, float qFactor); void SetBandPassParameter(float frequency, uint32_t samplingRate, float qFactor);
void SetCoeffs(double a0, double a1, double a2, double b0, double b1, double b2); void SetCoeffs(double a0, double a1, double a2, double b0, double b1, double b2);
void SetHighPassParameter(double frequency, double samplingRate, double param_4, float qFactor, double param_6); void SetHighPassParameter(float frequency, uint32_t samplingRate, double param_4, float qFactor, double param_6);
void SetLowPassParameter(double frequency, double samplingRate, float qFactor); void SetLowPassParameter(float frequency, uint32_t samplingRate, float qFactor);
private: private:
double x_1; double x_1;

View File

@ -24,7 +24,70 @@ double MultiBiquad::ProcessSample(double sample) {
} }
void void
MultiBiquad::RefreshFilter(FilterType type, double gainAmp, double freq, double samplingRate, double qFactor, bool param_7) { MultiBiquad::RefreshFilter(FilterType type, float gainAmp, float frequency, uint32_t samplingRate, float qFactor, bool param_7) {
float gain;
if (type - 5 < 3) { // type - 5 < 3 is always true... right?
gain = pow(10.0f, gainAmp / 40.0f);
} else {
gain = pow(10.0f, gainAmp / 20.0f);
}
double x = (2.0 * M_PI * (double) frequency) / (double) samplingRate;
double sinX = sin(x);
double cosX = cos(x);
double y;
double z;
if (type - 6 < 2) {
y = sinX / 2.0 * sqrt((1.0 / ((double) gain * 2.0)) * (1.0 / (double) qFactor - 1.0) + 2.0);
z = sqrt((double) gain) * y;
} else if (!param_7) {
y = sinX / ((double) qFactor / 2.0);
z = -1.0;
} else {
y = sinh(((double) qFactor * (log(2) / 2.0) * x) / sinX);
z = -1.0;
}
switch (type) {
case LOWPASS: {
break;
}
case HIGHPASS: {
break;
}
case BANDPASS: {
break;
}
case BANDSTOP: {
break;
}
case ALLPASS: {
break;
}
case PEAK: {
break;
}
case LOWSHELF: {
break;
}
case HIGHSHELF: {
break;
}
}
bool uVar1; bool uVar1;
double dVar4; double dVar4;
double dVar5; double dVar5;
@ -42,7 +105,7 @@ MultiBiquad::RefreshFilter(FilterType type, double gainAmp, double freq, double
double b2; double b2;
dVar10 = pow(10.0, gainAmp / 40.0); dVar10 = pow(10.0, gainAmp / 40.0);
dVar4 = (freq * 2 * M_PI) / samplingRate; dVar4 = (frequency * 2 * M_PI) / samplingRate;
dVar5 = sin(dVar4); dVar5 = sin(dVar4);
dVar11 = cos(dVar4); dVar11 = cos(dVar4);
uVar1 = type == HIGHSHELF; uVar1 = type == HIGHSHELF;

View File

@ -1,14 +1,14 @@
#pragma once #pragma once
enum FilterType { enum FilterType {
LOWPASS, LOWPASS = 0,
HIGHPASS, HIGHPASS = 1,
BANDPASS, BANDPASS = 2,
BANDSTOP, BANDSTOP = 3,
ALLPASS, ALLPASS = 4,
PEAK, PEAK = 5,
LOWSHELF, LOWSHELF = 6,
HIGHSHELF HIGHSHELF = 7
}; };
class MultiBiquad { class MultiBiquad {
@ -16,11 +16,18 @@ public:
MultiBiquad(); MultiBiquad();
double ProcessSample(double sample); double ProcessSample(double sample);
void RefreshFilter(FilterType type, float gainAmp, float frequency, uint32_t samplingRate, float qFactor, bool param_7);
void RefreshFilter(FilterType type, double gainAmp, double freq, double samplingRate, double qFactor, bool param_7); private:
double x_1;
double y_2, y_1, x_2, x_1; double x_2;
double b0, b1, b2, a1, a2; double y_1;
double y_2;
double a1;
double a2;
double b0;
double b1;
double b2;
}; };

View File

@ -1,8 +1,8 @@
#pragma once #pragma once
#include <cstdint> #include <cstdint>
#include "WaveBuffer.h"
#include "FIR.h" #include "FIR.h"
#include "WaveBuffer.h"
class Polyphase { class Polyphase {
public: public:

View File

@ -19,18 +19,18 @@ void Subwoofer::Process(float *samples, uint32_t size) {
double tmp = this->peak[index].ProcessSample(sample); double tmp = this->peak[index].ProcessSample(sample);
tmp = this->peakLow[index].ProcessSample(tmp); tmp = this->peakLow[index].ProcessSample(tmp);
tmp = this->lowpass[index].ProcessSample(tmp - sample); tmp = this->lowpass[index].ProcessSample(tmp - sample);
samples[i] = (float) ((sample / 2.0) + (tmp * 0.6f)); samples[i] = (float) ((sample / 2.0) + (tmp * 0.6));
} }
} }
void Subwoofer::SetBassGain(uint32_t samplerate, float gainDb) { void Subwoofer::SetBassGain(uint32_t samplingRate, float gainDb) {
double gain = 20.0 * log10( gainDb); float gain = 20.0f * log10( gainDb);
double gainLower = 20.0 * log10( gainDb / 8.0); float gainLower = 20.0f * log10( gainDb / 8.0f);
this->peak[0].RefreshFilter(FilterType::PEAK, gain, 44.0, (double) samplerate, 0.75, true); this->peak[0].RefreshFilter(FilterType::PEAK, gain, 44.0, (double) samplingRate, 0.75, true);
this->peak[1].RefreshFilter(FilterType::PEAK, gain, 44.0, (double) samplerate, 0.75, true); this->peak[1].RefreshFilter(FilterType::PEAK, gain, 44.0, (double) samplingRate, 0.75, true);
this->peakLow[0].RefreshFilter(FilterType::PEAK, gainLower, 80.0, (double) samplerate, 0.2, true); this->peakLow[0].RefreshFilter(FilterType::PEAK, gainLower, 80.0, (double) samplingRate, 0.2, true);
this->peakLow[1].RefreshFilter(FilterType::PEAK, gainLower, 80.0, (double) samplerate, 0.2, true); this->peakLow[1].RefreshFilter(FilterType::PEAK, gainLower, 80.0, (double) samplingRate, 0.2, true);
this->lowpass[0].RefreshFilter(FilterType::LOWPASS, 0.0, 380.0, (double) samplerate, 0.6, false); this->lowpass[0].RefreshFilter(FilterType::LOWPASS, 0.0, 380.0, (double) samplingRate, 0.6, false);
this->lowpass[1].RefreshFilter(FilterType::LOWPASS, 0.0, 380.0, (double) samplerate, 0.6, false); this->lowpass[1].RefreshFilter(FilterType::LOWPASS, 0.0, 380.0, (double) samplingRate, 0.6, false);
} }

View File

@ -8,7 +8,7 @@ public:
Subwoofer(); Subwoofer();
void Process(float *samples, uint32_t size); void Process(float *samples, uint32_t size);
void SetBassGain(uint32_t samplerate, float gainDb); void SetBassGain(uint32_t samplingRate, float gainDb);
private: private:
MultiBiquad peak[2]; MultiBiquad peak[2];

View File

@ -1,7 +1,7 @@
#include "WaveBuffer.h" #include "WaveBuffer.h"
#include <cstring> #include <cstring>
WaveBuffer::WaveBuffer(int channels, uint32_t size) { WaveBuffer::WaveBuffer(uint32_t channels, uint32_t size) {
this->channels = channels; this->channels = channels;
this->size = size * channels; this->size = size * channels;
this->index = 0; this->index = 0;

View File

@ -5,7 +5,7 @@
class WaveBuffer { class WaveBuffer {
public: public:
WaveBuffer(int channels, uint32_t size); WaveBuffer(uint32_t channels, uint32_t size);
~WaveBuffer(); ~WaveBuffer();
@ -24,7 +24,7 @@ private:
float *buffer; float *buffer;
uint32_t size; uint32_t size;
uint32_t index; uint32_t index;
int channels; uint32_t channels;
}; };