How to record audio

Revision as of 13:46, 23 September 2019 by Registered User

1. Article purpose[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 source]

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

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

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 source]

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

3.1.1. Record from ALSA device[edit source]

Launch 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 PulseAudio[edit source]

  • Configure Pulseaudio :

For ecosystem release ≥ v1.1.0 More info.png:
Change Pulseaudio default input source to 'analog_input':

Info white.png Information
The Pulseaudio analog_input is defined in system.pa configuration file.
 pacmd set-default-source analog_input

For ecosystem release v1.0.0 More info.png:

Change Pulseaudio active profile of the sound card, to 'analog-stereo' profile:

Info white.png Information
The Pulseaudio analog-stereo profile is the default profile. So next command is not required after boot.
Info white.png Information
Example below is given for sound card index 0. Check sound cards index with "pactl list cards short" command.
 pacmd set-card-profile 0 output:analog-stereo+input:analog-stereo
  • Launch audio record :
 arecord -d 10 /tmp/rec.wav

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

Warning white.png Warning
The support of digital microphone input is board dependent. Please, check available inputs with "arecord -l" command.

3.2.1. Record from ALSA device[edit source]

Launch mono audio record from 'record_dfsdm0' ALSA device:

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

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

Launch stereo audio record from 'multi' ALSA device:

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

3.2.3. Record via PulseAudio[edit source]

  • Configure Pulseaudio

Change Pulseaudio default input source to 'dmic1_input':

Info white.png Information
The Pulseaudio dmic1_input is defined in system.pa configuration file.
 pacmd set-default-source dmic1_input
  • Launch audio record :
 arecord -r 48000 -f S32_LE -c 2 -d 10 /tmp/rec.wav

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

Warning white.png Warning
The support of S/PDIF input is board dependent. Please, check available inputs with "arecord -l" command.

3.3.1. Record from ALSA device[edit source]

Launch 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 launching the record command. The record rate must be set according to S/PDIF signal sampling rate.
 arecord -D record_spdif -f S32_LE -c 2 -r 48000 -d 10 /tmp/rec.wav

3.3.2. Record via PulseAudio[edit source]

  • Configure Pulseaudio

Change Pulseaudio default input source to 'iec958_input':

Info white.png Information
The Pulseaudio iec958_input is defined in system.pa configuration file.
 pacmd set-default-source iec958_input
  • Launch audio record :
 arecord -r 48000 -f S32_LE -c 2 -d 10 /tmp/rec.wav

4. References[edit source]