Gentoo Linux ALSA Guide Shyam Mani This document helps a user setup ALSA on Gentoo Linux. 1.0 2005-05-13 Introduction
What is ALSA?

ALSA, which stands for Advanced Linux Sound Architecture provides audio and MIDI (Musical Instrument Digital Interface) functionality to the Linux operating system. ALSA is the default sound subsystem in the 2.6 kernel thereby replacing OSS (Open Sound System), which was used in the 2.4 kernels.

ALSA's main features include efficient support for all types of audio interfaces ranging from consumer soundcards to professional sound equipment, fully modularized drivers, SMP and thread safety, backward compatibility with OSS and a user-space library alsa-lib to make application development a breeze.

ALSA on Gentoo

One of Gentoo's main strengths lie in giving the user maximum control over how a system is installed/configured. ALSA on Gentoo follows the same principle. There are two ways you can get ALSA support up and running on your system. We shall look at them in detail in the next chapter.

Installing ALSA
Options Galore The methods shown in this guide are mutually exclusive. You cannot have ALSA compiled in your kernel and try to use media-sound/alsa-driver. It will fail.

The first method of getting ALSA support running on Gentoo is to compile ALSA into your kernel. The second method (recommended for genkernel users as well) is to use media-sound/alsa-driver provided by Gentoo.

The main difference between using alsa-driver and ALSA that comes with the kernel is that alsa-driver is generally more up to date than the version in the kernel. Although this does not make any huge difference as such, people are encouraged to use alsa-driver since it is the latest.

Before you proceed...

Whichever method of install you choose, you need to know what drivers your soundcard uses. lspci will help us in finding out the same. Please emerge sys-apps/pciutils to get lspci, if you don't already have it installed. We now proceed to find out details about the soundcard.

# lspci -v | grep -i audio
0000:00:0a.0 Multimedia audio controller: Creative Labs SB Live! EMU10k1 (rev 06)

We now know that the soundcard on the machine is a Sound Blaster Live! and the card manufacturer is Creative Labs. Head over to the ALSA Soundcard Matrix page and select Creative Labs from the drop down menu. You will be taken to the Creative Labs matrix page where you can see that the SB Live! uses the emu10k1 module. That is the information we need for now. If you are interested in detailed information, you can click on the link next to the "Details" and that will take you to the emu10k1 specific page.

Method 1 : For the kernel lovers

If you're a person who likes to keep things simple like I do, then this is the way to go.

Since the 2005.0 release, Gentoo Linux uses 2.6 as the default kernel. Unless you are specifically using the 2.4 profile, gentoo-sources will be a 2.6 kernel on most architectures. Please check that your kernel is a 2.6 series kernel. This method will NOT work on a 2.4 kernel.

Emerge the kernel sources of your choice. Here we'll use the gentoo-sources For more information on the various kernel sources available on Portage, please look up the Gentoo Linux Kernel Guide.

# emerge gentoo-sources

Now, lets get on with the task of configuring the kernel.

# cd /usr/src/linux
# make menuconfig
The above example assumes that /usr/src/linux symlink points to the kernel sources you want to use. Please ensure the same before proceeding.

Now we will look at some of the options we will have to enable in the 2.6 kernel to ensure proper ALSA support for our soundcard

Please note that for the sake of ease, all examples have the options compiled into the kernel. If you would like to have a modular kernel, ensure that you note down the various modules and adjust your config files accordingly. Also if you decide to modularize ALSA, please do NOT skip the Configuration section.
Device Drivers  --->
   Sound  --->
   
(This needs to be enabled)
<*> Sound card support

(Make sure OSS is disabled)
Open Sound System   --->
   < > Open Sound System (DEPRECATED)

(Move one step back and enter ALSA)
Advanced Linux Sound Architecture  --->
   <*> Advanced Linux Sound Architecture
   (Select this if you want MIDI sequencing and routing)
   <*> Sequencer support
   (Old style /dev/mixer* and /dev/dsp* support. Recommended.)
   <*> OSS Mixer API
   <*> OSS PCM (digital audio) API 

(You now have a choice of devices to enable support for. Generally,
you will have one type of device and not more. If you have more than one 
soundcard, please enable them all here.)

(Mostly for testing and development purposes, not needed for normal 
users unless you know what you are doing.)
Generic devices  --->
   
(For ISA Sound cards)
ISA devices   --->
(IF you had the Gravis, you would select this option)
   <*> Gravis UltraSound Extreme

(Move one level back and into PCI devices. Most soundcards today are 
PCI devices)
PCI devices   --->
   (We now select the emu10k1 driver for our card)
   <*> Emu10k1 (SB Live!, Audigy, E-mu APS)
   (Or an Intel card would be)
   <*> Intel/SiS/nVidia/AMD/ALi AC97 Controller
   (Or if you have a VIA Card)
   <*> VIA 82C686A/B, 8233/8235 AC97 Controller

(Move one level back and select in case you have an USB sound card)
USB Devices   --->

Now that your options are set, you can (re)compile the kernel and ALSA support for your card should be functional once you reboot into the new kernel. You can now proceed to ALSA Utils and see if everything is working as it should.

Method 2 : ALSA Driver

So you've decided to go the alsa-driver way. Let's get started then. There are a few minor things to be done to ensure only the drivers for your soundcard are compiled. Although this is not really necessary, it cuts down on the unecessary drivers that will be compiled otherwise.

If you don't have an idea of what drivers your soundcard might need, please take a look at this section of the guide. Once you have your driver name (emu10k1 in our example), edit /etc/make.conf and add a variable, ALSA_CARDS.

(For one soundcard)
ALSA_CARDS="emu10k1"
(For more than one, seperate names with spaces)
ALSA_CARDS="emu10k1 via82xx"
genkernel users may safely skip the below checks.

If you have compiled your kernel and want to use alsa-driver, please ensure the following before proceeding, else alsa-driver is likely to fail. Code Listing 2.6 gives you one way of performing the checks.

  1. CONFIG_SOUND is set. (Basic Sound support enabled)
  2. CONFIG_SOUND_PRIME is not set. (In-built OSS support disabled)
  3. CONFIG_SND is not set. (In-built ALSA support disabled)
  4. /usr/src/linux points to the kernel you want ALSA working on.
(Assuming the linux symlink points to the correct kernel)
# cd /usr/src/linux
# cat .config | grep SOUND
(1. is true)
CONFIG_SOUND=y
(2. is true)
CONFIG_SOUND_PRIME is not set
# cat .config | grep SND
(and 3. is true)
CONFIG_SND is not set

Now all you have to do is type the magic words...and no, its not abracadabra.

# emerge alsa-driver
Please note that you will have to run emerge alsa-driver after every kernel (re)compile, as the earlier drivers are deleted.
Configuring/Testing ALSA
ALSA Utilities

alsa-utils forms an integral part of ALSA as it has a truckload of programs that are highly useful, including the ALSA Initscripts. Hence we strongly recommend that you install alsa-utils

# emerge alsa-utils

If you followed Method 1, please proceed to the ALSA Initscript section. The rest of you need to configure ALSA. This is made very easy by the existence of the alsaconf tool provided by alsa-utils.

Configuration Please shut down any programs that might access the soundcard while running alsaconf.

The easiest way to configure your soundcard is to run alsaconf. Just type alsaconf in a shell as root.

# alsaconf

You will now see a neat menu guided interface that will automatically probe your devices and try to find out your soundcard. Once that's done, it will inform you of the same and ask you permission to automatically make required changes to /etc/modules.d/alsa. It will then ask you to check your volume settings as it automatically mutes them. We will do this in the following sections. Once alsaconf exits successfully, you can continue by setting up ALSA's Initscript.

ALSA Initscript

We're now almost all setup. Whichever method you chose to install ALSA, you'll need to have something load your modules or initialize ALSA and restore your volume settings when you system comes up. The ALSA Initscript handles all of this for you and is called alsasound Add it to the boot run-level.

# rc-update add alsasound boot
 * alsasound added to runlevel boot
 * rc-update complete.

Next, just check the /etc/conf.d/alsasound file and ensure that SAVE_ON_STOP variable is set to yes. This saves your sound settings when you shutdown your system.

Volume Check!

We've completed all the setups and pre-requisites, so let's fire up ALSA.

# /etc/init.d/alsasound start
  * Loading ALSA modules ... 
  * Restoring Mixer Levels ... [ok]

Now that the required things have been take care of, we shall jack up the volume as it is muted by default. We use alsamixer for this purpose.

(Opens up a console program. Only required settings are shown)
# alsamixer

This is how the ALSA Mixer might look the first time you open it. Pay attention to the Master and PCM channels which both have an MM below them. That means they are muted. If you try to play anything with alsamixer in this state, you will not hear anything on your speakers.

Now, we shall unmute the channels, and set volume levels as needed.

Both Master AND PCM need to be unmuted and set to audible volume levels if you want to hear some output on your speakers.
  • To move between channels, use your left and right arrow keys. (<- & ->)
  • To toggle mute, move to the specific channel, for example Master and press the m key on the keyboard.
  • To increase and decrease the volume levels, use the up and down arrow keys respectively.
Be careful when setting your Bass and Treble values. 50 is usually a good number for both. Extrememly high values of Bass may cause jarring on speakers that are not designed to handle them.

After you're all done, your ALSA Mixer should look similar to the one below. Note the 00 instead of the MM and also the volume levels for some optimum settings.

Sound Check!

Finally. Some music. If everything above is perfect, you should be able to now listen to some good music. A quick way to test is to use a command line tool like media-sound/madplay. You could aslo use something more well known like mpg123 or xmms. As always, emerge what you need.

(Get any, or get all)
# emerge madplay
# emerge mpg123
# emerge xmms

And then play your favorite soundtrack...

# madplay -v /mnt/shyam/Music/Paul\ Oakenfold\ -\ Dread\ Rock.mp3
MPEG Audio Decoder 0.15.2 (beta) - Copyright (C) 2000-2004 Robert Leslie et al.
          Title: Dread Rock
         Artist: Paul Oakenfold
          Album: Matrix Reloaded
           Year: 2003
          Genre: Soundtrack
                 Soundtrack
 00:04:19 Layer III, 160 kbps, 44100 Hz, joint stereo (MS), no CRC

# mpg123 /mnt/shyam/Music/Paul\ Oakenfold\ -\ Dread\ Rock.mp3
High Performance MPEG 1.0/2.0/2.5 Audio Player for Layer 1, 2 and 3.
Version 0.59s-r9 (2000/Oct/27). Written and copyrights by Michael Hipp.
Uses code from various people. See 'README' for more!
THIS SOFTWARE COMES WITH ABSOLUTELY NO WARRANTY! USE AT YOUR OWN RISK!
Title  : Dread Rock                      Artist: Paul Oakenfold
Album  : Matrix Reloaded                 Year  : 2003
Comment:                                 Genre : Soundtrack

Directory: /mnt/shyam/Music/
Playing MPEG stream from Paul Oakenfold - Dread Rock.mp3 ...
Found new ID3 Header
MPEG 1.0 layer III, 160 kbit/s, 44100 Hz joint-stereo
Issues?

If for some reason you're unable to hear sound, the first thing to do would be to check your alsamixer settings. 80% of the issues lie with muted channels or low volume. Also check up your Window Manager's sound applet and verify that volumes are set to audible levels.

/proc is your friend. And in this case, /proc/asound is your best friend. We shall just take a short look at how much info is made available to us there.

(First and foremost, if /proc/asound/cards shows your card, ALSA has 
picked up your sound card fine.)
# cat /proc/asound/cards
0 [Live           ]: EMU10K1 - Sound Blaster Live!
                     Sound Blaster Live! (rev.6, serial:0x80271102) at 0xb800, irq 11

(If you run ALSA off the kernel like I do and wonder how far behind 
you are from alsa-driver, this displays current running ALSA version)
# cat /proc/asound/version
Advanced Linux Sound Architecture Driver Version 1.0.8 (Thu Jan 13 09:39:32 2005 UTC).

(ALSA OSS emulation details)
# cat /proc/asound/oss/sndstat
Sound Driver:3.8.1a-980706 (ALSA v1.0.8 emulation code)
Kernel: Linux airwolf.zion 2.6.11ac1 #2 Wed May 4 00:35:08 IST 2005 i686
Config options: 0

Installed drivers:
Type 10: ALSA emulation

Card config:
Sound Blaster Live! (rev.6, serial:0x80271102) at 0xb800, irq 11

Audio devices:
0: EMU10K1 (DUPLEX)

Synth devices: NOT ENABLED IN CONFIG

Midi devices:
0: EMU10K1 MPU-401 (UART)

Timers:
7: system timer

Mixers:
0: SigmaTel STAC9721/23
Other things ALSA
Setting up MIDI support

If your soundcard is one of those that come with onboard MIDI synthesizers and you would like to listen to some .mid files, you have to install awesfx which is basically a set of utilities for controlling the AWE32 driver. We need to install it first. If you don't have a hardware synthesizer, you can use a virtual one. Please see the section on Virtual Synthesizers for more information.

# emerge awesfx
You will need to copy over SoundFont (SF2) files from your soundcard's driver CD or a Windows installation into /usr/share/sounds/sf2/. For example a soundfont file for the Creative SBLive! card would be 8MBGMSFX.SF2.

After copying over the Soundfont files, we can then play a midi file as shown. You can also add the asfxload command to /etc/conf.d/local.start, so that the soundfont is loaded everytime the system starts up.

/mnt paths mentioned in the code listing(s) below will NOT be the same in your machine. They are just an example. Please be careful to change the path to suit your machine.
(First, copy the Soundfont)
# cp /mnt/win2k/Program\ Files/CreativeSBLive2k/SFBank/8MBGMSFX.SF2 /usr/share/sounds/sf2/
(We load the specific Soundfont)
# asfxload /usr/share/sounds/sf2/8MBGMSFX.SF2

You can now play midi files using a program like aplaymidi. Run aplaymidi -l to get a list of available ports and then pick one to play the file on.

(Check open ports)
# aplaymidi -l
 Port    Client name                      Port name
 64:0    EMU10K1 MPU-401 (UART)           EMU10K1 MPU-401 (UART)
 65:0    Emu10k1 WaveTable                Emu10k1 Port 0
 65:1    Emu10k1 WaveTable                Emu10k1 Port 1
 65:2    Emu10k1 WaveTable                Emu10k1 Port 2
 65:3    Emu10k1 WaveTable                Emu10k1 Port 3
(Pick a port, and play a mid file)
#  aplaymidi --port=65:0 /mnt/shyam/music/midi/mi2.mid
Virtual Synthesizers

If your soundcard lacks a hardware synthesizer, you could use a virtual one like timidity++. Installation is a breeze.

# emerge timidity++

For timidity to play sounds, it needs a soundfont. If you do not have any, install timidity-eawpatches or timidity-shompatches which will give you some soundfonts. You can have multiple soundfont configurations installed, and you can place your own in /usr/share/timidity/. To switch between different timidity configurations, you should use the timidity-update tool provided in the timidity++ package.

# emerge timidity-eawpatches
# timidity-update -g -s eawpatches

(or)

# emerge timidity-shompatches
# timidity-update -g -s shompatches

Don't forget to add timidity to the default runlevel.

# rc-update add timidity default
# /etc/init.d/timidity start

You can now try out Playing MIDI files.

Tools and Firmware

Some specific sound cards can benefit from certain tools provided by the alsa-tools and alsa-firmware packages. If you need alsa-tools, be sure to define the ALSA_TOOLS variable in /etc/make.conf with the tools you require. For instance:

ALSA_TOOLS="as10k1 ac3dec"

If the ALSA_TOOLS variable is not set, all available tools will be built. Now, install the alsa-tools (and/or alsa-firmware) package(s):

# emerge alsa-tools
A Big thank you to...

Everyone who contributed to the earlier version of the Gentoo ALSA Guide: Vincent Verleye, Grant Goodyear, Arcady Genkin, Jeremy Huddleston, John P. Davis, Sven Vermeulen, Benny Chuang, Tiemo Kieft and Erwin.

References
  • The ALSA Project
  • Linux Sound/MIDI Software