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 nothing, maybe some channels are muted. Unmuted 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').