#!/usr/bin/env bash

m=$HOME/tmp/$$.dat
test -d $HOME/tmp || mkdir -p $HOME/tmp
trap "rm -f $m" EXIT
trap '' SIGHUP
echo '#' $(date) >>LOG
echo $0 $* >>LOG

# Do format conversions on the audio signal, before other processing.
# This will also resample the speech signal to a standard rate, if desired.
# Resampling is done with the -resample flag.
#
# This script is designed to take advantage of a Beowulf cluster,
# if available.  (Using qmake.)    To run with qmake, set the
# -qmake NNN flag, where NNN is the number of processors in the cluster.
#
# The input to this script is in two pieces.   One is a database that
# lists the data files, either in FIAT format or a=v format.   Specify
# the name of the database with the -db FFF or the -dbav FFF flags, as
# appropriate.
# Note that pathnames in the database should either be absolute or relative
# to the directory where the database sits.  Pathnames are stored in two
# columns, "d" and "f" and the full pathname of the input audio data
# is "$d/$f.wav".
# The other part of the input is a directory tree of audio files.

# The output of this script is a directory tree one level deeper,
# where audio file $d/$f.wav is expanded into a directory
# $d/$f, containing raw.wav, raw.dat,
# raw.wav is just a link to the original audio (or original audio, resampled)
# raw.dat is the audio converted into the "GPK ASCII Image" format (see speechresearch/gpkio).

B=$(dirname $(type -p g_audioprep.py))
CLUSTERSIZE=0

rewrite=''
s='True'
DB='DB.fiat'
DBD='.'
misc=''
debug=0
make=make
resample=0
wav=wav
while true
do
	arg=$1; shift;
	case "$arg" in
	'') break;;
	--) break;;
	-rewrite) rewrite=$m;;
	-debug) debug=1;;
	-resample) resample=$1; shift; ;;
	-wav) wav=$1; shift; ;;
	-j) CLUSTERSIZE=$1; shift;;
	-qmake) make="qmake -V -cwd --" ;;
	-db) DB="$1"; DBD=$(dirname "$1"); shift;;
	-dbav) DB="-rav $1"; DBD=$(dirname "$1"); shift;;
	-k | -t) misc="$misc $arg" ;;
	-s) s="$1"; shift;;
	*)	g_die "Unrecognized arg: $arg" || exit 2;;
	esac
done

if test "$CLUSTERSIZE" = 0 
then
  np=-j\ $(grep '^processor.:' /proc/cpuinfo | wc -l)
else
  np="-j $CLUSTERSIZE"
fi

>$m
echo B="$B" >>$m
echo REWRITE="$rewrite" >>$m
echo '.PHONY: all' >>$m
echo '.SUFFIXES:' >>$m
echo resample="$resample" >>$m
echo WAV="$wav" >>$m
echo >>$m

select_fiat_entries.py -select "$s" -penv 'import os.path' -penv "_dir='$DBD'" -show "'all : %s %s\n' % (os.path.join(_dir, d, f, 'raw.dat'), os.path.join(_dir, d, f, 'raw.wav'))" $DB >>$m

if test $resample != 0
then    # ---------------------------------------------

cat <<'ENDEND' >>$m

%/raw.wav : %.$(WAV) $(REWRITE)
	mkdir -p $* && sox $*.$(WAV) -r $(resample) $@ polyphase

ENDEND

else    # ---------------------------------------------

cat <<'ENDEND' >>$m

%/raw.wav : %.$(WAV)
	mkdir -p $* && sox $*.$(WAV) $@

ENDEND

fi    # ---------------------------------------------

cat <<'ENDEND' >>$m

%/raw.dat : %/raw.wav $(B)/g_audioprep.py $(REWRITE)
	mkdir -p $* && $(B)/g_audioprep.py $*/raw.wav $@

ENDEND

if test $debug = 0
then
  $make $np $misc -f $m || g_die "$0 fails!" 
else
  cat $m
fi
