Hack 14. Surround Yourself with Sound
Get theater-quality sound out of your multi-speaker setup.
When you've invested a lot of money in your audio and speaker setup, you want to make sure you get the most out of it. Don't settle for basic stereo soundmake your bass go boom boom, your center channel speak, and your rear speakers provide ambience. Under Windows, configuring this is often quite easy, as the soundcard manufacturer usually provides a program to enable surround sound, but no such software exists for Linux, so you need to go a more manual route. This hack will show you how to enable surround sound, test it, and use it with MPlayer [Hack #48].
For this hack to work, you need a soundcard that supports more than just stereo output and, of course, multiple speakers. The ideal setup, and the one I am targeting in this hack, is what is known as "5.1 surround sound." This means you have a pair of front speakers, a pair of rear speakers, a center channel speaker, and a subwoofer. As you can see, this is 5 speakers plus a .1 for the subwoofer. (Just to confuse things, some settings will call this a 6 channel configuration.) Here is what you can expect out of each speaker:
I tested this using nForce4 integrated sound and Logitech X-530 5.1 speaker set. The nForce4 sound is provided by the Intel8x0 Alsa driver, which is commonly used in many integrated solutions, so the instructions I give here should work without modification on many setups. I'm going to assume you already have basic audio working. (This is typically not a problem with most modern Linux distributions.) You'll also need to follow your speaker installation instructions and make sure you have each of the cables plugged into the correct outputs.
2.3.1. Configuring Your Mixer
[Hack #13] provided a basic introduction to the concept of audio mixers that control the volume levels of your audio outputs. For this hack you are going to use the alsamixer command-line mixer, because it is very straightforward to use. You should find each of the options I'll describe here in the KDE (kmix) and GNOME (gnome-alsamixer) equivalents, but the settings may be in non-obvious places in the program. To get started, run this command inside a terminal:
This opens an ncurses-based mixer program that you can control with your keyboard. The left and right arrows will move you to the different options, the up and down arrows control the volume or settings of that option, pressing M toggles the muting of that channel, and Esc quits and saves your changes. If you already have basic sound working, then your Master and PCM channels are already unmuted and their volume set.
In this program you need to set volume levels for the other speaker channels as well as tell the mixer how you want audio sent to the various speakers.
Using the alsamixer options for my nForce4 Intel8x0 based soundcard as a basis, here are the settings you want to configure:
These settings provide the basic foundation for 5.1 audio. These settings should exist for any soundcard that supports surround sound, but I can't guarantee the option labels will remain the same.
2.3.2. Testing Surround Sound
When first playing with surround sound, it's useful to test speaker output using an audio file that sends audio to each speaker in turn. You can obtain such a file from ftp://ling.lll.hawaii.edu/pub/greg/Surround-SDL-testfiles.tgz. Download and unpack this file:
$ wget ftp://ling.lll.hawaii.edu/pub/greg/Surround-SDL-testfiles.tgz $ tar -xvzf Surround-SDL-testfiles.tgz
In the expanded directory you'll find a few WAV files that have multichannel output. My personal favorite is chan-id.wav, which when played speaks the name of each channel in the appropriate speakerprovided, of course, that everything is configured correctly. To test, use this command:
$ aplay -Dsurround51 chan-id.wav
This command uses the Alsa aplay player to send output to each channel in turn. If you don't specify -Dsurround51, you will only get stereo output. You know everything is working correctly if you hear a woman's voice announcing each speaker channel in the appropriate speaker. If this doesn't happen for you, then go back to alsamixer and see if you missed anything.
I've noticed a few oddities with this test. For example, when I run it on my Mandriva machine with the previous command, everything works correctlyexcept my subwoofer delivers a muffled and jumbled "rear rear left right," and the rear channel speakers then give their identification correctly. The command's text output suggests that I run the command this way:
$ aplay -Dplug:surround51 chan-id.wav
When I do this, the front and center speakers work as they should, but I get no other output from my other speakers except for the rear right, which states the jumbled "rear rear left right." None of this turns out to matter. As long as you do get the front and center speakers to announce themselves correctly, and you hear something out of the rear speakers, surround sound should work with MPlayer.
2.3.3. Movie Watching
The real reason for surround sound is to create a near theater experience when movie watching. This section covers configuring MPlayer for surround sound. As you may recall from "Use MPlayer" [Hack #48], the mplayer command for viewing a DVD is simply:
$ mplayer dvd://
Go to a scene with dialogue. Pay careful attention to which speaker the dialogue is coming out of. It will most likely be the left and right front speakers. Is any background sound coming from the rear speakers? Probably not. When you launch mplayer this way, the center and rear speakers remain silent. You have to tell mplayer that you have a multi-speaker setup like this:
$ mplayer -channels 6 dvd://
Again, listen to a scene with dialogue and listen for background sound from the rear speakers. In my setup, my center channel is still muted, but I hear sound and dialogue from my rear speakers. The problem here is that we haven't told mplayer what sound system to use. This is done with a simple addition to the command:
$ mplayer -ao alsa -channels 6 dvd://
Now, go to the same scenes as before. If everything is configured correctly in alsamixer, you should hear most of your sound coming from the front left and right speakers, dialogue coming from the center speaker, ambient sounds from the rear speakers, and, if you're watching explosions, then a nice boom boom from your subwoofer.
Specifying these settings each time you run MPlayer can be annoying. To get around this, add the options to your ~/.mplayer/config file like so:
# Write your default config options here! ao=alsa channels=6
As you can see, each option is identical to what you type at the command line, minus the preceding dash and converting the space to an equal sign.
If you use a GUI frontend to mplayer, it will probably provide its own preference option to enable surround sound, so check those settings if the ones in your configuration file don't work.