在 AudioBufferList 中分配缓冲区,仅包含 inNumberFrames 信息 iOS



我有一个记录器回调(kAudioOutputUnit_SetInputCallback),其中我需要在 AudioBufferList 中分配缓冲区,其中只有一个回调返回的 informatin-inNUmberFrames。我该如何为此进行 malloc?如何仅使用帧数来确定有多少数据可用?

OSStatus AudioCapturer::recordingCallback(void *inRefCon, 
                              AudioUnitRenderActionFlags *ioActionFlags, 
                              const AudioTimeStamp *inTimeStamp, 
                              UInt32 inBusNumber, 
                              UInt32 inNumberFrames, 
                              AudioBufferList *ioData) {
AudioCapturer *capturer=(AudioCapturer * )inRefCon;
    AudioBuffer buffer;
    buffer.mNumberChannels = 1;
    buffer.mDataByteSize = inNumberFrames * 2;
    buffer.mData = malloc( inNumberFrames * 2 );
    // Put buffer in a AudioBufferList that is to be filled in the AudioUnitRender 
    AudioBufferList bufferList;
    bufferList.mNumberBuffers = 1;
    bufferList.mBuffers[0] = buffer;
    // Then:
    // Obtain recorded samples
    OSStatus status;
    status = AudioUnitRender(capturer->m_audioUnit, 
                             ioActionFlags, 
                             inTimeStamp, 
                             inBusNumber, 
                             inNumberFrames, 
                             &bufferList);
    capturer->checkStatus(status);

    //use the bufferlist data and free the allocated buffer
    free(bufferList.mBuffers[0].mData);
 return noErr;
}

相关内容

  • 没有找到相关文章

最新更新