#!/bin/sh
#####################################################-*-mode:shell-script-*-
##                                                                       ##
##                   Carnegie Mellon University and                      ##
##                   Alan W Black and Kevin A. Lenzo                     ##
##                      Copyright (c) 1998-2000                          ##
##                        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.                                                       ##
##                                                                       ##
###########################################################################
###                                                                      ##
###  Prompt and record diphone set                                       ##
###                                                                      ##
###########################################################################

if [ $# = 0 ]
then
   echo "prompt and record diphone nonsense words"
   echo "Usage: prompt_them DATALIST [POSITION] "
   echo "   DATALIST list of utterance prompts e.g. etc/MUMBLE.data"
   echo "   POSITION which utterance to start/restart at, default is 1"
   exit 1
fi

if [ ! "$ESTDIR" ]
then
   echo "environment variable ESTDIR is unset"
   echo "set it to your local speech tools directory e.g."
   echo '   bash$ export ESTDIR=/home/awb/projects/speech_tools/'
   echo or
   echo '   csh% setenv ESTDIR /home/awb/projects/speech_tools/'
   exit 1
fi

if [ ! -d prompt-wav ]
then
   echo "can't find prompt-wav/ directory."
   echo "are you in the right directory?"
   exit 1
fi

POSITION=1
if [ $# = 2 ]
then
   POSITION=$2
fi

cat $1 | grep "^ *(.*) *$" |
awk '{if (NR >= '$POSITION') print $0}' |
while read lll
do
   echo 24 | awk '{for(i=1; i<=$1; i++)
                   printf("\n");}'
   echo "--------------------------------------------------------------"
   echo -n $POSITION " "
   echo $lll | awk '{print $2}' 
   echo
   echo $lll | tr -d '"' | 
   awk '{ l=0;
         printf("   ");
         for(i=3; i<NF; i++)
         {
            if (l > 64)
            {
               printf("\n   "); 
               l = 0;
            }
            printf("%s ",$i);
            l += length($i)+1;
         }
         printf("\n")}' 
   echo
   f=`echo $lll | awk '{print $2}'`

   # Take duration from the prompt-wav
   duration=`$ESTDIR/bin/ch_wave -info prompt-wav/$f.wav | awk '{if ($1 == "Duration:") printf("%d\n",$2+1.5)}'`
   # Take duration from number of words (when no prompt-wavs are available
#   duration=`echo $lll | awk '{printf("%d\n",4.5+(NF/2.5))}'`

   # If you don't want to hear the prompt comment out the following line
   $ESTDIR/bin/na_play prompt-wav/$f.wav

#   echo press retrun to start recording
#   read x </dev/tty

   echo start recording for $duration seconds ...
   $ESTDIR/bin/na_record -f 16000 -time $duration -o wav/$f.wav
   echo ... end recording

   POSITION=$[$POSITION+1]

   # If you don't want to hear the review comment out the following lines
#   echo Review $f
#   $ESTDIR/bin/na_play prompt-wav/$f.wav
#   $ESTDIR/bin/na_play wav/$f.wav

done

