#!/bin/sh

# Script for interfacing to the SGI program sfplay.  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.
# SFPLAY [-mute | -unmute] [-start start_sample] file1 [file2, ...]
#
# This script (and sfplay) only works for sampling frequencies of 8000, 11025,
# 16000, 22050, 32000, 44100, or 48000 Hz.

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

# 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: SFPLAY,v 1.7 1996/09/10 AFsp-V2R2 $

Usage="SFPLAY [-mute | -unmute] [-start start_sample] file1 [file2, ...]"

start=0
mute=1		# Default is mute
while [ $# -gt 0 ]
do
  case $1 in
  -start) start=$2; shift; shift;;
  -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'`
  if [ "$line" = "" ]; then
    exit 1
  fi

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

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

# Check the sampling frequency - sfplay only supports certain values
  sf_freqs="8000 11025 16000 22050 32000 44100 48000"

  # See if the sampling rate is within 10% of one of the standard rates
  match_freq=0
  for f in $sf_freqs
  do
     res=`expr \(  9 '*' $sfreq '<' 10 '*' $f \) \& \
               \( 11 '*' $sfreq '>' 10 '*' $f \)`
     if [ $res -eq 1 ]; then
       if [ $f -ne $sfreq ]; then
         echo "Using sampling frequency $f instead of $sfreq"
       fi
       sfreq=$f
       match_freq=1
       break;
     fi
  done
  if [ $match_freq -eq 0 ]; then
    echo "Unsupported sampling frequency: $sfreq" 2>&1
    echo "  -- try SGPLAY"
    exit 1
  fi

# Form the command line options
  if [ "$dtype" = "mu-law8" ]; then
    data_type="mulaw"
    data_size=1
  elif [ "$dtype" = "integer16" ]; then
    data_type="integer 16 2scomp"
    data_size=2
  elif [ "$dtype" = "float32" ]; then
    data_type="float 1.0"
    data_size=4
  else
    echo "Unsupported data format: $dtype" 1>&2
    exit 1
  fi

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

  if [ "$dbo" = "big-endian" ]; then
    byte_order="byteorder big"
  else
    byte_order="byteorder little"
  fi

  doffs=`expr $hlen + $start \* $data_size \* $nchan`

# Form the command line (stored in variable CMLn)
  cml="sfplay -inputraw $byte_order $data_type $chan_count rate $sfreq dataoff $doffs end $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
  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
