Build your own Sonos player with Ruby on Rails

Hardware you'll need:

  1. A computer running linux (I used Fedora Core 4)
  2. Nokia 770 (or some other web tablet)
  3. An Airport Express

Software (all installed on the linux server):

  1. Ruby
  2. RubyGems
  3. MySQL
  4. id3lib
  5. id3lib-devel
  6. raop-play (for streaming to the Airport Express)


After you've installed all of those packages, you'll need to install a few gems.

  1. rails
  2. id3lib-ruby
  3. mysql
  4. rake
  5. net-mdns

To install these, just execute the following as root:

# gem install rake rails id3lib-ruby mysql net-mdns -y

Once you've got all that installed, download the OSAMP code from here. After you have untarred the OSAMP code, create your sonos development database, and tables like this:

$ mysql -h localhost -u root -e 'create database sonos_development' -p
Enter password:
$ cd sonos
$ rake migrate

After you execute 'rake migrate', a bunch of messages will scroll by indicating that the tables are being created. Once your database is created, you'll need to populate it with your mp3's. Do this by entering the bin directory and executing 'populate_db.rb' and pointing it to your mp3 collection.

$ cd bin
$ ruby populate_db.rb /home/aaron/mp3

Once you database is populated, you can start up the server. There are two main parts to the Open Sonos Player. The back end script that interacts with raop-play, and the rails app with provides the front end to the web client. When a song is selected on the front end, it sends a message to the back end to play the song. Start the back end by going in to the 'bin' directory and executing the 'aexplay.rb' script:

$ ruby bin/aexplay.rb &

aexplay.rb will auto-detect the Airport Express that is on the network, and stream to the first one it finds, so make sure your Airport Express is plugged in and working before you execute this script. Now start up your rails app:

$ ruby script/server

Once the rails app is up and running, you should be able to navigate to the rails app ( http://localhost:3000/music ) and control your Airport Express. Just open the url to your rails app on the Nokia 770, and you should be done!

Here are some screen shots of it running on my Nokia 770:
IMG_0171.JPGIMG_0172.JPGIMG_0173.JPG

Cost of the project, assuming you already have a linux machine (you had to rip the music somehow, right?):

  • Nokia 770: $359.99
  • Airport Express: $129.00

Total: $488.99

To make this setup equivalent to the Sonos system you would need two Airport Expresses, putting your total at $617.99 compared to the Sonos at $999.00. Make it even cheaper by buying used or using something other than the Nokia 770.

Every "zone" that you add to this system will cost $129.00 vs. $349.00 from Sonos. So that is a $381.01 (or 38%) savings upfront with a $220 (or 63%) ongoing savings if you add more "zones".

8 Comments

  1. Ben
    Posted February 28, 2008 at 3:37 pm | Permalink

    First off, strange that no one has responded to this. On the other DIY pages, with far less useful info, there are veritable verbal fist fights. So, I notice that this was written quite a long time ago, and was wondering if you would make any changes to the setup. From a software perspective, I know Jinzora works pretty well, but am a bit of a noob so I might miss the point of the ruby/rail setup. I like this setup since it using parts at hand to replace expensive media receivers (which you could also control with a laptop until you got money for the used tablet). So the next question is about the airport express: are there other alternatives to this, or is just a really nice AP of sort? Thanks again for the entry, this is something I really want and this would make it cost a lot less.

  2. Posted February 28, 2008 at 3:53 pm | Permalink

    Hey Ben! Yes, this was written quite a while ago. I haven’t been using it lately, and I really need to update the rails application I wrote. One thing that I would change is my dependency on raop-play. I wrote a pure ruby version that I would use instead.

    You could probably use something besides the Airport Express, but you would have to figure out some way to stream music to that device. I need to update the code base, but I’ve been ignoring it because people didn’t seem interested!

  3. Ben
    Posted February 28, 2008 at 9:29 pm | Permalink

    No actually this is perfect. I have a pretty small budget to work with, and therefore am doing things which can build on each other gradually with expandability in mind. So, grabbing an old computer this week to get a media server going on using Benji’s how to, except maybe starting with the Ubuntu Server Edition so that LAMP is already up. http://rubbervir.us/projects/ubuntumediaserver/ . Quick on that, these scripts should work within Ubuntu right? But I have really dug the Sonos system because of the controller and didn’t have anything that would work well to control the slimbox, so this way I can get something more versatile such as the 770, and then have more flexability in how and what I stream (file format concerns). Seriously though, I think it sounds pretty cool, and once I get the cash will invest in this end. Btw, any sound quality issues coming out of the Airport Express? Doesn’t seem like there is a better AP out there (and not for much cheaper). Let me know about the updates and improvements since I will do this by the summer.

  4. Ben
    Posted February 29, 2008 at 8:25 am | Permalink

    I realized after my last post that I didn’t ask some other important questions (sorry I am a bit of a noob on this):
    1) what file types does the open source sonos player (and I guess Airport Express) support?
    2) once you set the player on the server to stream, does the controller (PC or tablet PC) have to remain on to maintain the stream?
    3) do you think the airport express back-end could be fitted to other players running off of the server?
    4) is there not a general UPNP approach to take on a media AP (yes, I am a noob)?

    Sorry for the basic questions, and thanks for the great answers and please keep up the good code work.

  5. Posted February 29, 2008 at 9:43 am | Permalink

    Basically, the Airport Express will (sort of) accept any format. You have to decode your audio source before you send it to the airport express. So if you’re playing mp3s, you have to decode the mp3 to raw audio before sending it to the Airport Express. Same thing with Ogg. Basically, if you can play the audio file on linux, you can stream it to the Airport Express.

    2) No, the controller doesn’t have to be on. The server just has to keep streaming.

    3) Possibly. What did you have in mind? The stuff I have written is pretty specific to the Airport Express, but could be generalized to support other equipment.

    4) I don’t know. I don’t really know much about UPNP. :-(

  6. Ben
    Posted March 3, 2008 at 2:22 pm | Permalink

    Okay, so I really missed some of the points, so thanks for understanding my lack of understanding. So, about the third point, I am not really sure what I had in mind either. For the airport express, you really do get what seems like a pretty neat piece of equipment for the money, and I can’t seem to find anything out there that might work better (Linksys has something that people haven’t seemed to enjoy). When I was asking about the other UPNP options, this was more because there are a lot of digital media receivers coming onto the market (like D-link, which works really well with their NAS I believe), which are different from the Sonos and Squeezeboxes of the world, and it would be cool to also use the N770 to control them. Looking around, I am not quite sure how to control this, but this is because I just haven’t seen one in person. Here is an article about the N800 and UPNP:

    http://withouthyphens.wordpress.com/2007/05/30/playing-with-upnp-on-the-n800-windows-and-linux/

    Anyways, please keep the code coming and doing a great job about being innovative. Something that one day would be great is having this code more as a plugin for Rhythmbox or Amarok or even Slimserver. Personally the only thing I didn’t like about moving to Linux was losing Winamp, whose interface I loved, but the abundant codecs have been great. Will touch base again once I get my system going to let you know how it worked.

  7. Ben
    Posted March 14, 2008 at 5:52 am | Permalink

    So, maybe an alternative to the crappy linksys and apple options:

    http://www.slimdevices.com/pi_receiver.html

    Since they just came out with this I am totally unfamiliar with it, but it it might be a bit easier to control though I am not sure. Let me know what you think, and I am sure using the N770 as a controller is still way cooler.

  8. Rod
    Posted April 3, 2008 at 9:46 am | Permalink

    I’ve been curious about building a Sonos clone, with exactly the same hardware (Nokia Tablet & Airport Express). I just now stumbled across this post, I am surprised too at the lack of comments. Anyways, I installed the free SqueezeCenter (formerly SlimServer) and tried a few of the touch screen friendly skins. It works OK, a bit slow I think using iPod touch to navigate the AJAX skins. I’m not sure why RAOP isn’t included in SqueezeCenter since it is open source and all. Even as a plugin it would be great for those who have existing Airport Express devices. SqueezeCenter provides an mp3 stream which I opened in iTunes which sent the output audio to the Airport Express. What a hack.

    I hope you’ll continue development on this project!

Post a Comment

Your email is never shared. Required fields are marked *

*
*
Check Spelling
Activate Spell Check while Typing