AVAudioBuffer and AVAudioFormat, setting interleaved to true produces zero value sample data

Viewed 268

I have a Cocoa app that uses AVAudioBuffer and AVAudioFormat to read sample data from an audio file. However I am confused about the interleaved property of AVAudioFormat.

Here's the code (Xamarin, but that should not be relevant). _file is an instance of AVAudioFile.

using (var format = new AVAudioFormat(AVAudioCommonFormat.PCMFloat32, _file.FileFormat.SampleRate, channelCount, interleaved: true))
using (var buffer = new AVAudioPcmBuffer(format, checked((uint)frameCount)))
{
    _file.ReadIntoBuffer(buffer, out var error);

    Span<float> span;
    unsafe
    {
        var bufferPointer = (float*)Marshal.ReadIntPtr(buffer.FloatChannelData);
        span = new Span<float>(bufferPointer, (int)(frameCount * channelCount));
    }

    return span.ToArray();
}

So the critical part of this is the interleaved value passed to the constructor of AVAudioFormat. When it is true (as above), the FloatChannelData in the buffer is all zeros. When it is false, the sample data is present as expected.

I am confused as I assumed that the interleaved value specified the required format of the data in the buffer. My attempts to locate documentation that explains its role have not been successful.

Can someone please explain the role of interleaved, how I should decide the value to use, and why, when it is true, the sample data is all zero.

There is clearly some learning to do here, so if you can point me to some official (or otherwise) documentation that explains this I'd be grateful.

0 Answers
Related