Наши партнеры

Книги по Linux (с отзывами читателей)

Библиотека сайта rus-linux.net

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

22. Sound Files

@sf{Debian}: `alsa-base'
@sf{WWW}: http://www.alsa-project.org/

This chapter covers the basic control of audio on Linux-based systems, including how to adjust the audio mixer and how to play and record sound files using basic tools. You can also play and record audio with snd (see section Editing Sound Files) and many other sound applications.

In order to have sound working on your system, you need to have a sound driver installed and configured for your sound card. There are two free sound drivers that are popularly used on Linux-based systems. The standard Open Sound System driver ("OSS/Free") by 4Front Technologies is currently part of the Linux kernel and is what comes as default with most Linux distributions. The Advanced Linux Sound Architecture ("ALSA") is another free driver that is becoming popular with musicians who use Linux.

NOTE: Most systems come configured so that you must be the superuser to be able to use sound devices, including audio CDs. If this is the case on your system, ask your administrator to give you access to these devices, typically by adding you to the audio group (see section Groups and How to Work in Them).

22.1 Sound File Formats  Sound file formats.
22.2 Adjusting the Audio Controls  Adjusting the sound mixer.
22.3 Playing a Sound File  Playing a sound file.
22.4 Recording a Sound File  Recording a sound file.
22.5 Other Sound File Tools  Tools for playing and recording sound.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

22.1 Sound File Formats

The following table lists common audio file formats and their traditional file name extensions. You can also use file to determine a file's format (see section Determining File Type and Format).

.aiff Apple Macintosh audio file.
.au Sun Microsystems audio file (8000 Hz, u-law compression).
.cdda or .cdr Both are names for the audio compact disc format, used for burning audio CD-Rs and CD-RWs (44.1 KHz raw stereo).
.gsm Global System for Mobile Communications (GSM) speech file format, used in some voice-mail applications.
.midi or .mid The standard extensions for MIDI files.
.mod MOD file.
.mp3 MPEG II, Level 3 file.
.ogg Ogg Vorbis file.
.ra RealAudio file.
.raw Raw audio data.
.sf IRCAM SoundFile format, used by some music composition software, such as CSound and MiXViews.
.voc SoundBlaster VOC file.
.wav Microsoft RIFF format ("WAV").

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

22.2 Adjusting the Audio Controls

A mixer program is used to adjust various audio settings, such as volume and recording levels, and is also used for turning on or muting the microphone or other input device. You must use a mixer to adjust your audio settings before you play or record sound.

ALSA's default mixer is called amixer, and the following recipes assume its use. There are other mixers, and some of them are easier to use than amixer. If you want a graphical mixer, install the aumix package, or see the end of this chapter for others.

22.2.1 Listing the Current Audio Settings  Listing the current audio settings.
22.2.2 Changing the Volume Level  Changing the volume.
22.2.3 Muting an Audio Device  Muting an input device.
22.2.4 Selecting an Audio Recording Source  Specifying the recording input source.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

22.2.1 Listing the Current Audio Settings

To list all audio input and output devices and their settings, type amixer with no options.

Your sound card's components are organized in groups, from the Master group containing the master left and right volume settings to the individual groups for audio compact discs and digital sound files. (These groups have nothing to do with the file access groups described in Groups and How to Work in Them.)

  • To peruse the current mixer settings, type:

    $ amixer | less RET

The following table describes some of the important sound groups that amixer lists.

Master The master volume settings.
PCM Digital audio for playing sound files; the first channel is group PCM,0 and the second is PCM,1.
CD The audio compact disc player (a cable must be connected from the CD-ROM drive to the sound card).
Synth The synthesizer device for MIDI.
Line The sound input device (the jack on the back of the soundcard is usually labeled LINE IN).
MIC The microphone device (the jack on the back of the soundcard is usually labeled MIC).

To list the settings for only one group, use the `get' option followed by the name of the group you want to list. Group names are case sensitive--so giving MIC specifies the microphone group, while Mic and mic are not valid groups.

  • To output the microphone settings, type:
    $ amixer get MIC RET

  • To output the second PCM settings, type:

    $ amixer get PCM,1 RET

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

22.2.2 Changing the Volume Level

To change a mixer setting, give the amixer `set' command as an option, followed by both the group and setting to change as arguments. To change the volume level for a device, give either a numeric value or a percentage for the volume level.

  • To set the master volume to 75 percent, type:
    $ amixer set Master 75% RET

  • To set the PCM volume to 30, type:

    $ amixer set PCM 30 RET

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

22.2.3 Muting an Audio Device

The special `mute' and `unmute' arguments are used for muting the volume of a given device. Before you can record something, you must unmute the input device you want to record from. Remember to also mute the microphone after you have finished recording, to avoid feedback when you turn up your speakers.

  • To unmute the microphone and turn it on for recording, type:
    $ amixer set MIC unmute capture RET

  • To mute the microphone, type:
    $ amixer set MIC mute RET

  • To unmute the master volume and set it to 80 percent volume, type:

    $ amixer set Master 80% unmute RET

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

22.2.4 Selecting an Audio Recording Source

To select a device for recording, use set followed by the name of the device and the `capture' argument, which designates the specified group as the one to capture sound from for recording.

  • To select the LINE IN jack as the recording source, type:
    $ amixer set Line capture RET

  • To select the microphone jack as the recording source, type:

    $ amixer set MIC capture RET

NOTE: You can have only one group selected for capture at a time, and when you select a group as an input source for recording, you are simply turning the microphone or other input on; recording does not occur until you use a recording tool.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

22.3 Playing a Sound File

@sf{Debian}: `sox'
@sf{WWW}: http://home.sprynet.com/~cbagwell/sox.html

The play tool distributed with sox (a sound file translation tool) can recognize and play many audio formats, including WAV, VOC, AU, AIFF, and SND format files, as well as audio CD-format files and various raw binary files; just about the only common audio formats it can't handle are MP3 and MIDI files, which are discussed in the sections to follow.

  • To play the file `pentastar.aiff', type:

    $ play pentastar.aiff RET

NOTE: Before you begin playing sound, make sure you've set the master and PCM volume levels with the mixer (see section Adjusting the Audio Controls). The most common reason for no sound being produced when you try to play sound is not having the volume turned up!

ALSA comes with aplay, a tool for playing sound files that is similar to play, but does not recognize as many formats.

22.3.1 Playing an MP3 File  Playing MP3 files.
22.3.2 Playing a MIDI File  Playing MIDI files.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

22.3.1 Playing an MP3 File

@sf{Debian}: `mpg321'
@sf{WWW}: http://freshmeat.net/projects/mpg321/

To play an MP3 file, give its name as an argument to mpg321.

  • To play the MP3 file `september-wind.mp3', type:

    $ mpg321 september-wind.mp3 RET

To buffer the audio, useful for when the system is running many processes or otherwise has a lot of activity, give a buffer size, in kilobytes, as an argument to the `-b' option. The default is 0 (no buffer); if you need this option, use a size of at least 1024KB (which is 1MB), or about six seconds of MP3 audio.

You can also use mpg321 to play "streaming" MP3 audio from the Web; just give the URL of the MP3 stream as an argument.

NOTE: The mpg321 tool is a free software replacement of mpg123, another command-line MP3 player that is not free software.

But there are a great many other MP3 players available; some of them are listed in Other Sound File Tools.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

22.3.2 Playing a MIDI File

@sf{Debian}: `playmidi'
@sf{WWW}: http://playmidi.openprojects.net/

The playmidi tool is for playing MIDI files; give the name of the MIDI file to play as an argument.

  • To play the MIDI file `copa-cabana.mid', type:

    $ playmidi copa-cabana.mid RET

If you have a non-MIDI sound card, you can still play MIDI files by giving the `-f' option, which sends the MIDI output to the FM synthesizer on the sound card, which in turn plays it using FM patches that come with the playmidi distribution.

  • To play the MIDI file `copa-cabana.mid' on a non-MIDI sound card, type:

    $ playmidi -f copa-cabana.mid RET

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

22.4 Recording a Sound File

@sf{Debian}: `sox'
@sf{WWW}: http://home.sprynet.com/~cbagwell/sox.html

To record sound, first select an input device as a source for recording. Sound cards may have MIC and LINE IN jacks, as well as connections to the CD-ROM drive, all of which are sound inputs that can be recording sources. When you select a device for capture, your recording will come from this source.

Recording occurs from the currently active input, if any, which must be set with the mixer; unmute it and set its volume level before you begin recording. (Be sure to turn the volume on your speakers all the way off, or you'll get feedback.)

To record audio to a file, use the rec tool. It can write many audio file formats, either to a format you specify with the `-t' option, or by determining the format to use based on the file name extension you give the output file (see section Sound File Formats). Type C-c to stop recording.

Give the name of the sound file to record as an argument; if a `.wav' file is specified, it records a simple monaural, low-fidelity sound sample by default.

  • To record a simple WAV sample from the microphone and save it to a file called `hello.wav', type:

    $ rec hello.wav RET

This command begins an 8,000 Hz, monaural 8-bit WAV recording to the file `hello.wav', and keeps recording until you interrupt it with C-c. While the default is to make a low-fidelity recording--8,000 Hz, monaural 8-bit samples--you can specify that a high-fidelity recording be made. (But remember that high-fidelity recordings take up much more disk space.)

To make a stereo recording, use the `-c' option to specify the number of channels, giving 2 as the argument. To make a 16-bit recording, give `w' ("wide") as the argument to the `-s' ("sample size") option.

Set the recording sample rate by giving the samples per second to use as an argument to the `-r' option. For CD-quality audio at 44,100Hz, use `-r 44100'.

Finally, to record a file in a particular format, either give the name of the format as an argument to the `-f' option, or use the traditional file name extension for that format in the output file name (see section Sound File Formats).

  • To make a high-fidelity recording from the microphone and save it to a WAV-format file called `goodbye.wav', type:
    $ rec -s w -c 2 -r 44100 goodbye.wav RET

  • To make a sound recording in the CD audio format, and write the output to a file called `goodbye.cdr', type:

    $ rec goodbye.cdr RET

NOTE: When you're not recording sound, keep the inputs muted (see section Muting an Audio Device); this way, you can have a microphone plugged in without having feedback when playing sounds. Also, make sure the volume levels are not set too high or too low when recording; getting the right level for your microphone or other input device may take some initial adjustment.

Like play, rec is part of the sox toolkit.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

22.5 Other Sound File Tools

There are many mixer, playback, and recording tools available. The following table lists some of them, giving their Debian package name and URL, where available.

aumix A simple, visual audio mixer tool that can be used in X or in the console--use this if you are too frustrated by amixer. {@sf{Debian}}: `aumix'
freeamp A popular audio sound file player for X, the Free Audio Music Player can play MP3 and Ogg Vorbis formats, and its graphical appearance can be changed with "themes." {@sf{Debian}}: `freeamp' {@sf{WWW}}: http://www.freeamp.org/
maplay An MP3 player. {@sf{Debian}}: `maplay'
mp3asm Use this tool to cut and paste MP3 frames and fix broken MP3 files. {@sf{Debian}}: `mp3asm' {@sf{WWW}}: http://packages.debian.org/stable/sound/mp3asm.html
xmms Inspired by Winamp, XMMS is a popular, comprehensive audio player for X that features an array of plug-ins, support for many sound formats (including MP3 and Ogg Vorbis), and "skins" to change its look and feel; it can use FreeAmp themes as well--see previous listing for freeamp. {@sf{Debian}}: `xmms' {@sf{WWW}}: http://www.xmms.org/

[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]