Skip to content

Instantly share code, notes, and snippets.

@Gadgetoid
Last active February 21, 2018 15:50
Show Gist options
  • Save Gadgetoid/3301cec3e47495e75b31d3120d8f17d9 to your computer and use it in GitHub Desktop.
Save Gadgetoid/3301cec3e47495e75b31d3120d8f17d9 to your computer and use it in GitHub Desktop.
Pulse Audio Pop-reduction Config

Pulse Audio Configuration for Raspberry Pi Audio Systems

This Pulse Audio configuration is designed to sit between your audio source and sink, to keep the audio device open and mitigate speaker pops.

This is only for I2S DAC devices on the Raspberry Pi that pop when they are started/stopped.

Installing

First, install pulseaudio if it's not already installed:

sudo apt update
sudo apt install pulseaudio

/etc/pulse/client.conf

Edit /etc/pulse/client.conf and add or change these lines (anything preceeded with ";" is commented out so be sure to remove it):

You can edit a file with sudo nano /etc/pulse/client.conf and find lines by pressing Ctrl+W, typing your search term and hitting enter.

autospawn = no
default-server = unix:/tmp/pulseaudio.socket

/etc/pulse/default.pa

Edit /etc/pulse/default.pa and find the line load-module module-suspend-on-idle, add timeout=604800 onto the end.

It should look like this:

### Automatically suspend sinks/sources that become idle for too long
load-module module-suspend-on-idle timeout=604800

Next, find the line load-module module-native-protocol-tcp and add auth-ip-acl=127.0.0.1 to the end.

It should look like this:

### Allow local connections over tcp, for mopidy etc
load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1

Then, find the line load-module module-native-protocol-unix and add auth-anonymous=1 socket=/tmp/pulseaudio.socket.

It should look like this:

### Allow unauthenticated connections from other users so aplay, etc work via pulse
load-module module-native-protocol-unix auth-anonymous=1 socket=/tmp/pulseaudio.socket
If you want to use a VU Meter

If you're using Pi VU Meter with pHAT BEAT, Speaker pHAT or other compatible board then find the line that starts ### Automatically load driver modules and add the following before it:

load-module module-alsa-sink device=pivumeter

This will make sure Pulse outputs through the VU Meter plugin, rather than right to the audio device.

/etc/systemd/system/pulseaudio.service

Finally the pulseaudio.service file:

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

[Install]
WantedBy=default.target

[Service]
Restart=always
Type=simple
PrivateTmp=false
ExecStart=/usr/bin/pulseaudio --realtime --disallow-exit --no-cpu-limit --log-target=syslog
ExecStop=/usr/bin/pulseaudio --kill

Should go into /etc/systemd/system/pulseaudio.service and be enabled with:

sudo systemctl daemon-reload
sudo systemctl enable pulseaudio
sudo systemctl start pulseaudio

Verifying

Check that pulse is running by entering:

sudo systemctl status pulseaudio

This will also show you any relevant log output.

Tips & Tricks

Fix Choppy Audio When Streaming

One possible fix for this problem- particularly with Shairport Sync -is to disable WiFi power management.

You can test this by running sudo iw wlan0 set power_save off and, if it works, make it permenant by editing /etc/network/interfaces and:

allow-hotplug wlan0
iface wlan0 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
    wireless-power off # <---- Adding this line

Troubleshooting

First, double check that you followed all the config steps above correctly.

A useful troubleshooting tool is aplay, you can usee aplay -L to show a list of device names, including pulseaudio if it's running.

No sound when running pulseaudio

Try restarting your audio applications, and ensure they're outputting audio via pulse. This pulse configuration will tie up the raw sound device (by necessity to keep the clock running) and all sound must go via the daemon.

Check aplay --list-devices, if you have more than one device listed here your audio might be playing to the other one, or pulse might have selected the wrong device as its sink.

# /etc/pulse/client.conf
autospawn = no
default-server = unix:/tmp/pulseaudio.socket
# /etc/pulse/default.pa
# module-suspend-on-idle is the module that suspends ALSA, stopping the clock, when
# Pulse determines that there's no incoming audio stream.
# Raising this timeout keeps the i2s clock running perpetually, and eliminates pops
### Automatically suspend sinks/sources that become idle for too long
load-module module-suspend-on-idle timeout=604800
### Allow local connections over tcp, for mopidy etc
load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1
### Allow unauthenticated connections from other users so aplay, etc work via pulse
load-module module-native-protocol-unix auth-anonymous=1 socket=/tmp/pulseaudio.socket
[audio]
output = pulsesink server=127.0.0.1
[Unit]
Description=PulseAudio Daemon
After=sound.target
Requires=sound.target
[Install]
WantedBy=default.target
[Service]
Restart=always
Type=simple
PrivateTmp=false
ExecStart=/usr/bin/pulseaudio --realtime --disallow-exit --no-cpu-limit --log-target=syslog
ExecStop=/usr/bin/pulseaudio --kill
@pubdc
Copy link

pubdc commented Sep 2, 2017

Thanks for this, the popping noise is something I do want avoid.
After the entire install my messages and syslog show following errors : "[pulseaudio] main.c: This program is not intended to be run as root"
Also, when running that last command to test a functioning pulseaudio my system responded :
"pi@raspberrypi:~ $ sudo systemctl status pulseaudio
● pulseaudio.service - PulseAudio Daemon
Loaded: loaded (/etc/systemd/system/pulseaudio.service; enabled)
Active: failed (Result: start-limit) since Sat 2017-09-02 14:47:31 UTC; 8s ago
Process: 12065 ExecStop=/usr/bin/pulseaudio --kill (code=exited, status=1/FAILURE)
Process: 12062 ExecStart=/usr/bin/pulseaudio --realtime --disallow-exit --no-cpu-limit --log-target=syslog (code=exited, status=1/FAILURE)
Main PID: 12062 (code=exited, status=1/FAILURE)

Sep 02 14:47:31 raspberrypi systemd[1]: Unit pulseaudio.service entered failed state.
Sep 02 14:47:31 raspberrypi systemd[1]: pulseaudio.service holdoff time over, scheduling restart.
Sep 02 14:47:31 raspberrypi systemd[1]: Stopping PulseAudio Daemon...
Sep 02 14:47:31 raspberrypi systemd[1]: Starting PulseAudio Daemon...
Sep 02 14:47:31 raspberrypi systemd[1]: pulseaudio.service start request repeated too quickly, refusing to start.
Sep 02 14:47:31 raspberrypi systemd[1]: Failed to start PulseAudio Daemon.
Sep 02 14:47:31 raspberrypi systemd[1]: Unit pulseaudio.service entered failed state.
"
Have you any hints on what could be causing those, or if they are problematic ?
Many thanks, Peter

@Shane-Lester
Copy link

I get the same when I verify pulseaudio

@adacaccia
Copy link

adacaccia commented Nov 11, 2017

Hi, this tutorial really ROCKS!
I had the same problem as the two persons above while checking that pulseaudio was actually working.
So I decided to dig, since the LOUD clicks were very annoying, plus the audio tends to be low quality because of pops and other noises "mixed" to the playing sound.
My setup: Pirate Radio + Raspbian Stretch Lite (I don't really like the idea of sticking to an old release just to fix things... That approach only works on closed-source platorms, as on M$ so-called "operating systems" in the well known (and best avoided) Windows family).
2 terminals open via SSH to the "pir8radio" host (the RPi 0 W actually), in one sudo tail -f /var/log/syslog, while sudo systemctl restart pulseaudio on the other.
Offending root cause error was:
Nov 11 18:56:20 pir8radio pulseaudio[2009]: [pulseaudio] module.c: Failed to load module "module-alsa-sink" (argument: "device=pivumeter"): initialization failed.
In short, this splendid tutorial still needs some tweaking, but info are easily found on the official PA documentation:
https://www.freedesktop.org/wiki/Software/PulseAudio/Documentation/User/PerfectSetup/
Raspbian as a Linux distributions belongs to the "Group 1" according to own PA classification.
Then the section "Third Party Applications" follows, but the very first chapter is the only that matters "ALSA Applications".

  • edit /etc/asound.conf and add:
    pcm.pulse { type pulse }
    ctl.pulse { type pulse }

  • Then comment out any other pcm.!default and ctl.!default in there, and add the new default in the alternative form:
    pcm.default pulse
    ctl.default pulse

  • The "Unlikely Side Note" doesn't apply here, as this tutorial is already editing the PA config (default.pa) in the intended way.

Now that those LOUD CLICKS are away (only one survives, at boot time, but it's not a priority at the moment), I'll focus on the overall sound quality, maybe I'll also upgrade the speaker to 10 W to check both acoustic distortion and the chance that my current speaker got ruined exactly by those LOUD CLICKS...

@adacaccia
Copy link

adacaccia commented Nov 12, 2017

The results of my extensive testing today:

  • On Raspbian Stretch Lite, Pulseudio removes the LOUD clicks, but deteriorates heavily the overall audio quality (it remembers me of those ancient days with poor quality magnetic tape cassettes...)
  • But on the same OS, just the full image instead of the Lite, of course booted into CLI mode, the audio quality with Pulseaudio is almost good (little clicks here and there...).
    So I'll stick with the full image... Usually I don't like those "Lite" stuff anyway, but this time I've tried it because of the wide consensus on them... so I'll be the one out of the bucket!

@Shane-Lester
Copy link

What is the mopidy.conf file that is shown? It isn’t mentioned on the instructions. Do we create this? If so I’m which directory?

@Shane-Lester
Copy link

When I try this on stretch on a clean install (apart from the pi phat beat installer) it doesn’t run asa a service. aplay-L doesn’t list it and stays shows that it isn’t running

@DynamiteAndric
Copy link

I have tried the tutorial on currently latest raspbian stretch lite and Pirate Radio set with installed VLCD internet radio and VU meter. Except initial click there is no additional clicking, but sound is not good. Also when I change station in VLCD by pressing next button volume is resset to high level, it does not remember the previous setting. It's happening when change to any station.

@iurab
Copy link

iurab commented Nov 29, 2017

@Shane-Lester Mopidy is a music server, edit the mopidy.conf file only if you use the app.
I followed all the steps suggested, but I still encountered a problem. After playing a sound, in 10-15 seconds it was followed by the loud clicks.
The solution was to add my user in the pulseaudio.service so the service will not be started by root

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

[Install]
WantedBy=default.target

[Service]
User=pi
Restart=always
Type=simple
PrivateTmp=false
ExecStart=/usr/bin/pulseaudio --realtime --disallow-exit --no-cpu-limit --log-target=syslog
ExecStop=/usr/bin/pulseaudio --kill

Replace pi with your current username.

@cache-return
Copy link

+1 @iurab

This config works with phat beat, but I need to add "User=pi" to the systemd unit, or I get a pop about 30 seconds after audio playback ends.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment