[Audio application] Linux ALSA audio application programming

Above: [Audio driver] Linux ALSA sound card, WAV file related concepts

ALSA audio application programming for Linux

Use alsa-libs and alsa-utils to realize the playback and recording of .wav format files, and understand the application layer device nodes of the sound card in Linux. Introduces the application programming steps using alsa-libs.

1. ALSA architecture

The Advanced Linux Sound Architecture (ALSA) provides audio and MIDI functionality for the Linux operating system.

ALSA has the following salient features:

  • Efficiently supports all types of audio interfaces, from consumer sound cards to professional multi-channel audio interfaces.

  • Fully modular sound drivers.

  • SMP and thread safe design.

  • Userspace library (alsa-lib) to simplify application programming and provide advanced functionality.

  • Support for the old Open Sound System (OSS) API, providing binary compatibility for most OSS programs.

The ALSA system includes 7 sub-projects:

  • Driver package alsa-driver

  • Development package alsa-libs

  • Development package plug-in alsa-libplugins

  • Set the management tools package alsa-utils

  • The OSS interface is compatible with the simulation layer tool alsa-oss

  • Special audio firmware support package alsa-finnware

  • Other sound-related small package alsa-tools

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-tVm5tcyU-1682084270719)(image/Linux ALSA sound card application driver/1681615020116.png)]

​ alsa-libs is a set of C language function libraries in the Linux application layer, which provides a set of unified and standard
interfaces for audio application development. The application program only needs to call this set of APIs to complete the control of the underlying sound card device.

Two, alsa-libs porting

First, you need to download alsa-lib and alsa-utils from the official website http://www.alsa-project.org on the official website of ALSA.

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-K9o1Fqhz-1682084270721)(image/Linux ALSA sound card application driver/1681615443264.png)]

Specific steps reference:

Transplanting alsa-lib and alsa-utils_Transplanting alsa-utils library

Three, ALSA device file structure

/dev/snd


The sound device registered in the Linux kernel device driver layer and based on the ALSA audio driver framework will generate the corresponding device node file in the /dev/snd directory .

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-BWnihaaV-1682084270721)(image/Linux ALSA sound card application driver/1681616247600.png)]

We can see the following device files:

  • controlC0: used for sound card control, such as channel selection, mixing, microphone control, etc.
  • midiC0D0 : for playing midi audio
  • pcmC0D0c : pcm device for recording
  • pcmC0D0p : pcm device for playback
  • pcmC0D1p : pcm device for playback
  • seq : sequencer
  • timer: timer

C0D0 represents device 0 in sound card 0, the last c of pcmC0D0c represents capture, and the last p of pcmC0D0p represents
playback. These are the naming rules in alsa-driver. As can be seen from the above list, there are 7 devices connected to my sound card. According to the actual capabilities of the sound card, the driver can actually connect more types of devices. In include/sound/core.h, the following devices are defined type

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-bQ2NTLKT-1682084270722)(image/Linux ALSA sound card application driver/1681616672104.png)]

Usually, we are more concerned about the two devices pcm and control.

/proc/asound

In the /proc/asound directory of the Linux system, there are many files, which record information related to the sound card in the system

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-zP8ZdmXX-1682084270723)(image/Linux ALSA sound card application driver/1681617091453.png)]

  • cards : View the content of the cards file, which can list the available and registered sound cards in the system

  • devices : List all sound card registered devices in the system,

  • pcm : list all PCM devices in the system

  • timers : Timer device selection

  • version : ALSA driver version

  • card0: Records information related to sound card 0, such as the name of the sound card and the PCM device registered by the sound card

Four, alsa-utils testing tool

Alsa-utils test tool

  • apply

  • arecord

  • alsamixer

  • amixer

  • alsactl

  • alsaloop

aplay
aplay is a program for testing audio playback function, you can use aplay to play audio files in wav format.

Common commands:

aplay xxx.wav 

arecord

arecord tool is an application for recording test

Common commands:

arecord -r 20000 -f S16_LE -c 1 -d 10 -D hw:0,1 6.wav

-r indicates the sampling frequency (minimum 8000);

-f indicates the sampling format, here refers to 16 bit little endian (generally S16_LE, S32_LE);

-c indicates the number of channels (1 or 2);

-d indicates recording time;

-D indicates which recording device to use (hw:0,1: hw plug-in, the first parameter indicates the sound card number, and the second parameter indicates the device number);

The higher the sampling frequency, the clearer the sound and the larger the .wav file

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-HplWZx9E-1682084270724)(image/Linux ALSA sound card application driver/1682049469753.png)]

alsaloop

alsaloop is used for loopback testing, playing while recording.

Common commands:

alsaloop -C hw:0,1 -t 100

5. Write ALSA applications

Written based on alsa-lib

API and examples refer to the official website ALSA project - the C library reference: Index, Preamble and License (alsa-project.org)

1. Turn on the PCM device

int snd_pcm_open(snd_pcm_t **pcmp, const char *name, snd_pcm_stream_t stream, int mode)

2. Set hardware parameters

Instantiate snd_pcm_hw_params_t object

int snd_pcm_hw_params_malloc(snd_pcm_hw_params_t **ptr);

Initialize snd_pcm_hw_params_t object

int snd_pcm_hw_params_any(snd_pcm_t *pcm, snd_pcm_hw_params_t *params);

Set the access access type

int snd_pcm_hw_params_set_access(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t _access);

set data format

int snd_pcm_hw_params_set_format(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t val);

Set the number of channels

int snd_pcm_hw_params_set_channels(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val);

Set the sample rate size

int snd_pcm_hw_params_set_rate(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val, int dir);

set period size

int snd_pcm_hw_params_set_period_size(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t val, int dir);

Set the buffer size

int snd_pcm_hw_params_set_buffer_size(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t val);

int snd_pcm_hw_params_set_periods(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,unsigned int val,int dir)

Install/load hardware configuration parameters

int snd_pcm_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t *params);

3. Read/write data

snd_pcm_sframes_t snd_pcm_writei(snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size);

snd_pcm_sframes_t snd_pcm_readi(snd_pcm_t *pcm, void *buffer, snd_pcm_uframes_t size);

4. Release resources

int snd_pcm_close(snd_pcm_t *pcm);

void snd_pcm_hw_params_free(snd_pcm_hw_params_t *obj)

5. Asynchronous interface

int snd_async_add_pcm_handler(snd_async_handler_t **handler, snd_pcm_t *pcm, 
			      snd_async_callback_t callback, void *private_data);

int snd_pcm_poll_descriptors(snd_pcm_t *pcm,struct pollfd *pfds,unsigned int space);

References

Punctual atomic documentation
ALSA project - the C library reference: Index, Preamble and License (alsa-project.org)

Guess you like

Origin blog.csdn.net/m0_61737429/article/details/130297466