#!/bin/sh
# bewsoundtool - Converts a sound file to BEWorld format
# License:  Creative Commons Attribution NonCommercial ShareAlike 3.0
# Revision: 110908

#---------------------------------------------------------------------
# Check the command line.

OK=1
if [ x"$3"  == x ]; then OK=0; fi
if [ x"$4" \!= x ]; then OK=0; fi

if [ $OK -eq 0 ]; then
    cat <<END

snd2beworld $REVISION - Converts a sound file to BEWorld format

Usage: snd2beworld FILE VOLUME DIVISOR
       FILE    = sound file (may be a .mp3, a .wav, etc.)
       VOLUME  = volume multiplier (normally from 0.0 to 3.0)
       DIVISOR = compression divisor (1 to 10)

This produces two output files:  a ".bewsnd" file that can  be  edited
into "beworld" and a ".decoded.flac" file. The second output file is a
version of the ".bewsnd" file turned back into normal sound; it  isn't
needed but can be  used to check the results.  Both  output files have
the  same pathname  as the input but they  replace the filename exten-
sion.

Note:  If the last line in the ".bewsnd" file ends with  a space and a
backslash,  you'll need to remove those two characters manually before
using the file.

END
    exit 1
fi

#---------------------------------------------------------------------

IFNAME=$1
VOLUME=$2
DIVISOR=$3

PATH=.:$PATH                                    || exit 1
BASE=`deext $IFNAME`                            || exit 1
B64FILE=$BASE.b64                               || exit 1
BEWFILE=$BASE.bewsnd                            || exit 1
BXNAME=$BASE.bxdiv                              || exit 1
OCNAME=`basename $BASE`                         || exit 1

rm -f $BXNAME* $B64FILE $BEWFILE \
      $BASE.decoded.*                           || exit 1

sox -V -v $VOLUME $IFNAME -t ub \
    -r 44100 -c 1 $BASE.ub                      || exit 1

data2bxdiv $DIVISOR $BASE.ub > $BXNAME          || exit 1
ls -l                          $BXNAME          || exit 1
rm                  $BASE.ub                    || exit 1

lzbetool $BXNAME                                || exit 1
ls -l    $BXNAME.lz77                           || exit 1

base64 --wrap=68 < $BXNAME.lz77 > $B64FILE      || exit 1
ls -l                             $B64FILE      || exit 1
rm                 $BXNAME.lz77                 || exit 1

sed -re 's|^.{68}$|& \\|' \
    < $B64FILE > tempdata~                      || exit 1
rm    $B64FILE                                  || exit 1

cat > $BEWFILE <<END                            || exit 1
#---------------------------------------------------------------------
# Compressed "$OCNAME" sound.

lappend gdata(list_sounds) $OCNAME

# This variable contains a sound effect compressed (and encoded) using
# the "bxdiv-LZ77-base64" procedure discussed in the program document-
# ation.

set    ${OCNAME}_bxdiv_lz77_base64 ""
append ${OCNAME}_bxdiv_lz77_base64 \\
END

cat tempdata~ >> $BEWFILE                       || exit 1
rm  tempdata~                                   || exit 1

bxdiv2data $BXNAME       > $BASE.decoded.ub     || exit 1
sox -V -t ub -r 44100 -c 1 $BASE.decoded.ub \
                           $BASE.decoded.flac   || exit 1
rm         $BXNAME         $BASE.decoded.ub     || exit 1

ls -l $BEWFILE $BASE.decoded.flac               || exit 1
echo Done
