Improving Linux sound latency

Updated: 2005-08-21
Created: 2004-07-28

This document is an incomplete draft.

Various scheduling and sound low latency tweaks might be very useful to avoid sound breaking up, some pointers:

Using a real time priority
It may be very helpful to run the sound processing applications like ohphone, openmcu or GnomeMeeting at a real time priority. To avoid running them as root, start them as a normal user, then change their priority class to FIFO as root using chrt, for example like this:
for p in $(pidof ohphone); do chrt -v -r -p 5 $p; done
or when starting the application, for example with
chrt -v -r -p 5 xmms
Kernel latency improving patch collections for kernel 2.4.x and kernel 2.6.x
These two sites contain a set of low latency and preemption patches, ready made and packaged for common kernels.
My impression is that preemption patches should be only applied by the brave, as they can cause problems with several drivers.
Also, there is some evidence that in recent 2.6 series kernels, like 2.6.12, latency is pretty low already and the preemption logic does not help.
BIOS: ensure sound cards on interrups 9, 10 or 11
These interrupt levels have the highest priority in case of nearly simultaneous interrup events.
X11: enable PCI_burst, disable PCI_retry
This helps ensure the graphics card does not use too many PCI bus cycles, stalling the sound card.
System: tweaking the PCI latency timers
PCI devices share the bus, and the “latency timer” for each device determines for how many consecutive cycles the device can use the bus. Devices with a high value for the latency timer can and do cause gaps in sound. Often enough it is video cards that have high default latency timer values, to look better in benchmarks. It is possible to reset the default value of the latency timer with the command:
setpci -v -s '*:*' latency_timer=20
setpci -v -s '0:0' latency_timer=0
which sets it to 32 cycles and resets the latency timer to 0 for the host bridge, usually with id 00:00.0 (to prevent bad performance for DMA). It is possible and perhaps useful to give a larger value to a sound device, for example with one of:
setpci -v -d '1102:0002' latency_timer=80
setpci -v -s '00:09.0' latency_timer=80
where 1102:0002 is the manufacturer and model id of your sound card, or 00:09.0 is the PCI bus id of the same card. It will be different on your system, so use lspci and/or lspci -n to discover them.
Usually it is better to use the manufacturer and model id instead of the bus id, as moving the card from one slot to another is more common than replacing the a sound card with another in the same slot.
Note that sometimes setting too low a value for the latency of the video card might make video card operations less visually smooth; setting the latency timer of the host bridge to greater than zero can significantly impact the transfer rate of DMA.
There is more information on this issue in this general introduction to PCI latency, or this PCI latency HOWTO, and this Linux specific PCI latency article.
Audio Quality HOWTO
In parts it is a bit dated, but the sections on dropouts and latency contain a number of very interesting points.
Low latency with ALSA mini HOWTO
From the page:
This document is specifically aimed at those interested in using 2.4.x with Andrew Morton's low-latency patch for audio purposes.
It's not really hard when you know what to do, but there's a lot of stuff to upgrade before you can compile and install the kernel if you are moving from linux 2.2.x. So if you are, don't forget to read the section on updating your system files.
We suggest scanning through this whole document to get an overview before you start mucking with the kernel. Everything described here requires kernel configuration, and it's easier to do it all in one go rather than rebuilding the kernel again for each feature you want. You should also read through the necessary upgrades to make sure you have all your software in order before you start configuring. Once you have things working you may want to run some benchmarks on your system. Visit the LAD site for the latest info, apps and test results.
Low latency for audio work on linux 2.6.x
Information on latency issues and advice specific to the 2.6.x kernel series, by Florian Schmidt.
Daniel Robbins talk about IRQs and PCI latency
Informative, even if slightly old (2001). But I rather disagree with his conclusion to set very long PCI latencies to optimize thruput. I think shortish burst while slightly less efficient are more in tune with the relative slowness of the PCI bus.
hdrbench, latency-graph Linux latency resources
Latency checking tools and info.
Ardour hardware advice
Interesting advice. Except for the blanket warning about all VIA chipsets; many older motherboards with VIA chipsets do have limitations and performance issues with DMA and PCI transactions, but not all, and in particular recent (2004 and later) chipsets seem to be rather OK.
Ardour mailing list thread on latency
This thread contains several interesting contributions, especially as to IRQ priorities (highest to lowest: 0, 1, 8, 9, 10, 11, 12, 13, 14, 15, 3, 4, 5, 6, 7). I used to think that these do not matter that much, but then I always had my sound card on IRQs 9, 10 or 11, which are high priority ones. I also agree with the warning about APIC interrupt routing.
posting on low latency, advice on low latency
Benno Sennoner's advice on low latency audio, a bit old but still useful.
From the page:
Planet CCRMA (CCRMA is pronounced ``karma'') at Home is a collection of rpms (RPM stands for RedHat Package Manager) that you can add to a computer running RedHat 7.3, 8.0, 9 or Fedora Core 1 to transform it into an audio workstation with a low-latency kernel, current ALSA audio drivers and a nice set of music, midi, audio and video applications."
The CCRMA project's advice on hardware tuning and latency
Wise words from Planet CCRMA on realtime, and latency.