本文介绍了通过AVAssetReader读取音频样本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何通过AVAssetReader阅读音频样本?我发现使用AVAssetReader复制或混合的示例,但这些循环总是由AVAssetWriter循环控制。是否可以创建一个AVAssetReader并读取它,获取每个样本并将每个音频样本的int32放入数组?
谢谢。要扩展@ amrox的答案,你可以从CMBlockBufferRef中获取一个AudioBufferList,例如
pre>
CMItemCount numSamplesInBuffer = CMSampleBufferGetNumSamples(buffer);
AudioBufferList audioBufferList;
CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(
buffer,
NULL,
& audioBufferList,
sizeof(audioBufferList),
NULL,
NULL ,
kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment,
& buffer
);
for(int bufferCount = 0; bufferCount SInt16 * samples =(SInt16 *)audioBufferList.mBuffers [bufferCount] .mData;
for(int i = 0; i //样本的幅度是samples [i],假设您有线性pcm,以
开头
}
//完成样例后释放缓冲区
//(由CMSampleBufferGetAudioBufferListWithRetainedblockBuffer保留)
CFRelease(buffer);
How do you read audio samples via AVAssetReader? I've found examples of duplicating or mixing using AVAssetReader, but those loops are always controlled by the AVAssetWriter loop. Is it possible just to create an AVAssetReader and read through it, getting each sample and throwing the int32 of each audio sample into an array?
Thanks.
解决方案
To expand on @amrox's answer, you can get an AudioBufferList from the CMBlockBufferRef, e.g.
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);
这篇关于通过AVAssetReader读取音频样本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!