Created
April 24, 2014 13:24
-
-
Save Geri-Borbas/11254457 to your computer and use it in GitHub Desktop.
Also for http://stackoverflow.com/questions/23249034/how-to-simply-play-a-cmsamplebufferref-audio-data
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
// Get audio format description. | |
CMFormatDescriptionRef formatDescription = CMSampleBufferGetFormatDescription(sampleBuffer); | |
const AudioStreamBasicDescription format = *CMAudioFormatDescriptionGetStreamBasicDescription(formatDescription); | |
// Get sample count. | |
CMItemCount sampleCount = CMSampleBufferGetNumSamples(sampleBuffer); | |
AudioBufferList audioBufferList; | |
CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer( | |
sampleBuffer, | |
NULL, | |
&audioBufferList, | |
sizeof(audioBufferList), | |
NULL, | |
NULL, | |
kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment, | |
&sampleBuffer | |
); | |
// Enumerate buffers. | |
for (int bufferCount = 0; bufferCount < audioBufferList.mNumberBuffers; bufferCount++) | |
{ | |
SInt16 *samples = (SInt16*)audioBufferList.mBuffers[sampleCount].mData; | |
// OpenAL format. | |
ALenum alFormat; | |
if (format.mBitsPerChannel == 16) | |
{ alFormat = (format.mChannelsPerFrame > 1) ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16; } | |
if (format.mBitsPerChannel == 8) | |
{ alFormat = (format.mChannelsPerFrame > 1) ? AL_FORMAT_STEREO8 : AL_FORMAT_MONO8; } | |
static int counter; | |
counter++; | |
NSString *bufferName = [NSString stringWithFormat:@"buffer_%i", counter]; | |
ALBuffer *alBuffer = [ALBuffer bufferWithName:bufferName | |
data:samples | |
size:sampleCount * sizeof(UInt32) | |
format:alFormat | |
frequency:(ALsizei)round(format.mSampleRate)]; | |
// Retain. | |
if (alBuffer != nil) | |
[self.alBuffers addObject:alBuffer]; | |
// Keep the queue at 50 at most. | |
if (self.alBuffers.count > 50) | |
{ [self.alBuffers removeObject:self.alBuffers.firstObject]; } | |
NSLog(@"sampleCount: %li duration:%f", sampleCount, alBuffer.duration); | |
} | |
// Release the buffer when done with the samples | |
// (retained by CMSampleBufferGetAudioBufferListWithRetainedblockBuffer) | |
CFRelease(sampleBuffer); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment