Module: Tts
- Included in:
- String
- Defined in:
- lib/tts.rb
Constant Summary collapse
- @@default_url =
"http://translate.google.com/translate_tts"
- @@user_agent =
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.68 Safari/534.24"
- @@referer =
"http://translate.google.com/"
Class Method Summary collapse
Instance Method Summary collapse
- #chunk_text(text) ⇒ Object
- #fetch_mp3(url, file_name) ⇒ Object
- #generate_file_name ⇒ Object
- #merge_mp3_file(file_name) ⇒ Object
- #play(lang = "en", times = 1, pause_gap = 1) ⇒ Object
- #play_file_name ⇒ Object
- #split_into_words(text) ⇒ Object
- #temp_file_name ⇒ Object
- #to_file(lang, file_name = nil) ⇒ Object
- #to_url(lang) ⇒ Object
- #to_valid_fn ⇒ Object
- #validate_text_length(text) ⇒ Object
Class Method Details
.server_url(url = nil) ⇒ Object
11 12 13 14 |
# File 'lib/tts.rb', line 11 def self.server_url url=nil return @@default_url if url.nil? @@default_url = url end |
Instance Method Details
#chunk_text(text) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# 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) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# 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.}") exit(1) end end |
#generate_file_name ⇒ Object
51 52 53 |
# File 'lib/tts.rb', line 51 def generate_file_name to_valid_fn + ".mp3" end |
#merge_mp3_file(file_name) ⇒ Object
87 88 89 |
# 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) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 |
# 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 ⇒ Object
83 84 85 |
# File 'lib/tts.rb', line 83 def play_file_name @@play_file_file ||= Tempfile.new.path end |
#split_into_words(text) ⇒ Object
47 48 49 |
# File 'lib/tts.rb', line 47 def split_into_words text text.gsub(/\s+/m, ' ').strip.split(" ") end |
#temp_file_name ⇒ Object
79 80 81 |
# File 'lib/tts.rb', line 79 def temp_file_name @@temp_file ||= Tempfile.new.path end |
#to_file(lang, file_name = nil) ⇒ Object
16 17 18 19 20 21 22 23 |
# 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) ⇒ Object
59 60 61 62 63 |
# 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 ⇒ Object
55 56 57 |
# File 'lib/tts.rb', line 55 def to_valid_fn gsub(/[\x00\/\\:\*\?\"<>\|]/, '_') end |
#validate_text_length(text) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/tts.rb', line 25 def validate_text_length text if text.length > 100 chunk_text(text) else [text] end end |