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
- #split_into_words(text) ⇒ 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
10 11 12 13 |
# File 'lib/tts.rb', line 10 def self.server_url url=nil return @@default_url if url.nil? @@default_url = url end |
Instance Method Details
#chunk_text(text) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/tts.rb', line 32 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
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/tts.rb', line 64 def fetch_mp3 url, file_name begin content = open(url, "User-Agent" => @@user_agent, "Referer" => @@referer).read File.open("temp.mp3", "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
50 51 52 |
# File 'lib/tts.rb', line 50 def generate_file_name to_valid_fn + ".mp3" end |
#merge_mp3_file(file_name) ⇒ Object
78 79 80 |
# File 'lib/tts.rb', line 78 def merge_mp3_file file_name `cat temp.mp3 >> "#{file_name}" && rm temp.mp3` end |
#play(lang = "en", times = 1, pause_gap = 1) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/tts.rb', line 82 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 fn = "tts_playonce" self.to_file(lang, fn) times.times{|i| `mpg123 -q #{fn}`} File.delete(fn) end |
#split_into_words(text) ⇒ Object
46 47 48 |
# File 'lib/tts.rb', line 46 def split_into_words text text.gsub(/\s+/m, ' ').strip.split(" ") end |
#to_file(lang, file_name = nil) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/tts.rb', line 15 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
58 59 60 61 62 |
# File 'lib/tts.rb', line 58 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=t&q=#{URI.escape self}" end |
#to_valid_fn ⇒ Object
54 55 56 |
# File 'lib/tts.rb', line 54 def to_valid_fn gsub(/[\x00\/\\:\*\?\"<>\|]/, '_') end |
#validate_text_length(text) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/tts.rb', line 24 def validate_text_length text if text.length > 100 chunk_text(text) else [text] end end |