title: Decode AAC to PCM format using AVAudioConverter Swift
body: <p>How convert AAC to PCM using AVAudioConverter, AVAudioCompressedBuffer and AVAudioPCMBuffer on Swift?</p>
  
  <p>On WWDC 2015, 507 Session was said, that AVAudioConverter can encode and decode PCM buffer, was showed encode example, but wasn't showed examples with decoding.
  I tried decode, and something doesn't work. I don't know what:(</p>
  
  <p>Calls:</p>
  
  <pre><code>//buffer - it's AVAudioPCMBuffer from AVAudioInputNode(AVAudioEngine)
  let aacBuffer = AudioBufferConverter.convertToAAC(from: buffer, error: nil) //has data
  let data = Data(bytes: aacBuffer!.data, count: Int(aacBuffer!.byteLength)) //has data
  let aacReverseBuffer = AudioBufferConverter.convertToAAC(from: data) //has data
  let pcmReverseBuffer = AudioBufferConverter.convertToPCM(from: aacBuffer2!, error: nil) //zeros data. data object exist, but filled by zeros
  </code></pre>
  
  <p>It's code for converting:</p>
  
  <pre><code>class AudioBufferFormatHelper {
  
      static func PCMFormat() -&gt; AVAudioFormat? {
  
          return AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 44100, channels: 1, interleaved: false)
      }
  
      static func AACFormat() -&gt; AVAudioFormat? {
  
          var outDesc = AudioStreamBasicDescription(
                  mSampleRate: 44100,
                  mFormatID: kAudioFormatMPEG4AAC,
                  mFormatFlags: 0,
                  mBytesPerPacket: 0,
                  mFramesPerPacket: 0,
                  mBytesPerFrame: 0,
                  mChannelsPerFrame: 1,
                  mBitsPerChannel: 0,
                  mReserved: 0)
          let outFormat = AVAudioFormat(streamDescription: &amp;outDesc)
          return outFormat
      }
  }
  
  class AudioBufferConverter {
  
      static func convertToAAC(from buffer: AVAudioBuffer, error outError: NSErrorPointer) -&gt; AVAudioCompressedBuffer? {
  
          let outputFormat = AudioBufferFormatHelper.AACFormat()
          let outBuffer = AVAudioCompressedBuffer(format: outputFormat!, packetCapacity: 8, maximumPacketSize: 768)
  
          self.convert(from: buffer, to: outBuffer, error: outError)
  
          return outBuffer
      }
  
      static func convertToPCM(from buffer: AVAudioBuffer, error outError: NSErrorPointer) -&gt; AVAudioPCMBuffer? {
  
          let outputFormat = AudioBufferFormatHelper.PCMFormat()
          guard let outBuffer = AVAudioPCMBuffer(pcmFormat: outputFormat!, frameCapacity: 4410) else {
              return nil
          }
  
          outBuffer.frameLength = 4410
          self.convert(from: buffer, to: outBuffer, error: outError)
  
          return outBuffer
      }
  
      static func convertToAAC(from data: Data) -&gt; AVAudioCompressedBuffer? {
  
          let nsData = NSData(data: data)
          let inputFormat = AudioBufferFormatHelper.AACFormat()
          let buffer = AVAudioCompressedBuffer(format: inputFormat!, packetCapacity: 8, maximumPacketSize: 768)
          buffer.byteLength = UInt32(data.count)
          buffer.packetCount = 8
  
          buffer.data.copyMemory(from: nsData.bytes, byteCount: nsData.length)
          buffer.packetDescriptions!.pointee.mDataByteSize = 4
  
          return buffer
      }
  
      private static func convert(from sourceBuffer: AVAudioBuffer, to destinationBuffer: AVAudioBuffer, error outError: NSErrorPointer) {
  
          //init converter
          let inputFormat = sourceBuffer.format
          let outputFormat = destinationBuffer.format
          let converter = AVAudioConverter(from: inputFormat, to: outputFormat)
  
          converter!.bitRate = 32000
  
          let inputBlock : AVAudioConverterInputBlock = { inNumPackets, outStatus in
  
              outStatus.pointee = AVAudioConverterInputStatus.haveData
              return sourceBuffer
          }
  
          _ = converter!.convert(to: destinationBuffer, error: outError, withInputFrom: inputBlock)
      }
  }
  </code></pre>
  
  <p>In result AVAudioPCMBuffer has data with zeros.
  And in messages I see errors:</p>
  
  <pre><code>AACDecoder.cpp:192:Deserialize:  Unmatched number of channel elements in payload
  AACDecoder.cpp:220:DecodeFrame:  Error deserializing packet
  [ac] ACMP4AACBaseDecoder.cpp:1337:ProduceOutputBufferList: (0x14f81b840) Error decoding packet 1: err = -1, packet length: 0
  AACDecoder.cpp:192:Deserialize:  Unmatched number of channel elements in payload
  AACDecoder.cpp:220:DecodeFrame:  Error deserializing packet
  [ac] ACMP4AACBaseDecoder.cpp:1337:ProduceOutputBufferList: (0x14f81b840) Error decoding packet 3: err = -1, packet length: 0
  AACDecoder.cpp:192:Deserialize:  Unmatched number of channel elements in payload
  AACDecoder.cpp:220:DecodeFrame:  Error deserializing packet
  [ac] ACMP4AACBaseDecoder.cpp:1337:ProduceOutputBufferList: (0x14f81b840) Error decoding packet 5: err = -1, packet length: 0
  AACDecoder.cpp:192:Deserialize:  Unmatched number of channel elements in payload
  AACDecoder.cpp:220:DecodeFrame:  Error deserializing packet
  [ac] ACMP4AACBaseDecoder.cpp:1337:ProduceOutputBufferList: (0x14f81b840) Error decoding packet 7: err = -1, packet length: 0
  </code></pre>