#!/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.
#
# Originally developed by Chris Bagwell (cbagwell@sprynet.com)
#
#   TODO:  Put each set of fopts and filenames on an array and then 
#          play each filename back with the given effect.h
#
# Change History:
#
# June 1, 1998 - Chris Bagwell (cbagwell@sprynet.com)
#
#   Kjetil Torgrim Homme <kjetilho@ifi.uio.no> sent in a neat patch to
#   attempt an educated guess on how to play sound on sun hardware.
#   There is probably a better way to do it in the actual software though.
#
#   Use the parsed out volume flag to have sox change the volume.  Yes, its
#   better to use the audio devices hardware mixer to adjust volume but some
#   way is better then no way.  Its still parsed seperately so people may
#   optionally pass the parameter to a mixer.
#
# September 7, 1998 - Chris Bagwell (cbagwell@sprynet.com)
#
#   Updated usage checking a little more so that only one filename can
#   be given.
#

# Set up path to sox so that it can find it if user's path doesn't already
# include it.
PATH=$PATH:/usr/local/bin
export PATH

help()
{
  echo "play v1.4 - front end to Sox"
  echo ""
  echo "Usage: play [ fopts ] infile [effects]"
  echo
  echo "fopts: -c channels -h -r rate -t type -v volume -s/-u/-U/-A -b/-w/-l/-f/-d/-D -x"
  echo
  echo "effects: avg/band/chorus/copy/cut/deemph/echo/echos/flanger/highp/lowp/map/mask/phaser/pick/polyphase/rate/resample/reverb/reverse/split/stat/vibro"
  echo ""
  echo "See sox man page for more info on required effects options."
}

if [ "$1" = "" ] ; then
  help; exit 1;
fi

while [ $# -ne 0 ] # loop over arguments
do case $1 in
   avg|band|chorus|copy|cut|echo|echos|flanger|highp|lowp|map|mask|phaser|pick|pred|rate|resample|reverb|reverse|split|stat|vibro)
     effects="$@"
     break
     ;;
   -c)
     shift
     fopts="$fopts -c $1"
     ;;
   -h)
     help;
     exit 1;
     ;;
   -r)
     shift
     fopts="$fopts -r $1"
     ;;
   -t)
     shift
     fopts="$fopts -t $1"
     ;;
   -v)
     shift
     volume="-v $1"
     ;;
   -)
     filename="-"
     ;;
   -*)
     fopts="$fopts $1"
     ;;
   *)
     if [ "$filename" = "" ]; then
       filename="$1"
     else
       echo "Filename already give.  Ingoring extra name: $1"
     fi
     ;;
   esac
   shift
done

arch=`uname -s`

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

  case `arch -k` in
    sun4|sun4c|sun4d)
      # Use below for older Sun audio hardware
      sox $volume $fopts $filename -t sunau -U -c 1 /dev/audio $effects
      ;;

    *)
      # Use below for newer Sun audio hardware that supports stereo linear
      sox $volume $fopts $filename -t sunau -w -s /dev/audio $effects
      ;;

  esac

else
  if [ "$arch" = "Linux" ]; then

# Possible way to set volume
#    if [ "$volume" != "" ] ; then
#      mixer $volume
#    fi

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