#!/bin/csh

# reads files from command line and test them for being correct 
# NIST SPHERE sound files (optionally play them p=1)

# If invoked without options the script simply returns the number
# of uncorrect NIST SPHERE files (0 : everything is ok). If a severe 
# error occurs the script returns -1
# The option v controls debug output (default 0)
# The option p causes the script to play the signal to /dev/audio
# NOTE: The helpers defined here should be probably replaced by 
#       your local available helpers. Search for line with 'HELPERS'

# F. Schiel	16.06.98 / 31.08.98

# 13.08.98 : corrected bug : sample_sig_bits are reported as sample_rate
#            playing faster using ...

# Preset Options

set v = 0
set p = 0

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

if ( $1 == "" ) then
  echo 'usage: test_nist [v=0][p=0] file1.nist [file2.nist ...]'
  exit -1
endif

set stat = 0
while ( $1 != "" )
  set errflag = 0
  set swap = 0
  # check for basic things (magic number and size)
  set title = `head -1 $1`
  if ( $title != "NIST_1A" ) then
    echo "ERROR: $1 has no magic number (NIST_1A)"
    @ stat = $stat + 1
    shift
    continue
  endif
  set size = `head -2 $1 | grep '^   '`
  if ( $size == "" ) then
    echo "ERROR: $1 has no size of header"
    @ stat = $stat + 1
    shift
    continue
  endif
  @ size = $size * 1
  if ( $v > 1 ) echo $1 : size = $size
  # copy header part into seperate file to speed up
  set base = {1:t}
  set temp = /tmp/${base:r}.head
  head -c $size $1 >! $temp
  # check for required settings
  set sample_count = `grep '^sample_count' $temp | gawk '{print $3}'`
  if ( $v > 0 ) echo $1 : sample_count = $sample_count
  if ( $sample_count == "" ) then
    echo "ERROR: $1 : missing required key sample_count"
    @ errflag = $errflag + 1
  endif
  set sample_n_bytes = `grep '^sample_n_bytes' $temp | gawk '{print $3}'`
  if ( $v > 0 ) echo $1 : sample_n_bytes = $sample_n_bytes
  if ( $sample_n_bytes == "" ) then
    echo "ERROR: $1 : missing required key sample_n_bytes"
    @ errflag = $errflag + 1
  endif
  set channel_count = `grep '^channel_count' $temp | gawk '{print $3}'`
  if ( $v > 0 ) echo $1 : channel_count = $channel_count
  if ( $channel_count == "" ) then
    echo "ERROR: $1 : missing required key channel_count"
    @ errflag = $errflag + 1
  endif
  @ sollbytes = $sample_count * $sample_n_bytes
  @ sollbytes = $channel_count * $sollbytes
  set istbytes = `ls -l $1 | gawk '{print $5}'`
  @ istbytes = $istbytes - $size
  if ( $v > 1 ) echo $1 : istbytes = $istbytes
  if ( $v > 1 ) echo $1 : sollbytes = $sollbytes
  if ( $istbytes != $sollbytes ) then
    echo "ERROR: $1 : wrong size calculation"
    @ errflag = $errflag + 1
    echo "       (channel_count or sample_count or sample_n_bytes)"
  endif
  set sample_coding = `grep '^sample_coding' $temp | gawk '{print $3}'`
  if ( $v > 0 ) echo $1 : sample_coding = $sample_coding
  if ( $sample_coding == "" ) then
    echo "ERROR: $1 : missing required key sample_coding"
    @ errflag = $errflag + 1
  endif
  if ( $sample_coding != "pcm" && $sample_coding != "alaw" && $sample_coding != "ulaw" ) then
    echo "WARNING: $1 : unknown sample_coding : $sample_coding"
  endif
  set sample_byte_format = `grep '^sample_byte_format' $temp | gawk '{print $3}'`
  if ( $v > 0 ) echo $1 : sample_byte_format = $sample_byte_format
  if ( $sample_byte_format == "" ) then
    echo "ERROR: $1 : missing required key sample_byte_format"
    @ errflag = $errflag + 1
  endif
  if ( $sample_byte_format != "01" && $sample_byte_format != "10" && \
       $sample_byte_format != "1" ) then
    echo "WARNING: $1 : unknown sample_byte_format : $sample_byte_format"
  endif
  if ( $sample_byte_format == "10" ) set swap = 1
  set sample_rate = `grep '^sample_rate' $temp | gawk '{print $3}'`
  if ( $v > 0 ) echo $1 : sample_rate = $sample_rate
  if ( $sample_rate == "" ) then
    echo "ERROR: $1 : missing required key sample_rate"
    @ errflag = $errflag + 1
  endif
  set sample_sig_bits = `grep '^sample_sig_bits' $temp | gawk '{print $3}'`
  if ( $v > 0 ) echo $1 : sample_sig_bits = $sample_sig_bits
  if ( $sample_sig_bits == "" ) then
    echo "WARNING: $1 : missing required key sample_sig_bits"
  endif
  if ( $sample_n_bytes == 2 && $sample_sig_bits > 16 ) then
    echo "ERROR: $1 : sample_sig_bits > 16 while sample_n_bytes = 2"
    @ errflag = $errflag + 1
  endif
  if ( $sample_n_bytes == 1 && $sample_sig_bits > 8 ) then
    echo "ERROR: $1 : sample_sig_bits > 8 while sample_n_bytes = 1"
    @ errflag = $errflag + 1
  endif
  if ( $sample_sig_bits < 0 ) then
    echo "ERROR: $1 : sample_sig_bits < 0 "
    @ errflag = $errflag + 1
  endif
  set end_head = `grep '^end_head' $temp`
  if ( $end_head == "" ) then
    echo "ERROR: $1 : missing required end_head marker"
    @ errflag = $errflag + 1
  endif
  /bin/rm -f $temp
  if ( $errflag != 0 ) @ stat = $stat + 1 
  # finally try to play the sound part
  # setting of 'sample_sig_bits' is ignored here!
  # EDIT FOLLOWING LINES TO INSERT YOUR LOCAL HELPERS
  if ( $p == 1 ) then
   if ( $errflag == 0 ) then
     if ( $channel_count == 1 ) then
       if ( $sample_coding == "pcm" ) then
         if ( $sample_n_bytes == 2 ) then
           if ( $sample_byte_format == "01" ) then
             hear_raw skip=$size rate=$sample_rate swap=$swap $1
           else
             hear_raw skip=$size rate=$sample_rate $1
           endif
         else
           echo "$1 : playing of pcm, 8bit audio not implemented 
         endif
       endif
       if ( $sample_coding == "ulaw" ) then
         if ( $sample_n_bytes == 1 ) then
           echo playing ulaw : header not skipped, may cause burst in signal
           lplay u $1
         else
           echo "$1 : playing of ulaw, 16bit audio not implemented 
         endif
       endif
       if ( $sample_coding == "alaw" ) then
         if ( $sample_n_bytes == 1 ) then
           echo playing alaw : header not skipped, may cause burst in signal
           lplay a $1
         else
           echo "$1 : playing of alaw, 16bit audio not implemented 
         endif
       endif
     else
       echo "$1 : playing of multi-channel audio not implemented 
     endif
   else
     echo "$1 : cannot play because of wrong required keys (see above)"
   endif
  endif

  shift
end
exit $stat
