ALSA

This article describes the setup of a sound card with ALSA (Advanced Linux Sound Architecture).

Hardware detection
To choose the right driver, first detect the used audio controller. You can use lspci for this task:

With the controller name you can find the needed driver in the ALSA SoundCard Matrix.

Kernel
You need to activate the following kernel options:

Software
Portage knows the global USE flag alsa for enabling support for ALSA in other packages. Enabling this USE flag will pull in automatically (default in x86 and amd64 desktop profiles):

The USE flags of are:

After setting this you want to update your system so the changes take effect:

You also want to install, if it isn't already pulled in:

Permissions
If you have the USE flag acl enabled globally and are using ConsoleKit (i.e you're using a desktop profile) permissions to sound cards will be handled automatically. You can check the permissions using getfacl:

A broader solution is to add the user you want to be able to access the sound card to the audio group:

Service
You can now start ALSA:

To start ALSA at boot time, add it your boot runlevel:

Mixer
If you can't hear anything, the output channels may be muted. Unmute the channels with your Desktop Environment's mixer or with alsamixer:

Advanced Configuration

 * Power management

~/.asoundrc
This file can be optionally used so that on a per-user basis, ALSA defaults can be overridden. You might have special hardware, or card 0 and device 0 (the defaults) are not going to work for you.

One of the simplest changes is card and device. This is the case for me with an HDMI coming from an Nvidia card connected to an onboard Realtek ALC88* audio device.

You can find out what devices and cards you have with:

Tips
Try one of the many configuration options in PaulBredbury's asoundrc file.

Sound mixing inconsistent
Sometimes one app essentially takes over all sound devices. This might even be for performance reasons.

Force the use of dmix instead of direct audio output (which is what most things use by default, such as Flash (which really means your browser) and Wine).

If your device is card 1 and device 7 (which is the case for Nvidia HDMI for me):

Use of ~/.asoundrc is immediate and as long as you are not forcing the use of specific devices in any applications, they either will require a restart or will begin working immediately. One of the best tests is to run Chrome, go to YouTube, open a terminal, run mplayer with an audio or video file and see that you do not get an error about audio (such an error might be 'Device or resource busy').

Missing dialogue (sounds) with 4.0 speakers
If you're using a 4.0 sound card (like an old SB Live!) or 4.0 speakers in general, you may notice that in some games or movies the dialogues are very quiet or even missing. This is because most of those applications/movies support only either 2.0 (stereo) or 5.1 output. In order to achieve surround sound, the 5.1 audio track is used but the two excessive channels are discarded — the center channel (which usually carries dialogues) and subwoofer channel.

The above issue can be circumvented through creating a virtual device which will downmix 5.1 to 4.0, mixing the center and subwoofer channels with other audio channels.