 
   // MARK: - Initialization
 
-  public init(bufferSize: AVAudioFrameCount = 4096, delegate: PitchEngineDelegate?) {
+  public init(mode: Mode, bufferSize: AVAudioFrameCount = 4096, delegate: PitchEngineDelegate?) {
+    self.mode = mode
     self.bufferSize = bufferSize
     self.delegate = delegate
   }


public class PitchEngine {

  public weak var delegate: PitchEngineDelegate?
  public var active = false

  private let bufferSize: AVAudioFrameCount
  private var frequencies = [Float]()
  public var pitches = [Pitch]()
  public var currentPitch: Pitch!

  private lazy var signalTracker: SignalTrackingAware = { [unowned self] in
    let inputMonitor = InputSignalTracker(

  // MARK: - Processing

  public func start() {
    guard mode == .Play else {
      activate()
      return
    }

    AVAudioSession.sharedInstance().requestRecordPermission { [weak self] granted  in
      guard let weakSelf = self else { return }

      guard granted else {
        weakSelf.delegate?.pitchEngineDidRecieveError(weakSelf,
          error: Error.RecordPermissionDenied)
        return
      }

      dispatch_async(dispatch_get_main_queue()) {
        weakSelf.activate()
      }
    }
  }

  public func stop() {
    signalTracker.stop()
    active = false
  }

  private func activate() {
    do {
      try signalTracker.start()
      active = true
    } catch {
      delegate?.pitchEngineDidRecieveError(self, error: error)
    }
  }
}

