Last active
August 29, 2015 14:05
-
-
Save andrius/91130ecc94ac215d0894 to your computer and use it in GitHub Desktop.
Google TTS
This file contains 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
def tts(message, lang = 'en-US') | |
GoogleTTS.download(message, lang) | |
end | |
def read_pin(message, lang = 'en-US') | |
prompt = { audio: ANNOUNCEMENTS[lang][message], | |
max_digits: 10, | |
timeout: 20 } | |
#3.times do |attempt_no| | |
data = begin | |
AGISelection.new(prompt).read(:agi => agi).to_s | |
rescue => e | |
'' | |
end | |
LOGGER.info "Entered data: #{data.inspect}" | |
result = data.to_s | |
return result unless result.empty? | |
# change announcement here (no result) | |
crm_empty_pin | |
#end | |
'' | |
end |
This file contains 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
# encoding: utf-8 | |
# Main module source taken here | |
# https://gist.github.com/pachacamac/2906367 | |
# and | |
# http://jaredlevitz.com/post/61885725912/from-text-to-speech-using-ruby-and-google-translate | |
require 'net/http' | |
module GoogleTTS | |
require 'digest/md5' | |
require 'restclient' | |
TTS_PATH = "#{APP_ROOT}/tts" | |
class << self | |
def play(text, lang='en-US') | |
file = download_audio(text, lang) | |
mp3 = "#{file}.mp3" | |
@gttspid = fork{ exec 'mpg123', '-q', file } | |
end | |
def download(text, lang='en-US') | |
file = download_audio(text, lang) | |
mp3 = "#{file}.mp3" | |
sln = "#{file}.sln" | |
unless File.exist?(sln) | |
system("ffmpeg -i #{mp3} -ar 8000 -ac 1 -acodec pcm_s16le -f s16le -y #{sln} 2> /dev/null") | |
end | |
file | |
end | |
private | |
def download_audio(text, lang='en-US') | |
digest = Digest::MD5.hexdigest("#{lang}_#{text}") | |
file = "#{TTS_PATH}/gtts_#{digest}" | |
mp3 = "#{file}.mp3" | |
uri = URI('http://translate.google.com/translate_tts') | |
unless File.exist?(mp3) | |
# File.open(mp3, 'w') do |f| | |
# f.write RestClient.get("http://translate.google.com/translate_tts?tl=#{lang}&q=#{URI.encode(text)}") | |
# end | |
attempts = text.length / 99.0 | |
starts = 0 | |
ar = response = [] | |
attempts.ceil.times do | |
ends = starts + 99 | |
ar << text[starts...ends] | |
starts = ends | |
end | |
ar.each do |q| | |
uri.query = URI.encode_www_form({ ie: 'UTF-8', q: q, tl: lang, total: 1, idx: 5, textlen: q.length, prev: 'input' }) | |
res = Net::HTTP.get_response(uri) | |
response << res.body.force_encoding(Encoding::UTF_8) if res.is_a?(Net::HTTPSuccess) | |
end | |
File.open(mp3, 'wb') do |f| | |
f.write response.join | |
# return f.path | |
end | |
end | |
file | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment