Skip to content

Instantly share code, notes, and snippets.

@Xenakios
Created June 13, 2022 19:39
Show Gist options
  • Save Xenakios/13324fe71f7c606cd3345ee786b8f6bd to your computer and use it in GitHub Desktop.
Save Xenakios/13324fe71f7c606cd3345ee786b8f6bd to your computer and use it in GitHub Desktop.
class SimpleDelay
{
public:
SimpleDelay(int maxchans, int maxdelaytimesamples)
{
maxchans = juce::jlimit(1, 2, maxchans);
mDelayBuffer.setSize(maxchans, maxdelaytimesamples);
mDelayBuffer.clear();
}
float processSample(int chan, float input)
{
auto ptrs = mDelayBuffer.getArrayOfWritePointers();
int& writepos = mWritePos[chan];
ptrs[chan][writepos] = input + mFeedBackSignals[chan] * mFeedBackAmount;
++writepos;
if (writepos == mDelayBuffer.getNumSamples())
writepos = 0;
int& readpos = mReadPos[chan];
float outsample = ptrs[chan][readpos];
++readpos;
if (readpos == mDelayBuffer.getNumSamples())
readpos = 0;
mFeedBackSignals[chan] = outsample;
return outsample * mWetDryMix + input * (1.0f - mWetDryMix);
}
void processBlock(juce::AudioBuffer<float>& buffer)
{
auto ptrs = buffer.getArrayOfWritePointers();
for (int i = 0; i < buffer.getNumChannels(); ++i)
{
for (int j = 0; j < buffer.getNumSamples(); ++j)
{
ptrs[i][j] = processSample(i, ptrs[i][j]);
}
}
}
void setFeedbackGain(float g)
{
mFeedBackAmount = g;
}
void setWetDryMix(float m)
{
mWetDryMix = m;
}
void setDelayTimeSamples(int dtime)
{
if (mDelayTimes[0] == dtime)
return;
// super ugly sounding way to do this...can obviously be improved, but left as an exercise for the reader
// could really just use the Juce dsp::DelayLine anyway...
dtime = juce::jlimit(2, mDelayBuffer.getNumSamples() - 1, dtime);
mDelayTimes[0] = dtime;
mWritePos[0] = dtime;
mWritePos[1] = dtime;
mReadPos[0] = 0;
mReadPos[1] = 0;
}
private:
juce::AudioBuffer<float> mDelayBuffer;
int mWritePos[2] = { 22050,22050 };
int mReadPos[2] = { 0,0 };
float mFeedBackSignals[2] = { 0.0f,0.0f };
float mFeedBackAmount = 0.0f;
float mWetDryMix = 0.5f;
int mDelayTimes[2] = { 22050,22050 };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment