Can't copy buffer in processBlock


#1

Hi all, 

 

This simple code in processBlock produces silence :


    AudioSampleBuffer in(buffer);
    in.copyFrom(0,0,buffer,0,0,buffer.getNumSamples());
    in.copyFrom(1,0,buffer,1,0,buffer.getNumSamples());
    buffer.clear();
    buffer.copyFrom(0,0,in,0,0,in.getNumSamples());
    buffer.copyFrom(1,0,in,1,0,in.getNumSamples());
 

If I remove buffer.clear();, the output is as expected. Any clues ?


#2

From the documentation on the AudioSampleBuffer copy constructor:

Copies another buffer.

This buffer will make its own copy of the other's data, unless the buffer was created using an external data buffer, in which case boths buffers will just point to the same shared block of data.

It is indeed a bit unfortunate (I ran into the same issue once) that when you don't know how the buffer to be copied from was created, you cannot know what the constructor does (own copy of data or refer to the same data). To be safe, always create the new buffer blank, with the same number of channels and samples as the original, and then copy the data.


#3

Thanks for your prompt reply

I thought of that but normally when I modify in, buffer should be modified because they're shared ? And anyway the 2 first copyFrom should handle that.

But anyway, I'm gonna try that ! Thanks


#4

...and it works.