Using the Google Translate service as the speech engine, this gem generates a .mp3 voice file from any given string.


require 'tts'
# Will download "Hello World!.mp3" to your current directory
# Supported languages: ["zh", "en", "it", "fr"] 
"Hello World!".to_file "en"

# i18n
"人民代表代表人民".to_file "zh"

# Save the file to a specific location
"Light rain with highs of 5 degrees".to_file "en", "~/weather.mp3"

# Supports large text files, as the gem will batch up the requests to Google (as each request max 100 chars)
text = "People living on the east coast of England have been warned to stay away from their homes because of further high tides expected later. The tidal surge that hit the UK is said to have been the worst for 60 years, with thousands abandoning their homes."
text.to_file "en"

#Direct playback (require mpg123 installed and in PATH with a POSIX system)
"Established in 1853, the University of Melbourne is a public-spirited institution that makes distinctive contributions to society in research, learning and teaching and engagement.".play

#Direct playback in other language (2 times)
 "Oggi il tempo è buono, andiamo in gita di esso.".play("it", 2)

#RTL Arabic language
"اليوم كان الطقس جيدا، ونحن نذهب في نزهة منه.".play("ar")

Direct play dependencies

You need to install `mpg123`

sudo apt-get install mpg123 #for debain based
brew install mpg123 #mac


tts "ruby is great"   # play the string.
text2mp3 "中国上海天气不错" "zh" #create mp3 file.


0.4 fixed issue that long text unable to generated. 0.5 added all supported languages, added direct playback feature fixed some broken rspec. 0.7 added CLI support

Copyright © 2011 Yiling Cao / Charlie Revett. Check LICENSE.txt