#!/bin/bash

# Transduce a string to mbrola description
# Convert mbrola description to wav file and play
# $1 --> graphem2phonem file
# $2 --> regex in $1
# $3 --> phonem2mbrola file
# $4 --> regex in $3
# $5 --> input (string or a file with each word on a new line) 

# location of mbrola voice library
VOICE=$MBROLA_HOME/share/nl3

if [ "$1" ] && [ "$2" ] && [ "$3" ] && [ "$4" ] && [ "$5" ]
then
if [ -e "$1" ]
then
if [ -e "$3" ]
then

# input is a file
if [ -r "$5" ]; then
    awk 'BEGIN{FS=" ";OFS="\n"} $1=$1' $5 > input.tmp
# input is a string
else
    for i in $5; do
        echo "$i" >> input.tmp
    done
fi
    
    echo ""
    echo "..Create output.pho"
    fsa -aux $1 -raa $2 < input.tmp 2> speak2.log | sed 's/|: //' > output.g2p
    fsa -aux $3 -raa $4 < output.g2p 2>> speak2.log | sed 's/|: //' > output.pho
    
    echo -n "..Create "
    mbrola $VOICE output.pho output.wav
    qtplay output.wav
    echo ""
    
    rm input.tmp
else
    echo "..phonem2mbrola transducer $3 not found"
fi
else
    echo "..graphem2phonem transducer $1 not found"
fi
else
    echo "..Usage: [graphem2phonem] [regex] [phonem2mbrola] [regex] [stri
ng]"
fi

