Friday, September 4, 2009

Build A Real Time Audio Studio

Although it might not seem like it, properly dealing with audio requires an intricately tuned PC. The main issue is audio latency, which refers to both the time it takes for a sound to enter your machine and the time it takes for your machine to produce a sound. In everyday use, we don’t really notice this latency. When you click on a button that makes a beep, you don’t care that it might take several hundred milliseconds for the sound to reach your ears. But in the delicate world of audio engineering, several hundreds of milliseconds is far too long, and even a delay of just tens of milliseconds is undesirable.

Most professional systems will try to reduce audio latency to less than 10 milliseconds – faster than the average seek time on most hard drives. Some systems will get it down as low as 2 or 3ms, enabling them to process vocals and instruments in real-time, play software synthesizers just as they would the physical type, and mix and master recordings with the same near-zero latency as an analogue tape machine.

On Windows systems, this low-latency is produced by the ASIO driver framework that’s used by many pro-level audio cards to communicate with apps like Steinberg’s Cubase and Ableton’s Live. On OS X, however, a low latency audio framework is built into the operating system, and it’s a similar case with Linux.

ALSA, or the Advanced Linux Sound Architecture, sits within the Linux kernel itself as a series of hardware drivers. Audio data is passed from ALSA to the user-level applications that manipulate the audio. But despite this low level integration and a built-in potential for good low latency performance, most default Linux installations make poor audio production computers. That’s because there’s still too much going on in the background, and too many throwbacks from the time when Linux was designed primarily for server installation.

Luckily, Linux is a highly configurable operating system, and this means that you can create the perfect audio platform just by making a few tweaks. We’ve chosen the latest version of Ubuntu to be our guinea pig.

Get a real-time kernel

The key to getting audio latency as low as possible is to remove all obstacles to a fast response time, and the place to start is with the kernel. The Linux kernel is made up of a series of modules that huddle together to form the monolithic chunk of functionality. Some of these modules are designed to provide super-low latency operation, but they can have unwanted side-effects for general computer use. As a result, these modules are rarely enabled by default.

These real-time kernel enhancements were introduced to the mainstream kernel several years ago, and together they can have a massive effect on how your system handles audio processing. Without the real-time enhancements, the kernel can respond to an interrupt within about 600 milliseconds. With the enhancements enabled, that time is reduced to around 20 milliseconds. Those numbers might seem minute, but they can have a dramatic effect on your audio latency when your system’s kernel is dealing with hundreds of interrupts a second.






We wouldn’t advise installing the real-time kernel enhancements onto a machine you need to rely on, though. They’re disabled simply because they’re unstable. Happily, however, both sets of the kernel can coexist within the same Linux installation: you simply choose which to use from the Grub boot menu.

In Ubuntu, the real-time kernel can be installed through the package manager just like any other addition. Search for ‘linux-rt’ and install both the main package and the ‘linux-headers-rt’ package that appear. Both of these packages, and the resulting dependencies, are from the Multiverse repository. This means that they’re unofficial and won’t be covered by any security patches, but they are actively maintained by a third party.

Install audio software

After the kernel has been installed, you will need to restart your machine. When you get to the brief Ubuntu boot menu, press [Escape] to get to the Options screen. From here, you can choose between your standard kernel (most probably labelled ‘-generic’) and the real-time version we’ve just installed (labelled ‘-rt’). Choose this and press [Enter] to boot into the real-time environment. This is the point where the new kernel may cause some problems. As it’s responsible for all of the hardware connected to your PC, everything will need to be re-detected. On our test machine, for example, the display’s native resolution wasn’t correctly detected and we needed to fix this manually. If you use proprietary drivers, such as those for Nvidia hardware, you will have to make sure that these have been reinstalled. Ubuntu’s package manager should have grabbed the restricted modules to do this automatically, but if not, you’ll need to install the driver manually.

Now that you’ve got a working low-latency desktop, it’s time to install the audio production environment. Ubuntu’s package manager makes this easy. Just search for ‘ubuntustudio-audio’ in Synaptic, and install the resulting package. This includes the cream of Linux audio software and gives you everything that you need to get started with your low-latency audio environment. After those packages have installed, there are a couple of housekeeping tasks to perform. First, load up the User Manager from the System menu, and add any user that you want to have access to the low-latency audio features to the ‘audio’ group. This should also include your user account. Secondly, add the following two lines to your ‘/etc/security/limits.conf’ file using administrator privileges:

@audio - rtprio 99
@audio - nice -10

When you’ve finished, restart your machine, and don’t forget to choose the real-time kernel from the Grub boot menu. It’s now time to play with some audio software.

Configure Jack

The tool at the heart of the Linux audio system is called Jack – the Jack Audio Connection Kit. Jack sits on top of the ALSA layer and dynamically pipes audio to and from a series of inputs and outputs. It’s a very clever solution to an age-old problem: routing audio input to your soundcard from one application to another before it makes its way to an audio output like your headphones or speakers. Jack does this without adding any latency. It’s been such a success on the Linux platform that you can now find it doing the same job on OS X.

Before we can dive into things, though, we need to get the service started. Jack runs as a background daemon, and you can use a GUI to control the connections to and from your hardware and software. The best software we’ve found for controlling Jack is Qjackctl, which is installed as part of the Ubuntu Studio package. Look for ‘Jack Control’ in the Multimedia menu. When the app appears, click on the ‘Setup’ button. Getting Jack’s set-up parameters right is often a matter of trial and error. Your settings will be different to ours, as they’re dependent on your specific audio hardware.

Make sure that the sample rate is compatible with your hardware. Most motherboard-based devices, and those from Creative and Asus, use 48,000Hz. Ensure ‘Real-time’ is enabled, as this was the reason for installing the enhanced kernel. Use the dropdown menu to the right of the ‘Interface’ field to select your audio device: this is normally going to be ‘hw:0’, ALSA-speak for the first device on your system. Finally, set the ‘Frames/Period’ size to 512. This is the size of the buffer used by Jack, and it has a direct effect on the audio latency of your system. The 512 setting works out at around 20ms from input to output, but you should be able to lower this when you’re sure that your system is working.

Save the configuration as a preset at the top of the window, then close the set-up window. Finally, when you get back to the main Jack Control window, click on the ‘Start’ button to start the background daemon running. If everything has been configured correctly, you should see a yellow ‘Started’ in the faux LCD panel, and you will now be able to start playing around with Jack. If you don’t see this word in the panel, click on the ‘Messages’ button to see a report of any launch errors.

Connect the apps

You’ve done the hard part, so now it’s time to put it to some use. Click on the ‘Connect’ button in Jack Control and you’ll see two panels in the Audio tab. The panel on the left lists the software and hardware sound generators, while the panel on the right lists the software and hardware outputs. At this point there is no other Jack software running, so under the system clients you should see only the hardware connections for your audio device.

To rectify this, look for an application called ‘Zynaddsubfx’ in your Multimedia menu. This is a rather excellent software synthesizer cunningly disguised by a silly name and dodgy UI. If you’ve still got Jack’s Connect window open when Zynaddsubfx launches, you’ll see an additional node in the output panel appear. This will connect its output to your soundcard’s direct output on the right-hand side. Click on the ‘VK’ button in the Synth window, and a virtual keyboard will appear. Press a few keys on this and you’ll hear a sound, hopefully with very little latency. Congratulations: you’re now using a real-time kernel and the Jack Audio Connection Kit to generate sound.

Get recording

To build a production environment, you’ll need to run a piece of audio workstation software that can manage all of these components. There are two widely used applications on Linux. Rosegarden is a KDE application that can record both audio and MIDI, and it can also use the new DSSI open-source synthesiser standard. However, our favourite is Ardour, a comprehensive recording environment that has more in common with Pro-Tools than Cubase. Ardour works exclusively with Jack to create a studio environment in software. If you launch the app from the Multimedia menu and create a new project, you’ll see the Jack connection window spring into life. Every track, effects send and audio bus in Ardour uses Jack. This is initially overwhelming, but it does mean that you’re free to use Ardour as you would a multitrack recorder in a studio, routing and re-routing audio between your various devices.

To record output from Zynaddsubfx, right-click in the lower part of Ardour’s main window and add a stereo track. Open the Jack connection window, select the two outputs from the synth and the two system output ports that they’re connected to, and click on ‘Disconnect’. Now connect each synth output to ‘Audio/In1’ and ‘Audio/In2’ under the Ardour client in the right panel. These are the two channels of the stereo track that we just created. Press the ‘Record enable’ button in the Ardour track followed by the big record and play buttons on the transport control, and play something on the synth. Ardour will now record the digital audio output from the synth, and you should be able to see some of this setup’s amazing potential.



Your Comments

0 Responses to "Build A Real Time Audio Studio"