ViPERFX_RE/src/viper/effects/PlaybackGain.cpp

115 lines
2.8 KiB
C++
Raw Normal View History

2022-11-24 01:17:13 +01:00
#include <cmath>
#include "PlaybackGain.h"
2022-09-25 01:46:10 +02:00
#include "../constants.h"
2022-09-06 17:57:23 +02:00
PlaybackGain::PlaybackGain() {
2022-09-25 01:46:10 +02:00
this->enable = false;
2022-10-13 03:01:20 +02:00
this->samplingRate = VIPER_DEFAULT_SAMPLING_RATE;
2022-09-25 01:46:10 +02:00
this->unknown1 = 0.4342945;
this->counterTo100 = 0;
this->ratio1 = 2.0;
this->ratio2 = 0.5;
this->volume = 1.0;
this->maxGainFactor = 1.0;
this->unknown2 = 1.0;
this->unknown3 = 1.0;
this->biquad1.SetBandPassParameter(2200.0,this->samplingRate,0.33);
this->biquad2.SetBandPassParameter(2200.0,this->samplingRate,0.33);
2022-09-06 17:57:23 +02:00
}
2022-12-13 03:10:29 +01:00
double PlaybackGain::AnalyseWave(float *samples, uint32_t size) {
2022-11-24 01:17:13 +01:00
double tmpL = 0.0;
double tmpR = 0.0;
for (uint32_t i = 0; i < size * 2; i += 2) {
double tmpL2 = this->biquad1.ProcessSample(samples[i]);
tmpL += tmpL2 * tmpL2;
double tmpR2 = this->biquad2.ProcessSample(samples[i + 1]);
tmpR += tmpR2 * tmpR2;
}
2022-09-06 17:57:23 +02:00
2022-12-13 03:10:29 +01:00
double tmp;
if (tmpL > tmpR) {
tmp = tmpL;
} else {
tmp = tmpR;
}
2022-09-06 17:57:23 +02:00
2022-12-13 03:10:29 +01:00
return tmp / (double) size;
2022-09-06 17:57:23 +02:00
}
2022-12-13 03:10:29 +01:00
//static float PlaybackGain::ProcessSingle(float sample) {
//
//}
2022-09-25 01:46:10 +02:00
void PlaybackGain::Process(float *samples, uint32_t size) {
2022-12-13 03:10:29 +01:00
if (!this->enable) return;
if (size == 0) return;
double analyzed = AnalyseWave(samples, size);
double a = log(analyzed);
if (this->counterTo100 < 100) {
this->counterTo100++;
}
double b = a * this->unknown1 * 10.0 + 23.0;
double c = ((double) this->counterTo100 / 100.0) * (b * this->ratio2 - b);
double d = c / 100.0;
double e = pow(10.0, (c - d * d * 50.0) / 20.0);
uint32_t f;
if (size < this->samplingRate / 40) {
f = this->samplingRate / 40;
} else {
f = size;
}
double unk2 = this->unknown2;
double g = (e * this->volume - this->unknown2) / f;
if (g >= 0.0) {
g *= 0.0625;
}
2022-09-06 17:57:23 +02:00
2022-12-13 03:10:29 +01:00
for (uint32_t i = 0; i < size; i++) {
}
2022-09-06 17:57:23 +02:00
}
2022-09-25 01:46:10 +02:00
void PlaybackGain::Reset() {
this->biquad1.SetBandPassParameter(2200.0,this->samplingRate,0.33);
this->biquad2.SetBandPassParameter(2200.0,this->samplingRate,0.33);
this->unknown2 = 1.0;
this->counterTo100 = 0;
this->unknown3 = 1.0;
2022-09-06 17:57:23 +02:00
}
2022-09-25 01:46:10 +02:00
void PlaybackGain::SetEnable(bool enable) {
2022-11-24 01:17:13 +01:00
if (this->enable != enable) {
if (enable) {
Reset();
}
2022-12-13 03:10:29 +01:00
this->enable = enable;
2022-09-25 01:46:10 +02:00
}
2022-09-06 17:57:23 +02:00
}
2022-09-25 01:46:10 +02:00
void PlaybackGain::SetMaxGainFactor(float maxGainFactor) {
this->maxGainFactor = maxGainFactor;
2022-09-06 17:57:23 +02:00
}
2022-09-25 01:46:10 +02:00
void PlaybackGain::SetRatio(float ratio) {
this->ratio1 = ratio + 1.0f;
2022-11-24 01:17:13 +01:00
this->ratio2 = 1.0f / this->ratio1;
2022-09-06 17:57:23 +02:00
}
2022-09-25 01:46:10 +02:00
void PlaybackGain::SetSamplingRate(uint32_t samplingRate) {
2022-11-24 01:17:13 +01:00
if (this->samplingRate != samplingRate) {
this->samplingRate = samplingRate;
Reset();
}
2022-09-06 17:57:23 +02:00
}
2022-09-25 01:46:10 +02:00
void PlaybackGain::SetVolume(float volume) {
this->volume = volume;
2022-09-06 17:57:23 +02:00
}