How to use command line to change volume?

I am trying to control the volume using my programming script. How can I do the following in Fedora 15, Ubuntu linux?

  1. Mute/ Unmute
  2. Volume up and volume down

Note: Please note that I use a web USB microphone/speaker and also Analogue microphone/speaker. I want to apply to all to be sure.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can use amixer. It’s in the alsa-utils package on Ubuntu and Debian.

Run amixer without parameters to get an overview about your controls for the default device.

You can also use alsamixer without parameters (from the same package) to get a more visual overview. Use F6 to see and switch between devices. Commonly you might have PulseAudio and a hardware soundcard to select from.

Then use amixer with the set command to set the volume.
For example, to set the master channel to 50%:

amixer set Master 50%

Master is the control name and should match one that you see when running without paramters.

Note the % sign, without it it will treat the value as 0 – 65536 level.

If PulseAudio is not your default device, you might can use the -D switch:

amixer -D pulse set Master 50%

Other useful commands pointed out in the comments:

To increase/decrease the volume use +/- after the number, use

amixer set Master 10%+
amixer set Master 10%-

To mute, unmute or toggle between muted/unmuted state, use

amixer set Master mute
amixer set Master unmute
amixer set Master toggle

Also note that there might be two different percentage scales, the default raw and for some devices a more natural scale based on decibel, which is also used by alsamixer. Use -M to use the latter.

Finally, if you’re interested in PulseAudio only, you might want to check out pactl (see one of the other answers).

Method 2

To mute:

amixer -D pulse sset Master mute

To unmute:

amixer -D pulse sset Master unmute

To turn volume up 5%:

amixer -D pulse sset Master 5%+

To turn volume down 5%:

amixer -D pulse sset Master 5%-

Method 3

pactl/pacmd (unlike amixer) allows increasing volume over 100% :-).

pactl set-sink-mute 0 toggle  # toggle mute, also you have true/false
pactl set-sink-volume 0 0     # mute (force)
pactl set-sink-volume 0 100%  # max
pactl set-sink-volume 0 +5%   # +5% (up)
pactl set-sink-volume 0 -5%   # -5% (down)

Manual settings over 100% is possible in pavucontrol (unlike alsamixer).

Note: If you want to share the same commands on different hosts with different sinks, you can use @[email protected] as a sink instead of number 0:

pactl set-sink-volume @<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fdb9b8bbbca8b1a9a2aeb4b3b6bd">[email protected]</a> +5%

You set your default sink with pactl set-default-sink my-sink-name (list names with pactl list short sinks).

Source: askubuntu.com, wiki.archlinux.org.

Method 4

Higher volume:

amixer set Master 3%+

Lower volume:

amixer set Master 3%-

Mute toggle:

amixer set Master toggle

Example keybindings for i3/sway, the commands are after exec:

bindsym XF86AudioRaiseVolume exec amixer set Master 3%+
bindsym XF86AudioLowerVolume exec amixer set Master 3%-
bindsym XF86AudioMute exec amixer set Master toggle
bindsym Ctrl+$alt+Up  exec amixer set Master 3%+
bindsym Ctrl+$alt+Down exec amixer set Master 3%-

Method 5

if the user has muted the device you have to ‘unmute’ it. otherwise setting the percentage will work but the sound is still off

amixer set 'Master' 100% unmute
/usr/bin/amixer set 'PCM' 100% unmute

Method 6

amixer worked for me but I didn’t get the nice animation that I get when I press the volume up button on my keyboard.

I decided to use xte to directly press that key from the command line:

Volume up:
xte 'key 0x1008ff13'

Volume down:
xte 'key 0x1008ff11'

Mute:
xte 'key 0x1008ff12'

I figured out the keysym (that hex number) by using xev.

sudo apt-get install xbindkeys xautomation
xev

and then press the volume up button on your keyboard to get the keysym. The key sym may vary from system to system so finding it from xev will be the most reliable way.

The result looks like this for me:

KeyRelease event, serial 37, synthetic NO, window 0x2c00001,
root 0xef, subw 0x0, time 6660080, (566,573), root:(664,651),
state 0x0, keycode 123 (keysym 0x1008ff13, XF86AudioRaiseVolume), same_screen YES,
XLookupString gives 0 bytes:
XFilterEvent returns: False

Method 7

pevik‘s answer is almost correct for PulseAudio, with two things to mention:

  • the negative volume change command will generate an error because the minus-something portion of it will be interpreted as a separate (unrecognized) option; you need to turn off option parsing with a double-minus somewhere before the negative number, e.g. ” — -5%”
  • the true mute state is something that should be possible to toggle with the same key, which won’t work if you’re just setting the volume to a fixed value of 0, so the right command for that would be:

pactl set-sink-mute @[email protected] toggle

LE:
Whether the first hint is applicable may depend on the version of PulseAudio you’re running or on something else in the distro – I notice that on Ubuntu 16 it works without the “–” and fails if I add “–” in front of the negative percentage.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x