Created
March 31, 2010 10:01
-
-
Save alandipert/350150 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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