home *** CD-ROM | disk | FTP | other *** search
/ PC User 2003 January / Disc 3 / Amethyst.iso / live / usr / bin / play < prev    next >
Encoding:
Text File  |  2001-03-10  |  4.7 KB  |  200 lines

  1. #!/bin/sh
  2. # Shell script to play/record sound files to/from unix style sound devices.
  3. # Should auto detect most supported systems.
  4. #
  5. # Originally developed by Chris Bagwell (cbagwell@sprynet.com)
  6. #
  7. #   TODO:  Put each set of fopts and filenames on an array and then 
  8. #          play each filename back with the given effect.h
  9. #
  10. # Change History:
  11. #
  12. #   Major updates have been supplied by Kjetil Torgrim Homme and 
  13. #   Kirk Goff.
  14.  
  15. # Set up path so that it can find Sox if user's path doesn't already
  16. # include it.
  17. PATH=$PATH:/usr/local/bin
  18. program_name=`basename $0`
  19. program_version="2.0"
  20.  
  21. if [ -z "$1" ]; then
  22.     echo "\
  23. $program_name: too few arguments
  24. Try \`$program_name --help' for more information." 1>&2
  25.     exit 1
  26. fi
  27.  
  28. version()
  29. {
  30.     echo "$program_name (sox) $program_version"
  31.     exit 0
  32. }
  33.  
  34. help()
  35. {
  36.     echo "\
  37. Usage: $program_name [OPTION]... FILE [EFFECT]...
  38. Play/record sound files to/from unix style sound devices.
  39.  
  40.   -c, --channels=CHANNELS      specifies the number of sound channels in FILE
  41.   -d, --device=DEVICE          use DEVICE for input/output
  42.   -f, --format=FORMAT          specifies bit format of sample
  43.                                FORMAT is either s, u, U, A, a, or g
  44.   -r, --rate=RATE              sample rate in hertz of FILE
  45.   -s, --size=SIZE              interpret size of sample
  46.                                SIZE is either b, w, l, f, d, or D
  47.   -t, --type=TYPE              specifies file format of FILE
  48.   -v, --volume=VOLUME          change amplitude
  49.   -x, --xinu                   reverse bit order of sample
  50.                                (only works with 16-bit and 32-bit integer data)
  51.       --file                   next argument is FILE
  52.   -h, --help                   display this help and exit
  53.       --version                output version information and exit
  54.  
  55. EFFECTs are one or more of the following:  avg, band, chorus, copy, cut, 
  56. deemph, echo, echos, flanger, highp, lowp, map, mask, phaser, pick, polyphase
  57. rate, resample, reverb, reverse, split, stat, vibro.
  58.  
  59. See sox man page for detailed information on supported file types, data
  60. formats, and effect options."
  61.     exit 0
  62. }
  63.  
  64.  
  65. # loop over arguments
  66. while [ $# -ne 0 ]; do
  67.     case "$1" in
  68.     avg|band|bandreject|chorus|compand|copy|cut|deemph|echo|echos|filter|flanger|highp|lowp|map|mask|pan|phaser|pick|pitch|polyphase|rate|resample|reverb|reverse|speed|split|stat|stretch|vibro|vol)
  69.         effects="$@"
  70.         break
  71.         ;;
  72.     -c)
  73.         shift
  74.         fopts="$fopts -c $1"
  75.         ;;
  76.     --channels=*)
  77.         fopts="$fopts -c `echo $1 | sed 's/.*=//'`"
  78.         ;;
  79.     -d)
  80.         shift
  81.         device="$1"
  82.         ;;
  83.     --device=*)
  84.         device=`echo $1 | sed 's/.*=//'`
  85.         ;;
  86.     -f)
  87.         shift
  88.         fopts="$fopts -$1"
  89.         ;;
  90.     --format=*)
  91.         fopts="$fopts -`echo $1 | sed 's/.*=//'`"
  92.         ;;
  93.     -r)
  94.         shift
  95.         fopts="$fopts -r $1"
  96.         ;;
  97.     --rate=*)
  98.         fopts="$fopts -r `echo $1 | sed 's/.*=//'`"
  99.         ;;
  100.     -s)
  101.         shift
  102.         fopts="$fopts -$1"
  103.         ;;
  104.     --size=*)
  105.         fopts="$fopts -`echo $1 | sed 's/.*=//'`"
  106.         ;;
  107.     -t)
  108.         shift
  109.         fopts2="$fopts -t $1"
  110.         ;;
  111.     --type=*)
  112.         fopts2="$fopts -t `echo $1 | sed 's/.*=//'`"
  113.         ;;
  114.     -v)
  115.         shift
  116.         volume="-v $1"
  117.         ;;
  118.     --volume=*)
  119.         volume="-v `echo $1 | sed 's/.*=//'`"
  120.         ;;
  121.     -x|--xinu)
  122.         fopts="$fopts -x"
  123.         ;;
  124.     --file)
  125.         shift
  126.         if [ -z "$filename" ]; then
  127.         filename="$1"
  128.         else
  129.         echo "Filename already given.  Ignoring extra name: $1" 1>&2
  130.         fi
  131.         ;;
  132.     -h)
  133.         help
  134.         ;;
  135.     --help)
  136.         help
  137.         ;;
  138.     --version)
  139.         version
  140.         ;;
  141.     -)
  142.         filename="-"
  143.         ;;
  144.     -*)
  145.         fopts="$fopts $1"
  146.         ;;
  147.     *)
  148.         if [ -z "$filename" ]; then
  149.         filename="$1"
  150.         else
  151.         echo "Filename already given.  Ignoring extra name: $1" 1>&2
  152.         fi
  153.         
  154.         ;;
  155.     esac
  156.     shift
  157. done
  158.  
  159. arch=`uname -s`
  160. case $arch in
  161.   SunOS)
  162.     case `uname -r` in
  163.         # Solaris software can auto-detect hardware capabilities.
  164.         5.*)
  165.         arch_defines="-t sunau"
  166.         ;;
  167.     # For SunOS default to signed words.  Some hardware can only play u-law and would need
  168.     # to be changed here.
  169.     *)
  170.         arch_defines="-t sunau -w -s"
  171.         ;;
  172.     esac
  173.     if [ -z "$device" ]; then
  174.     device="/dev/audio"
  175.     fi
  176.     ;;
  177.   Linux|FreeBSD)
  178.     arch_defines="-t ossdsp"
  179.     if [ -z "$device" ]; then
  180.     device="/dev/dsp"
  181.     fi
  182.     ;;
  183. esac
  184.  
  185. # If name is "rec" then record else assume user is wanting to play
  186. # a sound file.
  187. if [ "$program_name" = "rec" ]; then
  188.  
  189.     # Don't send data to stdout if they are reading from it.
  190.     if [ "$filename" = "-" ]; then
  191.       echo "Send break (control-c) to end recording" 1>&2
  192.     else
  193.       echo "Send break (control-c) to end recording"
  194.     fi
  195.     sox $volume $arch_defines $fopts $device $fopts2 "$filename" $effects 
  196.  
  197. else
  198.     sox $volume $fopts $fopts2 "$filename" $arch_defines $device $effects
  199. fi
  200.