16

is there a way to adjust the system volume via commandline so that the default volume popup (the one that pops up when pressing the media keys on notebooks) is still shown.

I need this for my remote control. It'll be run using a lircrc file and irexec.

1

5 Answers 5

17

Install the xdotool package, and try issuing

xdotool key XF86AudioLowerVolume

and

xdotool key XF86AudioRaiseVolume
5
  • 1
    You might need to prepend that with DISPLAY=:0 (or another if your display is different) for the lirc user to be able to send it to the right place. Might not though.
    – Oli
    Nov 12, 2010 at 0:59
  • 1
    Thanks alot! I did not need to set the DISPLAY variable...
    – Lincoln
    Nov 12, 2010 at 1:10
  • 1
    You might need to use --clearmodifiers parameter after key to use it on Ubuntu keyboard shortcuts settings. Jan 18, 2017 at 2:31
  • @Oli Yes, you'll need that for e.g. changing the volume over SSH.
    – wjandrea
    Sep 27, 2017 at 23:29
  • @PabloBianchi In my experience, Unity remaps keys really easily, no need for --clearmodifiers, however you definitely need it for xbindkeys.
    – wjandrea
    Sep 27, 2017 at 23:55
3

You could bind a shortcut to this script I've found in the Arch forums (needs the package libnotify-bin):

#!/bin/sh

usage="usage: $0 -c {up|down|mute} [-i increment] [-m mixer]"
command=
increment=5%
mixer=Master

while getopts i:m:h o
do case "$o" in
    i) increment=$OPTARG;;
    m) mixer=$OPTARG;;
    h) echo "$usage"; exit 0;;
    ?) echo "$usage"; exit 0;;
esac
done

shift $(($OPTIND - 1))
command=$1

if [ "$command" = "" ]; then
    echo "usage: $0 {up|down|mute} [increment]"
    exit 0;
fi

display_volume=0

if [ "$command" = "up" ]; then
    display_volume=$(amixer set $mixer $increment+ unmute | grep -m 1 "%]" | cut -d "[" -f2|cut -d "%" -f1)
fi

if [ "$command" = "down" ]; then
    display_volume=$(amixer set $mixer $increment- unmute | grep -m 1 "%]" | cut -d "[" -f2|cut -d "%" -f1)
fi

icon_name=""

if [ "$command" = "mute" ]; then
    if amixer get Master | grep "\[on\]"; then
        display_volume=0
        icon_name="notification-audio-volume-muted"
        amixer set $mixer mute
    else
        display_volume=$(amixer set $mixer unmute | grep -m 1 "%]" | cut -d "[" -f2|cut -d "%" -f1)
    fi
fi

if [ "$icon_name" = "" ]; then
    if [ "$display_volume" = "0" ]; then
        icon_name="notification-audio-volume-off"
    elif [ "$display_volume" -lt "33" ]; then
        icon_name="notification-audio-volume-low"
    elif [ "$display_volume" -lt "67" ]; then
        icon_name="notification-audio-volume-medium"
    else
        icon_name="notification-audio-volume-high"
    fi
fi
notify-send " " -i $icon_name -h int:value:$display_volume -h string:synchronous:volume

Seems to work fine in Ubuntu 10.10.

2

Control Sound Volume

You can use amixer to control the sound volume, e.g.

amixer set 'Master' 50%
amixer set 'Master' 10%+
amixer set 'Master' 2dB-

You may need to set the soundcard using e.g. -c 1 for the second soundcard, see man amixer.

Play Sound

Sounds can be played using a player like aplay or paplay, e.g.

paplay /usr/share/sounds/freedesktop/stereo/audio-volume-change.oga

You might want to have a look at this question: Where do I find system sounds?

Display On-Screen Notification

You can reproduce the on-screen notification using the X On-Screen Display library XOSD. The package is called xosd-bin and the command osd_cat is used to display text, status bars etc. on the screen.

osd_cat -b percentage -P 20 -T Status: -f "-adobe-helvetica-bold-*-*--34-*-*-*-*"

displays

enter image description here

See this German wiki page for options and examples and man osd_cat for more.

1

This is an improved version of the script htorque posted.

It works for me on 14.04. Let me know if it works on 16.04 or newer.

It requires libnotify-bin installed.

#!/bin/sh
# Adjust the volume, play a sound, and show a notification.
#
# Replacement for default Ubuntu volume adjustment behaviour.
#
# Based on https://askubuntu.com/a/12769/301745

command=""
device="pulse"
display_volume=0
icon_name="error"
increment=5
mixer="Master"
usage="usage: $0 [-d device] [-i increment] [-m mixer] (up|down|mute)"

# For compatibility with SSH sessions.
export DISPLAY=:0

_amixer(){
    # amixer alias
    local set_get="$1"
    shift
    amixer -D "$device" "$set_get" "$mixer" "$@"
}

_get_display_volume(){
    # grep alias
    grep -Pom 1 '(?<=\[)[0-9]+(?=%\])'
}

while getopts d:hi:m: opt; do
    case "$opt" in
        d)
            device="$OPTARG"
            ;;
        h)
            echo "$usage"
            exit 0
            ;;
        i)
            increment="$OPTARG"
            ;;
        m)
            mixer="$OPTARG"
            ;;
        ?)
            echo "$usage"
            exit 1
            ;;
    esac
done

shift "$(($OPTIND - 1))"
command="$1"

case "$command" in
    down)
        display_volume="$(
            _amixer set "$increment%-" unmute |
                _get_display_volume
            )"
        ;;
    mute)
        if _amixer get | grep -q "\[on\]"; then
            display_volume=0
            icon_name="notification-audio-volume-muted"
            _amixer set mute > /dev/null
        else
            display_volume="$(
                _amixer set unmute |
                    _get_display_volume
                )"
        fi
        ;;
    up)
        display_volume="$(
            _amixer set "$increment%+" unmute |
                _get_display_volume
            )"
        ;;
    *)
        echo "$usage"
        exit 1
        ;;
esac

if [ "$icon_name" = "error" ]; then
    if [ "$display_volume" = "0" ]; then
        icon_name="notification-audio-volume-off"
    elif [ "$display_volume" -lt "33" ]; then
        icon_name="notification-audio-volume-low"
    elif [ "$display_volume" -lt "67" ]; then
        icon_name="notification-audio-volume-medium"
    else
        icon_name="notification-audio-volume-high"
    fi

    # In a subshell in the background to minimize latency.
    ( canberra-gtk-play --id=audio-volume-change & )
fi

notify-send "Volume: $display_volume%" -i "$icon_name" -h "string:synchronous:volume" -h "int:value:$display_volume"
0

I installed xmacro and added following lines to .lircrc:

begin
        prog = irexec
        button = KEY_VOLUMEUP
        repeat = 1
        delay = 2
        config = echo KeyStrPress XF86AudioRaiseVolume KeyStrRelease XF86AudioRaiseVolume | xmacroplay $DISPLAY
end
begin
        prog = irexec
        button = KEY_VOLUMEDOWN
        repeat = 1
        delay = 2
        config = echo KeyStrPress XF86AudioLowerVolume KeyStrRelease XF86AudioLowerVolume | xmacroplay $DISPLAY
end
begin
        prog = irexec
        button = KEY_MUTE
        config = echo KeyStrPress XF86AudioMute KeyStrRelease XF86AudioMute | xmacroplay $DISPLAY
end

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .