Hatherly.com

Blog



MythTV Tips: Universal Volume Control

Posted on February 25, 2012 by Adam under MythTV, Ubuntu

I have done a fair bit of tinkering with my MythTV box over the last year or two, and I thought I would share a couple of handy tips with the world.

One of the things I always found annoying about MythTV was the fact that I could not alter the volume unless I was in the music or video playback screens. Myth allows you to keep music playing in the background, for example while you browse some news feeds in MythNews – but if you do this you can no longer alter or mute the volume, which is annoying…

To get around this I decided to ditch Myth’s own internal volume controls and create a script that can alter the master volume control directly. This can be launched directly as a result of keypresses on the remote control thanks to a handy app called irexec. This allows me to control the volume even if MythTV is not running.

So, to begin with we need a script to alter the volume. The other problem is that we want to have some visual indication of the volume level as we alter it, so we need to use an OSD (On Screen Display) library of some sort. Initially I used the gnome notification library that ships with recent gnome versions, but I had a strange issue with it not updating quickly enough if I send a few updates in quick succession, so I tried a few others. In the end I settled on osd_cat. The script is loosely based on this one:

#!/bin/bash

/home/adam/.local-display-coordinates.sh

#####################
# Config
CHANNEL='Master'
FONT='10x20'
DELTA_VAL_UP=3+
DELTA_VAL_DOWN=3-

####################
# Code
action=$1; shift

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

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

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

show_volume() {
    killall -9 -q osd_cat
    (osd_cat --font="$FONT" --shadow=1 --color=green 
    --pos=bottom --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
        ;;

    up)
        delta=$DELTA_VAL_UP
        set_volume $delta
        show_volume $volume
        ;;

    down)
        delta=$DELTA_VAL_DOWN
        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 {up|down|mute|show}"
        ;;
esac

In recent versions of Ubuntu there has been some changes to the way that scripts can connect to the active X session, so the line at the start of this script:

/home/adam/.local-display-coordinates.sh

calls a script that is generated automatically when the X session starts. This is created by adding the following to your ~/.profile script:

case $DISPLAY in
  :*) export | grep -E ' 
  (DISPLAY|XAUTHORITY)=' 
  >~/.local-display-coordinates.sh;;
esac

This is based on the advice on this page.

You can test your script by running it from an xterminal using ./setVolume.sh up or ./setVolume.sh down to alter the system volume. You should see the volume displayed at the bottom of the screen in green.

The final step it to configure lirc to run your script when the volume buttons are pressed.
Make sure you have irexec installed, and add it to your startup applications so that it is always running (I will do a subsequent post about auto-restarting mythtv which will also cover auto-restarting lircmd in case it crashes).
You can now edit your .lircrc file to launch you script using irexec when the volume keys are pressed – add the following to your .lircrc file:

begin
    prog = irexec
    button = KEY_VOLUMEUP
    config = /home/adam/Scripts/setVolume.sh up
    repeat = 2
end
begin
    prog = irexec
    button = KEY_VOLUMEDOWN
    config = /home/adam/Scripts/setVolume.sh down
    repeat = 2
end

Make sure you remove the existing mythtv volume entries so you don’t end up controlling both volume controls whenever you press the button on the remote.
Restart lircd, and hopefully everything should work! You may have to restart your gnome session to make sure the script is able to pick up the active X session correctly.



  |  1 Comment


1 Comment:


laura on April 10, 2014

I am very lucky to have this amazing daddy (and to have one that almost always tickles me!)




Leave a Comment