Originally Posted by
Vermind
Hi,
nobody likes sharp edges, so I thought up a smooth fade of volume into the mute and lock script. It now remembers your pre-lock volume and fades it down to zero prior to muting. When unlocking, it fades the volume up again. I also made it modular and less platform-dependent by providing some settings for choosing the sound card and channel.
Code:
#!/bin/bash
########################################
# Settings #
########################################
card=0; # Sound card. 0 == default, first card.
channel="Master"; # Which volume to control. Examples: Master, PCM, Headphone
step=3; # Fade step: The volume increment/decrement amount
interval=0.2 # Seconds between increments/decrements when fading
########################################
# Functions #
########################################
# fades volume up or down
function fade {
# get current volume
vol=$( ${m} sget ${channel} | awk '$0 ~ "%" { print $3 }' )
if [ "$1" == "down" ]; then
# fade down:
while [ ${vol} -gt 0 ]; do
sleep ${interval}
${m} set ${channel} ${step}-
let vol-=${step}
done
${m} set ${channel} mute
else
# first unmute, then fade up
${m} set ${channel} unmute
while [ ${orig} -gt ${vol} ]; do
sleep ${interval}
let vol+=${step}
if [ ${vol} -gt ${orig} ]; then vol=${orig}; fi
${m} set ${channel} ${step}+
done
${m} set ${channel} ${orig}
fi
}
# Returns when the screen has been locked and unlocked
function unlockcheck {
haslock=
while true; do
sleep 2
locked=$( gnome-screensaver-command -q | grep " active" )
if [ -n "${haslock}" ]; then
# lock has happened before. Check unlock and break if unlocked
if [ -z "${locked}" ]; then break; fi
fi
if [ -n "${locked}" ]; then
haslock="true"
fi
done
}
########################################
# Startup #
########################################
# short mixer command with card
m="amixer -c ${card}";
# record original volume value
orig=$( ${m} sget ${channel} | awk '$0 ~ "%" { print $3 }' )
########################################
# Procedure #
########################################
gnome-screensaver-command -l # lock screen
fade down # fade volume down and mute
unlockcheck # wait for unlock
fade up # fade volume up and unmute
# That's it!
Ok, I better do some work...
Running this new script in the terminal gives me the following:
Code:
./mute-and-lock.sh: line 21: [: too many arguments
Simple mixer control 'Master',0
Capabilities: pvolume pswitch pswitch-joined
Playback channels: Front Left - Front Right
Limits: Playback 0 - 30
Mono:
Front Left: Playback 24 [80%] [-10.50dB] [off]
Front Right: Playback 24 [80%] [-10.50dB] [off]
Simple mixer control 'Master',0
Capabilities: pvolume pswitch pswitch-joined
Playback channels: Front Left - Front Right
Limits: Playback 0 - 30
Mono:
Front Left: Playback 24 [80%] [-10.50dB] [on]
Front Right: Playback 24 [80%] [-10.50dB] [on]
./mute-and-lock.sh: line 30: [: too many arguments
amixer: Invalid command!
Seems to be a problem somewhere!
Also (and I think this may be related to the error) after it unlocks the volume unmutes after about 1-2 seconds.
The old script gives the following output:
Code:
Simple mixer control 'Master',0
Capabilities: pvolume pswitch pswitch-joined
Playback channels: Front Left - Front Right
Limits: Playback 0 - 30
Mono:
Front Left: Playback 24 [80%] [-10.50dB] [off]
Front Right: Playback 24 [80%] [-10.50dB] [off]
Simple mixer control 'Master',0
Capabilities: pvolume pswitch pswitch-joined
Playback channels: Front Left - Front Right
Limits: Playback 0 - 30
Mono:
Front Left: Playback 24 [80%] [-10.50dB] [on]
Front Right: Playback 24 [80%] [-10.50dB] [on]
But, even using this script there is a 1-2 second delay after unlock for the volume to unmute.
Bookmarks