#!/bin/bash

# This scripts calsulate lattice given a wav sound file.
# eg: ./sound_to_lattice.sh aaa 2.lat.gz
# the first parameter is a sound file, which is ignored for now. The second parameter is the output lattice file.

source ~/github/kaldi/setenv.sh

sound_file=$1
feats=xxx

outfile=$2

# TODO: replace xxx
max_active=5000
beam=16
lattice_beam=10
acwt=0.1
graphdir=xxx
decode_extra_opts=
model=xxx

# for yesno eg
graphdir=~/github/kaldi/egs/yesno/s5/exp/mono0a/graph_tgpr
model=/Users/astropeak/github/kaldi/egs/yesno/s5/exp/mono0a/final.mdl
sdata=/Users/astropeak/github/kaldi/egs/yesno/s5/data/test_yesno

cmvn_opts=
delta_opts=

# 1: from sound to feature
# 1.1 create mffc, stored in fests.scp

# 1.2 process mffc, apply cmnv, add deltas
feats="ark,s,cs:apply-cmvn $cmvn_opts --utt2spk=ark:$sdata/utt2spk scp:$sdata/cmvn.scp scp:$sdata/feats.scp ark:- | add-deltas $delta_opts ark:- ark:- |"
# if remove --utt2spk, there is error like.
# WARNING (apply-cmvn[5.4.218~2-e6fe]:main():apply-cmvn.cc:112) No normalization statistics available for key 1_1_1_1_1_1_1_1, producing no output for this utterance
# LOG (apply-cmvn[5.4.218~2-e6fe]:main():apply-cmvn.cc:162) Applied cepstral mean normalization to 0 utterances, errors on 29
# so no output will be produced by apply-cmnv
# the utt2spk map all utt to a global speaker, which is a key to query cmnv statictics from cmnv.scp. So if that key is not availble, then no cmnv info, so on ouput
# so in decoer, we sould always map a utt to the global speker.
# feats="ark,s,cs:apply-cmvn $cmvn_opts scp:$sdata/cmvn.scp scp:$sdata/feats.scp ark:- | add-deltas $delta_opts ark:- ark:- |"

# 2: from feature to lattice
gmm-latgen-faster --max-active=$max_active --beam=$beam --lattice-beam=$lattice_beam \
                  --acoustic-scale=$acwt --allow-partial=true --word-symbol-table=$graphdir/words.txt $decode_extra_opts \
                  $model $graphdir/HCLG.fst "$feats" "ark:|gzip -c > $outfile" || exit 1;


Add sound_to_latice.sh, from a mfcc feature file
Add sound_to_latice.sh, from a mfcc feature file
