#!/bin/sh

# Script for interfacing to the Entropic WAVES SGI program sgplay.  The
# playback is intended for output from the line output of the SGI workstation.
# By default it mutes the SGI workstation internal speaker during playback
# SGPLAY [-mute | -unmute] file1 [file2, ...]
#
# This script (and sgplay for headerless files) only works for files with
# native byte-order 16-bit data.

# Run InfoAudio to determine the format of an audio file.  This script
# parses that information and sets up the appropriate parameters for sgplay.
# Note that after execution of this procedure, the SGI speaker will be unmuted.

# The documented -r option on sgplay does not work.  We have to use -s to avoid
# the click due to headers.

# 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: SGPLAY,v 1.3 1996/02/17 AFsp-V2R1 $

Usage="SGPLAY [-mute | -unmute] [-start start_sample] [-end end_sample] file1 [file2, ...]"

start=0
end=-1
mute=1
while [ $# -gt 0 ]
do
  case $1 in
  -unmute) mute=0; shift;;
  -mute)  mute=1; 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

  sfreqn=`echo $sfreq | sed 's/\..*//'`
  if [ "$sfreqn" != "$sfreq" ]; then
    echo "Fractional sampling rate $sfreq changed to $sfreqn"
    sfreq=$sfreqn
  fi

# Form the command line options
  if [ "$dtype" != "integer16" ]; then
    echo "Unsupported data type" 2>&1
  fi
  if [ "$nchan" -ne 1 -a "$nchan" -ne 2 ]; then
    echo "Error: Unsupported number of channels: $nchan" 1>&2
    exit 1
  fi

  if [ "$dbo" != "$hbo" ]; then
    echo "Unsupported byte order: $dbo" 2>&1
    exit 1
  fi

  offms=`expr $hlen \* 1000 \/ $sfreq \/ 2 + 1`  # header in ms (+1)
  offs=`echo 0000$offms | sed 's/\(.*\)\(...\)/\1\.\2/' | sed 's/^0*0/0/'`

# Form the command line (stored in variable CMLn)
  cml="sgplay -s $offs -c $nchan -f $sfreq $file"
  eval "CML$n=\"$cml\""

done
N=$n

# Set up trap to unmute the speaker
trap 'if [ $mute -eq 1 ]; then apanel -nodisplay -unmute; fi; exit' \
	1 2 3 15

# Mute the speaker
if [ $mute -eq 1 ]; then
  apanel -nodisplay -mute
else
  apanel -nodisplay -unmute
fi

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