My shell scripts #3 - Google Text-to-Speech API

This simple script outputs an mp3 audio of a given text string ready to be played by some commandline app (sox, madplay, ...). The only thing I can't figure out is how to get it to accept some national characters (right now it just says some gibberish). If you manage to figure out this one problem, I'd be more than happy to hear from you in the comments ;).

Usage:
$ say en lol #says "lol" in english
$ somecommand | say fr - #read text to say from a pipe
And here goes the script:
#!/bin/ash
lang=$1
shift
if [ "$1" = "-" ]
then
  read text
  echo $text
else
  text=$*
fi
len=`expr length "$text"`
if [ -z "$text" ] ; then
        echo "Please specify string to translate (up to 100 characters incl.)."
        exit 4
elif [ "$len" -gt "100" ] ; then
        echo "Can't translate more than 100 characters at once! (entered $len)"
        exit 2
fi
wget -qU Mozilla -O - "http://translate.google.com/translate_tts?ie=UTF-8&tl=$lang&q=$text" | madplay -Q -o wave:- - | aplay -q -
exit 0

3 comments:

  1. Hello Saironiq,

    I had the same problem with accents and finally solved it. Just add the parameter ie=UTF-8 to the URL after the ? sign. It worked with spanish. I guess it will work with croatian too.

    Best regards,
    Marduk

    ReplyDelete
  2. We can determine if the string has given doing this
    STRING="${@:?Usage: Give me words to speech}"

    ReplyDelete