#!/bin/csh -f
# playsflib (psfl) : script to play sflib soundfiles
# Linux version, revised  July 1999  A.S., revised for 96k sflib96 7/03

# cd $SFLIB

if ($#argv == 0) then
echo " syntax:  playsflib soundfile [soundfile2 soundfile3 ... soundfileN}"
echo ' The command "playsflib" can be abbreviated "psfl"'
echo ' Soundfiles from any 44.1k sflib or 96k sflib96 subdirectory can be played simply by'
echo ' typing the soundfile name (it is not necessary to include full paths to the soundfiles).'
echo ' The filename extension .wav can be omitted from the "playsflib" command line.'
echo ' Thus, to play soundfiles "/sflib/wind/fl.c4.wav" and "sflib96/brass/btrp/open/btrp.ff.a3.wav" one could type either'
echo "      playsflib  fl.c4.wav  btrp.ff.a3.wav   or else    psfl fl.c4 btrp.ff.a3"
endif

foreach i ($argv)
   set SF = ""
   if ( -e $i ) then
    	  set SF = $i
   else if ( -e $i.wav ) then
	set SF = $i.wav
   endif

   if ( $SF == "") then  # user need not supply the subdirectory of the soundfile
     # create list of all sflib96 subdirectories
     set DIR96 = `ls -R $SFLIB96 | grep sflib | grep : | sed -e 's/://g' # | sed -e 's^/sflib96^^g' | sed -e 's/\/snd//g'`

     foreach DIR (`echo  $DIR96 `) # search 961k sflib directoreis
       # DELETE echo  current DIR is $DIR
        #if ( -e $SFLIB96/$DIR96/$i ) then          
        if ( -e $DIR/$i ) then          
	   set SF = $DIR/$i
        else if ( -e $DIR/$i.wav ) then          
	   set SF = $DIR/$i.wav
        endif
     end
   endif
   if ( $SF == "") then  # user need not supply the subdirectory of the soundfile
     foreach DIR (`ls  $SFLIB `) # search 44.1k sflib directoreis
        if ( -e $SFLIB/$DIR/$i ) then          
	   set SF = $SFLIB/$DIR/$i
        else if ( -e $SFLIB/$DIR/$i.wav ) then          
	   set SF = $SFLIB/$DIR/$i.wav
        endif
     end
   endif
if ($SF == "") then  # abort if file not found
    echo "No soundfile called >> $i << or $i.wav in the sflib or sflib96 directories. Quitting."
    exit 1                        
endif

      set DUR = `sndinfo $SF | grep length: | sed -e 's/length:/duration:/g'`
      set FORMAT = `sndinfo $SF | grep type: | sed -e 's/type/format/g' | sed -e 's/format: RIFF/format: .WAV/g'`
   	echo " Playing soundfile >> $SF <<  $DUR seconds, format $FORMAT"
      /usr/local/bin/sndplay  $SF
      sleep 1
end

