Created
November 7, 2011 09:55
-
-
Save naotokui/1344583 to your computer and use it in GitHub Desktop.
Reading audio samples via AVAssetReader from http://stackoverflow.com/questions/4972677/reading-audio-samples-via-avassetreader
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
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); |
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
Have you used this to alter the amplitudes? When I do something like samples[i] *= 0.5 I get very choppy results.