From fd3fd0efcc104418479da9f68861133f5d45448f Mon Sep 17 00:00:00 2001 From: abscisys Date: Sat, 24 Dec 2022 15:17:13 +0100 Subject: [PATCH] Adding Flanger FX -> 1 point to fix as code is not compiling --- src/Makefile | 2 +- src/fx_flanger.cpp | 101 +++++++++++++++++++++++++++++++++++++++++++++ src/fx_flanger.h | 60 +++++++++++++++++++++++++++ 3 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 src/fx_flanger.cpp create mode 100644 src/fx_flanger.h diff --git a/src/Makefile b/src/Makefile index 0144f0a..385770d 100644 --- a/src/Makefile +++ b/src/Makefile @@ -10,7 +10,7 @@ OBJS = main.o kernel.o minidexed.o config.o userinterface.o uimenu.o \ mididevice.o midikeyboard.o serialmididevice.o pckeyboard.o \ sysexfileloader.o performanceconfig.o perftimer.o \ effect_compressor.o effect_platervbstereo.o \ - fx.o fx_orbitone.o fx_phaser.o fx_tape_delay.o fx_shimmer_reverb.o fx_rack.o \ + fx.o fx_flanger.o fx_orbitone.o fx_phaser.o fx_tape_delay.o fx_shimmer_reverb.o fx_rack.o \ uibuttons.o midipin.o OPTIMIZE = -O3 diff --git a/src/fx_flanger.cpp b/src/fx_flanger.cpp new file mode 100644 index 0000000..a34349b --- /dev/null +++ b/src/fx_flanger.cpp @@ -0,0 +1,101 @@ +#include "fx_flanger.h" + +#include + +#define MAX_FLANGER_DELAY 20.0f + +Flanger::Flanger(float32_t sampling_rate, float32_t delay_time, float32_t frequency, float32_t depth, float32_t feedback) : + FXElement(sampling_rate), + MaxDelayLineSize(static_cast(MAX_FLANGER_DELAY * sampling_rate / 1000.0f)), + lfo_phase_(0.0f) +{ + this->delay_lineL_ = new float32_t[this->MaxDelayLineSize]; + this->delay_lineR_ = new float32_t[this->MaxDelayLineSize]; + + this->setDelayTime(delay_time); + this->setFrequency(frequency); + this->setDepth(depth); + this->setFeedback(feedback); +} + +Flanger::~Flanger() +{ + delete[] this->delay_lineL_; + delete[] this->delay_lineR_; +} + +void Flanger::processSample(float32_t inL, float32_t inR, float32_t& outL, float32_t& outR) +{ + // Modulate the delay time using the LFO + float32_t delay = this->delay_time_ms_ + this->depth_ * sin(this->lfo_phase_); + + // Calculate the delay line index and interpolate between samples + int index = (i - (int) (this->getSamplingRate() * delay / 1000.0f)) % this->delay_line_size_; + 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 + outL = inL + sample * this->feedback_; + outR = inR + sample * this->feedback_; + + // Update the delay line + this->delay_lineL_[i % this->delay_line_size_] = outL; + this->delay_lineR_[i % this->delay_line_size_] = outR; + + // Update the phase of the LFO + this->lfo_phase_ += this->lfo_phase_increment_; + if(this->lfo_phase_ > 2.0 * PI) { + this->lfo_phase_ -= 2.0 * PI; + } +} + +void Flanger::setDelayTime(float32_t delayMS) +{ + this->delay_time_ms_ = constrain(delayMS, 1.0f, 10.0f); + this->adjustDelayCofficients(); +} + +float32_t Flanger::getDelayTime() const +{ + return this->delay_time_ms_; +} + +void Flanger::setFrequency(float32_t frequency) +{ + frequency = constrain(frequency, 0.1f, 1.0f); + this->frequency_ = frequency; + this->lfo_phase_increment_ = 2.0f * PI * frequency / this->getSamplingRate(); +} + +float32_t Flanger::getFrequency() const +{ + return this->frequency_; +} + +void Flanger::setDepth(float32_t depth) +{ + this->depth_ = constrain(depth, 0.0f, 10.0f); + this->adjustDelayCofficients(); +} + +float32_t Flanger::getDepth() const +{ + return this->depth_; +} + +void Flanger::setFeedback(float32_t feedback) +{ + this->feedback_ = constrain(feedback, 0.0f, 1.0f); +} + +float32_t Flanger::getFeedback() const +{ + return this->feedback_; +} + +void Flanger::adjustDelayCofficients() +{ + this->delay_line_size_ = static_cast(this->getSamplingRate() * (this->getDelayTime() + this->getDepth()) / 1000.0f); +} diff --git a/src/fx_flanger.h b/src/fx_flanger.h new file mode 100644 index 0000000..09a729d --- /dev/null +++ b/src/fx_flanger.h @@ -0,0 +1,60 @@ +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +// +// fx_flanger.h +// +// Stereo Flanger audio effects proposed in the context of the MiniDexed project +// +#pragma once + +#include "fx.h" + +class Flanger : public FXElement +{ + DISALLOW_COPY_AND_ASSIGN(Flanger); + +public: + Flanger(float32_t sampling_rate, float32_t delay_time = 5.0f, float32_t frequency = 0.5f, float32_t depth = 1.0f, float32_t feedback = 0.5f); + virtual ~Flanger(); + + virtual void processSample(float32_t inL, float32_t inR, float32_t& outL, float32_t& outR) override; + + void setDelayTime(float32_t delayMS); + inline float32_t getDelayTime() const; + + void setFrequency(float32_t frequency); + inline float32_t getFrequency() const; + + void setDepth(float32_t depth); + inline float32_t getDepth() const; + + void setFeedback(float32_t feedback); + inline float32_t getFeedback() const; + +private: + inline void adjustDelayCofficients(); + + const unsigned MaxDelayLineSize; + unsigned delay_line_size_; + float32_t* delay_lineL_; + float32_t* delay_lineR_; + + float32_t lfo_phase_; + float32_t lfo_phase_increment_; + + float32_t delay_time_ms_; // Delay time in milliseconds + float32_t frequency_; // LFO frequency in HZ (0.1 - 10.0) + float32_t depth_; // Depth of the flanger effect in milliseconds + float32_t feedback_; // Amount of feedback to apply to the delay line +}; \ No newline at end of file