   )
 )
 
-;; Is there a \default synthesizer in Overtone?
-(defsynth s [freq 440]
-  (let [src (* 0.3 (sin-osc freq))]
-    (out 0 src)))
-
 ;; Is there a better way?
 (do
   (def cont (atom 0))

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

;; Page 83
;; "foo" repeats every second


(go)
(go)


;; Page 88
///////////////////////////////////////////////////////////////
// Figure 3.3 Nesting tasks inside routines
(comment
(
r = Routine({
        c = TempoClock.new; // make a TempoClock
        // start a 'wobbly' loop
        t = Task({
                loop({
                        x.release(0.1);
                        x = Synth(\default, [freq: 61.midicps, amp: 0.2]);
                        0.2.wait;
                        x.release(0.1);
                        x = Synth(\default, [freq: 67.midicps, amp: 0.2]);
                        rrand(0.075, 0.25).wait; // random wait from 0.1 to 0.25 seconds
                });
        }, c); // use the TempoClock to play this Task
        t.start;
        nil.yield;

        // now add some notes
        y = Synth(\default, [freq: 73.midicps, amp: 0.3]);
        nil.yield;
        y.release(0.1);
        y = Synth(\default, [freq: 79.midicps, amp: 0.3]);
        c.tempo = 2; // double time
        nil.yield;
        t.stop; y.release(1); x.release(0.1); // stop the Task and Synths
});
)

r.next; // start loop
r.next; // first note
r.next; // second note; loop goes 'double time'
r.next; // stop loop and fade
)

(do
  (def cont (atom true))
  (def x (atom nil))
  (def y (atom nil))
  (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)
