    event's clock."
   ([e ratio]
    (let [{:keys [clock-id]} (if (sequential? e) (first e) e)
-         clock (get *clocks* clock-id)]
+         clock @(get *clocks* clock-id)]
      (time-stretch! e (current-time clock) ratio)))
   ([e time-reference ratio]
    (if (sequential? e)


(defn start!
  "Remove all scheduled events and start the clock."
  [clock]
  (let [{:keys [context started]} @clock]
    (when-not started
      (swap! clock assoc :events [])
      (let [clock-node (doto (.createScriptProcessor context 256 1 1)
                         (.connect (.-destination context))
                         (aset "onaudioprocess" #(tick! clock)))]
        (swap! clock assoc :clock-node clock-node :started true)
        clock))))

(defn stop!
  "Stops the clock."
  [clock]
  (let [{:keys [started clock-node]} @clock]
    (when started
      (.disconnect clock-node)
      (swap! clock assoc :started false :clock-node nil)
      clock)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defn set-timeout!
  "Schedules `f` after `delay-ms` milliseconds. Returns the event.

   `opts` may contain :tolerance-early and :tolerance-late for optionally
   overriding the clock's timing window for events."
  [clock-atom f delay-ms & {:as opts}]
  (create-event! clock-atom f (+ (current-time @clock-atom) delay-ms) opts))

(defn callback-at-time!
  "Schedules `f` to run before `deadline`. Returns the event.
