Wednesday, January 15, 2014

midi in clojure

It was a pleasant surprise for me to learn that core java ships with midi support. Mix that with clojure and one can have some music up and running really quickly

(ns music.core)

(import 'javax.sound.midi.MidiSystem)

(def synth (. MidiSystem getSynthesizer))

(. synth open)

(def mc (. synth getChannels))

(def instr (. (. synth getDefaultSoundbank) getInstruments))

(def yy (. synth loadInstrument (aget instr 0)))

(defn dhun [c f] (. (aget mc c) noteOn f 40))

(defn tune [f]
  (new Thread (fn [] (loop [] (f) (recur)))))

(def beats (tune (fn [] (dhun 9 40) (Thread/sleep 500))))

(def lead (tune (fn [] (dhun 0 40) (Thread/sleep 500))))

No comments:

Post a Comment