feat: adding lfo to filter
This commit is contained in:
parent
fe594a5494
commit
d8c1c90f15
6 changed files with 130 additions and 20 deletions
|
|
@ -1,6 +1,5 @@
|
|||
#include "Filter.hpp"
|
||||
#include <cmath>
|
||||
#include <sys/types.h>
|
||||
|
||||
#ifndef M_PI
|
||||
#define M_PI 3.14159265358979323846
|
||||
|
|
@ -15,15 +14,24 @@ Filter::Filter(float initCutOff, float initQ, float initSampleRate)
|
|||
|
||||
Filter::FilterType Filter::getType() const { return type_; }
|
||||
|
||||
float Filter::clampFreq(float freq) {
|
||||
return std::fmin(std::fmax(freq, kFilterMin), kFilterMax);
|
||||
}
|
||||
|
||||
void Filter::updateCoeffs() {
|
||||
float f = (cutOff_ + modulation_) / std::fmax(sampleRate_, 1.0f);
|
||||
float totalFreq = clampFreq(cutOff_ + modulation_);
|
||||
|
||||
float f = totalFreq / std::fmax(sampleRate_, 1.0f);
|
||||
|
||||
if (f > kMaxNormalizedFrequency)
|
||||
f = kMaxNormalizedFrequency;
|
||||
if (f < 0.0001f)
|
||||
f = 0.0001f;
|
||||
|
||||
g_ = 2.0f * std::sin(M_PI * f);
|
||||
}
|
||||
|
||||
float Filter::processFrame(float input, float &lp, float &bp) const {
|
||||
|
||||
float hp = input - (bp / q_) - lp;
|
||||
|
||||
bp = bp + g_ * hp;
|
||||
|
|
@ -48,8 +56,11 @@ void Filter::process(const float *in, float *out, uint32_t frames,
|
|||
float bp = bpL_;
|
||||
|
||||
for (uint32_t i = 0; i < frames; ++i) {
|
||||
if (modulation != nullptr)
|
||||
setModulation(modulation[i]);
|
||||
if (modulation != nullptr) {
|
||||
if (std::abs(modulation[i] - modulation_) > 0.001f) {
|
||||
setModulation(modulation[i]);
|
||||
}
|
||||
}
|
||||
out[i] = processFrame(in[i], lp, bp);
|
||||
}
|
||||
|
||||
|
|
@ -64,10 +75,17 @@ void Filter::processStereo(const float *inL, const float *inR, float *outL,
|
|||
float lpR = lpR_, bpR = bpR_;
|
||||
|
||||
for (uint32_t i = 0; i < frames; ++i) {
|
||||
if (modulation != nullptr)
|
||||
setModulation(modulation[i]);
|
||||
outL[i] = processFrame(inL[i], lpL, bpL);
|
||||
outR[i] = processFrame(inR[i], lpR, bpR);
|
||||
if (modulation != nullptr) {
|
||||
if (std::abs(modulation[i] - modulation_) > 0.001f) {
|
||||
setModulation(modulation[i]);
|
||||
}
|
||||
}
|
||||
|
||||
float inValL = (inL != nullptr) ? inL[i] : 0.0f;
|
||||
float inValR = (inR != nullptr) ? inR[i] : 0.0f;
|
||||
|
||||
outL[i] = processFrame(inValL, lpL, bpL);
|
||||
outR[i] = processFrame(inValR, lpR, bpR);
|
||||
}
|
||||
|
||||
lpL_ = lpL;
|
||||
|
|
@ -77,29 +95,35 @@ void Filter::processStereo(const float *inL, const float *inR, float *outL,
|
|||
}
|
||||
|
||||
float Filter::getCutOff() const { return cutOff_; }
|
||||
|
||||
void Filter::setCutOff(float freq) {
|
||||
cutOff_ = std::fmax(freq, 1.0f);
|
||||
cutOff_ = clampFreq(freq);
|
||||
updateCoeffs();
|
||||
}
|
||||
|
||||
float Filter::getQ() const { return q_; }
|
||||
|
||||
void Filter::setQ(float q) {
|
||||
q_ = std::fmax(q, 0.1f);
|
||||
q_ = std::fmin(std::fmax(q, kQMin), kQMax);
|
||||
updateCoeffs();
|
||||
}
|
||||
|
||||
void Filter::setType(FilterType type) { type_ = type; }
|
||||
|
||||
float Filter::getSampleRate() const { return sampleRate_; }
|
||||
|
||||
void Filter::setSampleRate(float sr) {
|
||||
sampleRate_ = sr;
|
||||
updateCoeffs();
|
||||
if (sr > 0.0f) {
|
||||
sampleRate_ = sr;
|
||||
updateCoeffs();
|
||||
}
|
||||
}
|
||||
|
||||
float Filter::getModulation() const { return modulation_; }
|
||||
|
||||
void Filter::setModulation(float mod) {
|
||||
if (modulation_ == mod)
|
||||
return;
|
||||
modulation_ = mod;
|
||||
updateCoeffs();
|
||||
if (std::abs(modulation_ - mod) > 0.0001f) {
|
||||
modulation_ = mod;
|
||||
updateCoeffs();
|
||||
}
|
||||
}
|
||||
|
|
@ -4,6 +4,10 @@
|
|||
#include <cstdint>
|
||||
|
||||
constexpr float kMaxNormalizedFrequency = 0.16667f;
|
||||
constexpr float kFilterMin = 20.0f;
|
||||
constexpr float kFilterMax = 10000.0f;
|
||||
constexpr float kQMin = 0.1f;
|
||||
constexpr float kQMax = 20.0f;
|
||||
|
||||
class Filter {
|
||||
public:
|
||||
|
|
@ -34,6 +38,7 @@ private:
|
|||
void updateCoeffs();
|
||||
float modulation_;
|
||||
|
||||
float clampFreq(float freq);
|
||||
float processFrame(float input, float &lp, float &bp) const;
|
||||
|
||||
float cutOff_;
|
||||
|
|
|
|||
|
|
@ -22,6 +22,8 @@ void Oscillator::process(float *out, uint32_t frames) {
|
|||
}
|
||||
}
|
||||
|
||||
void Oscillator::reset() { phase_ = 0; }
|
||||
|
||||
void Oscillator::recalc_() { inc_ = freq_ / sampleRate_; }
|
||||
|
||||
void Oscillator::setFrequency(float newFreq) {
|
||||
|
|
|
|||
|
|
@ -26,6 +26,8 @@ public:
|
|||
void setWaveShape(WaveShape shape);
|
||||
WaveShape getWaveShape() const;
|
||||
|
||||
void reset();
|
||||
|
||||
private:
|
||||
float freq_;
|
||||
float gain_;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue