 (defn create-instrument
   [scale]
   (let [start (.getTime (js/Date.))]
-    {:piano-keys (into (sorted-map) (map-indexed (fn [i freq] [freq {:n i}])
+    {:piano-keys (into (sorted-map) (map-indexed (fn [i freq] [freq {:n i :on? false}])
                                                  scale))
-     :notes [] :w 0 :h 0 :sound-ready false
+     :notes ()
+     :w 0 :h 0 :sound-ready false
      :px-per-ms 0.04
      :start start
      :playhead start}))

                  [instrument {touch-id :touch-id time :time :as event}]
                  (let [freq (touch->freq instrument event)
                        piano-key (get-in instrument [:piano-keys freq])]
                    (do
                      ;; (println "play")
                      (.play (.bang (:synth piano-key)))
                      (assoc instrument :notes
                             (conj (get instrument :notes)
                                   {:freq freq :on time :off nil :touch-id touch-id})))))
   "touchend" (fn
                [{piano-keys :piano-keys :as instrument}
                 {touch-id :touch-id time :time :as event}]
                (assoc instrument :notes
                       (map (fn [note]
                              (if (= touch-id (-> note :touch-id))
                                (do
                                  ;; (println "release")
                                  (.release (:synth (get piano-keys (-> note :freq))))
                                  (assoc note :off time))
                                note))
                            (-> instrument :notes))))})

(defn fire-event-on-instrument
  [instrument event]


(defn draw-piano-keys
  [draw-ctx {w :w h :h playhead :playhead :as instrument}]
  (let [piano-keys (-> instrument :piano-keys)
        piano-key-w (piano-key-width instrument)]
    (doseq [[n [freq piano-key]] (map-indexed vector piano-keys)]
      (set! (.-fillStyle draw-ctx)
            ((if (piano-key :on?) :light :dark) (nth colors (mod n (count colors)))))
      (.fillRect draw-ctx
                 (* n piano-key-w)
                 (t->px instrument playhead)
                 piano-key-w h))))

(defn draw-instrument
  [draw-ctx {w :w h :h playhead :playhead :as instrument}]
