Some things I noticed:
I think you would rather want to cycle one buffer size instead of the delayLength:
if (delayReadPos < 0)
delayReadPos += delayBuffer.getNumSamples();
You are calculating that value at the beginning anyway, so this line can go, and you can define that value inside the scope
You initialise the value just to overwrite it in the next line. Not a biggie, but pointless…
But I think the real problem comes, that you increment your delayWritePos for each channel, so it will create a mess…
You should probably better have a local variable and update the persistent one after the loop:
const auto numSamples = buffer.getNumSamples(); // Creating an integer for the number of samples in the buffer.
for (int channel = 0; channel < totalNumInputChannels; ++channel) // Looping through the channels.
float* channelData = buffer.getWritePointer(channel); // Pointer to the incoming signal.
float* delayData = delayBuffer.getWritePointer(channel); // Pointer to the delay buffer.
auto writePos = delayWritePos;
auto readPos = writePos - static_cast<int> (((*delayLength) * getSampleRate()));
if (readPos < 0)
readPos += delayBuffer.getNumSamples();
for (int i = 0; i < numSamples; ++i) // Cycling through the samples in the buffer.
const auto in = channelData[i];
float out = (((*dryMix) * in) + ((*wetMix) * delayData [readPos]));
delayData [writePos] = in + (delayData [readPos] * (*feedback)); // Writing the input plus any feedback into the delay buffer.
channelData[i] = out;
if (++readPos >= delayBufferLength) //If the read position exceeds the size of the buffer it returns to 0, creating a circular buffer.
readPos = 0;
if (++writePos >= delayBufferLength) //If the write position exceeds the size of the buffer it returns to 0, creating a circular buffer.
writePos = 0;
delayWritePos += numSamples;
if (delayWritePos > delayBuffer.getNumSamples())
delayWritePos -= delayBuffer.getNumSamples();
I haven’t tested code…
Further reading for alternative methods:
AudioBuffer::copyFrom(): Copy many samples at once (and with less CPU cycles as well)
AbstractFifo: How to implement a circular buffer copying data block wise
It becomes a bit more tricky then, what to store to implement the feedback properly though…