Last active
March 11, 2018 13:42
-
-
Save bdjnk/7435ae561dc7516b7728 to your computer and use it in GitHub Desktop.
Bash Suspend Alarm Clock with Gradual Volume and Fallback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
__script_version="1.1" | |
#----------------------------------------------------------------------- | |
# Default values | |
#----------------------------------------------------------------------- | |
human_time="6:20 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 mpv | |
-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") | |
seconds=$(($unix_time - $(date +%s))) | |
if [ $? -ne 0 ] | |
then | |
exit 1 | |
fi | |
hours_minutes="$(($seconds / 3600)) hours and $((($seconds / 60) % 60)) minutes" | |
read -p "Set alarm for $hours_minutes from now? [y/n] " go | |
if [ "$go" == n ] | |
then | |
exit 0 | |
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 & | |
mpv --really-quiet $media_url > /dev/null & | |
#----------------------------------------------------------------------- | |
# If mpv fails, use our fallback | |
#----------------------------------------------------------------------- | |
got_input=142 | |
while true; do | |
if read -t 1; then | |
got_input=$? | |
pkill --ns $$ mpv > /dev/null | |
break | |
elif ! pgrep --ns $$ mpv > /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! ;)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very nice