• Blog
  • Archives
  • Code
  • Talks
  • About

google-tts - a Javascript API for Google Text-to-Speech engine

Note: This post is over 6 months old and may now be out of date. Some of the links not work anymore.

(1 minute read)

One of the features I want to add to my experimental "learn Chinese" web app - http://zhongwen.co.uk/ - is the ability to have the Chinese characters read out to you so that you can practise the sounds too. To this end I'd been looking for web services which would do the text-to-speech conversion for me and stumbed upon /code which demonstrates how to use Google's text-to-speech engine from Javascript. This is the engine which reads out the translations on Google translate and using it as simple as calling a URL with some parameters and then playing the MP3 which gets returned.

I decided to spend a few hours packaging up Weston's code as a re-usable library and here it is - https://github.com/hiddentao/google-tts. It's small (< 1KB minified and gzipped), supports 42 languages, and plays back the audio using the HTML5 Audio tag (just as Weston's example does).

Note: Audio playback in the browser will only work when running the script locally as Google's server only returns audio if you can prevent the browser from sending the Referrer HTTP Header to their server. If you want to add background playback to your online site perhaps SoundManager will do the trick (I haven't tested this).