#!/bin/sh
# Shell script to record sound files from unix style sound devices.
# Should auto detect most supported systems and record the file for you.
#
# Originally developed by Chris Bagwell (cbagwell@sprynet.com)
#
# 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 rec 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.
#
# January 9, 1999  - Chris Bagwell (cbagwell@sprynet.com)
#
#   Quoted $filename so that files with spaces in their names can
#   be given.
#   Arnim Rupp patch file to work with multiple sound devices via
#   command line option.
#

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

help()
{
  echo "rec v1.5 - front end to Sox"
  echo ""
  echo "Usage: rec [ fopts ] outfile [effects]"
  echo
  echo "fopts: -c channels -d device -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"
     ;;
   -d)
     shift
     device="$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`

echo "Send break (control-c) to end recording"

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

  if [ "$device" = "" ]; then
    device="/dev/audio"
  fi

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

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

  esac

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

    if [ "$device" = "" ]; then
      device="/dev/dsp"
    fi

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

    sox $volume -t ossdsp $device $fopts "$filename" $effects
  fi
fi
