iPhone: AudioBufferList init and release

前端 未结 2 813
猫巷女王i
猫巷女王i 2020-12-09 05:47

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 w

2条回答
  •  佛祖请我去吃肉
    2020-12-09 06:14

    Here is how I do it:

    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(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(calloc(capacityFrames, bytesPerFrame));
            bufferList->mBuffers[bufferIndex].mDataByteSize = capacityFrames * bytesPerFrame;
            bufferList->mBuffers[bufferIndex].mNumberChannels = channelsPerBuffer;
        }
    
        return bufferList;
    }
    

提交回复
热议问题