Linux aplay Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the aplay command in Linux, which is part of the ALSA (Advanced Linux Sound Architecture) utilities. The aplay command provides a user-level interface to the ALSA sound system, allowing you to play audio files from the command line. You will start by installing the required packages to use aplay, then explore its various options and flags to play audio files in different ways.

The lab covers the following steps: installing the required packages for aplay, using the aplay command to play audio files, and exploring the available aplay command options and flags. This lab is designed to provide you with practical examples and a solid understanding of the aplay command, which can be a useful tool for managing audio playback in your Linux environment.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/PackagesandSoftwaresGroup(["`Packages and Softwares`"]) linux/PackagesandSoftwaresGroup -.-> linux/wget("`Non-interactive Downloading`") linux/PackagesandSoftwaresGroup -.-> linux/apt("`Package Handling`") subgraph Lab Skills linux/wget -.-> lab-422545{{"`Linux aplay Command with Practical Examples`"}} linux/apt -.-> lab-422545{{"`Linux aplay Command with Practical Examples`"}} end

Install the Required Packages for aplay

In this step, we will install the required packages to use the aplay command in Linux. The aplay command is part of the ALSA (Advanced Linux Sound Architecture) utilities, which provide a user-level interface to the ALSA sound system.

First, let's update the package index:

sudo apt-get update

Example output:

Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [114 kB]
Get:4 http://archive.ubuntu.com/ubuntu jammy-backports InRelease [99.8 kB]
Fetched 324 kB in 1s (324 kB/s)
Reading package lists... Done

Next, install the alsa-utils package, which contains the aplay command and other ALSA utilities:

sudo apt-get install -y alsa-utils

Example output:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  alsa-firmware-loaders alsa-topology-conf alsa-ucm-conf libatopology2 libsnd-dev libsndfile1 libsndfile1-dev
Suggested packages:
  alsa-oss
The following NEW packages will be installed:
  alsa-firmware-loaders alsa-topology-conf alsa-ucm-conf alsa-utils libatopology2 libsnd-dev libsndfile1 libsndfile1-dev
0 upgraded, 8 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,265 kB of archives.
After this operation, 5,120 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://archive.ubuntu.com/ubuntu jammy/main amd64 alsa-firmware-loaders amd64 1.2.4-1 [12.1 kB]
Get:2 http://archive.ubuntu.com/ubuntu jammy/main amd64 alsa-topology-conf all 1.2.4-1 [11.0 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy/main amd64 alsa-ucm-conf all 1.2.4-1 [89.0 kB]
Get:4 http://archive.ubuntu.com/ubuntu jammy/main amd64 alsa-utils amd64 1.2.6-1ubuntu1 [1,153 kB]
Get:5 http://archive.ubuntu.com/ubuntu jammy/main amd64 libatopology2 amd64 1.2.4-1 [13.9 kB]
Get:6 http://archive.ubuntu.com/ubuntu jammy/main amd64 libsnd-dev amd64 1.0.29-1 [18.0 kB]
Get:7 http://archive.ubuntu.com/ubuntu jammy/main amd64 libsndfile1 amd64 1.0.31-2 [111 kB]
Get:8 http://archive.ubuntu.com/ubuntu jammy/main amd64 libsndfile1-dev amd64 1.0.31-2 [57.1 kB]
Fetched 1,265 kB in 1s (1,265 kB/s)
Selecting previously unselected package alsa-firmware-loaders.
(Reading database ... 123412 files and directories currently installed.)
Preparing to unpack .../alsa-firmware-loaders_1.2.4-1_amd64.deb ...
Unpacking alsa-firmware-loaders (1.2.4-1) ...
Selecting previously unselected package alsa-topology-conf.
Preparing to unpack .../alsa-topology-conf_1.2.4-1_all.deb ...
Unpacking alsa-topology-conf (1.2.4-1) ...
Selecting previously unselected package alsa-ucm-conf.
Preparing to unpack .../alsa-ucm-conf_1.2.4-1_all.deb ...
Unpacking alsa-ucm-conf (1.2.4-1) ...
Selecting previously unselected package alsa-utils.
Preparing to unpack .../alsa-utils_1.2.6-1ubuntu1_amd64.deb ...
Unpacking alsa-utils (1.2.6-1ubuntu1) ...
Selecting previously unselected package libatopology2:amd64.
Preparing to unpack .../libatopology2_1.2.4-1_amd64.deb ...
Unpacking libatopology2:amd64 (1.2.4-1) ...
Selecting previously unselected package libsnd-dev:amd64.
Preparing to unpack .../libsnd-dev_1.0.29-1_amd64.deb ...
Unpacking libsnd-dev:amd64 (1.0.29-1) ...
Selecting previously unselected package libsndfile1:amd64.
Preparing to unpack .../libsndfile1_1.0.31-2_amd64.deb ...
Unpacking libsndfile1:amd64 (1.0.31-2) ...
Selecting previously unselected package libsndfile1-dev:amd64.
Preparing to unpack .../libsndfile1-dev_1.0.31-2_amd64.deb ...
Unpacking libsndfile1-dev:amd64 (1.0.31-2) ...
Setting up alsa-firmware-loaders (1.2.4-1) ...
Setting up alsa-topology-conf (1.2.4-1) ...
Setting up alsa-ucm-conf (1.2.4-1) ...
Setting up libatopology2:amd64 (1.2.4-1) ...
Setting up libsnd-dev:amd64 (1.0.29-1) ...
Setting up libsndfile1:amd64 (1.0.31-2) ...
Setting up libsndfile1-dev:amd64 (1.0.31-2) ...
Setting up alsa-utils (1.2.6-1ubuntu1) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for libc-bin (2.35-0ubuntu3) ...

Now the required packages for the aplay command are installed, and we can proceed to the next step.

Play Audio Files Using the aplay Command

In this step, we will learn how to use the aplay command to play audio files in Linux.

First, let's download a sample audio file to use in our examples:

wget https://www.soundjay.com/button/sounds/beep-07.wav -O ~/project/beep.wav

Example output:

--2023-04-12 08:31:33--  https://www.soundjay.com/button/sounds/beep-07.wav
Resolving www.soundjay.com (www.soundjay.com)... 104.24.108.5, 104.24.109.5
Connecting to www.soundjay.com (www.soundjay.com)|104.24.108.5|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 11174 (11K) [audio/x-wav]
Saving to: '~/project/beep.wav'

~/project/beep.wav     100%[===================>]  10.92K  --.-KB/s    in 0s

2023-04-12 08:31:33 (128 MB/s) - '~/project/beep.wav' saved [11174/11174]

Now, let's play the audio file using the aplay command:

aplay ~/project/beep.wav

Example output:

Playing WAVE '/home/labex/project/beep.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Stereo

You should hear the audio file playing through your system's speakers or headphones.

To stop the playback, press Ctrl+C.

The aplay command supports various options to control the audio playback, such as setting the volume, looping the audio, and more. We'll explore these options in the next step.

Explore aplay Command Options and Flags

In this final step, we will explore some of the available options and flags for the aplay command.

Let's start by displaying the help menu for the aplay command:

aplay --help

Example output:

Usage: aplay [OPTION]... [FILE]...
Plays a PCM sound file.

Options:
  -h, --help             help
  -D, --device=NAME      set the device name
  -c, --channels=NUM     set the number of channels
  -f, --format=FORMAT    set the sample format
  -r, --rate=RATE        set the sample rate
  -t, --type=TYPE        set the file type
  -v, --version          show version
  -V, --verbose          show PCM hardware parameters
  -q, --quiet            quiet mode
  -l, --list-devices     list all soundcards and digital audio devices
  -L, --list-pcm         list all PCM devices
  -Xalsa-opts            pass options for ALSA backend

Some useful options you can try:

  • Play the audio file in a loop:

    aplay -c 2 -r 44100 -f S16_LE ~/project/beep.wav -l 3

    This will play the beep.wav file in stereo (2 channels), at a sample rate of 44.1 kHz, with a 16-bit little-endian format, and loop it 3 times.

  • Adjust the volume:

    aplay -c 2 -r 44100 -f S16_LE ~/project/beep.wav --volume 50

    This will play the beep.wav file at 50% volume.

  • List available audio devices:

    aplay -l

    This will display a list of all the available audio devices on your system.

Feel free to experiment with the different options and flags to customize the audio playback to your needs.

Summary

In this lab, we learned how to install the required packages to use the aplay command in Linux, which is part of the ALSA (Advanced Linux Sound Architecture) utilities. We updated the package index and installed the alsa-utils package, which contains the aplay command and other ALSA utilities. We then explored the aplay command options and flags, which allow us to play audio files and customize the playback settings.

The lab covered the practical usage of the aplay command, including playing audio files and understanding the available options and flags. This knowledge can be applied to various audio-related tasks in a Linux environment.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like