#!/usr/local/bin/bash
###########################################################################
##                                                                       ##
##                Centre for Speech Technology Research                  ##
##                     University of Edinburgh, UK                       ##
##                       Copyright (c) 1996,1997                         ##
##                        All Rights Reserved.                           ##
##                                                                       ##
##  Permission to use, copy, modify, distribute this software and its    ##
##  documentation for research, educational and individual use only, is  ##
##  hereby granted without fee, 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.                         ##
##  This software may not be used for commercial purposes without        ##
##  specific prior written permission from the authors.                  ##
##                                                                       ##
##  THE UNIVERSITY OF EDINBURGH 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 THE UNIVERSITY OF EDINBURGH 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.                                                       ##
##                                                                       ##
###########################################################################
#
#  make utterances from set of xlabel files
# 
#  This could easy require editing for your particular database
#
#  Label files are assumed to be in the directory others/ with 
#  file extension identifying their stream name
#  
#  This assumes:  Segment, Syllable, Word, IntEvent, Target, Phrase
#  label file exist in Xlabel format
#
#  Note this assumes silience is marked by #, you *must* change
#  the two references in find_links() apropriately for your silence
#  phone name(s). And one reference in make_stream_syl()
#
#  Usage:  make_utts others/*.Segment
#

make_stream () {
   rm -f $1.stream
   awk '{if (NF > 1)
           printf("    %5.6f   26 %s; a%d ;\n",$1,$3,addr++)}' <others/$fname.$1 >$1.stream
}

make_stream_word () {
   rm -f $1.stream
   awk '{if (NF > 1)
           printf("    %5.6f   26 %s; a%d pos %s punc %s ;\n",$1,$3,addr++,$5,$6)}' <others/$fname.$1 >$1.stream
}

make_stream_syl () {
   rm -f $1.stream
   awk '{if ((NF > 1) &&
	     ($3 != "#"))
           if ($5 == "stressed")
              printf("    %5.6f   26 %s; a%d stress 1;\n",$1,$3,addr++)
           else
              printf("    %5.6f   26 %s; a%d stress 0;\n",$1,$3,addr++)
        }' <others/$fname.$1 >$1.stream
}

find_links () {
   echo find_links $1 $2 $3 $4
   st=0.0
   cp $1.stream t1
   cp $3.stream t2
   if [ $1 = "Segment" -a $3 = "Syllable" ] 
   then
	awk '{if (($3 != "#;"))
                 print $0
              else
                 printf "%s sil\n",$1}' <$1.stream >t1
   fi
   if [ $3 = "Segment" -a $1 = "Syllable" ] 
   then
	awk '{if (($3 != "#;"))
                 print $0}' <$3.stream >t2
   fi
   awk '{printf "%s\n%s\n",$1,$2}' t1 |
   while read end
   do
      read symbol
      if [ $symbol = "sil" ]
      then
         echo $3 ";"
      else
         echo -n $3" " 
         awk 'BEGIN {st='$st';end='$end';
                 lstream="'$3'";bstream="'$1'"
                 ltype="'$4'";btype="'$2'"}
           {
            if ((end-st) < ($1-lst))
               half = (end-st)/2.0
            else
               half = ($1-lst)/2.0
            if ((btype == "seg") && (ltype == "seg"))
            {
               if ((st < $1) && (lst < end))
               {
                  if (st > lst)
                     olpst=st
                  else
                     olpst=lst
                  if (end < $1)
                     olpend = end
                  else
                     olpend = $1
                  if (olpend-olpst > half) 
                     printf("%s ",substr($4,2,100))
               }
            }
            else if ((btype == "point") && (ltype == "seg"))
            {
               if ((end > lst) && (end <= $1))
                     printf("%d ",NR-1)
            }
            else if ((btype == "seg") && (ltype == "point"))
            {
               if (($1 > st) && ($1 <= end))
                     printf("%d ",NR-1)
            }
            else if ((btype == "point") && (ltype == "point"))
            {
               if (abs($1-end) < 0.009)
                     printf("%d ",NR-1)
            }
            if ($1 > end)
               exit; 
            lst = $1}
           END { printf(" ; \n"); }' < t2
      fi
      st=$end
   done > $1.$3.links
}

find_all_links () {
   base=$1
   basetype=$2
   shift; shift
   cp $base.stream $base.links   
   echo $* | awk '{for (i=1;i<=NF;i++) printf("%s\n",$i)}' |
   while read j
   do 
      read jtype
      rm -f $base.*.links
      find_links $base $basetype $j $jtype
      paste $base.links $base.$j.links >$base.newlinks
      mv $base.newlinks $base.links
      rm $base.$j.links
   done
   mv $base.links $base.stream
}

for i in $*
do
   fname=`basename $i .Segment`
   echo $i
   rm -f *.stream
   make_stream_word Word
   make_stream Phrase
   make_stream IntEvent
   make_stream_syl Syllable
   make_stream Segment
   make_stream Target
   find_all_links Word seg Syllable seg Phrase seg
   find_all_links Syllable seg Segment seg Word seg IntEvent point
   find_all_links Segment seg Syllable seg Target point
   find_all_links Target point Segment seg
   find_all_links IntEvent point Syllable seg
   find_all_links Phrase seg Word seg
   rm -f $fname.utt
   for s in *.stream
   do
      sname=`basename $s .stream`
      echo "# " $sname >>$fname.utt
      cat $s >>$fname.utt
   done
   rm -f *.stream
   mv $fname.utt utts/seg/$fname.seg.utt
done
