# TinyALSA

Source: [https://docs.qualcomm.com/doc/80-70014-16/topic/tinyalsa_apis.html](https://docs.qualcomm.com/doc/80-70014-16/topic/tinyalsa_apis.html)

TinyALSA is a library that wraps ALSA kernel interface into APIs that clients can
        invoke and provides a plug-in interface to emulate ALSA APIs.

TinyALSA source code can be found at:

build-qcom-wayland/workspace/sources/tinyalsa/opensource/tinyalsa
            and

build-qcom-wayland/workspace/sources/tinyalsa/opensource/tinycompress.

Some of the frequently used TinyALSA APIs are described below. See the [open source TinyALSA documentation](https://github.com/tinyalsa/tinyalsa/blob/master/include/tinyalsa/pcm.h) for a
            complete description of all APIs.

## pcm\_open

Used to open a PCM audio device for input and output
                 operations. Initializes a PCM device for communication, allowing
                 subsequent read/write operations in audio data.

    struct pcm *pcm_open(
         unsigned int card, 
         unsigned int device, 
         unsigned int flags, 
         struct pcm_config *config)Copy to clipboard

**Parameters**

| card | Specifies the card number |
| --- | --- |
| device | Specifies the device number in the chosen card |
| flags | Flags to configure various aspects of pcm device |
| config | Structure variable used to specify audio stream<br>                                parameters |

**Return value**

`pcm* handle`

## pcm\_is\_ready

Used to check if the PCM device is ready for input/output operations.

    int pcm_is_ready(struct pcm *pcm)
    Copy to clipboard

**Parameters**

| pcm | Pointer to opened PCM device |
| --- | --- |

**Return value**
- 0 on success
- Error code on failure

## pcm\_prepare

Prepares audio device input and output operations.

    int pcm_prepare(
         struct pcm *pcm)
    Copy to clipboard

**Parameters**

| pcm | Pointer to opened PCM device |
| --- | --- |

**Return value**
- 0 on success
- Error code on failure

## pcm\_start

Used to start the PCM audio device for input and output operations.

    int pcm_start(
         struct pcm *pcm)Copy to clipboard

**Parameters**

| pcm | Pointer to the PCM device that has been opened |
| --- | --- |

**Return value**
- 0 on success
- Error code on failure

## pcm\_write

Used to write audio data to the audio PCM device. Takes audio data as input and sends
                it to the PCM device for playback or processing.

    int pcm_write(
         struct pcm *pcm, 
         const void *data, 
         unsigned int count)Copy to clipboard

**Parameters**

| pcm | Pointer to PCM device that has been opened |
| --- | --- |
| data | Audio data to be written |
| count | Number of audio frames to be written |

**Return value**
- 0 on success
- Error code on failure

## pcm\_read

Retrieves audio data from the PCM device, which allows the application to capture
                audio data from a microphone.

    int pcm_read(
         struct pcm *pcm, 
         void *data, 
         unsigned int count)Copy to clipboard

**Parameters**

| pcm | Pointer to the PCM device that has been opened |
| --- | --- |
| data | Audio data to be read |
| count | Number of audio frames to be read |

**Return value**
- 0 on success
- Error code on failure

## pcm\_stop

Used to stop the PCM audio device from further input and output operations.

    int pcm_stop(
         struct pcm *pcm)Copy to clipboard

**Parameters**

| pcm | Pointer to the PCM device that has been opened |
| --- | --- |

**Return value**
- 0 on success
- Error code on failure

## pcm\_close

Used to close the PCM audio device. This releases the resources associated with PCM
                device and releases the memory.

    int pcm_close(
         struct pcm *pcm)Copy to clipboard

**Parameters**

| pcm | Pointer to the PCM device that has been opened |
| --- | --- |

**Return value**
- 0 on success
- Error code on failure

## TinyALSA-based applications

To realize an audio use case from TinyALSA, configure virtual mixer controls. These
                controls, created by the mixer plug-in, set up audio use case graphs and modules.
                Most are byte array-based and configured using the
                    `mixer_ctl_set_array` API. Metadata (referred to as ‘PCM100
                metadata’) is set using key-value (KV) pairs. For implementation details, check the
                    `set_agm_audio_intf_metadata` API in
                        *`build-qcom-wayland/workspace/sources/agm/opensource/agm/plugins/tinyalsa/test/agmmixer.c`*

    /**
     * Key Vector pair
     */
    struct agm_key_value {
        uint32_t key; /**< key */
        uint32_t value; /**< value */
    };
    /*Sample allocation for the key value pair*/
    gkv = calloc(num_gkv, sizeof(struct agm_key_value));
    ckv = calloc(num_ckv, sizeof(struct agm_key_value));
    Copy to clipboard

Follow this set up sequence to implement and execute an audio use case. Sample code
                for the TinyALSA-based agmplay and agmcap utilities can be found at:

build-qcom-wayland/workspace/sources/agm/opensource/agm/plugins/tinyalsa/test.
1. Set the audio interface (back-end) device configuration including sample
                        rate, channels, format, and data
                        format.

        'CODEC_DMA-LPAIF_WSA-RX-0 rate ch fmt' 48000 2 2(PCM_16)Copy to clipboard
2. Set metadata including graph keys, cal keys for the device, and
                        DevicePP.

        'CODEC_DMA-LPAIF_WSA-RX-0 metadata' bytesCopy to clipboard
3. Set the control to indicate that follow-on mixer configurations will set the
                        metadata for the stream and StreamPP subgraphs. Zero indicates that
                        subsequent commands are for the
                        stream.

        'PCM100 control' Zero
        'PCM100 metadata' bytes
        Copy to clipboard
4. Set the control to indicate that follow on mixer configurations will set the
                        metadata for the DevicePP and stream-device subgraphs.
                            `Codec_DMC-LPAIF_WSA-RX-0` indicates that the subsequent
                        commands are for stream-device. `CODEC_DMA-LPAIF_WSA-RX-0` is
                        one of the audio interfaces registered with the ALSA ASOC framework. A list
                        of all audio interfaces can be found at
                        /proc/asound/pcm.

        'PCM100 control' CODEC_DMA-LPAIF_WSA-RX-0 
        'PCM100 metadata' bytes
        Copy to clipboard
5. Retrieve all tags, module ID, and instance IDs associated with a given
                        session between the stream and audio
                        interface.

        'PCM100 getTaggedInfo' bytesCopy to clipboard
6. Set the control to indicate that follow on mixer configurations will set the
                        parameters for modules on the stream
                        subgraph.

        'PCM100 control' Zero 
        'PCM100 setParam' bytes
        Copy to clipboard
7. Set the control to indicate that follow on mixer configurations will set the
                        parameters for modules on the StreamDevice and DevicePP
                        subgraphs.

        'PCM100 control' CODEC_DMA-LPAIF_WSA-RX-0
        'PCM100 setParam' bytes 
        Copy to clipboard
8. Connect the front-end (stream) with the back-end (codec/audio
                        interface).

        'PCM100 connect'CODEC_DMA-LPAIF_WSA-RX-0Copy to clipboard
9. Initialize the PCM device for
                        communication.

        pcm_openCopy to clipboard
10. Prepare the audio device for input and output
                        operations.

        pcm_prepareCopy to clipboard
11. Start the PCM audio device for input/output
                        operations.

        pcm_startCopy to clipboard
12. Write/read audio data from the audio PCM
                        device.

        pcm_write/pcm_readCopy to clipboard
13. Stop the PCM device from further input/output
                        operations.

        pcm_stopCopy to clipboard
14. Close the PCM audio device.

        pcm_closeCopy to clipboard

All mixer controls for a virtual device can be fetched using the following
                commands.

    ssh root@ip-addr
    systemctl stop pulseaudio
    tinymix set -D 100
    Copy to clipboard

## TinyALSA level playback and record testing

**Record**

1. To verify record functionality using
                    TinyALSA:

        ssh root@ip-addr
        systemctl stop pulseaudio
        tinymix set 'VA DMIC MUX0' DMIC0
        tinymix set 'VA_AIF1_CAP Mixer DEC0' 1
        tinymix set 'VA_DEC0 Volume' 100
        agmcap /opt/test.wav -D 100 -d 101 -c 1 -r 48000 -b 16 -i CODEC_DMA-LPAIF_VA-TX-0 -dkv 0xA3000004 -skv 0xB1000009 -dppkv 0 -ikv 0 -T 10
        Copy to clipboard
2. To pull the recorded file, run the following command on the host
                    machine:

        scp root@[ip-addr]:/data/test.wavCopy to clipboard

**Playback**

1. To perform playback, first push the .wav file to the device using the following
                    commands:

        scp test.wav root@[ip-addr]:/data/Copy to clipboard
2. Run the following commands to initiate
                    playback:

        ssh root@ip-addr 
        mount -o remount,rw /
        systemctl stop pulseaudio
        scp test.wav root@[ip-addr]:/data/
        ssh root@ip-addr
        tinymix set 'SpkrLeft PA Volume' 20
        tinymix set 'WSA RX0 MUX' AIF1_PB
        tinymix set 'WSA_RX0 INP0' RX0
        tinymix set 'WSA_COMP1 Switch' 1
        tinymix set 'SpkrLeft WSA MODE' 0
        tinymix set 'SpkrLeft COMP Switch' 1
        tinymix set 'SpkrLeft BOOST Switch' 1
        tinymix set 'SpkrLeft DAC Switch' 1
        tinymix set 'SpkrLeft VISENSE Switch' 0
        tinymix set 'WSA_RX0 Digital Volume'  85
        tinymix set 'SpkrRight WSA MODE' 0
        tinymix set 'SpkrRight PA Volume' 20
        tinymix set 'WSA RX1 MUX' AIF1_PB
        tinymix set 'WSA_RX1 INP0' RX1
        tinymix set 'WSA_COMP2 Switch' 1
        tinymix set 'SpkrRight COMP Switch' 1
        tinymix set 'SpkrRight BOOST Switch' 1
        tinymix set 'SpkrRight DAC Switch' 1
        tinymix set 'SpkrRight VISENSE Switch' 0
        tinymix set 'WSA_RX1 Digital Volume'  85
        agmplay /opt/test.wav -D 100 -d 100 -i CODEC_DMA-LPAIF_WSA-RX-0
        Copy to clipboard

**Parent Topic:** [APIs and Sample Apps](https://docs.qualcomm.com/doc/80-70014-16/topic/apis_and_sample_apps.html)

Last Published: Jul 15, 2024

[Previous Topic
Platform Adaptation Layer (PAL)](https://docs.qualcomm.com/bundle/publicresource/80-70014-16/topics/platform_adaptation_layer_pal.md) [Next Topic
Software](https://docs.qualcomm.com/bundle/publicresource/80-70014-16/topics/software.md)