Skip to content

Instantly share code, notes, and snippets.

@danguita
Created November 13, 2012 13:28
Show Gist options
  • Save danguita/4065761 to your computer and use it in GitHub Desktop.
Save danguita/4065761 to your computer and use it in GitHub Desktop.
Text to speech through tts-api.com
#!/usr/bin/env ruby
require_relative 'tts'
class String
include TTS::String
end
"fuck yeah".to_speech
#!/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
@danguita
Copy link
Author

danguita commented Mar 5, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment