#!/bin/sh
###########################################################################
##                                                                       ##
##                   Language Technologies Institute                     ##
##                     Carnegie Mellon University                        ##
##                       Copyright (c) 2008-2013                         ##
##                        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.                                                       ##
##                                                                       ##
###########################################################################
##  Grapeheme based voice build                                          ##
###########################################################################

# Directory containing wav/*.wav and etc/txt.done.data
LANG_DIR=$1
# If you want to build with mixed excitation (you need the SPTK patch
# $FESTVOX/src/clustergen/SPTK-3.6.patch)
MIXED_EXCITATION=1

# Initial setup you should do this by hand -- I only automate this for 
# regression testing
if [ $# = 1 ]
then
   $FESTVOXDIR/src/clustergen/setup_cg cmu grapheme `basename $LANG_DIR`
## populate waveforms in wav/ (or cp them)
#   ./bin/get_wavs $LANG_DIR/wav/*.wav
   cp -pr $LANG_DIR/wav/*.wav wav
## Prune excessive silence from files
#  ./bin/prune_silence
## Get prompt list
   cp -pr $LANG_DIR/etc/txt.done.data etc
## If latin characters, normalize it.
#  mv etc/txt.done.data etc/txt.done.data.orig
#  $FESTVOX/src/grapheme/latin_norm etc/txt.done.data.orig >etc/txt.done.data
##
## Build unitran based grapheme mapping
   $FESTVOXDIR/src/grapheme/make_cg_grapheme etc/txt.done.data
fi

# Initial labeling
./bin/do_build parallel build_prompts
./bin/do_build label
./bin/do_clustergen parallel build_utts

# To save some space
rm -rf ehmm/feat
rm -rf ehmm/binfeat

# Build first generation of voice

# A slightly different set of duration features for flite (ok for festival)
#cp -p $FESTVOXDIR/src/clustergen/statedur.feats_flite festival/dur/etc/statedur.feats
./bin/do_clustergen generate_statenames
./bin/do_clustergen generate_filters

./bin/do_clustergen parallel f0_v_sptk
./bin/do_clustergen parallel mcep_sptk

if [ $MIXED_EXCITATION = 1 ]
then
   ./bin/do_clustergen parallel str_sptk
   ./bin/do_clustergen parallel combine_coeffs_me
   if [ ! -f festvox/clustergen.scm ]
   then
      mv festvox/clustergen.scm festvox/clustergen.scm.orig
   fi
   sed 's/cg:mixed_excitation nil/cg:mixed_excitation t/' <festvox/clustergen.scm.orig >festvox/clustergen.scm
else
   ./bin/do_clustergen parallel combine_coeffs_v
fi

./bin/traintest etc/txt.done.data

./bin/do_clustergen parallel cluster etc/txt.done.data.train
./bin/do_clustergen dur etc/txt.done.data.train

# Test initial build to get basic stats

$FESTVOXDIR/src/clustergen/cg_test resynth cgp >mcd-base.out
mv dur.dur.S25.out dur.dur.S25.out-base
$FESTVOXDIR/src/clustergen/cg_test tts tts

exit

# Optimize number of states in hmms
./bin/do_clustergen nstates

# Optimize wagon stop values for mcep trees
./bin/do_clustergen wstop

# Do 10 iterations of move label to improve build
$FESTVOXDIR/src/clustergen/do_move_label parallel 20

# Select the best build
$FESTVOXDIR/src/clustergen/do_move_label select

# Rebuild with best labels
./bin/do_clustergen cluster etc/txt.done.data.train
./bin/do_clustergen dur etc/txt.done.data.train
mv dur.dur.S25.out dur.dur.S25.out-ml

# Final stats
$FESTVOXDIR/src/clustergen/cg_test resynth cgp_all >mcd-ml.out

exit

# Build a flite voice from this build
rm -rf flite
$FLITEDIR/tools/setup_flite
./bin/build_flite cg
cd flite
make

./flite_cmu_us_${NAME} "A whole joy was reaping, but they've gone south, go fetch azure mike." whole_${NAME}.wav

ls -altr flite_cmu_us_${NAME}


