feat: add basic lp filter
This commit is contained in:
parent
2bd91c7d8e
commit
75843a7309
4 changed files with 134 additions and 9 deletions
49
core/Filter.cpp
Normal file
49
core/Filter.cpp
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
#include "Filter.hpp"
|
||||
#include <cmath>
|
||||
|
||||
Filter::Filter(float initCutOff, float initSampleRate)
|
||||
: cutOff_(initCutOff), feedback_(0.0f), alpha_(0.0f),
|
||||
sampleRate_(initSampleRate), zL_(0.0f), zR_(0.0f) {
|
||||
recompute();
|
||||
};
|
||||
|
||||
void Filter::process(const float *in, float *out, uint32_t frames) {
|
||||
filter(in, out, frames, &zL_);
|
||||
};
|
||||
void Filter::processStereo(const float *inLeft, const float *inRight,
|
||||
float *outLeft, float *outRight, uint32_t frames) {
|
||||
filter(inLeft, outLeft, frames, &zL_);
|
||||
filter(inRight, outRight, frames, &zR_);
|
||||
};
|
||||
|
||||
void Filter::filter(const float *in, float *out, uint32_t frames, float *z) {
|
||||
for (uint32_t i = 0; i < frames; ++i) {
|
||||
float feedbackInput = in[i] + (feedback_ * (*z));
|
||||
*z = *z + alpha_ * (feedbackInput - *z);
|
||||
out[i] = *z;
|
||||
}
|
||||
}
|
||||
|
||||
void Filter::recompute() {
|
||||
float fc = std::fmax(cutOff_, 1.0f);
|
||||
float rc = 1.0f / (2.0f * M_PI * fc);
|
||||
float val = 1.0f - std::exp(-1.0f / (rc * sampleRate_));
|
||||
alpha_ = std::fmin(val, 0.999f);
|
||||
};
|
||||
|
||||
void Filter::setCutOff(float newCutOff) {
|
||||
cutOff_ = newCutOff;
|
||||
recompute();
|
||||
}
|
||||
float Filter::getCutOff() const { return cutOff_; }
|
||||
|
||||
void Filter::setFeedback(float newFeedback) {
|
||||
feedback_ = std::fmin(std::fmax(newFeedback, 0.0f), 0.99f);
|
||||
}
|
||||
float Filter::getFeedback() const { return feedback_; }
|
||||
|
||||
void Filter::setSampleRate(float newSampleRate) {
|
||||
sampleRate_ = newSampleRate;
|
||||
recompute();
|
||||
}
|
||||
float Filter::getSampleRate() const { return sampleRate_; }
|
||||
33
core/Filter.hpp
Normal file
33
core/Filter.hpp
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#ifndef FILTER_HPP_INCLUDED
|
||||
#define FILTER_HPP_INCLUDED
|
||||
#ifndef M_PI
|
||||
#define M_PI 3.14159265358979323846
|
||||
#endif
|
||||
#include <cstdint>
|
||||
|
||||
class Filter {
|
||||
public:
|
||||
Filter(float initCutOff = 1.0f, float initSampleRate = 48000.0f);
|
||||
void process(const float *in, float *out, uint32_t frames);
|
||||
void processStereo(const float *inLeft, const float *inRight, float *outLeft,
|
||||
float *outRight, uint32_t frames);
|
||||
|
||||
void recompute();
|
||||
void setCutOff(float newCutOff);
|
||||
float getCutOff() const;
|
||||
void setFeedback(float newFeedback);
|
||||
float getFeedback() const;
|
||||
void setSampleRate(float newSampleRate);
|
||||
float getSampleRate() const;
|
||||
|
||||
private:
|
||||
void filter(const float *in, float *out, uint32_t frames, float *z);
|
||||
float cutOff_;
|
||||
float feedback_;
|
||||
float alpha_;
|
||||
float sampleRate_;
|
||||
float zL_;
|
||||
float zR_;
|
||||
};
|
||||
|
||||
#endif // FILTER_HPP_INCLUDED
|
||||
Loading…
Add table
Add a link
Reference in a new issue