1. Overview

These days, audio is an essential part of many applications. Therefore, operating systems must have controllers to control the audio.

In Linux, we have different applications to handle the volume level and other audio properties. Some of these applications may have a graphical user interface (GUI), but in this tutorial, we’ll discuss getting and setting the volume level from the command line.

2. What Is ALSA?

ALSA (Advanced Linux Sound Architecture) is a software framework and part of the Linux kernel. It provides APIs for sound card drivers to be used by applications. In fact, ALSA controls the volume and other audio parameters of any sound that comes from our computer.

To read or change audio volume in Linux, we need to get from or set values to a part of ALSA called the mixer.

3. Controlling the Mixer

In the command line, we can communicate with the ALSA mixer by using the amixer command:

$ amixer
Simple mixer control 'Master',0
Capabilities: pvolume pswitch pswitch-joined
Playback channels: Front Left - Front Right
Limits: Playback 0 - 65536
Mono:
Front Left: Playback 39322 [60%] [on]
Front Right: Playback 39322 [60%] [on]
Simple mixer control 'Capture',0
Capabilities: cvolume cswitch cswitch-joined
Capture channels: Front Left - Front Right
Limits: Capture 0 - 65536
Front Left: Capture 65536 [100%] [on]
Front Right: Capture 65536 [100%] [on]

We can see that there are two mixer controllers, called ‘Master’ and ‘Capture’. Each of these has a left and right volume. To get the current volume for a specific mixer controller, we can use amixer‘s sget (or get) command along with the mixer name.

Let’s get the current volume of the ‘Master’ controller:

$ amixer sget Master
Simple mixer control 'Master',0
Capabilities: pvolume pswitch pswitch-joined
Playback channels: Front Left - Front Right
Limits: Playback 0 - 65536
Mono:
Front Left: Playback 39322 [60%] [on]
Front Right: Playback 39322 [60%] [on]

4. Filtering the Volume Percentage

AWK is a powerful programming language for text processing. We can pass the amixer output to the awk command and filter the volume percentage.

For example, if we want to get the volume percentage for the left channel of the ‘Master’, we can pass amixer‘s sget output to awk and find the line with the ‘Left:’ keyword. Then, split the line by ‘[‘ or ‘]’ delimiters and print the second part:

$ amixer sget Master | awk -F"[][]" '/Left:/ { print $2 }'
60%

5. Setting the Volume Level

The amixer command can also set the volume for each mixer controller with the sset (or set) command.

Let’s change the ‘Master’ volume to 50%:

$ amixer sset Master 50%
Simple mixer control 'Master',0
Capabilities: pvolume pswitch pswitch-joined
Playback channels: Front Left - Front Right
Limits: Playback 0 - 65536
Mono:
Front Left: Playback 32768 [50%] [on]
Front Right: Playback 32768 [50%] [on]

If we want to increase or decrease the volume level by a certain percentage, we can use ‘+’ or ‘-‘ after the desired percent number:

$ amixer sset Master 5%+
 Simple mixer control 'Master',0
 Capabilities: pvolume pswitch pswitch-joined
 Playback channels: Front Left - Front Right
 Limits: Playback 0 - 65536
 Mono:
 Front Left: Playback 36045 [55%] [on]
 Front Right: Playback 36045 [55%] [on]

Also, we can use mute, unmute, or toggle commands to mute/unmute the sound.

6. Conclusion

In this article, we introduced ALSA and its amixer command-line interface (CLI). Then, we used amixer‘s sget and sset to get and set the volume level, respectively. And as a bonus, we learned how to parse and extract the volume percentage so that we can pass it to another app.

Comments are closed on this article!