Skip to content

Instantly share code, notes, and snippets.

@fogus
Forked from alandipert/midi.clj
Last active August 29, 2015 14:18
Show Gist options
  • Save fogus/4a5df6b47007d8dbc12b to your computer and use it in GitHub Desktop.
Save fogus/4a5df6b47007d8dbc12b to your computer and use it in GitHub Desktop.
(import '(javax.sound.midi MidiSystem Synthesizer))
(defn play-note [synth channel note-map]
(let [{:keys [note velocity duration]
:or {note 60
velocity 127
duration 1000}} note-map]
(. channel noteOn note velocity)
(Thread/sleep duration)
(. channel noteOff note)))
(defn play-notes
[notes]
(with-open [synth (doto (MidiSystem/getSynthesizer) .open)]
(let [channel (aget (.getChannels synth) 0)]
(doseq [note notes]
(play-note synth channel note)))))
(defn play-c-major-scale []
(let [c-major [0 2 4 5 7 9 11 12]
c-major-notes (map #(hash-map :note (+ % 60) :duration 100) c-major)]
(play-notes (cycle (concat c-major-notes
(rest (reverse c-major-notes)))))))
(play-c-major-scale)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment