gpt4 book ai didi

ios - 如何在 Swift 中将 CMSampleBuffer 转换为 Data?

转载 作者:行者123 更新时间:2023-11-30 11:09:26 43 4
gpt4 key购买 nike

我需要将 CMSampleBuffer 转换为 Data 格式。我正在使用一个第三方框架来执行音频相关任务。该框架为我提供了 CMSampleBuffer 对象中的流媒体(即实时音频)音频。

像这样:

func didAudioStreaming(audioSample: CMSampleBuffer!) {
//Here I need to conver this to Data format.
//Because I am using GRPC framework for Audio Recognization,
}

请向我提供将 CMSampleBuffer 转换为 Data 的步骤。

仅供引用

    let formatDesc:CMFormatDescription? = CMSampleBufferGetFormatDescription(audioSample)

<CMAudioFormatDescription 0x17010d890 [0x1b453ebb8]> {
mediaType:'soun'
mediaSubType:'lpcm'
mediaSpecific: {
ASBD: {
mSampleRate: 16000.000000
mFormatID: 'lpcm'
mFormatFlags: 0xc
mBytesPerPacket: 2
mFramesPerPacket: 1
mBytesPerFrame: 2
mChannelsPerFrame: 1
mBitsPerChannel: 16 }
cookie: {(null)}
ACL: {(null)}
FormatList Array: {(null)}
}
extensions: {(null)}
}

最佳答案

尝试下面的代码将 CMSampleBuffer 转换为 NSData。

let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
CVPixelBufferLockBaseAddress(imageBuffer!, CVPixelBufferLockFlags(rawValue: 0))
let bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer!)
let height = CVPixelBufferGetHeight(imageBuffer!)
let src_buff = CVPixelBufferGetBaseAddress(imageBuffer!)
let data = NSData(bytes: src_buff, length: bytesPerRow * height)
CVPixelBufferUnlockBaseAddress(imageBuffer!, CVPixelBufferLockFlags(rawValue: 0))

编辑-

对于 AudioBuffer,请使用以下代码 -

var audioBufferList = AudioBufferList()
var data = Data()
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))

for audioBuffer in buffers {
let frame = audioBuffer.mData?.assumingMemoryBound(to: UInt8.self)
data.append(frame!, count: Int(audioBuffer.mDataByteSize))
}

关于ios - 如何在 Swift 中将 CMSampleBuffer 转换为 Data?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52321928/

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