#!/bin/csh

# transforms wav file into NIST file with same prefix but '.nis' extension

# all header fields may be set from command line. If not set wav2nist will
# use default values which might be wrong

# usage wav2nist [options] file1.wav [file2.wav ... ]

# this script requires 'gawk'

set utterance_id = unknown
set sample_count = -1
set sample_n_bytes = 2
set channel_count = 1
set sample_coding = pcm
set sample_byte_format = 01
set sample_rate = 16000
set sample_sig_bits = 16
set sample_max = -1
set recording_date = `date "+%d-%m-%Y"`
set speaker_id = unknown
set speaker_sex = unknown

if ( $1 == "" ) then
  echo "usage: wav2nist [options] file1.wav [file2.wav ... ]"
  exit 
endif

# Actually do the argument parsing here

while ( "$1" != "" )
	switch ("$1")
	case *=*:
		set key = `echo $1 | cut -d= -f1`
		set val = `echo $1 | cut -d= -f2`
		eval "set $key "= \'"$val"\'
		unset key val
		shift
		breaksw
        default:
		break
        endsw
end

# end option parser

while ( $1 != "" )
  # transform wav file into raw file
  sox $1 -t raw ${1:r}.raw
  # get a few values if possible
  if ( $sample_count == -1 && $sample_n_bytes == 2 ) then
    set sample_count = `ls -l ${1:r}.raw | gawk '{print $5}'`
    @ sample_count = $sample_count / 2
  endif
  if ( $sample_count == -1 && $sample_n_bytes == 1 ) then
    set sample_count = `ls -l ${1:r}.raw | gawk '{print $5}'`
  endif
  if ( $utterance_id == "unknown" ) then
    set utterance_id = ${1:r}
  endif
  # write header
  echo "NIST_1A" >! ${1:r}.nis
  echo "   1024" >> ${1:r}.nis
  printf "utterance_id -s%d %s\n" `echo $utterance_id | gawk '{printf length($1)}'` $utterance_id >> ${1:r}.nis
  echo "sample_count -i $sample_count" >> ${1:r}.nis
  echo "sample_n_bytes -i $sample_n_bytes" >> ${1:r}.nis
  echo "channel_count -i $channel_count" >> ${1:r}.nis
  printf "sample_coding -s%d %s\n" `echo $sample_coding | gawk '{printf length($1)}'` $sample_coding >> ${1:r}.nis
  echo "sample_byte_format -s2 $sample_byte_format" >> ${1:r}.nis
  echo "sample_rate -i $sample_rate" >> ${1:r}.nis
  echo "sample_sig_bits -i $sample_sig_bits" >> ${1:r}.nis
  echo "sample_max -i $sample_max" >> ${1:r}.nis
  printf "recording_date -s%d %s\n" `echo $recording_date | gawk '{printf length($1)}'` $recording_date >> ${1:r}.nis
  printf "speaker_id -s%d %s\n" `echo $speaker_id | gawk '{printf length($1)}'` $speaker_id >> ${1:r}.nis
  printf "speaker_sex -s%d %s\n" `echo $speaker_sex | gawk '{printf length($1)}'` $speaker_sex >> ${1:r}.nis
  echo "end_head" >> ${1:r}.nis
  set headerlength = `cat ${1:r}.nis | wc -c`
  echo headerlength = $headerlength
  @ zerolength = 1024 - $headerlength 
  while ( $zerolength > 0 )
    echo -n " " >> ${1:r}.nis
    @ zerolength --
  end
  cat ${1:r}.raw >> ${1:r}.nis
  /bin/rm ${1:r}.raw
  shift
end

