#!/bin/bash
# Shell wrapper for Open JTalk.

CMDNAME=`basename $0`

usage() {
  echo "Usage: $CMDNAME [-h|--help] [-i|--infile infile] \\"
  echo "          [-v|--voice nitech|mei] \\"
  echo "          [-t|--tone angry|bashful|happy|normal|sad] \\"
  echo "          [infile]"
  exit 1
}

DIC_DIR=/opt/local/lib/open_jtalk/dic

VOICE_ROOT=/opt/local/lib/open_jtalk/voice
VOICE_NITECH="$VOICE_ROOT/nitech/nitech_jp_atr503_m001"
VOICE_MEI_BASE="$VOICE_ROOT/mei/mei_"
VOICE_TONE=normal
VOICE_MEI="$VOICE_MEI_BASE$VOICE_TONE"
VOICE_BASE="$VOICE_MEI"

INFILE=/dev/stdin

while test -n "$1"; do
    case "$1" in
        -h|--help)   usage ;;
        -i|--infile) INFILE="$2"; shift 2 ;;
        -v|--voice)  [ "$2" == "nitech" ] && VOICE_BASE="$VOICE_NITECH"; shift 2 ;;
        -t|--tone)   VOICE_BASE="$VOICE_MEI_BASE$2"; shift 2 ;;
        "")          INFILE=/dev/stdin ;;
        *)           INFILE="$1"; shift ;;
    esac
done

WAVFILE="/tmp/$CMDNAME-$$.wav"

while read LINE
do
    echo $LINE | \
        /opt/local/bin/open_jtalk \
        -m $VOICE_BASE.htsvoice \
        -x  $DIC_DIR \
        -ow $WAVFILE && afplay $WAVFILE
done < "$INFILE"

rm -f $WAVFILE
exit 0
