#!/bin/sh

START=`date +%s`;
echo "Script start time (Unix epoch): $START";

rm -rf hmm*
rm -rf aligned
rm -rf mono_mix*
rm -r phones

# Flat-start
echo "flat start...";
echo "Compute global mean and variance .... hcompv.log"

mkdir hmm0
mkdir hmm1
mkdir hmm2
mkdir hmm3;mkdir hmm4

echo "flat start...";
echo "Compute global mean and variance...hcompv.log"
HCompV -C configs/config0 -f 0.01 -m -S train_complete.list -M hmm0 hmm0/proto >logs/mono/hcompv.log

scripts/make_hmmdef.pl phones/monophones0 hmm0/proto hmm0/hmmdefs
scripts/make_macro.pl hmm0/vFloors hmm0/proto hmm0/macros

echo "estimate models without sp .... flat.log";
HERest -B -A -D -T 1 -C configs/config0 -I ../data_preparation/trans_out/train_trans_phones0.mlf -t 250.0 150.0 3000.0 -S train_complete.list -H hmm0/macros -H hmm0/hmmdefs -M hmm1 phones/monophones0 > logs/mono/flat.log
HERest -B -A -D -T 1 -C configs/config0 -I ../data_preparation/trans_out/train_trans_phones0.mlf -t 250.0 150.0 3000.0 -S train_complete.list -H hmm1/macros -H hmm1/hmmdefs -M hmm2 phones/monophones0 >> logs/mono/flat.log
HERest -B -A -D -T 1 -C configs/config0 -I ../data_preparation/trans_out/train_trans_phones0.mlf -t 250.0 150.0 3000.0 -S train_complete.list -H hmm2/macros -H hmm2/hmmdefs -M hmm3 phones/monophones0 >> logs/mono/flat.log
HERest -A -D -T 1 -C configs/config0 -I ../data_preparation/trans_out/train_trans_phones0.mlf -t 250.0 150.0 3000.0 -S train_complete.list -H hmm3/macros -H hmm3/hmmdefs -M hmm4 phones/monophones0 >> logs/mono/flat.log

####################################################
# Train silence models, tying "sp" and "sil"
echo "fix silence model .... hhed_flat_sil.log";
mkdir hmm5;mkdir hmm6;mkdir hmm7;mkdir hmm8;mkdir hmm9;mkdir hmm10
cp hmm4/* hmm5
scripts/add_sp.pl hmm5/hmmdefs hmm5/tmp
mv hmm5/tmp hmm5/hmmdefs
HHEd -A -D -T 1 -H hmm5/macros -H hmm5/hmmdefs -M hmm6 sil.led phones/monophones1 >logs/mono/hhed_flat_sil.log

echo "train silence model .... herest_sil.log";
HERest -B -A -D -T 1 -C configs/config0  -I ../data_preparation/trans_out/train_trans_phones1.mlf -t 250.0 150.0 3000.0 -S train_complete.list -H hmm6/macros -H  hmm6/hmmdefs -M hmm7 phones/monophones1 > logs/mono/herest_sil.log
HERest -B -A -D -T 1 -C configs/config0  -I ../data_preparation/trans_out/train_trans_phones1.mlf -t 250.0 150.0 3000.0 -S train_complete.list -H hmm7/macros -H  hmm7/hmmdefs -M hmm8 phones/monophones1 >> logs/mono/herest_sil.log
HERest -B -A -D -T 1 -C configs/config0  -I ../data_preparation/trans_out/train_trans_phones1.mlf -t 250.0 150.0 3000.0 -S train_complete.list -H hmm8/macros -H  hmm8/hmmdefs -M hmm9 phones/monophones1 >> logs/mono/herest_sil.log
HERest -B -A -D -T 1 -C configs/config0  -I ../data_preparation/trans_out/train_trans_phones1.mlf -t 250.0 150.0 3000.0 -S train_complete.list -H hmm9/macros -H  hmm9/hmmdefs -M hmm10 phones/monophones1 >> logs/mono/herest_sil.log


# Realignment
mkdir aligned;mkdir hmm11;mkdir hmm12;mkdir hmm13;mkdir hmm14
# The next 3 comands remove any corruputed files
echo "Forced alignment .....";
HVite -A -D -T 1 -l '*' -o SWT -b '</s>' -C configs/config0 -H hmm10/macros -H hmm10/hmmdefs -i aligned/aligned.mlf -m -t 250.0 150.0 3000.0 -y lab -a -I ../data_preparation/trans_out/train_trans_words.mlf -S train_complete.list ../data_preparation/dict_out/timit_dict_sp_tauexp033 phones/monophones1> aligned/HVite_log
scripts/new_train_list.pl aligned/HVite_log  train_list2.list

HERest -B -A -D -T 1 -C configs/config0 -I aligned/aligned.mlf -t 250.0 150.0 3000.0 -S train_list2.list -H hmm10/macros -H hmm10/hmmdefs -M hmm11 phones/monophones1 > logs/mono/mono.log
HERest -B -A -D -T 1 -C configs/config0 -I aligned/aligned.mlf -t 250.0 150.0 3000.0 -S train_list2.list -H hmm11/macros -H hmm11/hmmdefs -M hmm12 phones/monophones1 >> logs/mono/mono.log
HERest -B -A -D -T 1 -C configs/config0 -I aligned/aligned.mlf -t 250.0 150.0 3000.0 -S train_list2.list -H hmm12/macros -H hmm12/hmmdefs -M hmm13 phones/monophones1 >> logs/mono/mono.log
HERest -A -D -T 1 -C configs/config0 -I aligned/aligned.mlf -t 250.0 150.0 3000.0 -s hmm14/stats -S train_list2.list -H hmm13/macros -H hmm13/hmmdefs -M hmm14 phones/monophones1 >> logs/mono/mono.log


FINISH=`date +%s`
echo "Script finish time (Unix epoch): $FINISH"

ELAPSED=`expr $FINISH - $START`
echo "Elapsed time: $ELAPSED"
