Skip to content

Instantly share code, notes, and snippets.

@koppi
Last active April 23, 2022 19:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koppi/60b9d1f14b0af2bdde1e49b9c225649d to your computer and use it in GitHub Desktop.
Save koppi/60b9d1f14b0af2bdde1e49b9c225649d to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#
# mpd fade in / out script:
#
# fades the current mpc volume to the given <end volume>
# within the given <duration in secs>
#
# Required tools:
#
# sudo apt -y install mpc bc
#
# Typical usage in shell scripts or cronjobs:
#
# my /usr/local/bin/mpc-wakeme-up.sh looks like follows:
#
#!/usr/bin/env bash
#
# # play some playlist for 5 min, with 30 sec fadein and 5 sec fadeout
#
# mpc stop
# mpc clear
# mpc volume 15 # start with volume 15%
# mpc load "Radio Fantasy" # load a playlist
# mpc play
# mpc-fade 60 30 # fade to volume 60% within 30 sec
# sleep $((60*5)) # play for 5 min
# mpc-fade 0 5 # fade to volume 0% within 5 sec
# mpc stop
#
# EOF /usr/local/bin/mpc-wakeme-up.sh
readonly PROGRAM_FILE="$0"
readonly PROGRAM_NAME="$(basename $0)"
#echo >&2 "$PROGRAM_NAME: started from '$PROGRAM_FILE' with options: $*"
if [ $(( ${BASH_VERSINFO[0]} )) -lt 4 ]
then
echo >&2
echo >&2 "$PROGRAM_NAME: ERROR"
echo >&2 "BASH version 4 or later is required."
echo >&2 "You are running version: ${BASH_VERSION}"
echo >&2 "Please upgrade."
echo >&2
exit 1
fi
require_cmd() {
which "$1" >/dev/null
if [ $? -ne 0 ]
then
echo >&2 "$PROGRAM_NAME: ERROR: Command '$1' is not found in the system path."
return 1
fi
return 0
}
require_cmd sleep || exit 1
require_cmd bc || exit 1
require_cmd mpc || exit 1
MPC=`which mpc`
BC="`which bc` -l"
if [ $# -ne 2 ] ; then
echo "usage: mpc-fade <end volume> <duration in secs>" ;
exit 1 ;
fi
VOL=`$MPC volume | sed -e 's/[^0-9]*\([0-9]*\).*/\1/'`;
if [[ $VOL -eq $1 ]]; then
echo "Volume $VOL already reached. Exiting." ;
exit 0 ;
fi
D=`echo "$2/($VOL-$1)" | $BC`
D=${D#-} # sleep duration absolute value
if [[ $VOL -lt $1 ]] ; then
while [[ $VOL -le $1 ]] ; do
$MPC volume $VOL > /dev/null ;
VOL=$(($VOL + 1)) ;
sleep $D;
done ;
elif [[ $VOL -gt $1 ]] ; then
while [[ $VOL -ge $1 ]] ; do
$MPC volume $VOL > /dev/null ;
VOL=$(($VOL - 1)) ;
sleep $D;
done ;
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment