ALSA

From Gentoo Linux Wiki

Jump to: navigation, search
Gentoo.png
Gentoo has an official article on:

ALSA is a major sound system for Linux. It consists of several parts, the most important of which is the driver. The drivers are available as part of the kernel, or in the package media-sound/alsa-driver Typically the drivers in the portage tree are newer than those in the kernel, but if you use them, you will have to re-emerge them each time you rebuild your kernel.

Contents

[edit] Hardware Discovery

Use the lspci program(part of sys-apps/pciutils) to identify any sound cards on your system:

lspci -v | grep -i audio

We see here, as an example, that this computer is equipped with a Creative SoundBlaster Live!:

0000:00:0a.0 Multimedia audio controller: Creative Labs SB Live! EMU10k1 (rev 06)

The output states the type of device, brand, model, and potentially the necessary ALSA driver. To verify the driver name, go to the ALSA sound card matrix. This information will be to build the proper drivers in the kernel.

[edit] Kernel Configuration

Refer to the official Gentoo Kernel Configuration Guide on other matters surrounding the kernel and recompilation. Select the devices for which you wish to build drivers under the Generic devices, PCI devices and USB devices. Include them as modules:

Linux Kernel Configuration: Kernel Configuration
Loadable module support --->
    [*] Enable loadable module support
Device Drivers --->
    Character devices --->
        <*> Enhanced Real Time Clock Support
    Sound --->
        <M> Sound card Support
            <M> Advanced Linux Sound Architecture --->
                <M> Advanced Linux Sound Architecture
                <M> Sequencer support
                <M> Sequencer dummy client
                <M>   OSS Mixer API
                <M>   OSS PCM (digital audio) API
                [M]   OSS Sequencer API
                <*>   RTC Timer support
                [*]     Use RTC as default sequencer timer
                [ ]   Dynamic device file minor numbers
                [ ]   Support old ALSA API
                [ ]   Verbose procfs contents
                [ ]   Verbose printk
                [ ]   Debug
                Generic devices  --->
                PCI devices  --->
                USB devices  --->
                System on Chip audio support  --->
            < > Open Sound System (DEPRECATED)

(Recompile and (re)install your kernel.

[edit] Module Configuration

ALSA does not typically need user configuration on a system with only one sound card; all necessary modules should be loaded by udev automatically with dmix enabled. If sound does not function for some reason, or if you need to configure multiple sound cards, read on.

Note: After the modules have been loaded and initilaized, your mixer levels may be muted and/or set to zero, giving the appearance of a non-functional ALSA setup.

[edit] ALSA Userspace

You will need some userspace utilities for ALSA to function properly, these are provided with the media-sound/alsa-utils package. Note that you probably don't want to set the minimal on this package, as it will then only install the ALSA init scripts:

emerge -av media-sound/alsa-utils

When emerged it's a good idea to add alsasound to your boot runlevel(as advised by the post-emerge message):

rc-update add alsasound boot

Last but not least, edit /etc/conf.d/alsasound to your liking.

[edit] Plugins

media-libs/alsa-lib carries a special USE variable called ALSA_PCM_PLUGINS. These plugins take a raw sound stream and process it further, adding effects or filtering.

This feature is intended for embedded systems, so that the ALSA footprint can be reduced by installing only the necessary plugins. It is recommended that you leave all plugins on unless you know which plugins are required for your sound card or are willing to experiment with which plugins are optional. See the post from the developer of this feature.

For futher reading on ALSA's plugins, see the ALSA PCM Plugins section of the ALSA wiki.

[edit] Configuration

ALSA will for most single card users work out-of-the-box. If not, the following section might help.

[edit] alsaconf

The tool alsaconf(part of media-sound/alsa-utils) is probably the easiest way to configure your sound devices. It will try to detect your sound cards and then update v/etc/modprobe.d/alsa accordingly. So run as root:

alsaconf

[edit] Permissions

Users who want to have access to the sound card need to be in the audio group.

gpasswd -a <username> audio

Replace <username> with the user you wish to grant access. If you are adding your user to the group, remember that you must log out and back in again before the changes will take effect.

[edit] Mixer

Most window managers have a mixer which interfaces directly with ALSA. Alternatively you can use alsamixer. If you only have one sound card:

alsamixer

To unmute channels use the m key.

If multiple:

alsamixer -c (card number)

ALSA sound cards start with 0, so 0 is the first card, 1 is the second card, etc.

[edit] Troubleshooting

[edit] Default Sound Device

If you have several sound devices you might want to set one as the default. Start with finding the (ALSA-)name of the device you want to set using the aplay command with the --list(list devices) argument. Note that aplay -l output more than we need here, so we grep for cards:

aplay -l | grep card

In the following example we find a Intel card and a SoundBlaster Live! card:

card 0: Live [SB Live! 5.1], device 0: emu10k1 [ADC Capture/Standard PCM Playback]
card 0: Live [SB Live! 5.1], device 2: emu10k1 efx [Multichannel Capture/PT Playback]
card 0: Live [SB Live! 5.1], device 3: emu10k1 [Multichannel Playback]
card 2: nForce2 [NVidia nForce2], device 0: Intel ICH [NVidia nForce2]
card 2: nForce2 [NVidia nForce2], device 2: Intel ICH - IEC958 [NVidia nForce2 - IEC958]

Don't mind that cards appear more than once here, we are just after the names, being in this case, marked bold in the example above, Live and nForce2. Now open /etc/asound.conf and add:

File: /etc/asound.conf
pcm.!default { type hw card Live }
ctl.!default { type hw card Live }

This sets the PCM and mixer devices of the SoundBlaster Live! card as the defaults. You'll need to restart any applications using ALSA for playback for this to take effect.

[edit] MIDI for Soundblaster Live! and Audigy

This section assumes you have audio working using the emu10k1 alsa module. First you need to grab one or more soundfonts. They are suffixed with .sf2, and may be compressed using a special lossless compression tool. Soundfonts can conveniently be found, gzipped, at here.

Install awesfx, a tool to load the soundfont(s):

emerge -av awesfx

To load soundfonts, use the asfxload command:

asfxload /path/to/soundfont.sf2

If you have more than one, add additional sound fonts with the -N option:

asfxload -N /path/to/next/soundfont.sf2

To see what ports you've got for midi, run aplaymidi with the -l switch:

aplaymidi -l
 Port    Client name                      Port name
 20:0    SB Live! 5.1                     EMU10K1 MPU-401 (UART)
 21:0    Emu10k1 WaveTable                Emu10k1 Port 0        
 21:1    Emu10k1 WaveTable                Emu10k1 Port 1        
 21:2    Emu10k1 WaveTable                Emu10k1 Port 2        
 21:3    Emu10k1 WaveTable                Emu10k1 Port 3  

In this case the ports are 21:0 - 21:3. Theres also 20:0, but that is just a pass through to an external port that you probably don't have. Export these ports:

export ALSA_OUTPUT_PORTS="21:0 21:1 21:2 21:3"

Playing music should now work:

aplaymidi a_midi_file.mid

If it works as it should, all that's left is to automate the process. It would probably be best to make a new rc script in /etc/init.d/: Here are working examples of /etc/init.d/emu10k1midi and /etc/env.d/99emu10kmidi:

File: /etc/init.d/emu10k1midi
SOUNDFONT_DIR=/usr/share/soundfonts
depend() {
    need alsasound
    after modules alsasound
}

start() {
    ebegin "Starting MIDI services for SoundBlaster Live"
    if lsmod | grep emu10k1 > /dev/null; then
        einfo "Loading Sound Fonts in $FONT_DIR"
        find $SOUNDFONT_DIR -name '*.sf2' -print -exec asfxload -N {} \; ||\
            eerror "Unable to load Soundfont"
        einfo "Sound Font(s) loaded"
    else
        eerror "emu10k1 module not loaded. Unable to Load Soundfonts"
    fi
    eend 0
}

stop() {
    einfo "Unloading Sound Font samples"
    asfxload -i
    eend 0
}

restart() {
    svc_stop
    svc_start
}
File: /etc/env.d/99emu10kmidi
#!/bin/sh
ALSA_OUTPUT_PORTS=$(aplaymidi -l | awk '/Emu10k1 WaveTable/ {print $1}')
Personal tools