17

How to convert AVAudioPCMBuffer to NSData? If it should be done as

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

then how to calculate bufferLength?

And how to convert NSData to AVAudioPCMBuffer?

Shmidt
  • 15,652
  • 16
  • 79
  • 129

2 Answers2

31

Buffer length is frameCapacity * bytesPerFrame. Here are functions that can do conversion between NSData and 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
}
Rod Chen
  • 366
  • 4
  • 8
12

Copying the buffers is much easier if you copy via the audioBufferList API's. This also works no matter what the format is of the actual buffer.

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
    }
}
DZoki019
  • 321
  • 2
  • 11
Brian King
  • 2,804
  • 1
  • 25
  • 26
  • 1
    To avoid `'withUnsafeBytes' is deprecated: use 'withUnsafeBytes(_: (UnsafeRawBufferPointer) throws -> R) rethrows -> R'` warning with _Swift 5_, you can pass `addr.baseAddress` to `copyMemory` function instead of `addr`. – aleksmutlu Nov 07 '19 at 09:09