 (kill mid)
 (stop)
 
-;;;;;;;;;;;;;;;;;;;;;;;;
-;;Creating a Sequencer;;
-;;;;;;;;;;;;;;;;;;;;;;;;
-
-(def b (buffer 256))
-
-;;Samples
+;;;;;;;;;;;;;;;;;;;;;;;
+;; Timing AND Buffers;;
+;;;;;;;;;;;;;;;;;;;;;;;
+
+(def score-b         (buffer 128))
+(def duration-b      (buffer 128))
+(def bass-duration-b (buffer 128))
+(def bass-notes-b    (buffer 128))
+
+(def score [:F4 :F4 :F4 :F4 :F4 :F4 :F4
+            :G4 :G4 :G4 :G4 :G4 :G4 :G4 :G4 :G4 :G4 :G4 :G4 :G4 :G4 :G4 :G4
+            :BB4 :BB4 :BB4 :BB4 :BB4 :BB4
+            :D#4 :D#4 :D#4])
+
+(def duration     [1/7])
+
+(def score (concat
+            (map #(+ -5  (note %)) score)
+            (map #(+ -5  (note %)) score)
+            (map #(+ -10 (note %)) score)
+            (map #(+ -5  (note %)) score)
+            (map #(+ -1  (note %)) score)))
+
+(defsynth woody-beep [duration-bus 0 beat-count-bus 0 offset-bus 0 amp 1 out-bus 0]
+  (let [cnt    (in:kr beat-count-bus)
+        offset (buf-rd:kr 1 offset-bus cnt)
+        durs   (buf-rd:kr 1 duration-bus cnt)
+        trig (t-duty:kr (dseq durs INFINITE))
+        freq (demand:kr trig 0 (drand offset INFINITE))
+        freq (midicps freq)
+
+        env (env-gen:ar (env-asr :release 0.25 :sustain 0.8) trig)
+        tri (* 0.5 (lf-tri:ar freq))
+        sin (sin-osc:ar (* 1 freq))
+        sin2 (sin-osc:ar (* 1.01 freq))
+        wood (bpf:ar (* (white-noise:ar) (line:kr 5 0 0.02)) freq 0.02)
+        src (mix [sin sin2 tri wood])
+        src (free-verb src)]
+    (out:ar out-bus (* amp env (pan2 src)))))
+
+    (def w  (woody-beep :duration-bus duration-b :beat-count-bus beat-count-bus :offset-bus score-b :amp 4))
+
+(defsynth deep-saw [freq 100 beat-count-bus 0 offset-bus 0 duration-bus 0 out-bus 0 amp 1 pan 0]
+  (let [cnt    (in:kr beat-count-bus)
+        offset (buf-rd:kr 1 offset-bus cnt)
+        durs   (buf-rd:kr 1 duration-bus cnt)
+        trig (t-duty:kr (dseq durs INFINITE))
+        freq (demand:kr trig 0 (drand offset INFINITE))
+        freq (midicps freq)
+
+        saw1 (lf-saw:ar (* 0.5 freq))
+        saw2 (lf-saw:ar (* 0.25 freq))
+        sin1 (sin-osc freq)
+        sin2 (sin-osc (* 1.01 freq))
+        src (mix [saw1 saw2 sin1 sin2])
+        env (env-gen:ar (env-asr) trig)
+        src (lpf:ar src)
+        src (free-verb src 0.33 1 1)]
+    (out out-bus (* amp [src src]))))
+
+(def ps (deep-saw 100 :duration-bus bass-duration-b :beat-count-bus beat-count-bus :offset-bus bass-notes-b :amp 0.9))
+
+(ctl ps :damp 1)
+(ctl ps :room 1)
+
+(kill ps)
+
+(buffer-write! bass-duration-b (take 32 (cycle [(/ 1 3.5)])))
+(buffer-write! bass-notes-b (take 32 (cycle (map note [:F2 :F2 :G3 :G2 :G3 :BB2 :BB2 :G2 :G2]))))
+
+(buffer-write! bass-notes-b (take 32 (cycle (map #(+ 0 (note %)) [:F2 :F2 :G3 :G2 :G3 :BB2 :BB2 :G2 :G2]))))
+
+(buffer-write! score-b (take 128 (cycle (map note score))))
+(buffer-write! duration-b (take 128 (cycle duration)))
+
+(ctl root-trigger :rate 100)
+(ctl beat-trigger :div 30)
 
 ;;Hardware (External devices)
 
 (midi-connected-devices)
 
-(on-event
- [:midi :note-on]
- (fn [e] (when-let [drum (drum-kit (:note e))] (drum)))
- ::drumkit)
+(on-event [:midi :note-on]
+          (fn [m]
+            (buffer-write! music-buffer (map midi->hz
+                                             (map (fn [midi-note] (+ -12 midi-note))
+                                                  (repeat 256 (:note m))))))
+          ::phat-bass-keyboard)
 
 (comment
   (remove-event-handler ::drumkit))

;; play{d=Duty;f=d.kr(1/[1,2,4],0,Dseq([0,3,7,12,17]+24,inf));GVerb.ar(Blip.ar(f.midicps*[1,4,8],LFNoise1.kr(1/4,3,4)).sum,200,8)}

(defsynth pop [freq 300]
  (let [sin1 (* 1.1 (sin-osc freq))
        sin2 (sin-osc freq)
        sin3 (* 0.9 (sin-osc freq))
        src (mix [sin1 sin2 sin3])]
    (out 0 (* amp (pan2 src)))))

(def p (pop))



(defonce main-g  (group "main"))
(defonce early-g (group "early" :head main-g))
(defonce late-g  (group "late"  :after early-g))

(def tri-synth-inst (tri-synth [:tail early-g] :out-bus tri-bus))
(def sin-synth-inst (sin-synth [:tail early-g] :out-bus sin-bus))

(defsynth modulated-freq-tri [freq-bus 0 mid-freq 220 freq-amp 55]
  (let [freq (+ mid-freq (* (in:kr freq-bus) freq-amp))]
    (out 0 (pan2 (lf-tri freq)))))

(comment
  (def mft (modulated-freq-tri [:tail late-g] sin-bus)))

(comment
  (ctl mft :freq-bus sin-bus))

(comment
  (ctl tri-synth-inst :freq 0.5))

(comment
  (do
    (kill mft))
  )

;;;;;;;;;;;;;;;
;;Instruments;;
