ViPERFX_RE/src/cpp/viper/utils/Subwoofer.cpp

37 lines
1.8 KiB
C++
Raw Normal View History

2021-07-30 21:51:26 +02:00
#include "Subwoofer.h"
#include "../constants.h"
#include <cmath>
Subwoofer::Subwoofer() {
2022-09-19 02:36:53 +02:00
uint32_t samplingRate = DEFAULT_SAMPLERATE;
this->peak[0].RefreshFilter(FilterType::PEAK, 0.0, 37.0, samplingRate, 1.0, false);
this->peak[1].RefreshFilter(FilterType::PEAK, 0.0, 37.0, samplingRate, 1.0, false);
this->peakLow[0].RefreshFilter(FilterType::PEAK, 0.0, 75.0, samplingRate, 1.0, false);
this->peakLow[1].RefreshFilter(FilterType::PEAK, 0.0, 75.0, samplingRate, 1.0, false);
this->lowpass[0].RefreshFilter(FilterType::LOWPASS, 0.0, 200.0, samplingRate, 1.0, false);
this->lowpass[1].RefreshFilter(FilterType::LOWPASS, 0.0, 200.0, samplingRate, 1.0, false);
2021-07-30 21:51:26 +02:00
}
void Subwoofer::Process(float *samples, uint32_t size) {
for (int i = 0; i < size * 2; i++) {
2022-09-19 02:36:53 +02:00
auto sample = (double) samples[i];
2021-07-30 21:51:26 +02:00
int index = i % 2;
2022-09-19 02:36:53 +02:00
double tmp = this->peak[index].ProcessSample(sample);
2021-07-30 21:51:26 +02:00
tmp = this->peakLow[index].ProcessSample(tmp);
tmp = this->lowpass[index].ProcessSample(tmp - sample);
2022-09-21 03:43:03 +02:00
samples[i] = (float) ((sample / 2.0) + (tmp * 0.6));
2021-07-30 21:51:26 +02:00
}
}
2022-09-21 03:43:03 +02:00
void Subwoofer::SetBassGain(uint32_t samplingRate, float gainDb) {
float gain = 20.0f * log10( gainDb);
float gainLower = 20.0f * log10( gainDb / 8.0f);
2021-07-30 21:51:26 +02:00
2022-09-21 03:43:03 +02:00
this->peak[0].RefreshFilter(FilterType::PEAK, gain, 44.0, (double) samplingRate, 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) samplingRate, 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) samplingRate, 0.6, false);
this->lowpass[1].RefreshFilter(FilterType::LOWPASS, 0.0, 380.0, (double) samplingRate, 0.6, false);
2021-07-30 21:51:26 +02:00
}