Thursday, September 10, 2015

Using AirPlay with Raspberry Pi

AirPlay is an Apple protocol which allows wireless streaming of audio, video, photo and so on. Even if it's a proprietary protocol, it can also be used with Android devices or personal computers by installing simple apps.

Of course you can also install AirPlay on a Raspberry Pi.

When I built my Sphaera, I also added an external audio card, an audio amplifier and a speaker. Initially I used this audio system just for some notification, but going from that to a wireless speaker it really easy.

Firstly I thought to make a bluetooth speaker, but I change idea almost immediately. Bluetooth means adding and configuring a new usb dongle and I preferred to avoid this. So I searched how to use it as a wi-fi receiver and AirPlay was the best solution.

Lets' see how to proceed...

First of all remember to update and upgrade your Raspberry Pi. Then we need to install some prerequisites for Shairport, the software used as (audio) AirPlay receiver.

Type the following:

sudo apt-get install git libao-dev libssl-dev libcrypt-openssl-rsa-perl libio-socket-inet6-perl libwww-perl avahi-utils libmodule-build-perl

and wait for the installation of all the needed libraries.

Now we need to install the SDP protocol manager for Perl, so type the following commands:

git clone https://github.com/njh/perl-net-sdp.git
cd perl-net-sdp
perl Build.PL
sudo ./Build
sudo ./Build test
sudo ./Build install
cd

Now that all the prerequisites are ready, it' time to install Shairport:

git clone https://github.com/hendrikw82/shairport.git
cd shairport
sudo make
sudo make install

And to launch it just type

perl shairport.pl

Now you can connect any AirPlay enabled device to your RPi for audio streaming. If you do not like the default AirPlay name you can launch it typing

perl shairport.pl -a Sphaera

This will set the name to Sphaera. Of course you can use whatever name you like.
Now it would be much better to use Shairport as a daemon and launch it at boot. Fortunately Shairport comes with a ready-made daemonizer, so just type:

sudo cp shairport.init.sample /etc/init.d/shairport
cd /etc/init.d
sudo chmod a+x shairport
sudo update-rc.d shairport defaults

You can use it as is, but if you wish to use a specific AirPlay name, you need to edit the shairport file:

sudo nano /etc/init.d/shairport

Then find the following line:

DAEMON_ARGS="-w $PIDFILE"

and change it adding the -a parameter:

DAEMON_ARGS="-w $PIDFILE -a Sphaera"

That's it. After saving the file you can reboot and your RPi should be listed in the available AirPlay receivers.

Audio quality heavily depends on the whole RPi audio configuration. Using the internal audio card is not advisable as the quality is really poor. An external card (usb or a shield) will be much better. Also the amplifier and the speaker will affect the quality, so if you wish to make a real hi-fi system, remember to choose good components.

My Sphaera doesn't play bad, even if the components are not the best you can find and the case has not been developed thinking about audio quality.
Surely, for what I'm using it for it's more than enough.

Next step will be to analyze audio from the streaming (or maybe from a microphone) and use the RGB leds to make some audio driven light effects...

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.