-
-
Save JackDanger/256442 to your computer and use it in GitHub Desktop.
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
# 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