[Bug report] AudioBuffer makeCopyOf


#1

Hi,

I’d like to report a bug concerning AudioBuffer’s makeCopyOf method.
When you copy a non-clear buffer (secondBuff) to a cleared buffer (firstBuff), the “isClear” flag of first buffer is still true even though it contains new samples from the secondBuffer. Because of this, you can’t call clear() on the first buffer aferwards.

Test case:

AudioBuffer<float> firstBuff(1, 1);
AudioBuffer<float> secondBuff(1, 1);

firstBuff.clear();

float* secondBuffSamples = secondBuff.getWritePointer(0);
secondBuffSamples[0] = 42;

firstBuff.makeCopyOf(secondBuff);	

Actual result:

firstBuff.hasBeenCleared(); returns true
firstBuff.getSample(0,0); returns 42
firstBuff.clear();
firstBuff.getSample(0,0); returns 42

Expected result:

firstBuff.hasBeenCleared(); returns false
firstBuff.getSample(0,0); returns 42
firstBuff.clear();
firstBuff.getSample(0,0); returns 0

Solution:

modules/juce_audio_basics/buffers/juce_AudioSampleBuffer.h#L451 could be changed from

Type* const dest = channels[chan];

to

Type* const dest = getWritePointer(chan);

which sets the isClear to false.

I’m using JUCE version 4.3.1.


#2

Thanks for the bug report. A fix for this is on the develop branch of JUCE.


#3

Seems like something unit-testable…