Initialising AudioBuffer (for delay) correctly?


I'm working on a delay line, similar to the one in the Juce demo. However, when I start to work on interpolation, I get crazy values out of my delay buffer.

First some .h file stuff...

class DelayFeed
    AudioSampleBuffer delayBuf;
    float* delayData;
    double process(float input);

So I have an audioSampleBuffer and in the constructor, I do this:

: delayBuf(1, 44100){
    rptr = 0;
    wptr = 0;
    delayData = delayBuf.getSampleData(0);

Now if I go through this and pull out values from the delayData, I get usable results. I can even change the delay time and while there's a click, I still get good data.

The problem comes when I try to do interpolation.  I have isolated the problem to pulling the index above the lower index.

rpi = (int)floor(rptr); //integer value to use as array index
float diff = rptr - rpi;
output = delayData[rpi + 1];// should be: diff * delayData[rpi] +(1.f - diff)*delayData[rpi + 1];

I try to take the value at the index before, and the value at the index AFTER the 'interpolated index'...but that index AFTER is always garbage. I thought the delayBuf.clear() would basically initialise my data array to all 0's.


Am I just using the audio buffer fundamentally wrong? There is a more code involved and I can post more if needed.

If yall have any ideas, do let me know.


Yall already know this......

Something I thought I had tried earlier, but I guess not correctly with initialising the data....

I needed to check if the index was beyond the range of the data...I mistakenly assumed it would crash if it tried to do this.


This code solved it:

if ((rpi + 1) == numSamples){ rpi = -1; }

For ring buffers the modulo operator on the index is faster than checking for the upper bound. And always use the range 0 .. bufSize-1.

(idx = (idx + 1) % bufSize)

Ah the modulo yes - thank you for that tip!