Updated on | #pulseaudio, #sound, #steam | 0 User comments

Optimize PulseAudio for Steam: Latency, Realtime and Noise / Echo Cancellation

Steam is great under Ubuntu but some games have problems with PulseAudio sound. Sometimes it happends that the sound has short laggings in combination with small cuts: It has the feeling that the realtime behaviour is not giving in steam games. PulseAudio is one of the best sound server: It allows things like transferring the audio to a different machine, changing the sample format or channel count and mixing several sounds into one. I will give you a short guide how you can optimize your PulseAudio settings for steam.

PulseAudio, previously known as Polypaudio, is a network-capable sound server that is used in Ubuntu and other Linux distributions and maintained by freedesktop.org. In principle, it is a proxy for your sound applications that passes sound data between your application and your hardware. PulseAudio replaces the ESD sound server with much better latency, mixing/re-sampling quality and overall architecture. PulseAudio is designed for Linux systems but it also has been ported to and tested on Solaris, FreeBSD, NetBSD, MacOS X, Windows 2000 and Windows XP.

Steam is best known as the platform needed to play Source Engine games (e.g. Half-Life 2, Counter-Strike). Basically, it is a multiplayer and communications platform developed by Valve Corporation and is used to distribute games and related media online, from small independent developers to larger software houses. Steam offers many games from many other developers.

In some games like Counter-Strike: Global Offensive (CSGO) or Civilization V it happens that there is a small but notable delay in the sound. Sometimes these short laggings occurred in combination with small cuts. After some research in several relevant forums, I found the following solution for my sound problems.

Introduction

You can identify your sound cards by using:

  1. lspci -nnk | grep -i audio
00:14.2 Audio device [0403]: Advanced Micro Devices, Inc. [AMD] FCH Azalia Controller [1022:780d] (rev 01)
01:00.1 Audio device [0403]: NVIDIA Corporation GM204 High Definition Audio Controller [10de:0fbb] (rev a1)
    Subsystem: ZOTAC International (MCO) Ltd. GM204 High Definition Audio Controller [19da:2370]

In this example, I have two sound cards available: One is an onboard card from Azalia and the other one is a GM204 chip on my Nvidia GeForce GTX 970. You can find the PCI devices by ID here. For example, the Nvidia GM204 has 10de:0fbb, which you can use to get more information about driver and modules:

  1. lspci -nnk -d 10de:0fbb
01:00.1 Audio device [0403]: NVIDIA Corporation GM204 High Definition Audio Controller [10de:0fbb] (rev a1)
    Subsystem: ZOTAC International (MCO) Ltd. GM204 High Definition Audio Controller [19da:2370]
    Kernel driver in use: snd_hda_intel
    Kernel modules: snd_hda_intel

You can verify if the PulseAudio daemon is running by using:

  1. pulseaudio --check -v
I: [pulseaudio] main.c: Daemon running as PID 1875

List all Sinks

In order to get an overview about all your sinks you can list them by using:

  1. pactl list sinks short
0   alsa_output.pci-0000_01_00.1.hdmi-stereo-extra1 module-alsa-card.c  s16le 2ch 48000Hz   SUSPENDED
1   alsa_output.pci-0000_00_14.2.iec958-stereo  module-alsa-card.c  s16le 2ch 48000Hz   SUSPENDED

List all Sources

In order to get an overview about all your sources you can list them by using:

  1. pactl list sources short
0   alsa_output.pci-0000_01_00.1.hdmi-stereo-extra1.monitor module-alsa-card.c  s16le 2ch 48000Hz   SUSPENDED
1   alsa_output.pci-0000_00_14.2.iec958-stereo.monitor  module-alsa-card.c  s16le 2ch 48000Hz   SUSPENDED
2   alsa_input.pci-0000_00_14.2.analog-stereo   module-alsa-card.c  s16le 2ch 48000Hz   SUSPENDED

Optimize PulseAudio settings for Steam Games

The daemon.conf file defines base settings like the default sample rates used by PulseAudio modules, realtime scheduling and various other settings related to this sound server. You can find this file in /etc/pulse/daemon.conf, where I recommend you the following settings:

  1. sudo nano /etc/pulse/daemon.conf
# Start as daemon 
daemonize = yes
allow-module-loading = yes

# Realtime optimization
high-priority = yes
realtime-scheduling = yes
realtime-priority = 9

# Scales the device-volume with the volume of the "loudest" application
flat-volumes = no

# Script file management
load-default-script-file = yes
default-script-file = /etc/pulse/default.pa

# Sample rate
resample-method = speex-float-9
default-sample-format = s24-32le
default-sample-rate = 192000
alternate-sample-rate = 176000
exit-idle-time = -1

# Optimized fragements for steam
default-fragments = 5
default-fragment-size-msec = 2

# Volume
deferred-volume-safety-margin-usec = 1

Especially the settings default-fragments and default-fragment-size-msec are very important. Audio samples are split into multiple fragments of default-fragment-size-msec each. The larger the buffer is, the less likely audio will skip when the system is overloaded. On the downside this will increase the overall latency. Increase this value if you have issues.

After adding these settings, you can restart PulseAudio with:

  1. pulseaudio -k
  2. pulseaudio --start

Use Nvidia HDMI sound card

You can activate your Nvidia sound card (in my case GM204) by adding the following lines at the END of /etc/pulse/default.pa:

  1. sudo nano /etc/pulse/default.pa
### Activate Nvidia HDMI
load-module module-alsa-sink device=hdmi:NVidia

After adding this command, you can restart PulseAudio with:

  1. pulseaudio -k
  2. pulseaudio --start

Enable Microphone Echo or Noise Cancellation with PulseAudio for Steam

Think on the following situation: You’re using your external microphone in steam to have a chat with other players. My microphone is next to the PC, which makes a lot of noise. So, normally no one is able to understand me in the game because of all these crackles from my PC. Another scenario is when you use your Steam Machine where your microphone is next to your TV and you are listen the sound over your sound system.

In both scenario's you need to filter out the sound by using Echo Cancellation or Noise Cancellation. Mostly these cancellation filters are working like that:

  • Microphone on "outside" listens to sound
  • PulseAudio inverts phase of signal and mixes it with audio source that you can select
  • Resulting sound wave from headphones cancels out outside noise while leaving original sound clean

This technique is used to make headphones as resistant to external noise as possible. The Echo-Cancelation module is included in PulseAudio but not loaded in most linux distributions by default. You can activate it by adding the following lines at the END of /etc/pulse/default.pa:

  1. sudo nano /etc/pulse/default.pa
### Enable Echo/Noise-Cancelation
.ifexists module-echo-cancel.so
load-module module-echo-cancel source_name=echocancelsource aec_method=webrtc
set-default-source "echocancelsource"
set-source-volume echocancelsource 300000
.endif

I read a lot of times that the integrated AudioProcessing module, called WebRTC.org, has been showed very good noise and echo cancellation results. I can recommend you the articles from Arun Raghavan who helps to develop/maintain the PulseAudio sound server. For example, he described the feature "Beamforming" that allows you to use a set of microphones, called microphone array, in a specific direction.

The line set-source-volume echocancelsource 300000 solves the problem that your internal microphone has a low volume on default.

After adding these settings, you can restart PulseAudio with:

  1. pulseaudio -k
  2. pulseaudio --start

Auto Connect to Bluetooth Headphone by using PulseAudio

Whenever a new sink or source appears, this module will switch the default sink/source to be the new sink/source, and will move all currently running streams to the new sink/source. For example, if you plug in a Bluetooth headset, the output will be switched to that. If you unplug it, the output will be set back to the last device.

  1. sudo nano /etc/pulse/default.pa
### Automatically switch to newly-connected devices
.ifexists module-switch-on-connect.so
load-module module-switch-on-connect
.endif

Activate Bluetooth Audio Service

The bluetooth profile Advanced Audio Distribution Profile (A2DP) is designed to use high qualiy stereo sound with your bluetooth speaker. You can active this profile in Ubuntu with:

  1. sudo nano /etc/bluetooth/audio.conf
# Configuration file for the audio service
# This section contains options which are not specific to any
# particular interface
[General]
Enable=Gateway,Source

# Switch to master role for incoming connections (defaults to true)
Master=true

# If we want to disable support for specific services
# Defaults to supporting all implemented services
#Disable=Gateway,Source,Socket
Disable=Socket

# SCO routing. Either PCM or HCI (in which case audio is routed to/from ALSA)
# Defaults to HCI
#SCORouting=HCI

# Automatically connect both A2DP and HFP/HSP profiles for incoming
# connections. Some headsets that support both profiles will only connect the
# other one automatically so the default setting of true is usually a good
# idea.
AutoConnect=true

# Headset interface specific options (i.e. options which affect how the audio
# service interacts with remote headset devices)
[Headset]

# Set to true to support HFP, false means only HSP is supported
# Defaults to true
HFP=true

# Maximum number of connected HSP/HFP devices per adapter. Defaults to 1
MaxConnected=2

# Set to true to enable use of fast connectable mode (faster page scanning)
# for HFP when incoming call starts. Default settings are restored after
# call is answered or rejected. Page scan interval is much shorter and page
# scan type changed to interlaced. Such allows faster connection initiated
# by a headset.
FastConnectable=true

# Just an example of potential config options for the other interfaces
#[A2DP]
#SBCSources=1
#MPEG12Sources=0

After adding these settings, you can restart PulseAudio with:

  1. sudo service bluetooth restart
  2. pulseaudio -k
  3. pulseaudio --start

Steam Controller udev rules

Valve have changed the USB/Bluetooth communication the Steam Controller uses. That means you will need to update your udev rules.

  1. sudo nano /lib/udev/rules.d/99-steam-controller-perms.rules
# This rule is needed for basic functionality of the controller in Steam and keyboard/mouse emulation
SUBSYSTEM=="usb", ATTRS{idVendor}=="28de", MODE="0666"

# This rule is necessary for gamepad emulation; make sure you replace 'pgriffais' with a group that the user that runs Steam belongs to
KERNEL=="uinput", MODE="0660", OPTIONS+="static_node=uinput"

# DualShock 4 wired
SUBSYSTEM=="usb", ATTRS{idVendor}=="054c", ATTRS{idProduct}=="05c4", MODE="0666"
# DualShock 4 wireless adapter
SUBSYSTEM=="usb", ATTRS{idVendor}=="054c", ATTRS{idProduct}=="0ba0", MODE="0666"
# DualShock 4 slim wired
SUBSYSTEM=="usb", ATTRS{idVendor}=="054c", ATTRS{idProduct}=="09cc", MODE="0666"

# Valve HID devices over USB hidraw
KERNEL=="hidraw*", ATTRS{idVendor}=="28de", MODE="0666"

# Valve HID devices over bluetooth hidraw
KERNEL=="hidraw*", KERNELS=="*28DE:*", MODE="0666"

# DualShock 4 over bluetooth hidraw
KERNEL=="hidraw*", KERNELS=="*054C:05C4*", MODE="0666"

# DualShock 4 Slim over bluetooth hidraw
KERNEL=="hidraw*", KERNELS=="*054C:09CC*", MODE="0666"

# HTC Vive HID Sensor naming and permissioning
KERNEL=="hidraw*", SUBSYSTEM=="hidraw", ATTRS{idVendor}=="0bb4", ATTRS{idProduct}=="2c87", MODE="0666"

Well, what do you think?

Comments powered by LudwigDisqus for ModX