#!/bin/csh -f
  # script to simplify the playing of multiple soundfiles in succession
  # A.S. 8/95, revised for Linux 7/99, revised for aplay, float, aiff  7/03

cd $SFDIR

if ($#argv == 0) then
echo " syntax:  play soundfile [soundfile2 soundfile3 ... soundfileN}"
echo '  The command "play" can be abbreviated "p"'
echo " For soundfiles whose names end with the extensions .wav, .aif or .aiff"
echo ' the extension can be omitted from the "play" command line.'
echo 'Play can play  44.1,48,88.2 and 96k WAVE soundfiles at 16 or 24 bit int or'
echo '32 bit float resolution, 1,2 or 4 channels.'
echo " Thus, to play a soundfile called "groove2.wav" one could type either"
echo "      play  groove2.wav    or else    p groove2"
echo 'Metacharacters can be used but must be preceded by a backslash.'
echo "Example: play  pow\*"
echo 'Result: All soundfiles in your $SFDIR that begin with the character string "pow" will be played.'
endif

foreach i ($argv)
   if ( -e $i ) then
    	  set SF = $i
   else if ( -e $i.wav ) then
	set SF = $i.wav
   else if ( -e $i.aif ) then
	set SF = $i.aif
   else if ( -e $i.aiff ) then
	set SF = $i.aiff
   else
          echo " *** ERROR: *** "
          echo " No soundfile named >> $i << "
          echo "     (or $i.wav, $i.aif or $i.aiff)"
          echo " *** (Your current working soundfile directory is $SFDIR) ***"
          exit 1
   endif

   set FMT = `sfcheck $SF  | awk '{print $1}'`
   set DUR = `sfdur $SF  | awk '{print $1}'`
   set SR = `sfsr $SF | awk '{print $1}'`
   set BITS = `sfbits $SF | awk '{print $1}'`
   set CHANS = `sfchans $SF | awk '{print $1}'`
 # choose play command based on format
set PLAYCOMM = sndplay

# OLD caveate on AIFF removed 
#if ( $FMT == "AIFF" ) then
# echo "ERROR: soundfile $SF  is in AIFF format." 
# echo "The AIFF format play option currently is broken. Use sweep to play AIFF  soundfiles."
# exit 1
#endif
# Restore when ecasound is fixed to read AIFF files
#  ecasound -a:1 "-f:s$BITS_be,$CHANSch,$SR,i -i $SF -o alsahw,0,0

if ( $BITS == "32" ) then
  set PLAYCOMM = playfloat
endif

# aplay gives format but not duration so throw away it's sterr  output
      # echo "  >> $SF <<  $DUR seconds $CHANS chan"
      echo "  >> $SF <<  $DUR seconds $SR sr $BITS-bit $CHANS chan"
      $PLAYCOMM $SF  >& /dev/null
      sleep 1
end
