#!/bin/bash
(set -o igncr) 2>/dev/null && set -o igncr; # force bash to ignore \r character



if  [ "$1" = "x64" ] || [ "$2" = "x64" ] || [ "$#" -eq 0 ]
    then
    PLATFORM=_X64
    ISAC=../x64/Release/ReleaseTest-API_2005.exe
elif [ "$1" = "LINUX" ] || [ "$2" = "LINUX" ]
    then
    PLATFORM=_linux
    ISAC=../ReleaseTest-API/isacswtest
else
    PLATFORM=_2005
    ISAC=../win32/Release/ReleaseTest-API_2005.exe
fi

if  [ "$#" -eq 0 ] || [ "$1" = "all" ] || [ "$1" = "wb" ]
    then
    LOGFILE=logNormal"$PLATFORM".txt
    echo "START ISAC WB TEST" > $LOGFILE
    echo >> $LOGFILE

    INFILES=$(cat InputFiles.txt)
    SUBSET=$(cat InputFilesFew.txt)
    CHANNELFILES=$(cat ChannelFiles.txt)
    CHANNELLIST=($(cat ChannelFiles.txt))
    INDIR=../data/orig
    OUTDIR=../dataqa"$PLATFORM"
    mkdir -p $OUTDIR
    rm -f $OUTDIR/*
    
    idx=0
    RATE=10000
    FRAMESIZE=30
    

    for file in $INFILES # loop over all input files
      do
      
      echo "Input file: " $file
      echo "-----------------------------------"
      echo "Instantaneous with RATE " $RATE ", and Frame-size " $FRAMESIZE
      $ISAC -I -B $RATE -FL $FRAMESIZE -FS 16 $INDIR/"$file" $OUTDIR/i_"$FRAMESIZE"_"$RATE"_"$file" >> $LOGFILE
      echo
      
      name="${CHANNELLIST[$idx]}"
      echo "Adaptive with channel file: " $name 
      
      $ISAC -B $INDIR/${CHANNELLIST[$idx]} -FS 16 $INDIR/"$file" $OUTDIR/a_${name%.*}_"$file" >> $LOGFILE
      
      echo
      echo
      
#     alternate between 30 & 60 ms.
      if [ $FRAMESIZE -eq 30 ]
	  then
	  FRAMESIZE=60
      else
	  FRAMESIZE=30
      fi
      
#     rate between 10000 to 32000 bits/sec
      if [ $RATE -le 30000 ]
	  then
	  let "RATE=RATE+2000"
      else
	  let "RATE=10000"
      fi
      
#     there are only three channel file
      if [ $idx -ge 2 ]; then
	  idx=0
      else
	  let "idx=idx+1"
      fi
      
    done

    idx=0
    
#   loop over the subset of input files
    for file in $SUBSET 
      do
      
      if [ $idx -eq 0 ]; then
	  $ISAC -B $INDIR/${CHANNELLIST[0]} -FL 30 -FIXED_FL -FS 16 $INDIR/"$file" $OUTDIR/a30_"$file" >> $LOGFILE
	  idx=1
      else
	  $ISAC -B $INDIR/${CHANNELLIST[0]} -FL 60 -FIXED_FL -FS 16 $INDIR/"$file" $OUTDIR/a60_"$file" >> $LOGFILE
	  idx=0
      fi
    done

    $ISAC -B $INDIR/${CHANNELLIST[0]} -INITRATE 25000 -FL 30 -FS 16 $INDIR/"$file" $OUTDIR/a60_Init25kbps_"$file" >> $LOGFILE

    echo
    echo WIDEBAND DONE!
    echo
    echo
fi

if  [ "$#" -eq 0 ] || [ "$1" = "all" ] || [ "$1" = "swb" ]
    then

    LOGFILE=logNormal_SWB"$PLATFORM".txt
    echo "START ISAC SWB TEST" > $LOGFILE
    echo >> $LOGFILE

    echo STARTING TO TEST SUPER-WIDEBAND
    
    INFILES=$(cat InputFilesSWB.txt)
    INDIR=../data/origswb
    OUTDIR=../dataqaswb"$PLATFORM"
    mkdir -p $OUTDIR
    rm -f $OUTDIR/*
    
    for file in $INFILES
      do
      echo
      echo "Input file: " $file
      echo "--------------------------------"
      for RATE in 12000 20000 32000 38000 45000 50000 56000  
	do
	
	echo "Rate " $RATE
	$ISAC -I -B $RATE -FL 30 -FS 32 $INDIR/"$file" $OUTDIR/swb_"$RATE"_"$file" >> $LOGFILE
	echo
	
      done
  
    done
fi

if  [ "$#" -eq 0 ] || [ "$1" = "all" ] || [ "$1" = "API" ]
    then

    LOGFILE_API=logNormal_API"$PLATFORM".txt
    echo
    echo
    echo "START ISAC API TEST" > $LOGFILE_API
    echo >> $LOGFILE_API
    idx=1
    echo "                            Test Enforcement of frame-size"
    echo "========================================================================================"
    mkdir -p ../FrameSizeLim"$PLATFORM"
    rm -f ../FrameSizeLim"$PLATFORM"/*
    echo
    echo "-- No enforcement; BN 10000"
    echo
    $ISAC -B 10000 -FS 16 ../data/orig/speech_and_misc_WB.pcm \
	../FrameSizeLim"$PLATFORM"/APITest_"$idx".pcm >> $LOGFILE_API
    let "idx=idx+1"
    echo
    echo
    echo "-- Now Enforce 30 ms frame size with the same bottleneck"
    echo "There should not be any 60 ms frame"
    echo
    $ISAC -B 10000 -FL 30 -FIXED_FL -FS 16 ../data/orig/speech_and_misc_WB.pcm \
	../FrameSizeLim"$PLATFORM"/APITest_"$idx".pcm >> $LOGFILE_API
    let "idx=idx+1"
    echo
    echo
    echo "-- No enforcement; BN 32000"
    echo
    $ISAC -B 32000 -FS 16 ../data/orig/speech_and_misc_WB.pcm \
	../FrameSizeLim"$PLATFORM"/APITest_"$idx".pcm >> $LOGFILE_API
    let "idx=idx+1"
    echo
    echo
    echo "-- Now Enforce 60 ms frame size with the same bottleneck"
    echo "There should not be any 30 ms frame"
    echo 
    $ISAC -B 32000 -FL 60 -FIXED_FL -FS 16 ../data/orig/speech_and_misc_WB.pcm \
	../FrameSizeLim"$PLATFORM"/APITest_"$idx".pcm >> $LOGFILE_API
    let "idx=idx+1"
    echo
    echo
    echo
    echo
    echo

    echo "                           Test Limiting of Payload Size and Rate"
    echo "========================================================================================"
    mkdir -p ../PayloadLim"$PLATFORM"
    rm -f ../PayloadLim"$PLATFORM"/*
    echo
    echo
    echo "-- No Limit, frame-size 60 ms, WIDEBAND"
    echo 
    $ISAC -I -B 32000 -FL 60 -FS 16                ../data/orig/speech_and_misc_WB.pcm \
	../PayloadLim"$PLATFORM"/APITest_"$idx".pcm >> $LOGFILE_API
    let "idx=idx+1"
    echo
    echo
    echo "-- Payload-size limit of 250, frame-size 60 ms, WIDEBAND"
    echo 
    $ISAC -I -B 32000 -FL 60 -FS 16 -MAX 250       ../data/orig/speech_and_misc_WB.pcm \
	../PayloadLim"$PLATFORM"/APITest_"$idx".pcm >> $LOGFILE_API
    let "idx=idx+1"
    echo
    echo
    echo "-- Rate limit of 33 kbps for 60 ms frame-size"
    echo
    $ISAC -I -B 32000 -FL 60 -FS 16 -MAXRATE 33000 ../data/orig/speech_and_misc_WB.pcm \
	../PayloadLim"$PLATFORM"/APITest_"$idx".pcm >> $LOGFILE_API
    let "idx=idx+1"
    echo 
    echo "________________________________________________________"
    echo
    echo

    echo "-- No Limit, frame-size 30 ms, WIDEBAND"
    echo 
    $ISAC -I -B 32000 -FL 30 -FS 16                ../data/orig/speech_and_misc_WB.pcm \
	../PayloadLim"$PLATFORM"/APITest_"$idx".pcm >> $LOGFILE_API
    let "idx=idx+1"
    echo
    echo    
    echo "-- Payload-size limit of 130, frame-size 30 ms, WIDEBAND"
    echo 
    $ISAC -I -B 32000 -FL 30 -FS 16 -MAX 130       ../data/orig/speech_and_misc_WB.pcm \
	../PayloadLim"$PLATFORM"/APITest_"$idx".pcm >> $LOGFILE_API
    let "idx=idx+1"
    echo
    echo
    echo "-- Rate limit of 33 kbps for 30 ms frame-size, wideband"
    echo
    $ISAC -I -B 32000 -FL 30 -FS 16 -MAXRATE 33000 ../data/orig/speech_and_misc_WB.pcm \
	../PayloadLim"$PLATFORM"/APITest_"$idx".pcm >> $LOGFILE_API
    let "idx=idx+1"
    echo
    echo "________________________________________________________"
    echo
    echo

    echo "-- No limit for 32 kbps, 30 ms, SUPER-WIDEBAND"
    echo
    $ISAC -I -B 32000 -FL 30 -FS 32          ../data/origswb/jstest_32.pcm \
	../PayloadLim"$PLATFORM"/APITest_"$idx".pcm >> $LOGFILE_API
    let "idx=idx+1"
    echo
    echo
    echo "-- Payload limit of 130 bytes for 32 kbps, 30 ms, SUPER-WIDEBAND"
    echo
    $ISAC -I -B 32000 -FL 30 -FS 32 -MAX 130 ../data/origswb/jstest_32.pcm \
	../PayloadLim"$PLATFORM"/APITest_"$idx".pcm >> $LOGFILE_API
    let "idx=idx+1"
    echo
    echo "________________________________________________________"
    echo
    echo

    echo "-- No limit, Rate 45 kbps, 30 ms, SUPER-WIDEBAND, 12 kHz"
    echo
    $ISAC -I -B 45000 -FL 30 -FS 32               ../data/origswb/jstest_32.pcm \
	../PayloadLim"$PLATFORM"/APITest_"$idx".pcm >> $LOGFILE_API
    let "idx=idx+1"
    echo
    echo
    echo "-- Rate limit of 46 kbps for 42 kbps, 30 ms, SUPER-WIDEBAND, 12 kHz"
    echo
    $ISAC -I -B 45000 -FL 30 -FS 32 -MAXRATE 46000 ../data/origswb/jstest_32.pcm \
	../PayloadLim"$PLATFORM"/APITest_"$idx".pcm >> $LOGFILE_API
    let "idx=idx+1"
    echo
    echo
    echo "-- Payload limit of 170 bytes for 45 kbps, 30 ms, SUPER-WIDEBAND, 12 kHz"
    echo
    $ISAC -I -B 45000 -FL 30 -FS 32 -MAX 170       ../data/origswb/jstest_32.pcm \
	../PayloadLim"$PLATFORM"/APITest_"$idx".pcm >> $LOGFILE_API
    let "idx=idx+1"
    echo
    echo "________________________________________________________"
    echo
    echo
  
    echo "-- No limit for 56 kbps, 30 ms, SUPER-WIDEBAND, 16 kHz"
    echo
    $ISAC -I -B 56000 -FL 30 -FS 32                ../data/origswb/jstest_32.pcm \
	../PayloadLim"$PLATFORM"/APITest_"$idx".pcm >> $LOGFILE_API
    let "idx=idx+1"
    echo
    echo
    echo "-- Payload limit of 200 bytes for 56 kbps 30 ms, SUPER-WIDEBAND, 16 kHz"
    echo
    $ISAC -I -B 56000 -FL 30 -FS 32 -MAX 200       ../data/origswb/jstest_32.pcm \
	../PayloadLim"$PLATFORM"/APITest_"$idx".pcm >> $LOGFILE_API
    let "idx=idx+1"
    echo
    echo
    echo "-- Rate limit of 57 kbps for 56 kbps 30 ms, SUPER-WIDEBAND, 16 kHz"
    echo
    $ISAC -I -B 56000 -FL 30 -FS 32 -MAXRATE 57000 ../data/origswb/jstest_32.pcm \
	../PayloadLim"$PLATFORM"/APITest_"$idx".pcm >> $LOGFILE_API
    let "idx=idx+1"
    echo
    echo "________________________________________________________"
    echo
    echo
    echo
    echo
    echo
    
    echo "                                    Test Trans-Coding"
    echo "========================================================================================"
    mkdir -p ../Transcoding"$PLATFORM"
    rm -f ../Transcoding"$PLATFORM"/*
    echo
    echo
    echo "-- 20 kbps, 30 ms, WIDEBAND"
    echo
    $ISAC -I -B 20000 -FL 30 -FS 16          ../data/orig/speech_and_misc_WB.pcm \
	../Transcoding"$PLATFORM"/APITest_refTrans20WB.pcm >> $LOGFILE_API
    let "idx=idx+1"
    echo
    echo
    
    echo "-- 32 kbps trans-coding to 20 kbps, 30 ms, WIDEBAND"
    echo
    $ISAC -I -B 32000 -FL 30 -FS 16  -T 20000  ../Transcoding"$PLATFORM"/APITest_32T20.pcm \
	../data/orig/speech_and_misc_WB.pcm ../Transcoding"$PLATFORM"/APITest_"$idx".pcm >> $LOGFILE_API
    let "idx=idx+1"
    echo
    echo "________________________________________________________"

    echo
    echo
    echo "-- 38 kbps, 30 ms, SUPER-WIDEBAND"
    echo
    $ISAC -I -B 38000 -FL 30 -FS 32          ../data/origswb/jstest_32.pcm \
	../Transcoding"$PLATFORM"/APITest_refTrans38.pcm >> $LOGFILE_API
    let "idx=idx+1"
    echo
    echo
    
    echo "-- 45 kbps trans-coding to 38 kbps, 30 ms, SUPER-WIDEBAND"
    echo
    $ISAC -I -B 45000 -FL 30 -FS 32  -T 38000  ../Transcoding"$PLATFORM"/APITest_45T38.pcm \
	../data/origswb/jstest_32.pcm ../Transcoding"$PLATFORM"/APITest_"$idx".pcm >> $LOGFILE_API
    let "idx=idx+1"
 
    echo
    echo
    echo "-- 20 kbps, 30 ms, SUPER-WIDEBAND"
    echo
    $ISAC -I -B 20000 -FL 30 -FS 32          ../data/origswb/jstest_32.pcm \
	../Transcoding"$PLATFORM"/APITest_refTrans20SWB.pcm >> $LOGFILE_API
    let "idx=idx+1"

    echo
    echo
    
    echo "-- 45 kbps trans-coding to 20 kbps, 30 ms, SUPER-WIDEBAND"
    echo
    $ISAC -I -B 45000 -FL 30 -FS 32  -T 20000  ../Transcoding"$PLATFORM"/APITest_45T20.pcm \
	../data/origswb/jstest_32.pcm ../Transcoding"$PLATFORM"/APITest_"$idx".pcm >> $LOGFILE_API
    let "idx=idx+1"
    echo
    echo "________________________________________________________"
    echo
    echo
    echo "-- 50 kbps, 30 ms, SUPER-WIDEBAND"
    echo
    $ISAC -I -B 50000 -FL 30 -FS 32          ../data/origswb/jstest_32.pcm \
	../Transcoding"$PLATFORM"/APITest_refTrans50.pcm >> $LOGFILE_API
    let "idx=idx+1"
    echo
    echo
    
    echo "-- 56 kbps trans-coding to 50 kbps, 30 ms, SUPER-WIDEBAND"
    echo
    $ISAC -I -B 56000 -FL 30 -FS 32  -T 50000  ../Transcoding"$PLATFORM"/APITest_56T50.pcm \
	../data/origswb/jstest_32.pcm ../Transcoding"$PLATFORM"/APITest_"$idx".pcm >> $LOGFILE_API
    let "idx=idx+1"
    echo
    echo
    
    echo "-- 56 kbps trans-coding to 20 kbps, 30 ms, SUPER-WIDEBAND"
    echo
    $ISAC -I -B 56000 -FL 30 -FS 32  -T 20000 ../Transcoding"$PLATFORM"/APITest_56T20.pcm \
	../data/origswb/jstest_32.pcm ../Transcoding"$PLATFORM"/APITest_"$idx".pcm >> $LOGFILE_API
    let "idx=idx+1"
    echo
    echo "________________________________________________________"
    echo
    echo
    echo 
    echo
    echo
    
    echo "                                         Test FEC"
    echo "========================================================================================"
    mkdir -p ../FEC"$PLATFORM"
    rm -f ../FEC"$PLATFORM"/*
    echo
    echo
    echo "-- 32 kbps with transcoding to 20kbps, 30 ms, WIDEBAND, 10% packet loss"
    $ISAC -I -B 32000 -FL 30 -FS 16 -PL 10 -T 20000 ../FEC"$PLATFORM"/APITest_PL10_WB30_T20.pcm \
	../data/orig/speech_and_misc_WB.pcm ../FEC"$PLATFORM"/APITest_PL10_WB30.pcm >> $LOGFILE_API
    let "idx=idx+1"
    echo
    echo
    
    echo "-- 32 kbps, 60 ms, WIDEBAND, 10% packet loss"
    $ISAC -I -B 32000 -FL 60 -FS 16 -PL 10 ../data/orig/speech_and_misc_WB.pcm \
	../FEC"$PLATFORM"/APITest_PL10_WB60.pcm >> $LOGFILE_API
    let "idx=idx+1"
    echo
    echo
    
    echo "-- 32 kbps with transcoding to 20 kbps, 30 ms, SUPER-WIDEBAND, 10% packet loss"
    $ISAC -I -B 32000 -FL 30 -FS 32 -PL 10 -T 20000 ../FEC"$PLATFORM"/APITest_PL10_SWB_8kHz_T20.pcm \
	../data/origswb/jstest_32.pcm ../FEC"$PLATFORM"/APITest_PL10_SWB_8kHz.pcm >> $LOGFILE_API
    let "idx=idx+1"
    echo
    echo

    echo "-- 45 kbps with Trascoding to 38 kbps, 30 ms, SUPER-WIDEBAND, 10% packet loss"
    $ISAC -I -B 45000 -FL 30 -FS 32 -PL 10 -T 38000 ../FEC"$PLATFORM"/APITest_PL10_SWB_12kHz_T38.pcm \
	../data/origswb/jstest_32.pcm ../FEC"$PLATFORM"/APITest_PL10_SWB_12kHz.pcm >> $LOGFILE_API
    let "idx=idx+1"
    echo
    echo

    echo "-- 56 kbps with transcoding to 50 kbps, 30 ms, SUPER-WIDEBAND, 10% packet loss"
    $ISAC -I -B 56000 -FL 30 -FS 32 -PL 10 -T 50000 ../FEC"$PLATFORM"/APITest_PL10_SWB_16kHz_T50.pcm \
	../data/origswb/jstest_32.pcm ../FEC"$PLATFORM"/APITest_PL10_SWB_16kHz.pcm >> $LOGFILE_API
    let "idx=idx+1"
    echo
    echo
fi
