Skip to content

Instantly share code, notes, and snippets.

@bmatthewshea
Created June 3, 2020 21:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmatthewshea/1ac0ce6157aa5a3f2f88ed4427e61153 to your computer and use it in GitHub Desktop.
Save bmatthewshea/1ac0ce6157aa5a3f2f88ed4427e61153 to your computer and use it in GitHub Desktop.
Sound activated recording using SOX
#!/bin/bash
tempfile1=/tmp/temp.mp3
# Baremetal/Realtek analog etc
AUDIODEV=hw:0,0
# Pi USB mic (usually):
# AUDIODEV=hw:1,0
set_filenames () {
NAME=`date +%Y-%m-%d_%H-%M-%S`
FILENAME="/tmp/${NAME}.mp3"
#FILENAME="/var/www/Recordings/$NAME.mp3"
}
cleanup_vox () {
[ -f "$tempfile1" ] && rm -rf $tempfile1
}
start_vox () {
cleanup_vox
sox -t alsa -d "$tempfile1" silence 1 0 10% 1 0:00:04 10%
#wait
}
normalize_vox () {
[ -r "$FILENAME" ] && \
sox $FILENAME /tmp/${NAME}-norm.mp3 gain -n -1
}
filesize_vox () {
if [ -r "$tempfile1" ]; then
for i in "$tempfile1" ; do
b=`stat -c %s "$i"`
if [ $b -ge 390 ] ; then
set_filenames
cp "$tempfile1" "$FILENAME"
normalize_vox
break
fi
done
fi
}
# Execute functions in infinite loop
while :
do
start_vox && \
sleep 1 # so you can exit loop easily w/ ctrl-c
filesize_vox && \
cleanup_vox && \
echo
sleep 1 # so you can exit loop with ctrl-c
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment