#!/bin/bash
####################################################-*-mode:shell-script-*-
##                                                                       ##
##                     Carnegie Mellon University                        ##
##                   Language Technologies Instutute                     ##
##                         Copyright (c) 2009                            ##
##                        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.                                                       ##
##                                                                       ##
###########################################################################
##  Remove extra silence on start and end of waveform files              ##
##  Uses the F0 detector to find speech (approximately)                  ##
###########################################################################

# Keep up to SILMARGIN seconds of speech before/after first/last voiced frame
SILMARGIN=0.2
if [ "$(uname)" == "Darwin" ]; then
   REVERSE="tail -r"
else
   REVERSE=tac
fi


if [ -d unpruned ]
then
  mv unpruned prev_unpruned
  mkdir unpruned
  mv prev_unpruned unpruned
fi

if [ ! -d unpruned ]
then
   mkdir unpruned
fi

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

mv wav unpruned
mkdir wav

# Not yet tested if this is worthwhile to customize to the particular voice
PDAPARAMS='-fmin 70 -fmax 240'

cat $PROMPTFILE |
awk '{print $2}' |
while read fname
do
    echo $fname | awk '{printf("%s "),$1}'

    $ESTDIR/bin/pda -L $PDAPARAMS -otype est_ascii unpruned/wav/$fname.wav |
    awk 'BEGIN {id="'$fname'"; start=0.0}
         {if ($1 == "EST_Header_End")
            ofinterest=1;
          if (ofinterest == 1)
          {
             if ($3 > 1.0)
             {
                start=$1;
                ofinterest = 0;
             }
          }
         }
         END { if (start-'$SILMARGIN' < 0.0)
                  printf(" %f",0.0);
               else
                  printf(" %f",start-'$SILMARGIN');
             }'

    $ESTDIR/bin/pda -L $PDAPARAMS -otype est_ascii unpruned/wav/$fname.wav |
    $REVERSE |
    awk 'BEGIN {id="'$fname'"; end=0.0; ofinterest = 1}
         {if (NR == 1) 
          {
             fileend = $1;
             end = fileend;
          }
          if ($1 == "EST_Header_End")
            ofinterest=0;
          if (ofinterest == 1)
          {
             if ($3 > 1.0)
             {
                end=$1;
                ofinterest = 0;
             }
          }
         }
         END { if (end+'$SILMARGIN' > fileend)
                  printf(" %f\n",fileend); 
               else 
                  printf(" %f\n",end+'$SILMARGIN'); 
             }'
done |
awk '{printf("$ESTDIR/bin/ch_wave unpruned/wav/%s.wav -start %s -end %s -o wav/%s.wav\n",$1,$2,$3,$1)}' | sh -v






