Page 1 of 1

How to adjust sound volume from a script?

Posted: Sat Apr 24, 2010 1:40 pm
by holck
I am a quite happy user, running LXDE on my small eee 901 notebook.

I would like to be able to control the sound volume and to mute/un-mute sound from a small program or script, but it is a little more complicated and strange than I thought.

When I first enter the LXDE session, the volume control applet in the panel shows sound as muted, and volume = 0. I would guess that the commands below would change this:

Code: Select all

$ amixer cset iface=MIXER,name='Speaker Playback Switch' on
$ amixer cset iface=MIXER,name='Speaker Playback Volume' 50
But apparently nothing happens. If I now un-mute sound, using the applet, and issue this command

Code: Select all

$ amixer cset iface=MIXER,name='Speaker Playback Switch' off
the applet shows the sound as muted. Now I can turn on sound again with

Code: Select all

$ amixer cset iface=MIXER,name='Speaker Playback Switch' on
Volume is still 0, and the following command doesn't change this:

Code: Select all

$ amixer cset iface=MIXER,name='Speaker Playback Volume' 50
But I can turn up volume on the applet (to e.g. 100) and now control volume via the command line

Code: Select all

$ amixer cset iface=MIXER,name='Speaker Playback Volume' 50
sets volume = 58

Strange, isn't it? Any help appreciated
Jesper, Denmark

Re: How to adjust sound volume from a script?

Posted: Sat Apr 24, 2010 7:13 pm
by maces
Hi,

here is my script, works quite well for me:

Code: Select all

#!/bin/sh

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

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

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

if [ "$command" = "micdown" ]; then
    display_volume=$(amixer set $mic $increment- unmute | grep -m 1 "%]" | cut -d "[" -f2|cut -d "%" -f1)
    display_volume=$(amixer set "Front $mic" $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"
    else
        if [ "$display_volume" -lt "33" ]; then
            icon_name="notification-audio-volume-low"
        else
            if [ "$display_volume" -lt "67" ]; then
                icon_name="notification-audio-volume-medium"
            else
                icon_name="notification-audio-volume-high"
            fi
        fi
    fi
fi
notify-send " " -i $icon_name -h int:value:$display_volume -h string:synchronous:volume
call it like that:

Code: Select all

vol.sh up -i 1% -m Master
vol.sh down -i 1% -m Master
vol.sh micdown -i 1% -m 
vol.sh micup -i 1% -m 
And for muting use this:

Code: Select all

amixer set Master toggle
Don't know if it works with eeepc901 immediately. Hope it helps.

maces

Re: How to adjust sound volume from a script?

Posted: Sun Apr 25, 2010 1:32 pm
by Marty Jack
An interesting fact to know is that the mixer that is controlled by the Volume applet is in the device "default", the first of "Master", "Front", "PCM", and "LineOut" that is found. I do not believe this is the one the original poster is setting with their script.

We expect to make this configurable in a future release. It is not going to be easy because of the great variety of ways that the sound system can be set up, particularly if you factor in PulseAudio, and because of the abstraction level of the ALSA API.

Re: How to adjust sound volume from a script?

Posted: Tue Apr 27, 2010 8:08 am
by holck
Thanks for the help and the script - but unfortunately, the script did not solve my problem.

I have done some experimentation, and the small script below seems to work for me, i.e. it unmutes sound:

Code: Select all

amixer sset 'Master' 50 unmute
amixer sset 'Speaker' 50 unmute
amixer sset 'Headphone' 50 unmute
One thing I found out is that isn't enough to unmute just one control, you have to unmute several, including "Headphone".

Jesper,
Denmark

Re: How to adjust sound volume from a script?

Posted: Tue Apr 27, 2010 10:38 am
by rockdoctor
Don't know if it's relevant, but on my Acer Aspire One, I find I need to up the setting on the 'Beep" device too