Skip to content

Instantly share code, notes, and snippets.

@rogerallen
Created January 19, 2015 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogerallen/c4be4d970b61a4717012 to your computer and use it in GitHub Desktop.
Save rogerallen/c4be4d970b61a4717012 to your computer and use it in GitHub Desktop.
overtone at & recursive playing example
(ns overtone-at-example
(:use [overtone.live]
[overtone.inst.sampled-piano]))
(def metro (metronome 110))
(defn play
[beat note]
(let [{:keys [pitch level dur]} note
cur-synth (at (metro beat) (sampled-piano :note pitch :level level))]
(at (metro (+ beat dur)) (ctl cur-synth :gate 0))))
(defn play-chord
[beat notes]
(doall (map #(play beat %) notes)))
(defn play-seq
[beat chords]
(let [cur-chord (first chords)
dur (:dur (first cur-chord))]
(println "play!" beat)
(play-chord beat cur-chord)
(apply-by (metro (+ beat dur)) #'play-seq [(+ beat dur) (rotate 1 chords)])))
(comment
;; play a note
(play (metro) {:pitch 60 :level 0.9 :dur 2})
;; play a chord
(play-chord (metro) [{:pitch 60 :level 0.9 :dur 2}
{:pitch 64 :level 0.9 :dur 1}
{:pitch 67 :level 0.9 :dur 1}
])
;; play several chords
(play-seq (metro)
[[{:pitch 60 :level 0.9 :dur 2}
{:pitch 64 :level 0.9 :dur 1}
{:pitch 67 :level 0.9 :dur 1}]
[{:pitch 70 :level 0.9 :dur 2}
{:pitch 74 :level 0.9 :dur 1}
{:pitch 77 :level 0.9 :dur 1}]
[{:pitch 50 :level 0.9 :dur 2}
{:pitch 54 :level 0.9 :dur 1}
{:pitch 57 :level 0.9 :dur 1}]]
)
(defn play-seq [beat chords]) ;; exec to stop
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment