Debian GNU/Linux, Audio and Low Latency

What's latency and when latency is a problem in audio domains?

According Wikipedia, "Latency is a time delay between the moment something is initiated, and the moment one of its effects begins. The word derives from the fact that during the period of latency the effects of an action are latent, meaning "potential" or "not yet observed"."

Not all latency is perceptible. When somebody plays guitar, there is latency: there is a time between the moment fingers touch the strings and the moment sound is listened. However, this latency is imperceptible: listener don't distinguish both events (touching and listening).

Perceptible latency is a problem when latency shouldn't be perceptible :) as, e.g., realtime communications (telephone) or audio live recordings. According Dave Phillips,

"In the audio domain, studies have indicated that the ear is sensitive to timing differences at the millisecond level, perhaps even down to a single millisecond. However, latencies under 7 msec are not typically perceptible and are considered acceptable for desktop and semiprofessional applications. Systems achieving average latencies of less than 5 msec should be considered ideal platforms for professional latency-critical applications."

So, in audio domains, latency of less than 5 msec i.e., (1 second divided by 1000) times 5) is ideal; of between 5 and 7 msec is acceptable and above, inadequated.

If audio processing latency is above 7 msec and is necessary realtime responses (i.e., input, comput and output data must happen while one play or sing)) then will have a problem: latency will be perceptible. And the problem increase from discomfort to unacceptable levels, according latency increase.

Linux kernel problem

Official linux kernel offers unacceptable latencies for audio domains.

(...) FIXME explain linux scheduler and preemption

Linux kernel solution

Realtime Preemption: a linux patch for the realtime kernel scheduler. Was written an is maintained by Ingo Molnar and Arjan van de Ven. This patch aims to fix all latency sources that generate higher than ~1 msec latencies. Usefull if you have these requirements (e.g., if you run critical audio applications as live recording).

Debian implementation of solution

You need a Linux Kernel with realtime preemption. So, you need (re)compile kernel. Add following lines into /etc/apt/sources.list:
deb http://pkg-freebob.alioth.debian.org/debian unstable main

And run:

# apt-get update

If strange error messages appear, take a look here. For install kernel-source and realtime preemption patch:

# apt-get install linux-source-KERNEL_VERSION linux-patch-rt
where KERNEL_VERSION looks like 2.6.21.

Untar linux source:

# cd /usr/src
# tar -xjf linux-source-KERNEL_VERSION.tar.bz2
# ln -s /usr/src/linux-source-KERNEL_VERSION linux

Test patch application

# cd linux
# /usr/src/kernel-patches/all/apply/rtpreem

If something looks like...

START applying rtpreem patch (Realtime Preemption)
Testing whether "Realtime Preemption" patch for KERNEL_VERSION applies (dry run):
"Realtime Preemption" patch for KERNEL_VERSION succeeded
Removing empty files:
Done.
END applying rtpreem patch
... returns, so patch application is ok. Don't forget unpatch
# /usr/src/kernel-patches/all/unpatch/rtpreem

Config your kernel and clean tree:

# make menuconfig
# make-kpkg clean

Apply patch

# /usr/src/kernel-patches/all/apply/rtpreem

Compile and make package

# make-kpkg --added-patches rtpreem kernel_image

Answer kernel config questions. If doubt, choice default options (only enter). After, compilation will start. When prompt returns, install package and reboot for test

# dpkg -i ../linux-image-KERNEL_VERSION_10.00_i386.deb
# reboot

Try to run some application with realtime privileges (as root). By example:

# jackd -R -P 70 -d freebob

Normal users running with realtime privileges

With libpam-modules normal user can run with realtime privileges. If you want install it, run, as root:

# apt-get install libpam-modules

Edit /etc/security/limits.conf. Add something looks like:

   @audio		-	nice		-10
   @audio		-	rtprio		99
   @audio		-	memlock		250000

Remember, these settings take effect only after (re)login. All users of audio group (cf. /etc/security/limits.conf) will can run programs with realtime privileges up to 70, e.g.

$ jackd -R -P 70 -d freebob

Userland tunning

rtirq script enable setting realtime priorities . If you want install it, run, as root:

Creative Commons License
This work is licensed under a Creative Commons Attribution-Share Alike 2.5 Brazil License.