Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Bash Suspend Alarm Clock with Gradual Volume and Fallback
#!/bin/bash
__script_version="1.0"
#-----------------------------------------------------------------------
# Default values
#-----------------------------------------------------------------------
human_time="7 tomorrow"
media_url="http://178.33.191.197:6060"
#=== FUNCTION ================================================================
# NAME: gradual
# DESCRIPTION: Increase the volume gradually to 100.
#===============================================================================
function gradual ()
{
ponymix set-volume 10 > /dev/null
current_volume=$(ponymix get-volume)
while [[ current_volume -lt 100 ]]
do
sleep 0.5
current_volume=$(ponymix increase 1)
done
} # ---------- end of function gradual ----------
#=== FUNCTION ================================================================
# NAME: fallback
# DESCRIPTION: Play obnoxious speaker-test tones.
#===============================================================================
function fallback ()
{
got_input=142
until [ $got_input -eq 0 ]
do
frequency=$(shuf -i 200-600 -n 1)
speaker-test -f $frequency -t sine -l 1 > /dev/null &
disown $! # avoid useless kill messages
duration=$(echo "$(shuf -i 5-25 -n 1) * 0.1" | bc)
read -t $duration # play beep for .5 to 2.5 seconds
got_input=$?
pkill -9 --ns $$ speaker-test
done
} # ---------- end of function fallback ----------
#=== FUNCTION ================================================================
# NAME: usage
# DESCRIPTION: Display usage information.
#===============================================================================
function usage ()
{
echo "Usage : $0 [options] [--]
Options:
-h|help Display this message
-v|version Display script version
-m|media The audio url, to be played by mplayer
-t|time The human readable time and date for the alarm"
} # ---------- end of function usage ----------
#-----------------------------------------------------------------------
# Handle command line arguments
#-----------------------------------------------------------------------
while getopts ":hvm:t:" opt
do
case $opt in
h|help ) usage; exit 0 ;;
v|version ) echo "$0 -- version $__script_version"; exit 0 ;;
m|media ) media_url=$OPTARG ;;
t|time ) human_time=$OPTARG ;;
* ) echo -e "\n Option does not exist : $OPTARG\n"
usage; exit 1 ;;
esac # --- end of case ---
done
shift $(($OPTIND-1))
#-----------------------------------------------------------------------
# Suspend the machine, and wake at the given time
#-----------------------------------------------------------------------
unix_time=$(date +%s -d "$human_time")
if [ $? -ne 0 ]
then
exit 1
fi
sudo rtcwake -m mem -t $unix_time > /dev/null
sleep 30 # give time to restore network connectivity
#-----------------------------------------------------------------------
# Set the volume and play our media
#-----------------------------------------------------------------------
saved_volume=$(ponymix get-volume)
gradual &
mplayer -noconsolecontrols -really-quiet -msglevel all=-1 -nolirc "$media_url" > /dev/null &
#-----------------------------------------------------------------------
# If mplayer fails, use our fallback
#-----------------------------------------------------------------------
got_input=142
while true; do
if read -t 1; then
got_input=$?
pkill --ns $$ mplayer > /dev/null
break
elif ! pgrep --ns $$ mplayer > /dev/null; then
break
fi
done
if [ $got_input -ne 0 ]
then
gradual &
fallback
fi
ponymix set-volume $saved_volume > /dev/null
echo "Good Morning! ;)"

Very nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment