#!/bin/sh
###########################################################################
##                                                                       ##
##                  Language Technologies Institute                      ##
##                     Carnegie Mellon University                        ##
##                        Copyright (c) 2003                             ##
##                        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.                                                       ##
##                                                                       ##
###########################################################################
##             Author:  Alan W Black (awb@cs.cmu.edu)                    ##
##               Date:  September 2003                                   ##
###########################################################################
## make an HTS voice from a FestVox voice                                ##
## (currently only tested for English)                                   ##
###########################################################################

LANG=C; export LANG

if [ ! -d hts_build ]
then
    mkdir hts_build
fi

if [ ! -d hts ]
then
    mkdir hts
fi

if [ ! "$FESTVOXDIR" ]
then
   echo "environment variable FESTVOXDIR is unset"
   echo "set it to your local festvox directory e.g."
   echo '   bash$ export FESTVOXDIR=/home/awb/projects/festvox/'
   echo or
   echo '   csh% setenv FESTVOXDIR /home/awb/projects/festvox/'
   exit 1
fi

if [ ! "$ESTDIR" ]
then
   echo "environment variable ESTDIR is unset"
   echo "set it to your local Edinburgh Speech Tools directory e.g."
   echo '   bash$ export ESTDIR=/home/awb/projects/speech_tools/'
   echo or
   echo '   csh% setenv ESTDIR /home/awb/projects/speech_tools/'
   exit 1
fi

if [ ! "$SPTKBINDIR" ]
then
   echo "environment variable SPTKBINDIR is unset"
   echo "set it to your local Nagoya Institute of Technology Speech Toolkit e.g."
   echo '   bash$ export SPTKBINDIR=/home/awb/projects/SPTK/bin'
   echo or
   echo '   csh% setenv SPTKBINDIR /home/awb/projects/SPTK/bin'
   exit 1
fi

if [ ! "$HTSBINDIR" ]
then
   echo "environment variable HTSBINDIR is unset"
   echo "set it to your local Cambridge University HTK bin directorylogy e.g."
   echo '   bash$ export HTSBINDIR=/home/awb/projects/HTK/bin.i386'
   echo or
   echo '   csh% setenv HTSBINDIR /home/awb/projects/HTK/bin.i386'
   exit 1
fi

FESTIVALDIR=`dirname $ESTDIR`/festival
export FESTIVALDIR

HTSDIR=$FESTVOXDIR/src/hts_build/
export HTSDIR

# To keep our assumptions about perl compatible
LANG=C
export LANG

if [ "$PROMPTFILE" = "" ]
then
   PROMPTFILE=etc/txt.done.data
fi

source etc/voice.defs

if [ $# = 0 ]
then
   $0 setup
   $0 files
   $0 f0
   $0 data
   $0 train
   $0 festvox
   $0 festvox_dist
   exit
fi


if [ "$1" = "setup" ]
then
    echo "HTS Setup subdirectories"
    mkdir hts_build/data
    mkdir hts_build/data/cmp
    mkdir hts_build/data/f0
    mkdir hts_build/data/mcep
    mkdir hts_build/data/raw
    mkdir hts_build/data/utts
    mkdir hts_build/data/log_f0
    mkdir hts_build/data/lists
    cp -pr $HTSDIR/data/data_Makefile hts_build/data/Makefile

    mkdir hts_build/data/labels
    mkdir hts_build/data/labels/fullcontext
    cp -pr $HTSDIR/data/full.mlf hts_build/data/labels
    cp -pr $HTSDIR/data/mono.mlf hts_build/data/labels
    mkdir hts_build/data/labels/fullcontext/gen
    cp -pr $HTSDIR/data/gen/*.lab hts_build/data/labels/fullcontext/gen
    
    mkdir hts_build/data/questions
    cp -pr $HTSDIR/data/questions_qst001.hed hts_build/data/questions
    mkdir hts_build/data/win
    cp -pr $HTSDIR/data/*.win hts_build/data/win
    mkdir hts_build/data/scripts
    cp -pr $HTSDIR/data/*.pl hts_build/data/scripts
    cp -pr $HTSDIR/data/utt2lab.sh hts_build/data/scripts
    cp -pr $HTSDIR/data/extra_feats.scm hts_build/data/scripts

    # Training directories and scripts
    mkdir hts_build/train
    cp -pr $HTSDIR/train/train_Makefile  hts_build/train/Makefile
    mkdir hts_build/train/configs
    cp -pr $HTSDIR/train/default.conf hts_build/train/configs
    cp -pr $HTSDIR/train/synthesis.conf hts_build/train/configs
    mkdir hts_build/train/edfiles
    mkdir hts_build/train/gen
    mkdir hts_build/train/gen/scp
    mkdir hts_build/train/hmms
    mkdir hts_build/train/logs
    mkdir hts_build/train/proto
    cp -pr $HTSDIR/train/s5_m1_c75p1p1p1.prt hts_build/train/proto
    mkdir hts_build/train/scripts
    cp -pr $HTSDIR/train/convstats.pl hts_build/train/scripts
    cp -pr $HTSDIR/train/lfreq2pch.pl hts_build/train/scripts
    cp -pr $HTSDIR/train/Training.pl.bak hts_build/train/scripts
    mkdir hts_build/train/stats
    mkdir hts_build/train/trees

    cp -pr $HTSDIR/build_hts bin/build_hts

fi

if [ "$1" = "files" ]
then
    echo HTS "copy in files (utts,wavs)"

    for i in `cat $PROMPTFILE | awk '{print $2}'`
    do
      echo wave and utt for $i
      $ESTDIR/bin/ch_wave -otype raw -F 16000 wav/$i.wav -o hts_build/data/raw/db_v_$i.raw
      cp -p festival/utts/$i.utt hts_build/data/utts/db_v_$i.utt
    done
fi


if [ "$1" = "f0" ]
then
    echo HTS "make f0"
# Will replace this bit first with some pm based
    if [ ! -f etc/f0range ]
	then
	cp -p $HTSDIR/f0range etc/f0range
    fi

    . etc/f0range

    for i in `cat $PROMPTFILE | awk '{print $2}'`
    do
      echo f0 for $i
      if [ -f $HTSDIR/../vc/src/tempo/tempo ]
      then
         $HTSDIR/../vc/src/tempo/tempo -lf0 $FMIN -uf0 $FMAX wav/$i.wav xxx.f0
         $SPTKBINDIR/x2x +af xxx.f0 >hts_build/data/f0/db_v_$i.f0
      elif [ -f $HTSDIR/getf0/getf0 ]
      then
         $HTSDIR/getf0/getf0 -r 16000 hts_build/data/raw/db_v_$i.raw hts_build/data/f0/db_v_$i.f0
      else
         $ESTDIR/bin/pda -fmin $FMIN -fmax $FMAX wav/$i.wav -otype ascii |
         $SPTKBINDIR/x2x +af >hts_build/data/f0/db_v_$i.f0
      fi
    done
fi

if [ "$1" = "data" ]
then
    echo HTS "Set up data"
    (cd hts_build/data; make) | tee hts_build/data.log
fi

if [ "$1" = "train" ]
then
    echo HTS "Embedded training"
    (cd hts_build/train; 
     make;
     ./scripts/Training.pl
    ) | tee hts_build/train.log
fi

if [ "$1" = "festvox" ]
then
    echo HTS "Make festvox voice"
    # Build models
    cp -p hts_build/train/trees-dur.inf hts/
    cp -p hts_build/train/trees-lf0.inf hts/
    cp -p hts_build/train/trees-mcep.inf hts/
    cp -p hts_build/train/duration.pdf hts/
    cp -p hts_build/train/mcep.pdf hts/
    cp -p hts_build/train/lf0.pdf hts/

    # Features from build
    cp -p hts_build/data/scripts/label.feats hts/feat.list
    cp -p hts_build/data/win/*.win hts

    # Festvox stuff
    cat $HTSDIR/VOICE_hts.scm |
    sed 's/VOICENAME/'${FV_VOICENAME}'/g' |
    cat > festvox/${FV_VOICENAME}_hts.scm

fi

if [ "$1" = "festvox_dist" ]
then
    echo HTS "Make festvox voice distribution"
    mkdir -p festival/lib/voices/
    mkdir -p festival/lib/voices/${FV_LANG}
    ln -s ../../../.. festival/lib/voices/${FV_LANG}/${FV_VOICENAME}_hts

    tar zcvf festvox_${FV_VOICENAME}_hts.tar.gz \
         festival/lib/voices/$FV_LANG/${FV_VOICENAME}_hts/hts \
         festival/lib/voices/$FV_LANG/${FV_VOICENAME}_hts/festvox/${FV_VOICENAME}*.scm \
         festival/lib/voices/$FV_LANG/${FV_VOICENAME}_hts/README \
         festival/lib/voices/$FV_LANG/${FV_VOICENAME}_hts/COPYING
    rm -rf festival/lib

fi

