Using the Raspberry Pi as an AirPlay server

Lead Image © Ultra Pop, 123RF.com

AirPlay

AirPlay lets you send music from iTunes to AirPlay-capable receivers. Now you can use Shairport to retrofit a Rasp Pi and avoid the expense of purchasing an AirPlay receiver.

AirPlay lets Apple customers create a wireless connection from an iPhone, iPad, or Mac computer to a stereo system or loud speakers. Both the Airport Express basic station [1] and an Apple TV [2] connected to a home theater system support AirPlay, and many audio/video receivers come with an integrated AirPlay receiver.

However, if you have an iOS device, you can turn to the Raspberry Pi as a way of integrating your stereo system with AirPlay. Shairport [3] is an AirPlay server that runs on Raspbian and other Linux systems. Shairport consumes few resources and is therefore ideally suited for the Rasp Pi – you just need to connect it to your stereo (see "Which Rasp Pi Should You Use for HiFi Audio?").

Which Rasp Pi Should You Use for HiFi Audio?

The first generation Raspberry Pi models were not exactly known for great sound quality. The voltage converter that was used for these boards led to disruptions in the audio signal and caused strange sounds and clicks. The disruption problem was solved starting with the Raspberry Pi B+. Therefore, this is the model you should first consider, or you can go for an upgraded older Rasp Pi outfitted with a USB sound card or a HiFiBerry module [5].

Installing Shairport

The Shairport server is not part of the packet sources for Debian and Raspbian, so you have to build the software by hand from the source code. Another option, if the Raspbian version is current enough for your needs, is to use a distro with Shairport preinstalled [4].

You will need to update the system and install the necessary conditions and libraries from the Raspbian packet sources to compile Shairport (Listing 1). Then, load the Shairport source code from GitHub and build the software (Listing 2).

Listing 1

Getting Updates

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install libssl-dev libavahi-client-dev libasound2-dev avahi-daemon

Listing 2

Installing Shairport

$ cd /tmp
$ git clone https://github.com/abrasive/shairport.git
$ cd shairport
$ ./configure
$ make
$ sudo make install

Once you have decided on a Rasp Pi model, you need to know that the installation routine copies only the program file to /usr/local/bin/. The configuration file is missing. Likewise, there is no init script to launch Shairport automatically when the system starts. Thus, you need to copy these files by hand into the system for the final steps of installation (Listing 3).

Listing 3

Getting Shairport Scripts

$ sudo cp scripts/debian/init.d/shairport /etc/init.d/
$ sudo cp scripts/debian/default/shairport /etc/default/

After you finish the installation process, the configuration file of the Shairport server will need some attention. Using an editor with root rights, open the shairport file,

sudo nano /etc/default/shairport

and edit the named variables found in Listing 4. These include USER, GROUP, AP_NAME, and MDNS. You can select whatever you want for the AirPlay name; then, you should write the modifications on the memory card using Ctrl+O; Ctrl+X closes the Nano editor.

Listing 4

Editing /etc/default/shairport

# User and group under which shairport should be run
# user should have permission to output sound
# Check the audio output documentation for details.
USER=pi
GROUP=nogroup
...
# Set the AirPlay advertised name.
# Defaults to computer's hostname
AP_NAME=Raspberry Pi
...
# Force the mDNS backend
# By default, shairport will try all backends until one works.
# Check 'shairport -h' for details
MDNS=avahi

The best way to test the installation is to start Shairport (Listing 5, line 1) and look at an iPhone or Mac computer running iTunes software. The dialog under the AirPlay icon should now specify the Shairport server that is active on the Raspberry Pi as a possible receiver for streaming iTunes music (Figure 1).

Listing 5

Testing Shairport

01 $ shairport -a 'Shairport Test'
02 Starting Shairport 1.1.1-22-gd679d19
03 Listening for connections.
04 $ sudo update-rc.d shairport defaults
05 $ sudo service shairport start
Figure 1: Shairport lets you redirect music playback from iTunes to the Raspberry Pi.

If Shairport doesn't produce any sound in spite of being configured correctly, you should try increasing playback volume or manually designating the audio output used by Raspbian. To carry out a quick test, select a sine curve with

speaker-test -t sine -f 500

via the loudspeakers. If you still don't hear anything, use alsamixer and the Up arrow to increase playback volume (Figure 2). Stop before reaching an area colored in red.

Figure 2: Use Alsamixer to turn up the playback volume of the Rasp Pi.

If you still don't hear anything and the Raspberry Pi is connected via the earphone jack to the stereo system, you can try forcing the output over the analog 3.5mm jack socket per instructions found in the "Selecting Audio Output" box. If your Raspberry Pi is connected to an AV receiver via HDMI, then you should explicitly specify the digital path with Force HDMI in the raspi-config audio dialog. One of these techniques should give you correctly configured audio output.

Selecting Audio Output

The Rasp Pi puts out sound either over a 3.5mm jack or via HDMI. Raspbian should automatically be able to recognize whether a cable is connected to the analog audio output with a corresponding audio redirect. If this does not work in your setup, you should call the Raspbian setup tool with

sudo raspi-config

and manually set audio output under Advanced Options | Audio (Figure 3).

Figure 3: The raspi-config tool lets you specify which audio output the Raspberry Pi should use.

When you're satisfied with the way things are working, you can close the Shairport server you have started for your test using Ctrl+C. Then, use the last two commands from Listing 5 to add the program to those services that automatically start during the boot process. To avoid a restart, you should start the service manually for the last time.

XBMC as an Alternative to Shairport

If you are already using a Raspberry Pi that runs Raspbmc or OpenELEC for an XBMC media center, you won't have to bother installing an AirPlay server. Your software is versatile enough to include the relevant functions; you only need to activate the AirPlay service in the software settings.

Activation is accomplished by going to System | Services | AirPlay and finding the Allow XBMC to receive AirPlay content option (Figure 4). Once this is activated, you will see that the AirPlay streams are delivered to a media center in iTunes that is called either XBMC (OpenELEC) or Kodi (OpenELEC), depending on what has been installed.

Figure 4: The XBMC media center comes with an integrated AirPlay server.

Because a media center is large and resource hungry, you should only set your sights on XBMC when you are sure you really want to use a Rasp Pi as a media center. If you are only looking to use the Rasp Pi as an AirPlay receiver for music, Shairport is the better option.

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content