1: Yet another Raspberry Pi Airplay hack (w/ Pulseaudio benefits)

One sunny afternoon on a beach in Hawaii, I was mindfully drinking my second glass of lemonade while reflecting on simplifying my life. It was at this moment it occurred to me that I could easily optimize the use of my old gadgets, and – believe it or not – make them IoT compatible. Great! Instead of adding to our waste problem by buying new electronics and discarding the old ones; they could still be “upgraded” and easily used as a shared resource in my home. Finally, no more cable-fights between me and my housemate!

OK, I may have exaggerated a little. I did have this eureka moment but in the toilet. This is a long read and you need some Linux experience.

Here’s what I had lying around in my home office/hobby room:

  1. Raspberry Pi (Pi 2 Model B v1.1) — barely used but already configured for network (SSH) access
  2. USB Mixer (Behringer Q802) — used as a USB sound card for the Pi because Pi’s inbuilt audio hardware is horrible.
  3. My DELL laptop — running Opensuse Linux
  4. USB Bluetooth dongle — not decided on its use yet
  5. HP Deskjet 1500  — basic printer and scanner with great ink management.
  6. Amplifier from the 90’s — dated but still very good sound

I decided it was time to put my Pi into better use since it could easily be a server with low power consumption.

Routing Sound

As an acoustic musician, my first concern was to push music from our devices over the network. Airplay was the best option for me cause of the iOS devices at home (and great tutorials here and here). In addition, I was able to route audio from my linux box via Pulseaudio (accidental but cool discovery.). Enough of the bants. Lets get to work!

  1. Get the latest packages for your Pi (I’m running Debian Stretch); and Shairport-sync. This what does the magic

    sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get install shairport-sync

  2. Make the USB card the default audio device. If you’re OK with using the crappy headphone jack of the Pi, skip this step. You will need to edit/create a config file for ALSA

    sudo nano /etc/modprobe.d/alsa-base.conf

    Next, add these lines to prioritize the default device you wanna use (mine, USB card)

    # sets the index value of the cards but doesn’t reorder.
    options snd_usb_audio index=0
    options snd_bcm2835 index=1

    # does the reordering.
    options snd slots=snd_usb_audio,snd_bcm2835

    You can close the file.

  3. Start the shairport-sync service.

    sudo service shairport-sync start

    It is most likely that it has already started. You can test by searching for your Pi on an Apple device on the same network. Android users can use the AllConnect app to push content to Airplay devices.

    img-20181001-wa0003.jpg

    Screengrab from iOS.

  4. The default name used was my Pi’s hostname – HavenPi. You can change this by either changing the hostname using raspi-config OR modify the following line in shairport-sync.conf

    sudo nano /etc/shairport-sync.conf

    Uncomment the 4th line by removing the forward slashes and changing %H to any name of choice e.g. “BlazingPi”

    // General Settings
    general =
    {
    // name = “%H”; // This means “Hostname”

    So far so good, all iOS devices and select Android devices can send high quality sound to the amplifier via the Pi. Linux is a bit shaky and I’ve not tested on Windows. Forget about bluetooth, audio quality sucks!

  5. What about Linux? It turns out that the new default audio service – Pulseaudio – supports network streaming to another box. Shairport-sync can be the user that initializes Pulseaudio. The earlier steps were for the former audio service – ALSA.
    Go ahead to install pulseaudio

    sudo apt-get install pulseaudio pulseaudio-module-zeroconf avahi-daemon

  6. You can configure Pulseaudio as a system or user service. I prefer the user option so that shairport-sync can initiate pulseaudio

    sudo nano /etc/default/pulseaudio

    Remove this line or comment it by adding # in front

    #PULSEAUDIO_SYSTEM_START to 1

  7. Modify the default config file for Pulseaudio on the Pi to accept audio from the network

    sudo nano /etc/pulse/default.pa

    Add the following to the end of the file to allow connections from ONLY devices in your network

    #Network Sink
    load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1;192.168.1.0/24 auth-anonymous=1
    load-module module-zeroconf-publish

  8. Now make Pulseaudio the default backend for shairport-sync.

    sudo nano /etc/shairport-sync.conf

    Modify this line (remember to remove the beginning forward slashes)

    //output_backend = “pulse”; // Run “shairport-sync -h” to get a list of all backend

  9.  Reboot your Pi and we are done!

Final thoughts

Congrats if you’ve gotten this far. The rewards are way better than the config stress. Music from a proper amplifier is bliss! Now you can send audio via Airplay and Pulseaudio (on PCs that have it). Here’s what I see under Sound options. If you can’t see anything, install paprefs and check the options under “Network Access”

Screenshot from 2018-10-01 19-19-55

To route ANY sound from my PC on the Pi, I select my USB card and voila! Airplay did not work off my Linux box but Pulseaudio is more flexible. Here are other advantages I discovered:

  • I can stream via Airplay and Pulseaudio together.
  • I can configure the Pi to be my default sound sink.
  • Compared to ALSA, you can choose what engine will be used based on application. For example, my music streaming service can be through the Pi and Skype through my PC speakers.
  • There is no stuttering of any kind (most of my devices are wired so there’s enough bandwidth for wireless devices)

Alright. I hope this works for you. Post your questions below and I will be glad to help out. My next post will be about using this same Pi as a Printer/Scanner server.

Enjoy!


Some bonus tips

  1. If you want to easily select a default audio device from the volume control of the status menu, install this Gnome Extension here:
    screenshot-from-2018-10-28-06-08-51-e1540703462530.png

    Easy access to change default audio output device. My USB Card is selected.

    You will enjoy this most if you media control on your keyboard.

  2. Three weeks after setting this system up, I decided to ignore this golden rule – “if it’s not broke, don’t fix it” – and upgraded my Pi. It stopped working after the reboot. On checking the service’s status, I saw there was an issue connecting to a running instance of pulseaudio. Easiest way for me to solve this issue was to start shairport-sync when the pi auto-logins

    sudo service shairport-sync status
    [pulseaudio] pid.c: Failed to open PID file ‘/tmp/pulse-NBDOvmnpGLKL/pid‘: No such file or directory

    I simply :

      1. stopped shairport-sync service from starting on boot

        sudo systemctl disable shairport-sync.service

      2. edited raspi-config to allow auto-logon for my default user. Sorry, you’ve gotta figure that out
      3. added this line to the .bashrc file located in /home/pi. It will ensure that only one instance of shairport-sync will start on boot (i.e. user logon)

        [ ! -f “/run/shairport-sync/shairport-sync.pid” ] && sudo service shairport-sync start

I hope this helps saves time for you guys. Been jamming music all night long, this amplifier rocks!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s