2021-07-30 16:54:59 +02:00
|
|
|
#include "AnalogX.h"
|
2022-08-30 00:39:15 +02:00
|
|
|
#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-10-13 03:01:20 +02:00
|
|
|
this->samplingRate = VIPER_DEFAULT_SAMPLING_RATE;
|
2021-07-30 16:54:59 +02:00
|
|
|
this->processingModel = 0;
|
2022-10-11 00:36:38 +02:00
|
|
|
this->enable = false;
|
2021-07-30 16:54:59 +02:00
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnalogX::Process(float *samples, uint32_t size) {
|
2022-10-11 00:36:38 +02:00
|
|
|
if (this->enable) {
|
|
|
|
for (int i = 0; i < size * 2; i++) {
|
|
|
|
float sample = samples[i];
|
|
|
|
int channel = i % 2;
|
2021-07-30 16:54:59 +02:00
|
|
|
|
2022-10-13 03:01:20 +02:00
|
|
|
double tmp = this->highpass[channel].ProcessSample(sample);
|
2022-10-11 00:36:38 +02:00
|
|
|
tmp = this->harmonic[channel].Process(tmp);
|
2021-07-30 16:54:59 +02:00
|
|
|
|
2022-10-11 00:36:38 +02:00
|
|
|
tmp = this->lowpass[channel].ProcessSample(sample + tmp * this->gain);
|
|
|
|
tmp = this->peak->ProcessSample(tmp * 0.8f);
|
2021-07-30 16:54:59 +02:00
|
|
|
|
2022-10-11 00:36:38 +02:00
|
|
|
samples[i] = tmp;
|
|
|
|
}
|
2021-07-30 16:54:59 +02:00
|
|
|
|
2022-10-11 00:36:38 +02:00
|
|
|
if (this->freqRange < this->samplingRate / 4) {
|
|
|
|
this->freqRange += size;
|
|
|
|
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-10-11 00:36:38 +02:00
|
|
|
void AnalogX::SetEnable(bool enable) {
|
|
|
|
if (this->enable != enable) {
|
|
|
|
if (!this->enable) {
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
this->enable = enable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|