“How to play audio”的版本间的差异

来自百问网嵌入式Linux wiki
第1行: 第1行:
{{DISPLAYTITLE:如何播放音频}}
+
== Article purpose ==
 +
This article explains how to play audio through the [[ALSA overview|ALSA]] audio framework in '''Linux<sup>&reg;</sup> OS''' context.
 +
The examples below, show how to play audio on the different audio hardware interfaces of the STM32MPU [[:Category:ST boards| boards]].
  
=如何播放音频=
+
== Audio playback overview ==
==文章目的==
+
The [[ALSA overview|ALSA]] framework exposes audio devices associated to the board audio hardware interfaces.
: 本文介绍了如何通过具备 ALSA 音频架构的 Linux ® OS 环境播放音频。以下示例显示了如何在 STM32MPU 开发板上的不同音频硬件接口上播放音频。
 
==音频播放概述==
 
: ALSA框架公开了与板音频硬件接口关联的音频设备。
 
: 默认情况下,应用程序音频流通过 PulseAudio 声音服务器进行路由。PulseAudio 公开了映射在 ALSA 声卡音频设备上的音频配置文件。PulseAudio 服务器提供了一个命令行界面,该界面允许列出音频配置文件并选择配置文件以在特定的音频接口上播放。
 
: 以下各节中针对 ALSA 实用程序给出对应的音频播放示例。但是,音频回放可以由其他应用程序启动,例如 gstreamer 多媒体框架。
 
  
==示例==
+
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.
===在耳机输出设备上播放音频===
 
: 在 ALSA 设备上播放
 
:: 在 “playback_codec” ALSA 设备上运行音频播放:
 
  
:: 注意:“playback_codec” 在 /etc/asound.conf 中为耳机输出设备定义别名。
+
The audio playback examples in following sections are given for [[ALSA_overview#How_to_use|ALSA utilities]]. However, the audio playbacks can be launched by other applications, such as [[Gst-play|gstreamer]] multimedia framework.
<syntaxhighlight lang="bash">
 
Board $> aplay -D playback_codec /usr/share/sounds/alsa/Front_Left.wav
 
</syntaxhighlight>
 
: 通过 PulseAudio 播放
 
:: 将声卡的Pulseaudio活动配置文件更改为"analog-stereo”配置文件:
 
  
:: 注意:Pulseaudio 模拟立体声时使用系统默认的配置文件件。因此引导后不需要下一个命令。
+
== Examples ==
:: 注意:下面给出了声卡索引 0 的示例。使用 “pactl list cards short” 命令检查所有的声卡索引。
 
<syntaxhighlight lang="bash">
 
Board $> pacmd set-card-profile 0 output:analog-stereo+input:analog-stereo
 
</syntaxhighlight>
 
:: 播放音频:
 
<syntaxhighlight lang="bash">
 
Board $> aplay /usr/share/sounds/alsa/Front_Left.wav
 
</syntaxhighlight>
 
  
===在HDMI输出设备上播放音频===
+
=== Audio playback on headset output ===
: 注意:HDMI输出的支持取决于主板的硬件是否支持。请使用 “aplay -l” 命令检查所有的可用的输出。
 
: 在 ALSA 设备上播放。
 
: 在 “playback_hdmi” ALSA 设备上运行音频播放:
 
  
: 注意:“playback_hdmi” 是在 /etc/asound.conf 文件中为 hdmi 输出设备定义别名。
+
==== Playback on ALSA device ====
<syntaxhighlight lang="bash">
+
Run audio playback on 'playback_codec' ALSA device:
Board $> aplay -D playback_hdmi /usr/share/sounds/alsa/Front_Left.wav
+
{{Info|'playback_codec' is an alias defined in /etc/asound.conf, for headset output device.}}
</syntaxhighlight>
+
{{Board$}} aplay -D playback_codec /usr/share/sounds/alsa/Front_Left.wav
: 通过PulseAudio播放
 
: 将声卡的Pulseaudio活动配置文件更改为"hdmi-stereo”配置文件:
 
  
: 注意:下面给出了声卡索引0的示例。使用"pactl list cards short"命令可检查所有的声卡索引。
+
==== Playback via PulseAudio ====
<syntaxhighlight lang="bash">
 
Board $> pacmd set-card-profile 0 output:hdmi-stereo
 
</syntaxhighlight>
 
: 运行音频播放:
 
<syntaxhighlight lang="bash">
 
Board $> aplay /usr/share/sounds/alsa/Front_Left.wav
 
</syntaxhighlight>
 
  
===在 S/PDIF 输出设备上播放音频===
+
* '''Configure Pulseaudio'''
: 注意::  S/PDIF 输出的支持取决于主板的硬件是否支持。请使用"aplay -l"命令检查可用的输出。
+
Change the Pulseaudio active profile of the sound card, to 'analog-stereo' profile:
: 注意: S/PDIF ALSA 设备仅支持 32 位音频流。由于示例声音文件中未提供 32 位波形文件,因此在这里为方便起见,使用扬声器测试代替了播放。扬声器测试在以下示例中生成 440Hz 正弦波。
+
{{Info|The Pulseaudio analog-stereo profile is the default profile. So next command is not required after boot.}}
: 在 ALSA 设备上播放
+
{{Info|Example below is given for {{highlight|sound card index 0}}. Check sound cards index with "''pactl list cards short''" command.}}
: 在"playback_spdif” ALSA 设备上运行音频播放:
+
  {{Board$}} pacmd set-card-profile 0 output:analog-stereo+input:analog-stereo
  
: 注意:"playback_spdif” 是在 /etc/asound.conf 文件中为 S/PDIF 输出设备定义的别名。
+
* '''Run audio playback:'''
<syntaxhighlight lang="bash">
+
{{Board$}} aplay /usr/share/sounds/alsa/Front_Left.wav
Board $> speaker-test -D playback_spdif -c 2 -F S32_LE -f 440 -t sine -l 1
 
</syntaxhighlight>
 
: 通过 PulseAudio 播放
 
: 将声卡活动配置文件更改为 S/PDIF 'iec958-stereo' 配置文件。
 
  
: 注意:下面给出了声卡索引 0 的示例。使用 "pactl list cards short" 命令检查所有的声卡索引。
+
=== Audio playback on HDMI output ===
<syntaxhighlight lang="bash">
+
{{Warning|The support of HDMI output is board dependent. Please, check available outputs with "''aplay -l''" command.}}
Board $> pacmd set-card-profile 0 output:iec958-stereo
 
</syntaxhighlight>
 
: 运行音频播放:
 
<syntaxhighlight lang="bash">
 
Board $> speaker-test -c 2 -F S32_LE -f 440 -t sine -l 1
 
</syntaxhighlight>
 
  
 +
==== Playback on ALSA device ====
 +
Run audio playback on 'playback_hdmi' ALSA device:
 +
{{Info|'playback_hdmi' is an alias defined in /etc/asound.conf, for hdmi output device.}}
 +
{{Board$}} aplay -D playback_hdmi /usr/share/sounds/alsa/Front_Left.wav
  
[[Category:Linux_Operating_System]]
+
==== Playback via PulseAudio ====
[[Category:Audio]]
+
 
[[Category:ALSA]]
+
* '''Configure Pulseaudio'''
[[Category:How_to ]][[Category:How_to_run_use_cases ]]
+
Change the Pulseaudio active profile of the sound card, to 'hdmi-stereo' profile:
 +
{{Info|Example below is given for {{highlight|sound card index 0}}. Check sound card index with "''pactl list cards short''" command.}}
 +
{{Board$}} pacmd set-card-profile 0 output:hdmi-stereo
 +
 
 +
* '''Run audio playback:'''
 +
{{Board$}} aplay /usr/share/sounds/alsa/Front_Left.wav
 +
 
 +
=== Audio playback on S/PDIF output ===
 +
{{Warning|The support of S/PDIF output is board dependent. Please, check available outputs with "''aplay -l''" command.}}
 +
{{Info|S/PDIF ALSA device supports only {{highlight|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.}}
 +
 
 +
==== Set IEC958 status bits ====
 +
Example: set IEC958 mode bit.
 +
{{Board$}} iecset -c STM32MP1EV -n device=0 pro on
 +
 
 +
==== Playback on ALSA device ====
 +
Run audio playback on 'playback_spdif' ALSA device:
 +
{{Info|'playback_spdif' is an alias defined in /etc/asound.conf, for S/PDIF output device.}}
 +
{{Board$}} speaker-test -D playback_spdif -c 2 -F S32_LE -f 440 -t sine -l 1
 +
 
 +
==== Playback via PulseAudio ====
 +
 
 +
* '''Configure Pulseaudio'''
 +
Change the sound card active profile, to S/PDIF 'iec958-stereo' profile.
 +
{{Info|Example below is given for {{highlight|sound card index 0}}. Check sound card index with "''pactl list cards short''" command.}}
 +
{{Board$}} pacmd set-card-profile 0 output:iec958-stereo
 +
 
 +
* '''Run audio playback:'''
 +
{{Board$}} speaker-test -c 2 -F S32_LE -f 440 -t sine -l 1
 +
 
 +
<noinclude>
 +
{{PublicationRequestId | 10391 | 2019-01-21}}
 +
[[Category:How to run use cases]]
 +
[[Category:ALSA]]
 +
</noinclude>

2020年5月7日 (四) 11:06的版本

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.

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.

Examples

Audio playback on headset output

Playback on ALSA device

Run audio playback on 'playback_codec' ALSA device:

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

Playback via PulseAudio

  • Configure Pulseaudio

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

Info.png The Pulseaudio analog-stereo profile is the default profile. So next command is not required after boot.
Info.png Example below is given for sound card index 0. Check sound cards index with "pactl list cards short" command.
Board $> pacmd set-card-profile 0 output:analog-stereo+input:analog-stereo
  • Run audio playback:
Board $> aplay /usr/share/sounds/alsa/Front_Left.wav

Audio playback on HDMI output

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

Playback on ALSA device

Run audio playback on 'playback_hdmi' ALSA device:

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

Playback via PulseAudio

  • Configure Pulseaudio

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

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

Audio playback on S/PDIF output

Warning.png The support of S/PDIF output is board dependent. Please, check available outputs with "aplay -l" command.
Info.png 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.

Set IEC958 status bits

Example: set IEC958 mode bit.

Board $> iecset -c STM32MP1EV -n device=0 pro on

Playback on ALSA device

Run audio playback on 'playback_spdif' ALSA device:

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

Playback via PulseAudio

  • Configure Pulseaudio

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

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


<securetransclude src="ProtectedTemplate:PublicationRequestId" params="10391 | 2019-01-21 |"></securetransclude>