Created
October 26, 2012 08:50
-
-
Save goonzoid/3957700 to your computer and use it in GitHub Desktop.
Audio Queue Playback Callback
This file contains hidden or 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
typedef struct FCAudioPlayer { | |
AudioStreamBasicDescription asbd; | |
AudioBuffer fileBuffer; | |
UInt32 playbackPosition; | |
UInt32 totalFrames; | |
} FCAudioPlayer; | |
static void FCAudioOutputCallback(void *inUserData, AudioQueueRef inAudioQueue, AudioQueueBufferRef inCompleteAudioQueueBuffer) | |
{ | |
FCAudioPlayer *audioPlayer = (FCAudioPlayer *)inUserData; | |
UInt32 queueBufferFrameCapacity = inCompleteAudioQueueBuffer->mAudioDataBytesCapacity / audioPlayer->asbd.mBytesPerFrame; | |
UInt32 framesLeftInFile = audioPlayer->totalFrames - audioPlayer->playbackPosition; | |
UInt32 numberOfFramesToCopy = MIN(queueBufferFrameCapacity, framesLeftInFile); | |
if (numberOfFramesToCopy > 0) | |
{ | |
UInt32 sizeOfDataToCopy = numberOfFramesToCopy * audioPlayer->asbd.mBytesPerFrame; | |
UInt32 offset = audioPlayer->playbackPosition * audioPlayer->asbd.mBytesPerFrame; | |
memcpy(inCompleteAudioQueueBuffer->mAudioData, &audioPlayer->fileBuffer.mData[0] + offset, sizeOfDataToCopy); | |
inCompleteAudioQueueBuffer->mAudioDataByteSize = sizeOfDataToCopy; | |
audioPlayer->playbackPosition += numberOfFramesToCopy; | |
OSStatus anErr = AudioQueueEnqueueBuffer(inAudioQueue, inCompleteAudioQueueBuffer, 0, 0); | |
assert(anErr == noErr); | |
} | |
else | |
{ | |
OSStatus anErr = AudioQueueStop(inAudioQueue, false); | |
assert(anErr == noErr); | |
return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment