   overtone.time-utils
   (:import (java.util.concurrent ScheduledThreadPoolExecutor TimeUnit
                                  PriorityBlockingQueue))
-  (:use (overtone event)))
+  (:use (overtone event util)))
 
 ; Time
 

           ms-period     (long ms-period)]
       (.scheduleAtFixedRate pool fun initial-delay ms-period TimeUnit/MILLISECONDS))))

(defn stop-and-reset-pool!
  "Shuts down a given pool (passed in as an atom) either immediately or not depending on whether the optional now param is used.
   The pool is then reset to a fresh new pool preserving the original size."
  [pool-ref & [now]]
  (let [pool @pool-ref
        num-threads (.getCorePoolSize pool)]
    (reset! pool-ref (make-pool num-threads))
    (if now
      (.shutdownNow pool)
      (.shutdown pool))))
  (on-sync-event :reset ::player-reset #(stop-and-reset-pool! player-pool* true ))


(defn stop-player [player & [now]]
  (.cancel player (or now false)))

; Recursion in Time
;   By passing a function using #'foo syntax instead of just foo, when later

; it seems that timing isn't accurate enough.

(defn make-pool
  "Creates a new pool of threads to schedule new events for. Pool size defaults to the cpu count"
  ([] (make-pool (cpu-count)))
  ([num-threads]
     (ScheduledThreadPoolExecutor. num-threads)))

(def player-pool* (agent (make-pool)))

(defn schedule
  "Schedules fun to be executed after ms-delay milliseconds. Pool defaults to the player-pool."
