How to programmatically change volume in Ubuntu
Solution 1
Ubuntu uses pulseaudio as sounderver. It can be controlled from the command line using the pactl
and pacmd
utilities, for example:
pactl set-sink-volume 0 20%
would set the volume of the sink #0 to 20%.
see: man pactl
and pacmd help
edit:
to avoid -xx
being interpreted as command line option you must prefix it with --
. That stops option parsing from that point:
pactl set-sink-volume 0 -- -20% # or:
pactl -- set-sink-volume 0 -20% # doesn't matter where the `--` goes
Solution 2
I do it using ALSA mixer. You probably need to download python-alsaaudio
sudo apt-get install python-alsaaudio
Then to control volume,
import alsaaudio
m = alsaaudio.Mixer() # defined alsaaudio.Mixer to change volume
m.setvolume(50) # set volume
vol = m.getvolume() # get volume float value
Read http://pyalsaaudio.sourceforge.net/libalsaaudio.html to know about alsaaudio library in details.
Solution 3
amixer command worked in Ubuntu 13.04,
Increase volume by 5%
amixer -D pulse sset Master 5%+
Decrease volume by 5%
amixer -D pulse sset Master 5%-
pactl or pacmd did not work for me correctly in Ubuntu 13.04.
Solution 4
Dirty snippet to read volume (don't forget volume goes past "100%" on ubuntu - at which point this returns ~0.66).
#!/usr/bin/python
import subprocess
vol = int(filter(lambda l: l.startswith('set-sink-volume'),
subprocess.check_output(["pacmd","dump"])
.split('\n'))[0]
.split()[-1],16)/100000.
print vol
Solution 5
I can recommend this tool that controls pulseaudio: https://github.com/graysky2/pulseaudio-ctl
me@mypc ~ $ pulseaudio-ctl
pulseaudio-ctl v1.63
/usr/bin/pulseaudio-ctl {up,down,mute,mute-input,set,atmost,full-status} [n]
Where up and down adjust volume in ±5 % increments
Where up and down [n] adjust volume in ±n % increments
Where mute toggles the mute status on/off
Where mute-input toggles the input status on/off
Where set set the volume to [n] %
Where atmost only takes effect if current volume is higher than [n]
Where full-status prints volume level, sink and source mute state to stdout
Optionally, redefine an upper threshold in /home/me/.config/pulseaudio-ctl/config
Volume level : 80 %
Is sink muted : no
Is source muted : no
Detected sink : 1
Detected source : 3
Pulse version : 8.0
me@mypc ~ $
Related videos on Youtube
Cerin
Updated on February 18, 2020Comments
-
Cerin about 4 years
How do you programmatically change volume in Gnome on Ubuntu, either from the command line or an API (Python preferrably)?
The only answers I found to similar questions use
amixer
, which seems to have no effect on Ubuntu 12.04. Running:amixer set Headphone 10-
shows:
Simple mixer control 'Headphone',0 Capabilities: pvolume pswitch penum Playback channels: Front Left - Front Right Limits: Playback 0 - 115 Mono: Front Left: Playback 0 [57%] [-57.50dB] [on] Front Right: Playback 0 [57%] [-57.50dB] [on]
The x% changes each time I run it. Unfortunately, it has no effect on the actual volume. Eventually it says 0%, but volume is still at full blast.
The other downside is I have to specify the exact active output device, which I might not know if there are multiple devices. For example, if I have a "Master" and "Headphone", how do I determine which one is the active device?
-
aland almost 12 yearsJust a guess: does
amixer set Master 10-
works? Changing master volume would affect all other channels. as far as I know. -
jfs almost 12 years@aland:
amixer set Master 10-
works. -
Cerin almost 12 yearsamixer has no effect, regardless of which device I specify...
-
-
Cerin almost 12 yearsHow do you set relative volume changes? The manpage says "If the volume specification start with a + or - the volume adjustment will be relative to the current sink volume." but doing
pactl set-sink-volume 0 -10%
gives me the error "pactl: invalid option -- '1'" -
mata almost 12 years@Cerin - that's a common problem with option parsing in the shell... updated my answer.
-
Cerin almost 12 yearsThanks for the clarification.
-
Froyo almost 12 yearsI know but I'm using ubuntu 12.04 and this is still working. So, I guess you can use this.
-
Cerin almost 12 yearsWeird. I'm also using 12.04, on a macbook, and none of the alsa utilities work for me. However, all the pulseaudio utils work perfectly...
-
dom0 about 11 yearsPulseaudio will never talk to hardware directly, it'll still use ALSA for a reasonably long time. And, not everyone is using pulseaudio or is going to use pulseaudio.
-
Cubic about 11 years@dom0 This still doesn't actually work for me though. Setting the volume doesn't do anything.
-
dom0 about 11 yearsIf you use Pulseaudio, you need to explicitly specify the card ID (usually 0) as you would want to use alsamixer [passing cardindex=0 to alsaaudio.Mixer]. If you don't you'll automatically connect to pulseaudio via some wrapper library (the emulated ALSA mixers you'll get from PA cannot change system volume). I used that with success to read and change the volume while using Pulseaudio: github.com/enkore/i3pystatus/blob/master/i3pystatus/alsa.py#L41
-
Honza almost 11 yearsHas no effect on Ubuntu 12.
-
Michael Butler about 10 yearsThis worked for me in Ubuntu 13.04 (also posted as answer below, please upvote for visibility).
amixer -D pulse sset Master 5%+
-
VasiliNovikov about 10 yearsBoth the proposed
amixer set Master 10-
and your commands work for me, thanks! -
PVH about 10 years@mata - Can we check the sink-volume using command. means how much percent it is.
-
mata about 10 years
pacmd dump-volumes
produces some output, but it's not really friendly to parse (writes welcome and command prompts to stdout). Alternatively,amixer -D pulse get Master
may be an option...