              (linen:kr gate 0.01 0.7 0.3 2))]
     (offset-out out (pan2 z pan amp))))
 
-(defn release [x releaseTime]
-  (ctl x :gate (- releaseTime)))
+(defn wait-release
+  ([x releaseTime] (if @x (release @x releaseTime) (recur x releaseTime)))
+  ([x] (wait-release x 0.0)))
+
+(defn release
+  ([x releaseTime] (ctl x :gate (- releaseTime)))
+  ([x] (release x 0.0)))
+
+(defn generator [& thunks]
+  (let [r (agent thunks)]
+    #(if (seq @r)
+       (do
+         (send-off r (fn [ts] (when (seq ts) ((first ts))) (rest ts)))
+         'ok)
+       'done)))
 
 ;; Page 83
 ;; "foo" repeats every second

  (def t (atom 1))

  (defn go1 []
   (loop []
     (when (not (nil? @x)) (release @x 0.1))
     (when @cont
       (reset! x (s :freq (midi->hz 61) :amp 0.2))
       (Thread/sleep (/ 200 @t))
       (release @x 0.1)
       (reset! x (s :freq (midi->hz 67) :amp 0.2))
       (Thread/sleep (/ (ranged-rand 75 250) @t))
       (recur))))

  (defn go2 []
    (reset! y (s :freq (midi->hz 73) :amp 0.3)))

  (defn go3 []
    (when (not (nil? @y)) (release @y 0.1))
    (reset! y (s :freq (midi->hz 79) :amp 0.3))
    (reset! t 2))

  (defn go4 []
    (when (not (nil? @y)) (release @y 0.1))
    (reset! cont false))
)

(go1)
(go2)
(go3)
(go4)

(ns sc-three
  (:use [overtone.live]))

;; Helpers

;; This is the default synth. Is it accessible from Overtone?
(comment
