【问题标题】:Convert AVAudioPCMBuffer to NSData and back将 AVAudioPCMBuffer 转换为 NSData 并返回
【发布时间】:2015-01-20 15:02:12
【问题描述】:

如何将AVAudioPCMBuffer 转换为NSData?如果应该这样做

let data = NSData(bytes: buffer.floatChannelData, length: bufferLength)

那么如何计算bufferLength

以及如何将NSData 转换为AVAudioPCMBuffer

【问题讨论】:

    标签: swift ios8 nsdata avaudiopcmbuffer


    【解决方案1】:

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

    extension AVAudioPCMBuffer {
        func data() -> Data {
            let channelCount = 1  // given PCMBuffer channel count is 1
            let channels = UnsafeBufferPointer(start: self.floatChannelData, count: channelCount)
            let ch0Data = NSData(bytes: channels[0], length:Int(self.frameCapacity * self.format.streamDescription.pointee.mBytesPerFrame))
            return ch0Data as Data
        }
    }
    
    
    func toPCMBuffer(data: NSData) -> AVAudioPCMBuffer? {
        let audioFormat = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 8000, channels: 1, interleaved: false)!  // given NSData audio format
        guard let PCMBuffer = AVAudioPCMBuffer(pcmFormat: audioFormat, frameCapacity: UInt32(data.length) / audioFormat.streamDescription.pointee.mBytesPerFrame) else {
            return nil
        }
        PCMBuffer.frameLength = PCMBuffer.frameCapacity
        let channels = UnsafeBufferPointer(start: PCMBuffer.floatChannelData, count: Int(PCMBuffer.format.channelCount))
        data.getBytes(UnsafeMutableRawPointer(channels[0]) , length: data.length)
        return PCMBuffer
    }
    

    【讨论】:

    • 我也是@Massimo。
    • 我成功地将NSData 转换为AVAudioPCMBuffer(我不需要反向,所以没有那个代码):pastebin.com/raw/m5c74FPA
    • @Massimo 如何反转它,意味着 AVAudioPCMBuffer 到 16 位 NSData ?
    • 对于 Swift 4,将 streamDescription.memory 替换为 streamDescription.pointee
    • 嗨,当数据实际上是 int16 时,可以使用.floatChannelData 吗?
    【解决方案2】:

    如果您通过 audioBufferList API 进行复制,则复制缓冲区会容易得多。无论实际缓冲区的格式是什么,这也有效。

    extension Data {
        init(buffer: AVAudioPCMBuffer, time: AVAudioTime) {
            let audioBuffer = buffer.audioBufferList.pointee.mBuffers
            self.init(bytes: audioBuffer.mData!, count: Int(audioBuffer.mDataByteSize))
        }
    
        func makePCMBuffer(format: AVAudioFormat) -> AVAudioPCMBuffer? {
            let streamDesc = format.streamDescription.pointee
            let frameCapacity = UInt32(count) / streamDesc.mBytesPerFrame
            guard let buffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: frameCapacity) else { return nil }
    
            buffer.frameLength = buffer.frameCapacity
            let audioBuffer = buffer.audioBufferList.pointee.mBuffers
    
            withUnsafeBytes { (bufferPointer) in
                guard let addr = bufferPointer.baseAddress else { return }
                audioBuffer.mData?.copyMemory(from: addr, byteCount: Int(audioBuffer.mDataByteSize))
            }
    
            return buffer
        }
    }
    

    【讨论】:

    • 为避免 Swift 5 出现'withUnsafeBytes' is deprecated: use 'withUnsafeBytes<R>(_: (UnsafeRawBufferPointer) throws -> R) rethrows -> R' 警告,您可以将addr.baseAddress 传递给copyMemory 函数而不是addr
    猜你喜欢
    • 2022-01-11
    • 1970-01-01
    • 2013-11-29
    • 1970-01-01
    • 1970-01-01
    • 2012-01-16
    • 1970-01-01
    • 2015-10-11
    • 2013-11-29
    相关资源
    最近更新 更多