#!/usr/bin/env bash

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

#Calculate the loudness of the main microphone
#signal.

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

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.loud.sd -o $n.dat -nt $n.loud.dat
    elif test $rewrite = 1 -o $n.dat -nt $n.loud.dat
    then
      # rm -f $n.loud.dat $n.loud.sd $o
      rm -f $n.loud.dat $o
      if python $OV/emphasis_stevens.py -dt $DT -f $n.dat -c 0 -o $o
      then
        imop -write BITPIX=-32 r $o = $n.loud.dat
        # imop -write BITPIX=-32 r $o = $n.loud.sd
      else
        g_die "emphasis.py fails!" || exit 1
      fi
    fi
done

