Pop-free sound from a Raspberry Pi running XBMC

UPDATE: I’ll leave this around for posterity but a large part of this problem has now been fixed in the latest Raspberry Pi firmware. See here for instructions for raspbmc until that gets updated.

I’ve been in the process of setting up a Raspberry Pi in my office so I can play my mp3 collection through my old stereo. It’s generally gone well and I have to take my hat off to the developers of Raspbmc which makes setting up XBMC on the Pi ridiculously easy and fast. It didn’t take me long to have Airplay set up and running as well as being able to use my phone to remote control XBMC to play things direct from my music library sitting on my Synology NAS. Quite a nice setup really.

Just one problem. I play the music out through the Pi’s audio jack which doesn’t have a fantastic DAC. The big noticeable issue is audible pops every-time XBMC starts and stops playing. For Airplay this isn’t too bad, you get a pop when it first starts but only another after you stop playing. Playing direct on XBMC though you get two pops between each track as it stops playing one and starts the next. Very annoying. It’s a pretty well known problem and the best solution so far is to simply never stop playing. If you have a player that uses pulseaudio then you can configure it to keep the audio stream going even when idle. Of course it isn’t that easy, XBMC doesn’t use pulseaudio on the Pi. There is some work going on that might change that but for now it is very buggy to the point of being unusable. It seemed I was stuck … or was I?

It took some experimentation but I finally came across something of a hack that solves the problem for me. It probably works on other distributions but I did all this using Raspbmc.

First as root you want to do this:

echo "snd-bcm2835" >>/etc/modules

This makes the kernel module for the sound devices load on startup. It allows alsa and by proxy pulseaudio to talk to the audio hardware. Next edit /etc/pulse/system.pa and comment out this line:

load-module module-suspend-on-idle

This tells pulseaudio to keep the audio stream alive even when not playing anything. Now reboot your Pi. Once it is started up copy a plain wav file (nothing mp3 encoded or anything) to the Pi, log in and play it through pulseaudio:

paplay test.wav

If it doesn’t come out of the speakers then alsa might be trying to play to the HDMI port. Try running this then running the above command again:

sudo amixer cset numid=3 1

What just happened is pulseaudio played the sound file, but now should have kept the audio hardware active and will continue to do so until the Pi is next turned off. You might think that that might mean that XBMC can’t play anything now. You’d be wrong, it plays just fine and no longer suffers from the popping between tracks. Occasionally I hear a pop when I first start playing after startup, but after that I’ve not heard any issues.

There are probably easier ways to initialise pulseaudio but I don’t mind playing some sound on every startup to let me know my Pi is ready. I made it automatic by sticking this at the top of .bashrc for the pi user (which is used to run xbmc):

/usr/bin/paplay $HOME/test.wav

It means it also plays every-time I log in with ssh but I’m not expecting to do that much now it’s all working. I’m sure someone will point out the better way to do this that I’m too lazy to look for now everything is working for me.

Random music

I’ve known for a while that I’m not terribly good at varying the music I listen to. Even within my own music collection I tend to have a few favourite albums of the moment, and while those favourites vary I rarely listen to anything else. Except when I have the radio on, and then it is your usual popular stuff, some good, some pap. I still pick up new things from friends occasionally but I never really go out of my way to vary what I’m listening to. I started to change that a little over the last few weeks.

First I tried sticking most of my music collection into a playlist, putting it on shuffle and just listening through it. I didn’t get very far, maybe a couple of hundred tracks in. While it was pretty good to hear lots of stuff that I hadnt listened to in absolutely ages it wasn’t so great that it just kept jumping from genre to genre. I quite like listening to an entire set of songs in one style. Maybe I could figure out how to make it just randomly play entire albums or something.

Second I experimented with iTunes’ new “genius” feature. I actually used this because I needed a good playlist for going to the gym. It takes a certain kind of music and the theory was that by choosing one good track, iTunes would make me a playlist with similar stuff. It was pretty bad. I’m not sure how it matches up tracks but it certainly isn’t by music style. Who would put together Foo Fighters and Newton Faulkner?

Finally I have been trying Pandora. This is a pretty cool service all in all. I have no idea how they fund it just seemingly with ads, but you give it an artist and it just streams you music to listen to that is similar. It does a good job of matching up the music styles. The only place where I felt it fell down was the lack of variety. Maybe I was just unlucky but it seemed to just flip flop between about 4 artists for each channel I created, all of them fairly mainstream artists too, nothing particularly new, I owned most of the songs it was choosing to play for me.

Pandora is certainly pretty good for making me listen to new stuff but I wonder what else there is out there that I might be missing. Unfortunately I’m too lazy to go hunting so I hope someone can tell me…