Taking a reference from some AudioBuffer channels without copying

I’d like to take the firsts channels from an AudioBuffer in another one without copying them, is this possible? or will I do something wrong?

AudioSampleBuffer ab {12, 1024};
AudioSampleBuffer section {ab.getArrayOfWritePointers(), 2, 512, 128};

Or using AudioBuffer::setDataToReferTo() (but I think it’s the same)

Edit: I know the AudioBuffer description states that the number of channel must be the same that its source, but maybe it’s still possible since I’m using only the first ones

In case anyone else came here looking for a similar thing, the way I solved this is to create an array of pointers to the channels I want to refer to.

For example to refer to channels 2 and 3 from a 4 channel buffer:

float *channels[2] = { buffer.getWritePointer( 2 ), buffer.getWritePointer( 3 ) };
AudioSampleBuffer buffer( channels, 2, 0, buffer.getNumSamples() );

Obviously, you could adapt this for read-only etc.

I know it’s an old post but since it got resurrected…

yes, that is absolutely possible. You just need to adapt the array and possibly reorder the pointers to you needs (like @jamiebullock described above).

In general if you can, change the interface to juce::dsp::AudioBuffer. That makes it easier:

AudioSampleBuffer ab {12, 1024};
auto section = juce::dsp::AudioBlock (ab).getSubBlock (512, 128);

juce::dsp::AudioBlock doesn’t own sample data hence it doesn’t allocate and you can copy it around as you like it (with one exception, if you construct it giving it a juce::HeapBlock).

1 Like

Thanks. It’s an old post, but it was unreplied. You saved me some CPU cycles! :slight_smile: