#!/bin/sh

TMP_FILE="/tmp/make_sphere_$$.sw";

for arg
do
    FILENAME="$arg";
    BASENAME=`echo $FILENAME|cut -d'.' -f1`;
    SPHERE_FILE="$BASENAME.sphere";
    echo "converting $FILENAME -> $SPHERE_FILE";

    DATA=`sox -r 8000 -t .sw $FILENAME -t .sw -r 8000 $TMP_FILE stat 2>&1|cat`;
    SAMPLE_COUNT=`wc $TMP_FILE|cut -b'17-25'`;
    SAMPLE_COUNT=`echo "$SAMPLE_COUNT / 2"|bc`;
    echo "SAMPLE_COUNT=$SAMPLE_COUNT";
    SAMPLE_COUNT=`echo $SAMPLE_COUNT`;

    h_add $TMP_FILE $SPHERE_FILE;
    h_edit -Isample_count=$SAMPLE_COUNT $SPHERE_FILE;
    h_edit -Isample_rate=8000 $SPHERE_FILE;
    h_edit -Isample_n_bytes=1 $SPHERE_FILE;
    h_edit -Ichannel_count=1 $SPHERE_FILE;
    h_edit -Isample_max=255 $SPHERE_FILE;
    h_edit -Isample_min=0 $SPHERE_FILE;
    h_edit -Ssample_byte_format="10" $SPHERE_FILE;
    h_edit -Ssample_coding="pcm" $SPHERE_FILE;

    rm $TMP_FILE;
    
done

cat <<EOF
Warning: sample_max has not been properly set.

This program's implementation does not include setting sample_max
EOF

