71 lines
2.3 KiB
C++
71 lines
2.3 KiB
C++
#include <VulcanoLE/Audio/Filter.h>
|
|
#include <VulcanoLE/Audio/Types.h>
|
|
#include <valarray>
|
|
#include <VUtils/Math.h>
|
|
#include <VUtils/Logging.h>
|
|
|
|
namespace Audio {
|
|
|
|
Filter::Filter() {
|
|
for (int i = 0; i < 3; ++i) {
|
|
m_filterOutput[i] = 0.0f;
|
|
for (int j = 0; j < 16; ++j) {
|
|
m_feedback[i][j] = 0.0f;
|
|
}
|
|
}
|
|
}
|
|
float Filter::process(float input, float cut, float) {
|
|
cut *= cut;
|
|
output = input;
|
|
if (m_counter >= 8)
|
|
m_counter = 0;
|
|
for (int i = 0; i < 4; ++i) {
|
|
output = output * cut + m_feedback[i][m_counter + 7] * (1.f - cut);
|
|
m_feedback[i][m_counter] = output;
|
|
m_feedback[i][m_counter + 8] = output;
|
|
m_filterOutput[i] = output;
|
|
}
|
|
m_counter++;
|
|
return m_filterOutput[3];
|
|
}
|
|
stereoSampleFrame FilterHelper::work(stereoSample *buffer, int size) {
|
|
output.clear();
|
|
output.resize(size);
|
|
double peakL = 0;
|
|
double peakR = 0;
|
|
for (int i = 0; i < size; ++i) {
|
|
output[i].r = filter_right.process(buffer[i].r, cutoff, 0);
|
|
output[i].l = filter_left.process(buffer[i].l, cutoff, 0);
|
|
double l = std::abs(output[i].l);
|
|
double r = std::abs(output[i].r);
|
|
if (l > peakL) peakL = l;
|
|
if (r > peakR) peakR = r;
|
|
}
|
|
return doLimit(peakL, peakR);
|
|
}
|
|
stereoSampleFrame FilterHelper::doLimit(double left, double right) {
|
|
if (left > m_limit) {
|
|
m_limit = left + 0.05;
|
|
DBG("Overshot! New Limit: %.2f", m_limit)
|
|
}
|
|
if (right > m_limit) {
|
|
m_limit = right + 0.05;
|
|
DBG("Overshot! New Limit: %.2f", m_limit)
|
|
}
|
|
|
|
m_limit = std::clamp(m_limit, 0.0, 1.0);
|
|
if (left < 0.00005 && right < 0.00005) {
|
|
// reset limit :D
|
|
overshot_times++;
|
|
if (overshot_times > 10000 && m_limit != FILTER_LIMIT) {
|
|
DBG("OVER 10000 SILENCE SIGNALS... RESET TO INITIAL LIMIT")
|
|
m_limit = FILTER_LIMIT;
|
|
overshot_times = 0;
|
|
}
|
|
}
|
|
return {
|
|
(float) VUtils::Math::map(left, 0, m_limit, 0, 255),
|
|
(float) VUtils::Math::map(right, 0, m_limit, 0, 255),
|
|
};
|
|
}
|
|
} |