Disclaimer - I'm a novice at bash scripting so I'm sure it can be improved but its been working as desired and reliably for several days now so thought I'd share it.
My project was to have my doors play different holiday music clips when they were opened. So, I ditched my legacy security system and hooked my door contact sensors up to a Raspberry Pi. I then connected powered speakers to the 3.5mm audio plug on the Pi and called the script in Domoticz Events (Blocky) to trigger sounds when the doors open. This version of the script has options for volume control, audio delay between playing sound files and plays the list of arguments as audio files. If an argument of a filename isn't present or isn't valid, it plays the default sound.
This script, adjusts the master volume to desired level (if -v option is present), then plays the list of audio wav filenames supplied using aplay with the ability to add a delay between file plays (if -w option is present). In my case, I hardcoded the path to the audio files to a mounted NFS share on my Synology NAS (this path would have to be changed as appropriate for other use cases). I tested it with a Bluetooth as well, using bluealsa connected to an Amazon Alexa Dot. I found it to be temperamental, the sound was broken at times and if more than one sound event trigger overlapped it would hang aplay. I didn't spend a lot of time troubleshooting the bluetooth but left the line commented in the script below. I used the aplay -d options to limit play length to a short duration no matter the length of the actual wav file.
I used http://www.fromtexttospeech.com/ to create a standard door open announcement for when it's not a holiday or special event.
Now that I have the basic audio function working my next goal is to find an inexpensive way to distribute the audio to the locations where my legacy security system keypads hung so I don't have to blast the audio from centrally located speakers.
Syntax:
./filename.sh [-v VolumeLevel] [-w WaitDelay] FileListArgs
#!/bin/bash
#Check for Volume Option and set
while getopts v:w: name
do
case $name in
v)vol=$OPTARG;;
w)wait=$OPTARG;;
*)echo "Invalid arg";;
esac
done
if [[ ! -z $vol ]]
then
amixer sset 'PCM' $vol
fi
shift $(($OPTIND -1))
#Play audio file
for i do
FILE="/home/pi/domoticz/scripts/HA/audio_files/""$i"".wav"
if [ "$wait" ]; then
#aplay -D bluealsa:DEV=xx:xx:xx:xx:xx:Xx -t wav -N -d 12 $FILE &
aplay -t wav -N -d 18 $FILE &
sleep $wait
elif [ -f "$FILE" ]; then
#aplay -D bluealsa:DEV=xx:xx:xx:xx:xx:Xx -t wav -N -d 12 $FILE &
aplay -t wav -N -d 18 $FILE &
else
#aplay -D bluealsa:DEV=xx:xx:xx:xx:xx -t wav -N -d 10 /home/pi/domoticz/scripts/HA/audio_files/NoAudioFileError.wav &
aplay -t wav -N -d 18 /home/pi/domoticz/scripts/HA/audio_files/NoAudioFileError.wav &
fi
done
Simple Play Audio Script for Door Contact Sensors or any triggered event
Moderator: leecollings
-
- Posts: 11
- Joined: Friday 29 June 2018 13:42
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 4.9700
- Contact:
Simple Play Audio Script for Door Contact Sensors or any triggered event
- Attachments
-
- Blocky Capture.PNG (85.22 KiB) Viewed 1942 times
Who is online
Users browsing this forum: No registered users and 1 guest