Home HiFi Electronics Audio Test and Measurement for DIY IT Stuff Downloads Bookmarks WaveSpectra and WaveGene mirror

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

Note: This is presented as a bash script to maintain syntax highlighting - but the commands below are intended to be used standalone.

#!/bin/bash
#
# some examples to play and record audio using arecord and sox
# all examples process 24 or 32 bits of audio at 96khz
# aplay -l will list playback devices
# arecord -l will list capture devices
#
#
# arecord - capture 60 seconds of 2 channel audio from hw:2,0 and write it to tmpwav.wav
#
arecord -D hw:2,0 -c 2 -r 96000 -fS24_3LE -d 60 tmpwav.wav
#
# sox - capture 60 seconds of 2 channel audio from hw:2,0 and write it to tmpwav2.wav
#
sox -b 24 -r 96000 -c 2 -t alsa hw:2,0 tmpwav2.wav trim 0 60
#
# generate a tone using play (part of sox)
# generate a sinewave - this will generate a 2 channel 1khz sinewave at -9dB
# of 100 seconds duration on playback hardware device 3,0 using a sample rate of 96khz
#
AUDIODEV=hw:3,0 play -r 96000 -n -b 24 -c 2 synth 100 sin 1000 vol -9dB
#
# create a file with the above instead of generating a tone
#
sox -b 32 -r 96000 -c 2 -n soundfile.wav synth 100 sine 1000 vol -9dB
#
# play a .wav file with aplay - the "-f" switch here is crucial (as with arecord above) and will vary according to your hardware
# most likely options are S24_3LE or S32_LE (there are many more - see the aplay man page for details)
#
aplay -D hw:3,0 -f S24_3LE [filename]
#
# do the same with the play (sox) utility
#
AUDIODEV=hw:3,0 play [filename]
#
# the AUDIODEV variable is not required is .asoundrc is configured correctly.
# AUDIODEV could also be set in the environment, e.g. in .bashrc e.g.
export AUDIODEV=hw:0,0
#

Related items: