Skip to content

Instantly share code, notes, and snippets.

Created February 19, 2013 10:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4984847 to your computer and use it in GitHub Desktop.
Save anonymous/4984847 to your computer and use it in GitHub Desktop.
Getting separate buffers of float data from CoreAudio
// Setup the output asbd
_outputFormat.mFormatID = kAudioFormatLinearPCM;
_outputFormat.mFormatFlags = kAudioFormatFlagsAudioUnitCanonical; // kAudioFormatFlagIsFloat | kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked | kAudioFormatFlagIsNonInterleaved
_outputFormat.mSampleRate = inFormat.mSampleRate;
_outputFormat.mChannelsPerFrame = inFormat.mChannelsPerFrame;
_outputFormat.mFramesPerPacket = 1;
_outputFormat.mBytesPerFrame = 4;
_outputFormat.mBytesPerPacket = 4;
_outputFormat.mBitsPerChannel = 32;
// Create enough AudioBuffers for our data.
// AudioBufferList only defines 1 buffer in it so we have to allocate the other ones.
AudioBufferList *bufferList = (AudioBufferList*)malloc(sizeof(AudioBufferList) + (sizeof(AudioBuffer) * (_outputFormat.mChannelsPerFrame - 1)));
bufferList->mNumberBuffers = _outputFormat.mChannelsPerFrame;
for ( int i=0; i < bufferList->mNumberBuffers; i++ ) {
bufferList->mBuffers[i].mNumberChannels = 1;
bufferList->mBuffers[i].mDataByteSize = BUFFER_SIZE * sizeof(float);
bufferList->mBuffers[i].mData = malloc(BUFFER_SIZE * sizeof(float));
}
while (1) {
OSStatus status;
UInt32 frameCount = BUFFER_SIZE / sizeof(float);
status = ExtAudioFileRead(_fileRef, &frameCount, bufferList);
if (check_status_is_error(status, "ExtAudioFileRead")) {
_error = make_error(status, "ExtAudioFileRead");
break;
}
if (frameCount == 0) {
break;
}
for (i = 0; i < _outputFormat.mChannelsPerFrame; i++) {
// Do something with the data in bufferList->mBuffers[i].mData
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment