ALSA
This article describes the setup of a sound card with ALSA (Advanced Linux Sound Architecture).
Contents |
Installation
Hardware detection
To choose the right driver, first detect the used audio controller. You can use lspci for this task:
root # lspci | grep --color -i audioWith the controller name you can find the needed driver in the ALSA SoundCard Matrix.
Kernel
You need to activate the following kernel options:
Device Drivers --->
Sound --->
<*> Sound card Support
<*> Advanced Linux Sound Architecture --->
[*] PCI sound devices --->
Select the driver for your audio controller, e.g.:
<*> Intel HD Audio ---> (snd-hda-intel)
Select a codec or enable all and let the generic parse choose the right one:
[*] Build Realtek HD-audio codec support
[*] ...
[*] Build Silicon Labs 3054 HD-modem codec support
[*] Enable generic HD-audio codec parser
Software
Portage knows the global USE flag alsa for enabling support for ALSA in other packages. Enabling this USE flag will pull in media-libs/alsa-lib automatically (default in x86 and amd64 desktop profiles):
USE="... alsa ..."
The USE flags of media-libs/alsa-lib are:
| USE flag | Default | Recommended | Description |
|---|---|---|---|
| alisp | No | Enable support for ALISP (ALSA LISP) interpreter for advanced features. | |
| debug | No | Enable extra debug codepaths, like asserts and extra output. If you want to get meaningful backtraces see http://www.gentoo.org/proj/en/qa/backtraces.xml | |
| doc | No | Adds extra documentation (API, Javadoc, etc). It is recommended to enable per package instead of globally | |
| python | Yes | Adds optional support/bindings for the Python language |
After setting this you want to update your system so the changes take effect:
root # emerge --ask --changed-use --deep @worldYou also want to install media-sound/alsa-utils, if it isn't already pulled in:
root # emerge --ask alsa-utilsConfiguration
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:
user $ getfacl /dev/snd/controlC0 | grep larry
user:larry:rw-A broader solution is to add the user you want to be able to access the sound card to the audio group:
root # gpasswd -a larry audioService
You can now start ALSA:
root # /etc/init.d/alsasound startTo start ALSA at boot time, add it your boot runlevel:
root # rc-update add alsasound bootMixer
If you can't hear anything, the output channels may be muted. Unmute the channels with your Desktop Environment's mixer or with alsamixer:
user $ alsamixerTest setup
If everything above is perfect, you should now be able to test your sound card and your speakers. We will use the speaker-test command line tool from package media-sound/alsa-utils (this should already be installed as per our previous recommendation).
user $ speaker-test -t wav -c 2If you have a 5.1 system:
user $ speaker-test -t wav -c 6 -D surround51Press Ctrl+C to interrupt the test.
Advanced Configuration
~/.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.
defaults.pcm.!card 1 defaults.pcm.!device 7
You can find out what devices and cards you have with:
user $ aplay -LS/PDIF or HDMI .asoundrc
Background
Most sound cards allow passing through audio to an external consumer receiver or DAC using S/PDIF digital coaxial or optical cables. Doing so, preserves sound quality and compressed Dolby/DTS encoded material. Uncompressed Dolby/DTS or Bluray material, require HDMI connections. The more common mono or analog stereo cables cannot carry Dolby/DTS signals.
You may find your S/PDIF or HDMI connection with default installed ALSA works with no .asoundrc file alterations. While only some applications, such as the Web Browser Flash plugin will fail playing sound. As such, the below .asoundrc is usually required for most S/PDIF and HDMI connections. Also, any media applications open will need to be restarted for the .asoundrc files to take affect. (ie. Web Browser with Flash plugin)
Preference for Connections Versus Media Types
Basic Analog
Basic Analog (ie. RCA) Connections - Basic user. Quality depends on sound card DAC. Look for a sound card with a high SNR db level.
S/PDIF
S/PDIF provides good quality audio for music, videos and DVD quality movies containing Dobly/DTS compressed signals. Most sound cards and motherboards, these days, provide some sort of S/PDIF port. Nowadays, it's more common to see S/PDIF Toslink ports on motherboards. Many computer games provide compressed Dolby/DTS signals.
S/PDIF Digital Coaxial
S/PDIF Digital Coaxial may have problems with voltage cross talk, but is more common as it only requires a simple mini jack or RCA coaxial cable.
S/PDIF Optical (Toslink)
S/PDIF Optical (Toslink) Cable completely avoids possible electrical cross talk or interference amongst cables as it's fiber optic, but is suspectible to signal degradation if the cable is bent too much. You'll find audiophiles tend to favor Optical/Toslink.
HDMI
HDMI can carry compressed Dolby/DTS and uncompressed (ie. Blueray) Dolby/DTS signals. The audio market has favored this connection, but still preserves S/PDIF connections. One concern being, HDMI cables are copper wire, still susceptible to electrical cross talk or interference, similar to S/PDIF Digital Coaxial connections. There are HDMI Optical cables, but too expensive for the consumer market. If audiophiles have S/PDIF Optical ports, they will use the S/PDIF connections for other media such as music and DVD movies, while only using HDMI when needed for processing uncompressed Dolby/DTS encoded material such as Bluray media. Again, the basic user will likely just use HDMI, avoiding the fuss of changing configuration files around.
Configuration
Find your digital output device.
root # aplay --list-devicesAdjust the below file to use your card/device number.
pcm.!spdif {
type hw
card 0
device 1
}
pcm.!default {
type plug
slave {
pcm "spdif"
}
}
# Share a single card with multiple applications
#pcm.!default {
# type plug
# slave.pcm "dmix:CARD=0,DEVICE=1,RATE=48000"
#}
A/52, AC3, Dolby, DTS
Soundcards providing S/PDIF output can pass through lossy compressed multichannel audio. To my hears, the high bitrate compressed media has little loss in comparison to two channel CD or DVD audio. I surmise this is because we now have multichannels feeding multiple speakers. Hence, the more hardware, the equivelant or better sounding?
If a soundcard states it has S/PDIF, it will likely pass through Dolby or DTS even though it does not specifically show a Dolby or DTS icon or listed within it's features. This is because, when they do list Dolby or DTS, it's because they're providing software for upmixing, or providing the decoded signal through analog output.
HDMI will pass through uncompressed multichannel audio, but a video feed is interleaved as required by the HDMI specification. In comparison to DVD video and audio, I see and hear very little quality difference, if any! About the only thing I noticed between S/PDIF Toslink and HDMI, HDMI seems to amplify the signal by a few watts or 5db. (I conclude this is because of the higher bitrate?)
As such, S/PDIF is still quite popular, even today.
Decoding or encoding to Dolby or DTS requires a license. If you have already purchased a sound card with this multichannel support, then you likely already have a license.[1]
ALSA media-plugins/alsa-plugins package requires recompilation to include the ffmpeg USE Flag with the A/52 (pcm.a52encode) plugin.
root # USE="ffmpeg" emerge -q media-plugins/alsa-pluginsDecode or Playback
Recompile mplayer or your other favorite software player to include the ALSA libraries.
root # USE="a52 dts" emerge -q media-video/mplayerAdd the multichannel codecs to mplayer.conf, so media attempting to be played has first been provided the option of hardware passthrough rather then down mixing.
ac=hwac3,hwdts,hwmpa,spdifac3,spdifeac3,spdifaac,spdifdts,spdifmpa,spdifthd,dts,ffaac,
You should now be able to pass through (and enjoy) almost any Dolby or DTS signal through S/PDIF to your reciever.
Encode to A/52
To similate A/52 encoded audio from normal one or two channel audio streams or files, ALSA can upmix using it's A/52 plugin. (A/52 is also known as AC-3 or Dolby Digital encoding.)
A/52 upmixing preferred when playing computer games or watching video without Dolby/DTS encoded material, such as older Movies. Many sound card manufacturers provide their applications with a feature for software upmixing to Dolby Digital, and recommend enabling this upmixing when listening to such media, except for Music.
Upmixing to A/52 is frowned upon by audiophiles. As such, it's user preference if you want to upmix all your sounds to A/52 before sending the stream to your consumer stereo receiver or DAC. Matter of fact, audiophiles prefer simple stereo, and further state stereo is still better quality than Dolby/DTS material or other audio encoded with gimmicks. Also, your consumer stereo or DAC likely possibly includes a feature for upmixing audio into five channel audio.
pcm.!default {
type plug:surroundaudio
}
ctl.!default {
type plug:surroundaudio
}
pcm.a52encode {
type a52
format S16_LE
channels 6
rate 48000
bitrate 448
}
pcm.surroundaudio a52encode
ctl.surroundaudio {
type hw
card 0
device 1
}
user $ speaker-test -Dsurroundaudio -c 6JACK Audio Connection Kit
Jack provides an additional layer, to the already low latency ALSA drivers and applications, and further iterates low latency audio playback or customized latency audio playback and recording. Jack only seems to provide one or two channel audio playback support. Jack further provides the ability to view, manipulate or pipe audio streams, such as JAMin which can intercept an audio stream before exporting the stream through analog or S/PDIF outputs. The Jack user owned daemon, is usually started using qjackctl. The qjackctl utility provides many other fuctions including manipulation of audio streams.
If I'm not mistaken, what Jack does not do, is handle A/52 encoded (ie. AC-3 or Dolby/DTS) material on input; or anything other than one or two channel audio streams unless the media is being recorded through multiple channels. The ac3jack tool is required for encoding to AC-3 streams. A common side effect after configuring ALSA to use Jack, may leave you with the undesirable effect of not being able to playback A/52 encoded media and media players will have no sound, or the Jack daemon may abruptly quit. This will occur even when using S/PDIF or HDMI connections, as Jack processing occurs before output.
What I suggest, configure your Kernel and ALSA according to Jack installation instructions for providing low latency audio. Since ALSA is said to be already very effeciant and low latency, providing very good quality playback with no additional mixing when stated within it's .asoundrc file, unlike MS Windows, Jack probably isn't needed for most. (The ac3jack package is currently hosted by the pro-audio layman.)
It may be possible, to provide a more detailed .asoundrc to handle the multiple channels, but I'm currently unware of this as of this writing. (ie. How do I route audio to/from generic ALSA-using applications?
Hopefull this small brief section about Jack will give some quick insight about Jack's features, while reserving the details of Jack to a Wiki page specifically focused on Jack.
Tips
Try one of the many configuration options in PaulBredbury's asoundrc file.
Test Mic Recording
If needed, select your audio device if no default one is provided and add the "--device=hw:0,0" to your incantation of arecord, substituting your CardNumber,DeviceNumber.
The following will list possible devices.
user $ arecord --list-devicesThe following will record indefinitely until CTRL-C is depressed and provide a default 8000 Hz mono quality recording.
user $ arecord /tmp/test.wavThe following will provide will record for two seconds (--duration=2) using DAT 48000 Hz quality (--format=dat) and display the curses vumeter in stereo (--vumeter=stereo) and save to /tmp/test.wav.
user $ arecord --duration=2 --format=dat --vumeter=stereo /tmp/test.wavUse mplayer or aplay to playback the saved file. I usually record in "dat" or atleast "cd" quality formats. DAT is best when benchmarking..
Troubleshooting
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):
pcm.dmixed {
type asym
playback.pcm {
type dmix
ipc_key 5678293
ipc_perm 0660
ipc_gid audio
slave {
channels 2 # make 6 or 5.1 channel
pcm {
format S16_LE # S32_LE
rate 48000 # can also be 44100
type hw
card 1 # your card
device 7 # your device
subdevice 0 #important?
}
period_size 1024
buffer_size 8192
}
bindings {
0 0
1 1
# Uncomment below if using 6 channel
# 2 2
# 3 3
# 4 4
# 5 5
}
}
capture.pcm "hw:0"
}
pcm.!default {
type plug
slave.pcm "dmixed"
}
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.
pcm.downmix {
type route
slave {
# the sound card output to be used
pcm surround40
# real number of output channels
channels 4
}
# ttable.A.B G
# where A - input channel
# B - output channel
# G - volume gain (1.0 = original)
# copy channels 0-3
ttable.0.0 1
ttable.1.1 1
ttable.2.2 1
ttable.3.3 1
# mix channel 4 (center) into front speakers, and a bit (0.3) into rear ones
ttable.4.0 1.0
ttable.4.1 1.0
ttable.4.2 0.3
ttable.4.3 0.3
# mix channel 5 (subwoofer) mostly (0.6) into rear speakers, and a bit (0.3) into front ones
ttable.5.0 0.3
ttable.5.1 0.3
ttable.5.2 0.6
ttable.5.3 0.6
}
ctl.downmix {
type hw
card 0
}