# Initialising AudioBuffer (for delay) correctly?

**URL:** <https://forum.juce.com/t/initialising-audiobuffer-for-delay-correctly/12237>\
**Category:** Audio Plugins\
**Created:** [January 21, 2014, 12:01am UTC](https://forum.juce.com/t/initialising-audiobuffer-for-delay-correctly/12237 "2014-01-21T00:01:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![turntable](https://avatars.discourse-cdn.com/v4/letter/t/c0e974/32.png) [@turntable](https://forum.juce.com/u/turntable)\
**Post date:** [January 21, 2014, 12:01am UTC](https://forum.juce.com/t/initialising-audiobuffer-for-delay-correctly/12237/1 "2014-01-21T00:01:29Z")

</div>

Howdy

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
{
public:
    DelayFeed();
    ~DelayFeed();
    AudioSampleBuffer delayBuf;
    float* delayData;
...
    double process(float input);
```

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

```
DelayFeed::DelayFeed()
: delayBuf(1, 44100){
    rptr = 0;
    wptr = 0;
    delayBuf.clear();
    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.&nbsp; 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.**

&nbsp;

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.

Thanks

---

<div class="post-metadata">

**Author:** ![turntable](https://avatars.discourse-cdn.com/v4/letter/t/c0e974/32.png) [@turntable](https://forum.juce.com/u/turntable)\
**Post date:** [January 21, 2014, 12:26am UTC](https://forum.juce.com/t/initialising-audiobuffer-for-delay-correctly/12237/2 "2014-01-21T00:26:31Z")

</div>

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.

&nbsp;

This code solved it:

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

---

<div class="post-metadata">

**Author:** ![Peter\_Emanuel\_Roos](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/peter_emanuel_roos/32/189_2.png) [@Peter\_Emanuel\_Roos](https://forum.juce.com/u/Peter_Emanuel_Roos)\
**Post date:** [January 21, 2014, 5:34am UTC](https://forum.juce.com/t/initialising-audiobuffer-for-delay-correctly/12237/3 "2014-01-21T05:34:07Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![turntable](https://avatars.discourse-cdn.com/v4/letter/t/c0e974/32.png) [@turntable](https://forum.juce.com/u/turntable)\
**Post date:** [January 22, 2014, 4:10pm UTC](https://forum.juce.com/t/initialising-audiobuffer-for-delay-correctly/12237/4 "2014-01-22T16:10:52Z")

</div>

Ah the modulo yes - thank you for that tip!
