<?xml version='1.0' encoding="UTF-8"?>
<!-- $Header: /var/cvsroot/gentoo/xml/htdocs/doc/en/alsa-guide.xml,v 1.21 2003/11/23 16:30:50 swift Exp $ -->
<!DOCTYPE guide SYSTEM "/dtd/guide.dtd">

<guide link = "/doc/en/alsa-guide.xml">
<title>Gentoo Linux ALSA Guide</title>
<author title="Author">
  <mail link="zu@pandora.be">Vincent Verleye</mail>
</author>
<author title="Author">
  <mail link="g2boojum@gentoo.org">Grant Goodyear</mail>
</author>
<author title="Author">
  <mail link="agenkin@gentoo.org">Arcady Genkin</mail>
</author>
<author title="Editor"><!-- zhen@gentoo.org -->
	John P. Davis
</author>
<author title="Editor">
  <mail link="swift@gentoo.org">Sven Vermeulen</mail>
</author>
<author title="Editor">
  <mail link="bennyc@gentoo.org">Benny Chuang</mail>
</author>
<author title="Editor">
  <mail link="blubber@gentoo.org">Tiemo Kieft</mail>
</author>

<license/>

<abstract>
This guide will show you how to set up the Advanced Linux Sound Architecture 
(ALSA) on Gentoo Linux. In addition to the Gentoo Linux Desktop Configuration 
Guide, this guide is supposed to give you more information on this subject.
</abstract>

<version>1.3.4</version>
<date>November 23, 2003</date>

<chapter>
<title>Introduction</title>
<section>
<title>What is ALSA?</title>
<body>

<p>
The Advanced Linux Sound Architecture (ALSA) is a project to improve the Linux 
sound subsystem by rewriting large chunks of it. It is anticipated that ALSA 
will make it into Linux kernel 2.6.x (or 3.x.x -- whichever comes first) as it 
becomes ready.
</p>

<p>
ALSA provides audio and MIDI functionality for Linux.
</p>
 
<p>
Quoted from <uri>http://www.alsa-project.org</uri>, ALSA has the following 
features:
</p>

<ul>
  <li>
    Efficient support for all types of audio interfaces, from consumer 
    soundcards to professional multichannel audio interfaces
  </li>
  <li>Fully modularized sound drivers</li>
  <li>SMP and thread-safe design</li>
  <li>
    User space library (alsa-lib) to simplify application programming and 
    provide higher level functionality
  </li>
  <li>
    Support for the older OSS API, providing binary compatibility for most OSS 
    programs
  </li> 
</ul>

<p>
There's lots more to ALSA however, like support for Full Duplex playback and 
recording, multiple soundcard support, hardware mixing of streams, extensive 
mixer capabilities (to support advanced features of new soundcards), ...	
</p>

</body>
</section>
<section>
<title>Why use ALSA?</title>
<body>

<p>
If your soundcard is supported by the Linux kernel sound system or the 
commercial OSS/4Front sound driver system, which can be found in all 2.4.x 
Linux kernels, you could just aswell build <e>those modules</e> for use with 
your soundcard. If you want this, just read through the <uri 
link="http://www.tldp.org/HOWTO/Sound-HOWTO/index.html">Linux Sound HOWTO</uri>.
</p> 

<p>
However, those OSS/4Front drivers have some limitations -- being commercial is 
one. ALSA is an attempt to go beyond these limitations and to do so in an open 
source fashion. ALSA is a fully GPL and LGPL'ed sound driver system, that 
provides a professional quality system for recording, playback, and MIDI 
sequencing.
</p> 

</body>
</section>
<section>
<title>What cards does ALSA support?</title>
<body>

<p>
ALSA tries to support as many (new) cards as possible by providing open-source 
drivers. However, some vendors may provide binary-only packages. 
</p>

<p>
To know if your card is supported, you can find a Soundcard Matrix of supported 
and not-supported cards here: <uri>http://www.alsa-project.org/alsa-doc/</uri>.
</p>

</body>
</section>
</chapter>

<chapter>
<title>Installation</title>
<section>
<title>Gentoo USE flags</title>
<body>

<p>
To compile programs with ALSA-support, be sure to add <e>alsa</e> to
your USE-variable. However, several tools don't support alsa yet, and
require OSS. ALSA provides OSS-emulation if you define <e>oss</e> in your 
USE-variable before you start.
</p>

</body>
</section>
<section>
<title>Kernel modules</title>
<body>

<p>
Since we're still using 2.4.x kernel sources, we'll have to compile kernel 
modules and ALSA modules separately. People who are using a 2.5.x kernel can 
do this from within their kernel configuration, since the ALSA modules are 
included in the kernel sources and should be built there.
</p>

<p>
First we'll make sure that our kernel configuration is ready for use with ALSA. 
All you need in your kernel configuration is having Sound Card Support set to 
be built as a module (M). This will build <path>soundcore.o</path>.
</p>

<note>
Possibly, this will also work when you built Sound Card Support in the kernel 
(Y) instead of building it as a module (M). However, the official ALSA 
documentation suggests building it as a module, since ALSA will try loading it.
</note>

<p>
If you already have a working kernel configuration, make sure you remove all 
sound drivers (except for Sound Card Support). If you wish to do this without 
having to reboot, you could do like this:
</p>

<pre caption="Kernel configuration">
# <i>cd /usr/src/linux</i>
# <i>cp .config ~/</i>
# <i>make mrproper</i>
# <i>cp ~/.config .</i>
# <i>make menuconfig</i>
</pre>

<p>
Now select <e>Sound Card Support</e> as Module (M) and deselect all other sound 
drivers. Exit and say Y to save your kernel configuration. After that, build 
the modules:
</p>

<pre caption="Kernel compilation">
# <i>make dep clean</i>
# <i>make modules modules_install</i>
</pre>

<p>
Before installing your new modules, this last line will delete all your previous
modules, even the ones from a previous ALSA installation.
</p>

<impo>
This means, whenever you recompile your kernel later on, you MUST recompile 
<c>alsa-driver</c>.
</impo>

</body>
</section>
<section>
<title>ALSA modules</title>
<body>

<p>
Now it's time to install the ALSA drivers for your soundcard(s). If your 
soundcard is PCI, you can find out the name and type of your soundcard by 
looking at the output of <path>/proc/pci</path>.
</p>

<pre caption="Search for soundcard information">
# <i>grep audio /proc/pci</i>
</pre>

<warn>
If you had a previous sound setup and there are still non-ALSA sound modules 
loaded, unload them <e>now</e>. Check with <c>lsmod</c> and use <c>rmmod</c> 
to unload all sound-related modules on your system.
</warn>

<p>
We could simply do an <c>emerge alsa-driver</c> now, this would compile and 
install <e>all</e> ALSA sound drivers.
</p>

<p>
However, to save some time, lookup the <e>Module Name</e> of your soundcard(s) 
on the <uri link="http://www.alsa-project.org/alsa-doc">ALSA Soundcard 
Matrix</uri> by following the <e>Details</e> link in the <e>Driver and Docs</e> 
column in the row of the chipset of your soundcard. Mine is <c>snd-emu10k1</c>, 
since I have an SBlive! soundcard, with the <e>EMU10K1</e> chipset. We'll set 
ALSA_CARDS environment to the value of the module name before emerging (but 
without the snd prefix), so emerge will only compile the drivers we need.
</p>

<pre caption="Compile correct modules">
# <i>env ALSA_CARDS='emu10k1' emerge alsa-driver</i> 
</pre>

<note>
You can also add this value in <path>/etc/make.conf</path>, so when you have 
to emerge the alsa-driver later on you can just run <c>emerge alsa-driver</c>.
For example, like this: <c>echo 'ALSA_CARDS="emu10k1"' >> /etc/make.conf</c>
</note>

<note>
When you want to install ALSA drivers for more than one soundcard, you could
set ALSA_CARDS to a space-separated list of drivers; like this: <c>env 
ALSA_CARDS='emu10k1 intel8x0 ens1370' emerge alsa-driver</c>
</note>

<note>
If you want to have OSS compatibility, make sure to emerge <i>alsa-oss</i>, it 
is the ALSA/OSS compatibility wrapper.
</note>

<note>
If you plan on recompiling your kernels numerous times, it might
be adviseable to emerge <c>alsa-driver</c> with <c>--buildpkg</c>. This
will create a binary package for it. Later, after recompiling your kernel, 
you can just do <c>emerge --usepkg alsa-driver</c> which will install the 
binary package instead of recompiling it completely.
</note>

<p>
After this, the ALSA modules should be installed on your system.
</p>

</body>
</section>
<section>
<title>Configuration of ALSA</title>
<body>

<p>
Let's start configuring now to get ALSA working properly. We'll need to edit 
some files, to let our system know about the freshly installed ALSA modules.
</p>

<p>
First, install <c>alsa-utils</c> on your system:
</p>

<pre caption = "Emerging alsa-utils">
# <i>emerge alsa-utils</i>
</pre>

<p>
Hereafter, we need to edit <path>/etc/modules.d/alsa</path>.
</p>

<warn>
There is no need to edit <path>/etc/modules.conf</path>. Instead, always edit 
files in <path>/etc/modules.d</path>.
</warn>

<p>
Check the ALSA portion <e>at the bottom of the file</e>. By tweaking this line 
you can specify the max number of soundcards you have (generally, just one).
</p>

<pre caption="At the bottom of /etc/modules.d/alsa">
<comment>Set this to the correct number of cards.</comment>
options snd cards_limit=1
</pre>

<p>
Now we'll specify the sounddriver(s) ALSA should use. In the same file, edit 
like this:
</p>

<pre caption="In /etc/modules.d/alsa">
## and then run `modules-update' command.
## Read alsa-driver's INSTALL file in /usr/share/doc for more info.
##
##  ALSA portion
alias snd-card-0 snd-emu10k1
<comment>## If you have more than one, add:</comment>
## alias snd-card-1 snd-intel8x0
## alias snd-card-2 snd-ens1370
##  OSS/Free portion
## alias sound-slot-0 snd-card-0
## alias sound-slot-1 snd-card-1
##
</pre>

<note>
If you have more than one soundcard, adjust the <c>cards_limit</c> value and add
more snd-card aliases to the file. I don't have experience with this, but you 
can find examples for configurations with two or more soundcards in <uri 
link="http://www.alsa-project.org/alsa-doc/alsa-howto/c1660.htm">Chapter 6</uri>
of the <uri 
link="http://www.alsa-project.org/alsa-doc/alsa-howto/alsa-howto.html">ALSA 
Howto</uri>.
</note>

<p>
Last thing to do in this file, almost at the end, check if these lines are there
and uncommented:
</p>

<pre caption="Near the end of /etc/modules.d/alsa">
alias /dev/mixer snd-mixer-oss
alias /dev/dsp snd-pcm-oss
alias /dev/midi snd-seq-oss
</pre>

<p>
Now double-check the file <path>/etc/modules.d/alsa</path> and when you're sure 
everyting is ok, run <c>modules-update</c>.
</p>

<pre caption="Running modules-update">
# <i>modules-update</i>
</pre>

<note>
Running <c>modules-update</c> here will insert the data from 
<path>/etc/modules.d/alsa</path> into <path>/etc/modules.conf</path>
</note>

<p>
You should also verify that <path>/etc/devfsd.conf</path> has the alsa devices 
and permissions correctly registered.
</p>

<pre caption="/etc/devfsd.conf">
# ALSA/OSS stuff
# Comment/change these if you want to change the permissions on
# the audio devices
LOOKUP          snd          MODLOAD ACTION snd
LOOKUP          dsp          MODLOAD
LOOKUP          mixer        MODLOAD
LOOKUP          midi         MODLOAD
REGISTER        sound/.*     PERMISSIONS root.audio 660
REGISTER        snd/.*       PERMISSIONS root.audio 660
</pre>

<note>
Notice that devfsd.conf sets <path>/dev/sound</path> permissions to be 
root.audio. Thus, for non-root users to use audio they will have to be part of 
the audio group.
</note>

</body>
</section>
</chapter>

<chapter>
<title>Starting ALSA</title>
<section>
<title>Adding alsasound to a runlevel</title>
<body>

<p>
First thing to do now, is to make ALSA startup at boottime. Like this:
</p>

<pre caption="Add ALSA to the default runlevel">
# <i>rc-update add alsasound boot</i>
</pre>

<warn>
Note that the alsasound script should be added to the "boot" runlevel, not the 
"default" runlevel.
</warn>

</body>
</section>
<section>
<title>Running and unmuting</title>
<body>

<p>
Since we're Linux users, we don't want to reboot. So we'll start the alsasound 
script manually.
</p>

<pre caption="Starting ALSA">
# <i>/etc/init.d/alsasound start</i>
</pre>

<p>
ALSA is running now. If everything is ok, you should be able to see the ALSA 
modules loaded when running <c>lsmod</c>. However, sound won't work yet, 
because the channels are still muted. We need <c>amixer</c> for this.
</p>

<pre caption = "Running amixer">
# <i>amixer</i>
</pre>

<warn>
You shouldn't get this, but <e>if</e> you get an error about "amixer: Mixer 
attach default error: No such file or directory", you should manually modprobe
<c>snd-mixer-oss</c> and <c>snd-pcm-oss</c> once. After that run amixer again.
</warn>

<pre caption="Only if you get an error when running amixer">
# <i>modprobe snd-mixer-oss</i>
# <i>modprobe snd-pcm-oss</i>
# <i>amixer</i>
</pre>

<p>
If you got this far, now unmute Master and PCM channels. Some hardware
even requires you to unmute the center channel or even the surround
channel.
</p>

<pre caption = "Unmuting channels">
# <i>amixer set Master 100 unmute</i>
# <i>amixer set PCM 100 unmute</i>
<comment>Only if the above doesn't succeed on its own:</comment>
# <i>amixer set Center 100 unmute</i>
# <i>amixer set Surround 100 unmute</i>
<comment>Test the sound:</comment>
# <i>aplay $KDEDIR/share/sounds/pop.wav</i> <codenote>(pop.wav is part of KDE)</codenote>
</pre>

<p>
We check to see if sound is working by using the aplay (alsa play) command. If 
you hear a pop, then sound is indeed working. Then, adjust the volume settings 
to your liking; the ncurses-based <c>alsamixer</c> is a great way to get them 
"just so". 
</p>

<p>
You may want to emerge <c>alsa-xmms</c> as that will provide ALSA support for 
XMMS.
</p>

<p>
When you reboot your system, the <c>alsasound</c> init script will properly saveand restore your volume settings. 
</p>

</body>
</section>
</chapter>

<chapter>
<title>Final Notes</title>
<section>
<title>After kernel-upgrades..</title>
<body>

<p>
When you ever rebuild your kernel, or upgrade to another kernel, you'll have to 
rebuild the ALSA modules.
</p>

<p>
Although you might have installed <c>alsa-driver</c>, <c>alsa-libs</c> and 
<c>alsa-utils</c>, only the first will have to be installed again, since it will
put the alsa modules in <path>/lib/modules/*/kernel/sound/pci/</path>.
</p>

<pre caption="needed after each kernel compile">
# <i>emerge alsa-driver</i>
</pre>

</body>
</section>
<section>
<title>/etc/modules.autoload</title>
<body>

<p>
You won't have to edit this file for use with ALSA. After our <c>rc-update add 
alsasound boot</c>, our system will load the correct modules at startup.
</p>

<p>
It's not necessary to add <c>snd-pcm-oss</c> or <c>snd-mixer-oss</c> in this 
file. Check the <uri link="http://www.djcj.org/LAU/guide/alsbook/faq1.html">this
FAQ</uri> for more info.
</p>

</body>
</section>
<section>
<title>Known bugs</title>
<body>

<note>
This guide lags behind on the alsa-development. Chances are these bugs
are already fixed when you read this.
</note>

<ul>
  <li>
    If you have <b>lots of noise</b> when using <b>oss</b> emulation, add
    <e>options snd-pcm-oss dsp_map=1</e> to <path>/etc/modules.d/alsa</path>
  </li>
</ul>

</body>
</section>
<section>
<title>Activating Joystick Support</title>
<body>

<p>
If your soundcard has a joystick plug, you might be interested in activating
joystick support for your soundcard. If so, start by verifying if your soundcard
driver has a joystick parameter. You can verify this by running <c>modinfo</c>
against your <path>snd-&lt;your chipset&gt;</path>. For instance, for the
<c>snd-via82xx</c>:
</p>

<pre caption="Running modinfo">
# <i>modinfo snd-via82xx</i>
filename:    /lib/modules/2.4.22-ck2/snd-via82xx.o
description: "VIA VT82xx audio"
author:      "Jaroslav Kysela &lt;perex@suse.cz&gt;"
license:     "GPL"
parm:        index int array (min = 1, max = 8), description "Index value for VIA 82xx bridge."
parm:        id string array (min = 1, max = 8), description "ID string for VIA 82xx bridge."
parm:        enable int array (min = 1, max = 8), description "Enable audio part of VIA 82xx bridge."
parm:        mpu_port long array (min = 1, max = 8), description "MPU-401 port. (VT82C686x only)"
<i>parm:        joystick int array (min = 1, max = 8), description "Enable joystick. (VT82C686x only)"</i>
parm:        ac97_clock int array (min = 1, max = 8), description "AC'97 codec clock (default 48000Hz)."
parm:        dxs_support int array (min = 1, max = 8), description "Support for DXS channels 
             (0 = auto, 1 = enable, 2 = disable, 3 = 48k only, 4 = no VRA)"
</pre>

<p>
If it has the <c>joystick</c> parameter, then append <c>joystick=1</c> to your
<c>options</c> line in <path>/etc/modules.d/alsa</path>. For instance, for the
<c>snd-via82xx</c>:
</p>

<pre caption="Adding the joystick parameter">
alias snd-card-0 snd-via82xx
options snd-via82xx joystick=1
</pre>

</body>
</section>
<section>
<title>More links..</title>
<body>

<p>
You could check these for additional info:
</p>

<ul>
  <li>
    <uri link="http://www.gentoo.org/doc/en/desktop.xml">The Gentoo Linux 
    Desktop Configuration Guide</uri>
  </li>
  <li>
    <uri link="http://www.alsa-project.org">ALSA Project Homepage</uri>
  </li>
  <li>
    <uri link="http://www.alsa-project.org/documentation.php3">ALSA Users 
    Documentation</uri>
  </li>
  <li>
    <uri link="http://www.djcj.org">ALSA Howto's and FAQ's</uri>
  </li>
  <li>
    <uri link="http://tldp.org/HOWTO/Sound-HOWTO/index.html">Linux Sound 
    HOWTO</uri>
  </li>
  <li>
    <uri link="http://linux-sound.org/">Sound and MIDI Software For Linux</uri>
  </li>
</ul>

</body>
</section>
</chapter>
</guide>
