#!/bin/sh 
# returns the number of channels of one or more soundfiles; 
# revised Jan 2004 ; changed from Schottsteadt's sndinfo, which became very slow,
# to Csound sndinfo
# metacharacters can be used on command line but must be preceded by a \

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

cd $SFDIR
for i in $* ; do
    if ( ! ( test -s "${i}" ) ) ; then echo -e  "ERROR: 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"`
#   CHANS=`echo $SNDINFO | tr -d "[:digit:][\t\054]" | awk ' { print $2 } ' | tr -d "[\n\040]"` ;
 CHANS=`echo $SNDINFO |  awk ' { print $3 }' | sed -e 's/,//g'`

   case ${CHANS} 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 "    ;;
        #*)              echo -n -e "ERROR: input channels >> $CHANS << in " ;;
        *)              echo  -e "ERROR: input channels >> $CHANS << in " 
                        echo "Channels must be between 1 and 8. Quitting."
                        exit 1 ; ;;
   esac
echo -e "\t $i"

# Old script using Schottstead's sndinfo, partially broken as of 1/1/04
     #SFINFO=`sfinfo $i`
     #   CHANS=`echo $SFINFO | awk '{print $5}'`
     #echo $CHANS  $i
done

