Skip to content

Instantly share code, notes, and snippets.

@pachacamac
Created June 10, 2012 15:59
Show Gist options
  • Save pachacamac/2906367 to your computer and use it in GitHub Desktop.
Save pachacamac/2906367 to your computer and use it in GitHub Desktop.
Google TTS
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