Created
January 11, 2016 14:20
-
-
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)
This file contains hidden or 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
| (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