Search code examples
swiftpcmunsafemutablepointercmsamplebufferaudiobufferlist

Generating Float32 Array (Float32 PCM data) using CMSampleBuffer


I get the callbacks from camera with for audio with data in the format of CMSampleBuffer but I am unable to convert this data to PCM data.

I followed the docs provided by Apple copyPCMData, UnsafeMutablePointer, AudioBufferList but all I get is 0.0 at the end.

Here is my code:

private let pcmBufferPointer = UnsafeMutablePointer<AudioBufferList>.allocate(capacity: 1024)

init(....){
    //...
    let unsafeRawPointer = UnsafeMutableRawPointer.allocate(byteCount: 4, alignment: 0)
    let audioBuffer = AudioBuffer(mNumberChannels: 1, mDataByteSize: 4, mData: unsafeRawPointer)
    let audioBufferList = AudioBufferList(mNumberBuffers: 0, mBuffers: audioBuffer)
    self.pcmBufferPointer.initialize(repeating: audioBufferList, count: 1024)
}


//CMSampleBuffer obtained from AVCaptureAudioDataOutputSampleBufferDelegate
private func audioFrom(sampleBuffer: CMSampleBuffer) -> Void {
    let status = CMSampleBufferCopyPCMDataIntoAudioBufferList(sampleBuffer, 0, 1024, pcmBufferPointer)
    if status == 0 {
        Logger.log(key: "Audio Sample Buffer Status", message: "Buffer copied to pointer")
        let dataValue = pcmBufferPointer[0].mBuffers.mData!.load(as: Float32.self) //Tried with Int, Int16, Int32, Int64 and Float too
        Logger.log(key: "PCM Data Value", message: "Data value : \(dataValue)") //prints 0.0
    }else{
        Logger.log(key: "Audio Sample", message: "Buffer allocation failed with status \(status)")
    }
}

Solution

  • Finally got it working.

    Had to add extra step for the conversion of AudioBufferList pointer to AudioList pointer

    if status == 0 {
        let inputDataPtr = UnsafeMutableAudioBufferListPointer(pcmBufferPointer)
        let mBuffers : AudioBuffer = inputDataPtr[0]
        if let bufferPointer = UnsafeMutableRawPointer(mBuffers.mData){
            let dataPointer = bufferPointer.assumingMemoryBound(to: Int16.self)
            let dataArray = Array(UnsafeBufferPointer.init(start: dataPointer, count: 1024))
            pcmArray.append(contentsOf: dataArray)
        }else{
            Logger.log(key: "Audio Sample", message: "Failed to generate audio sample")
        }
    }else{
        Logger.log(key: "Audio Sample", message: "Buffer allocation failed with status \(status)")
    }
    

    Above code works only for single channel PCM data. For 2 channels data refer the following GIST - https://gist.github.com/hotpaw2/ba815fc23b5d642705f2b1dedfaf0107