iPhone:AudioBufferList初始化和发布

时间:2010-09-22 08:30:57

标签: iphone ios core-audio audiounit

使用3个AudioBuffers初始化(分配内存)和释放(释放)AudioBufferList的正确方法是什么? (我知道可能有多种方法可以做到这一点。)

我想使用这3个缓冲区来读取音频文件的连续部分并使用音频单元播放它们。

2 个答案:

答案 0 :(得分:16)

我是这样做的:

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;
}

答案 1 :(得分:12)

首先,我认为你实际上需要3个AudioBufferLists,而不是一个带有3个AudioBuffer成员的AudioBufferList。 AudioBuffer表示单个数据通道,因此如果您有3个立体声音频文件,则应将它们放在3个AudioBufferLists中,每个列表包含2个AudioBuffers,一个缓冲区用于左声道,一个缓冲区用于右声道。然后,您的代码将分别处理每个列表(及其各自的通道数据),您可以将列表存储在NSArray或类似的内容中。

从技术上讲,没有理由你不能拥有一个带有3个交错音频通道的缓冲区列表(意味着左右通道都存储在单个数据缓冲区中),但这与传统的API会有点令人困惑。

无论如何,CoreAudio API的这部分比Objective-C-ish更加C-ish,所以你使用malloc / free而不是alloc / release。代码看起来像这样:

#define kNumChannels 2
AudioBufferList *bufferList = (AudioBufferList*)malloc(sizeof(AudioBufferList) * kNumChannels);
bufferList->mNumberBuffers = kNumChannels; // 2 for stereo, 1 for mono
for(int i = 0; i < 2; i++) {
  int numSamples = 123456; // Number of sample frames in the buffer
  bufferList->mBuffers[i].mNumberChannels = 1;
  bufferList->mBuffers[i].mDataByteSize = numSamples * sizeof(Float32);
  bufferList->mBuffers[i].mData = (Float32*)malloc(sizeof(Float32) * numSamples);
}

// Do stuff...

for(int i = 0; i < 2; i++) {
  free(bufferList->mBuffers[i].mData);
}
free(bufferList);

上面的代码假设您正在将数据作为浮点读取。如果你没有对文件进行任何特殊处理,那么以SInt16(原始PCM数据)读取它们会更有效率,因为iPhone没有FPU。

此外,如果您没有在单个方法之外使用列表,那么通过将它们声明为常规对象而不是指针,将它们分配到堆栈而不是堆上更有意义。你仍然需要malloc()AudioBuffer的实际mData成员,但至少你不必担心free()实际的AudioBufferList本身。