De-interleaved stereo and AudioBuffers

For the version 2 of bcAnalyze I am digging much deeper into CoreAudio to be able to do some of the things I have planned.

The first that bcAnalyze does when it encounters an AudioFile is reading its data into an internal SInt16 buffer. ExtAudioFileRead works well for that if the file format is wav, mp3, aiff or one of the other known formats. While version 1 of bcAnalyze asks the user which channel to read, I decided to overcome this limitation. Still, the UI should only display and work with one channel, but the user should be able to switch between left/right for display and such.

Simple solution, tell ExtAudioFileRead to read the stereo data into two buffers, one for each channel. Setting the proper client format was simple. For my purpose the internal format / client formatFlag is set:

clientFormat.mFormatFlags = (!kAudioFormatFlagIsBigEndian | kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked | kAudioFormatFlagIsNonInterleaved);

Then, whenever I tried to read a stereo file, ExtAudioFileRead coughed up a memory access error. Worked well with mono files, though. In the end, I had to malloc memory for the additional buffer in case I encounter stereo files. Since that wasn't that obvious to me, here is how to proceed:

AudioBufferList *fillBufList = (AudioBufferList*)malloc(sizeof(*fillBufList) + (([self channelCount]-1) * sizeof(AudioBuffer)));

fillBufList->mNumberBuffers = [self channelCount];



With that code ExtAudioFileRead produces data of each channel in a separate buffer. Charming.

Beliebte Posts aus diesem Blog

EEG, Grüne Energie und die Folgen

Swift 2, AudioConverter, Callback and Core Audio

Aussagekraft der 7-Tage-Inzidenz