gpt4 book ai didi

ios - 将 CMSampleBuffer 转换为 AVAudioPCMBuffer 以获取实时音频

转载 作者:行者123 更新时间:2023-11-28 07:24:08 49 4
gpt4 key购买 nike

我正在尝试从 AVCaptureAudioDataOutputSampleBufferDelegatecaptureOutput 返回的 CMSampleBuffer 中读取频率值。

我的想法是创建一个AVAudioPCMBuffer,这样我就可以读取它的floatChannelData。但我不确定如何将缓冲区传递给它。

我想我可以创建它:

public func captureOutput(_ output: AVCaptureOutput,
didOutput sampleBuffer: CMSampleBuffer,
from connection: AVCaptureConnection) {
guard let blockBuffer = CMSampleBufferGetDataBuffer(sampleBuffer) else {
return
}
let length = CMBlockBufferGetDataLength(blockBuffer)

let audioFormat = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 44100, channels: 1, interleaved: false)
let pcmBuffer = AVAudioPCMBuffer(pcmFormat: audioFormat!, frameCapacity: AVAudioFrameCount(length))
pcmBuffer?.frameLength = pcmBuffer!.frameCapacity

但是我该如何填充它的数据呢?

最佳答案

这些方面的内容应该有所帮助:

var asbd = CMSampleBufferGetFormatDescription(sampleBuffer)!.audioStreamBasicDescription!
var audioBufferList = AudioBufferList()
var blockBuffer : CMBlockBuffer?

CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(
sampleBuffer,
bufferListSizeNeededOut: nil,
bufferListOut: &audioBufferList,
bufferListSize: MemoryLayout<AudioBufferList>.size,
blockBufferAllocator: nil,
blockBufferMemoryAllocator: nil,
flags: 0,
blockBufferOut: &blockBuffer
)

let mBuffers = audioBufferList.mBuffers
let frameLength = AVAudioFrameCount(Int(mBuffers.mDataByteSize) / MemoryLayout<Float>.size)
let pcmBuffer = AVAudioPCMBuffer(pcmFormat: AVAudioFormat(streamDescription: &asbd)!, frameCapacity: frameLength)!
pcmBuffer.frameLength = frameLength
pcmBuffer.mutableAudioBufferList.pointee.mBuffers = mBuffers
pcmBuffer.mutableAudioBufferList.pointee.mNumberBuffers = 1

这似乎在捕获 session 的末尾创建了一个有效的 AVAudioPCMBuffer。但它现在的帧长度对于我的用例来说是错误的,所以需要做一些进一步的缓冲。

关于ios - 将 CMSampleBuffer 转换为 AVAudioPCMBuffer 以获取实时音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57083315/

49 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com