将AVAudioPCMBuffer转换为NSData并返回

将AVAudioPCMBuffer转换为NSData并返回,第1张

将AVAudioPCMBuffer转换为NSData并返回

缓冲区长度为frameCapacity * bytesPerframe。以下是可以在NSData和AVAudioPCMBuffer之间进行转换的函数

func tonSData(PCMBuffer: AVAudioPCMBuffer) -> NSData {    let channelCount = 1  // given PCMBuffer channel count is 1    var channels = UnsafeBufferPointer(start: PCMBuffer.floatChannelData, count: channelCount)    var ch0Data = NSData(bytes: channels[0], length:Int(PCMBuffer.frameCapacity * PCMBuffer.format.streamDescription.memory.mBytesPerframe))    return ch0Data}func toPCMBuffer(data: NSData) -> AVAudioPCMBuffer {    let audioFormat = AVAudioFormat(commonFormat: AVAudioCommonFormat.PCMFormatFloat32, sampleRate: 8000, channels: 1, interleaved: false)  // given NSData audio format    var PCMBuffer = AVAudioPCMBuffer(PCMFormat: audioFormat, frameCapacity: UInt32(data.length) / audioFormat.streamDescription.memory.mBytesPerframe)    PCMBuffer.frameLength = PCMBuffer.frameCapacity    let channels = UnsafeBufferPointer(start: PCMBuffer.floatChannelData, count: Int(PCMBuffer.format.channelCount))    data.getBytes(UnsafeMutablePointer<Void>(channels[0]) , length: data.length)    return PCMBuffer}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5427913.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存