2021-07-28 14:20:36 +02:00
|
|
|
#include "DynamicBass.h"
|
|
|
|
#include "../constants.h"
|
|
|
|
|
|
|
|
DynamicBass::DynamicBass() {
|
|
|
|
this->qPeak = 0;
|
2022-10-13 03:01:20 +02:00
|
|
|
SetSamplingRate(VIPER_DEFAULT_SAMPLING_RATE);
|
2021-07-28 14:20:36 +02:00
|
|
|
this->bassGain = 1.f;
|
|
|
|
this->sideGainX = 1.f;
|
|
|
|
this->sideGainY = 1.f;
|
|
|
|
this->lowFreqX = 120;
|
|
|
|
this->highFreqX = 80;
|
|
|
|
this->lowFreqY = 40;
|
2022-09-23 04:15:43 +02:00
|
|
|
this->highFreqY = (uint32_t) ((float) this->samplingRate / 4.0f);
|
2021-07-28 14:20:36 +02:00
|
|
|
|
|
|
|
this->filterX.SetPassFilter(this->lowFreqX, this->highFreqX);
|
|
|
|
this->filterY.SetPassFilter(this->lowFreqY, this->highFreqY);
|
2022-09-23 04:15:43 +02:00
|
|
|
this->lowPass.SetLowPassParameter(55.f, this->samplingRate, this->qPeak / 666.f + 0.5f);
|
2021-07-28 14:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DynamicBass::FilterSamples(float *samples, uint32_t size) {
|
|
|
|
if (this->lowFreqX <= 120) {
|
|
|
|
for (int i = 0; i < size; i++) {
|
2022-08-23 00:26:44 +02:00
|
|
|
float left = samples[2 * i];
|
|
|
|
float right = samples[2 * i + 1];
|
2022-09-23 04:15:43 +02:00
|
|
|
float avg = (float) this->lowPass.ProcessSample(left + right);
|
2022-08-23 00:26:44 +02:00
|
|
|
samples[2 * i] = left + avg;
|
|
|
|
samples[2 * i + 1] = right + avg;
|
2021-07-28 14:20:36 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
float x1, x2, x3, x4, x5, x6, y1, y2, y3, y4, y5, y6;
|
|
|
|
|
2022-08-23 00:26:44 +02:00
|
|
|
this->filterX.DoFilterLeft(samples[2 * i], &x1, &x2, &x3);
|
|
|
|
this->filterX.DoFilterRight(samples[2 * i + 1], &x4, &x5, &x6);
|
2021-07-28 14:20:36 +02:00
|
|
|
this->filterY.DoFilterLeft(this->bassGain * x1, &y1, &y2, &y3);
|
|
|
|
this->filterY.DoFilterRight(this->bassGain * x4, &y4, &y5, &y6);
|
|
|
|
|
2022-08-23 00:26:44 +02:00
|
|
|
samples[2 * i] = x2 + y3 + this->sideGainX * y2 + this->sideGainY * y1 + x3;
|
|
|
|
samples[2 * i + 1] = x5 + y6 + this->sideGainX * y5 + this->sideGainY * y4 + x6;
|
2021-07-28 14:20:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-23 04:15:43 +02:00
|
|
|
void DynamicBass::Reset() {
|
|
|
|
this->filterX.Reset();
|
|
|
|
this->filterY.Reset();
|
|
|
|
this->lowPass.SetLowPassParameter(55.0, this->samplingRate, this->qPeak / 666.0f + 0.5f);
|
|
|
|
}
|
|
|
|
|
2021-07-28 14:20:36 +02:00
|
|
|
void DynamicBass::SetBassGain(float gain) {
|
|
|
|
this->bassGain = gain;
|
2022-09-23 04:15:43 +02:00
|
|
|
|
|
|
|
double x = ((gain - 1.0) / 20.0) * 1600.0;
|
|
|
|
if (x > 1600.0) {
|
|
|
|
x = 1600.0;
|
2021-07-28 14:20:36 +02:00
|
|
|
}
|
2022-09-23 04:15:43 +02:00
|
|
|
this->qPeak = (float) x;
|
2021-07-28 14:20:36 +02:00
|
|
|
|
2022-09-23 04:15:43 +02:00
|
|
|
this->lowPass.SetLowPassParameter(55.0, this->samplingRate, this->qPeak / 666.0f + 0.5f);
|
2021-07-28 14:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DynamicBass::SetFilterXPassFrequency(uint32_t low, uint32_t high) {
|
|
|
|
this->lowFreqX = low;
|
|
|
|
this->highFreqX = high;
|
|
|
|
|
|
|
|
this->filterX.SetPassFilter(low, high);
|
2022-09-23 04:15:43 +02:00
|
|
|
this->filterX.SetSamplingRate(this->samplingRate);
|
|
|
|
this->lowPass.SetLowPassParameter(55.0, this->samplingRate, this->qPeak / 666.0f + 0.5f);
|
2021-07-28 14:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DynamicBass::SetFilterYPassFrequency(uint32_t low, uint32_t high) {
|
|
|
|
this->lowFreqY = low;
|
|
|
|
this->highFreqY = high;
|
|
|
|
|
|
|
|
this->filterY.SetPassFilter(low, high);
|
2022-09-23 04:15:43 +02:00
|
|
|
this->filterY.SetSamplingRate(this->samplingRate);
|
|
|
|
this->lowPass.SetLowPassParameter(55.0, this->samplingRate, this->qPeak / 666.0f + 0.5f);
|
2021-07-28 14:20:36 +02:00
|
|
|
}
|
|
|
|
|
2022-09-23 04:15:43 +02:00
|
|
|
void DynamicBass::SetSamplingRate(uint32_t samplingRate) {
|
|
|
|
this->samplingRate = samplingRate;
|
|
|
|
this->filterX.SetSamplingRate(samplingRate);
|
|
|
|
this->filterY.SetSamplingRate(samplingRate);
|
|
|
|
this->lowPass.SetLowPassParameter(55.0, samplingRate, this->qPeak / 666.0f + 0.5f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DynamicBass::SetSideGain(float gainX, float gainY) {
|
|
|
|
this->sideGainX = gainX;
|
|
|
|
this->sideGainY = gainY;
|
2021-07-28 14:20:36 +02:00
|
|
|
}
|