 (defsynth divider [div 32 in-bus 0 out-bus 0]
   (out:kr out-bus (pulse-divider (in:kr in-bus) div)))
 
-(defonce root-count   (counter [:head timing-g] :in-bus root-trigger-bus :out-bus root-count-bus))
 (defonce root-trigger (trigger [:head timing-g] :rate 100 :in-bus root-trigger-bus))
-(defonce beat-trigger (divider [:after root-count] :div current-beat :in-bus root-trigger-bus :out-bus beat-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)))
 
-(get-beat)
-
+(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 60)
+(ctl beat-trigger :div 10)
 (ctl root-trigger :rate 100)
 
 (comment (remove-event-handler ::beat-watch))
 
-;; Timing and Buffers
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;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])
 

    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?
"
  (:use overtone.live)
  (:require [codemash.graphs :as graph]))


;;Sampled
(comment (require 'overtone.inst.sampled-piano))
(require '[overtone.inst.piano :as piano])

(require '[overtone.inst.sampled-piano :as s-piano])

(piano/piano :note 50)
(s-piano/sampled-piano :note 50)

;;Processing GUI
(require '[quil.core :as q :refer [defsketch]])
