110 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();
}
AnalogX::~AnalogX() {
}
2021-07-30 16:54:59 +02:00
void AnalogX::Process(float *samples, uint32_t size) {
for (int i = 0; i < 2 * size; i++) {
2021-07-30 16:54:59 +02:00
float sample = samples[i];
int index = i % 2;
float tmp = this->highpass[index].ProcessSample(sample);
2022-09-13 02:16:31 +02:00
tmp = this->harmonic[index].Process(tmp);
2021-07-30 16:54:59 +02:00
tmp = this->lowpass[index].ProcessSample(sample + tmp * this->gain);
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;
memset(samples, 0, 2 * size * sizeof(float));
}
}
void AnalogX::Reset() {
2022-09-13 02:16:31 +02:00
for (auto &highpass : this->highpass) {
highpass.RefreshFilter(FilterType::HIGHPASS, 0.0f, 240.0f, (float) this->samplingRate, 0.717f, false);
}
2021-07-30 16:54:59 +02:00
2022-09-13 02:16:31 +02:00
for (auto &peak : this->peak) {
peak.RefreshFilter(FilterType::PEAK, 0.58f, 633.0f, (float) this->samplingRate, 6.28f, true);
}
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) {
lowpass.RefreshFilter(FilterType::LOWPASS, 0.0f, 19650.0f, (float) this->samplingRate, 0.717f, false);
}
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) {
lowpass.RefreshFilter(FilterType::LOWPASS, 0.0f, 18233.0f, (float) this->samplingRate, 0.717f, false);
}
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) {
lowpass.RefreshFilter(FilterType::LOWPASS, 0.0f, 16307.0f, (float) this->samplingRate, 0.717f, false);
}
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
}