#!/bin/bash
  # script to display header information for wave, aiff & snd soundfiles
 # A.S. revised for bash & 9624 audio June 2003; revised Jan 2004 to use Csound
 # sndinfo rather than partially broken Schottstedt sndinfo

cd $SFDIR

if ( [ $# == 0 ] ) ; then
echo " syntax:  sfinfo  soundfile [soundfile2 soundfile3 ... soundfileN}"
echo '  The command "sfinfo" can be abbreviated "si"'
echo " "
echo " For soundfiles whose names end with the extensions .wav, .aif or .aiff"
echo ' the extension can be omitted from the "sfinfo" command line.'
echo ' Thus, to display header information for  a soundfile called "infile.wav"'
echo "  one could type either"
echo " "
echo "      sfinfo  infile.wav    or else    si infile"
echo " "
echo 'Metacharacters such as * can be used but must be preceded by a \'
echo "For full usage details type:  man sfinfo"
exit 0
fi

TMPFILE=/tmp/tmpsfinfo$
if [ $1 == -v ]; then
  VERBOSE="YES"
  SHORT="NO"
  shift;
else
  SHORT="YES"
  VERBOSE="NO"
fi

for i in $* ; do

  if [ -f $i ] ; then
     SF=$i
  elif [ -f $i.wav ] ; then
     SF=$i.wav
  elif [ -f $i.aif ] ; then
     SF=$i.aif
  elif [ -f $i.aiff ] ; then
     SF=$i.aiff
  else
    echo "ERROR: Cannot find  a soundfile named  -> $i <- "
    echo "(Your current working soundfiles directory is $SFDIR) Quitting."
    exit 0 
  fi

if [ $SHORT == "NO" ] ; then
csound -U sndinfo "${SF}" >& /dev/null | grep -v "Using\ default\ language" | grep -v "util\ SNDINFO" | grep -v "ead\ PEAK" > $TMPFILE
  cat $TMPFILE | grep -v "creation time:" | grep -v "at sample" | grep -v "sample frames)"
   if [ $VERBOSE == "YES" ] ; then
    cat $TMPFILE | grep "at sample"
    cat $TMPFILE | grep "creation time:"
   fi
  rm -f $TMPFILE
 echo " -   -   -   -   -   -   -"
else   # do a succinct listing

 SR=`sfsr $SF | awk ' {print $1 }'`
 CHANS=`sfchans $SF | awk ' {print $1 }'`
 DUR=`sfdur $SF | awk ' {print $1 }'`
 FORMAT=`sfcheck $SF | awk ' {print $1 }'`
 BITS=`sfbits $SF | awk ' {print $1 }'`
if ( ! ( csound -U sndinfo "${SF}" 2> /dev/null | grep -q -e "floats" ) ) ; then
    WORDSIZE=`echo $BITS bit ints`
  else
    WORDSIZE=`echo $BITS bit floats`
 fi
  echo -e "$SF : srate $SR; chans $CHANS; $WORDSIZE; dur $DUR ;$FORMAT" 
fi
done
