#!/bin/bash

ENABLE=1				# Set to 0 to disable speech output
TXT2PHO=/usr/share/txt2pho		# Where are the txt2pho files?
MBROLA=/usr/share/mbrola		# Where are the mbrola files?
VOICE=de3				# Choose mbrola-voice
SEX=f					# m/f Which sex has your voice?

[ -f /etc/mbrola/mbrola.conf ] && . /etc/mbrola/mbrola.conf
[ -f ~/.mbrola ] && . ~/.mbrola
[ "${ENABLE}" = "true" ] || [ "${ENABLE}" = "1" ] || exit 0

# parse arguments
while getopts s:v:h flag; do
  case ${flag} in
  s)
    SEX=${OPTARG}
    ;;
  v)
    VOICE=${OPTARG}
    ;;
  h | *)
    cat <<EOF

Usage:  say [-s:v:h] <file to say>

        -s <SEX>        choose sex (f or m)
        -v <VOICE>      choose voice (e.g. de6)
        -h              this help

EOF
    exit 0
    ;;
  esac
done

set -- extra ${@}
shift ${OPTIND}

eval "cat ${1} | \
sed s/@/\ ät\ /g | \
recode UTF-8..lat1 | \
pipefilt | \
preproc ${TXT2PHO}/data/Rules.lst ${TXT2PHO}/data/hadifix.abk | \
txt2pho -${SEX} -p ${TXT2PHO}/data/ | mbrola ${MBROLA}/${VOICE}/${VOICE} - -.au | \
aplay -q"

exit 0
