Skip to content

Instantly share code, notes, and snippets.

@imabuddha
Last active January 9, 2021 06:44
Show Gist options
  • Save imabuddha/fec74b9bcbe133bcdf6d2d4ee3744925 to your computer and use it in GitHub Desktop.
Save imabuddha/fec74b9bcbe133bcdf6d2d4ee3744925 to your computer and use it in GitHub Desktop.
Deal with pulseaudio's crazy output volume

How to deal with pulseaudio's crazy output volume

Best method

TIL from the wonderful ArchWiki that one can specify @DEFAULT_SINK@ as the sink number as follows:

pactl set-sink-mute @DEFAULT_SINK@ toggle

pactl set-sink-volume @DEFAULT_SINK@ +1%

This simplification means that the series of commands to determine the active sink are unnecessary. Not only that, but it works even when the default sink isn't active ("RUNNING"). WTF this isn't documented clearly in the man page is beyond me. It is documented in pactl --help however. 🖕🏻 pulseaudio!

The following info is still valid, but unnecessary when using the method above

Unfortunately pulseaudio doesn't have a single master volume that applies to all outputs ("sinks"). You have to know the index or logical name of the active ("RUNNING") sink (note that > 1 might be running…).

To get the index of the active sink you can run: pactl list sinks short | grep RUNNING | awk ' { print $1 } ' Then you can use the index of the active sink to set volume or mute.

For example, here's how to toggle muting the active sink:

pactl set-sink-mute "$(pactl list sinks short | grep RUNNING | awk ' { print $1 } ')" toggle

This example increases the volume by 1%:

pactl set-sink-volume "$(pactl list sinks short | grep RUNNING | awk ' { print $1 } ')" +1%

Note: pulseaudio allows increasing the volume above 100%! I guess this could be helpful in some contexts.

#!/bin/bash
display_usage() {
echo -e "\nSet pulseaudio output mute state.\n"
echo -e "Usage: $0 1|0|toggle \n"
}
if [ ! $# -eq 1 ]
then
display_usage
exit 1
fi
if [[ ( $1 == "--help") || $1 == "-h" ]]
then
display_usage
exit 0
fi
pactl set-sink-mute @DEFAULT_SINK@ $1
#!/bin/bash
display_usage() {
echo -e "\nSet pulseaudio output volume.\n"
echo -e "Usage: $0 VOLUME\n"
echo -e " VOLUME can be specified as an integer (e.g. 2000, 16384), a linear"
echo -e "\tfactor (e.g. 0.4, 1.100), a percentage (e.g. 10%, 100%) or a"
echo -e "\tdecibel value (e.g. 0dB, 20dB). If the volume specification"
echo -e "\tstarts with a + or - the volume adjustment will be relative to"
echo -e "\tthe current volume.\n"
}
if [ ! $# -eq 1 ]
then
display_usage
exit 1
fi
if [[ ( $1 == "--help") || $1 == "-h" ]]
then
display_usage
exit 0
fi
pactl set-sink-volume @DEFAULT_SINK@ $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment