#!/bin/bash
# Test ca0106 driver playback.
# Written by Ben Stanley <Ben.Stanley@exemail.com.au>
#
# Test configuration:
# Creative Labs SB Audigy LS SB0570
# Yamaha RX-V557 via SPDIF
# Note that SPDIF output must be enabled in the mixer 
# settings prior to commencing the tests!


passes=0
fails=0

function test_passed () {
	echo "PASS: $*"
	((passes+=1));
	return 0;
}

function test_failed() {
	echo "FAIL: $*"
	((fails+=1));
	return 0;
}

function print_test_summary() {
	local total_tests
	((total_tests=passes+fails))
	echo "Performed ${total_tests} tests."
	echo "There were ${passes} passes and ${fails} failures."
}

function do_test() {
	$* >/dev/null 2>&1
	local RESULT=$?
	if [ ${RESULT} -eq 0 ]
	then
		test_passed $*
	else
		test_failed $*
	fi
}

function do_fail_test() {
	$* >/dev/null 2>&1
	local RESULT=$?
	if [ ${RESULT} -ne 0 ]
	then
		test_passed $*
	else
		test_failed $*
	fi
}

function do_audible_test() {
	local PID result
	echo Testing $*
	$* >/dev/null 2>&1 &
	PID=$!
	result="";
	while [ -z "${result}" ]
	do
		if ! (ps | grep ${PID}) > /dev/null
		then
			test_failed $*;
			break;
		fi
		echo Can you hear sound? [y/n]
		read answer
		case ${answer} in
		y|Y) 	result=${answer}; kill ${PID} >/dev/null 2>&1;
			test_passed $*;
			break;;
		n|N) 	result=${answer}; kill ${PID} >/dev/null 2>&1;
			test_failed $*;
			break;;
		esac
	done
}

function EquivalentRate() {
	Rate1=$1
	Rate2=$2
	EquivalentRates="48000 96000 192000"
	if [ ${Rate1} = ${Rate2} ]
	then
		return 0;
	fi
	if (echo ${EquivalentRates} | grep ${Rate1} >/dev/null) && (echo ${EquivalentRates} | grep ${Rate2} >/dev/null)
	then
		return 0;
	else
		return 1;
	fi
}

if false
then
do_test true
do_test false
do_fail_test true
do_fail_test false

do_test EquivalentRate 44100 44100
do_fail_test EquivalentRate 44100 48000
do_fail_test EquivalentRate 44100 96000
do_fail_test EquivalentRate 44100 192000

do_fail_test EquivalentRate 48000 44100
do_test EquivalentRate 48000 48000
do_test EquivalentRate 48000 96000
do_test EquivalentRate 48000 192000

do_fail_test EquivalentRate 96000 44100
do_test EquivalentRate 96000 48000
do_test EquivalentRate 96000 96000
do_test EquivalentRate 96000 192000

do_fail_test EquivalentRate 192000 44100
do_test EquivalentRate 192000 48000
do_test EquivalentRate 192000 96000
do_test EquivalentRate 192000 192000

EquivalentRate 44100 48000
echo EquivalentRate 44100 48000 $?
exit 0
fi

DevicesToTest="hw:0,0 hw:0,1 hw:0,2 hw:0,3"
RatesToTest="44100 48000 96000 192000"
FormatsToTest="S16 S32"

if true
then
DeviceToTest="hw:0,0"
#for DeviceToTest in ${DevicesToTest}
#do
	for FormatToTest in ${FormatsToTest}
	do
		for RateToTest in ${RatesToTest}
		do
			do_audible_test speaker-test --device=${DeviceToTest} --rate=${RateToTest} --format=${FormatToTest} --channels=2
		done
	done
#done
fi

for DeviceToSet in ${DevicesToTest}
do
	for RateToSet in ${RatesToTest}
	do
		for FormatToSet in ${FormatsToTest}
		do
			echo "Configuring ${DeviceToSet} for ${RateToSet} ${FormatToSet}."
			speaker-test \
				--device=${DeviceToSet} \
				--rate=${RateToSet} \
				--format=${FormatToSet} \
				--channels=2 >/dev/null &
			PID=$!
	
			for DeviceToTest in ${DevicesToTest}
			do
				if [ ${DeviceToTest} = ${DeviceToSet} ]
				then
					continue;
				fi
				for FormatToTest in ${FormatsToTest}
				do
					for RateToTest in ${RatesToTest}
					do
						if (EquivalentRate ${RateToTest} ${RateToSet}) && [ ${FormatToTest} = ${FormatToSet} ]
						then
							do_test speaker-test \
								--device=${DeviceToTest} \
								--rate=${RateToTest} \
								--format=${FormatToTest} \
								--channels=2 --nloops 1
						else
							do_fail_test speaker-test \
								--device=${DeviceToTest} \
								--rate=${RateToTest} \
								--format=${FormatToTest} \
								--channels=2 --nloops 1
						fi
					done
				done
			done
	
			kill ${PID} >/dev/null 2>&1;
		done
	done
done

print_test_summary

exit 0

speaker-test --device=${DeviceToTest} --rate=44100 --format=S16 --channels=2 --nloops 5 # works, amp says 44.1kHz
speaker-test --device=${DeviceToTest} --rate=48000 --format=S16 --channels=2 --nloops 5 # works, amp says 48kHz
speaker-test --device=${DeviceToTest} --rate=96000 --format=S16 --channels=2 --nloops 5 # works, amp says 96kHz
speaker-test --device=${DeviceToTest} --rate=192000 --format=S16 --channels=2 --nloops 5 # Sometimes works, sometimes does not work (No sound, no PCM light on amp)
speaker-test --device=${DeviceToTest} --rate=44100 --format=S32 --channels=2 --nloops 5 # works, amp says 44.1kHz
speaker-test --device=${DeviceToTest} --rate=48000 --format=S32 --channels=2 --nloops 5 # works, amp says 48kHz
speaker-test --device=${DeviceToTest} --rate=96000 --format=S32 --channels=2 --nloops 5 # works, amp says 96kHz
speaker-test --device=${DeviceToTest} --rate=192000 --format=S32 --channels=2 --nloops 5 # works, amp says 192kHz

