#!/bin/bash
# tklabashfuncs : provides frequently used bash functions needed by
# other ECMC/TKLA scripts
# function   output                 purpose
# sfexist  $SOURCESF        checks if soundfile exists
# getsffmt $FMT (WAVE or AIFF) checks if soundfile is WAVE or AIFF
# getsfsr   $SR            gets sampling rate
# getsfbits $BITS          gets word size (usually 16, 24 or 32)
# getsfchans $CHANS          gets number of channels
# getsfdur  $DUR             gets duration of soundfile
# floattest   checks if soundfile contains floating point samples
##########################
# sfexist:  See if file exists, else abort. Output is $SOURCESF
sfexist () {
#cd $SFDIR    
for i in $* ; do
  if [ -e $i ] ; then
    SOURCESF=$i
  elif [ -e $i.wav ] ; then
    SOURCESF=$i.wav
  elif [ -e $i.aif ] ; then
    SOURCESF=$i.aif
  elif [ -e $i.aiff ] ; then
    SOURCESF=$i.aiff
  else
    echo " ERROR: No file named >> $i << found. Quitting."
          echo " (Your current working soundfile directory is $SFDIR)"
     exit 1
  fi
done
}

# getsffmt : checks that input soundfile is in WAVE or AIFF format, else aborts
# defines FMT as WAVE or AIFF
getsffmt() {
for i in $* ; do
TMPOUT=/tmp/tmpsfcheck$$
#  run csound to get header info
csound -U sndinfo $SOURCESF  >& $TMPOUT
#  check for WAVE or AIFF in header info
  if ( ( grep  -e "WAVE\ soundfile" $TMPOUT >& /dev/null ) && ( grep  -e "$SOURCESF: WAVE" $TMPOUT >& /dev/null  ) ) ; then
   FMT="WAVE"
  elif ( ( grep  -e "AIFF\ soundfile" $TMPOUT >& /dev/null ) && ( grep  -e "$SOURCESF: AIFF" $TMPOUT >& /dev/null ) ) ; then
   FMT="AIFF"
 # errors: 
   elif ( file $i | grep -e "$i:\ directory" >& /dev/null ) ; then
       echo " ERROR: File $SOURCESF is a directory, not a soundfile"
   elif [ -e $i ] ; then
    echo -n " ERROR: File $i is not a WAVE or AIFF soundfile: "
    file $i
fi
rm -f $TMPOUT
done
}

# getsfsr : finds sampling rate; output is $SFSR
getsfsr () {
for i in $* ; do
SR=`csound -U sndinfo "${i}" 2> /dev/null | grep -e "srate" | grep -e "seconds" | awk ' { print $2 }' | sed -e 's/,//g'`
done
}

# getsfbits : finds word size of soundfile; output is $BITS
getsfbits () {
for i in $* ; do
BITS=`csound -U sndinfo "${i}" 2> /dev/null | grep -e "srate" | grep -e "seconds" | awk ' { print $4 }' | sed -e 's/,//g'`
done
}

# getsfchans : gets number of channels in soundfile; output is $CHANS
getsfchans () {
for i in $* ; do
  x=`csound -U sndinfo "${i}" 2> /dev/null | grep -e "srate" | awk ' { print $3 }' | sed -e 's/,//g'`
CHANS=`case ${x} in
        (monaural)      echo -n "1"    ;;
        (stereo)        echo -n "2"    ;;
        (quad)          echo -n "4"    ;;
        (oct)         echo -n "8"    ;;
        (3-channel)     echo -n "3"    ;;
        (5-channel)     echo -n "5"    ;;
        (hex)           echo -n "6"    ;;
        (7-channel)     echo -n "7"    ;;
        (9-channel)     echo -n "9"    ;;
        *)              echo  -e "ERROR: input channels >> $CHANS << in "
                        echo "Channels must be between 1 and 9. Quitting."
                        exit 1 ; ;;
esac`
done
}

# getsfdur : defnes $DUR as duration of soundfile in seconds
getsfdur () {
for i in $* ; do
DUR=`csound -U sndinfo "${i}" 2> /dev/null  | grep -e "srate" | grep -e "seconds" | awk ' { print $(NF-1) }'` #  | sed -e 's/,//g'`
done
}
floattest () {
FLOATTEST=`csound -U sndinfo $SFDIR/$1  2> /dev/null | grep "32 bit floats"  | awk '{print $6}'  | sed -e 's/s,//g'`
  # if true $FLOATTEST returns "float", else null string
if [ -z $FLOATTEST ] ; then # if $FLATTEST is an empty string:
  echo "ERROR: Soundfile $i is not a floating point soundfile. Quitting."
    exit 1
  #else
  #  echo $FLOATTEST # returns " float "
fi
}
