Linux Sound Programming

Please see disclaimer on the main page.

This is a bit rough - the better source is probably the official programmer's guide, but then it's not in one file.

Sound Devices

These do not exist on every system, and may not "work" on every system (trying to open them will produce errors etc), if sound is not supported or has not been configured correctly.

Generally, we have device names of the form <device-description>< id-number>. For instance, if there are two sound cards installed there is usually a /dev/dsp0 as well as a /dev/dsp1. There should be a link, /dev/<device-description>, which points to the default device.


DSP access

DSP access is obtained through three devices: /dev/dspx, /dev/dspWx and /dev/audiox (replace 'x' with a decimal number). The three are functionally identical (they refer to the same physical device) apart from the fact that they have different default formats (which are only set if supported by the device). Only one can be open (and only once) at any one time:

Data is 'reasonably buffered'; reading or writing too quickly will cause blocking (the read/write routine will not return until the operation has been performed). Reading or writing too slowly will cause gaps in the noise. At the moment, (25/4/99) the sound drivers do not support non-blocking IO. Not all sound cards support full duplex operation (they may only be opened for read OR write access, not both at the same time).

The following ioctl calls are supported, as well as the mixer ioctl calls (you do not need to open the mixer seperately). The mixer calls will affect the first mixer only. The values shown here can be found in <linux/soundcard.h>. The third ioctl function argument is always a pointer to an integer which contains the value, or which will recieve the value required.


Mixer programming

Mixer programming is performed throught the /dev/mixer (/dev/mixer0 etc) device. This device does not support read/write operations, only ioctl calls. As with the DSP, the ioctl third parameter is a pointer to an integer.

If no mixer is supported, the device entry /dev/mixer will still exist, however any ioctl calls will return ENXIO.

The following mixer channels (devices) may exist:

Each device has a volume setting, which can be read/written using the following macros. The setting ranges from 0 to 100 and is scaled by the driver. In the case of stereo devices, the least significant byte is the volume for the left channel, the next byte is the volume for the right channel:

const char *SOUND_DEVICE_LABELS[] is an array of descriptions of each mixer channel (device). const char *SOUND_DEVICE_NAMES[] is an array of single word names suitable for command line switches etc. The number of devices can be found from SOUND_MIXER_NRDEVICES.