title: Tap Mic Input Using AVAudioEngine in Swift
body: <p>I'm really excited about the new AVAudioEngine. It seems like a good API wrapper around audio unit. Unfortunately the documentation is so far nonexistent, and I'm having problems getting a simple graph to work.</p>
  <p>Using the following simple code to set up an audio engine graph, the tap block is never called. It mimics some of the sample code floating around the web, though those also did not work.</p>
  <pre><code>let inputNode = audioEngine.inputNode
  var error: NSError?
  let bus = 0
      
  inputNode.installTapOnBus(bus, bufferSize: 2048, format: inputNode.inputFormatForBus(bus)) { 
      (buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -&gt; Void in
      println(&quot;sfdljk&quot;)
  }
      
  audioEngine.prepare()
  if audioEngine.startAndReturnError(&amp;error) {
      println(&quot;started audio&quot;)
  } else {
      if let engineStartError = error {
          println(&quot;error starting audio: \(engineStartError.localizedDescription)&quot;)
      }
  }
  </code></pre>
  <p>All I'm looking for is the raw pcm buffer for analysis. I don't need any effects or output. According to the WWDC talk &quot;502 Audio Engine in Practice&quot;, this setup should work.</p>
  <blockquote>
  <p>Now if you want to capture data from the input node, you can install a node tap and we've talked about that.</p>
  <p>But what's interesting about this particular example is, if I wanted to work with just the input node, say just capture data from the microphone and maybe examine it, analyze it in real time or maybe write it out to file, I can directly install a tap on the input node.</p>
  <p>And the tap will do the work of pulling the input node for data, stuffing it in buffers and then returning that back to the application.</p>
  <p>Once you have that data you can do whatever you need to do with it.</p>
  </blockquote>
  <p>Here are some links I tried:</p>
  <ol>
  <li><a href="http://hondrouthoughts.blogspot.com/2014/09/avfoundation-audio-monitoring.html" rel="noreferrer">http://hondrouthoughts.blogspot.com/2014/09/avfoundation-audio-monitoring.html</a></li>
  <li><a href="http://jamiebullock.com/post/89243252529/live-coding-audio-with-swift-playgrounds" rel="noreferrer">http://jamiebullock.com/post/89243252529/live-coding-audio-with-swift-playgrounds</a> (SIGABRT in playground on startAndReturnError)</li>
  </ol>
  <p>Edit: This is the implementation based on Thorsten Karrer's suggestion. It unfortunately does not work.</p>
  <pre><code>class AudioProcessor {
      let audioEngine = AVAudioEngine()
  
      init(){
          let inputNode = audioEngine.inputNode
          let bus = 0
          var error: NSError?
      
          inputNode.installTapOnBus(bus, bufferSize: 2048, format:inputNode.inputFormatForBus(bus)) {
              (buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -&gt; Void in
                  println(&quot;sfdljk&quot;)
          }
      
          audioEngine.prepare()
          audioEngine.startAndReturnError(nil)
          println(&quot;started audio&quot;)
      }
  }
  </code></pre>