Home HiFi Electronics Audio Test & Measurement for DIY IT Stuff Downloads Bookmarks

Audio testing scripts for Linux

Audio testing scripts for Linux (all scripts require 'sox' and 'alsa*' - On Debian or Pi - apt install sox)
*alsa is usually installed by default.
Along with Windows software, I often use Linux for audio testing.
I have written a few basic scripts to help with this.
To find the name of your audio interface - aplay -l (playback) and arecord -l (record)
The following script will generate a sine tone using sox:

#!/bin/bash
#
# edit to change bits (usually not required)
#
bits="24"
# should make this a command line parameter
playback_dev="hw:1,0"
#
if [ $# -ne 4 ]; then
echo 1>&2 "Usage: $0 Frequency(Hz) Duration(s) Sample Rate(Hz) Level(dB)"
echo "e.g. $0 1000 10 96000 -3 = 1kHz tone for 10s 96k sample rate at -3dB"
exit 3
fi
#
frequency=${1}
duration=${2}
sample_rate=${3}
level=${4}
#
echo "Playing a ${1}Hz / ${3}Hz tone at ${4}dB for ${2} seconds..."
#
AUDIODEV=$playback_dev play -r $sample_rate -n -b $bits -c 2 synth $duration sin $frequency vol ${level}dB > /dev/null 2>&1
#
#

The following script can be used to set input levels.
It requires 'parallel' (on Debian or Pi - apt install parallel)

#!/bin/bash
#
if [ $# -ne 2 ]; then
echo 1>&2 ""
echo 1>&2 "Usage: $0 Sample Rate(Hz) Level(dB)"
echo "e.g. $0 96000 -3 = 96k sample rate at -3dB"
echo 1>&2 ""
exit 3
fi
#
sample_rate=${1}
level=${2}
duration=2
datafile=/tmp/$0_output.txt
message="Set levels to ${level}dB: (wait $duration seconds for levels to appear) [ hit CTRL+C to stop ]"
rm -f $datafile
clear
echo $message
#
for i in {1..50}
do
parallel ::: "AUDIODEV=hw:3,0 play -r $sample_rate -n -b 24 -c 2 synth $duration sin 1000 vol ${level}dB > /dev/null 2>&1" "sox -b 24 -r $sample_rate -c 2 -t alsa hw:2,0 ${i}.wav trim 0 $duration > /dev/null 2>&1"
amplitude=$(sox ${i}.wav -n stats 2>&1 | grep "Pk lev dB")
echo $amplitude > $datafile
rm -f ${i}.wav
clear
echo ${message} "loop" $i
cat $datafile
done
echo "Done"
rm -f $datafile


Related items: