#!/usr/bin/env bash
set -e

# SoX Resampler Library       Copyright (c) 2007-15 robs@users.sourceforge.net
# Licence for this file: LGPL v2.1                  See LICENCE for details.

# Tests rate conversion time for qualities 0..7 & variable-rate.

tool=./3-options-input-fn
ext=f32; e=0
test z"$1" != z && c="$1" || c=2
test z"$2" != z && qs="$2" || qs="`seq 0 7` v"
rates="48000 77773 96000"
time=`which time`
BASE=`basename $0`
TIME=/tmp/$BASE-time-$$
ERR=/tmp/$BASE-err-$$
uname -m |grep -q ^arm && len=60 || len=600
export OMP_NUM_THREADS=2

for rate0 in $rates; do
	rate1=44100
	rate2=$rate0
	for n in 1 2; do
		sox -R -r $rate1 -n -c $c 0.$ext synth $len noise; sync
		for q in $qs; do
			test $q = v && Q="4 20" || Q=$q
			$time -f %e -o $TIME $tool $rate1 $rate2 $c $e $e $Q < 0.$ext > /dev/null 2> $ERR
			echo $rate1 '-->' $rate2 c=$c q=$q t=`cat $TIME` `cat $ERR | sed 's/.*(/(/'`
		done
		rate1=$rate0
		rate2=44100
	done
done

rm 0.$ext
