Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 133 lines (115 sloc) 2.23 KB
#!/usr/bin/env bash
#
# Play and stop radio automagically.
# Check for holidays and user disabling.
#
# Author: Rafael Cavalcanti
#
HOLIDAYS="/opt/rc/data/holidays.txt"
DISABLED_DATES="/opt/rc/ra_disabled.txt"
SILENT_TRACK="http://files.pi.lan/mpd_wa/silence.ogg"
PA_SINK="alsa_output.usb-GeneralPlus_USB_Audio_Device-00.analog-stereo"
ALSA_CARD="1"
ALSA_MIXER="Speaker"
RADIO_API="http://radio.lan/api"
MPC="mpc -q"
CURL="curl -sS"
PACTL="pactl"
usage() {
cat <<-END >&2
Usage:
$(basename "$0") --start volume stream [backup_stream]
$(basename "$0") --stop volume
END
exit 1
}
check_holiday() {
if grep -q "$(date +%Y-%m-%d)" "$HOLIDAYS"; then
printf "Today is a holiday. Not doing anything.\n" >&2
exit 0
fi
}
check_disabled() {
if grep -q "$(date +%Y-%m-%d)" "$DISABLED_DATES"; then
printf "Radioauto was disabled for today. Not doing anything.\n" >&2
exit 0
fi
}
# Set volume accordingly depending on which sound system is used
# (PulseAudio or ALSA).
#
# args: volume
set_volume() {
if $PACTL list >/dev/null 2>&1; then
set_volume_pa "$1"
else
set_volume_alsa "$1"
fi
}
# Set volume when using ALSA.
#
# args: volume
set_volume_alsa() {
amixer -c "$ALSA_CARD" set "$ALSA_MIXER" unmute >/dev/null
$MPC volume "$1"
}
# Set volume when using PulseAudio.
# It works even if MPC doesn't have a source on PulseAudio.
#
# args: volume
set_volume_pa() {
# Make sure PA sink isn't mute and it's at 100%.
$PACTL set-sink-mute "$PA_SINK" false
$PACTL set-sink-volume "$PA_SINK" 100%
# Set volume opening the source first
$MPC clear
$MPC add "$SILENT_TRACK"
$MPC play
sleep 10
$MPC volume "$1"
$MPC clear
}
# args: stream backup_stream
start_playback() {
$CURL \
-F "cmd=play_radios" \
-F "names[]=$1" \
-F "names[]=${2:-$1}" \
$RADIO_API
}
stop_playback() {
$MPC stop
}
# args: volume stream backup_stream
start() {
check_holiday
check_disabled
set_volume "$1"
start_playback "$2" "$3"
}
# args: volume
stop() {
check_holiday
check_disabled
stop_playback
set_volume "$1"
}
main() {
[[ $# == 0 ]] && usage
local command="$1"
shift
case "$command" in
--start)
[[ $# != 2 && $# != 3 ]] && usage
start "$@"
;;
--stop)
[[ $# != 1 ]] && usage
stop "$@"
;;
*)
usage
;;
esac
}
main "$@"
You can’t perform that action at this time.