Last edited 6 months ago

How to record audio

Applicable for STM32MP13x lines, STM32MP15x lines, STM32MP25x lines

1. Article purpose[edit | edit source]

This article explains how to record audio via the kernel ALSA audio framework in Linux® OS context. The examples below, show how to record audio from the different audio hardware interfaces of the STM32MPU boards.

2. Audio record overview[edit | edit source]

The ALSA framework exposes audio devices associated to the board audio hardware interfaces.

Warning white.png Warning
The support of audio input interfaces is board dependent. Please, check available inputs using the "arecord -l" command.

The application audio streams are routed by default through the Pipewire[1] sound server. Pipewire exposes audio profiles, which are mapped on the ALSA sound card audio devices. The Pipewire server provides a command line interface to list audio profiles and to select one, in order to record from a specific audio interface.

Info white.png Information
In this article, the PulseAudio 'pactl' command is used in the examples. This is possible because the PipeWire sound server maintains backward compatibility with the former PulseAudio sound server through the pipewire-pulse package.
Warning white.png Warning
The applications that interact with the Weston framework have to be launched as "Weston" user instead of "root" user. The Pulseaudio commands, for instance, can be launched as follows:
su -l "weston" -c "pactl info"

The audio record examples in following sections are based on ALSA utilities. Some input paths are configured through ALSA controls. These configurations are detailed in sound card configuration article. If an error is issued when running an example, please refer to Audio troubleshooting grid article for debug.

3. Examples[edit | edit source]

3.1. Audio record from headset microphone input[edit | edit source]

3.1.1. Record from ALSA device[edit | edit source]

Start audio record from 'record_codec' ALSA device:

Info white.png Information
'record_codec' is an alias defined in /etc/asound.conf, for headset microphone input device.
 arecord -D record_codec -f S16_LE -d 10 /tmp/rec.wav

3.1.2. Record via Pipewire[edit | edit source]

  • Configure Pipewire :

Change Pulseaudio default input source to 'analog_input':

Info white.png Information
The Pulseaudio analog_input is defined in /etc/pipewire/pipewire-pulse.conf.d/system.pa.conf configuration file.
 pactl set-default-source analog_input
  • Start audio record :
Info white.png Information
The Pipewire device is the default one, so "-D pipewire" option can be omitted in the record command.
 arecord -d 10 /tmp/rec.wav

3.2. Audio record from digital microphone input[edit | edit source]

3.2.1. Record from ALSA device[edit | edit source]

Start mono audio record from 'record_dmic0' ALSA device:

Info white.png Information
'record_dmic0' is an alias defined in /etc/asound.conf, for digital microphone U1 input device.
 arecord -D record_dmic0 --rate 16000 --format S32_LE --channels 1 -d 10 /tmp/rec.wav

3.2.2. Record multiple digital microphones from a virtual ALSA device[edit | edit source]

Start stereo audio record from 'multi' ALSA device:

Info white.png Information
'multi' device, based on ALSA multi plugin[2], has to be defined in /etc/asound.conf file.
 arecord -D multi --rate 16000 --format S32_LE --channels 2 -d 10 /tmp/rec.wav

3.2.3. Record via Pipewire[edit | edit source]

  • Configure Pipewire

Change Pulseaudio default input source to 'dmic1_input':

Info white.png Information
The Pulseaudio dmic1_input is defined in /etc/pipewire/pipewire-pulse.conf.d/system.pa.conf configuration file.
 pactl set-default-source dmic1_input
  • Start audio record :
Info white.png Information
The Pipewire device is the default one, so "-D pipewire" option can be omitted in the record command.
 arecord -r 48000 -f S32_LE -c 2 -d 10 /tmp/rec.wav

3.3. Audio record from S/PDIF input[edit | edit source]

3.3.1. Get IEC958 status bits[edit | edit source]

Some restrictions may apply to IEC958 control, depending on the SPDIFRX device configuration as it is explained in SPDIFRX device tree configuration and SPDIFRX Linux driver articles.

Info white.png Information
The example below is given for sound card index 0. Check sound card index using the "aplay -l" command.
 amixer -c 0 cget iface=PCM,name='IEC958 Capture Default'

3.3.2. Record from ALSA device[edit | edit source]

Start audio record from 'record_spdif' ALSA device:

Info white.png Information
'record_spdif' is an alias defined in /etc/asound.conf, for S/PDIF input device.
Info white.png Information
A S/PDIF signal must be available on S/PDIF RCA input connector before starting the record command. The record rate must be set according to S/PDIF signal sampling rate.
 arecord -D record_spdif --rate 48000 --format S32_LE --channels 2 -d 10 /tmp/rec.wav

3.3.3. Record via Pipewire[edit | edit source]

  • Configure Pipewire

Change Pulseaudio default input source to 'iec958_input':

Info white.png Information
The Pulseaudio iec958_input is defined in /etc/pipewire/pipewire-pulse.conf.d/system.pa.conf configuration file.
 pactl set-default-source iec958_input
  • Start audio record :
Info white.png Information
The Pipewire device is the default one, so "-D pipewire" option can be omitted in the record command.
 arecord --rate 48000 --format S32_LE --channels 2 -d 10 /tmp/rec.wav

4. References[edit | edit source]