filename - The file that will be generated options - Posible key, values
:voice - use voice file of this name from espeak-data/voices. ie 'en', 'de', ... :pitch - pitch adjustment, 0 to 99 :speed - speed in words per minute, 80 to 370 :quiet - remove printing to stdout. Affects only lame (default false)
# File lib/espeak/speech.rb, line 14 def initialize(text, options={}) @text = text @options = options end
Returns mp3 file bytes as a result of Text-To-Speech conversion.
# File lib/espeak/speech.rb, line 35 def bytes() stdout_str, stderr_str, process = Open3.capture3(espeak_command(command_options, "--stdout") + " | " + std_lame_command(command_options)) stdout_str end
Returns wav file bytes as a result of Text-To-Speech conversion.
# File lib/espeak/speech.rb, line 43 def bytes_wav() stdout_str, stderr_str, process = Open3.capture3(espeak_command(command_options, "--stdout")) stdout_str end
espeak dies handling some chars this function sanitizes text
# File lib/espeak/speech.rb, line 51 def sanitized_text @text.gsub(/(!|\?|"|`|\)/, ' ').strip end
Generates mp3 file as a result of Text-To-Speech conversion.
# File lib/espeak/speech.rb, line 28 def save(filename) system(espeak_command(command_options, "--stdout") + " | " + lame_command(filename, command_options)) end
Speaks text
# File lib/espeak/speech.rb, line 21 def speak system(espeak_command(command_options)) end
# File lib/espeak/speech.rb, line 57 def command_options default_options.merge(symbolize_keys(options)) end
Although espeak itself has default options I'm defining them here for easier generating command (with simple hash.merge)
# File lib/espeak/speech.rb, line 65 def default_options { :voice => 'en', :pitch => 50, :speed => 170, :quiet => true } end
# File lib/espeak/speech.rb, line 72 def espeak_command(options, flags="") %Qespeak "#{sanitized_text}" #{flags} -v#{options[:voice]} -p#{options[:pitch]} -s#{options[:speed]}| end
# File lib/espeak/speech.rb, line 80 def lame_command(filename, options) "lame -V2 - #{filename} #{'--quiet' if options[:quiet] == true}" end
# File lib/espeak/speech.rb, line 76 def std_lame_command(options) lame_command("-", options) end
# File lib/espeak/speech.rb, line 84 def symbolize_keys(hash) hash.inject({}) do |options, (key, value)| options[(key.to_sym rescue key) || key] = value options end end