# Audio buffer read pointer = -1?

**URL:** <https://forum.juce.com/t/audio-buffer-read-pointer-1/32220>\
**Category:** Development\
**Created:** [March 11, 2019, 9:26pm UTC](https://forum.juce.com/t/audio-buffer-read-pointer-1/32220 "2019-03-11T21:26:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![harry97uk](https://avatars.discourse-cdn.com/v4/letter/h/a8b319/32.png) [@harry97uk](https://forum.juce.com/u/harry97uk)\
**Post date:** [March 11, 2019, 9:26pm UTC](https://forum.juce.com/t/audio-buffer-read-pointer-1/32220/1 "2019-03-11T21:26:27Z")

</div>

Does anyone know why \*buffer.getReadPointer(0) would = -1? Trying to debug a problem and this is the recurring theme. How do I make it not equal -1?

---

<div class="post-metadata">

**Author:** ![alassandro](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/alassandro/32/4297_2.png) [@alassandro](https://forum.juce.com/u/alassandro)\
**Post date:** [March 11, 2019, 9:33pm UTC](https://forum.juce.com/t/audio-buffer-read-pointer-1/32220/2 "2019-03-11T21:33:05Z")

</div>

You don’t mean the actual pointer is `-1`, right? The title seems to suggest that but then:

> [@harry97uk](#):
>
> \*buffer.getReadPointer(0)

De-referencing the pointer from `getReadPointer(0)` will return the the first float sample in channel `0`. What value are you expecting?

---

<div class="post-metadata">

**Author:** ![harry97uk](https://avatars.discourse-cdn.com/v4/letter/h/a8b319/32.png) [@harry97uk](https://forum.juce.com/u/harry97uk)\
**Post date:** [March 12, 2019, 12:43pm UTC](https://forum.juce.com/t/audio-buffer-read-pointer-1/32220/3 "2019-03-12T12:43:08Z")

</div>

I have a buffer that contains all the samples from an audio file and I’m trying to read them to the getNextAudioBlock buffer like this:  
`bufferToFill.buffer->copyFrom(0, bufferToFill.startSample, buffer, 0, *buffer.getReadPointer(0), bufferToFill.numSamples);`

This works fine until I try to run two different Audio Source objects and it crashes. When I try to debug \*buffer.getReadPointer(0) = -1 is the problem.

Sorry if this is a bit vague, not sure if this is the right method

---

<div class="post-metadata">

**Author:** ![asimilon](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/asimilon/32/4235_2.png) [@asimilon](https://forum.juce.com/u/asimilon)\
**Post date:** [March 12, 2019, 1:08pm UTC](https://forum.juce.com/t/audio-buffer-read-pointer-1/32220/4 "2019-03-12T13:08:57Z")

</div>

[https://docs.juce.com/master/classAudioBuffer.html#a9ec751bfa23564c011bf3940ca17b743](https://docs.juce.com/master/classAudioBuffer.html#a9ec751bfa23564c011bf3940ca17b743)

5th argument to `copyFrom` is `int sourceStartSample`. What’s happening with `*buffer.getReadPointer(0)` is you’re dereferencing the first float in `buffer` channel 0, which happens to be equivalent to -1 as an integer, and `copyFrom` doesn’t like negative indexes it seems.

---

<div class="post-metadata">

**Author:** ![harry97uk](https://avatars.discourse-cdn.com/v4/letter/h/a8b319/32.png) [@harry97uk](https://forum.juce.com/u/harry97uk)\
**Post date:** [March 12, 2019, 1:19pm UTC](https://forum.juce.com/t/audio-buffer-read-pointer-1/32220/6 "2019-03-12T13:19:39Z")

</div>

Ok thank you, I understand. I don’t suppose you know how else I would find the sample offset from that buffer? Or is copyFrom not good to use?

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [March 12, 2019, 1:25pm UTC](https://forum.juce.com/t/audio-buffer-read-pointer-1/32220/7 "2019-03-12T13:25:00Z")

</div>

The sample offset is just an integer into the position within the buffer. It’s up to you to figure out how to keep track of the position. Probably in your case you should have a member variable for the position and increment that the same amount as what the output buffer length is after you’ve done the audio data copying.

---

<div class="post-metadata">

**Author:** ![harry97uk](https://avatars.discourse-cdn.com/v4/letter/h/a8b319/32.png) [@harry97uk](https://forum.juce.com/u/harry97uk)\
**Post date:** [March 12, 2019, 1:29pm UTC](https://forum.juce.com/t/audio-buffer-read-pointer-1/32220/8 "2019-03-12T13:29:43Z")

</div>

I had a playhead variable that kept track of what the current sample was. Put that in and I heard total silence. Now I have put 0 as the sourceStartSample for both channels and it works perfectly. I’m sure there will be something wrong so I’ll keep looking
