         }
     }
     
+    // MARK: - Lifecycle
+    
     override func viewWillAppear(_ animated: Bool) {
         super.viewWillAppear(animated)
     }

        // FIXME: Incorporate this method with stopPlayback?
        // FIXME: Make sure that the reply performances are reset as well as the main performance.
        NSLog("JAMVC: Stop Timer Called (either finished or cancelled).")
        if let recordView = self.recordView {
            if recordView.recording {
                self.stopRecording()
                recordView.recording = false
            } else {
                self.stopPlayback()
                recordView.playing = false
            }
        } else {
            self.stopPlayback()
        }
        
        for view in self.performanceViews {
            view.playing = false
        }
        
        self.playButton.setTitle("play", for: UIControlState.normal)
        self.state = ChirpJamModes.loaded
        self.updateUI()
        
        self.progressTimer?.invalidate()
        self.progress = 0.0
        self.recordingProgress?.progress = 0.0
        
        // Restart Playback in Jam Mode.
        if (self.jamming) {
            print("JAMVC: Restarting playback for the jam.")
            if (self.state != ChirpJamModes.playing) {
                self.playButtonPressed(self.playButton) // start playing if not already playing.
            }
        }
    }
    
    /// Increment the recording progress bar by 10ms; called automatically by timers.
    func incrementRecordingProgress(_ : Timer) {
        self.progress += 0.01;
        self.recordingProgress?.progress = Float(self.progress / self.RECORDING_TIME)
        if (self.progress >= self.RECORDING_TIME) {self.stopTimer()}
    }
    
    /// Starts a recurring timer that increments the progress bar.
    func startProgressBar() {
        self.progressTimer = Timer.scheduledTimer(withTimeInterval: 0.01, repeats: true, block: self.incrementRecordingProgress)
    }
    
    // MARK: - Recording Functions
    /// Sets into recording mode and starts the timer.
    func startRecording() {
        if (self.state == ChirpJamModes.new) {
            NSLog("JAMVC: Starting a recording.")
            self.recordView!.recording = true
            self.state = ChirpJamModes.recording
            self.jamming = false
            self.recordingProgress!.progress = 0.0
            self.progress = 0.0
            self.updateUI()
            self.startProgressBar()
            self.playBackPerformances()
        }
    }
    

import UIKit
import DropDown

let RECORDING_TIME = 5.0

// TODO: how to tell between loaded and saved and just loaded?

class ChirpJamViewController: UIViewController, UIDocumentInteractionControllerDelegate {
    /// Maximum allowed recording time.
    var state = ChirpJamModes.new
    var newPerformance : Bool = true
    var jamming : Bool = false
