Resizing and setDataToReferTo

Here’s a question …

How can I resize a buffer that has been declared with …

AudioSampleBuffer::AudioSampleBuffer (float ** dataToReferTo, numChannels, numSamples)

… and still have a directly accessible data array to refer to?

If I do the standard:

recordedBuffer->setSize(2, recordedBuffer->getNumSamples() + 300000, true, true);

Then it no longer refers to the data it was initiated with … (from the docs) … “Note that if the buffer is resized or its number of channels is changed, it will re-allocate memory internally and copy the existing data to this new area, so it will then stop directly addressing this memory.”

I can do a realloc on the array …

recordedData = (float**)juce_realloc(myLoops[loopNumber].recordedData, sizeof(myLoops[loopNumber].recordedData) + sizeof(float)*300000);

But then the juce recordedBuffer doesn’t realize the data has been resized and it throws an error …

I tried adding this after the realloc …

recordedBuffer->setDataToReferTo((float**)recordedData, 2, sizeof(recordedData));

but that doesn’t do it either.

What am I missing here … I need to have directly addressable data for my buffer, but I also need it to be dynamically resizable. Surely this is possible?

Thanks,
A

How could that be possible!? If you give the audiosamplebuffer a pointer to some random blob of memory, then how on earth could it know how to resize it? What if you give it a block of memory from the stack!? You either let the buffer create and manage the memory itself, or give it read-only access to your own block of data. You can’t have it both ways!

sure … but can’t I give it read only access to a block of data, and let it know when that block is resized?

Or at that point perhaps I just need to delete and create a new AudioSampleBuffer with it’s newly resized block ?

Well yes, you’d have to just create a new buffer.