module Tts
Public Class Methods
server_url(url=nil)
click to toggle source
# File lib/tts.rb, line 11 def self.server_url url=nil return @@default_url if url.nil? @@default_url = url end
Public Instance Methods
chunk_text(text)
click to toggle source
# File lib/tts.rb, line 33 def chunk_text text chunks = [] words = split_into_words(text) chunk = '' words.each do |word| if (chunk.length + word.length) > 100 chunks << chunk.strip! chunk = '' end chunk += "#{word} " end chunks << chunk.strip! end
fetch_mp3(url, file_name)
click to toggle source
# File lib/tts.rb, line 65 def fetch_mp3 url, file_name begin content = open(url, "User-Agent" => @@user_agent, "Referer" => @@referer).read File.open(temp_file_name, "wb") do |f| f.puts content end merge_mp3_file(file_name) rescue => e $stderr.puts("Internet error! #{e.message}") exit(1) end end
generate_file_name()
click to toggle source
# File lib/tts.rb, line 51 def generate_file_name to_valid_fn + ".mp3" end
merge_mp3_file(file_name)
click to toggle source
# File lib/tts.rb, line 87 def merge_mp3_file file_name `cat #{temp_file_name} >> "#{file_name}" && rm #{temp_file_name}` end
play(lang="en", times=1, pause_gap = 1)
click to toggle source
# File lib/tts.rb, line 91 def play lang="en", times=1, pause_gap = 1 #test if mpg123 exists? `which mpg123` if $?.to_i != 0 puts "mpg123 executable NOT found. This function only work with POSIX systems.\n Install mpg123 with `brew install mpg123` or `apt-get install mpg123`" exit 1 end self.to_file(lang, play_file_name) times.times{|i| `mpg123 --no-control -q #{play_file_name}`} File.delete(play_file_name) end
play_file_name()
click to toggle source
# File lib/tts.rb, line 83 def play_file_name @@play_file_file ||= Tempfile.new.path end
split_into_words(text)
click to toggle source
# File lib/tts.rb, line 47 def split_into_words text text.gsub(/\s+/m, ' ').strip.split(" ") end
temp_file_name()
click to toggle source
# File lib/tts.rb, line 79 def temp_file_name @@temp_file ||= Tempfile.new.path end
to_file(lang, file_name=nil)
click to toggle source
# File lib/tts.rb, line 16 def to_file lang, file_name=nil parts = validate_text_length(self) file_name = self[0..20].generate_file_name if file_name.nil? parts.each do |part| url = part.to_url(lang) fetch_mp3(url, file_name) end end
to_url(lang)
click to toggle source
# File lib/tts.rb, line 59 def to_url lang langs = ['af', 'ar', 'az', 'be', 'bg', 'bn', 'ca', 'cs', 'cy', 'da', 'de', 'el', 'en', 'en_us', 'en_gb', 'en_au', 'eo', 'es', 'et', 'eu', 'fa', 'fi', 'fr', 'ga', 'gl', 'gu', 'hi', 'hr', 'ht', 'hu', 'id', 'is', 'it', 'iw', 'ja', 'ka', 'kn', 'ko', 'la', 'lt', 'lv', 'mk', 'ms', 'mt', 'nl', 'no', 'pl', 'pt', 'ro', 'ru', 'sk', 'sl', 'sq', 'sr', 'sv', 'sw', 'ta', 'te', 'th', 'tl', 'tr', 'uk', 'ur', 'vi', 'yi', 'zh', 'zh-CN', 'zh-TW'] raise "Not accepted language, accpeted are #{langs * ","}" unless langs.include? lang base = "#{Tts.server_url}?tl=#{lang}&ie=UTF-8&client=tw-ob&q=#{URI.escape self}" end
to_valid_fn()
click to toggle source
# File lib/tts.rb, line 55 def to_valid_fn gsub(/[\x00\/\\:\*\?\"<>\|]/, '_') end
validate_text_length(text)
click to toggle source
# File lib/tts.rb, line 25 def validate_text_length text if text.length > 100 chunk_text(text) else [text] end end