Skip to content

Instantly share code, notes, and snippets.

@robert-spurrier
Created January 11, 2016 14:20
Show Gist options
  • Select an option

  • Save robert-spurrier/d0197af6b5382602b904 to your computer and use it in GitHub Desktop.

Select an option

Save robert-spurrier/d0197af6b5382602b904 to your computer and use it in GitHub Desktop.
Using Clojure with Java interop to play a sample (javax.sound.sampled)
(ns sample-fiddlin.core
(import javax.sound.sampled.AudioSystem
javax.sound.sampled.DataLine$Info
javax.sound.sampled.Clip)
(:gen-class))
;; (play "clap/clap-808.wav")
(defn play [sound]
(let [audio-file (java.io.File. (str "dev-resources/" sound))
audio-stream (AudioSystem/getAudioInputStream audio-file)
audio-format (.getFormat audio-stream)
audio-info (DataLine$Info. Clip audio-format)
audio-clip (cast Clip (AudioSystem/getLine audio-info))]
(.open audio-clip audio-stream)
(.start audio-clip)
(Thread/sleep 500)
(.close audio-clip)
(.close audio-stream)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment