How to play audio

Stable: 08.06.2022 - 11:48 / Revision: 08.06.2022 - 11:46

I am here to prevent the first-page-empty bug!

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 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 which allows to list audio profiles and to select a profile, to play on 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:

Info white.png Information
'playback_codec' is an alias defined in /etc/asound.conf, for headset output device.
 aplay -D playback_codec /usr/share/sounds/alsa/Front_Left.wav

3.1.2 Playback via PulseAudio

  • Configure Pulseaudio

Change the 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
  • Run audio playback:
 aplay /usr/share/sounds/alsa/Front_Left.wav

3.2 Audio playback on HDMI output

Warning white.png Warning
The support of HDMI output is board dependent. Please, check available outputs with "aplay -l" command.

3.2.1 Playback on ALSA device

Run audio playback on 'playback_hdmi' ALSA device:

Info white.png Information
'playback_hdmi' is an alias defined in /etc/asound.conf, for hdmi output device.
 aplay -D playback_hdmi /usr/share/sounds/alsa/Front_Left.wav

3.2.2 Playback via PulseAudio

  • Configure Pulseaudio

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

Info white.png Information
Example below is given for sound card index 0. Check sound card index with "pactl list cards short" command.
 pacmd set-card-profile 0 output:hdmi-stereo
  • Run audio playback:
 aplay /usr/share/sounds/alsa/Front_Left.wav

3.3 Audio playback on S/PDIF output

Warning white.png Warning
The support of S/PDIF output is board dependent. Please, check available outputs with "aplay -l" command.
Info white.png Information
S/PDIF ALSA device supports only 32-bit audio streams. As 32-bit wave files are not provided in sample sound files, speaker-test is used instead of aplay, for convenience, here. Speaker-test generates a 440Hz sine wave in following examples.

3.3.1 Set IEC958 status bits

Example: set IEC958 mode bit.

 iecset -c STM32MP1EV -n device=0 pro on

3.3.2 Playback on ALSA device

Run audio playback on 'playback_spdif' ALSA device:

Info white.png Information
'playback_spdif' is an alias defined in /etc/asound.conf, for S/PDIF output device.
 speaker-test -D playback_spdif -c 2 -F S32_LE -f 440 -t sine -l 1

3.3.3 Playback via PulseAudio

  • Configure Pulseaudio

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

Info white.png Information
Example below is given for sound card index 0. Check sound card index with "pactl list cards short" command.
 pacmd set-card-profile 0 output:iec958-stereo
  • Run audio playback:
 speaker-test -c 2 -F S32_LE -f 440 -t sine -l 1



IMPORTANT NOTICE – READ CAREFULLY
STMicroelectronics NV and its subsidiaries ("ST") reserve the right to make changes, corrections, enhancements, modifications, and improvements to ST products and/or to this document at any time without notice. Purchasers should obtain the latest relevant information on ST products before placing orders. ST products are sold pursuant to ST’s terms and conditions of sale in place at the time of order acknowledgment.

Purchasers are solely responsible for the choice, selection, and use of ST products and ST assumes no liability for application assistance or the design of purchasers’ products.

No license, express or implied, to any intellectual property right is granted by ST herein.

Resale of ST products with provisions different from the information set forth herein shall void any warranty granted by ST for such product.

ST and the ST logo are trademarks of ST. For additional information about ST trademarks, refer to www.st.com/trademarks. All other product or service names are the property of their respective owners.

Information in this document supersedes and replaces information previously supplied in any prior versions of this document.