#!/bin/bash

if [ $# = 0 ] ; then
    echo "Power normalise waveforms according to precomputed factors"
	echo "Usage:  make_wav_powernorm wav_norm wav/*.wav"
    echo "normalisation factors should exist in etc/powfacts"
    echo "puts processed waveforms in wav_fn/*.wav"
    exit 1
fi

WAVNORM=$1
shift

if [ ! "$ESTDIR" ] ; then
    echo "environment variable ESTDIR is unset"
    exit 1
fi

if [ ! -f etc/powfacts ] ; then 
    echo "etc/powfacts should be precomputed"
    exit 1
fi

for i in $* ; do
    fname=`basename $i .wav`
    echo $i
    
    powfact=`awk '{if ($1 == "'$fname'") print $2}' etc/powfacts`
    if [ ! "$powfact" ]
	then
	powfact=1.0
    fi
    $ESTDIR/bin/ch_wave -scale $powfact $i -o $WAVNORM/${fname}.wav
   # Change the overall volume too, of the normalised file
   # $ESTDIR/bin/ch_wave -scaleN 0.65 -o /tmp/tmp$$.wav /tmp/tmp$$.wav
done
