Created
June 10, 2012 15:59
-
-
Save pachacamac/2906367 to your computer and use it in GitHub Desktop.
Google TTS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module GoogleTTS | |
require 'digest/md5' | |
require 'restclient' | |
TMP_PATH = '/tmp' | |
class << self | |
def say(text, lang='en') | |
file = download_audio(text, lang) | |
@gttspid = fork{ exec 'mpg123', '-q', file } | |
end | |
private | |
def download_audio(text, lang='en') | |
digest = Digest::MD5.hexdigest("#{lang}_#{text}") | |
file = "#{TMP_PATH}/gtts_#{digest}.mp3" | |
unless File.exist?(file) | |
File.open(file, 'w') do |f| | |
f.write RestClient.get("http://translate.google.com/translate_tts?tl=#{lang}&q=#{URI.encode(text)}") | |
end | |
end | |
file | |
end | |
end | |
end | |
GoogleTTS.say 'pure awesomeness' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment