Tinyalsa/Alsa Reference


1. Tinyalsa


1.1. Overview

Tinyalsa = Tiny + ALSA(Advanced Linux Sound Architecture)

At present, the mainstream audio architecture in Linux is ALSA. Tinyalsa is a simplified ALSA, which encapsulates the kernel's ALSA interface and is used to simplify ALSA programming in user space.


1.2. Test with Tinyalsa's Tool

  1. Tinyalsa source code

    Download: https://github.com/tinyalsa/tinyalsa/releases

    There are tinymix.c, tinycminfo.c, tinycap.c, tinyplay.c in utils.

    Enter the top-level directory to modify the makefile, and specify the cross-compilation tool chain and compiler.

    export ARCH = arm
    
    export CROSS_COMPILE = arm-linux-gnueabihf-sigmastar-9.1.0-
    
    export CC = $(CROSS_COMPILE)gcc
    

    Execute make clean; make in the top-level directory to generate a bin file in utils for the next test.

  2. Configure menuconfig kernel and open mi_alsa module switch in SDK

    Note: The drivers required by Tinyalsa are soundcore.ko, snd.ko, snd-timer.ko, snd-pcm.ko and mi_alsa.ko

    • Configure the kernel

      The configuration of the audio framework requires the support of Power Manager.

      Power management options  --->
      
          [*] De[*] Device power management core functionality  /* Need to replace the kernel */
      

      Execute make menuconfig to configure options (compiled into modules):

      Device Drivers  --->    
          <M> Sound card support  --->
              /* The next level directory will not choose to generate soundcore.ko and snd.ko by default. */
              <M>   Advanced Linux Sound Architecture  --->  
                  /* Generate snd-pcm.ko, snd-pcm-oss.ko */
                  <M>   OSS PCM (digital audio) API
                  /* Generate snd-timer.ko */
                  [*]   [*]   PCM timer interface
      

      Execute make modules; ls modules to view the compiled ko.

    • open mi_alsa module switch in SDK

      Execute make menuconfig in directory project.

      Sdk Config  --->
          Interface Compile Config  --->  
              [*] alsa
      

      Execute make alsa in directory interface to view the generated mi_alsa.ko.

    • Load all drivers

      Pay attention to the order of loading modules.

      insmod  soundcore.ko
      insmod  snd.ko
      insmod  snd-timer.ko   
      insmod  snd-pcm.ko    
      insmod  mi_alsa.ko
      
  3. Use tinyplay, tinycap, tinymix and tinycminfo to test

    tinyplay is a playing test: ./tinyplay ./1_17/nearend.wav

    tinycap is a recording test: ./tinycap cap_out.wav

    tinymix set and get volume: ./tinymix contents, ./tintmix set 1 60, ./tinymix get 1

    tinypcminfo check pcm channel information: ./tinypcminfo -D 0


2. Alsa


2.1. Overview

ALSA is short for Advanced Linux Sound Architecture, which provides alsa-driver in kernel device driver layer and alsa-lib in application layer. The application calls the API (libasound.so) provided by alsa-lib to complete the control of the underlying audio hardware. alsa-utils is an application of alsa, which can be used for recording and playback testing.

The ALSA API can be broken down into the following main interfaces:

  1. Control interface

    It provides general functions for managing sound card registration and requesting available devices.

  2. PCM interface

    It manages the interface of digital audio playback and capture.

  3. Raw MIDI interface

    It supports MIDI (Musical Instrument Digital Interface) and is a standard electronic musical instrument.

  4. Timer interface

    为同步音频事件提供对声卡上时间处理硬件的访问。

  5. Sequencer interface

  6. Mixer interface


2.2. Alsa-lib and Alsa-utils Porting

  1. Preparation

    alsa-lib version: alsa-lib-1.2.4.tar.bz2

    Lick: http://www.linuxfromscratch.org/blfs/view/svn/multimedia/alsa-lib.html

    alsa-util version: alsa-utils-1.2.4.tar.bz2

    Link: http://www.linuxfromscratch.org/blfs/view/stable/multimedia/alsa-utils.html

    arm platform: ipc_Pudding demo board.

  2. Cross compile and install

    Download the source code and unzip it, enter the source root directory: tar -xvf alsa-lib-1.2.4.tar.bz2

    Define the output path of compiled alsa:

    export ALSA_INSTALL_PATH=/customer/alsa-1.2.4/alsa_build
    
    export CC=arm-linux-gnueabihf-gcc
    
    export CXX=arm-linux-gnueabihf-g++
    
    • alsa-lib

      1. Configure

        ./configure --prefix=$ALSA_INSTALL_PATH/alsa/ --host=arm-linux --disable-aload --disable-rawmidi --disable-seq --disable-ucm --disable-alisp --disable-old-symbols --disable-python --enable-debug CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ --with-plugindir=$ALSA_INSTALL_PATH/alsa/lib --with configdir=$ALSA_INSTALL_PATH/alsa/etc
        
      2. Compile

        make
        
      3. Install

        make install
        

      Important config items:

      1. --host, specify the cross compiler.

      2. --prefix, specify the installation path of the compiled file, and then the installation command will create lib and include in this directory.

      3. --with-configdir, specify the installation directory of conf, the alsa.conf will be directly ported to the target system.

      4. --with-alsa-devdir, specify the directory of audio device files. Such as /dev/snd

    • alsa-utils

      1. Configure

        ./configure --prefix=$ALSA_INSTALL_PATH/alsa-utils --host=arm-linux --with-curses=ncurses CC="arm-linux-gnueabihf-gcc -lasound -I$ALSA_INSTALL_PATH/alsa/include -L$ALSA_INSTALL_PATH/alsa/lib" --disable-xmlto --disable-alsamixer
        
      2. Compile

        make
        

        Note: Add --disable-xmlto and --disable-nls in the configure step if it is failed.

      3. Install

        make install
        

      Important config items:

      1. --host, specify the compiler.

      2. --prefix, specify the installation path of the compiled file, and then the installation command will create lib and include in this directory.

      3. --with-alsa-inc-prefix, specify the directory of the files compiled by alsa-lib

      4. --with-alsa-prefix, specify the lib directory of the files compiled by alsa-lib

      5. --disable-alsamixer alsamixer, a graphical amixer tool, garbled characters may appear, please disable it.

  3. Use the bin file in alsa-utils to test

    Copy the files in ALSA_INSTALL_PATH to the same path on the arm platform.

    In the lib and utils file system, the following files must be copied to the corresponding path of the target board:

    1. alsa-lib library files

      cp /customer/alsa-1.2.4/alsa_build/alsa/lib/lib* path of the target board(/Lib or other paths, the other paths need to add dynamic libraries to search)

      Add dynamic library to search the path by environment variable LD_LIBRARY_PATH: export LD_LIBRARY_PATH=/customer/alsa

      -1.2.4/alsa_build/alsa/lib:$LD_LIBRARY_PATH

    2. alsa config file

      Copy the files in $ALSA_INSTALL_PATH/alsa/etc to the same path on the arm platform: cp -r /customer/alsa-1.2.4/alsa_build/alsa/etc path of the target board(/customer/alsa-1.2.4/alsa_build/alsa/)

      Copy the bin file aplay generated in aplay path after alsa-utils is compiled to the arm platform.

      Note: In this version, aplay is arecord. Modify aplay to arecord to complete the recording test!

      Playing test: ./aplay ../../1_17/nearend.wav

      Recording test: ./arecord -Dhw:0,0 -r8000 -f S16_LE -c 2 ./1.wav

      -l List sound cards and digital audio devices

      -D Specify audio device PCM

      -r Specify sample rate

      -f Specify sampling format

      -c Mono, stereo

      -h help