Skip to content

Instantly share code, notes, and snippets.

@alandipert
Created March 31, 2010 10:01
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 alandipert/350150 to your computer and use it in GitHub Desktop.
Save alandipert/350150 to your computer and use it in GitHub Desktop.
(import '(javax.sound.midi MidiSystem Synthesizer))
(defn play-note [synth channel note]
(let [n (:note note 60)
v (:velocity note 127)
d (:duration note 1000)]
(. channel noteOn n v)
(Thread/sleep d)
(. channel noteOff n)))
(defn play-notes
[notes]
(with-open [synth (doto (MidiSystem/getSynthesizer) .open)]
(let [channel (aget (.getChannels #^Synthesizer synth) 0)]
(loop [remaining notes]
(when-not (empty? remaining)
(play-note synth channel (first remaining))
(recur (rest remaining)))))))
(defn play-c-major-scale []
(let [c-major [0 2 4 5 7 9 11 12]]
(play-notes (map #(hash-map :note (+ % 60) :duration 400)
(cycle (concat c-major (rest (reverse (rest c-major)))))))))
(play-c-major-scale)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment