marvin.im

Raspberry Pi 3 - Bluetooth Audio and Airplay Reciever

Hey guys,

this article is about setting up a raspberry pi 3 to receive Audio via Bluetooth and Airplay and play it via connected USB-Soundcard.

In my setup the USB-Soundcard is a Behringer USB multi channel Mixer connected to my yamaha speakers.

The part with the Airplay is done easily - even with multi room audio sync.

Here I recommend installing shairport-sync (https://github.com/mikebrady/shairport-sync)

On your way you’ll stumble across the following:

Problems

  • raspberry pi 3 internal bluetooth chip is not useable when it comes to audio recieving
    • many problems, jittery audio etc.
  • which soundsystem should I use? Alsa, Pulseaudio etc.
  • does it run inside docker?
    • unfortunately I wasn’t able to set it up inside a docker container
  • pulseaudio bluetooth module requires an X11 Server

etc. etc.

Solutions

  • get a external usb bluetooth stick (I bought a 3$ one on ebay)
  • use pulseaudio (Amazon Echo wasn’t able to play sound via bluez-alsa, but Android was)

  • pulseaudio and shairport-sync will run as user pi and will be started via an entry in /etc/rc.local

Step by Step

General

install the follwoing

sudo apt-get update

# stuff for compiling and build process
sudo apt-get install -y build-essential git

# stuff for audio and bluetooth audio
sudo apt-get install -y alsa-base alsa-utils pulseaudio pulseaudio-module-bluetooth bluez bluez-tools xvfb xinit dbus dbus-x11

xvfb xinit dbus and dbus-x11 are needed for pulseaudio-module-bluetooth to work

add user pi to the needed groups

sudo usermod -aG pulse,pulse-access,lp pi

lp is bluetooth group

add a new file with the following content and make it executable

/xinit-script.sh

#!/bin/bash
service dbus start
while true; do su -c "DISPLAY=:1 pulseaudio -v" -l pi; sleep 5; done

Airplay

clone repo

git clone https://github.com/mikebrady/shairport-sync
cd shairport-sync

install dependencies

sudo apt-get update
sudo apt-get install -y xmltoman autoconf automake libtool libdaemon-dev libasound2-dev libpopt-dev libconfig-dev avahi-daemon libavahi-client-dev libssl-dev libsoxr-dev libpulse-dev
autoreconf -i -f
./configure --sysconfdir=/etc --with-alsa --with-pa --with-avahi --with-ssl=openssl --with-metadata --with-soxr #--with-systemd
make
sudo make install

add configuration

/etc/shairport-sync.conf

general = {
  name = "My Airplay Speakers";
  output_backend = "pa";
};

make startable withour systemd (systemd doesn’t work in this particular pulseaudio setup)

add the following to /etc/rc.local

# shairport audio
nohup bash -c 'while true; do su -c "shairport-sync" -l pi; sleep 5; done' &

Bluetooth Sink

Configure Bluetooth USB Stick

to avoid conflicts with the internal raspberry-pi bluetooth module I recommend to give the internal bluetooth module the hostname of the raspberry pi.

the usb bluetooth stick gets the name you want your bluetooth devices connect to.

to add configure each bluetooth devices you’ll need their mac address.

bluetoothctl will show your devices (internal and usb device) pulling the usb stick out and plugging it in again can help you which one is the internal one and which one is the external one.

each devices has it’s own configuration beneath /var/lib/bluetooth/<mac-addr>/settings

vi '/var/lib/bluetooth/00:AD:AD:11:11:11/settings'

my external config

[General]
Discoverable=true
Alias=Yamaha Speakers

my internal config

[General]
Discoverable=false
Name=raspberrypi

to set a global default name you can configure the following

vi /etc/machine-info

PRETTY_HOSTNAME=device-name

Add Bluetooth device

you can use bluetoothctl to configure new bluetooth devices.

I recommend to use it as user pi, since this is the user all our audio daemons will run.

bluetoothctl

discoverable on
scan on
pair 34:D2:XX:XX:XX:XX
trust 34:D2:XX:XX:XX:XX

and you’re done pairing with 34:D2:XX:XX:XX:XX

PulseAudio/Bluetooth Setup

add the following to the /etc/rc.local

# bluetooth pulseaudio
nohup bash -c "xinit /xinit-script.sh -- /usr/bin/Xvfb :1 -screen 0 1x1x8" &