Skip to content

Instantly share code, notes, and snippets.

@Xenakios
Created June 13, 2022 20:42
Show Gist options
  • Save Xenakios/0475d72b48713b428ddbfc515000c1e9 to your computer and use it in GitHub Desktop.
Save Xenakios/0475d72b48713b428ddbfc515000c1e9 to your computer and use it in GitHub Desktop.
class SimpleDelay2
{
public:
SimpleDelay2() {}
void prepare(float samplerate, int numchans, float delaytime_ramp_dur_seconds, float maxdelaytime_seconds)
{
mDelayTime.reset(samplerate, delaytime_ramp_dur_seconds);
mWetDryMix.reset(samplerate, 0.05f); // 50 millisecond ramps for the wet dry
mFeedBackAmount.reset(samplerate, 0.05f); // likewise for feedback gain
mDelayLine.setMaximumDelayInSamples(maxdelaytime_seconds*samplerate);
juce::dsp::ProcessSpec spec;
spec.maximumBlockSize = 0; // we only use the sample by sample processing of the delay line, so this can be zero
spec.numChannels = numchans;
spec.sampleRate = samplerate;
mDelayLine.prepare(spec);
}
void processBlock(juce::AudioBuffer<float>& buffer)
{
auto ptrs = buffer.getArrayOfWritePointers();
// Might be a tiny bit more efficient to have the loops running the other way around (channels first, then samples)
// but then we'd have to add more complicated code to get the smoothed delay time, feedback gain and wetdry values
for (int i = 0; i < buffer.getNumSamples(); ++i)
{
float smootheddelaytime = mDelayTime.getNextValue();
float smoothedmix = mWetDryMix.getNextValue();
float smoothedfeedbackgain = mFeedBackAmount.getNextValue();
for (int j = 0; j < buffer.getNumChannels(); ++j)
{
float input = ptrs[j][i];
mDelayLine.pushSample(j, input + mFeedBackSignals[j] * smoothedfeedbackgain);
float output = mDelayLine.popSample(j, smootheddelaytime);
mFeedBackSignals[j] = output;
output = output * smoothedmix + input * (1.0f - smoothedmix);
ptrs[j][i] = output;
}
}
}
void setFeedbackGain(float g)
{
mFeedBackAmount.setTargetValue(g);
}
void setWetDryMix(float m)
{
mWetDryMix.setTargetValue(m);
}
void setDelayTimeSamples(float dtime)
{
mDelayTime.setTargetValue(dtime);
}
void setDelayTimeSeconds(float seconds, float samplerate)
{
mDelayTime.setTargetValue(seconds*samplerate);
}
private:
juce::dsp::DelayLine<float> mDelayLine;
float mFeedBackSignals[2] = { 0.0f,0.0f };
juce::SmoothedValue<float> mFeedBackAmount;
juce::SmoothedValue<float> mWetDryMix;
juce::SmoothedValue<float> mDelayTime;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment