Q

How to get low latency audio

Learn how to compile a real time Kernel for Linux and get smooth audio.

Summary

If you want to use Linux for audio purposes you need a low latency OS. To achieve that you will need to run Linux with a real time kernel (RTK). The process of compiling a RTK might be confusing so I will explain how I normally do it. Before I was able to compile a proper RTK I did a lot of research and tests. At this point I have a set of commands that I just copy, paste and execute each time that I want to try a new RTK on my system. I will give you all commands and a brief explanation of each one so you can easily execute them in you Linux terminal.

Definition: In general words a real time (RT) system is a system where an input signal is processed to generate an output signal before a specified time (deadline) is reached. The deadline is also known as latency. If the latency is small enough there is no perceptible delay between the input and the output. If for some reason the deadline is not achieved at some point, the output signal will not be available on time and therefore a noise will be produced. This is also known as xrun.

A tweaked Linux kernel is required to have imperceptible latency and avoid xruns. There is a lot of online information, however I had many problems to compile a functional kernel when I started. That's why I want to give you my experience, emphasising what it was hard for me. I have done this project in Ubuntu 12.04 and Linux Mint 15.

I identified 6 general steps:

  • Update the system and install dependencies.
  • Download a vanilla kernel and real time patch.
  • Uncompress kernel and apply the patch.
  • Configure the patched kernel
  • Compile and install the RT kernel
  • Configure Linux

1. Update the system and install dependencies

Before anything it's recommended to update the system. Open a terminal and execute:

sudo apt-get update

If there is something to update just follow the steps and wait. After you update your system you will need to install a few packets/programs that will be used during this process:

sudo apt-get install kernel-package libncurses5-dev fakeroot wget bzip2 build-essential zlib1g-dev cpufrequtils p7zip p7zip-full

Don't close the terminal, you will use it during the whole process.

2. Download a vanilla kernel and real time patch

First of all we have to define what a "vanilla kernel" is. A "vanilla kernel" is a kernel source code that follows the mainline development. This means that it's a standard kernel source without any tweak or modification for any specific use. A vanilla kernel source code and a RT patch are required to compile a RTK.

There are many versions of vanilla kernels and you will probably want to use the latest one that has a RT patch available. You can find the 3.x vanilla kernels here and the RT patches here. If you want to check the stable releases go to the home page.

The RT patch and the vanilla kernel MUST have the same version. I usually open 2 windows in Chrome and I look for both files at the same time to find the proper ones. The average size of a kernel is about 100MB and the patch about 150KB. There are 2 types of patches: "patch-x.x.x.x-rtx.patch.xz" and "patches-x.x.x.x-rtx.patch.xz" I don't know what's the difference but the one who works for me is "patch-x.x.x.x-rtx.patch.xz".

After you find a kernel and a RT patch with the same version you can download them.

Currently I'm using the Kernel 3.6.11 and RT patch 3.6.11.2-rt34 and they work quite well. I have tried to compile and install other kernels with no luck: 3.8.13-rt10, 3.10.17-rt12, 3.6.11.9-rt42.

NOTE1: All examples and commands that I'll give will consider this version of Kernel and RT patch. Therefore if you want to try another kernel you must change the version number in the commands. Also I will consider that both files are going to be downloaded in the "Downloads" folder.

3. Uncompress kernel and apply the patch

Go to the console and create a directory in your "home" folder to put the source code. Then go to that folder:

mkdir ~/KERNEL
cd ~/KERNEL

Uncompress the kernel and change its name to easily identify it:

tar xvf ~/Downloads/linux-3.6.11.tar.xz
mv linux-3.6.11 linux-3.6.11.2-rt34

Copy the RT patch to the to this folder:

cp ~/Downloads/patch-3.6.11.2-rt34.patch.xz ./

Go to the "linux" link and patch the kernel:

cd linux
xzcat ../patch-3.6.11.2-rt34.patch.xz | patch -p1

At this point you have patched the kernel source code.

4. Configure the kernel

Now you need to configure the kernel. It's a good idea to use your current kernel configuration as base to configure your new RT kernel. The following command reads the existing ".config" file and prompts the user for options in the current kernel source that are not found in that file:

make oldconfig

You should basically choose "no" or "default" to all options except for "Preemption Mode" where you must choose "5″ (real time).

You must also change the "Timer Frequency" of the kernel. Execute the following command:

make menuconfig

This command opens a configuration menu where you can change any options of the kernel. To change the timer frequency you must go to "Processor type and features → Timer frequency" and select "1000″. After that, exit and save the changes.

5. Compile and install the new kernel

Finally you can compile your new RT kernel:

make-kpkg clean
time fakeroot make-kpkg –initrd kernel_image kernel_headers

The compilation process will take about 90 minutes depending of your computers speed. After the compilation is done you can install the RTK:

cd ..
sudo dpkg -i linux-headers-*.deb linux-image-*.deb

After the installation is done reboot your computer. When the system starts you will see the GRUB menu where you can choose between different kernels and other OS if they are installed. Choose the new RTK and press enter.

NOTE2: If you don't see the GRUB menu probably it is not installed on your system or it was not updated during the kernels installation. If this happens you will need to check online how to fix it.

6. Configure Linux

We have to modify the "limits.conf" file to create a group that has real time privileges and doesn't have memory limitations. Open that file with "nano" editor:

sudo nano /etc/security/limits.conf

At the end of this file add the following lines:

# rtprio
@audio – rtprio 99
@audio – nice -19
@audio – memlock unlimited

Finally you have to add your user name to the "audio" group that we just created:

sudo usermod -a -G audio [user]

Where [user] is your Linux user name.

Other important step is to tweak Linux to avoid CPU scaling because when the CPU is put in a low-power consumption it creates long-latency events that will produce xruns. One option is to change the system's governor to "performance". The processor will work at maximum speed all the time and real time processes will not be interrupted. The computer will get hotter than usual so try to keep it ventilated. You can change the "governor" using "cpufrequtils" installed on the first step.

sudo cpufreq-set -r -g performance

Please note that this command will not change the governor permanently. You have to execute it each time that you restart Linux.

NOTE3: Just a few weeks ago I found this document. It explains in detail how to compile a RTK for Linux.