#!/bin/sh
#
# soxeffect - When this script is ran using a different name then soxeffect
# it will run sox using that name as the effect.  It uses stdin/stdout
# to grab data and output data and so is mainly useful in pipes.
# 
# Example usage:
#   ln -s soxeffect lowpass
#   rec | lowpass | play
#
# TODO: It would be nice to specify different output parameters then
# the input format.
#

SOX=/usr/local/bin/sox

help()
{
  echo "soxeffect v1.0 - effects front end to Sox"
  echo ""
  echo "Usage: [effectname] [ fopts ] [effectopts]"
  echo
  echo "When ran as the name of an effect that Sox supports, it will take"
  echo "audio data from stdin, apply the effect, and write the output back"
  echo "to stdout.  This means that [ fopts ] need to be given so that"
  echo "sox will know what format the audio data is in."
  echo
  echo "effectname: avg/band/bandpass/bandreject/chorus/compand/copy/cut/deemph/earwax/echo/echos/fade/filter/flanger/highp/highpass/lowp/lowpass/map/mask/noiseprof/pan/phaser/pick/pitch/polyphase/rate/repeat/resample/reverb/reverse/speed/split/stat/stretch/swap/trim/vibro/vol"
  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 "See sox man page for more info on required effects options."
}

NAME=$0
case $NAME in
	*/*)
		NAME=`echo $NAME | sed "s'^.*/''"`
	;;
esac

while [ $# -ne 0 ] # loop over arguments
do case $1 in
   -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"
     ;;
   -*)
     fopts="$fopts $1"
     ;;
   *)
     effectopts="$@"
     break;
     ;;
   esac
   shift
done

case $NAME in
	*sox)
		exec $SOX $*
	;;
	*avg|*band|*bandpass|*bandreject|*chorus|*compand|*copy|*cut|*deemph|*earwax|*echo|*echos|*fade|*filter|*flanger|*highp|*highpass|*lowp|*lowpass|*map|*mask|*pan|*phaser|*pick|*pitch|*polyphase|*rate|*resample|*reverb|*reverse|*speed|*split|*stat|*stretch|*swap|*trim|*vibro|*vol)
		$SOX $volume $fopts - $fopts - $NAME $effectopts
	;;
esac
