Skip to content

Instantly share code, notes, and snippets.

@yellowcrescent
Last active November 13, 2024 00:46
Show Gist options
  • Save yellowcrescent/b1cd72540e7b492370e685895705331d to your computer and use it in GitHub Desktop.
Save yellowcrescent/b1cd72540e7b492370e685895705331d to your computer and use it in GitHub Desktop.
Yamaha Alarm Clock
#!/bin/bash
# vim: set ts=4 sw=4 expandtab syntax=sh:
##############################################################################
#
# wakeup.sh
# Rev 2 (09/30/2024)
#
# Plays audio file via mpv and Yamaha AVR
# Waits until the specified time-of-day
# Ramps up volume by X steps every Y seconds, until reaching max
# mpv will loop the track a set number of times, then abort
# This script will mute all other audio sources, then unmute on exit
#
##############################################################################
### Volume control method
## Yamaha AVR
PLAYMODE="yamaha"
AVRHOST="yamaha-rxv683"
AVRINPUT="hdmi2"
PASINK="alsa/plughw:CARD=NVidia,DEV=8"
VOL_START=20
VOL_STEP=3
VOL_WAIT=5
VOL_MAX=120
## Local control via mpv
#PLAYMODE="local"
#PASINK="alsa/front:CARD=PCH,DEV=0"
#VOL_START=50
#VOL_STEP=5
#VOL_WAIT=5
#VOL_MAX=105
SONGPATH="/mnt/asgardfs/media/music/_sorted/The Presidents Of The United States Of America/The Presidents Of The United States Of America - The Presidents Of The United States Of America/06. Peaches.mp3"
##########
MPV_IPC_PATH="$HOME/mpv.ipc"
SONGLOOP=5
WAKETIME="9:00"
##########
function cleanup() {
echo -en "\n\n"
echo "> Resetting volume to sane level..."
curl -s "http://${AVRHOST}/YamahaExtendedControl/v1/main/setVolume?volume=${VOL_START}" >/dev/null
echo "> Turning off AVR..."
curl -s "http://${AVRHOST}/YamahaExtendedControl/v1/main/setPower?power=standby" >/dev/null
echo "> Unmuting all other applications..."
for sinkid in $(pactl list short sink-inputs | awk '{print $1}'); do
pactl set-sink-input-mute $sinkid 0
done
echo "** Bye"
exit
}
function mpv_cmd() {
[ -n "$2" ] && p2=", \"$2\"" || p2=""
[ -n "$3" ] && p3=", \"$3\"" || p3=""
echo "{\"command\": [\"$1\" ${p2} ${p3}]}" | nc -w 1 -U ${MPV_IPC_PATH} 2>&1 >/dev/null
if [ $? -ne 0 ]; then
echo "> mpv has terminated"
cleanup
fi
}
if [ -n "$1" ]; then
WAKETIME="$1"
fi
if [[ "${PLAYMODE}" == "yamaha" ]]; then
if [[ -z "${AVRHOST}" ]]; then
echo "ERROR: AVRHOST not set"
exit 1
fi
if [[ -z "${AVRINPUT}" ]]; then
echo "ERROR: AVRINPUT not set"
exit 1
fi
echo "> Starting in AVR/Yamaha mode (${AVRHOST} -> ${AVRINPUT})"
MPV_VOL_INIT=100
elif [[ "${PLAYMODE}" == "local" ]]; then
echo "> Starting in local mode"
MPV_VOL_INIT=$VOL_START
fi
# sleep until it's time...
tdiff=$(($(date -d "${WAKETIME}" +%s) - $(date +%s)))
if [ $tdiff -lt 0 ]; then
tdiff=$(($tdiff + 86400))
fi
echo "> Waiting ${tdiff} seconds to wake you up at ${WAKETIME}..."
echo " [Ctrl+C to abort]"
sleep $tdiff
echo "*** THE TIME HAS COME ***"
# set trap
trap cleanup INT
echo "> Muting all other applications..."
for sinkid in $(pactl list short sink-inputs | awk '{print $1}'); do
pactl set-sink-input-mute $sinkid 1
done
if [[ "${PLAYMODE}" == "yamaha" ]]; then
echo "> Turn on AVR..."
curl -s "http://${AVRHOST}/YamahaExtendedControl/v1/main/setPower?power=on" >/dev/null
echo "> Set AVR input to ${AVRINPUT}"
curl -s "http://${AVRHOST}/YamahaExtendedControl/v1/main/setInput?input=${AVRINPUT}" > /dev/null
echo "> Set initial volume (${VOL_START})"
curl -s "http://${AVRHOST}/YamahaExtendedControl/v1/main/setVolume?volume=${VOL_START}" >/dev/null
fi
echo "> Starting playback via mpv..."
\mpv --really-quiet --loop=${SONGLOOP} --input-ipc-server="${MPV_IPC_PATH}" --audio-device="${PASINK}" --volume="${MPV_VOL_INIT}" "${SONGPATH}" & disown
echo "*** Hit Ctrl+C to silence ***"
echo -en "Volume: "
volume=${VOL_START}
while true; do
# wait
sleep ${VOL_WAIT}
# send a command just to ensure mpv is alive
mpv_cmd get_property time-pos
# pump up the volume
let "volume = volume + ${VOL_STEP}"
if [ $volume -ge $VOL_MAX ]; then
volume=$VOL_MAX
echo -en "\n** MAX VOLUME REACHED **\n"
continue
fi
# set volume on AVR
if [[ "${PLAYMODE}" == "yamaha" ]]; then
curl -s "http://${AVRHOST}/YamahaExtendedControl/v1/main/setVolume?volume=${volume}" >/dev/null
else
mpv_cmd set_property volume ${volume}
fi
echo -en "#"
done
cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment