本文介绍了iPhone:AudioBufferList初始化和发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用3个AudioBuffers初始化(分配内存)和释放(释放)AudioBufferList的正确方法是什么? (我知道可能有多种方法可以做到这一点。)
What are the correct ways of initializing (allocating memory) and releasing (freeing) an AudioBufferList with 3 AudioBuffers? (I'm aware that there might be more than one ways of doing this.)
我想用这3个缓冲区来读取音频文件的连续部分进入它们并使用音频单元播放它们。
I'd like to use those 3 buffers to read sequential parts of an audio file into them and play them back using Audio Units.
推荐答案
我是这样做的:
AudioBufferList *
AllocateABL(UInt32 channelsPerFrame, UInt32 bytesPerFrame, bool interleaved, UInt32 capacityFrames)
{
AudioBufferList *bufferList = NULL;
UInt32 numBuffers = interleaved ? 1 : channelsPerFrame;
UInt32 channelsPerBuffer = interleaved ? channelsPerFrame : 1;
bufferList = static_cast<AudioBufferList *>(calloc(1, offsetof(AudioBufferList, mBuffers) + (sizeof(AudioBuffer) * numBuffers)));
bufferList->mNumberBuffers = numBuffers;
for(UInt32 bufferIndex = 0; bufferIndex < bufferList->mNumberBuffers; ++bufferIndex) {
bufferList->mBuffers[bufferIndex].mData = static_cast<void *>(calloc(capacityFrames, bytesPerFrame));
bufferList->mBuffers[bufferIndex].mDataByteSize = capacityFrames * bytesPerFrame;
bufferList->mBuffers[bufferIndex].mNumberChannels = channelsPerBuffer;
}
return bufferList;
}
这篇关于iPhone:AudioBufferList初始化和发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!