使用CMSampleBuffer生成Float32阵列(Float32 PCM数据)

时间:2018-06-05 07:34:49

标签: swift pcm unsafemutablepointer cmsamplebuffer audiobufferlist

我从相机获取回调,音频格式为CMSampleBuffer,但我无法将此数据转换为PCM数据。

我按照Apple copyPCMDataUnsafeMutablePointerAudioBufferList提供的文档,但最后得到的是0.0

这是我的代码:

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)")
    }
}

1 个答案:

答案 0 :(得分:0)

终于搞定了。

必须添加额外的步骤才能将AudioBufferList指针转换为AudioList指针

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)")
}

以上代码仅适用于单通道PCM数据。对于2个频道数据,请参阅以下GIST - https://gist.github.com/hotpaw2/ba815fc23b5d642705f2b1dedfaf0107