#!/bin/bash

mydir="`dirname $0`"

# Our input sweep is at 96kHz, so these factors correspond to 48, 24,
# 12, 6, 3, and 1.5kHz.

factors="2 4 8 16 32 64"
signals="sweep lengthy"
impls="zoh decimate resample_hq resample_mq resample_lq src"

original=96000

resample="$mydir/qm-dsp-resample/resample"
decimate="$mydir/qm-dsp-decimate/decimate"

indir="$mydir"/testsignals

outdir="$mydir"/out
mkdir -p "$outdir"

if [ ! -x "$resample" ]; then
    echo "Program $resample not found: make not run, or make failed?"
    exit 1
fi

if [ ! -x "$decimate" ]; then
    echo "Program $decimate not found: make not run, or make failed?"
    exit 1
fi

if ! sndfile-resample 2>&1 | grep -q samplerate ; then
    echo "Program sndfile-resample not found in PATH?"
    exit 1
fi

if ! sndfile-info 2>&1 | grep -q libsndfile ; then
    echo "Program sndfile-info not found in PATH?"
    exit 1
fi

do_src() {
    factor="$1"
    infile="$2"
    outfile="$3"
    time sndfile-resample -to "$(($original/$factor))" "$infile" "$outfile"
}

do_zoh() {
    factor="$1"
    infile="$2"
    outfile="$3"
    time sndfile-resample -to "$(($original/$factor))" -c 3 "$infile" "$outfile"
}

do_resample_hq() {
    factor="$1"
    infile="$2"
    outfile="$3"
    time "$resample" --snr 100 --bandwidth 0.02 --to "$(($original/$factor))" "$infile" "$outfile"
}

do_resample_mq() {
    factor="$1"
    infile="$2"
    outfile="$3"
    time "$resample" --snr 70 --bandwidth 0.03 --to "$(($original/$factor))" "$infile" "$outfile"
}

do_resample_lq() {
    factor="$1"
    infile="$2"
    outfile="$3"
    time "$resample" --snr 50 --bandwidth 0.05 --to "$(($original/$factor))" "$infile" "$outfile"
}

decimate_twice() {
    first="$1"
    second="$2"
    infile="$3"
    outfile="$4"
    "$decimate" --by "$first" "$infile" "$outfile".tmp
    "$decimate" --by "$second" "$outfile".tmp "$outfile"
    rm "$outfile".tmp
}    

do_decimate() {
    factor="$1"
    infile="$2"
    outfile="$3"
    time case "$factor" in
	16) decimate_twice 4 4 "$infile" "$outfile";;
	32) decimate_twice 8 4 "$infile" "$outfile";;
	64) decimate_twice 8 8 "$infile" "$outfile";;
	*) "$decimate" --by "$factor" "$infile" "$outfile";;
    esac
}

for s in $signals; do
    for f in $factors; do
	for impl in $impls; do
	    echo "signal $s, factor $f, impl $impl..."
	    stem="$s"_"$f"_"$impl"
	    do_$impl "$f" "$indir"/*"$s"*.wav "$outdir"/"$stem".wav > "$outdir"/"$stem".log 2>&1
	done
    done
done

# timings

lengthyframes=`sndfile-info testsignals/sine-lengthy.wav | grep '^Frames' | awk '{ print $3; }'`

for f in $factors; do
    echo "For $lengthyframes input frames, decimation factor $f..."
    for log in out/lengthy_"$f"_*.log; do
	impl=`basename "$log" .log | sed 's/^.*lengthy_[0-9]*_//'`
	seconds=`grep '^real' "$log" | tail -1 | sed 's/^.*m\([0-9.]*\)s$/\1/'`
	fps=`echo "$lengthyframes $seconds / p" | dc`
	fps=`printf "%11d" "$fps"`
	echo "$fps fps [$seconds s]: $impl"
    done | sort -rn
    echo
done

for impl in $impls; do
    echo "For $lengthyframes input frames, implementation $impl..."
    for log in out/lengthy_*_"$impl".log; do
	f=`basename "$log" .log | sed 's/^.*lengthy_//' | sed 's/_.*//'`
	seconds=`grep '^real' "$log" | tail -1 | sed 's/^.*m\([0-9.]*\)s$/\1/'`
	fps=`echo "$lengthyframes $seconds / p" | dc`
	fps=`printf "%11d" "$fps"`
	echo "$fps fps [$seconds s]: factor $f"
    done | sort -rn
    echo
done
