#!/bin/sh 
# returns the duration of one or more soundfiles
# A.S. revised 1/1/04 to use Csound sndinfo rather than Schottsteadt's sndinfo
# Note: Csound rounds the duration slightly, so the result returned by this script
# may be off by a couple of milliseconds

if  ( [ $# == 0 ] ) ; then  # print usage summary
  echo "sfdur: displays the duration in seconds of one or more soundfiles"
  echo "syntax:  sfdur  soundfile1 [soundfile2  soundfileN]"
  echo "   Metacharacters such as * can be used but must be preceded by a \ "
  echo "Example: sfdur flute\*  (this will return the durations of all soundfiles whose"
  echo 'names begin with the character string "flute") '
  exit 0 ;
fi

cd $SFDIR

for i in $* ; do
    if ( ! ( test -s "${i}" ) ) ; then echo -e  "No soundfile named >> ${i} << found. Quitting." ; exit 0 ; fi
                   # exit if input file does not exist
 SNDINFO=`csound -U sndinfo "${i}" 2> /dev/null | grep -e "srate" | grep -e "seconds"`
 DUR=`echo $SNDINFO |  awk ' { print $(NF-1) }'` #  | sed -e 's/,//g'`
echo $DUR $i
done
