   [scale]
   {:piano-keys (into (sorted-map) (map (fn [freq] [freq {}])
                                        scale))
-   :notes [] :w 0 :h 0 :sound-ready false})
+   :notes [] :w 0 :h 0 :sound-ready false
+   :px-per-ms 0.01
+   :start (.getTime (js/Date.))
+   :playhead (.getTime (js/Date.))})
 
 (defn add-synths-to-instrument
   [instrument]

  (go
   (loop [instrument (update-size (create-instrument (scales/c-minor)) canvas-id)]
     (>! c-instrument instrument)
     (let [[data c] (alts! [c-touch c-orientation-change])]
       (condp = c
         c-orientation-change (recur (update-size instrument canvas-id))
         c-touch (recur (fire-touch-data-on-instrument instrument data))))))
  )

  (util/find-first (fn [note] (= freq (-> note :freq)))
                   (-> instrument :notes)))

(defn t->px
  [{start :start px-per-ms :px-per-ms} t]
  (* px-per-ms (- t start)))

(defn note-rect
  [{on :on off :off freq :freq :as note}
   {px-per-ms :px-per-ms playhead :playhead :as instrument}]
  (let [piano-key-w (piano-key-width instrument)
        note-w 4
        note-left (* piano-key-w (util/index-of (-> instrument :piano-keys keys) freq))]
    {:x (- (+ note-left (/ piano-key-w 2))
           (/ note-w 2))
     :y (+ (t->px instrument on)
           (/ (-> instrument :h) 2))
     :w note-w
     :h (- (t->px instrument (or off playhead))
           (t->px instrument on))}))

(defn screen-rect
  [{w :w h :h playhead :playhead :as instrument}]
  {:x 0 :y (t->px instrument playhead) :w w :h h})

(defn color-set
  [instrument freq]
  (nth colors (mod (util/index-of (-> instrument :piano-keys keys) freq)
                   (count colors))))

(defn draw-note
  [draw-ctx instrument note]
  (let [{x :x y :y w :w h :h} (note-rect note instrument)]
    (set! (.-fillStyle draw-ctx) "white")
    (.fillRect draw-ctx x y w h)))

(defn colliding?
  [r1 r2]
  (not (or (< (+ (:x r1) (:w r1)) (:x r2))
           (< (+ (:y r1) (:h r1)) (:y r2))
           (> (:x r1) (+ (:x r2) (:w r2)))
           (> (:y r1) (+ (:y r2) (:h r2))))))

(defn on-screen?
  [instrument note]
  (colliding? (note-rect note instrument)
              (screen-rect instrument)))

(defn draw-notes
  [draw-ctx instrument]
  (dorun (map (partial draw-note draw-ctx instrument)
              (filter (partial on-screen? instrument)
                      (-> instrument :notes)))))

(defn draw-piano-keys
  [draw-ctx {w :w h :h playhead :playhead :as instrument}]
  (let [freqs (-> instrument :piano-keys keys)
        piano-key-w (piano-key-width instrument)]
    (dotimes [n (count freqs)]
      (let [freq (nth freqs n)
            note (latest-note instrument freq)
            piano-key-on (and note (-> note :off nil?))]
        (set! (.-fillStyle draw-ctx)
              ((if piano-key-on :light :dark) (color-set instrument freq)))
        (.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}]
  (.save draw-ctx)
  (.translate draw-ctx 0 (-> (t->px instrument playhead) -))
  (.clearRect draw-ctx 0 0 w h)
  (draw-piano-keys draw-ctx instrument)
  (draw-notes draw-ctx instrument)
  (.restore draw-ctx))

(defn touch->note
  [x instrument]
  (let [note-index (.floor js/Math (/ x (piano-key-width instrument)))]
    (nth (-> instrument :piano-keys keys) note-index)))

(defn create-note-synth
  [freq]
