Linux System Administrator's Survival Guide lsg10.htm

Previous Page TOC Next Page



Chapter 10


Sound Cards


UNIX wasn't designed with sound cards in mind, and only a few applications make use of one (DOOM being the most popular). Linux also lets you load a special driver to make use of the PC speaker for more than the occasional beep, useful when you don't have a proper sound card. However, sound cards are a common accessory in PCs, especially those designed for multimedia applications. Making your sound card work under Linux is not difficult, as long as you know the manufacturer and model of your sound card. Software device drivers for the most common models are included with most Linux distribution software packages, and some other drivers are available from FTP and BBS sites. This chapter looks at how you can install the device drivers for your sound card and make sure they work properly under Linux.

Linux doesn't support many DOS sound file formats (such as .WAV and .MID), but relies instead on sound files and commands that are taken from the Sun workstation product line. Sun audio files are the general format for Linux, usually with the .au filetype appended. You can play other sound files, although some may need conversion to Sun format first. Sun workstation device drivers are used as the model for the sound card drivers in Linux, although the drivers are designed to work with standard PC sound cards.

A Linux system can support two sound cards, although most installations will involve only one card. The two cards must be different and cannot have any of the same configuration information (both must have different I/O addresses, IRQs, and DMAs). If you have a separate dedicated card for a MIDI interface, you may want to use two cards. For example, you may have a PC equipped with a Sound Blaster card for the usual game effects and a Roland MIDI card for professional sounding audio. Linux can support both types of devices at the same time. You can't use two cards that both support Sound Blaster, even as emulations, as the drivers will conflict unless you disable one card.



The drivers and software associated with the device drivers for sound cards changes frequently, so check for the latest versions and examine any documentation that accompanies them. It may supersede the information in this chapter. Many versions of Linux now include automated installation scripts for sound cards or support the mkdev sound command. Check for the installation scripts before installing the sound card manually.


Checking for Sound Card Support


The list of sound cards supported by Linux includes most of the popular models, and many others emulate one of these supported models and so can be run in emulation mode without a problem. The most popular supported models of sound cards are the following:

A complete list of supported sound boards is usually supplied with the Linux distribution software. Because the location of the files describing the sound support varies considerably, search for any files to do with sound using the find command. You can do this search easily with a CD-ROM distribution package. Mount the CD-ROM (see Chapter 9, "CD-ROM Drives," for more information) and issue a find command with the string sound as a starting point:


find / -name sound* -print

This search should at last point you to the directories that contain information about the sound boards, drivers, and C source code for the drivers. The sound drivers for the CD-ROM version of Linux included with this book, for example, are stored in /usr/src/linux-1.2.8/drivers/sound. Figure 10.1 shows the output from a find command run against this version of Linux. In this example, the find command reports matches in the DOS filesystem, too, because that is mounted as /dos. You may want to unmount your CD-ROM drive before issuing this command to save time.If you can't search your distribution media, or don't have a complete Linux distribution handy, check the README files that were supplied with your software for potential locations of drivers.

Figure 10.1.
You can search for the sound card drivers by issuing a find command.


Configuring Your Sound Card


Once you have determined that your model of sound card is supported, you can begin the process of making it available to Linux. Install the sound card, if necessary. This procedure usually involves nothing more complex than placing the card in an available slot on the motherboard. Because Linux uses its own device drivers, installing the DOS-based software has no effect on the card under Linux.

When you install the sound card, note the basic configuration information, such as the card's DMA, IRQ, and I/O address. Some cards have more configuration information, including 8-bit and 16-bit DMAs, MIDI addresses, and so on. Most sound cards use the default values without a problem, so you can check the card's documentation for these values. If you had to change some of the default settings to make the card work under DOS, have the changed values readily available for the Linux device configuration.

Later versions of Linux include sound card drivers for the most common cards as part of the basic driver package. In some cases, the setup or installation script includes a sound card configuration section. This script installs the proper drivers and relinks the kernel automatically. If your setup or install script handles the sound card setup for you, there is little else you need to do except reboot the system and test the sound card.

The sound card drivers are stored in different locations, depending on the Linux distribution. A good general location to look for drivers is the directory /usr/src/linux/drivers/sound, although the names tend to change somewhat. A quick method of finding the drivers is to search for the sound directory using the command


find / -name sound -print

If your Linux software is supplied on CD-ROM, you will want to search the CD-ROM as well as the hard disk filesystem because few installation routines copy all the drivers to the hard drive. The Slackware distribution CD-ROM, for example, keeps the sound drivers on the CD-ROM until they are explicitly installed. The Slackware release keeps the drivers in the directory /cdrom/live/usr/src/linux-1.2.8/drivers/sound (the version number changes with each release of Slackware Linux).

Understanding Sound Card Device Files


Linux uses a number of device files in association with a sound card. Each device driver is in the /dev directory by convention (although it can, in theory, be anywhere). Linux supports up to two different sound cards, so two device driver files are possible for each type of driver.

The system administrator can change the device driver files and their purposes, but the administrator should retain the basic names for ease of integration with applications that require specific device names. The sound card device drivers are the following:

The PC speaker devices (/dev/pcaudio, /dev/pcsp, and /dev/pcmixer) are only used when the PC speaker device driver is loaded into the kernel.

Linking the Sound Card Files


Before you link in the sound card drivers, you must isolate the proper files that the compiler uses to form the device driver that is linked into the kernel. These files are usually supplied as part of the distribution software, or you can obtain them from the FTP or BBS sites. You may be able to skip the compilation step if you can find a precompiled device driver.

Each sound card has several source code files, usually with the .c extension, that take care of various aspects of the sound card support. These files include the device's initialization file and the drivers for the mixer, sequencer, MIDI interface, and so on. As a bare minimum, you need the file that initializes the card. This file is usually named after the sound card itself, such as sb_card.c for the Sound Blaster card or gus_card.c for the Gravis UltraSound card. In addition to the .c files, there are essential matching .h files for each component. You also need the files soundcard.c, soundcard.h, and audio.c (only for later versions of Linux), which are the driver skeletons to which the specific sound card details are linked.

An important component of the sound card device driver is the soundinstall script that constructs the drivers. This script may be supplied as a specific file, or it may be attached to the end of a documentation file. Slackware releases, for example, include a file called Readme.Linux in the sound directory that has the script appended to the end of the file. This script must be cut out and run by itself. The soundinstall script checks for the existence of a current device file, removes it if it exists, and then uses mknod to create a new device file. Of course, you also can perform these steps manually.

To compile the sound card drivers, follow a sequence of steps that places the required files in particular locations (as defined in the compilation script or Makefile), and then invoke the compilation process. The procedure is as follows:

The soundinstall script uses the mknod command to create the device drivers in the /dev directory. If you want to perform the steps manually, these basic mknod commands are required:


mknod -m 666 /dev/mixer c 14 0

mknod -m 666 /dev/sequencer c 14 1

mknod -m 666 /dev/patmgr0 c 14 17

mknod -m 666 /dev/midi00 c 14 2

mknod -m 666 /dev/dsp c 14 3

mknod -m 666 /dev/audio c 14 4

mknod -m 666 /dev/sndstat c 14 6

Some of these commands aren't needed if your sound card doesn't support the functions (such as a MIDI interface or sequencer). The preceding steps are for the first sound card. If you are installing two sound cards, you have to repeat the commands with the second device names. The soundinstall script handles this step automatically.

Providing Configuration Information


When the installation process is running, either from an installation script like setup or install or from a compilation of the sound card drivers, you are asked for a number of configuration parameters. These parameters are used to generate a file called local.h that defines the sound card configuration information. You are prompted for the type of sound card, IRQ, DMA, and I/O address, as well as other information pertaining to each type of driver.

If you don't get asked for this kind of information, you must edit the local.h file (if it exists) manually. A file called sound_config.h contains generic configuration information. You can edit this file to complete any configuration parameters that are not prompted for by the configuration routine. Most of the sections of the sound_config.h file are clearly labeled as to their purpose.

The installation procedure should end with the Linux kernel rebuilding, relinking, and then setting the new kernel as the boot image so that the drivers are active when you start Linux. Without this step, none of the changes you made will be effective.

Testing the Sound Card Drivers


After the kernel has been rebuilt with the sound card drivers embedded, reboot the machine to test the sound card. Watch the startup messages carefully to see the initialization messages. If you miss the messages, you can recall them with the command


dmesg

which shows all the kernel's startup messages on-screen again.

If the sound card drivers were properly installed in the kernel, Linux tries to contact the sound card and initialize it. In the startup messages, you see a line similar to


snd1 <SoundBlaster Pro> at 0x220 irq 5 drq 1

which, in this case, shows that the driver for a Sound Blaster Pro card (or compatible) was loaded with the I/O address set to 220H, an IRQ of 5, and a DMA of 1. If the sound card had values for any of these parameters that were different from the ones shown, the card initialization would probably fail.

If you are booting Linux for the first time after installing new drivers, some diagnostic, warning, or extra error messages may be displayed. These messages may not be repeated every time you reboot, just the first time. For this reason, you should use the dmesg command to check for these messages after relinking and rebooting the kernel with the sound card driver.

Assuming the drivers are linked properly, the device drivers initialize the card and the Linux registers that use it. All you need to do now is test the sound card with an application that tries to access the card. Any sound file will do. A few samples are usually included with the sound card drivers, or you can obtain sound files from FTP or BBS sites or possibly the Linux distribution disks.

To test the sound card, use the cat command to send the test file to the sound device. For example, if you have a sound file called parrot_sketch.au (many sound files have .au as their extension, although this is far from a convention) and your sound card is called /dev/audio, you can send the file to the sound card with the command


cat parrot_sketch.au > /dev/audio

If you don't hear anything from the sound card (and you have headphones or speakers connected with the volume set properly), the drivers are not communicating with the card properly. (Or your sound file is not properly formatted, in which case a prudent test is to use another sound file to confirm that the problem is the configuration.) Try the steps outlined in the Troubleshooting section later in this chapter. If you hear your sound file played properly, all is well and you have completed your sound card's installation.

If your sound card is capable of recording, and you have linked in a driver that supports recording from the sound card's microphone (or other input), you can test the recording capability of the sound card by recording a few seconds of audio and storing it in a file. The easiest way to do this procedure is to use the command


dd bs=8k count=10 < /dev/audio > test_mic.au

This command records 10 seconds from the input of /dev/audio and stores it in the file test_mic.au. You can then play this sound file back to check the fidelity of the recording with the command


cat test_mic.au > /dev/audio

If you heard whatever sounds you recorded, the input sampling of the sound card is working properly. If you heard silence or got an error message, either the sound card driver doesn't support recording or the settings are incorrect.

Using the PC Speaker


Don't have a sound card in your system, or you do have one but it is not supported and you don't want to hassle with drivers? Use the PC speaker instead. The quality of sound from a PC internal speaker is a lot more limited than a sound card, but it isbetter than no sound at all.

The Linux drivers for the PC speaker are usually located in an archive file with the name pcsndrv followed by the version number, such as pcsndrv-0.6.tar.z. You can search your filesystem or distribution media for a file with this name using the command


find / -name pcsndrv* -print

Many Linux software distributions do not include the PC speaker sound drivers, but they are available from almost all FTP and BBS sites. For example, the latest PC speaker sound drivers are located in the directory /pub/Linux/kernel/misc-patches at the sunsite.unc.edu FTP site. Check for the most recent release of the drivers.

Installing the PC speaker sound driver is a matter of linking the device driver into the kernel, and then rebuilding the kernel. The documentation that accompanies the device driver explains the process in detail.

Sampling Linux Sound Applications


A few Linux applications use sound, and the number is increasing almost daily. This section describes sample applications for each type of sound card function. These applications are generally supplied with Linux distributions. For a complete list of all applications that support music, check the Linux documentation or local download sites. The USENET newsgroups also are helpful for finding the most useful applications for particular purposes.


Troubleshooting the Sound Card


If you have properly installed the sound card, yet don't hear anything when you try to test it (or get error messages during initialization), it's time to follow a few simple diagnostic and troubleshooting procedures. Follow these steps in the order they're presented to try and isolate the problem.

Configuration Information


Check the initialization messages from the kernel. When the machine reboots with the kernel that has the sound drivers linked in, you should see some initialization messages similar to


snd1 <Sound_Card_Name> at 0x330 irq 1 drq 1

where the sound card's name and configuration parameters are shown. Verify the sound card parameters! Incorrect prarmeters are the most common cause of sound card failure, as many users assume default values when the values have been changed. Sometimes the default values are used on the sound card but are incorrectly entered in the configuration information.

If no lines in the bootup messages refer to the sound card driver, then the driver is inactive or not linked to the current boot kernel. Make sure you linked the drivers to the current boot kernel. Some Linux systems don't display any boot messages if the configuration information is correct, and others generate a message telling you the information is correct.

Use the sound card's manual to determine the default settings and compare them to the board. Physically remove the sound board and examine the jumpers, DIPs, or whatever method is used for setting the configuration. Some boards use software configuration, so you should boot your machine in DOS and use the diagnostics supplied with the card to examine and set the board. Write down all the settings, and then check the device driver configuration file to ensure that they match.

A good method of checking settings is to run a DOS application that uses the sound board. If the settings work in DOS, they will work in Linux. However, if a DOS application can't access the sound card properly, that indicates a configuration problem (or the sound card is defective). If the settings are incorrect, re-enter them into the device driver configuration information, recompile and relink the kernel, and then reboot the machine and test the sound card again.

Check the File /dev/sndstat


The file /dev/sndstat should contain some basic information about the sound card and its initialization. Not all versions of Linux provide this file, so don't be too surprised if it doesn't exist. In this case, skip this step. If the /dev/sndstat file does exist, the contents should show the name of the configured card and its parameters, as well as any additional installed devices. For example, a file may contain the following lines:


Sound Driver 3.1

HW config:

Type 1: SoundBlaster Pro at 0x220 irq 5 drq 1

PCM devices:

0: SoundBlaster Pro 3.2

Synth devices:

0: Yamaha OPL-3

Midi devices:

0: SoundBlaster

Mixer(s) installed

This sample file shows the drivers and components that are installed for a typical Sound Blaster Pro card. Additional messages may appear for other drivers or for error conditions. Check the file for anything of use, and verify the configuration parameters with your sound card documentation.

If you can't find the /dev/sndstat file or it is empty, either your Linux version doesn't support that file or the sound card was not recognized during boot. Usually the HW config section is filled in even if the sound card was not found.

No Such File or Device Errors


If the sound card seems to load but then you can't test it, or you get the error message


No such file or directory

when the sound card tries to initialize or you run an application that uses the sound card, the problem is the device driver files. Typically, these problems mean that the device driver files do not exist or the files are not in the proper location.

Check to ensure the device driver files are linked into the kernel and are in the /dev directory. Check the section "Understanding Sound Card Device Files" earlier in this chapter for a list of the sound card device driver files you should have. If the device driver files exist, check that the major and minor device numbers do not conflict with any other active device.

If you get the error message


No such device

when an application tries to use the sound card, it means the device driver wasn't loaded in the kernel boot process. Check to make sure you did relink the kernel and you have booted the system using the new image.

If you get the message


device busy

when an application is running and trying to communicate with the sound card, it means that more than one process is using the sound card at the same time. Linux allows only one process to access the sound card device at a time. You can determine which processes are using the sound card using the fuser command. For example, if the problem is with the /dev/audio device file, issue the command


fuser -v /dev/audio

and examine the output to see which processes are currently using the device. Repeat the command for the /dev/dsp device if the /dev/audio device has no conflict.

If the device busy error message persists, it is likely a DMA error. Make sure you are not using DMA 0 for the sound card. Some sound cards allow this DMA channel to be chosen, but Linux uses DMA 0 as a special refresh channel for DRAM. Change to DMA channel, reconfigure the kernel, relink, rebuild, reboot, and then test again.

Incomplete Playing of Sound Files


If you only hear a small section of a sound file and then the playing abruptly stops, or you get an error message after a file has started to play, the problem is probably an incorrect IRQ or DMA setting. You may see messages that tell you the IRQ or DMA has timed out.

To correct this problem, check the DMA and IRQ values on the sound card and in the configuration files. If the values match, check for a conflict with other devices in your PC. Odds are that the IRQ or DMA is shared with another device (network cards are a common culprit). Change the settings on whichever card is the easiest. (If you already have a network up and running, change the sound card if it will let you.)

Sounds Stop and Start When Playing


Sounds stop and start when playing because the sound card, computer, or hard disk can't keep up with each other. The simplest method of solving this problem is to choose a lower sampling rate or switch to mono. Alternatively, if your system is running many processes (as it will with some games), try eliminating applications you don't need. X applications that run on the desktop are good at gobbling up huge chunks of processor time. Freeing up enough CPU time to support the sound card will help. For a longer-term solution, consider either upgrading your computer to a faster processor or getting a sound card with more capabilities, including on-board RAM.

Summary


The sound card is a useful peripheral when it's properly supported by a Linux application. It is also one of the most frustrating devices to get working properly. If you can use an automated installation utility instead of trying to manually build the drivers, do so. They will save you a lot of grief!

A growing number of applications support sound cards, especially games and X applications, so when you have your sound card properly configured and tested, keep your eye out for them. They make using Linux a lot more interesting.

Previous Page Page Top TOC Next Page