Audio on the Raspberry Pi with Node.js

I recently wanted to play some audio through the Raspberry Pi programatically, but primarily being a web developer I wasn’t really sure how to approach the task. I can manipulate the DOM all day without breaking a sweat, but without the Web Audio API or <audio> element I was a bit stumped. Previously I’ve played around with Node.js on the Pi, but I didn’t think it would have any packages for audio and thought I’d have to use Python or Scratch which are well supported on the Pi.

Turns out you can use Node and it’s pretty simple too. I based my implementation on this Gist:

https://gist.github.com/TooTallNate/3947591

If you’re not familiar with Node, I’ll explain a little about what this does. The first few lines are importing all the modules we’ll need: fs the filesystem module baked into Node, which will read in the MP3. Next lame which will decode the MP3 into raw PCM data. Finally speaker that simply outputs PCM audio data you feed it.

In this example the file is passed to Node as an argument when the script is run from the command line, like so:

$ node mp3player.js sounds/file.mp3

The MP3 is piped through lame and then as it is decoded output to speaker. This worked right out of the box on my Mac, but a bit of fiddling was required to get it working on the Pi.

First lame needed to compile a bunch of native extensions for the MP3 decoding, this wasn’t really a problem as it compiled cleanly the first time, it just took a while.

Second, the program ran without error, but I couldn’t hear anything. Turns out the Pi can output audio over HDMI or the 3.5mm stereo jack, but in my case it was sending the audio over HDMI, even though the monitor connected didn’t have speakers. Fortunately you can override the output and the following command will switch it to the stereo jack:

$ amixer cset numid=3 1

After that it worked a treat, playing crisp and clear sound.

I didn’t realise it at the time, but I’ve just noticed that the Gist and modules are all by Nathan Rajlich, nice work!

Quick, easy and free prototyping on Heroku

Matt Griffin’s recent article on A List Apart about designing responsive sites in HTML, rather than something like OmniGraffle or Photoshop is a great idea and something I’ve done a few times before.

Clients love it as it let’s them click through a site, resize their browser, look at it on their smartphone and see hover states – rather than go through pages and pages of ‘static’ designs. You do spend longer on the wireframe and design phases than you would traditionally, but as the article mentions, you’ll have a boat load of frontend work done before you go full on into build.

One new thing that caught my attention was the use of Heroku as the service where they hosted the wireframes, which is a great idea – you get everything in version control from the start, don’t have to worry about setting up hosting or domains on your own servers and best of all it’s free.

I spent a couple of hours this afternoon getting to grips with the platform and knocking together a lean prototyping kit, which I’d like to release after I’ve given it a bit more polish. You can’t just host static content on Heroku, but it’s easy to use one of the supported languages (PHP is supported, but currently undocumented), to wrap up and serve any static content.

With just a couple of dozen lines using the Express framework on Node.js you can build an app that serves plain ol’ HTML as well as doing cleverer stuff using Jade templates and routing (a bit like fancy permalinks in WordPress).

Responsive web design requires a whole new way of thinking about how sites are built, so having a tools like this makes the process better for clients and easier for developers.