ViPERFX_RE/src/viper/effects/FETCompressor.cpp

268 lines
6.2 KiB
C++
Raw Normal View History

2022-10-16 16:54:32 +02:00
#include <cmath>
2022-09-06 17:57:23 +02:00
#include "FETCompressor.h"
2022-10-16 16:54:32 +02:00
#include "../constants.h"
2022-09-06 17:57:23 +02:00
2022-10-16 16:54:32 +02:00
static const float DEFAULT_FETCOMP_PARAMETERS[] = {
1.000000,
0.000000,
0.000000,
0.000000,
1.000000,
0.000000,
1.000000,
0.514679,
1.000000,
0.384311,
1.000000,
0.500000,
0.879450,
0.884311,
0.615689,
0.660964,
1.000000
};
2022-09-06 17:57:23 +02:00
2022-10-16 16:54:32 +02:00
static double calculate_exp_something(double param_1, double param_2) {
return 1.0 - exp(-1.0 / (param_2 * param_1));
2022-09-06 17:57:23 +02:00
}
2022-10-16 16:54:32 +02:00
FETCompressor::FETCompressor() {
this->samplingRate = VIPER_DEFAULT_SAMPLING_RATE;
for (uint32_t i = 0; i < 17; i++) {
2022-10-17 00:48:32 +02:00
SetParameter((FETCompressor::Parameter) i, GetParameterDefault((FETCompressor::Parameter) i));
2022-10-16 16:54:32 +02:00
}
2022-09-06 17:57:23 +02:00
2022-10-16 16:54:32 +02:00
Reset();
2022-09-06 17:57:23 +02:00
}
2022-10-16 16:54:32 +02:00
float FETCompressor::GetMeter(int param_1) {
if (param_1 != 0) {
return 0.0;
}
2022-10-17 00:48:32 +02:00
if (this->enable) {
2022-10-16 17:01:43 +02:00
float tmp = (6.907755 - this->unk28) / 6.907755;
if (tmp < 1.0) {
if (tmp < 0.0) {
tmp = 0.0;
}
return tmp;
}
}
2022-09-06 17:57:23 +02:00
2022-10-16 16:54:32 +02:00
return 1.0;
2022-09-06 17:57:23 +02:00
}
2022-10-17 00:48:32 +02:00
float FETCompressor::GetParameter(FETCompressor::Parameter parameter) {
return this->parameters[parameter];
2022-10-16 16:54:32 +02:00
}
2022-09-06 17:57:23 +02:00
2022-10-17 00:48:32 +02:00
float FETCompressor::GetParameterDefault(FETCompressor::Parameter parameter) {
if (parameter < 17) {
return DEFAULT_FETCOMP_PARAMETERS[parameter];
2022-10-16 16:54:32 +02:00
}
return 0.0;
2022-09-06 17:57:23 +02:00
}
2022-10-16 16:54:32 +02:00
void FETCompressor::Process(float *samples, uint32_t size) {
2022-10-25 03:24:27 +02:00
return;
2022-10-16 16:54:32 +02:00
if (size == 0) return;
for (uint32_t i = 0; i < size * 2; i += 2) {
double inL = abs(samples[i]);
double inR = abs(samples[i + 1]);
double in;
if (inL > inR) {
in = inL;
} else {
in = inR;
}
2022-09-06 17:57:23 +02:00
2022-10-16 16:54:32 +02:00
double out = ProcessSidechain(in);
2022-10-17 00:48:32 +02:00
if (this->enable) {
2022-10-16 16:54:32 +02:00
samples[i] *= (float) out;
samples[i + 1] *= (float) out;
}
2022-10-17 00:48:32 +02:00
this->unk23 = this->unk23 + (this->threshold - this->unk23) * this->unk22;
this->unk24 = this->unk24 + this->unk22 * (this->gain - this->unk24);
2022-10-16 16:54:32 +02:00
}
2022-09-06 17:57:23 +02:00
}
2022-10-16 16:54:32 +02:00
double FETCompressor::ProcessSidechain(double in) {
double in2 = pow(in, 2.0);
if (in2 < 0.000001) {
in2 = 0.000001;
}
2022-10-17 00:48:32 +02:00
float a = this->attack2;
float b = this->unk25 + this->crest2 * (in2 - this->unk25);
float c = this->unk26 + this->crest2 * (in2 - this->unk26);
float d = this->attack1;
2022-10-16 16:54:32 +02:00
if (in2 < b) {
in2 = b;
}
this->unk26 = c;
this->unk25 = in2;
in2 /= c;
2022-10-17 00:48:32 +02:00
if (this->autoAttack) {
2022-10-16 16:54:32 +02:00
}
2022-09-06 17:57:23 +02:00
2022-10-17 00:48:32 +02:00
if (this->autoRelease) {
2022-10-16 16:54:32 +02:00
}
if (in >= 0.000001) {
}
2022-10-17 00:48:32 +02:00
if (!this->autoKnee) {
2022-10-16 16:54:32 +02:00
} else {
}
2022-10-17 00:48:32 +02:00
if (this->autoGain) {
if (!this->noClip) {
2022-10-16 16:54:32 +02:00
} else {
}
// return exp(-a - fVar6); // FIXME: Change "a" and "fVar6" variable
}
return exp(this->unk24 - a); // FIXME: Change "a" variable
2022-09-06 17:57:23 +02:00
}
void FETCompressor::Reset() {
2022-10-16 16:54:32 +02:00
this->unk22 = calculate_exp_something(this->samplingRate, 0.05);
2022-10-17 00:48:32 +02:00
this->unk23 = this->threshold;
this->unk24 = this->gain;
2022-10-16 16:54:32 +02:00
this->unk25 = 0.000001;
this->unk26 = 0.000001;
this->unk27 = 0.0;
this->unk28 = 0.0;
this->unk29 = 0.0;
2022-09-06 17:57:23 +02:00
}
2022-10-17 00:48:32 +02:00
void FETCompressor::SetParameter(FETCompressor::Parameter parameter, float value) {
this->parameters[parameter] = value;
2022-09-06 17:57:23 +02:00
2022-10-17 00:48:32 +02:00
switch (parameter) {
case ENABLE: {
this->enable = value >= 0.5;
2022-10-16 16:54:32 +02:00
break;
}
2022-10-17 00:48:32 +02:00
case THRESHOLD: {
this->threshold = log(pow(10.0, (value * -60.0) / 20.0));
2022-10-16 16:54:32 +02:00
break;
}
2022-10-17 00:48:32 +02:00
case RATIO: {
this->ratio = -value;
2022-10-16 16:54:32 +02:00
break;
}
2022-10-17 00:48:32 +02:00
case KNEE: {
this->knee = log(pow(10.0, (value * 60.0) / 20));
2022-10-16 16:54:32 +02:00
break;
}
2022-10-17 00:48:32 +02:00
case AUTO_KNEE: {
this->autoKnee = value >= 0.5;
2022-10-16 16:54:32 +02:00
break;
}
2022-10-17 00:48:32 +02:00
case GAIN: {
this->gain = log(pow(10.0, (value * 60.0) / 20.0));
2022-10-16 16:54:32 +02:00
break;
}
2022-10-17 00:48:32 +02:00
case AUTO_GAIN: {
this->autoGain = value >= 0.5;
2022-10-16 16:54:32 +02:00
break;
}
2022-10-17 00:48:32 +02:00
case ATTACK: {
2022-10-16 16:54:32 +02:00
double tmp = exp(value * 7.600903 - 9.21034);
2022-10-17 00:48:32 +02:00
this->attack1 = tmp;
2022-10-16 16:54:32 +02:00
if (tmp <= 0.0) {
tmp = 1.0;
} else {
tmp = calculate_exp_something(this->samplingRate, tmp);
}
2022-10-17 00:48:32 +02:00
this->attack2 = tmp;
2022-10-16 16:54:32 +02:00
break;
}
2022-10-17 00:48:32 +02:00
case AUTO_ATTACK: {
this->autoAttack = value >= 0.5;
2022-10-16 16:54:32 +02:00
break;
}
2022-10-17 00:48:32 +02:00
case RELEASE: {
2022-10-16 16:54:32 +02:00
double tmp = exp(value * 5.991465 - 5.298317);
2022-10-17 00:48:32 +02:00
this->release1 = tmp;
2022-10-16 16:54:32 +02:00
if (tmp <= 0.0) {
tmp = 1.0;
} else {
tmp = calculate_exp_something(this->samplingRate, tmp);
}
2022-10-17 00:48:32 +02:00
this->release2 = tmp;
2022-10-16 16:54:32 +02:00
break;
}
2022-10-17 00:48:32 +02:00
case AUTO_RELEASE: {
this->autoRelease = value >= 0.5;
2022-10-16 16:54:32 +02:00
break;
}
2022-10-17 00:48:32 +02:00
case KNEE_MULTI: {
this->kneeMulti = value * 4.0;
2022-10-16 16:54:32 +02:00
break;
}
2022-10-17 00:48:32 +02:00
case MAX_ATTACK: {
this->maxAttack = exp(value * 7.600903 - 9.21034);
2022-10-16 16:54:32 +02:00
break;
}
2022-10-17 00:48:32 +02:00
case MAX_RELEASE: {
this->maxRelease = exp(value * 5.991465 - 5.298317);
2022-10-16 16:54:32 +02:00
break;
}
2022-10-17 00:48:32 +02:00
case CREST: {
2022-10-16 16:54:32 +02:00
double tmp = exp(value * 5.991465 - 5.298317);
2022-10-17 00:48:32 +02:00
this->crest1 = tmp;
2022-10-16 16:54:32 +02:00
if (tmp <= 0.0) {
tmp = 1.0;
} else {
tmp = calculate_exp_something(this->samplingRate, tmp);
}
2022-10-17 00:48:32 +02:00
this->crest2 = tmp;
2022-10-16 16:54:32 +02:00
break;
}
2022-10-17 00:48:32 +02:00
case ADAPT: {
2022-10-16 16:54:32 +02:00
double tmp = exp(value * 1.386294);
2022-10-17 00:48:32 +02:00
this->adapt1 = tmp;
2022-10-16 16:54:32 +02:00
if (tmp <= 0.0) {
tmp = 1.0;
} else {
tmp = calculate_exp_something(this->samplingRate, tmp);
}
2022-10-17 00:48:32 +02:00
this->adapt2 = tmp;
2022-10-16 16:54:32 +02:00
break;
}
2022-10-17 00:48:32 +02:00
case NO_CLIP: {
this->noClip = value >= 0.5;
2022-10-16 16:54:32 +02:00
break;
}
}
2022-09-06 17:57:23 +02:00
}
void FETCompressor::SetSamplingRate(uint32_t samplingRate) {
2022-10-16 16:54:32 +02:00
this->samplingRate = samplingRate;
2022-09-06 17:57:23 +02:00
2022-10-16 16:54:32 +02:00
for (uint32_t i = 0; i < 17; i++) {
2022-10-17 00:48:32 +02:00
SetParameter((FETCompressor::Parameter) i, GetParameter((FETCompressor::Parameter) i));
2022-10-16 16:54:32 +02:00
}
2022-09-06 17:57:23 +02:00
2022-10-16 16:54:32 +02:00
Reset();
2022-09-06 17:57:23 +02:00
}