Skip to content

Instantly share code, notes, and snippets.

@trianglesis
Created April 17, 2024 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trianglesis/8e73dc7e84c4dfbc6baa2017977f09d1 to your computer and use it in GitHub Desktop.
Save trianglesis/8e73dc7e84c4dfbc6baa2017977f09d1 to your computer and use it in GitHub Desktop.
Simple script to play a WAV notification sound
#!/bin/bash
# Play notification sound at givel volume with given file
PATH_TO_NOTIFICATIONS="/home/USER/Music/notifications"
PATH_TO_DING="/home/USER/Music/notifications/stop.wav"
verbose=$3
sound_volume=$1
file_to_play=$2
play_sound_notification(){
# No verbose by default, and verbose if any arg!
if [ -z "$verbose" ]; then
verbose=false
else
verbose=true
echo "DEBUG: mode at any arg: $verbose"
fi
# Volume check if integer:
if [[ "$sound_volume" =~ ^[0-9]+$ || "$sound_volume" =~ ^[-][0-9]+$ ]]; then
# Volume check if GTE 101 or LT 1:
if [[ $sound_volume -ge 101 ]] || [[ $sound_volume -lt 0 ]]; then
echo "ERROR: Volume is incorrect not 1-100: $volume"
return 0
fi
# All correct:
if $verbose ; then echo "DEBUG: volume is OK: $volume"; fi
else
echo "ERROR: Volume is incorrect, not an integer: $volume"
return 0
fi
# Check if file exsists:
file_full_path="$PATH_TO_NOTIFICATIONS/$file_to_play"
if [ -f $file_full_path ]; then
if $verbose ; then echo echo "DEBUG: file exists: $file_full_path"; fi
else
echo "File: $file_to_play"
echo "ERROR: The file does not exist at path with this name: $file_full_path"
return 0
fi
# Set volume:
if $verbose; then
echo "Set volume at level: $sound_volume"
echo "============================"
amixer set Master $sound_volume%
echo "============================"
echo "Play initial ding to wake up USB Sound device: $PATH_TO_DING"
aplay --device="dmix:CARD=Audio,DEV=0" -q --nonblock --duration=1 $PATH_TO_DING
echo "Now play requested notification: $file_full_path"
aplay --device="dmix:CARD=Audio,DEV=0" -q $file_full_path
echo "Set volume back at level 10%"
echo "============================"
amixer set Master 10%
echo "============================"
else
amixer set Master $sound_volume% >> /dev/null
aplay --device="dmix:CARD=Audio,DEV=0" -q --nonblock --duration=1 $PATH_TO_DING
aplay --device="dmix:CARD=Audio,DEV=0" -q $file_full_path
amixer set Master 10% >> /dev/null
fi
# Play notification:
# End and exit
return 0
}
play_sound_notification $sound_volume $file_to_play $verbose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment