feat(filter): Add modulation support and LFO integration
This commit is contained in:
parent
3c3fec9b90
commit
fe594a5494
6 changed files with 131 additions and 20 deletions
|
|
@ -1,21 +1,22 @@
|
|||
#include "Filter.hpp"
|
||||
#include <cmath>
|
||||
#include <sys/types.h>
|
||||
|
||||
#ifndef M_PI
|
||||
#define M_PI 3.14159265358979323846
|
||||
#endif
|
||||
|
||||
Filter::Filter(float initCutOff, float initQ, float initSampleRate)
|
||||
: cutOff_(initCutOff), q_(initQ), sampleRate_(initSampleRate),
|
||||
type_(kLowPass), g_(0.0f), lpL_(0.0f), bpL_(0.0f), lpR_(0.0f),
|
||||
bpR_(0.0f) {
|
||||
: modulation_(0.0f), cutOff_(initCutOff), q_(initQ),
|
||||
sampleRate_(initSampleRate), type_(kLowPass), g_(0.0f), lpL_(0.0f),
|
||||
bpL_(0.0f), lpR_(0.0f), bpR_(0.0f) {
|
||||
updateCoeffs();
|
||||
}
|
||||
|
||||
Filter::FilterType Filter::getType() const { return type_; }
|
||||
|
||||
void Filter::updateCoeffs() {
|
||||
float f = cutOff_ / std::fmax(sampleRate_, 1.0f);
|
||||
float f = (cutOff_ + modulation_) / std::fmax(sampleRate_, 1.0f);
|
||||
if (f > kMaxNormalizedFrequency)
|
||||
f = kMaxNormalizedFrequency;
|
||||
g_ = 2.0f * std::sin(M_PI * f);
|
||||
|
|
@ -41,11 +42,14 @@ float Filter::processFrame(float input, float &lp, float &bp) const {
|
|||
}
|
||||
}
|
||||
|
||||
void Filter::process(const float *in, float *out, uint32_t frames) {
|
||||
void Filter::process(const float *in, float *out, uint32_t frames,
|
||||
const float *modulation) {
|
||||
float lp = lpL_;
|
||||
float bp = bpL_;
|
||||
|
||||
for (uint32_t i = 0; i < frames; ++i) {
|
||||
if (modulation != nullptr)
|
||||
setModulation(modulation[i]);
|
||||
out[i] = processFrame(in[i], lp, bp);
|
||||
}
|
||||
|
||||
|
|
@ -54,11 +58,14 @@ void Filter::process(const float *in, float *out, uint32_t frames) {
|
|||
}
|
||||
|
||||
void Filter::processStereo(const float *inL, const float *inR, float *outL,
|
||||
float *outR, uint32_t frames) {
|
||||
float *outR, uint32_t frames,
|
||||
const float *modulation) {
|
||||
float lpL = lpL_, bpL = bpL_;
|
||||
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);
|
||||
}
|
||||
|
|
@ -88,3 +95,11 @@ void Filter::setSampleRate(float sr) {
|
|||
sampleRate_ = sr;
|
||||
updateCoeffs();
|
||||
}
|
||||
|
||||
float Filter::getModulation() const { return modulation_; }
|
||||
void Filter::setModulation(float mod) {
|
||||
if (modulation_ == mod)
|
||||
return;
|
||||
modulation_ = mod;
|
||||
updateCoeffs();
|
||||
}
|
||||
|
|
@ -13,21 +13,26 @@ public:
|
|||
enum FilterType { kLowPass, kHighPass, kBandPass, kNotch, kFilterTypeCount };
|
||||
|
||||
void setCutOff(float freq);
|
||||
void setQ(float q);
|
||||
void setSampleRate(float sr);
|
||||
void setType(FilterType type);
|
||||
|
||||
float getCutOff() const;
|
||||
float getQ() const;
|
||||
float getSampleRate() const;
|
||||
FilterType getType() const;
|
||||
|
||||
void process(const float *in, float *out, uint32_t frames);
|
||||
void setQ(float q);
|
||||
float getQ() const;
|
||||
void setSampleRate(float sr);
|
||||
float getSampleRate() const;
|
||||
void setType(FilterType type);
|
||||
FilterType getType() const;
|
||||
void setModulation(float mod);
|
||||
float getModulation() const;
|
||||
|
||||
void process(const float *in, float *out, uint32_t frames,
|
||||
const float *modulation = nullptr);
|
||||
void processStereo(const float *inL, const float *inR, float *outL,
|
||||
float *outR, uint32_t frames);
|
||||
float *outR, uint32_t frames,
|
||||
const float *modulation = nullptr);
|
||||
|
||||
private:
|
||||
void updateCoeffs();
|
||||
float modulation_;
|
||||
|
||||
float processFrame(float input, float &lp, float &bp) const;
|
||||
|
||||
|
|
|
|||
44
core/Oscillator.cpp
Normal file
44
core/Oscillator.cpp
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
#include "Oscillator.hpp"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
Oscillator::Oscillator(float initFreq)
|
||||
: freq_(initFreq), gain_(100.0f), waveShape_(kWaveShapeSine),
|
||||
sampleRate_(48000.f), phase_(0.0f), inc_(0.0f) {
|
||||
recalc_();
|
||||
};
|
||||
|
||||
void Oscillator::process(float *out, uint32_t frames) {
|
||||
for (uint32_t i = 0; i < frames; ++i) {
|
||||
switch (waveShape_) {
|
||||
default:
|
||||
case kWaveShapeSine:
|
||||
out[i] = std::sinf(phase_ * 2.0f * M_PI) * gain_;
|
||||
break;
|
||||
}
|
||||
phase_ += inc_;
|
||||
if (phase_ >= 1.0f)
|
||||
phase_ -= 1.0f;
|
||||
}
|
||||
}
|
||||
|
||||
void Oscillator::recalc_() { inc_ = freq_ / sampleRate_; }
|
||||
|
||||
void Oscillator::setFrequency(float newFreq) {
|
||||
freq_ = std::fmax(newFreq, 0.0f);
|
||||
}
|
||||
float Oscillator::getFrequency() const { return freq_; };
|
||||
|
||||
void Oscillator::setGain(float newGain) {
|
||||
gain_ = newGain > 0 ? newGain : 1.0f;
|
||||
}
|
||||
float Oscillator::getGain() const { return gain_; };
|
||||
|
||||
void Oscillator::setSampleRate(float newSampleRate) {
|
||||
sampleRate_ = newSampleRate > 0 ? newSampleRate : 48000.0f;
|
||||
recalc_();
|
||||
}
|
||||
float Oscillator::getSampleRate() const { return sampleRate_; }
|
||||
|
||||
void Oscillator::setWaveShape(WaveShape shape) { waveShape_ = shape; }
|
||||
Oscillator::WaveShape Oscillator::getWaveShape() const { return waveShape_; }
|
||||
39
core/Oscillator.hpp
Normal file
39
core/Oscillator.hpp
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
#ifndef OSCILLATOR_HPP_INCLUDED
|
||||
#define OSCILLATOR_HPP_INCLUDED
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#ifndef M_PI
|
||||
#define M_PI 3.14159265358979323846
|
||||
#endif
|
||||
|
||||
class Oscillator {
|
||||
enum WaveShape { kWaveShapeSine, kWaveShapeTriangle, kWaveShapeSquare };
|
||||
|
||||
public:
|
||||
Oscillator(float initFreq);
|
||||
void process(float *out, uint32_t frames);
|
||||
|
||||
void setFrequency(float newFreq);
|
||||
float getFrequency() const;
|
||||
|
||||
void setGain(float newGain);
|
||||
float getGain() const;
|
||||
|
||||
void setSampleRate(float newSampleRate);
|
||||
float getSampleRate() const;
|
||||
|
||||
void setWaveShape(WaveShape shape);
|
||||
WaveShape getWaveShape() const;
|
||||
|
||||
private:
|
||||
float freq_;
|
||||
float gain_;
|
||||
WaveShape waveShape_;
|
||||
float sampleRate_;
|
||||
float phase_;
|
||||
float inc_;
|
||||
void recalc_();
|
||||
};
|
||||
|
||||
#endif // OSCILLATOR_HPP_INCLUDED
|
||||
Loading…
Add table
Add a link
Reference in a new issue