This article is more than 1 year old

P-P-P-Pick up our PENGUIN-POWERED Pi PIPER of Python

Download El Reg's Raspberry Pi jukebox

How-to Turning the Raspberry Pi into a music player is old hat, but turning it into a personalised DJ is slightly more difficult if a lot more interesting.

The Raspberry Pi, an ARM-powered £20 computer sold as the educationalists' dream, is finding its place as a media player in many tech-aware homes, but installing media player XBMC and plugging in a TV is hardly the spirit in which the Pi was conceived, especially when one can get one's hands good and dirty with the minimum of effort.

The Pi arrived at just the right time for me: the home music player I wrote in Java for a book over a decade ago had been abused and extended to the point where it was tottering and leaking memory like nobody's business. When the PC on which it was running coughed and died it was the perfect storm in which to develop an entirely new music playback application for a Raspberry Pi, and a good opportunity to learn a little Python.

One might ask why bother developing a music player when there are so many free solutions available; ones that offer remote control through a web browser and will find album covers too. But none of those solutions has a single-click "play the rest of this album then go back to random" button, the ability to mark some music as less liked nor rotate through one's collection rather than being entirely random, not to mention the critical lack of a "play wife-friendly tracks" button which excludes everything labelled as 1970s Rock.

The "wife-approved music" button has been replaced with "child-friendly" these days, and cuts out tracks marked as having too much swearing while the wife has to put up with the sounds of Tull. But the point is that by developing one's own solution one can create the functionality wanted, and change it to suit changing times, which is surely what the Pi is for.

Client screen shot

Tapping on a queued track changes it, tapping on the playing track changes the preferences, while the volume controls do nothing

At its peak the Java version would respond when the doorbell was pressed: it would pause the music, turn on all the speakers around the house, play back the sound of a wooden knocker, and return things to how they were. It worked too, until we moved house and never wired up the new place for speaker control or doorbell, but the code to do all that still clutters up the application; waiting dutifully for an interrupt that never comes.

Which prompted the clean sweep and deployment of the Pi. The board worked perfectly connected up an HDMI monitor with a USB keyboard and mouse; one can get into X Windows in a moment, but Python is cross platform so one can develop the source on Windows then copy it to the Pi to run when necessary.

I could, of course, have used Java - the computer is capable of running software built with it - but I wanted to gain from the Pi community and as Python is the recommended language it seemed appropriate to embrace it. Not that I relied entirely on the community; I also bought a copy of Python Programming for the Absolute Beginner which cost almost as much as the Pi and is a good deal bigger, but proved incredibly helpful in getting going with the language.

Coding in Python is something of a shock for old hands: it mandates indentation, has dynamic typing, and doesn't need a semicolon to delimit each statement (the habit of adding a semicolon at the end of every line dies hard). Once one gets into the swing of it, the power of the language is remarkable.

The first version of the Java player used MP3 files, but eight or nine years ago IBM's Java library for the MP3 format was withdrawn as it lacked a licence. I was obliged to dig out the CDs and rip the lot into OGG in an attempt to future proof which has, so far, worked. I must point out that one can buy an MPEG2 licence for the Pi, for the princely sum of £2.40, but as the Pygame library copes with OGGs very well I had no need to worry about such things. Even better, the Pygame library is included in Debian Wheezy, the standard Pi distribution of GNU/Linux, the penguin-branded open-source operating system. This simplified things considerably.

The software also relies on tracks being organised in directories for Artist and Album, with file names containing the track number as well as the artist and track name, which is fine if one starts out that way but will need to be tweaked for anyone else wanting to run the code.

 Directory of X:\Breaking Benjamin\Phobia

18/06/2012 15:14 1,287,922 01 Breaking Benjamin - Intro.ogg
18/06/2012 15:15 3,815,186 02 Breaking Benjamin - The Diary Of Jane.ogg
18/06/2012 15:15 4,137,473 03 Breaking Benjamin - Breath.ogg 
...

...the numbering is important for when the "play rest of this album" button is pressed, and the system always prefers to play the next track on an album anyway, selecting that before picking one at random. In practice that results in two or three tracks from an album before the system moves on, which works for me. One can also change the "liked" rating for a track, which biases the randomness of the system so it is more likely to be picked, although that value will drop every time the track is played, and increases every time it's skipped. This ensures the jukebox is rotated over time and according to taste.

Shifting the working software onto the Pi was surprisingly problem free: the code had to be altered to change the path separator, and for some reason the HTTP import statement is different, and the text menu (which is only used for testing) had to be changed as Windows takes integers at the command prompt while Linux considers them to be strings containing numbers, but only that last problem was unexpected.

That just left one problem - the complete lack of signal, of any kind, coming out of the Pi's 3.5mm audio socket. The Pi defaults to output over the HDMI and when I tested my music software with a TV it was equally silent, but other media applications (the aforementioned XBMC) worked fine, so something was wrong. After much hair tearing the problem was solved by turning the volume all the way up, as it turns out that anything less that 100 per cent volume results in silence from the Pi, and as my app set the volume to the middle it never made a sound.

Client preferences screen shot

Do you really want the good Mr Oden popping up at random?

So then it was just a matter of setting the Pi to automatically login on boot-up, and run the Python code (with sufficient privileges so it can set up the HTTP server). It also needed a static IP address after it went missing a couple of times following a bout of power cuts and a slaphappy DHCP server.

Developing the app was fun, and a useful way to learn some Python as well as getting a music system which does exactly what one wants it to do. The Python source is available to look at [ZIP], along with the JavaScript client, but do bear in mind that it's the first Python I've written (and, as should be obvious, I'm no JavaScript expert either).

Like most programmers of my generation I started coding because the computers wouldn't do the things I wanted them to do, and writing software was the only way to resolve that. The Raspberry Pi enables such development, but the flexibility of the Pi also fogs the focus of community; developers are working with a plethora of languages and packages, so it's not always easy to find support for specific problems, although we did get there, eventually.

The Raspberry Pi in its new home

The Pi jukebox in its new home at the back of a living-room cupboard

Download the code here. ®

More about

TIP US OFF

Send us news


Other stories you might like