title: iOS - Detect Blow into Mic and convert the results! (swift)
body: <p>I need to develop an iOS App in swift which detects a blow in the microphone from a user. This has to be a Challenge-Game where two players have to blow into the iPhone mic one after the other. The decibel values should be measured and converted in meter or kilometer so I can determine a winner. The player which "blows further" (player1: 50km, player2: 70km) wins. </p>
  
  <p>Is this a possible implementation?</p>
  
  <p>I have this code in swift and I don't know how to proceed:</p>
  
  <pre><code>import Foundation
  import UIKit
  import AVFoundation
  import CoreAudio
  
  class ViewController: UIViewController {
  // @IBOutlet weak var mainImage: UIImageView!
  
  var recorder: AVAudioRecorder!
  var levelTimer = NSTimer()
  var lowPassResults: Double = 0.0
  override func viewDidLoad() {
      super.viewDidLoad()
      let url = NSURL.fileURLWithPath("dev/null")
      //numbers are automatically wrapped into NSNumber objects, so I simplified that to [NSString : NSNumber]
      var settings : [NSString : NSNumber] = [AVSampleRateKey: 44100.0, AVFormatIDKey: kAudioFormatAppleLossless, AVNumberOfChannelsKey: 1, AVEncoderAudioQualityKey: AVAudioQuality.Max.rawValue]
      var error: NSError?
     // mainImage?.image = UIImage(named: "flyForReal.png");
      recorder = AVAudioRecorder(URL:url, settings:settings, error:&amp;error)
  
      if((recorder) != nil){
          recorder.prepareToRecord()
          recorder.meteringEnabled = true
          recorder.record()
          levelTimer = NSTimer.scheduledTimerWithTimeInterval(0.05, target: self, selector: Selector("levelTimerCallback"), userInfo: nil, repeats: true)
      }
      else{
          NSLog("%@", "Error");
      }
  }
  func levelTimerCallback(timer:NSTimer) {
      recorder.updateMeters()
  
      let ALPHA: Double = 0.05
      var peakPowerForChannel = pow(Double(10), (0.05 * Double(recorder.peakPowerForChannel(0))))
      lowPassResults = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * lowPassResults;
      if(lowPassResults &gt; 0.95){
          NSLog("@Mic blow detected");
      }
      NSLog("@Average input: %f Peak input: %f Low pass results: %f", recorder.averagePowerForChannel(0), recorder.peakPowerForChannel(0), lowPassResults);
   }
  }
  </code></pre>
  
  <p>Thanks ahead! </p>