#!/bin/sh
#####################################################-*-mode:shell-script-*-
##                                                                       ##
##                     Carnegie Mellon University                        ##
##                         Copyright (c) 2005                            ##
##                        All Rights Reserved.                           ##
##                                                                       ##
##  Permission is hereby granted, free of charge, to use and distribute  ##
##  this software and its documentation without restriction, including   ##
##  without limitation the rights to use, copy, modify, merge, publish,  ##
##  distribute, sublicense, and/or sell copies of this work, and to      ##
##  permit persons to whom this work is furnished to do so, subject to   ##
##  the following conditions:                                            ##
##   1. The code must retain the above copyright notice, this list of    ##
##      conditions and the following disclaimer.                         ##
##   2. Any modifications must be clearly marked as such.                ##
##   3. Original authors' names are not deleted.                         ##
##   4. The authors' names are not used to endorse or promote products   ##
##      derived from this software without specific prior written        ##
##      permission.                                                      ##
##                                                                       ##
##  CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK         ##
##  DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING      ##
##  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT   ##
##  SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE      ##
##  FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES    ##
##  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN   ##
##  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,          ##
##  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF       ##
##  THIS SOFTWARE.                                                       ##
##                                                                       ##
###########################################################################
##                                                                       ##
##  Generate a track with Maximum Likelihood Parameter Generation        ##
##  takes mcep track means/variances and generates mceps (for mlsa)      ##
##                                                                       ##
###########################################################################

LANG=C; export LANG

TRACK=$1
MCEPTRACK=$2

if [ $# = 4 ]
then
   # with GV
   GVVM=$3
   GVVV=$4
fi

TMPFILENAME=/tmp/cg_$$
F0_FILENAME=$TMPFILENAME.f0
COEFS_FILENAME=$TMPFILENAME.coefs
CSEQ_FILENAME=$TMPFILENAME.cseq
WSEQ_FILENAME=$TMPFILENAME.wseq
MSEQ_FILENAME=$TMPFILENAME.mseq
COV_FILENAME=$TMPFILENAME.cov
SAFE_TRACK=$TMPFILENAME.safe

synthesis=$FESTVOXDIR/src/vc/src/synthesis/synthesis
mlpg=$FESTVOXDIR/src/vc/src/mlpg/mlpg_vit
dynwin=$FESTVOXDIR/src/vc/src/win/dyn.win
ORDER=25
FSHIFT=0.005
#ORDER=13

if [ "$CLUSTERGENDIR" = "" ]
then
    CLUSTERGENDIR=$FESTVOXDIR/src/clustergen
fi

# We are just going to treat each frame as a different gaussian
# even though they may actually be the same sometimes
DNUM=`$ESTDIR/bin/ch_track -otype ascii $TRACK | wc -l`
CLSNUM=$DNUM
ORDER=`$ESTDIR/bin/ch_track -otype ascii $TRACK | awk '{o=(NF-2)/4} END {print o}'`
ORDER=25;
#ORDER=13;

## When running cg_test you may get 0s in the final frames
## which crashes mlpg, so duplicate the last non-zero frame to make it
## not crash
$ESTDIR/bin/ch_track -otype ascii $TRACK |
awk '{allzeros = 1;
      for (i=1; i<=NF; i++)
         if ($i != 0.0) allzeros = 0;
      if (allzeros == 1)
         print l
      else
      {
         print $0
         l=$0;
      }}' >$SAFE_TRACK
cat $SAFE_TRACK |
awk '{print $1}' >$F0_FILENAME
cat $SAFE_TRACK |
awk '{print NR-1}' |
perl $CLUSTERGENDIR/a2l.pl >$CSEQ_FILENAME
cat $SAFE_TRACK |
awk '{print 0.9}' |
perl $CLUSTERGENDIR/a2d.pl >$WSEQ_FILENAME
cat $SAFE_TRACK |
awk '{for (i=3; i<=NF; i+=2)
       printf("%s\n",$i); }' |
perl $CLUSTERGENDIR/a2d.pl >$MSEQ_FILENAME
cat $SAFE_TRACK |
awk '{for (i=3; i<=NF; i+=2)
       printf("%g\n",$(i+1)*$(i+1)); }' |
perl $CLUSTERGENDIR/a2d.pl >$COV_FILENAME

## dnum is num_frames
#	$CSEQ_FILENAME   # class dnum longs (which gaussian)
#	$WSEQ_FILENAME   # weight dnum doubles, nearly 1.0
#	$CSEQ_FILENAME   # mean dnum * dim * 2 (static means and deltas)
#	$COV_FILENAME     # covariance matrices [clsnum * dim][dim]
#	$COEFS_FILENAME    # the output file dnum * dim (no deltas)

if [ /bin/true ]
then
   oo=`echo $ORDER | awk '{printf("%d",$1+$1)}'`
   if [ "$GVVV" = "" ]
   then
      echo mlpg with NO gv
      $mlpg -nmsg -sm -dia \
            -dim $oo \
            -clsnum $CLSNUM \
   	    -dynwinf $dynwin \
	    $CSEQ_FILENAME  \
	    $WSEQ_FILENAME  \
	    $MSEQ_FILENAME  \
	    $COV_FILENAME   \
	    $COEFS_FILENAME  
   else
      echo mlpg with gv
      $mlpg -nmsg -sm -dia \
            -dim $oo \
            -clsnum $CLSNUM \
   	    -dynwinf $dynwin \
  	    -vmfile $GVVM \
	    -vvfile $GVVV \
	    $CSEQ_FILENAME  \
	    $WSEQ_FILENAME  \
	    $MSEQ_FILENAME  \
	    $COV_FILENAME   \
	    $COEFS_FILENAME  
   fi

   cat $COEFS_FILENAME |
   perl $CLUSTERGENDIR/d2a.pl |
   awk '{printf("%s ",$1); if ((NR%('$ORDER')) == 0) printf("\n")}' |
   paste $F0_FILENAME - |
   $ESTDIR/bin/ch_track -itype ascii -otype est_binary -s $FSHIFT -o $MCEPTRACK
fi

if [ ! /bin/true ]
then
   cat $MSEQ_FILENAME |
   perl $CLUSTERGENDIR/d2a.pl |
   awk '{printf("%s ",$1); if ((NR%(2*'$ORDER')) == 0) printf("\n")}' |
   awk '{for (i=1; i<=25; i+=1)
           printf("%s ",$i);
         printf("\n") }' |
   paste $F0_FILENAME - |
   $ESTDIR/bin/ch_track -itype ascii -otype est_binary -s $FSHIFT -o $MCEPTRACK
fi

rm -f $TMPFILENAME.*



