title: Recreating Voice Memo app: Unexpected behavior when storing audio from AVAudioEngine as Data
body: <p>I'm trying to create something very close to the Voice Memo app on iOS. My requirements are:</p>
  <ul>
  <li>The user press a button to record audio, and release to stop recording.</li>
  <li>The user sees the audio waveform in real-time.</li>
  <li>The user can scroll through this audio waveform.</li>
  <li>Tricky part: The user can record in the middle of the previous recording, thus erasing the data. The waveform should display the new recording in real-time.</li>
  </ul>
  <p>After experimenting a lot with AVAudioRecorder and AVMutableComposition, I settled upon using AVAudioEngine (with AVAudioFile), because it does everything I want: measure audio levels and write to a file at any given audio time.</p>
  <p>Listening to the microphone is working:</p>
  <pre><code>private var audioSession: AVAudioSession
  private var engine: AVAudioEngine
  private var file: AVAudioFile?
  private var player: AVAudioPlayerNode
  
  // holds all the audio samples (for one second of audio, holds 48000 samples)
  private var buffer: Data
  
  // downsampled version for display -&gt; at the moment the sample period is the one of the tap function
  // so it's 0.1s. Therefore for one second of audio there is 4800 samples
  private var levels: [Float]
  
  init() {
      audioSession = AVAudioSession.sharedInstance()
      engine = AVAudioEngine()
      player = AVAudioPlayerNode()
      buffer = Data()
      levels = []
  }
  
  func setupAudioEngine() throws {
      let fileName = &quot;\(UUID().uuidString).m4a&quot;
      file = try AVAudioFile(forWriting: folderURL.appendingPathComponent(fileName), settings: engine.inputNode.inputFormat(forBus: 0).settings)
      engine.attach(player)
      engine.connect(player, to: engine.mainMixerNode, format: nil)
  }
  
  func startRecording() throws {
      try audioSession.setCategory(.playAndRecord, mode: .default, options: .defaultToSpeaker)
      try audioSession.setActive(true)
      try setupAudioEngine()
      
      engine.inputNode.installTap(onBus: 0, bufferSize: 1024, format: engine.inputNode.outputFormat(forBus: 0)) { [weak self] buffer, time in
          self?.tap(buffer: buffer, time: time)
      }
  
      engine.prepare()
      try engine.start()
  }
  
  private func tap(buffer: AVAudioPCMBuffer, time: AVAudioTime) {
      // called 10 times per second
          
      let bufferData = Data(bytes: buffer.floatChannelData![0], count: Int(buffer.frameLength))
      self.buffer.append(bufferData)
          
      var sampleBuffer = buffer.floatChannelData![0] // 4800 float samples
      let nbSamplesInOneLevel = UInt(buffer.frameLength) // = 4800
      let totalSamples = self.buffer.count // a multiple of 4800
      let numberOfBuffersAdded = totalSamples / Int(nbSamplesInOneLevel)
  
      vDSP_vabs(sampleBuffer, 1, sampleBuffer, 1, nbSamplesInOneLevel)
      // 1
      print(sampleBuffer[0], sampleBuffer[1], sampleBuffer[2], sampleBuffer[3], sampleBuffer[4])
  
      self.buffer.withUnsafeBytes { (samples: UnsafePointer&lt;Float&gt;) in
          var processingBuffer = [Float](repeating: 0.0, count: totalSamples)
          vDSP_vabs(samples, 1, &amp;processingBuffer, 1, vDSP_Length(totalSamples))
  
          let offset = (numberOfBuffersAdded-1)*Int(nbSamplesInOneLevel)
          // 2
          print(samples[offset+0], samples[offset+1], samples[offset+2], samples[offset+3], samples[offset+4])
  
          // ...compute dB to draw waveform
      }
  }
  </code></pre>
  <p>Now my problem is when storing the buffers in the <code>Data</code> variable. Basically I would expect print 1 and print 2 to be identical, because they should represent the same underlying data, but they are not, except the first time the function is called:</p>
  <pre><code>1.2723527e-06 2.019807e-06 2.2901681e-06 1.7905508e-06 3.3355818e-07
  -1.2723527e-06 -2.019807e-06 -2.2901681e-06 -1.7905508e-06 -3.3355818e-07
  
  0.002492284 0.0023660995 0.0023821378 0.0025995593 0.0029681565
  0.0 0.0 0.0 0.0 0.0
  
  0.003351898 0.003712599 0.0035988796 0.0032182669 0.0028063601
  0.0 0.0 0.0 0.0 0.0
  </code></pre>
  <p>Why is that?</p>