class AgileNotifier::TTS::Service

Public Class Methods

mary_tts(text, language) click to toggle source
# File lib/agile_notifier/tts.rb, line 99
def mary_tts(text, language)
  url = 'http://mary.dfki.de:59125/'
  text = encode_www_form_and_remove_punctuation(text)
  audio_format = 'WAVE_FILE'
  play(url + "process?INPUT_TEXT=#{text}&INPUT_TYPE=TEXT&OUTPUT_TYPE=AUDIO&LOCALE=#{language}&AUDIO=#{audio_format}")
end
speak_on_linux(text, language, voice = nil) click to toggle source
# File lib/agile_notifier/tts.rb, line 59
def speak_on_linux(text, language, voice = nil)
  # TODO: should check service availability first, if not raise exception
  mary_tts(text, language)
end
speak_on_osx(text, language, voice) click to toggle source
# File lib/agile_notifier/tts.rb, line 64
def speak_on_osx(text, language, voice)
  if voice
    unless osx_speech(voice, text)
      raise("No available voice found, please check if you have downloaded voice [#{voice}] in System Preferences -> Speech")
    end
  else
    list_of_available_voices = `say -v '?'`.split("\n")
    voices = list_of_available_voices.inject({}) do |collection, record|
      matched_results = record.match(/^(.*[^\s])\s+([a-z]{2})[_-][a-zA-Z]{2,}\s+/)
      available_language = matched_results[2].downcase.intern
      available_voice = matched_results[1]
      if collection.has_key?(available_language)
        collection[available_language] << available_voice
      else
        collection[available_language] = [available_voice]
      end
      collection
    end
    osx_speech(voices[language.intern].first, text)
  end
end
speak_on_windows(text, language, voice = nil) click to toggle source
# File lib/agile_notifier/tts.rb, line 86
def speak_on_windows(text, language, voice = nil)
  raise(NotImplementedError, "Method [#{__method__}] is empty, please implement", caller)
end
tts_api(text) click to toggle source
# File lib/agile_notifier/tts.rb, line 94
def tts_api(text)
  url = 'http://tts-api.com/tts.mp3?q='
  play(url + encode_www_form_and_remove_punctuation(text))
end

Private Class Methods

download_file(url, file = @@temp_file) click to toggle source
# File lib/agile_notifier/tts.rb, line 26
def download_file(url, file = @@temp_file)
  open(url) do |remote_file|
    data = remote_file.read
    File.open(file, 'wb') do |local_file|
      local_file.write(data)
    end
  end
end
encode_www_form(text) click to toggle source
# File lib/agile_notifier/tts.rb, line 51
def encode_www_form(text)
  URI.encode_www_form('' => text)[1..-1]
end
encode_www_form_and_remove_punctuation(text) click to toggle source
# File lib/agile_notifier/tts.rb, line 55
def encode_www_form_and_remove_punctuation(text)
  encode_www_form(replace_punctuation_by_space(text))
end
osx_speech(voice, text) click to toggle source
# File lib/agile_notifier/tts.rb, line 90
def osx_speech(voice, text)
  system("say -v #{voice} '#{text}' > /dev/null 2>&1")
end
play(url) click to toggle source
# File lib/agile_notifier/tts.rb, line 39
def play(url)
  download_file(url)
  os_type = get_os_type
  player = "play_on_#{os_type}"
  Player.send(player.intern, @@temp_file)
  remove_file
end
remove_file(file = @@temp_file) click to toggle source
# File lib/agile_notifier/tts.rb, line 35
def remove_file(file = @@temp_file)
  File.delete(file)
end
replace_punctuation_by_space(text) click to toggle source
# File lib/agile_notifier/tts.rb, line 47
def replace_punctuation_by_space(text)
  text.gsub(/[[:punct:]](\s+)*/, ' ').gsub(/\s$/, '')
end