Buffering and Latencies

This section explains how uninterrupted audio playback can be ensured. This problem is by no means limited to Linux, but is inherent in all multitasking operating systems. In a multitasking operating system, several processes usually run concurrently. As the processor can only handle one process at a time, each process is assigned a certain amount of time by the operating system's scheduler. The switching action between processes normally happens so quickly that the user does not notice it.

However, during audio playback, even brief interruptions are noticeable in the form of clicks. Therefore, audio programs use a buffer for the playback, enabling the audio data in the buffer to be emitted continuously by the sound card even when the audio program is interrupted by the scheduler. Accordingly, the playback is click-free if the buffer is large enough to bridge the longest possible interruption.

However, the buffer size also determines the reaction time (latency) of the program. Therefore, the buffer size is kept as small as possible especially for interactive applications, such as real-time synthesizers and DJ mixer consoles. Basically, the length of the interruptions depends on the system load and the priority of the process. Consequently, the size of the buffer required for click-free playback can be reduced by increasing the priority of the audio program or by switching to a real-time scheduler. For this reason, many audio programs attempt to switch their processes to a real-time scheduler, but switching a process to another scheduler is only possible with root privileges. The application setpriority in the rtstools package is required for this task.

For example, proceed as follows to run the application timidity with the FIFO scheduler:

  1. Start timidity

  2. Start a root console

  3. Use the following command to find the process ID of timidity:

    pidof timidity
    

  4. Change the scheduler with the command

    setpriority <processID> fifo 10
    

You can use the following command in a root shell to speed up this procedure:

for i in `pidof timidity`; do setpriority $i fifo 10; done

Running a program in root mode is always risky, as the program is permitted to do anything. If the computer is connected to the Internet, the security risk would be unacceptable. Security bugs in the program could be exploited for the purpose of gaining access to the system.

Warning

The commands described in the following paragraphs should never be executed on machines that can be accessed from the Internet or if a system crash or data loss would have serious consequences.

The sudo mechanism should be used for running a program in root mode. This mechanism is demonstrated by means of the timidity++ application. To enable all users on your system to execute timidity++ with root privileges, modify the file /etc/sudoers. See sudo and sudoers for the procedure. If you are not familiar with vi, select a different editor, such as joe, by exporting the desired editor, for example, with the command export EDITOR=joe. Then execute visudo as root and append the following line at the end of the file /etc/sudoers:

ALL ALL=(ALL) /usr/bin/timidity

Now all system users are permitted to run timidity++ in root mode with the command sudo timidity. The password of the respective user is requested if more than five minutes passed since the last sudo command.