#!/bin/bash

# decb2 - decodes 4 channel B format soundfiles to stereo with UHJ decoding
#   11/04

if  ( [ $# == 0 ] ) ; then  # print usage summary
    echo "decb2  syntax : decb2 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 2 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 "ST" prepended and with'
    echo 'the ".wxyz" extension replaced by ".wav" ' 
    echo "   -    -    -    - "
    echo " Example command lines: " 
    echo "(1)     decb2  whistles.wxzy   "
    echo 'Result: Input B format soundfile "whistles.wxzy" is decoded with UHJ decoding'
     echo 'into an output stereo soundfile named "STwhistles.wav"'
    echo "(2) decb2 happytimes.wxyz  happytimes.stereo.wav"
    echo 'Result: Input B format soundfile "happytimes.wxyz" is decoded into output stereo'
    echo 'soundfile "happytimes.stereo.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 decb2 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=ST$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=2 " >> $TMPORC
 
echo "instr 1 " >> $TMPORC
  echo 'aw, ax,ay,az  soundin "'$SOURCESF'" , 0, 6' >> $TMPORC
echo '  aWre, aWim   hilbert  aw' >> $TMPORC
echo '  aXre, aXim   hilbert  ax' >> $TMPORC
echo '  aYre, aYim   hilbert  ay' >> $TMPORC
echo '  aZre, aZim   hilbert  az' >> $TMPORC

echo '  aWXre = .0928 * aXre + .4699*aWre' >> $TMPORC
echo '  aWXim = .255 * aXim - .171 * aWim' >> $TMPORC

echo '  aleft = aWXre + aWXim + .3277 * aYre' >> $TMPORC
echo '  aright = aWXre - aWXim - .3277 * aYre' >> $TMPORC
echo 'outs aleft,aright' >> $TMPORC
echo 'endin'  >> $TMPORC

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

