#!/bin/bash

# decb4 - decodes 4 channel B format soundfiles to a quad soundfile
#   11/04

if  ( [ $# == 0 ] ) ; then  # print usage summary
    echo "decb4  syntax : decb4 inputfile  [outputfile]"
    echo "       where"
    echo "   inputfile is a 4 channel B format ambisonic encoded soundfile (any sample rate or bit depth) "
    echo "   outputfile is a quad (4 channel) output soundfile (same sampling rate and bit depth) "
    echo " If no output soundfile name is provided, the output soundfile will have"
    echo 'the same name as the input soundfile, but with "quad." prepended and with'
    echo 'the ".wxyz" extension replaced by ".wav" ' 
    echo "   -    -    -    - "
    echo " Example command lines: " 
    echo "(1)     decb4  whistles.wxzy   "
    echo 'Result: Input B format soundfile "whistles.wxzy" is decoded into a 4 channel output'
     echo 'soundfile named "quad.whistles.wav"'
    echo "(2) decb4 happytimes.wxyz  happytimes.quad.wav"
    echo 'Result: Input B format soundfile "happytimes.wxyz" is decoded into a quad output'
    echo 'soundfile named "happytimes.quad.wav"'
    echo " --  --  --  --  --  --  --  --  --  --  --  --  --  --  --"
    exit 0
fi

cd $SFDIR
 . /usr/local/lib/tklabashfuncs  # access bash functions in this file ##
sfexist $1
getsfchans $SOURCESF # get number of channels $CHANS
if ( [ `echo $CHANS` != "4" ] ) ; then
  echo "ERROR: Input soundfile $SOURCESF has $CHANS channels."
  echo "Input to decb4 must be a 4-channel B format soundfile. Quitting."
  exit 1
fi
getsffmt $SOURCESF # WAVE or AIFF format?
getsfsr $SOURCESF # get sr $SFSR
getsfbits  $SOURCESF # get word size $SFBITS
getsfdur $SOURCESF # get duration
# echo $FMT $SR $BITS $CHANS $DUR 

OUTFILE=$2
if [ -z $OUTFILE ] ; then
  OUTFILE=`basename $SOURCESF | sed -e 's^.wxyz^.wav^g'`
  OUTFILE=quad.$OUTFILE
fi

#######################################
# Create csound orc and sco files
    CSOUND="/usr/bin/csound"  # ECMC csound alias  includes -W
    CSFLAGS="-d "
if [ $FMT == "WAVE" ] ; then
   CSFLAGS="$CSFLAGS -W"
elif [ $FMT == "AIFF" ] ; then
   CSFLAGS=`echo $CSFLAGS -A`
fi
     if [ $BITS == '24' ] ; then
           CSFLAGS=`echo $CSFLAGS -3`
    elif [ $BITS == '32' ] ;  then # make sure 32 bits are floats, then add csound float flag
       if ( ! ( `sfinfo $SOURCESF | grep -e "32-bit" | grep -e "float"` == "" >& /dev/null ) ) ; then
               CSFLAGS=`echo $CSFLAGS -f`  # float input & output
            fi
          fi
TMPSCO=~/tmp.sco
TMPORC=~/tmp.orc
# (1) Create  a Csound score file
  echo "i1 0 $DUR" > $TMPSCO

echo " ; csound header " > $TMPORC
echo "sr=$SR " >> $TMPORC
 KR=`expr $SR \/ 5`
echo "kr=$KR " >> $TMPORC
#echo "ksmps=$KSMPS " >> $TMPORC
echo "nchnls=4 " >> $TMPORC
 
echo "instr 1 " >> $TMPORC
  echo 'aw, ax,ay,az  soundin "'$SOURCESF'" , 0, 6' >> $TMPORC
  echo 'iwxygain = .6 ' >> $TMPORC #.3536 '>> $TMPORC
  echo '   aFL = (aw * iwxygain) + ((ax + ay) * iwxygain) '>> $TMPORC
  echo '  aFR = (aw * iwxygain) + ((ax - ay) * iwxygain) '>> $TMPORC
  echo ' aBL = (aw * iwxygain) - ((ax - ay) * iwxygain) '>> $TMPORC
  echo ' aBR = (aw * iwxygain) - ((ax + ay) * iwxygain) ' >> $TMPORC

echo '  ; output speaker signals' >> $TMPORC
echo 'outq aFL, aFR , aBL, aBR' >> $TMPORC
echo 'endin'  >> $TMPORC

##### Run Csound : #################
$CSOUND $CSFLAGS -o $OUTFILE $TMPORC $TMPSCO  >& /dev/null
rm -f $TMPORC $TMPSCO 
if [ -f $OUTFILE ] ; then
    echo "    4 channel output soundfile:"
    sfpeak $OUTFILE  | head -4
else
   echo 'ERROR somewhere in this job: No output soundfile created.'
   exit 1
fi

