Next


Chapter 14. Programming Sound Devices

Contents:
The Linux Sound Driver
Basic Device Programming
Sound Programming Basics
Programming /dev/sndstat
Programming /dev/dsp
Programming /dev/audio
Programming /dev/mixer
Programming /dev/sequencer
Advanced Sound Programming
The PC Speaker Sound Driver

In this chapter we look at sound programming under Linux. The examples use the C programming language, as that is by far the most commonly used language under Linux.

C is also the obvious choice as a matter of convenience. Linux, like UNIX systems, is built around a kernel and libraries written in C. Using the operating system facilities from other programming languages usually requires additional effort. The examples will also compile using the GNU C++ compiler, g++.

If you have experience with C, but are not familiar with UNIX (or POSIX) compatible programming environments, don't despair. The next section should cover enough of the basics to allow you to understand the examples. But you should also consider supplementing the material with other references that cover UNIX programming.

If you are already familiar with programming devices under Linux or other UNIX-like systems, you may choose to skim through or skip the next section entirely.

Keep in mind that not all multimedia applications will need to program at this level. In Chapter 17, Using Toolkits for Multimedia Programming, I will explore some toolkits that can avoid, or at least minimize, having to program at the device level. Often, though, toolkits do not provide the functionality or efficiency that is needed, and C programming is required.

The Linux Sound Driver

Under Linux, like any UNIX-compatible system, user programs rarely access the hardware devices directly. The kernel has drivers for all hardware devices. At the kernel driver level, controlling hardware involves bit manipulation of hardware registers in a highly hardware-dependent and often time-critical manner. The kernel shields this from user programs. To an application, the devices appear as (somewhat special) files in the filesystem. This unified high-level view of devices is mostly hardware-independent. For example, usually you don't need to know what kind of sound card is installed in order to write applications to control it.

The Linux sound driver has an interesting history behind it. In 1992 Hannu Savolainen wrote a Linux kernel driver for the original 8-bit SoundBlaster card. Later, partly at the request of users, he added support for more sound cards. The sound driver has been included as a standard part of the kernel since the early days of Linux (that is, prior to the version 1.0 kernel).

The same driver was adapted to work with several other Intel-based UNIX and compatible operating systems. As the drivers could be used independently of UNIX, they were given a distinct name--Voxware. Unfortunately the same name was later registered by Voxware Incorporated and could no longer be used. For a period of time the package was known as TASD, the Temporarily Anonymous Sound Driver.

For some time Hannu had been looking for assistance from sound card vendors to help sponsor the development of the driver. Some vendors did provide cards, programming information, and funding, but it was still mostly a part-time effort for Hannu. In March 1996 it was announced that the sound driver was "going commercial." The company 4Front Technologies is now offering a commercial sound card driver for a number of UNIX and compatible operating systems for PCs and workstations, based on the former Voxware, called the UNIX Sound System (USS). It is sold and supported commercially, and like most commercial software products, is distributed in binary form.

At the same time the free version continues to be offered for Linux with similar conditions as before (e.g., freely redistributable source code). This version is called USS Lite. While it will not be supported by 4Front Technologies, it will continue to be maintained by Hannu Savolainen. The commercial and free versions will be compatible with each other, although the commercial USS release may include additional features (for example, support for sound cards from vendors who don't release programming information). If USS is widely accepted, it may become the standard sound programming API for UNIX systems. As I write this book, however, USS is in its first beta release and is available only for the Linux platform.

All of the programming information and examples in this chapter are based on the sound driver included in the Linux kernel version 2.0. They should work with later versions, but new features are being continually added to the sound driver. To be sure, you should check the latest driver README files for changes, new features, and possible incompatibilities.

Alternative device drivers are available as kernel patches. These drivers operate independently of the Linux sound driver, typically providing some functionality that the "official" kernel sound driver does not support. Using these sound drivers is not recommended, because they make your applications less portable. If you are contemplating writing drivers for new sound cards, you are urged to add the functionality to the kernel sound drivers, and ask the author to merge them into the standard driver source code (and ultimately, the standard Linux kernel sources).

While the examples in this chapter are specifically for Linux, the Linux sound drivers are supported on a number of other Intel UNIX platforms, making your code potentially portable to other operating systems. The examples are also aimed at the Intel version of Linux. At the time of writing, this was the only architecture that supported the Linux sound drivers.


Next  Basic Device Programming


O'Reilly Home