Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A clojure friendly library for playing sounds

Tags:

clojure

audio

I'm looking for an easy to program library for infrequently playing sounds (notifications and the like) from a clojure function.

edit: like this

(use 'my.sound.lib') 
(play-file "filename")
(beep-loudly)
(bark-like-a-dog)
...
like image 653
Arthur Ulfeldt Avatar asked Jan 19 '10 20:01

Arthur Ulfeldt


1 Answers

OK, with the question now including an API wishlist... ;-)

You can use JLayer for MP3 playback on the JVM. On Ubuntu it's packaged up as libjlayer-java. There's a simple example of use in Java here. A Clojure wrapper:

(defn play-file [filename & opts]
  (let [fis (java.io.FileInputStream. filename)
        bis (java.io.BufferedInputStream. fis)
        player (javazoom.jl.player.Player. bis)]
    (if-let [synchronously (first opts)]
      (doto player
        (.play)
        (.close))
      (.start (Thread. #(doto player (.play) (.close)))))))

Use (play-file "/path/to/file.mp3") to play back an mp3 fly in a separate thread, (play-file "/path/to/file.mp3" true) if you'd prefer to play it on the current thread instead. Tweak to your liking. Supply your own loud beep and barking dog mp3. ;-)

For a load beep and the like, you could also use MIDI... Perhaps this blog entry will be helpful if you choose to try.

Also, the link from my original answer may still be helpful in your tweaking: Java Sound Resources: Links.

like image 148
Michał Marczyk Avatar answered Nov 23 '22 01:11

Michał Marczyk