509 lines
17 KiB
Bash
Executable File
509 lines
17 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
#talking-clock written by Storm Dragon
|
|
#project first created on Wednesday, March 23, 2011
|
|
#license WTFPL: http://wtfpl.net
|
|
|
|
number_to_text()
|
|
{
|
|
# If the first argument is not numeric, then it is the return variable.
|
|
if ! [[ "$1" =~ ^[0-9]+$ ]]; then
|
|
local __numberVariable=$1
|
|
shift
|
|
fi
|
|
local number=$1
|
|
# Check for negative numbers.
|
|
if [ "${number:0:1}" = "-" ]; then
|
|
local textNumber="negative "
|
|
number="${number:1}"
|
|
else
|
|
local textNumber=""
|
|
fi
|
|
# This loop processes the numbers, only up to 999 for now, but can be expanded easily, I think.
|
|
while [ ${#number} -gt 0 ]; do
|
|
# To expand, add or to the if, 2 or 5 or 8, etc
|
|
if [ ${#number} -eq 2 ]; then
|
|
case $number in
|
|
0[1-9])
|
|
textNumber="${textNumber}and "
|
|
number="${number:1}"
|
|
;;
|
|
10)
|
|
textNumber="${textNumber}ten"
|
|
;;
|
|
11)
|
|
textNumber="${textNumber}eleven"
|
|
;;
|
|
12)
|
|
textNumber="${textNumber}twelve"
|
|
;;
|
|
13)
|
|
textNumber="${textNumber}thirteen"
|
|
;;
|
|
14)
|
|
textNumber="${textNumber}fourteen"
|
|
;;
|
|
15)
|
|
textNumber="${textNumber}fifteen"
|
|
;;
|
|
16)
|
|
textNumber="${textNumber}sixteen"
|
|
;;
|
|
17)
|
|
textNumber="${textNumber}seventeen"
|
|
;;
|
|
18)
|
|
textNumber="${textNumber}eightteen"
|
|
;;
|
|
19)
|
|
textNumber="${textNumber}nineteen"
|
|
;;
|
|
2*)
|
|
textNumber="${textNumber}twenty"
|
|
;;
|
|
3*)
|
|
textNumber="${textNumber}thirty"
|
|
;;
|
|
4*)
|
|
textNumber="${textNumber}forty"
|
|
;;
|
|
5*)
|
|
textNumber="${textNumber}fifty"
|
|
;;
|
|
6*)
|
|
textNumber="${textNumber}sixty"
|
|
;;
|
|
7*)
|
|
textNumber="${textNumber}seventy"
|
|
;;
|
|
8*)
|
|
textNumber="${textNumber}eighty"
|
|
;;
|
|
9*)
|
|
textNumber="${textNumber}ninety"
|
|
;;
|
|
esac
|
|
fi
|
|
# This stops the loop if the final digit is 0 otherwise we get a repeat of the digit before, 90 becomes 99 etc.
|
|
if [ ${#number} -eq 2 -a ${number:$((${#number} - 1)):1} -eq 0 ]; then
|
|
break
|
|
fi
|
|
# Put dashes in the proper places.
|
|
if [ ${#number} -eq 2 -a $number -gt 20 ]; then
|
|
if [ $(($number % 10)) -ne 0 ]; then
|
|
textNumber="${textNumber}-"
|
|
fi
|
|
fi
|
|
# Remove digits from the number that have already been processed.
|
|
if [ ${#number} -eq 2 -a $number -lt 20 ]; then
|
|
number="${number:2}"
|
|
fi
|
|
case ${number:0:1} in
|
|
1)
|
|
textNumber="${textNumber}one"
|
|
;;
|
|
2)
|
|
textNumber="${textNumber}two"
|
|
;;
|
|
3)
|
|
textNumber="${textNumber}three"
|
|
;;
|
|
4)
|
|
textNumber="${textNumber}four"
|
|
;;
|
|
5)
|
|
textNumber="${textNumber}five"
|
|
;;
|
|
6)
|
|
textNumber="${textNumber}six"
|
|
;;
|
|
7)
|
|
textNumber="${textNumber}seven"
|
|
;;
|
|
8)
|
|
textNumber="${textNumber}eight"
|
|
;;
|
|
9)
|
|
textNumber="${textNumber}nine"
|
|
esac
|
|
# Add the correct bit to the text number, million, thousand, hundred, etc. Trim off already processed digits.
|
|
case ${#number} in
|
|
3)
|
|
if [ $(($number % 100)) -eq 0 ]; then
|
|
textNumber="${textNumber} hundred"
|
|
else
|
|
textNumber="${textNumber} hundred "
|
|
fi
|
|
number="${number:1}"
|
|
;;
|
|
2)
|
|
number="${number:2}"
|
|
;;
|
|
*)
|
|
number="${number:1}"
|
|
esac
|
|
done
|
|
# If we have a return variable set its value to the text.
|
|
if [ -n "$__numberVariable" ]; then
|
|
eval $__numberVariable="'$textNumber'"
|
|
# Else just echo the text.
|
|
else
|
|
echo "$textNumber"
|
|
fi
|
|
}
|
|
|
|
#Create or modify cron jobs
|
|
if [ "$1" == "--cron" ] ; then
|
|
if [ "$#" -ne "2" ] ; then
|
|
echo -e "Invalid cron option:\n"\
|
|
"to set a cron enter:\n"\
|
|
"talking-clock --cron chimes-per-hour\n"\
|
|
"If you would like cron to chime 4 times per hour (every quarter hour) you would enter:\n"\
|
|
"talking-clock --cron 4\n"\
|
|
"Valid entries are 1, 2, and 4. Anything else causes the cron to be removed if it exists.\n"\
|
|
"After you have made changes you can use the command:\n"\
|
|
"crontab -l\n"\
|
|
"to view your settings."
|
|
exit 1
|
|
fi
|
|
crontabFile="$(mktemp)"
|
|
crontab -l > "$crontabFile"
|
|
read -n1 -p "You are about to modify talking-clock settings. Are you sure you want to do this? (Only y or Y confirms, anything else cancels) ", answer
|
|
if [ "${answer^}" != "Y" ]; then
|
|
echo "talking-clock settings unchanged."
|
|
rm "$crontabFile"
|
|
exit 0
|
|
fi
|
|
sed -i -r '/^.*talking-clock.*$/d' "$crontabFile"
|
|
case "$2" in
|
|
"1")
|
|
#Chime once per hour.
|
|
echo "# talking-clock settings" >> "$crontabFile"
|
|
echo '0 * * * * /usr/bin/talking-clock &> /dev/null' >> "$crontabFile"
|
|
;;
|
|
"2")
|
|
#Chime twice per hour.
|
|
echo "# talking-clock settings" >> "$crontabFile"
|
|
echo '0,30 * * * * talking-clock &> /dev/null' >> "$crontabFile"
|
|
;;
|
|
"4")
|
|
#Chime four times per hour.
|
|
echo "# talking-clock settings" >> "$crontabFile"
|
|
echo '0,15,30,45 * * * * talking-clock &> /dev/null' >> "$crontabFile"
|
|
esac
|
|
#install the new cron file.
|
|
if [ -f/"$crontabFile" ]; then
|
|
if crontab "$crontabFile" ; then
|
|
echo "talking-clock settings updated."
|
|
rm "$crontabFile"
|
|
exit 0
|
|
else
|
|
echo "There was an error installing the new crontab file."
|
|
rm "$crontabFile"
|
|
exit 1
|
|
fi
|
|
else
|
|
echo "Couldn't create new crontab."
|
|
exit 1
|
|
fi
|
|
exit 0
|
|
fi
|
|
|
|
#initialize variables
|
|
hour=$(date +'%-l')
|
|
minute=$(date +'%-M')
|
|
timeOfDay=$(date +'%p' | sed 's/AM/A M/')
|
|
#Default sound to play on half hour *:30
|
|
prependSound="/usr/share/talking-clock/prepend.ogg"
|
|
quarterHourChime="/usr/share/talking-clock/15.ogg"
|
|
#Default sound to play on hours
|
|
hourChime="/usr/share/talking-clock/bell.ogg"
|
|
#play chimes?
|
|
chime="true"
|
|
#command used to play sounds
|
|
soundCommand="play -qV0"
|
|
#default voice for speaking time is espeak
|
|
voice="espeak"
|
|
#should the time be spoken?
|
|
speakTime="true"
|
|
|
|
#Check for settings files in order of importants
|
|
if [ -f "$HOME/.talking-clockrc" ] ; then
|
|
#Read from local settings
|
|
confFile="$HOME/.talking-clockrc"
|
|
elif [ -f "/etc/talking-clockrc" ] ; then
|
|
#Read from global settings
|
|
confFile="/etc/talking-clockrc"
|
|
fi
|
|
|
|
#read from configuration file if it exists
|
|
if [ -n "$confFile" ] ; then
|
|
#use varCheck to test if the variable is set in the file
|
|
#if it is, set it to the correct variable, if not use the default.
|
|
#check for 24 hour time format
|
|
varCheck="$(grep -i '^format=24' "$confFile")"
|
|
if [ -n "$varCheck" ] ; then
|
|
format="24"
|
|
fi
|
|
#check for hour chime in file
|
|
varCheck=$(grep -i '^bell=' "$confFile" | cut -d = -f 2)
|
|
if [ -n "$varCheck" ] ; then
|
|
hourChime="$varCheck"
|
|
fi
|
|
#check for quarter-hour chime in file
|
|
varCheck=$(grep -i '^quarter=' "$confFile" | cut -d = -f 2)
|
|
if [ -n "$varCheck" ] ; then
|
|
quarterHourChime="$varCheck"
|
|
fi
|
|
#check for prepended sound
|
|
varCheck=$(grep -i '^prepend=' "$confFile" | cut -d = -f 2)
|
|
if [ -n "$varCheck" ] ; then
|
|
prependSound="$varCheck"
|
|
fi
|
|
#check for command to play sounds
|
|
varCheck=$(grep -i '^sound=' "$confFile" | cut -d = -f 2)
|
|
if [ -n "$varCheck" ] ; then
|
|
soundCommand="$varCheck"
|
|
fi
|
|
#get the voice used to speak time
|
|
varCheck=$(grep -i '^voice=' "$confFile" | cut -d = -f 2 | tr -d "&;<>\"|\$\)\(")
|
|
if [ -n "$varCheck" ] ; then
|
|
voice="$varCheck"
|
|
fi
|
|
#check for zipcode for current temperature
|
|
varCheck=$(grep -i '^zipcode=' "$confFile" | cut -d = -f 2)
|
|
if [ -n "$varCheck" ] ; then
|
|
zipcode="$varCheck"
|
|
fi
|
|
#check for soundpack directory
|
|
varCheck=$(grep -i '^soundpack=' "$confFile" | cut -d = -f 2)
|
|
if [ -n "$varCheck" ] ; then
|
|
soundPack="$varCheck"
|
|
fi
|
|
#Check for tor mode
|
|
varCheck=$(grep -i '^torify=' "$confFile" | cut -d = -f 2)
|
|
if [ -n "$varCheck" ] ; then
|
|
torify="$varCheck"
|
|
fi
|
|
#Check for no speech mode
|
|
varCheck=$(grep -i '^speak=' "$confFile" | cut -d = -f 2)
|
|
if [ -n "$varCheck" ] ; then
|
|
speakTime="$varCheck"
|
|
fi
|
|
#Check for no chime mode
|
|
varCheck=$(grep -i '^chime=' "$confFile" | cut -d = -f 2)
|
|
if [ -n "$varCheck" ] ; then
|
|
chime="$varCheck"
|
|
fi
|
|
fi
|
|
|
|
#Get and process commandline args which override all other settings.
|
|
while [ $# -gt 0 ] ; do
|
|
case "$1" in
|
|
"-a" | "--audio")
|
|
shift
|
|
soundCommand="$1"
|
|
;;
|
|
"-q" | "--quarterhour")
|
|
shift
|
|
if [ -f "$1" ] ; then
|
|
quarterHourChime="$1"
|
|
else
|
|
echo "File $1 not found."
|
|
exit 1
|
|
fi
|
|
;;
|
|
"-b" | "--bell")
|
|
shift
|
|
if [ -f "$1" ] ; then
|
|
hourChime="$1"
|
|
else
|
|
echo "File $1 not found."
|
|
exit 1
|
|
fi
|
|
;;
|
|
"-f" | "--format")
|
|
shift
|
|
if [ "$1" == "12" -o "$1" == "24" ] ; then
|
|
if [ $1 -eq 24 ] ; then
|
|
format="24"
|
|
fi
|
|
else
|
|
echo "Invalid time format: Valid options are 12 and 24."
|
|
exit 1
|
|
fi
|
|
;;
|
|
"-p" | "--prepend")
|
|
shift
|
|
if [ -f "$1" ] ; then
|
|
prependSound="$1"
|
|
else
|
|
echo "File $1 not found."
|
|
exit 1
|
|
fi
|
|
;;
|
|
"-s" | "--soundpack")
|
|
shift
|
|
if [ -d "$1" ] ; then
|
|
soundPack="$1"
|
|
else
|
|
echo "Directory $1 does not exist."
|
|
exit 1
|
|
fi
|
|
;;
|
|
"-n" | "--nospeak")
|
|
speakTime="false"
|
|
;;
|
|
"-t" | "--torify")
|
|
torify="true"
|
|
;;
|
|
"-c" | "--nochime")
|
|
chime="false"
|
|
;;
|
|
"-v" | "--voice")
|
|
shift
|
|
voice="$1"
|
|
;;
|
|
"-z" | "--zipcode")
|
|
shift
|
|
zipcode="$1"
|
|
;;
|
|
*)
|
|
echo -e "Talking-clock by Storm Dragon\nTalking-clock accepts the following arguments:\n--cron number:\nIf you use --cron the only valid entry is a number following it. 1 will cause the clock to chime every hour, 2 every\nhalf-hour, 4 every quarter, and anything else will delete your existing settings if they exist.\n-a --audio Command for playing sound. The default is play -q provided from the sox package.\n-b --bell Set path to the sound used for hourly chimes.\n-c --nochime Turn off chimes.\n-f --format: 12 or 24 hour format. Default is 12 hour time.\n-n --nospeak turn off spoken time.\n-p --prepend Select a sound file to be played before chiming.\n-q --quarterhour Set the path to the sound for quarter-hour chimes.\n-s --soundpack Set path to soundpack. Sound packs should be in ogg format and contain 1.ogg, 2.ogg, ... 11.ogg, 12.ogg and\nquarter.ogg for the quarter-hour chime.\n-v --voice Select voice. Default is espeak other options are\ncepstral, espeak, festival, pico, speech-dispatcher and custom.\nTo set a custom voice enter the command as in:\n-v 'espeak -v en-us+klatt2'\n-t --torify retrieve temperature anonymously using torify, used with -z --zipcode\n-z --zipcode postal code Used for current temperature, may not be available inn all areas.\nFor complete information read the README located at /usr/share/talking-clock/README"
|
|
exit 0
|
|
esac
|
|
shift
|
|
done
|
|
|
|
#Speak the time
|
|
#Safely create voice files for tts who write to file
|
|
voiceFile="$(mktemp --tmpdir tlkclkXXXX.wav)"
|
|
if [ "$speakTime" == "true" ] ; then
|
|
if [ "$minute" -eq "0" ] ; then
|
|
timeString="$hour o clock $timeOfDay"
|
|
elif [ "$minute" -lt "10" ] ; then
|
|
timeString="$(number_to_text $hour) o $(number_to_text $minute) $timeOfDay"
|
|
else
|
|
timeString="$(number_to_text $hour) $(number_to_text $minute) $timeOfDay"
|
|
fi
|
|
#if 24 time is set, override the above with correct settings.
|
|
if [ -n "$format" ] ; then
|
|
#Make it read purdy for speech synthesizers.
|
|
timeString="$(date +'%H:%M' | sed -e 's/^00:00$/zero hundred hours/' -e 's/:00$/ hundred hours/' -e 's/:0/ o /' -e 's/^0//')"
|
|
else
|
|
timeString="$(echo "$timeString" | sed -e 's/^0//' -e 's/^10/ten/' -e 's/^11/eleven/' -e 's/^12/twelve/')"
|
|
fi
|
|
#Add temperature if zipcode is set
|
|
if [ -n "$zipcode" ] ; then
|
|
if [ "$torify" == "true" ] ; then
|
|
temperature="$(torify curl -s "http://weather.yahooapis.com/forecastrss?p=${zipcode}&u=f" | grep -A 1 "Current Conditions:" | tr -Cd '[:digit:]-')"
|
|
else
|
|
temperature="$(curl -s "http://weather.yahooapis.com/forecastrss?p=${zipcode}&u=f" | grep -A 1 "Current Conditions:" | tr -Cd '[:digit:]-')"
|
|
fi
|
|
if [ -n "$temperature" ] ; then
|
|
timeString="$timeString $(number_to_text $temperature) degrees."
|
|
fi
|
|
fi
|
|
case "$voice" in
|
|
"cepstral")
|
|
swift -o $voiceFile "$timeString"
|
|
#If the default sound command is used write time to file and normalize it before playing, cause Cepstral is kind of low volume.
|
|
if [ "$soundCommand" == "play -qV0" ] ; then
|
|
$soundCommand $voiceFile norm
|
|
else
|
|
$soundCommand $voiceFile
|
|
fi
|
|
;;
|
|
"espeak")
|
|
espeak -v en-us -a 175 "$timeString"
|
|
;;
|
|
"festival")
|
|
#If the default sound command is used write time to file and normalize it before playing, cause festival is kind of low volume.
|
|
if [ "$soundCommand" == "play -qV0" ] ; then
|
|
echo "$timeString" | text2wave -o $voiceFile
|
|
$soundCommand $voiceFile norm
|
|
else
|
|
echo "$timeString" | festival --tts
|
|
fi
|
|
;;
|
|
"flite")
|
|
#For some reason flite has trouble with ##:## format.
|
|
echo "$timeString" | flite
|
|
;;
|
|
"googletts")
|
|
/usr/bin/translate-shell -speak -b "$timeString"
|
|
;;
|
|
"flite_time")
|
|
flite_time $(date +'%H:%M') &> /dev/null
|
|
;;
|
|
"speech-dispatcher")
|
|
spd-say -w -P important "$timeString"
|
|
;;
|
|
"pico")
|
|
pico2wave -w $voiceFile "$timeString"
|
|
#ogg123 doesn't do wav, so hopefully everyone has aplay...
|
|
if [[ "$soundCommand" == "ogg123" || "$soundCommand" == "ogg123 -q" ]] ; then
|
|
aplay -q $voiceFile
|
|
else
|
|
$soundCommand $voiceFile
|
|
fi
|
|
;;
|
|
*)
|
|
$voice "$timeString"
|
|
esac
|
|
rm $voiceFile
|
|
fi
|
|
|
|
#Play the prepended sound if one is selected
|
|
#There will be a slight gap between the prepended sound and the actual chiming.
|
|
#This is to simulate real clocks based on my experience.
|
|
if [ "$minute" -eq "0" ]; then
|
|
if [ -n "$prependSound" ] ; then
|
|
$soundCommand "$prependSound"
|
|
fi
|
|
fi
|
|
|
|
#chime for quarter hour
|
|
if [[ "$minute" -eq "15" || "$minute" -eq "30" || "$minute" -eq "45" ]] ; then
|
|
#Play correct sound pack file if we are using sound packs
|
|
if [ -n "$soundPack" ] ; then
|
|
quarterHourChime="$soundPack/$minute.ogg"
|
|
else
|
|
quarterHourChime="/usr/share/talking-clock/$minute.ogg"
|
|
fi
|
|
#Play sound for half-hour
|
|
if [ $chime != "false" ] ; then
|
|
$soundCommand "$quarterHourChime"
|
|
fi
|
|
fi
|
|
|
|
#Chime on the hour
|
|
if [ "$minute" -eq "0" ] ; then
|
|
#Check if we are using a soundpack
|
|
if [ -z "$soundPack" ] ; then
|
|
if [ $chime != "false" ] ; then
|
|
i=0
|
|
#create chime string for sound players that can handle more than one sound argument.
|
|
soundString=""
|
|
while [ "$i" -lt "$hour" ] ; do
|
|
if [[ "$soundCommand" == "play" || "$soundCommand" == "play -q" || "$soundCommand" == "ogg123" || "$soundCommand" == "ogg123 -q" ]] ; then
|
|
soundString="$soundString $hourChime"
|
|
else
|
|
$soundCommand "$hourChime"
|
|
fi
|
|
i=$(($i + 1))
|
|
done
|
|
if [ -n "$soundString" ] ; then
|
|
#No quotes around soundString here because it will not work.
|
|
$soundCommand $soundString
|
|
fi
|
|
fi
|
|
else
|
|
if [ $chime != "false" ] ; then
|
|
$soundCommand "$soundPack/$hour.ogg"
|
|
fi
|
|
fi
|
|
fi
|
|
exit 0
|
|
|