Skip to content

Instantly share code, notes, and snippets.

@brandon-lockaby
Created January 5, 2023 11:03
Show Gist options
  • Save brandon-lockaby/0d635a7b12097d3e7c93cbbe89a7c4db to your computer and use it in GitHub Desktop.
Save brandon-lockaby/0d635a7b12097d3e7c93cbbe89a7c4db to your computer and use it in GitHub Desktop.
make a record of loud noise
#!/bin/bash
# Set the threshold, duration (in seconds), and recording directory
threshold=0.2
duration=10
recording_dir="/mnt/tera/audio/NOISE2"
# Set output options
output_to_console=true
output_to_log=true
# Set trap to handle SIGINT (CTRL+C) signal
trap "echo Exiting...; exit" SIGINT
while :
do
# Record audio for the specified duration
timeout "$duration" arecord -d "$duration" -f S16_LE -c 1 -r 44100 audio.wav &> /dev/null &
pid=$!
# Wait for the arecord command to complete
wait $pid
# Calculate the average audio level
level=$(sox audio.wav -n stat 2>&1 | grep "Maximum amplitude" | awk '{print $3}')
now=$(date +"%Y.%m-%d.%T")
# Save the file and log the timestamp and maximum amplitude
log_entry="$now $level"
if (( $(echo "$level > $threshold" | bc -l) )); then
mv audio.wav "$recording_dir/$now $level.wav"
fi
if $output_to_console; then
echo "$log_entry"
fi
if $output_to_log; then
echo "$log_entry" >> "$recording_dir/audio.log"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment