Last edited 5 months ago

How to play audio



1. Article purpose

This article explains how to play audio through the ALSA audio framework in Linux® OS context. The examples below, show how to play audio on the different audio hardware interfaces of the STM32MPU boards.

2. Audio playback overview

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

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.

The audio playback examples in following sections are given for ALSA utilities. However, the audio playbacks can be launched by other applications, such as gstreamer multimedia framework.

3. Examples

3.1. Audio playback on headset output

3.1.1. Playback on ALSA device

Run audio playback on 'playback_codec' ALSA device:

aplay -D playback_codec /usr/share/sounds/alsa/Front_Left.wav

3.1.2. Playback via Pipewire

  • Configure Pipewire

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

pactl set-card-profile alsa_card.platform-sound output:analog-stereo
  • Run audio playback:
aplay /usr/share/sounds/alsa/Front_Left.wav

3.2. Audio playback on HDMI output

3.2.1. Playback on ALSA device

Run audio playback on 'playback_hdmi' ALSA device:

aplay -D playback_hdmi /usr/share/sounds/alsa/Front_Left.wav

3.2.2. Playback via Pipewire

  • Configure Pipewire

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

pactl set-card-profile alsa_card.platform-sound output:hdmi-stereo
  • Run audio playback:
aplay /usr/share/sounds/alsa/Front_Left.wav

3.3. Audio playback on S/PDIF output

3.3.1. Set IEC958 status bits

Example: set IEC958 mode bit.

iecset -c 0 -n device=0 pro on

3.3.2. Playback on ALSA device

Run audio playback on 'playback_spdif' ALSA device:

speaker-test -D playback_spdif --channels 2 --format S32_LE -f 440 -t sine -l 1

3.3.3. Playback via Pipewire

  • Configure Pipewire

Change the sound card active profile, to S/PDIF 'iec958-stereo' profile.

pactl set-card-profile alsa_card.platform-sound output:iec958-stereo
  • Run audio playback:
speaker-test --channels 2 --format S32_LE -f 440 -t sine -l 1