Created
February 19, 2013 10:51
-
-
Save anonymous/4984847 to your computer and use it in GitHub Desktop.
Getting separate buffers of float data from CoreAudio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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