xbindkeys-friendly mixer control for pulseaudio

Share your own howto's etc. Not for support questions!

xbindkeys-friendly mixer control for pulseaudio

Postby lancelot » 2012-01-14 14:07

If you find yourself trying this script, please give me feedback, I'd like to know if it works for everyone.

Code: Select all
#!/bin/bash
# /usr/local/bin/pavolcontrol
#
# Copyright (C) 2012 Maze <maze@pkbd.org>
#
# pavolcontrol is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pavolcontrol is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pavolcontrol.  If not, see <http://www.gnu.org/licenses/>.

INCREMENT=5

function get_mute_status
{
        ISMUTE=0
        if ( pactl list sinks | grep "Mute: yes" ) ; then
                ISMUTE=1
        fi
}

function toggle_mute
{
        get_mute_status
        declare -i NEWMUTE=1-$ISMUTE
        pactl set-sink-mute 0 $NEWMUTE
}

function  get_cur_vol
{
        CURVOL=$( pactl list sinks | awk '/Volume: 0:/ {print substr($3, 1, index($3, "%") - 1)}' )
}

case "$1" in
increase)       get_cur_vol
                declare -i NEWVOL=$CURVOL+$INCREMENT
                pactl set-sink-volume 0 ${NEWVOL}"%"
                ;;
decrease)       get_cur_vol
                declare -i NEWVOL=$CURVOL-$INCREMENT
                pactl set-sink-volume 0 ${NEWVOL}"%"
                ;;
toggle)         toggle_mute
                ;;
*)              echo "Usage: /usr/local/bin/pavolctrl {increase|decrease|toggle}"
                exit 2
                ;;
esac
exit 0
User avatar
lancelot
 
Posts: 365
Joined: 2008-12-27 11:50
Location: France

Re: xbindkeys-friendly mixer control for pulseaudio

Postby secipolla » 2012-01-14 20:32

Thank you, works fine here (Debian sid Xfce/pulseaudio).
secipolla
 
Posts: 1127
Joined: 2010-06-21 14:20

Re: xbindkeys-friendly mixer control for pulseaudio

Postby acimmarusti » 2012-06-11 23:59

lancelot wrote:If you find yourself trying this script, please give me feedback, I'd like to know if it works for everyone.


Thanks man, this is awesome!. Unfortunately I found a problem with it. In my laptop, I find myself constantly using the HDMI audio output to watch videos on a TV. Normally I use pavucontrol to switch to the HDMI output. However, everytime this changes, the sink number changes, even when I switch back to the laptop speakers (not sure why this is so...some internal pulseaudio stuff I guess...I wonder if this is how it's supposed to work) rendering your awesome script unusable. I think I feel confident enough to modify it to account for this issue (I'm thinking creating a get_sink_num function, similar to your get_cur_vol or get_mute_status). I will post it when I've tested it... but if you beat me to it, let me know!
acimmarusti
 
Posts: 393
Joined: 2009-02-27 04:59
Location: College Park, MD USA

Re: xbindkeys-friendly mixer control for pulseaudio

Postby acimmarusti » 2012-06-12 01:05

I got it working! I'm so happy!
I used a similar trick as the get_cur_vol function you used. However, I can see limitations in this script. For example, if there are several sinks, it will only choose the first one (which I suppose is the default one...but I'm not sure).

Here's the modified script. Thoughts?

Code: Select all
#!/bin/bash
# /usr/local/bin/pavolctl
#
# Copyright (C) 2012 Maze <maze@pkbd.org>
#
# pavolctl is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pavolctl is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pavolctl.  If not, see <http://www.gnu.org/licenses/>.

INCREMENT=5

function get_sink_num
{
    SINK_NUM=$( pactl list short sinks | awk '{print $1}')
}

function get_mute_status
{
    ISMUTE=0
    if ( pactl list sinks | grep "Mute: yes" ) ; then
        ISMUTE=1
    fi
}

function toggle_mute
{
    get_mute_status
    get_sink_num
    declare -i NEWMUTE=1-$ISMUTE
    pactl set-sink-mute $SINK_NUM $NEWMUTE
}

function  get_cur_vol
{
    CURVOL=$( pactl list sinks | awk '/Volume: 0:/ {print substr($3, 1, index($3, "%") - 1)}' )
}

case "$1" in
    increase)
   get_cur_vol
        get_sink_num
   if [ $CURVOL -lt 100 ]; then
            declare -i NEWVOL=$CURVOL+$INCREMENT
            pactl set-sink-volume $SINK_NUM ${NEWVOL}"%"
   fi
        ;;
    decrease)
   get_cur_vol
        get_sink_num
        declare -i NEWVOL=$CURVOL-$INCREMENT
        pactl set-sink-volume $SINK_NUM ${NEWVOL}"%"
        ;;
    toggle)
   toggle_mute
        ;;
    *)
   echo "Usage: /usr/local/bin/pavolctl {increase|decrease|toggle}"
        exit 2
        ;;
esac
exit 0


EDIT: I modified the script yet once more to put a constraint on the volume so that it can't be increased over 100% (pactl allows the volume to go up to 150%, which makes my speakers crackle).
acimmarusti
 
Posts: 393
Joined: 2009-02-27 04:59
Location: College Park, MD USA

relative volume setting, mute detection failure

Postby konfiguros » 2013-04-02 18:10

For increasing and decreasing volume why don't you just use something like +10% or -10% ? This will only produce volumes from 0% to 100%.

Note that for the -10% stuff you have to do -- -10% for the -10% not being parsed as an option, like
Code: Select all
pactl set-sink-volume 0 -- -10%


Please also note, that in localised versions, "Mute: yes" might be replaced by "Stumm: yes" or something else. It is really disturbing, that we have to parse ununified output. Is there not anything like get-sink-mute or something? Maybe some other pulse audio utility that can do this?
konfiguros
 
Posts: 119
Joined: 2008-11-08 19:49


Return to Docs, Howtos, Tips & Tricks

Who is online

Users browsing this forum: No registered users and 4 guests

fashionable