#!/bin/bash
#
# RJP 14.2.2025  audio card test script
# Packages alsa and yad must be installed
# 15.2.2025 Test with all options as all choises
# 19.3.2025 PCLinuxOS version which uses /usr/share/webkitgtk-3.0/resources/audio/Composite.wav file as test sound
# 23.3.2025 basic an full test together
# 14.6.2026 new version with changed dialogs


touch ~/.Xresources
echo ' ! Use a truetype font and size.
xterm*faceName: Monospace ' | grep -xFv -f $HOME/.Xresources >> $HOME/.Xresources
echo 'xterm*faceSize: 14 ' | grep -xFv -f $HOME/.Xresources >> $HOME/.Xresources
xrdb -merge ~/.Xresources


aplay -l | awk -F \: '/,/{print $2}' | awk '{print $1}' | uniq > /tmp/adevice-full.txt
AUDIO=$(/tmp/adevice-full.txt)
choose=$(cat /tmp/adevice-full.txt | \
yad --list --text="Choose audio device" --title="Choose audio device" --height=500 --width=600 --center \
--column="")
--button=Exit:1
cat "$choose" 2> /tmp/audio2.txt
cat /tmp/audio2.txt | awk '{print $2}' > /tmp/audiodevice
cat /tmp/audiodevice | sed 's/^.//' > /tmp/audiodevice3
sed 's/.$//' /tmp/audiodevice3 > /tmp/audiodevice4
sed 's/.$//' /tmp/audiodevice4 > /tmp/audiodevice5
sed 's/.$//' /tmp/audiodevice5 > /tmp/audio.txt
#
yad --center --width=300 --height=100 --button=OK:1 --text-align=center --title=$"Basic test" --text="Test selected card"
if [ $? = 1 ];
then
xterm -e "aplay -D plughw:CARD=$(cat /tmp/audio.txt) /usr/share/webkitgtk-3.0/resources/audio/Composite.wav" & \
#
# Message if audio was running or not
#
sleep 0,5 && (ps -ef|grep -v grep|grep aplay >/dev/null && echo "If you heard noise, the card is the right. You can close this message" || echo "if you did not heard noise, you probably has the wrong audio device. You can close this message") > /tmp/message.txt
yad --center --button=OK:1 --width=700 --height=200 --text-align=center --title=$"Basic test info" --text="INFO: $(cat /tmp/message.txt)"
#
fi
sleep 1
rm /tmp/audio2.txt /tmp/adevice.txt /tmp/audiodevice3 /tmp/audiodevice4 /tmp/audiodevice4 /tmp/audiodevice5 /tmp/audio.txt  

aplay -L | grep :CARD > /tmp/adevice-full.txt
AUDIO=$(/tmp/adevice-full.txt)
choose=$(cat /tmp/adevice-full.txt | \
yad --list --text="Choose audio device" --title="Choose audio device" --height=500 --width=600 --center \
--column="")

cat "$choose" 2> /tmp/audio2.txt
cat /tmp/audio2.txt | awk '{print $2}' > /tmp/audiodevice
cat /tmp/audiodevice | sed 's/^.//' > /tmp/audiodevice3
sed 's/.$//' /tmp/audiodevice3 > /tmp/audiodevice4
sed 's/.$//' /tmp/audiodevice4 > /tmp/audiodevice5
sed 's/.$//' /tmp/audiodevice5 > /tmp/audio.txt
#
yad --center --width=300 --height=100 --button=OK:1 --text-align=center --title=$"Full test" --text="Test selected card"
if [ $? = 1 ];
then
xterm -e "aplay -D $(cat /tmp/audio.txt) /usr/share/webkitgtk-3.0/resources/audio/Composite.wav" & \
#
# Message if audio was running or not
#
sleep 0,5 && (ps -ef|grep -v grep|grep aplay >/dev/null && echo "If you heard noise, the card is the right. You can close this message" || echo "if you did not heard noise, you probably has the wrong audio device. You can close this message") > /tmp/message.txt
yad --center --width=700 --height=200 --button=OK:1 --text-align=center --title=$"Full test info" --text="INFO: $(cat /tmp/message.txt)"
#
fi
sleep 1
rm /tmp/audio2.txt /tmp/adevice.txt /tmp/audiodevice3 /tmp/audiodevice4 /tmp/audiodevice4 /tmp/audiodevice5 /tmp/audio.txt 
