Howto show volume in Linux with On-Screen Display

libaosd Method is much prettier than the XOSD Method

=libaosd Method=

Requirements

 * libaosd

Usage
all the following displays volume
 * Increase volume
 * aosd_volume volup
 * Decrease volume
 * aosd_volume voldown
 * Mute volume
 * aosd_volume mute

Script
name the script, aosd_volume
 * 1) !/bin/bash

MUTESTATUS=$(amixer get $MIXERNAME | grep "Left:" | awk '{print $4}' | tr -d '[]')
 * 1) Detect mute status and set OSD color

if [ $MUTESTATUS == "off" ]; then OSDCOLOR=red; else OSDCOLOR=green fi

volset=$(amixer get $MIXERNAME | grep "Left:" | awk '{print $4}')
 * 1) Create the "I" bar based on mixer setting and granularity

let volcounter=volset/$VOLINC if [ $volcounter -gt $GRANULARITY ]; then volcounter=$GRANULARITY fi

OSDI=""

while [ $volcounter -gt 0 ] do OSDI=`echo $OSDI"I"` let volcounter=volcounter-1 done

killall aosd_cat &> /dev/null
 * 1) Clean up any running aosd_cat processes

echo "$OSDI" | aosd_cat  -n "Arial Black $FONTSIZE" -u 1000 -o 200 -R $OSDCOLOR -S none -f 0 -y -10
 * 1) Display the "I" bar


 * 1) END

=XOSD Method=

Requirements

 * XOSD
 * Large Bitmap font
 * Example: 10x20 (download)

Usage
all the following displays volume
 * Display volume
 * xosd_volume show
 * Increase volume
 * xosd_volume incr
 * Decrease volume
 * xosd_volume decr
 * Mute volume
 * xosd_volume mute on/off

Script
Name the script xosd_volume
 * 1) !/bin/bash

CHANNEL='PCM' FONT='10x20'
 * 1) Config
 * 1) Config

action=$1; shift
 * 1) Code
 * 1) Code

set_volume { amixer sset $CHANNEL unmute &> /dev/null & volume=$(amixer sset $CHANNEL $1 unmute | \            grep "Left: Playback" | \             grep -o "\[[0-9]*%\]" | \             tr '[%]' ' ') }
 * 1) Unmute the volume and increase/decrease it
 * 2) Arg 1: volume change to set (1+ or 1-)

get_volume { volume=$(amixer sget $CHANNEL | \            grep "Left: Playback" | \             grep -o "\[[0-9]*%\]" | \             tr '[%]' ' ') }
 * 1) Get the current volume %
 * 2) No args

mute_volume { status=$(amixer sset $CHANNEL toggle | \            grep "Left: Playback" | \             grep -o "\[on\]\|\[off\]" | \             tr '[]' ' ') }
 * 1) Toggle Master volume mute on/off
 * 2) No args

show_volume { killall -9 -q osd_cat &>/dev/null
 * 1) Use xosd to show a volume guage on the screen
 * 2) Arg 1: Current volume as percent of full volume
 * 3) Arg 2: (optional) Text to show above bar

osd_cat    --font="$FONT"     --shadow=1     --color=green     --pos=middle     --align=center     --delay=2 --text "$( [ "z$2" = "z" ] && echo Volume: $1% || echo $2 )"    --barmode=percentage --percentage=$1 }

case "$action" in   show)        get_volume		show_volume $volume         ;;

incr)       delta=1+        set_volume $delta        show_volume $volume         ;;

decr)       delta=1-        set_volume $delta        show_volume $volume         ;;

mute)       mute_volume        case "$status" in            off) show_volume 0 "Muted" ;;           on)                get_volume                show_volume $volume                ;;        esac        ;;    *) echo "Usage: $0 {incr|decr|mute|show}" ;; esac