summaryrefslogtreecommitdiff
path: root/speakerSqueaker.sh
blob: 71a7edeaf15b8bfa796d6582a2d6760dad9a6159 (plain)
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
55
56
57
58
59
60
61
62
63
64
65
#! /bin/bash
# Workaround for stupid 'powersave' 'feature' of Creative Speakers
#
# SJP 11 Dec 2021
#
# Dependencies:     sox (required)
#                   xprintidle (not required)
#
# Record a snippet of default stream:
#    rec       sox in record mode
#    -d        default input
#    -n        direct output to /dev/null
#    trim 0 .1 duration 0.1 second
#    stat      write info about the audio stream
#    2>&1      redirect stderr to stdout 
#    |         pipe
#    grep      finds this line:    Minimum amplitude:     0.000000
#    |         pipe
#    awk       ruturns just the third word (the number)

maxAmp=$(rec -d -c1 -n trim 0 .5 stat 2>&1 | grep 'Maximum amp' | awk {'print $3'})

# xprintidle is logfile eyecandy. Not necessary for the script to work
echo $(xprintidle)  Max Amplitude = "$maxAmp" | \
    systemd-cat -t "idle time (msec) & audio level " -p notice
    
timerMins=10m       # default timer duration

if [ "$maxAmp" == "0.000000" ]; then
    # We have silence
    if [ ! -f "/tmp/silencemarker" ] ; then
        # This is the first instance of silence after detecting noise
        touch /tmp/silencemarker
    fi
    # Play the tone.
    # Even though there may have been some sounds played since the previous
    # observation, let's play it safe.
    echo 'Play inaudible tone (20 Hz for 2 sec)'
    play -nq synth 2 sine 20 fade 0.4 0
else 
    # Sound was detected so remove the marker file if it is there
    if [ -f "/tmp/silencemarker" ] ; then
        rm /tmp/silencemarker
    fi
fi

# Set a timer. Accuracy is 'per-minute' so the interval may be
# as long as 11 minutes. This is ok for Creative Gigaworks; they turn off after 11+mins.
systemd-run --user --on-active=${timerMins} /usr/local/bin/speakerSqueaker.sh

#TODO: possibly allow actual turn off if no sound has been heard for a 'long' time
#TODO: Measure actual power consumption to see how futile erp actually is
# http://eco3e.eu/regulations/erp-directive/


#to generate a tone that is not audible:
#play -n  synth 1 sine  40 fade .2 0
#
#  play    sox alias
#  -n      dummy input file
#  synth   synthesise a sound
#  sine    sine wave [the default, so not strictly necessary]
#  40      Hz
#  fade    fade in from zero volume to max volume over 200 msec.
#          '0' says fade out at end over same period.