Allocate buffers in AudioBufferList with only inNumberFrames information iOS

1.8k views Asked by At

I have a recorder callback(kAudioOutputUnit_SetInputCallback) in which I need to allocate buffers in AudioBufferList with only one informatin-inNUmberFrames that the callback returns. How do I malloc for this? How can I determine how much data is available using only the number of frames?

1

There are 1 answers

1
Saraswati On
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;
}