Skip to content

Instantly share code, notes, and snippets.

@Geri-Borbas
Created April 24, 2014 13:24
Show Gist options
  • Save Geri-Borbas/11254457 to your computer and use it in GitHub Desktop.
Save Geri-Borbas/11254457 to your computer and use it in GitHub Desktop.
// 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