Fixing Flanger

pull/409/head
Vincent 2 years ago
parent fd3fd0efcc
commit 992f275643
  1. 41
      src/fx_flanger.cpp
  2. 1
      src/fx_flanger.h

@ -6,7 +6,8 @@
Flanger::Flanger(float32_t sampling_rate, float32_t delay_time, float32_t frequency, float32_t depth, float32_t feedback) : Flanger::Flanger(float32_t sampling_rate, float32_t delay_time, float32_t frequency, float32_t depth, float32_t feedback) :
FXElement(sampling_rate), FXElement(sampling_rate),
MaxDelayLineSize(static_cast<unsigned>(MAX_FLANGER_DELAY * sampling_rate / 1000.0f)), MaxDelayLineSize(static_cast<unsigned>(2.0f * MAX_FLANGER_DELAY * sampling_rate / 1000.0f)),
delay_line_index_(0),
lfo_phase_(0.0f) lfo_phase_(0.0f)
{ {
this->delay_lineL_ = new float32_t[this->MaxDelayLineSize]; this->delay_lineL_ = new float32_t[this->MaxDelayLineSize];
@ -26,34 +27,34 @@ Flanger::~Flanger()
void Flanger::processSample(float32_t inL, float32_t inR, float32_t& outL, float32_t& outR) void Flanger::processSample(float32_t inL, float32_t inR, float32_t& outL, float32_t& outR)
{ {
// Modulate the delay time using the LFO static float32_t M2PI = 2.0f * PI;
float32_t delay = this->delay_time_ms_ + this->depth_ * sin(this->lfo_phase_);
// Calculate the delay line index and interpolate between samples // Calculate the delay time based on the depth and rate parameters
int index = (i - (int) (this->getSamplingRate() * delay / 1000.0f)) % this->delay_line_size_; float32_t delay = this->getDelayTime() + this->getDepth() * std::sin(this->lfo_phase_);
float32_t frac = (this->getSamplingRate() * delay / 1000.0f) - (int) (this->getSamplingRate() * delay / 1000.0f);
float32_t x1 = this->delay_lineL_[index];
float32_t x2 = this->delay_lineL_[(index + 1) % this->delay_line_size_];
float32_t sample = x1 + frac * (x2 - x1);
// Process the input sample through the flanger // Convert the delay time to samples
outL = inL + sample * this->feedback_; unsigned delay_samples = static_cast<unsigned>(delay * this->getSamplingRate() / 1000.0f);
outR = inR + sample * this->feedback_;
// Update the delay line // mix the input audio with the delayed audio and the feedback signal
this->delay_lineL_[i % this->delay_line_size_] = outL; outL = inL + this->delay_lineL_[(this->delay_line_index_ + this->delay_line_size_ - delay_samples) % this->delay_line_size_] * (1.0 - this->getFeedback());
this->delay_lineR_[i % this->delay_line_size_] = outR; outR = inR + this->delay_lineR_[(this->delay_line_index_ + this->delay_line_size_ - delay_samples) % this->delay_line_size_] * (1.0 - this->getFeedback());
// Update the delay buffer with the mixed audio and the feedback signal
this->delay_lineL_[this->delay_line_index_] = inL + outL * this->getFeedback();
this->delay_lineR_[this->delay_line_index_] = inR + outR * this->getFeedback();
this->delay_line_index_ = (this->delay_line_index_ + 1) % this->delay_line_size_;
// Update the phase of the LFO
this->lfo_phase_ += this->lfo_phase_increment_; this->lfo_phase_ += this->lfo_phase_increment_;
if(this->lfo_phase_ > 2.0 * PI) { if(this->lfo_phase_ > M2PI)
this->lfo_phase_ -= 2.0 * PI; {
this->lfo_phase_ -= M2PI;
} }
} }
void Flanger::setDelayTime(float32_t delayMS) void Flanger::setDelayTime(float32_t delayMS)
{ {
this->delay_time_ms_ = constrain(delayMS, 1.0f, 10.0f); this->delay_time_ms_ = constrain(delayMS, 1.0f, MAX_FLANGER_DELAY);
this->adjustDelayCofficients(); this->adjustDelayCofficients();
} }
@ -76,7 +77,7 @@ float32_t Flanger::getFrequency() const
void Flanger::setDepth(float32_t depth) void Flanger::setDepth(float32_t depth)
{ {
this->depth_ = constrain(depth, 0.0f, 10.0f); this->depth_ = constrain(depth, 0.0f, MAX_FLANGER_DELAY);
this->adjustDelayCofficients(); this->adjustDelayCofficients();
} }

@ -46,6 +46,7 @@ private:
inline void adjustDelayCofficients(); inline void adjustDelayCofficients();
const unsigned MaxDelayLineSize; const unsigned MaxDelayLineSize;
unsigned delay_line_index_;
unsigned delay_line_size_; unsigned delay_line_size_;
float32_t* delay_lineL_; float32_t* delay_lineL_;
float32_t* delay_lineR_; float32_t* delay_lineR_;

Loading…
Cancel
Save