#!/bin/bash
# labelling script for f0 contours.

# Expects the listfile to be called:
#   labeller_dataset_number
#   eg: rob_nina_3

htmp=${HOSTNAME#*.}
net=${htmp%%.*}

echo $net

if [ "$net" = "inf" ] ; then
    DICE="true"
    echo "This is DICE"
fi




# Set Environment
if [ -z "$ESPS_BASE" ] ; then

  if [ -z "$DICE" ] ; then
  export ESPS_BASE=/cstr/linux/entropic/esps531.linux
  export PATH=$PATH:$ESPS_BASE/bin
  else
  export ESPS_BASE=/group/cstr/tools/entropic/esps531.linux
  export PATH=$PATH:$ESPS_BASE/bin
  fi
fi


if [ -z "$DICE" ] ; then
root=/projects/cougar/data/cstr
else 
root=/group/cstr/projects/cougar/data/cstr
fi

labdir=$HOME/labelling
list=$1

if [ ! -r "$list" ] ; then
    list=$root/labelling/$list
    if [ ! -r "$list" ] ; then
	echo "Usage: $0 listfile"
    fi
fi

# check for labelling directory
if [ ! -d "$labdir" ] ; then
    echo "making directory $labdir"
    mkdir $labdir
fi

tname=${list#*_}
data=${tname%_*}


wavroot=$root/$data/wav
f0root=$root/$data/f0
wordroot=$root/$data/word
f0menu=$root/labelling/f0.menu
ismenu=$root/labelling/is.menu
wordmenu=$root/labelling/word.menu

# start xwaves if necessary

if [ -z "`ps -a | grep \"xwaves\"`" ] ; then
  echo "Starting xwaves"
  xwaves &
  sleep 2
fi

# attach xlabel
send_xwaves attach function xlabel 

# loop through given files
for name in `cat $list` ; do

echo "Loading file set $name"

  wave=$wavroot/$name.wav
  f0=$f0root/$name.f0
  wordlab=$wordroot/$name.word
  f0lab=$labdir/$name.f0.lab
  islab=$labdir/$name.is.lab

  send_xwaves make name $name file $wave width 1100 height 150 loc_x 10 loc_y 60 
  send_xwaves make name $name file $f0   width 1100 height 200 loc_x 10 loc_y 250
  send_xwaves $name  op file $f0 op "bracket horiz. markers"
  send_xwaves $name activate file $f0 op clear identifiers k1 ac_peak rms "prob_voice"
  send_xwaves send make signal $f0 name $name file $islab color 125 menufile $ismenu
  send_xwaves send activate fields 1 2 3
  send_xwaves send make signal $f0 name $name file $f0lab color 125 menufile $f0menu
  send_xwaves send activate fields 1 2 3
  send_xwaves send make signal $f0 name $name file $wordlab color 125 menufile $wordmenu
  send_xwaves send activate fields 1

  send_xwaves $name align file $wave
  send_xwaves pause
  send_xwaves kill
done
