SSD_Tinyalsa/Alsa Instructions


1. Tinyalsa


1.1. Introduction

Tinyalsa = Tiny + ALSA (Advanced Linux Sound Architecture)

The current mainstream audio architecture in linux is ALSA, tinyalsa is a simplified version of ALSA, tinyalsa is a lightweight library that encapsulates the ALSA interface of the kernel, and is used to simplify ALSA programming in user space.


1.2. Test with Tinyalsa's own tools

1.2.1. Tinyalsa source code

Reference link: https://github.com/tinyalsa/tinyalsa/tree/1.1.1. Due to the tinyalsa-2.0.0 version, pcm_state will be done before each write data, causing the data of the alsa runtime to be modified, resulting in abnormal playback, and this version is prohibited.

Reference link: https://github.com/tinyalsa/tinyalsa/releases

You can see that the directory structure utils contains tinymix.c, tinypcminfo.c, tinycap.c, tinyplay.c.

Enter the top-level directory of the source package, modify the makefile, and specify the cross-compilation toolchain and compiler, such as:

export ARCH = arm

export CROSS_COMPILE = arm-linux-gnueabihf-sigmastar-9.1.0-

export CC = $(CROSS_COMPILE)gcc

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


1.2.2. menuconfig kernel configuration and turn on the mi_alsa module switch under sdk

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

  • Kernel configuration

    The configuration of the audio framework requires Power Manager support. The configuration is as follows:

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

    The make menuconfig are configured as follows (compiled into modules):

    Device Drivers  --->    
        <M> Sound card support  --->
            /* If the next level directory is not selected, soundcore.ko and snd.ko will be generated by default */
            <M>   Advanced Linux Sound Architecture  --->  
                /* It will generate snd-pcm.koļ¼Œsnd-pcm-oss.ko */
                <M>   OSS PCM (digital audio) API
                /* It will generate snd-timer.ko */
                [*]   [*]   PCM timer interface
    

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

  • Enable mi_alsa module under the sdk:

    Enter the project directory, make menuconfig, configure as follows:

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

    In the interface directory make alsa, you can view the generated mi_alsa.ko in the corresponding directory

  • Load all drivers

    Note: Pay attention to the order of loading modules through the insmod method

    insmod soundcore.ko
    insmod snd.ko
    insmod snd-timer.ko
    insmod snd-pcm.ko
    insmod mi_alsa.ko
    

1.2.3. Test with bin file

  • tinyplay playback test

    ./tinyplay ./1_17/nearend.wav
    
  • tinycap recording test

    ./tinycap cap_out.wav
    
    -D card //sound card, mi_alsa only supports sound card 0
    -d device //device, same as mi_audio device_id number
    -c channels //number of channels
    -r rate //sample rate
    -b bits //bit width
    -p period_size //number of frames per interrupt
    -n n_periods //number of periods
    
  • tinymix set and get volume

    ./tinymix contents
    
    ./tintmix set 1 60
    
    ./tinymix get 1
    
  • tinypcminfo view the relevant information of the pcm channel

    ./tinypcminfo -D 0
    

2. Alsa


2.1. Introduction

The full name of ALSA is Advanced Linux Sound Architecture. In the kernel device driver layer, ALSA provides alsa-driver (alsa driver); in the application layer, ALSA provides alsa-lib (API function called by the application), the application only needs to call the API provided by alsa-lib ( libasound.so), you can complete the control of the underlying audio hardware, ALSA has become the mainstream audio architecture of Linux. alsa-utils is a collection of tools and functions (that is, alsa applications), which can be used for recording and playback testing.

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

  1. Control interface

    Provides common functionality for managing sound card registration and requesting available devices

  2. PCM interface

    Interface for managing digital audio playback and capture

  3. Raw MIDI interface

    Supports MIDI (Musical Instrument Digital Interface), a standard electronic musical instrument.

  4. Timer interface

    Provides access to the timing hardware on the sound card for synchronizing audio events.

  5. Sequencer interface

  6. Mixer interface


2.2. alsa-lib and alsa-utils porting

2.2.1. Preparations


2.2.2. Cross compilation and installation

After downloading the source code and decompressing it, enter the source code root directory: tar -xvf alsa-lib-1.2.4.tar.bz2

Define the output path for compiling 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 the compiled files

      make install
      

    Important configuration items:

    1. --host specifies the compiler, here it is a cross compiler.

    2. --prefix specifies the installation path of the compiled files, and subsequent installation commands will create lib and include in this directory.

    3. --with-configdir specifies the installation directory of the conf file, the most useful alsa.conf in this directory, this file will be ported directly to your target system

    4. --with-alsa-devdir specifies the directory for audio device files. e.g. in /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: If compilation fails, add options --disable-xmlto and --disable-nls when configuring

    3. Install the compiled files

      make install
      

    Important configuration items:

    1. --host specifies the compiler, which is the same as the lib configuration option

    2. --prefix specifies the installation path of the compiled file, which is the same as the configuration option of lib

    3. --with-alsa-inc-prefix is used to specify the include directory of files compiled by alsa-lib

    4. --with-alsa-prefix is used to specify the lib directory of the files compiled by alsa-lib

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


2.2.3. Use the bin file test under alsa-utils

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

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

    • Library files for alsa-lib

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

      Add the dynamic library search path through the environment variable LD_LIBRARY_PATH: export LD_LIBRARY_PATH=/customer/alsa -1.2.4/alsa_build/alsa/lib:$LD_LIBRARY_PATH

    • alsa configuration file

      Copy the files (alsa.conf, cards, pcm) under $ALSA_INSTALL_PATH/alsa/etc to the exact same path of the target board: cp -r /customer/alsa-1.2.4/alsa_build/alsa/etc target board path (/ customer/alsa-1.2.4/alsa_build/alsa/)

  2. Copy the bin file aplay generated under the aplay path after compiling alsa-utils to the arm platform

    Note: In this version, aplay and arecord are one file, just modify aplay to arecord to complete the recording test!

  3. Playing test

    ./aplay ../../1_17/nearend.wav
    
  4. Recording test

    ./arecord -Dhw:0,0 -r8000 -f S16_LE -c 2 ./1.wav
    
    -l List sound cards and digital audio devices
    -D specifies the audio device PCM
    -r specifies the sample rate
    -f specifies the sampling format
    -c mono, stereo
    -h help
    

3. Alsa/Tinyalsa usage restrictions

3.1. AO(Playback)

  • Some versions of Tinyalsa restrict gain from setting negative values, so map AO gain [-60, 30] to [0, 90], such as: -60→0, 30→90.

  • If mi_alsa is used, Lineout does not support sampling rates of 12k and 24k (limited by alsa itself); supported sampling rates: 8k, 11.025k, 16k, 22.05k, 32k, 44.1k, 48k; Lineout supports 1chn mono or stereo.

  • The upper layer of Alsa/Tinyalsa cannot download I2S parameters temporarily: eWorkmode, bSyncClock, eFmt, eMclk, which will be limited when using I2S.


3.2. AI(Capture)

  • The default gain is 0, you need to set gain before using AI; sampling rate: 8k, 16k, 32k, 48k.

  • AI also only supports recording 1chn mono or stereo, such as Amic, Dmic, etc.; the use of I2S will also be limited.


3.3. MI_ALSA

  • At present, mi_alsa only supports some simple recording and playback functions. It is recommended that customers use the API interface directly. If customers need to use alsa, please explain the usage restrictions of alsa to the customer (for example, the alsa-lib plug-in does not currently support, etc.).

  • Subsequent plans will make major changes to the mi_alsa architecture, and will try to be compatible and support more scenarios and functions.