Skip to content

Instantly share code, notes, and snippets.

@JackDanger
Forked from nakajima/google-says.rb
Created December 14, 2009 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JackDanger/256442 to your computer and use it in GitHub Desktop.
Save JackDanger/256442 to your computer and use it in GitHub Desktop.
# Saves an MP3 of the phrase using Google's text-to-voice.
#
# USAGE
#
# From the command line:
#
# $ ruby google-say.rb Hello World
#
# Or you can just require it and call Google.say(phrase)
require 'open-uri'
require 'cgi'
class Google
BASE = 'http://translate.google.com/translate_tts?tl=en&q='
def self.say(phrase, target="google-say.mp3")
raise ArgumentError, 'Too long bro' if phrase.size > 100
phrase = CGI.escape(phrase)
result = open(BASE + phrase)
File.open(target, 'w+') { |f| f.write(result.read) }
system("afplay #{target}")
end
end
if __FILE__ == $0
begin
puts "Saying: #{ARGV.join(' ')}"
Google.say(ARGV.join(' '))
rescue ArgumentError => e
abort e.message
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment