Skip to content

Instantly share code, notes, and snippets.

@fstanis
Created February 28, 2018 00:30
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fstanis/50261c573204d21506b86476ef46d0f8 to your computer and use it in GitHub Desktop.
Save fstanis/50261c573204d21506b86476ef46d0f8 to your computer and use it in GitHub Desktop.
Setting up Speaker pHAT or pHAT DAC with pulseaudio

Setting up Speaker pHAT or pHAT DAC with pulseaudio

Overview

The purpose of this document if to provide a simple method of getting pulseaudio to work on Raspberry Pi with either Speaker pHAT or pHAT DAC.

Please see Software installer for Speaker pHAT or Setting up pHAT DAC for the more official guide / installer. The idea behind this guide is to provide you with a more manual alternative that lets you have a minimal setup.

Installation

  1. Enable I2C, I2S and HiFiBerry DAC by appending these lines to /boot/config.txt:
dtoverlay=i2s-mmap
dtoverlay=hifiberry-dac
dtparam=i2c_arm=on
  1. Install ALSA and pulseaudio packages:
sudo apt-get install alsa-utils pulseaudio
  1. Reboot the device.

  2. Create /etc/systemd/system/pulseaudio.service with the following contents:

[Unit]
Description=PulseAudio Daemon
After=sound.target
Requires=sound.target

[Install]
WantedBy=multi-user.target

[Service]
Type=notify
ExecStart=/usr/bin/pulseaudio --realtime --disallow-exit --no-cpu-limit --daemonize=no --system --log-target=journal
  1. Enable and start the newly-created service:
sudo systemctl --system enable pulseaudio.service
sudo systemctl --system start pulseaudio.service
  1. Add the current user to the pulse-access group:
sudo adduser $USER pulse-access
  1. Test and ensure everything works at this stage:
speaker-test
  1. (Optional) Increase the realtime priority which usually helps with stuttering. Append this line to /etc/pulse/daemon.conf:
realtime-priority = 9
  1. (Optional) Increase time before the sink is suspended by pulseaudio by editing /etc/pulse/system.pa and replacing
load-module module-suspend-on-idle

with

load-module module-suspend-on-idle timeout=604800

Set up network audio server

Once sound works on your Pi, you can play files remotely from your PC thanks to pulseaudio.

Server setup

Do these steps on your server (Raspberry Pi).

  1. Install the zeroconf module and avahi:
sudo apt-get install pulseaudio-module-zeroconf avahi-daemon
  1. Update /etc/systemd/system/pulseaudio.service to require and wait for avahi:
[Unit]
Description=PulseAudio Daemon
After=sound.target network.target avahi-daemon.service
Requires=sound.target
Wants=avahi-daemon.service

[Install]
WantedBy=multi-user.target

[Service]
Type=notify
ExecStart=/usr/bin/pulseaudio --realtime --disallow-exit --no-cpu-limit --daemonize=no --system --log-target=journal
  1. Reboot the device.

  2. Append these lines to /etc/pulse/system.pa to enable zeroconf for your server:

load-module module-native-protocol-tcp auth-ip-acl=192.168.0.0/24 auth-anonymous=1
load-module module-zeroconf-publish

Note: This gives access to anyone in the IP range 192.168.0.1 - 192.168.0.254 to broadcast sound to your Pi. If you'd like to limit to a single IP, for example 10.0.0.1, replace module-native-protocol-tcp auth-ip-acl=192.168.0.0/24 with module-native-protocol-tcp auth-ip-acl=10.0.0.1.

  1. Restart pulse:
sudo systemctl --system restart pulseaudio.service

Client setup

Do these steps on the machine where you'd like to play sound from.

  1. Install the zeroconf module and avahi:
sudo apt-get install pulseaudio-module-zeroconf avahi-daemon
  1. Append this line to /etc/pulse/default.pa to enable zeroconf discovery:
load-module module-zeroconf-discover
  1. Reboot the machine.

  2. Check to make sure the new sink is detected:

pacmd list-sinks | grep -B 1 'name: <tunnel'

# Example output:
#
#     index: 8
#         name: <tunnel.raspberry.local.alsa_output.platform-soc_sound.analog-stereo>
  1. Set the default sink to the one printed previously:
pacmd set-default-sink 8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment