106 lines
2.8 KiB
C++
Raw Normal View History

2021-07-30 16:54:59 +02:00
#include "AnalogX.h"
#include <cstring>
2021-07-30 16:54:59 +02:00
#include "../constants.h"
static float ANALOGX_HARMONICS[10] = {
0.01f,
0.02f,
0.0001f,
0.001f,
2022-09-16 03:16:58 +02:00
0.0f,
0.0f,
0.0f,
0.0f,
0.0f,
0.0f
2021-07-30 16:54:59 +02:00
};
AnalogX::AnalogX() {
2022-09-13 02:16:31 +02:00
this->samplingRate = DEFAULT_SAMPLERATE;
2021-07-30 16:54:59 +02:00
this->processingModel = 0;
this->enabled = false;
Reset();
}
void AnalogX::Process(float *samples, uint32_t size) {
2022-10-04 05:00:43 +02:00
for (int i = 0; i < size * 2; i++) {
2021-07-30 16:54:59 +02:00
float sample = samples[i];
2022-10-04 05:00:43 +02:00
int channel = i % 2;
2021-07-30 16:54:59 +02:00
2022-10-04 05:00:43 +02:00
float tmp = this->highpass[channel].ProcessSample(sample);
tmp = this->harmonic[channel].Process(tmp);
2021-07-30 16:54:59 +02:00
2022-10-04 05:00:43 +02:00
tmp = this->lowpass[channel].ProcessSample(sample + tmp * this->gain);
2021-07-30 16:54:59 +02:00
tmp = this->peak->ProcessSample(tmp * 0.8f);
samples[i] = tmp;
}
2022-09-13 02:16:31 +02:00
if (this->freqRange < this->samplingRate / 4) {
2021-07-30 16:54:59 +02:00
this->freqRange += size;
2022-10-04 05:00:43 +02:00
memset(samples, 0, size * 2 * sizeof(float));
2021-07-30 16:54:59 +02:00
}
}
void AnalogX::Reset() {
2022-09-13 02:16:31 +02:00
for (auto &highpass : this->highpass) {
2022-10-04 05:00:43 +02:00
highpass.RefreshFilter(MultiBiquad::FilterType::HIGHPASS, 0.0f, 240.0f, this->samplingRate, 0.717f, false);
2022-09-13 02:16:31 +02:00
}
2021-07-30 16:54:59 +02:00
2022-09-13 02:16:31 +02:00
for (auto &peak : this->peak) {
2022-10-04 05:00:43 +02:00
peak.RefreshFilter(MultiBiquad::FilterType::PEAK, 0.58f, 633.0f, this->samplingRate, 6.28f, true);
2022-09-13 02:16:31 +02:00
}
2021-07-30 16:54:59 +02:00
2022-09-13 02:16:31 +02:00
for (auto &harmonic : this->harmonic) {
harmonic.Reset();
}
2021-07-30 16:54:59 +02:00
if (this->processingModel == 0) {
2022-09-13 02:16:31 +02:00
for (auto &harmonic : this->harmonic) {
harmonic.SetHarmonics(ANALOGX_HARMONICS);
}
2021-07-30 16:54:59 +02:00
this->gain = 0.6f;
2022-09-13 02:16:31 +02:00
for (auto &lowpass : this->lowpass) {
2022-10-04 05:00:43 +02:00
lowpass.RefreshFilter(MultiBiquad::FilterType::LOWPASS, 0.0f, 19650.0f, this->samplingRate, 0.717f, false);
2022-09-13 02:16:31 +02:00
}
2021-07-30 16:54:59 +02:00
} else if (this->processingModel == 1) {
2022-09-13 02:16:31 +02:00
for (auto &harmonic : this->harmonic) {
harmonic.SetHarmonics(ANALOGX_HARMONICS);
}
2021-07-30 16:54:59 +02:00
this->gain = 1.2f;
2022-09-13 02:16:31 +02:00
for (auto &lowpass : this->lowpass) {
2022-10-04 05:00:43 +02:00
lowpass.RefreshFilter(MultiBiquad::FilterType::LOWPASS, 0.0f, 18233.0f, this->samplingRate, 0.717f, false);
2022-09-13 02:16:31 +02:00
}
2021-07-30 16:54:59 +02:00
} else if (this->processingModel == 2) {
2022-09-13 02:16:31 +02:00
for (auto &harmonic : this->harmonic) {
harmonic.SetHarmonics(ANALOGX_HARMONICS);
}
2021-07-30 16:54:59 +02:00
this->gain = 2.4f;
2022-09-13 02:16:31 +02:00
for (auto &lowpass : this->lowpass) {
2022-10-04 05:00:43 +02:00
lowpass.RefreshFilter(MultiBiquad::FilterType::LOWPASS, 0.0f, 16307.0f, this->samplingRate, 0.717f, false);
2022-09-13 02:16:31 +02:00
}
2021-07-30 16:54:59 +02:00
}
this->freqRange = 0;
}
2022-09-13 02:16:31 +02:00
void AnalogX::SetProcessingModel(int processingModel) {
if (this->processingModel != processingModel) {
this->processingModel = processingModel;
Reset();
}
2021-07-30 16:54:59 +02:00
}
2022-09-13 02:16:31 +02:00
void AnalogX::SetSamplingRate(uint32_t samplingRate) {
if (this->samplingRate != samplingRate) {
this->samplingRate = samplingRate;
Reset();
}
2021-07-30 16:54:59 +02:00
}