#! /bin/sh

# Issue #552: Incorrect ByteRate when converting 33kHz TWX files to WAV
# https://sourceforge.net/p/sox/bugs/304

rm -f bd.wav

${sox:-sox} -t txw BD.W01 bd.wav

# See what's in the WAV file
#byterate="$(od -t d4 bd.wav | head -2 | tail -1 | sed 's/.* //')"
# Version that also works on big-endian hosts
byterate () {
  od -t d1 -j 28 -N 4 bd.wav | head -1 | sed 's/^[0-9]*//' | {
    read a0 a1 a2 a3  # Least significant to most significant
    byterate=$a3 
    byterate=$(($byterate * 256 + $a2))
    byterate=$(($byterate * 256 + $a1))
    byterate=$(($byterate * 256 + $a0))
    echo $byterate
  }
}

byterate=`byterate`

case "$byterate" in
66666) status=0;;
66667) status=2;;
*)     echo "Unexpected byte rate of $byterate in bd.wav" 1>&2
       exit 2;; # Leave bd.wav
esac

rm -f bd.wav

exit $status
