我正在尝试从CMSampleBuffer
访问样本以进行进一步处理(主要是可视化),这是我的代码:
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
var audioBufferList = AudioBufferList()
var blockBuffer : CMBlockBuffer?
CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampleBuffer, nil, &audioBufferList, MemoryLayout<AudioBufferList>.size, nil, nil, 0, &blockBuffer)
let buffers = UnsafeBufferPointer<AudioBuffer>(start: &audioBufferList.mBuffers, count: Int(audioBufferList.mNumberBuffers))
var data = audioBufferList.mBuffers.mData
let pointer = data?.assumingMemoryBound(to: Float.self)
let floatPointer = data?.bindMemory(to: Float.self, capacity: 1024)
let floatBuffer = UnsafeBufferPointer(start: floatPointer, count: 1024)
let outputArray = Array(floatBuffer)
print(outputArray[0..<20])
}
我正在获取结果,但是它们看起来像是一种奇怪的形式(对我而言),例如(打印语句的输出):
[-snan(0x1dff94), -nan(0x15ffba), 2.93871626e-39, 2.29594486e-39, 8.26537682e-40, -nan(0x1affef), -nan(0xeffe0), 2.84690739e-39, 4.95919386e-39, 4.86737658e-39, 1.19389788e-39, -nan(0x8ffe4), -snan(0xaffa6), -3.29645785e+38, -snan(0x17ff85), -nan(0x5ffae), 2.66320417e-39, 2.11228928e-39, -nan(0x2fff6), -nan(0x19ffd6)]
[-2.45903224e+38, -3.13694095e+38, -snan(0xdff85), -nan(0xbffb4), -snan(0x11ffba), -2.83122967e+38, -2.41915276e+38, -2.67170487e+38, -2.83122724e+38, -2.73817357e+38, -2.39257084e+38, -1.50864781e+38, -1.34249056e+38, -1.71464408e+38, -2.53878186e+38, -2.48561761e+38, -2.64512051e+38, -3.19011393e+38, -snan(0x12ff96), -snan(0x1bffb7)]
问题是,如何以“正常” CMSampleBuffer
的方式访问AVAudioPCMBuffer
样品?
FloatChannelData
还是我在做什么错?
答案 0 :(得分:1)
当我创建并AVCaptureAudioDataOutput
个样本时,它们是Int16
个(不确定是否可以配置Float
个),并且在打印时您修改后的代码,就很有意义了:
let intPointer = data?.bindMemory(to: Int16.self, capacity: 1024)
是否在任何地方记录了音频Int16
的声音?如果不是这样,您总是可以通过查看示例缓冲区的AudioStreamBasicDescription
let asbd = CMAudioFormatDescriptionGetStreamBasicDescription(CMSampleBufferGetFormatDescription(sampleBuffer)!)!.pointee
或开始使用swift 4使其更具可读性
let asbd = sampleBuffer.formatDescription!.audioStreamBasicDescription!.pointee