 
 (defn split [t fraction]
   (letfn [(f [note]
-            [(-> note
-                 (assoc :duration fraction))
-             (-> note
-                 (update-in [:duration] - fraction)
+            [(-> note (assoc :duration fraction))
+             (-> note (update-in [:duration] - fraction)
                  (update-in [:time] + fraction))])]
     (vary t f)))
 

            [leipzig.live :as live]
            [leipzig.live :refer [stop]]))

; Natural numbers
(comment
  (range 0 8))

; Raw frequencies
(comment
  (->> (phrase (repeat 1/4) [440 494 554 622 659 740 831 880])
       live/play))

; Midi
(comment
  (->> (phrase (repeat 1/4) [69 71 73 74 76 78 80 81])
       (where :pitch temperament/equal)
       live/play))

; Scales
(def major (scale [2 2 1 2 2 2 1]))
(def minor (scale [2 1 2 2 1 2 2]))
(def pentatonic (scale [2 3 2 2 3]))
(def car (comp high pentatonic -))

(comment
  (->> (phrase (repeat 1/4) (range 0 8))
       (where :pitch (comp temperament/equal A major))
       live/play))

(defn forever [riff]
  (concat riff (lazy-seq (->> riff forever (after (duration riff))))))

(defn clapping-music []
  (let [african-bell-pattern (rhythm [1/8 1/8 1/4 1/8 1/4 1/4 1/8 1/4])]
    (->> african-bell-pattern forever (all :part :clap1)
         (canon #(->> % (take 32) (then (rhythm [1/8])) forever (all :part :clap2))))))

(comment
  (->> (clapping-music)
       live/play))

(defn vary [t f]
  (fn [notes]
    (let [before? #(-> % :time (< t))


(defn part [model & variations]
  ((apply juxt identity variations) model))

(def first-drum
  (let [model (->> (rhythm [2/5 3/5 1/5 1/5 3/5]) (all :part :clap1))
        a (split 6/5 1/10)
        b (split 5/5 1/10)
        c (comp (split 17/15 2/15) (split 5/5 2/15) (omit 6/5))
        d (comp (omit 7/5) (omit 6/5))]
    (part model a b c d)))

(def second-drum
  (let [model (->> (rhythm [2/5 2/5 4/5 1/5 1/5]) (all :part :clap2))
        a (omit 9/5)
        b (omit 0)
        c (comp a b)
        d #(then % (->> (rhythm (repeat 5 2/5)) (all :part :clap2)))]
    (part model a b c d)))

(def third-drum
  (let [model (->> (rhythm [2/5 1/5 2/5]) (all :part :clap3))
        a (omit 2/5)
        b (omit 3/5)
        c (comp (split 0 1/5) a)
        d (split 3/5 1/5)
        e (comp (split 3/5 1/10) d)
        f (split 0 1/5)
        g (comp (split 0 1/10) f)]
    (part model a b c d e f g)))

(def aga-terumo ; p299
  (let [drums [first-drum second-drum third-drum]]
    (->> drums
         ;(map (comp list first))
         (map rand-variations)
         (map after (range 0 (* 4 3) 4))
         (reduce with))))

(comment
  (->>
    aga-terumo
    (tempo (bpm 90))
    (live/play)))

; Clapping music
(defn forever [riff]
  (concat riff (lazy-seq (->> riff forever (after (duration riff))))))

(defn clapping-music []
  (let [african-bell-pattern (rhythm [1/8 1/8 1/4 1/8 1/4 1/4 1/8 1/4])]
    (->> african-bell-pattern forever (all :part :clap1)
         (canon #(->> % (take 32) (then (rhythm [1/8])) forever (all :part :clap2))))))

(comment
  (->> (clapping-music)
       live/play))

; Natural numbers
(comment
  (range 0 8))

; Raw frequencies
(comment
  (->> (phrase (repeat 1/4) [440 494 554 622 659 740 831 880])
       live/play))

; Midi
(comment
  (->> (phrase (repeat 1/4) [69 71 73 74 76 78 80 81])
       (where :pitch temperament/equal)
       live/play))

; Scales
(def major (scale [2 2 1 2 2 2 1]))
(def minor (scale [2 1 2 2 1 2 2]))
(def pentatonic (scale [2 3 2 2 3]))
(def car (comp high pentatonic -))

(comment
  (->> (phrase (repeat 1/4) (range 0 8))
       (where :pitch (comp temperament/equal A major))
       live/play))

(def tete
  (let [model (phrase [8/4 3/4 5/4] (repeat 0))
