Questa soluzione eviterà di scrivere ripetutamente su disco, e anche se nel peggiore dei casi impiega un secondo invece del desiderato meno di mezzo secondo, l'ho trovato abbastanza veloce dopo averlo provato. Quindi, ecco i due script che uso:
./detect:
while true; do
arecord -d 1 /dev/shm/tmp_rec.wav ; sox -t .wav /dev/shm/tmp_rec.wav -n stat 2>\
&1 | grep "Maximum amplitude" | cut -d ':' -f 2 | ./check.py
if [ $? -eq 0 ] ; then
amixer set Master 0
else
amixer set Master 80
fi
done
./check.py:
#!/usr/bin/env python
import sys
number = 0.0
thing="NO"
line = sys.stdin.readline()
thing = line.strip()
number = float(thing)
if number < 0.15:
raise Exception,"Below threshold"
Poco elegante, ma funziona.
Nota:se vuoi qualcosa di più graduale, aggiungi qualcosa del genere:
for i in `seq 0 80 | tac`; do
amixer set Master $i
done
per silenziare e
for i in `seq 0 80`; do
amixer set Master $i
done
per riattivare.
Solo la versione senza script python e TALKING_PERIOD, che imposta quanti secondi suoneranno al livello DOWN_SOUND_PERC, quindi passa al livello UP_SOUND_PERC.
#!/bin/bash
TALKING_PERIOD=16
UP_SOUND_PERC=65
DOWN_SOUND_PERC=45
counter=0
while true; do
echo "counter: " $counter
if [ "$counter" -eq 0 ]; then
nmb=$(arecord -d 1 /dev/shm/tmp_rec.wav ; sox -t .wav /dev/shm/tmp_rec.wav -n stat 2>&1 | grep "Maximum amplitude" | cut -d ':' -f 2)
echo "nmb: " $nmb
if (( $(echo "$nmb > 0.3" |bc -l) )); then
echo "ticho"
amixer -D pulse sset Master 45%
counter=$TALKING_PERIOD
else
echo "hlasno"
amixer -D pulse sset Master 65%
fi
fi
if [[ $counter -gt 0 ]]; then
((counter--))
fi
sleep 1
fatto