title: Best way to store AVAudioPCMBuffer into file while recording using AvaudioEngine
body: <p>I am looking for a solution in which, I can save AVAudioPCMBuffer continuously or at some interval.</p>
  <p>Below is my code</p>
  <pre><code>let bus = 0
  let inputNode = engine.inputNode
  
  let equalizer = AVAudioUnitEQ(numberOfBands: 2)
  
  equalizer.bands[0].filterType = .lowPass
  equalizer.bands[0].frequency = 3000
  equalizer.bands[0].bypass = false
  
  equalizer.bands[1].filterType = .highPass
  equalizer.bands[1].frequency = 1000
  equalizer.bands[1].bypass = false
  engine.attach(equalizer) //Attach equalizer
  
  // Connect nodes
  engine.connect(inputNode, to: equalizer, format: inputNode.inputFormat(forBus: 0))
  engine.connect(equalizer, to: engine.mainMixerNode, format: inputNode.inputFormat(forBus: 0))
  
  // call before creating converter because this changes the mainMixer's output format
  engine.prepare()
  
  let outputFormat = AVAudioFormat(commonFormat: .pcmFormatInt16,
                                   sampleRate: 5000,
                                   channels: 1,
                                   interleaved: false)!
  
  // Downsampling converter
  guard let converter: AVAudioConverter = AVAudioConverter(from: engine.mainMixerNode.outputFormat(forBus: 0), to: outputFormat) else {
      print(&quot;Can't convert in to this format&quot;)
      return
  }
  
  engine.mainMixerNode.installTap(onBus: bus, bufferSize: 2688, format: nil) { (buffer, time) in
      var newBufferAvailable = true
      
      let inputCallback: AVAudioConverterInputBlock = { inNumPackets, outStatus in
          if newBufferAvailable {
              outStatus.pointee = .haveData
              newBufferAvailable = false
              return buffer
          } else {
              outStatus.pointee = .noDataNow
              return nil
          }
      }
      
      
      let convertedBuffer = AVAudioPCMBuffer(pcmFormat: outputFormat, frameCapacity: AVAudioFrameCount(outputFormat.sampleRate) * buffer.frameLength / AVAudioFrameCount(buffer.format.sampleRate))!
      
      var error: NSError?
      let status = converter.convert(to: convertedBuffer, error: &amp;error, withInputFrom: inputCallback)
      assert(status != .error)
      
      
      if status == .haveData {
          // Process with converted buffer
      }
  }
  
  do {
      try engine.start()
  } catch {
      print(&quot;Can't start the engine: \(error)&quot;)
  }
  
  </code></pre>
  <p><strong>Successful attempt</strong></p>
  <p>If I store all buffer into a variable, convert it into data, and save those on stop is works very well</p>
  <p><strong>Issue on above process</strong></p>
  <p>There are serval cases where users may terminate the app, or we have some interruption. So instead of losing all data, we prefer to save buffers at some interval</p>
  <p><strong>What we have tried so far</strong></p>
  <ol>
  <li>Saving Buffer with AvaudioFile</li>
  </ol>
  <p>AvaudioFile is not saving the data in the correct format as it took 8000hz as the minimum sample rate, while our requirement is below 8k Hz.</p>
  <ol start="2">
  <li>Using FileHandel try to store data at serval interval</li>
  </ol>
  <p>Filehandle &gt; somehow it is not saving all the data, it overwrites and stores last interval data only.</p>
  <p>We appreciate any help.</p>