Streaming to an Airport Express in Ruby
In the current Apple frenzy, what’s better than a post about an old Apple product: the Airport Express. Small and versatile, this device can be used for several different applications but it’s most popular to send music over wi-fi (to your good ole’ amplifier located on the other side of your apartment for example). It’s also a pretty cheap thing.
I mentioned in my last post that I was using my little last.fm reader script to feed an Airport Express and somebody asked me how. First, I have to say that there isn’t much to do, thanks to the nice raop client created by Aaron Patterson. So like many other Ruby hacks, it starts with a:
gem install raop-client
Playing an MP3 file to your Airport is now just a small script and a command away:
[source:ruby]
require ‘raop’
raop = Net::RAOP::Client.new(“192.168.1.63″)
raop.connect
# Closer to 0 is louder
raop.volume=5
raop.play $stdin
[/source]
What looks like an IP address above (192.168.1.63) really is an IP address. It’s the one of your Airport Express on your local network, I’m sure you now how to find it (hint: your router probably knows). Save this script in a file (aex.rb for example). It takes a stream of raw wav as input, so we’ll need to summon the power of LAME (Lame Ain’t an MP3 Encoder) to produce the stream:
lame --decode "Joe Dassin - Les Champs-Élysées.mp3" - | ruby aex.rb
Mind the dash after the file name, it’s important. If you’re not a Linux happy user ™, you’ll need to get LAME. If you’re a Windows user, good luck with the pipe. Divide & Conquer with Cygwin would probably work though.
Note that making open source software that uses MP3 files isn’t an easy task. The Fraunhofer Society owns a patent on the MP3 format and has enforced it in several occasions (generating a good deal of revenue). Everything that encodes, decodes or reads MP3s have to pay and yes, that includes your iPod. I don’t think it ever went directly against open source developers but then it never said it wouldn’t either. A very reasonable move would be to authorize GPL licensed software to use their patent. I’m sure they use open source software as well, so they should really stop leaching.
Anyway you’re now happily playing songs to your Airport Express but you’re still missing the last.fm part. As I said, raop client accepts any wav stream so it’s just a matter of piping the last.fm stream (obtained in my last installment) through LAME and redirecting that to raop client.
To glue this with the last.fm script I’ve shown you last time, just do:
lastfm = LastFM.login("mriou", "****")
lastfm.tune("lastfm://user/mriou/playlist")
aex = AirportListener.new("192.168.1.65")
lastfm.play(aex)
If you want to see the whole script plus a few additional niceties, it’s available on Rubyforge. You can checkout the whole thing and then run bin/lastfm.rb.
svn co svn://rubyforge.org/var/svn/dubya/trunk dubya
Happy listening!
Matthieu Riou on July 9th 2007 in Uncategorized

