Audio buffer read pointer = -1?

audio
#1

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?

0 Likes

#2

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

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

0 Likes

#3

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

0 Likes

#4

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.

1 Like

#6

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?

0 Likes

#7

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.

0 Likes

#8

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

0 Likes