         audioRecorder = nil
         
     }
+    
+    
+    func saveDrumKits(data: [DrumKit],index:Int) {
+        let drumKitData = NSKeyedArchiver.archivedData(withRootObject: data)
+        UserDefaults.standard.set(drumKitData, forKey: "drumKits")
+        UserDefaults.standard.set(index, forKey: "selectedDrumKitIndex")
+    }
+    
+    func loadDrumKits() -> ([DrumKit],Int) {
+        let drumkits = UserDefaults.standard.object(forKey: "drumKits")
+        
+        if let drumkits = drumkits as? Data {
+            let drumkits = NSKeyedUnarchiver.unarchiveObject(with: drumkits)
+            
+            
+            if let drumkits = drumkits as? [DrumKit] {
+                let index = UserDefaults.standard.object(forKey: "selectedDrumKitIndex")
+                if let index = index as? Int {
+                    return (drumkits,index)
+                }
+                else {
+                    return ([DrumKit(name: "Default", gestures: gestures)],0) //use default kit
+                }
+            } else {
+                return ([DrumKit(name: "Default", gestures: gestures)],0) //use default kit
+            }
+            
+        } else {
+            return ([DrumKit(name: "Default", gestures: gestures)],0) //use default kit
+        }
+    }
+    
+    
+    
+    
 
 }

    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //set the time to "now" for all instruments
        players["Hi-Hat"]!["time"] = Date()

    var orientationBuffer = RingBuffer()
    let magValue = 1.0
    var numDataPoints = 0
    var recording = false

    var indicatorView: ESTMusicIndicatorView!
    var startAnimating: Bool = false
