Basically it works by sending a request to the Google translate servers which then, pretty much instantly, reply with an MP3 file which contains the requested words in their most sexy of robot voices.
Check this URL for an example of said voice – http://translate.google.com/translate_tts?q=check+out+my+sexy+robot+voice (if it doesn’t load in your browser, open VLC, CTRL+N and paste the URL in)
First, we want to be able to get python to stream an MP3 file off the web:
More info on this can be found here, the following code was is a stripped down version of the code found at codeboje.de.
Now we want to make a string to send :
We’re going to use command line arguments as the string which wants to be read by the Google TTS engine, the following code will grab those arguments and concatenate them into a string. It also has to be in a format like a usual Google URL, these usually replace white space for the ‘+’ sign, that’s easy to do.
Now let’s just pass this URL to the bit we wrote to stream an MP3 and watch it go:
Full Source Code:
(Uses command line arguments as it’s input, run like ‘python ttsString.py Hello World‘)
That’s pretty much how easy it is to use Google’s free online TTS engine, one main thing to watch out for is the 100 character limit to the use of this service in this way along with the API call limits which apply to each IP address. There are loads of cool things this can be used for and I’d love to see what anyone comes up with, keep me posted and have fun!