-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio_is_playing.sh
54 lines (51 loc) · 1.62 KB
/
audio_is_playing.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
#maximum value of sample before its classified as not-silence
volume_threshold=10
#number of loops with no signal before changing state from playing to silence
silence_delay=6
#delay in seconds to wait at the end of each loop
loop_delay=5
#file to log state changes
logfile="/tmp/audioisplaying.log"
log () {
timestamp=`date +"%Y-%m-%d_%H-%M-%S"`
echo "$timestamp : $1" >> "$logfile"
}
echo "1" > /tmp/audioisplaying
counter=0
power="1"
log "Starting to monitor audio output"
while :
do
volume=`parec --device=1 --latency=2 --channels=1 2>/dev/null | od -N2 -td2 | head -n1 | cut -d' ' -f2- | tr -d ' ' | perl -pe 's/^-//'`
oldpower="$power"
if [ "$volume" -lt "$volume_threshold" ]
then
echo "quiet"
((counter++))
else
echo "heard something"
counter=0
fi
if [ "$counter" -gt "$silence_delay" ]
then
echo "announcing silence"
power="0"
if [ "$oldpower" != "$power" ]
then
log "signal switching off"
fi
echo "0" > /tmp/audioisplaying
ssh [email protected] /home/stephen/ener_s1_off.sh
else
echo "announcing signal"
power="1"
if [ "$oldpower" != "$power" ]
then
log "signal switching on"
fi
echo "1" > /tmp/audioisplaying
ssh [email protected] /home/stephen/ener_s1_on.sh
fi
sleep "$loop_delay"
done