     your ears if  you listen for to long.
     Ears are useful, volume down, avoid ear phones if possible.
 
-  Mission:
-    * Create instruments
-    * Use instruments
-    * Manupliate instruments live
-    * Maybe Music?
+  Musical Mission:
+    * Create instruments new heard before.
+    * Use instruments for greater good.
+    * Bend/Manupliate instruments live.
+    * Maybe Music...
 "
   (:use overtone.live)
   (:require [codemash.graphs :as graph]))


;;UGENS - Unit generators (Sweet shop)

(defsynth foo [freq 200 dur 0.5]
  (let [src (saw [freq (* freq 1.01) (* 0.99 freq)])
        low (sin-osc (/ freq 2))
        filt (lpf src (line:kr (* 10 freq) freq 10))
        env (env-gen (perc 0.1 dur) :action FREE)]
    (out 0 (pan2 (* 0.8 low env filt)))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Busses - Wiring synths         ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defonce tri-bus (audio-bus)) ;;Audio busses are just ids preallocated at boot.
(defonce sin-bus (audio-bus))

(println sin-bus)

(defsynth divider [div 32 in-bus 0 out-bus 0]
  (out:kr out-bus (pulse-divider (in:kr in-bus) div)))

(defonce root-trigger (trigger [:head timing-g] :rate 100 :in-bus root-trigger-bus))
(defonce root-count   (counter [:after root-trigger] :in-bus root-trigger-bus :out-bus root-count-bus))
(defonce beat-trigger (divider [:after root-trigger] :div current-beat :in-bus root-trigger-bus :out-bus beat-trigger-bus))
(defonce beat-count   (counter [:after beat-trigger] :in-bus beat-trigger-bus :out-bus beat-count-bus))
(defsynth get-beat [] (send-trig (in:kr beat-trigger-bus) count-trigger-id (+ (in:kr beat-count-bus) 1)))

(defonce beat (get-beat [:after beat-count]))

(require '[overtone.inst.drum :as drum])
(on-trigger count-trigger-id (fn [x] (drum/kick)) ::beat-watch)

(ctl beat-trigger :div 10)
(ctl root-trigger :rate 100)

(comment (remove-event-handler ::beat-watch))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Building Sequencer with buffers;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(def beats-g (group "beats"))

(def kick-s (load-sample "~/Workspace/music/samples/sliced-p5/kick.aif"))

(defonce kick-sequencer-buffer (buffer 8))

(sample-player kick-s)

(defsynth mono-sequencer
  [buf 0 rate 1 out-bus 0 beat-num 0 sequencer 0 numsteps 8 amp 1]
  (let [cnt      (in:kr beat-count-bus)
        beat-trg (in:kr beat-trigger-bus)
        bar-trg  (and (buf-rd:kr 1 sequencer cnt)
                      (= beat-num (mod cnt numsteps))
                      beat-trg)
        vol      (set-reset-ff bar-trg)]
    (out
     out-bus (* vol
                amp
                (pan2
                 (rlpf
                  (scaled-play-buf 1 buf rate bar-trg)
                  (demand bar-trg 0 (dbrown 200 20000 50 INF))
                  (lin-lin:kr (lf-tri:kr 0.01) -1 1 0.1 0.9)))))))


(def kicks
  (doall
   (for [x (range 8)]
     (mono-sequencer [:tail beats-g] :buf kick-s :beat-num x
                     :sequencer kick-sequencer-buffer))))

(buffer-write! kick-sequencer-buffer [1 0 0 0 0 1 0 0])

(defonce tom-sequencer-buffer (buffer 8))
(defonce tom-s (load-sample "~/Workspace/music/samples/sliced-p5/tom.aif"))

(def toms
  (doall
   (for [x (range 8)]
     (mono-sequencer [:tail beats-g] :buf tom-s :beat-num x
                     :sequencer tom-sequencer-buffer))))

(buffer-write! tom-sequencer-buffer [0 0 1 0 0 0 1 0])

(defonce shake-sequencer-buffer (buffer 8))
(defonce shake-s (load-sample "~/Workspace/music/samples/sliced-p5/shaker.aif"))

(def shakes
  (doall (for [x (range 8)]
           (mono-sequencer [:tail beats-g] :buf shake-s :beat-num x
                           :sequencer shake-sequencer-buffer))))

(buffer-write! shake-sequencer-buffer [0 1 0 0 0 0 0 0])

(defonce shake2-s (load-sample "~/Workspace/music/samples/sliced-p5/double-shake.aif"))
(defonce shake2-sequencer-buffer (buffer 8))

(def shakes2
  (doall
   (for [x (range 8)] (mono-sequencer [:tail beats-g] :buf shake2-s :beat-num x
                                      :sequencer shake2-sequencer-buffer))))

(buffer-write! shake2-sequencer-buffer [0 0 0 1 0 0 0 0])

(defonce shake2d-s (load-sample "~/Workspace/music/samples/sliced-p5/double-shake-deep.aif"))
(defonce shake2d-sequencer-buffer (buffer 8))

(def shakes2d
  (doall
   (for [x (range 8)] (mono-sequencer [:tail beats-g] :buf shake2d-s :beat-num x
                     :sequencer shake2d-sequencer-buffer))))

(buffer-write! shake2d-sequencer-buffer [0 0 0 0 0 0 0 1])


(defonce shake1-s      (load-sample "~/Workspace/music/samples/sliced-p5/single-shake.aif"))

(defonce shake1-sequencer-buffer (buffer 8))

(def shakes1
  (doall
   (for [x (range 8)] (mono-sequencer [:tail beats-g] :buf shake1-s :beat-num x
                     :sequencer shake1-sequencer-buffer))))

(buffer-write! shake1-sequencer-buffer [0 0 0 0 1 0 0 0])


(stop)

;;;;;;;;;;;;;;;;;;;;;;;
;; Timing and Buffers;;
;;;;;;;;;;;;;;;;;;;;;;;

(require '[codemash.timing :as time])

