Monthly Archives: April 2013

AirPlay server on the Raspberry Pi

As said in my post’s title, I received my Raspberry Pi at home something like a month ago, but I was unable to play around it because of high school etc… Now that I’m officially in holidays for three whole weeks, I will have some free time to spend on hacking with my new toy!

A wild raspberry as an AirPlay receiver

A wild raspberry as an AirPlay receiver

As you may already know if you follow me on Twitter, the main reason I ordered this Raspberry Pi for was to use it as an AirPlay server, so I can stream some sound over the air, directly through my speakers from iTunes or an iOS device, like an iPad. So, as soon as I bough an SD card (4gb – Class 4, enough storage space and relatively fast, basically what is recommended to install Raspbian) for my Pi, I installed Raspbian (which is a custom Debian version for the Raspberry Pi) and I began setting up Shairport, the open-source Airplay server that I used for this process.

So let’s get into it, most of the commands I’m gonna be using here requires root access, so log you as a superuser for all the tutorial, it’ll be much easier. Just be careful, don’t screw up and follow my steps when you’ll be root on your device.

First of all, you will need to install a few packets which will enable you to compile Shairport’s sources, what we’re gonna be doing in the next step.

root@Hera:/home/drav# apt-get install git libao-dev libssl-dev libcrypt-openssl-rsa-perl libio-socket-inet6-perl libwww-perl avahi-utils

Next, we will fetch Shairport’s source code to compile it with all the tools that we have downloaded.

root@Hera:/home/drav# git clone https://github.com/albertz/shairport.git shairport
root@Hera:/home/drav# cd shairport
root@Hera:/home/drav/shairport# make

So, at this point you may get a few compilation errors. If everything went as expected, you can skip this step and go to the next one. Anyway, on my device I got these errors, for a pretty simple reason… If you see that sort of ugly things instead of a glorious “done” message at the end of the process:

cc -O2 -Wall -c alac.c -o alac.o
make: cc: Command not found
make: *** [alac.o] Error 127

… this is simply because the development package is not installed on your machine. To fix this problem, the solution is pretty simple:

root@hera:~/shairport# apt-get install build-essential

And that’s it. Relauch your make command and normally everything went back to normal! Let’s get into the next step, we’re going to select an audio output and start our Shairport server to see if everything is all right.

Talking about the audio output, if you wanna use your Pi’s one, then you will have to use that command:

root@Hera:/home/drav/shairport# amixer cset numid=3 1

Congratulations, in theory your sound should come out of your Raspberry’s jack plug, let’s check if everything works well by launching the server.

root@Hera:/home/drav/shairport# ./shairport.pl -a HeraSound

So, just a little explanation on how this command works… ./shairport.pl is obvioulsy to start the server using the executable file that we just compiled, no need to be genius to understand this (well, in fact I’m actually writing some useless stuff to raise the word counter on my WordPress admin interface ^-^). Anyway, something maybe more helpful, -a HeraSound is to specify the name you wanna give to your AirPlay server. You can set it to “LivingRoom”, “bedroom”, whatever you want. Personally, mine is called HeraSound simply because my server is called Hera Linux. It’s up to you, use your cat’s name if you really don’t know which name to use.

Okay, let’s go back to the tutorial. In principle, and I say “in principle” only, your Shairport server should work fine. But it’s possible that you experience sound issues, for example, my Raspberry Pi’s sound didn’t come out of its jack output. So, once again, there is a completely rational explanation for this problem (which, from what I read and what I heard is quite common), that is that Alsa (I mean something which related to Alsa) isn’t installed. So yeah, that’s normal, it won’t work if you’re in the same case as me. :D
To resume, If you get that kind of error messages:

root@Hera:/home/drav/shairport# ./shairport.pl -a HeraSound
Established under name ’24803C3438E3@HeraSound 1004 on Hera’
ALSA lib confmisc.c:768:(parse_card) cannot find card ’0′
ALSA lib conf.c:4170:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4170:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
ALSA lib confmisc.c:1251:(snd_func_refer) error evaluating name
ALSA lib conf.c:4170:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:4649:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2190:(snd_pcm_open_noupdate) Unknown PCM default
FATAL: Could not open ao device

… then the audio driver isn’t correctly loaded on your device! To solve that issue, nothing easier… We just have to install the alsa-utils package and to run the right modprobe, and the job’s done!

root@Hera:/home/drav/shairport# apt-get install alsa-utils
root@Hera:/home/drav/shairport# modprobe snd_bcm2835
root@Hera:/home/drav/shairport# ./shairport.pl -a HeraSound

Voila! So, yeah, I don’t really know if that normal but on my machine, I’m obligated to run my modprobe after every reboot so my sound works properly. Because that command requires root access, that a little bit complicated and we cannot manage to run it on every boot with the init.d directory, we’ll see this at the end of this article. Anyway, if even after rebooting your device your sound works well, you won’t need to follow this extra step. That’s just a part for luckless guys like me.

So, normally your AirPlay server works pretty well (if it doesn’t, then I can no longer help you, Google is your friend). The only (big) issue is that we need to start the server manualy. So basically what we’re going to do next is moving Shairport’s script to our init.d directory, so that it starts automatically when your Raspberry Pi boots up.

root@hera:/home/drav/shairport# make install
root@hera:/home/drav/shairport# cp shairport.init.sample /etc/init.d/shairport
root@hera:/home/drav/shairport# cd /etc/init.d
root@hera:/etc/init.d# chmod a+x shairport
root@hera:/etc/init.d# update-rc.d shairport defaults

… but it’s not sufficient! At this point your Shairport daemon starts on startup, but without any name specified. So what we’re gonna do is editing the perl script with nano shairport and finding the DAEMON_ARGS line to make it look like this:

DAEMON_ARGS=”-w $PIDFILE -a HeraSound”

After, same as before, put anything you want instead of HeraSound. Personally, I don’t care.

Okay so this is the part for luckless guys like me. If you’re not concerned and if your sound is working on your raspberry without having to do any modprobe after the boot, that’s great and you can skip this section.
To get the sound working on your Pi, you have to do a modprobe snd_bcm2835, am I right ? But this command can be ran only as a superuser, because of this we can’t run it on every boot using the init.d way.
In fact, it’s not really difficult to fix this problem. You just have to do a nano /etc/rc.local and to type modprobe snd_bcm2835 just before exit 0. Voila! The job’s done and the modprobe is gonna be launched on every boot to load the audio driver!

Congratulations, you just installed a 100% working AirPlay server on your Raspberry Pi! Use it as you want, plug it behind a stereo or build a speaker with it! :)

And, as a bonus, a little home-made video showing you a little bit the result of the process (french).