Page 1 of 2

Pulseaudio - adjust master volume from command line

PostPosted: October 25th, 2011, 12:07 am
by dbbolton
I am looking for a way to utilize the multimedia keys on my laptop. Specifically I want to bind a key to raise, lower, or mute the master volume (I'm using pulseaudio). I want something like

Code: Select all
amixer -c 0 set PCM 2dB+

which works with ALSA.

Re: Pulseaudio - adjust master volume from command line

PostPosted: October 25th, 2011, 1:40 am
by sgosnell
About 15 seconds with Google got me this how-to.

Re: Pulseaudio - adjust master volume from command line

PostPosted: October 25th, 2011, 1:56 am
by dbbolton
sgosnell wrote:About 15 seconds with Google got me this how-to.


Thanks for the link. I don't think it will do anything since I don't have a .volume file. The followup post that included a modified script to generate the necessary file, but it just arbitrarily printed the value '65536' into it. So if that's not the current volume, then you can't actually adjust the volume over the whole range.

If you mean to imply that I didn't search before posting, that's not the case. I searched for "pulseaudio command line" and which turned up essentially nothing other than references to pactl and pacmd. I checked their man pages and they didn't seem suitable-- I searched for /vol and there was no match.

Re: Pulseaudio - adjust master volume from command line

PostPosted: October 25th, 2011, 2:26 am
by sgosnell
Searching requires using useful terms. I think you don't really want to use the command line, you want to use hardware keys. I used 'pulseaudio volume control' and found that and other sources. Try that and see if you find more information.

Re: Pulseaudio - adjust master volume from command line

PostPosted: October 25th, 2011, 3:33 am
by dbbolton
sgosnell wrote:I think you don't really want to use the command line, you want to use hardware keys.

I want to use hardware keys to execute a shell command that will adjust the volume.

Re: Pulseaudio - adjust master volume from command line

PostPosted: October 25th, 2011, 3:43 am
by sgosnell
Better to have the hardware keys control the volume directly. In any case, your search returned no useful information. Try other terms.

Re: Pulseaudio - adjust master volume from command line

PostPosted: October 25th, 2011, 6:27 am
by mojoman
Like you I've done the same thing with alsa, aumix and multimedia keys. Never tried it with pulseaudio though but I'm assuming what you need is the cli commands? Could this be of use?

http://www.pulseaudio.org/wiki/CLI

Re: Pulseaudio - adjust master volume from command line

PostPosted: October 25th, 2011, 6:09 pm
by dbbolton
sgosnell wrote:Better to have the hardware keys control the volume directly.

And how would that be accomplished? Even the solution in the page you suggested accomplished it through a shell script.

mojoman wrote:Like you I've done the same thing with alsa, aumix and multimedia keys. Never tried it with pulseaudio though but I'm assuming what you need is the cli commands? Could this be of use?

http://www.pulseaudio.org/wiki/CLI


The page is timing out. I will have to try it out later.

My guess is it's probably about pactl and pacmd. The script used pactl to adjust the volume in increments, but the problem is that you need to know what the current volume is in order to do it over the entire range. I tried looking through the output of pactl list but couldn't find it. I suppose I could just set the volume to an arbitrary value using the command and then I would know where to start.

Re: Pulseaudio - adjust master volume from command line

PostPosted: October 25th, 2011, 11:31 pm
by ZitZ
alright, I have exactly what you want working on my laptop using only pulseaudio so I think I can help. I don't remember where I got this script from, but I guess a repost at a different spot on the internets won't hurt and will help make this information easier to find.

Code: Select all
#!/bin/bash
#device="alsa_output.pci-0000_00_14.2.analog-stereo"
device="alsa_output.pci-0000_00_1b.0.analog-stereo"
case "$1" in
   "up")    # increase volume by 1000
      pacmd dump | awk --non-decimal-data '$1~/set-sink-volume/{if ($2~/'${device}'/) {if ($3+1000 > 65535) {system ("pactl "$1" '${device}' "65535)} else {system ("pactl "$1" '${device}' "$3+1000)}}}'
      ;;
   "down")  # decrease volume by 1000
      pacmd dump | awk --non-decimal-data '$1~/set-sink-volume/{if ($2~/'${device}'/) {if ($3-1000 < 0) {system ("pactl "$1" '${device}' "0)} else {system ("pactl "$1" '${device}' "$3-1000)}}}'
      ;;
   "mute")  # toggle mute
      pacmd dump|awk --non-decimal-data '$1~/set-sink-mute/{if ($2~/'${device}'/) {system ("pactl "$1" '${device}' "($3=="yes"?"no":"yes"))}}'
      ;;
esac


So put that file into your path and call it something like "volumeincrease" or whatever you want. Obviously, you will need awk installed and pacmd but these are both standard with debian and pulseaudio I believe. Atleast, I've never had to install either on debian.

The other potential problem I can forsee is that you will have to change the device variable in that script to the appropriate device on your machine if it is not the same as mine. I don't remember how to do this from the command line off the top of my head, but it can be found in the pulseaudio manager (paman) application I think it is called, if you chose to install that.

Then all that is left to do is make the script executable and then open up whatever application you are choosing to use to setup your keybindings. I personally use compiz. I bind the mute, volume up, and volume down keys with the following commands.

Mute: /usr/bin/volumeincrease mute
Volume Up: /usr/bin/volumeincrease up
Volume Down: /usr/bin/volumeincrease down

Okay, I hope my explanation works for you. :)

Re: Pulseaudio - adjust master volume from command line

PostPosted: October 26th, 2011, 12:06 am
by dbbolton
Zitz - thanks for the response. I will test it out along with the other one.

I decided, for the time being, to use kmix. I already installed k3b so installing kmix only took up about 2MiB of disk space. It uses more memory, but the nice thing is that it works with my media keys without any tweaking and it shows an OSD when adjusting the volume. Since I have plenty of RAM on my laptop this is probably the best option. I think using the script will be ideal for my lower-memory machines.