Skip to content

Instantly share code, notes, and snippets.

@dolcini
Created December 4, 2023 21:43
Show Gist options
  • Save dolcini/a570adc2e028e9f1ed4129ad85c8c446 to your computer and use it in GitHub Desktop.
Save dolcini/a570adc2e028e9f1ed4129ad85c8c446 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# This test checks on analog audio:
# recording mic/line
# playing the recorded file and playing a wav test-file
#
# This test checks if it can play on hdmi-audio (not on apalis-imx6 due to
# lacking upstream support)
#source ./tests/testHelpers.sh
somfamily=verdin
reportResult()
{
echo reportResult $*
}
test_name="audio-simple"
tmp_dir="$(mktemp -d)"
# This function takes one argument which is a non ambiguous substring of the
# name and returns the number of the card associated with it using
# /proc/asound/cards
# $1 name e.g. "sgtl5000audio"
# return 0 on success, 1 otherwise (string not found)
get_card_number_by_name()
{
local name=$1
grep -Po -m1 "^ \K\d(?= \[[\w ]+\]\:.*${name})" /proc/asound/cards
}
# records a file and checks if the expected time is within a second
# $1 card name to play on (e.g. "hdmi")
# $2 channels
# $3 time in seconds
# $4 record filename
record_and_check_size()
{
local card=$1
local channels=$2
local time=$3
local file=$4
local filename
filename=$(basename "$file")
arecord -D hw:"$card",0 -c $channels -f S16_LE -r 44100 -t wav -d $time $file
reportResult $? "${test_name}-arecord-card${card}-${channels}ch-${filename}"
# WAV header size 44bytes
expected_size=$(( 44100*16*channels*time/8 + 44 ))
real_size=$(stat -c%s "$file")
# WAV file is aligned to even size, for 8bit mono recording this check would be not correct.
# See https://github.com/certik/record/blob/master/arecord.c#L970
[ "$expected_size" -eq "$real_size" ]
reportResult $? "${test_name}-arecord-card${card}-${channels}ch-${filename}-sizecheck" "$real_size" "bytes"
}
# Plays a file and checks if the expected time is within a second
# $1 file to play
# $2 card name to play on (e.g. "hdmi")
# $3 expected time in seconds
# return 0 on success, 1 otherwise
play_and_check_time()
{
local file=$1
local cardname=$2
local time_desired=$3
local cardnumber
local time_actual
local result
local filename
filename=$(basename "$file")
cardnumber=$(get_card_number_by_name "$cardname")
reportResult $? "${test_name}-get_card_number_of-${cardname}"
SECONDS=0
time aplay -D hw:"$cardnumber",0 "$file"
result=$?
time_actual=$SECONDS
reportResult $result "${test_name}-${cardname}-aplay-${filename}"
[ "$time_actual" -ge $((time_desired-1)) ] && \
[ "$time_actual" -le $((time_desired+2)) ]
reportResult $? "${test_name}-${cardname}-playtime-${filename}" "$time_actual" "s"
}
# This function does record from mic and linein and try to play everything plus
# playing Gong.wav
# $1 cardname
# no return value, does fail if check fails
check_audio()
{
local cardname=$1
card=$(get_card_number_by_name "$cardname")
reportResult $? "${test_name}-get_card_number_of-${cardname}"
if [ "$cardname" = "wm8904" ]; then
amixer -c "$card" set 'Left Capture Inverting Mux' 'IN1L'
reportResult $? "${test_name}-${cardname}-amixer-IN1L"
amixer -c "$card" set 'Right Capture Inverting Mux' 'IN1R'
reportResult $? "${test_name}-${cardname}-amixer-IN1R"
amixer -c "$card" set 'Capture' '31'
reportResult $? "${test_name}-${cardname}-amixer-capture-31"
# It seems WM8904 cannot record 1 channel
record_and_check_size "$card" 2 3 "${tmp_dir}/mic.wav"
amixer -c "$card" set 'Left Capture Inverting Mux' 'IN2L'
reportResult $? "${test_name}-${cardname}-amixer-IN2L"
amixer -c "$card" set 'Right Capture Inverting Mux' 'IN2R'
reportResult $? "${test_name}-${cardname}-amixer-IN2R"
amixer -c "$card" set 'Capture' '16'
reportResult $? "${test_name}-${cardname}-amixer-capture-16"
record_and_check_size "$card" 2 5 "${tmp_dir}/line.wav"
play_and_check_time "${tmp_dir}/mic.wav" "$cardname" 3
play_and_check_time "${tmp_dir}/line.wav" "$cardname" 5
elif [ "$cardname" = "sgtl5000" ]; then
amixer -c "$card" set "Capture Mux" "MIC_IN"
reportResult $? "${test_name}-${cardname}-amixer-MIC_IN"
record_and_check_size "$card" 1 3 "${tmp_dir}/mic.wav"
amixer -c "$card" set "Capture Mux" "LINE_IN"
reportResult $? "${test_name}-${cardname}-amixer-LINE_IN"
record_and_check_size "$card" 2 5 "${tmp_dir}/line.wav"
play_and_check_time "${tmp_dir}/mic.wav" "$cardname" 3
play_and_check_time "${tmp_dir}/line.wav" "$cardname" 5
fi
play_and_check_time /home/root/sound/Gong.wav "$cardname" 6
}
# Do not execute this test on the following modules:
# apalis-tk1: I didn't even bother trying to make it work
# colibri-imx6ull*: has no soundcard
if [ "$machine" = "apalis-tk1" ] || \
[ "$machine" = "colibri-imx6ull" ] || \
[ "$machine" = "colibri-imx6ull-emmc" ]
then
lava-test-case "$test_name" --result skip
exit
fi
# log available cards
echo "Entries of /proc/asound/cards:"
cat /proc/asound/cards
case "$somfamily" in
"apalis")
check_audio sgtl5000
if [ "$machine" != "apalis-imx6" ]; then
# Do not check hdmi-audio on apalis-imx6 as upstream lacks
# support for this.
check_audio hdmi
fi
;;
"colibri")
check_audio sgtl5000
;;
"verdin")
check_audio wm8904
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment