#!/bin/bash

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


# location of mbrola voice library
VOICE=/usr/share/mbrola/nl2/nl2

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

# input is a file
if [ -r "$3" ]; then
    awk 'BEGIN{FS=" ";OFS="\n"} $1=$1' $3 > input.tmp
# input is a string
else
    > input.tmp
    for i in $3; do
        echo "$i" >> input.tmp
    done
fi
    
    echo ""
    echo "..Create output.pho"
    fsa -aux $1 -raa $2 < input.tmp 2> speak.log | sed 's/|://' | tee output.pho
    
    echo -n "..Create "
    mbrola $VOICE output.pho output.wav
    aplay output.wav
    echo ""
    
    rm input.tmp

else
    echo "..grapheme2mbrola transducer $1 not found"
fi
else
    echo "..Usage: speak [grapheme2mbrola] [regex] [string]"
fi
