/**************************************************************************
Copyright (C) 2020 Arnaud Champenois arthelion@free.fr
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 <http://www.gnu.org/licenses/>.
**************************************************************************/
#pragma once
#include "AudioParameters.h"
#include "../JuceLibraryCode/JuceHeader.h"
#include "WaveGenerator.h"
class StereoDelay {
public:
StereoDelay(const SynthParameters¶m) :
mP(param), mLeftLine(MaxDelayTime), mRightLine(MaxDelayTime) {
}
~StereoDelay() {}
void init(double sampleRate) {
mSampleRate = sampleRate;
mLeftLine.init(sampleRate);
mRightLine.init(sampleRate);
auto coeffs = dsp::IIR::Coefficients< double >::makeLowPass(mSampleRate, 6000);
mLPFilterL.coefficients = coeffs;
mLPFilterL.reset();
mLPFilterR.coefficients = coeffs;
mLPFilterR.reset();
updateInternalValues();
}
void next(double&outL, double&outR) {
const ScopedLock slock(lock);
double valueL = mLPFilterL.processSample(outL);
double valueR = mLPFilterR.processSample(outR);
double lastL = mLeftLine.next();
double lastR = mRightLine.next();
mLeftLine.push(lastL*mFeed + valueL);
mRightLine.push(lastR*mFeed + valueR);
outL = mMix * lastL + outL;
outR = mMix * lastR + outR;
}
bool isActive() const { return mMix != 0.0; }
void updateInternalValues() {
const ScopedLock slock(lock);
double timeCoeff = 1.0;
if (*mP.mDlySync) {
timeCoeff = 60.0 / *mP.mMetronomTempo;
}
double prevLeft = mDelayLeft;
mDelayLeft = timeCoeff * (*mP.mDlyLeftTime);
if (mDelayLeft < 0.010f)
mDelayLeft = 0.010f;
if (prevLeft != mDelayLeft)
mLeftLine.setDelay(mDelayLeft);
double prevRight = mDelayRight;
mDelayRight = timeCoeff * (*mP.mDlyRightTime);
if (mDelayRight < 0.010f)
mDelayRight = 0.010f;
if (prevRight != mDelayRight)
mRightLine.setDelay(mDelayRight);
mMix = *mP.mDlyMix;
mFeed = *mP.mDlyFeed;
}
private:
const SynthParameters & mP;
juce::dsp::IIR::Filter<double> mLPFilterL;
juce::dsp::IIR::Filter<double> mLPFilterR;
double mSampleRate = 44100.0;
double mFeed = 0.2;
double mMix = 0.0;
double mDelayLeft = 0.0;
double mDelayRight = 0.0;
DelayLine mLeftLine;
DelayLine mRightLine;
CriticalSection lock;
};