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:

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!

The Journalist Hustings

Candidates divided over how to take magazine online and charging for it

Candidates at the hustings last night. From left to right:

Last night seven of the eight candidates for the next editor of the NUJ’s magazine the Journalist were at hustings hosted by London Press & PR Branch at the NUJ’s head office in Kings Cross.

Each candidate was first given five minutes to introduce themselves and their candidacy after which questions were taken from the audience.

Between the seven candidates almost all sectors of the industry were represented, with a heavy leaning towards print. Candidates varied greatly in their union involvement – Christine Buckley, Michael Cross and Mark Watts all used the phrase “I’m a journalist, not an activist”

One of the most divisive subjects between the candidates was the level of involvement that online should have. Some said it should continue in it’s current form as ‘complementary’ with a PDF of the print edition, others were much bolder in their proposals of daily news items online, more discussion of issues and the use of social media.

Candidates were asked how they would generate income outside the central funding allocated to the magazine, Steve Usher said “The magazine is a potential goldmine to fund all the web work we need to do. It goes out to over 40,000 ABC1 journalists, advertisers offered that will go for it big time” Richard Simcox proposed that revenue should be increased by the recruitment of new members, not advertising. He quipped “I’m a union rep, not an ad sales rep” Mark Watts said that as the Journalist was specialist content he would charge non-union members for access online.