Skip to content

Instantly share code, notes, and snippets.

@naotokui
Created November 7, 2011 09:55
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 naotokui/1344583 to your computer and use it in GitHub Desktop.
Save naotokui/1344583 to your computer and use it in GitHub Desktop.
CMItemCount numSamplesInBuffer = CMSampleBufferGetNumSamples(buffer);
AudioBufferList audioBufferList;
CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(
buffer,
NULL,
&audioBufferList,
sizeof(audioBufferList),
NULL,
NULL,
kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment,
&buffer
);
for (int bufferCount=0; bufferCount < audioBufferList.mNumberBuffers; bufferCount++) {
SInt16* samples = (SInt16 *)audioBufferList.mBuffers[bufferCount].mData;
for (int i=0; i < numSamplesInBuffer; i++) {
// amplitude for the sample is samples[i], assuming you have linear pcm to start with
}
}
//Release the buffer when done with the samples
//(retained by CMSampleBufferGetAudioBufferListWithRetainedblockBuffer)
CFRelease(buffer);
@rdetert
Copy link

rdetert commented Mar 23, 2012

Have you used this to alter the amplitudes? When I do something like samples[i] *= 0.5 I get very choppy results.

@TroikaTronix
Copy link

If you want CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer to always work, you need to ask it how big to make the AudioBufferList first, allocate that buffer, and then call it again to get the data. See my example code in this Stackoverflow article. https://stackoverflow.com/questions/27444000/how-do-i-call-cmsamplebuffergetaudiobufferlistwithretainedblockbuffer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment