#!/bin/csh

# This script is used to convert a SAM speech signal and a SAM description
# file into PhonDat 1 speech file (Header version 1) for usage with 
# PhonDat standard software.

# F. Schiel	05.08.96 / 05.08.96

# Description:
# The script reads the necessary contents in the SAM description file,
# creates a standard PhonDat header (version 1) with all necessary 
# contents and cats the speechsignal after it.
# The output file has the same prefiy as the input files but the extension '.16'.
# E.g.  awed5480.SES  ->  awed5480.16
# The input file is exspected to be a standard SAM speech file (raw data) with
# the extension '.SES'. The script will look for a SAM description file 
# with the same prefix but the extension '.SEO'.


# This script requires
# - installed raw2pho
# - read/write access to /tmp
# - read/write access to the directory where the input files are stored

# Usage:
# sam2pho sam-speech-file1 [sam-speech-file2 ...]

if ( $1 == "" ) then
  echo usage:  $0 sam-speech-file1 \[sam-speech-file2 ...\]
  exit 1
endif

while ($1 != "") 
  set outfile = ${1:r}.16
  set labelfile = ${1:r}.SEO
  set filenam = ${outfile:t}
#debug  echo filenam = $filenam
#debug  echo input = $1
#debug  echo outspeech = $outfile
#debug  echo indescr = $labelfile
  set rate = `grep 'SAM:' $labelfile | awk '{print $2}'`
  set sex = `grep  'SPI:' $labelfile | awk '{print $2}'`
  set sex = `echo $sex|sed 's/^\(.\).*/\1/'`
  if ( ! ( $sex == "M" || $sex == "m" || $sex == "F" || $sex == "f" ) ) then
    echo WARNING: could not find sex of speaker in $labelfile
    set sex =
  endif
  if ($sex == "F" || $sex == "f") then
    set sex = W
  endif
  set date = `grep  'RED:.*/../..' $labelfile | sed 's/.*\(..\/..\/..\).*/\1/'`
#debug  echo date = $date
  if ($date != "") then
    set month = `echo $date | sed 's/\(..\)\/..\/../\1/'`
    set day = `echo $date | sed 's/..\/\(..\)\/../\1/'`
    set year = `echo $date | sed 's/..\/..\/\(..\)/\1/'`
    if ($year < 50) then
      @ year = $year + 100
    endif
    @ year = $year + 1900
  else
    set day = 0
    set month = 0
    set year = 0
  endif
  cat $1 | raw2pho rate=$rate filename=$filenam sex=$sex \
   day=$day month=$month year=$year   >! $outfile
  shift
end
