ViPERFX_RE/src/cpp/viper/utils/DepthSurround.cpp

84 lines
2.8 KiB
C++
Raw Normal View History

2021-07-28 12:40:34 +02:00
#include "DepthSurround.h"
#include <cstring>
#include <cmath>
#include "../constants.h"
DepthSurround::DepthSurround() {
this->strength = 0;
this->enabled = false;
this->strengthAtLeast500 = false;
this->gain = 0;
2022-09-16 03:16:58 +02:00
for (auto &prev : this->prev) {
prev = 0.0f;
}
this->SetSamplingRate(DEFAULT_SAMPLERATE);
this->RefreshStrength(this->strength);
2021-07-28 12:40:34 +02:00
}
void DepthSurround::Process(float *samples, uint32_t size) {
if (this->enabled) {
if (!this->strengthAtLeast500) {
for (int i = 0; i < size; i++) {
float sampleLeft = samples[2 * i];
float sampleRight = samples[2 * i + 1];
2021-07-28 12:40:34 +02:00
this->prev[0] = this->gain * this->delay[0].ProcessSample(sampleLeft + this->prev[1]);
this->prev[1] = this->gain * this->delay[1].ProcessSample(sampleRight + this->prev[0]);
float l = this->prev[0] + sampleLeft;
float r = this->prev[1] + sampleRight;
float diff = (l - r) / 2.f;
float avg = (l + r) / 2.f;
float avgOut = this->highpass.ProcessSample(diff);
samples[2 * i] = avg + (diff - avgOut);
samples[2 * i + 1] = avg - (diff - avgOut);
2021-07-28 12:40:34 +02:00
}
} else {
for (int i = 0; i < size; i++) {
float sampleLeft = samples[2 * i];
float sampleRight = samples[2 * i + 1];
2021-07-28 12:40:34 +02:00
this->prev[0] = this->gain * this->delay[0].ProcessSample(sampleLeft + this->prev[1]);
this->prev[1] = -this->gain * this->delay[1].ProcessSample(sampleRight + this->prev[0]);
float l = this->prev[0] + sampleLeft;
float r = this->prev[1] + sampleRight;
float diff = (l - r) / 2.f;
float avg = (l + r) / 2.f;
float avgOut = this->highpass.ProcessSample(diff);
samples[2 * i] = avg + (diff - avgOut);
samples[2 * i + 1] = avg - (diff - avgOut);
2021-07-28 12:40:34 +02:00
}
}
}
}
void DepthSurround::RefreshStrength(short strength) {
this->strengthAtLeast500 = strength >= 500;
this->enabled = strength != 0;
if (strength != 0) {
2022-09-16 03:16:58 +02:00
float gain = powf(10.0f, ((strength / 1000.0f) * 10.0f - 15.0f) / 20.0f);
if (fabsf(gain) > 1.0f) {
gain = 1.0f;
2021-07-28 12:40:34 +02:00
}
2022-09-16 03:16:58 +02:00
this->gain = gain;
2021-07-28 12:40:34 +02:00
} else {
this->gain = 0;
}
}
void DepthSurround::SetSamplingRate(uint32_t samplerate) {
this->delay[0].SetParameters(samplerate, 0.02);
this->delay[1].SetParameters(samplerate, 0.014);
2022-09-16 03:16:58 +02:00
this->highpass.SetHighPassParameter(800.0f, samplerate, -11.0f, 0.72f, 0);
for (auto &prev : this->prev) {
prev = 0.0f;
}
2021-07-28 12:40:34 +02:00
}
void DepthSurround::SetStrength(short strength) {
this->strength = strength;
2022-09-16 03:16:58 +02:00
this->RefreshStrength(strength);
2021-07-28 12:40:34 +02:00
}