Last active
January 20, 2023 20:39
-
-
Save thehunmonkgroup/f7eb52913c4049d5e35007def3bfe026 to your computer and use it in GitHub Desktop.
Bash script to play a sound file every X seconds
This file contains hidden or 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
| #!/usr/bin/env bash | |
| progname=$(basename ${0}) | |
| command=${1} | |
| default_seconds="60" | |
| default_volume="1" | |
| default_filepath="${HOME}/attention.wav" | |
| usage() { | |
| echo " | |
| Usage: attention.sh start [seconds] [file] [volume] | |
| attention.sh stop [file] | |
| Simple bash script to play a file every X seconds. | |
| The 'stop' command is simply a convenience function if the script was | |
| put in a background job, eg. '${progname} start &'. | |
| seconds: Optional, play file every N seconds, defaults to ${default_seconds}. | |
| Can also be set via the ATTENTION_SECONDS environment variable. | |
| file: Optional, file to play, defaults to ${default_filepath} for the | |
| start action, and no file for the stop action. | |
| For the start action, can also be set via the ATTENTION_FILEPATH | |
| environment variable. | |
| volume: Optional, play file at specified relative volume. Less than 1 | |
| decreases, greater than 1 increases, defaults to ${default_volume}. | |
| Can also be set via the ATTENTION_VOLUME environment variable. | |
| CAVEATS: | |
| The sox 'play' binary must be installed and in your PATH, it will play the | |
| sound through the default audio device. | |
| For PulseAudio users, it is recommended to install the 'libsox-fmt-pulse' | |
| package as well, to prevent issues with sox/PulseAudio integration. | |
| " | |
| } | |
| if [ "$(which play)" = "" ]; then | |
| echo "ERROR: no 'play' binary found" | |
| echo | |
| usage | |
| exit 1 | |
| fi | |
| if [ $# -lt 1 ]; then | |
| usage | |
| exit 1 | |
| fi | |
| if [ "${command}" = "start" ]; then | |
| if [ $# -gt 3 ]; then | |
| usage | |
| exit 1 | |
| fi | |
| if [ -n "${2}" ]; then | |
| seconds="${2}" | |
| else | |
| seconds="${ATTENTION_SECONDS:-${default_seconds}}" | |
| fi | |
| if [ -n "${3}" ]; then | |
| filepath="${3}" | |
| else | |
| filepath="${ATTENTION_FILEPATH:-${default_filepath}}" | |
| fi | |
| if [ -n "${4}" ]; then | |
| volume="${4}" | |
| else | |
| volume="${ATTENTION_VOLUME:-${default_volume}}" | |
| fi | |
| elif [ "${command}" = "stop" ]; then | |
| if [ $# -gt 2 ]; then | |
| usage | |
| exit 1 | |
| fi | |
| if [ -n "${2}" ]; then | |
| filepath="${2}" | |
| fi | |
| fi | |
| log() { | |
| message="${1}" | |
| echo "${message}" | |
| logger "${message}" | |
| } | |
| start() { | |
| log "Playing ${filepath} every ${seconds} seconds, volume: ${volume}" | |
| while true; do | |
| sleep ${seconds} | |
| play -q --volume ${volume} ${filepath} 2> /dev/null | |
| done | |
| } | |
| stop() { | |
| log "Stopping ${progname}" | |
| if [ -n "${filepath}" ]; then | |
| play -q "${filepath}" & | |
| fi | |
| pkill -f -9 ${progname} | |
| } | |
| case ${command} in | |
| start) | |
| start | |
| ;; | |
| stop) | |
| stop | |
| ;; | |
| *) | |
| usage | |
| exit 1 | |
| ;; | |
| esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment