Created
November 13, 2012 13:28
-
-
Save danguita/4065761 to your computer and use it in GitHub Desktop.
Text to speech through tts-api.com
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
#!/usr/bin/env ruby | |
require_relative 'tts' | |
class String | |
include TTS::String | |
end | |
"fuck yeah".to_speech |
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
#!/usr/bin/env ruby | |
# string.to_speech through tts-api.com | |
require 'net/http' | |
SERVICE_URI_FORMAT = "http://tts-api.com/tts.mp3?q=%s" | |
module TTS | |
class Speech | |
def initialize text | |
response = get_response(SERVICE_URI_FORMAT % text.to_url) | |
@text = text | |
@location = response['location'] | |
@filename = text.to_filename('mp3') | |
end | |
def to_mp3 | |
download_here @location, @filename | |
end | |
def to_s | |
[@text, @location, @filename].join(' -> ') | |
end | |
private | |
def get_response service_uri | |
Net::HTTP.get_response(URI.parse(service_uri)) | |
end | |
def download_here url, filename | |
system "curl -o #{filename} #{url}" | |
end | |
end | |
module String | |
def to_speech | |
speech = Speech.new(self) | |
puts speech | |
speech.to_mp3 | |
end | |
def to_url | |
gsub(' ', '+').safe | |
end | |
def to_filename extension = 'bin' | |
[(gsub(' ', '_')).safe, extension].join('.') | |
end | |
def safe | |
downcase.strip.gsub(/[^\w-]/, '') | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Blog post: http://blog.davidanguita.name/2012/11/23/text-to-speech-through-tts-api-com/