How to programmatically change volume in Ubuntu
Asked Answered
F

6

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?

Francklyn answered 24/5, 2012 at 14:22 Comment(3)
Just a guess: does amixer set Master 10- works? Changing master volume would affect all other channels. as far as I know.Specialistic
@aland: amixer set Master 10- works.Selfcommand
amixer has no effect, regardless of which device I specify...Francklyn
P
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
Promenade answered 24/5, 2012 at 14:42 Comment(6)
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'"Francklyn
@Francklyn - that's a common problem with option parsing in the shell... updated my answer.Promenade
Has no effect on Ubuntu 12.Quickman
This worked for me in Ubuntu 13.04 (also posted as answer below, please upvote for visibility). amixer -D pulse sset Master 5%+Cellarer
@Promenade - Can we check the sink-volume using command. means how much percent it is.Myriagram
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...Promenade
S
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.

Sand answered 24/5, 2012 at 20:57 Comment(7)
I know but I'm using ubuntu 12.04 and this is still working. So, I guess you can use this.Sand
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...Francklyn
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.Maundy
@Maundy This still doesn't actually work for me though. Setting the volume doesn't do anything.Tetreault
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: github.com/enkore/i3pystatus/blob/master/i3pystatus/alsa.py#L41Maundy
Is there any way to control an HDMI device volume using pyalsaaudio?Bikales
Use sudo apt-get install python3-alsaaudio to install alsaaudio for python3Zildjian
C
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.

Cellarer answered 19/2, 2014 at 0:4 Comment(1)
Both the proposed amixer set Master 10- and your commands work for me, thanks!Archlute
I
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
Indiscerptible answered 11/9, 2013 at 17:35 Comment(0)
L
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

Luellaluelle answered 29/4, 2014 at 19:51 Comment(0)
A
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 ~ $ 
Antilogy answered 17/2, 2017 at 18:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.