#!/usr/bin/env bash

o=/tmp/$$.dat
trap "rm -f $o" EXIT

#Calculate the loudness of the main microphone
#signal, and (incidentally) the spectral slope.
# Loudness in channel 0, spectral slope in channel 1.


err="$0: OXIVOICE needs to be set in the environment.  Try /home/mint/gpk/voicing."
OV=${OXIVOICE:?$err}
# PATH=$OV:${OXIROOT:-$ORD}/bin:$PATH

rewrite=0
DT=0.010
while true
do
	arg=$1; shift;
	case "$arg" in
	'') break;;
	-rewrite) rewrite=1;;
	-dt) DT=$1; shift;;
	*)	g_die "Unrecognized arg: $arg" || exit 2;;
	--) break;;
	esac
done

for n in $(select_fiat_entries.py -show "'%s/%s' % (d, f)" DB.fiat)
do
    echo 'Working on' $n
    if test ! -s $n.dat
    then
      g_die "No data: $n" || exit 1
    # elif test $rewrite = 1 -o $n.dat -nt $n.pdur.dat -o $n.dat -nt $n.pdur.sd \
    		# -o $OV/pdur.py -nt $n.dat -o $OV/pdur.py -nt $n.pdur.sd
    elif test $rewrite = 1 -o $n.dat -nt $n.pdur.dat  -o $OV/pdur.py -nt $n.dat
    then
      # rm -f $o $n.pdur.dat $n.pdur.sd
      rm -f $o $n.pdur.dat
      python $OV/pdur.py -dt $DT -f $n.dat -c 0 -o $o  ||
		g_die "pdur.py fails!" || exit 1
      imop -write BITPIX=-32 r $o = $n.pdur.dat
      # imop -write BITPIX=-32 r $o = $n.pdur.sd
    fi
done

