#!/bin/bash
# sbc_encoder [-jsv] [-lblk_len] [-nsubbands] [-p] [-rrate] [-ooutputfile] inputfile
#            [-s] use the stereo mode for stereo signals
#            [-v] verbose mode
#            [-j] allow the use of joint coding for stereo signals
#            [-lblk_len] blk_len specifies the APCM block length, out of [4,8,12,16]
#            [-nsubbands] subbands specifies the number of subbands, out of [4,8]
#            [-p] a simple psycho acoustic model is used
#            [-rrate] specifies the bit rate in bps
#            [-ooutputfile] specifies the name of the bitstream output file
#            inputfile specifies the audio input file, the major audio formats are supported

REF_ENCODER='wine ./ownplc/sbc.conformance/sbc_encoder.exe -p'

#SBC Decoder LIB Version 1.5
#Copyright (c) 2002  Philips Consumer Electronics, ASA Labs
#
#get command line option: Invalid argument
#Usage:
#sbc_decoder [-v] [-ooutputfile] [-pstartpos] inputfile
#            [-v] verbose mode
#            [-pstartpos] startpos specifies the byte offset to start with decoding
#            [-ooutputfile] specifies the name of the audio output file
#            inputfile specifies the name of the bitstream input file

REF_DECODER='wine ./ownplc/sbc.conformance/sbc_decoder.exe'

#SBC encoder utility ver 4.17
#Copyright (c) 2004-2008  Marcel Holtmann
#Usage:
#	sbcenc [options] file(s)
#Options:
#	-h, --help           Display help
#	-v, --verbose        Verbose mode
#	-s, --subbands       Number of subbands to use (4 or 8)
#	-b, --bitpool        Bitpool value (default is 32)
#	-j, --joint          Joint stereo

BLUEZ_ENCODER=./bluez-4.17/sbc/sbcenc

#SBC decoder utility ver 4.17
#Copyright (c) 2004-2008  Marcel Holtmann
#Usage:
#	sbcdec [options] file(s)
#Options:
#	-h, --help           Display help
#	-v, --verbose        Verbose mode
#	-d, --device <dsp>   Sound device
#	-f, --file <file>    Decode to a file

BLUEZ_DECODER=./bluez-4.17/sbc/sbcdec

# parameters

SUBBANDS=4
BITPOOL=28
MODE="MONO"
CHANNELS=1
JOINT=
RATE=48000
BLOCKS=16

# mode, subbands, bitpool, rate

bitrate ()
{
    if [ "$1" = "MONO" ] && [ "$2" = "4" ] 
    then 
     
	bitrate=$((($3*2+6)*$4/8))

    elif [ "$1" = "MONO" ] && [ "$2"="8" ]
    then
	bitrate=$((($3*2+8)*$4/16))

    elif [ "$1" = "STEREO" ] && [ "$2"="4" ]
    then
	bitrate=$(((10+$3*4)*$4/8))

    elif [ "$1" = "STEREO" ] && [ "$2"="8" ]
    then
	bitrate=$((($3*4+12)*$4/16))

    elif [ "$1" = "JOINTSTEREO" ] && [ "$2"="4" ]
    then
	bitrate=$(((10+$3*4)*$4/8))

    elif [ "$1" = "JOINTSTEREO" ] && [ "$2"="8" ]
    then
	bitrate=$((($3*4+12)*$4/16))
    else
	echo ERROR wrong mode
	exit 3
    fi
    echo $bitrate
}

# format change
sox $1 -c $CHANNELS -r $RATE ref.wav
sox $1 -c $CHANNELS -r $RATE ref.au

# ENCODING

br=`bitrate $MODE $SUBBANDS $BITPOOL $RATE`
echo -n SBC REF ENCODER
$REF_ENCODER -n$SUBBANDS $JOINT -l$BLOCKS -r$br -v -otmp.ref.sbc ref.wav
echo SBC BLUEZ ENCODER
$BLUEZ_ENCODER -b $BITPOOL $JOINT -s $SUBBANDS -v ref.au >tmp.bluez.sbc

# DECODING
$REF_DECODER -v -otmp.refref.wav tmp.ref.sbc
sox tmp.refref.wav tmp.refref.sw
$REF_DECODER -v -otmp.bluezref.wav tmp.bluez.sbc
sox tmp.bluezref.wav tmp.bluezref.sw
$BLUEZ_DECODER -v -f tmp.bluezbluez.au tmp.bluez.sbc
sox tmp.bluezbluez.au tmp.bluezbluez.sw
sox tmp.bluezbluez.au tmp.bluezbluez.wav
$BLUEZ_DECODER -v -f tmp.refbluez.au tmp.ref.sbc
sox tmp.refbluez.au tmp.refbluez.sw
sox tmp.refbluez.au tmp.refbluez.wav

diff tmp.ref.sbc tmp.bluez.sbc
diff tmp.refref.sw tmp.bluezbluez.sw
diff tmp.refref.sw tmp.refbluez.sw
diff tmp.refref.sw tmp.bluezref.sw

echo "Running PEAQ"
rm -f peaq.ref.ref.txt  peaq.bluez.bluez.txt peaq.bluez.ref.txt peaq.ref.bluez.txt
RunPeaqBasicMono.sh ref.wav tmp.refref.wav peaq.ref.ref.txt -Delay TrackingOn -Signal StaticGainOn  AutoInvertTestSig DCFilterOn -PassThrough GainComp
RunPeaqBasicMono.sh ref.wav tmp.bluezbluez.wav peaq.bluez.bluez.txt -Delay TrackingOn -Signal StaticGainOn AutoInvertTestSig DCFilterOn -PassThrough GainComp
RunPeaqBasicMono.sh ref.wav tmp.bluezref.wav peaq.bluez.ref.txt -Delay TrackingOn -Signal StaticGainOn AutoInvertTestSig DCFilterOn -PassThrough GainComp
RunPeaqBasicMono.sh ref.wav tmp.refbluez.wav peaq.ref.bluez.txt -Delay TrackingOn -Signal StaticGainOn AutoInvertTestSig DCFilterOn -PassThrough GainComp
RunPeaqBasicMono.sh ref.wav tmp.refref.wav peaq.ref.ref.txt -Delay TrackingOn -Signal AutoInvertTestSig DCFilterOn -PassThrough FixedGain
RunPeaqBasicMono.sh ref.wav tmp.bluezbluez.wav peaq.bluez.bluez.txt -Delay TrackingOn -Signal AutoInvertTestSig DCFilterOn -PassThrough FixedGain
RunPeaqBasicMono.sh ref.wav tmp.bluezref.wav peaq.bluez.ref.txt -Delay TrackingOn -Signal AutoInvertTestSig DCFilterOn -PassThrough FixedGain
RunPeaqBasicMono.sh ref.wav tmp.refbluez.wav peaq.ref.bluez.txt -Delay TrackingOn -Signal AutoInvertTestSig DCFilterOn -PassThrough FixedGain
cat peaq.ref.ref.txt  peaq.bluez.bluez.txt peaq.bluez.ref.txt peaq.ref.bluez.txt | sort -u >peaq.txt
rm peaq.ref.ref.txt  peaq.bluez.bluez.txt peaq.bluez.ref.txt peaq.ref.bluez.txt