#!/bin/sh
# Shell script to play sound files to unix style sound devices.
# Should auto detect most supported systems and play the file for you.
#
# Default is to covert sound into Signed Linear PCM data for Sun audio
# devices.  Older hardware can only handle mono ulaw and alaw.  If this is
# true for your system then change commandline to always force to ulaw and
# one channel 1.
#
# TODO: -v volume option is being parsed but requires an external
# program to use its value to set the playing volume.
#

# Make sure play can find its way to sox program incase users path
# doesn't already include it.
PATH=$PATH:/usr/bin
export PATH

if [ "$1" = "" ] ; then
  echo "play v1.2 - front end to Sox"
  echo ""
  echo "Usage: [ fopts ] infile [effects]"
  echo "fopts: -r rate -v volume -c channels -s/-u/-U/-A -b/-w/-l/-f/-d/-D -x"
  echo "effects: copy/rate/avg/stat/echo/vibro/lowp/band"
  echo ""
  echo "See sox man page for more info on required effects options."
  exit
fi

for i
do case $1 in
   copy|rate|avg|stat|echo|vibro|lowp|band)
     break
     ;;
   -v)
     shift
     volume=$1
     ;;
   -)
     fopts="$fopts /dev/stdin"
     ;;
   *)
     fopts="$fopts $1"
   esac
   if test $# -gt 0
   then shift
   fi
done

arch=`uname -s`

if [ "$arch" = "SunOS" ]; then

# Use below for newer Sun audio hardware that supports stereo linear
  sox $fopts -t sunau -w -s /dev/audio $@

# Use below for older Sun audio hardware
#  sox $fopts -t sunau -U -c 1 /dev/audio $@

else
  if [ "$arch" = "Linux" ]; then
    if [ "$volume" != "" ] ; then
      mixer $volume
    fi

    # Best to always use highest quality output for sound.
    sox $fopts -t ossdsp -w -s /dev/dsp $@
  fi
fi
