#!/bin/sh

# Script for interfacing to the TCT datlink program naplay.
# NAPLAY [-start start_sample] [-end end_sample] file1 [file2, ...]
#
# Run InfoAudio to determine the format of an audio file.  This script
# parses that information and sets up the appropriate parameters for naplay.

# Environment variables:
# AUDIOPATH:     colon separated list of directories to look for audio files
# RAWAUDIOFILE:  controls assumed format for headerless files, see the man
#                page or file header for InfoAudio for more details.
#                The default settings used by this script correspond to the
#	         following settings of RAWAUDIOFILE.
#                 16-bit data, 0 byte header, 8000 Hz sampling rate, native
#                 byte order, 1 channel, scale factor 1.0
#                % setenv RAWAUDIOFILE "integer16,0,8000,native,1,1.0"

# $Id: NAPLAY,v 1.4 1996/02/17 AFsp-V2R1 $

Usage="NAPLAY [-start start_sample] [-end end_sample] file1 [file2, ...]"

start=0
end=
while [ $# -gt 0 ]
do
  case $1 in
  -start) start=$2; shift; shift;;
  -end)   end=$2; shift; shift;;
  -h)     echo $Usage; exit;;
  *)      break;;
  esac
done

if [ $# -eq 0 ]; then
  echo $Usage 1>&2
  exit 1
fi

# Set the environment variable RAWAUDIOFILE if it is not already set
if [ "$RAWAUDIOFILE" = "" ]; then
  RAWAUDIOFILE="integer16,0,8000,native,1,1.0"
  export RAWAUDIOFILE
fi

# Loop over file arguments
n=0
filelist="$*"
for file in $filelist
do

  n=`expr $n + 1`

# Print the header info
  InfoAudio -i1 $file

# Pick off the file format information
  line=`InfoAudio -i2 $file | sed -n 's/^File name: //p; \
	s/^Header length: //p; s/^Sampling frequency: //p; \
	s/^No. channels: //p; s/^Data type: //p; \
	s/^File byte order: //p; s/^Host byte order: //p'`
  if [ "$line" = "" ]; then
    exit 1
  fi

  set $line
  file=$1
  hlen=$2
  sfreq=$3
  nchan=$4
  dtype=$5
  dbo=$6
  hbo=$7

# Form the command line options
  if [ "$dtype" = "mu-law8" ]; then
    encoding=ULAW
    data_size=1
  elif [ "$dtype" = "integer16" ]; then
    encoding=linear
    data_size=2
  elif [ "$dtype" = "float32" ]; then
    encoding=IEEE
    data_size=4
  else
    echo "Error: Unknown data format" 1>&2
    exit 1
  fi

  if [ "$dbo" = "$hbo" ]; then
    dswap="native"
  else
    dswap="swap"
  fi

  if [ "$nchan" -eq 1 ]; then
    chan=mono
  elif [ "$nchan" -eq 2 ]; then
    chan=stereo
  else
    echo "Error: Unsupported number of channels" 1>&2
  fi

  recl=`expr $data_size \* $nchan`
  hsamples=`expr \( $hlen + $recl - 1 \) / $recl`

  dstart=`expr $hsamples + $start`
  if [ "$end" != "" ]; then
    dend=`expr $hsamples + $end`
  else
    dend=
  fi

# Form the command line (stored in variable CMLn)
  cml="naplay -e $encoding -f $dswap -o $chan -r $dstart:$dend -s $sfreq $file"
  eval "CML$n=\"$cml\""

done
N=$n

# Playback loop
while true
do
  n=0
  blanks=""
  for file in $filelist
  do
    n=`expr $n + 1`
    if [ $N -gt 1 ]; then
      echo "${blanks}File $n: $file"
    fi
    eval "\$CML$n"
    blanks=$blanks"  "
    sleep 1
  done
done