25

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?

Cerin
  • 60,957
  • 96
  • 316
  • 522

6 Answers6

40

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
mata
  • 67,110
  • 10
  • 163
  • 162
  • 1
    How 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'" – Cerin May 24 '12 at 18:39
  • @Cerin - that's a common problem with option parsing in the shell... updated my answer. – mata May 24 '12 at 19:29
  • 4
    Has no effect on Ubuntu 12. – Honza Jun 07 '13 at 05:23
  • 1
    This worked for me in Ubuntu 13.04 (also posted as answer below, please upvote for visibility). `amixer -D pulse sset Master 5%+` – Michael Butler Feb 19 '14 at 00:01
  • @mata - Can we check the sink-volume using command. means how much percent it is. – PVH Apr 26 '14 at 10:44
  • `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... – mata Apr 26 '14 at 11:20
17

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.

Froyo
  • 17,947
  • 8
  • 45
  • 73
  • I know but I'm using ubuntu 12.04 and this is still working. So, I guess you can use this. – Froyo May 27 '12 at 04:59
  • Weird. 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... – Cerin May 27 '12 at 15:04
  • 1
    Pulseaudio 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. – dom0 Feb 25 '13 at 17:33
  • @dom0 This still doesn't actually work for me though. Setting the volume doesn't do anything. – Cubic Feb 26 '13 at 16:24
  • If 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: https://github.com/enkore/i3pystatus/blob/master/i3pystatus/alsa.py#L41 – dom0 Feb 28 '13 at 23:20
  • Is there any way to control an HDMI device volume using pyalsaaudio? – Luan Souza Sep 10 '22 at 18:16
  • Use sudo apt-get install python3-alsaaudio to install alsaaudio for python3 – Biplob Das Oct 29 '22 at 11:38
7

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.

Michael Butler
  • 6,079
  • 3
  • 38
  • 46
2

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
1

You could also try the simple and elegant ponymix utill. It makes it very easy to increase/decrease the volume, toggle (mute/unmute) the audio, etc.

First get a list of available audio sources with ponymix

In my case, I can see both a sink 0 and a source 0. I can use either the number 0 or the full name, Built-in Audio Digital Stereo (HDMI), to control the audio.

Increase the volume of card 0 by 5%: ponymix -c 0 increase 5

Decrease the volume of card 0 by 5%: ponymix -c 0 decrease 5

1

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 ~ $ 
Nicolas
  • 754
  • 8
  • 22