How-to configure alsa lib configuration file

From RidgeRun Developer Wiki

ALSA-lib Configuration Introduction

This document provides guidance on configuring ALSA-Lib by making adjustments to its configuration file.

In the realm of Linux operating systems, there exist two significant audio system APIs for playing and recording audio PCM: ALSA-Lib and PulseAudio. PulseAudio is included in the Freescale Ubuntu root file system release, whereas ALSA-Lib is the default audio system in the LTIB release (Linux Target Image Builder).

Alsa diagram
Alsa diagram

Architecture

ALSA-Lib offers a standardized set of APIs that simplifies application development. Simultaneously, it provides a flexible mechanism to fulfill various functionalities, such as resampling, channel remixing, and sound mixing from different applications, among others.

Alsa architecture
Alsa architecture

As depicted in the diagram above, ALSA plugins provide essential functionality, enabling customization throughout the entire audio pipeline.

The ALSA-Lib API acts as an ALSA device emulator, providing a name that callers can use to access it. The specific type of plugin represented by this name is determined by the configuration. For instance, "card0" serves as a pseudo ALSA device name with a "hw" type, representing the first actual ALSA device.

    pcm.card0 {

       type hw

       card 0

    }

Similarly, "plug" functions as a pseudo ALSA device name with a "plug" type, signifying an audio conversion processor. Furthermore, it can accept arguments from applications, enhancing its flexibility.

    pcm.plug {

        @args [ SLAVE ]

        @args.SLAVE {

            type string

        }

        type plug

        slave.pcm $SLAVE

    }

When we invoke "snd_pcm_open" within the application, we establish a data flow pipeline. This pipeline begins by potentially converting the source PCM data into a format compatible with sound card 0, if needed, using the "plug" plugin. Subsequently, the processed audio is played through sound card 0 via the "card0" plugin. In the above provided code snippet, the "slave.pcm" parameter serves as the pivotal link connecting various plugins.

For instance:

    snd_pcm_open(.., "plug:card0",..);

It's worth noting that the number of arguments can exceed one, as exemplified below:

    pcm.xxx {

        @args [ arg1 arg2 arg3 ]

        @args.arg1 { type string }

        @args.arg2 { type string }

        @args.arg3 { type string }

        ...

    }

These arguments may also have default values, and you can consult "/usr/share/alsa/alsa.conf" for reference. To pass these arguments, you can use the following syntax:

    snd_pcm_open(.., "xxx:arg1,arg2,arg3",..);

The name "xxx" allows you to trace the audio pipeline to its final plugin, which can have types such as "hw" (for the ALSA driver), "file" (for file-based operations), or other types like "pulse," "bluetooth," "network," "protocol stack," and more, depending on the specific use case and configurations.

The Configuration Files

In configuration file, we mainly define the fake alsa device name. The root configuration file is "/usr/share/alsa/alsa.conf", which will load additional configuration files which might overwrite previous name definition in the previously loaded file. The load sequence is:

  • 1. /usr/share/alsa/alsa.conf
  • 2. /usr/share/alsa/alsa.conf.d/*
  • 3. /etc/asound.conf for administrator
  • 4. $(HOME)/.asoundrc for certain user

In practice, alsa applications (e.g. aplay or speaker-test) are always using "default" as the fake device name, so that the most important thing to customize your own pipeline is to overwirte "default". For example:

    pcm.dmix_44100{

        type dmix

        ipc_key 5678293

        ipc_key_add_uid yes

        slave{

            pcm "hw:0,0"

            period_time 10000

            format S16_LE

            rate 44100

        }
    }
    pcm.!default{

        type plug

        route_policy "average"

        slave.pcm "tee:dmix_44100,/home/wayne/a.pcm"

    }


The "!" in "pcm.!default" means forcing overwrite. The pipeline defined above is as following figure:

Alsa diagram
Alsa diagram

The next example is the "default" definition on ubuntu root fs.

    pcm.!default {

        type pulse

        hint {

            show on

            description "Playback/recording through the PulseAudio sound server"
        }
    }

The only alsa plugin is "pulse", and the pipeline is as following:

Alsa diagram
Alsa diagram

There are a lot of alsa plugins developed, with various configuration parameters. Please refer to .asoundrc - ALSA wiki for more details.