Class: VoiceRSSe

Inherits:
Object
  • Object
show all
Defined in:
lib/voicerss2017e.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, config: {}, cache_filepath: 'cache') ⇒ VoiceRSSe

Returns a new instance of VoiceRSSe.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/voicerss2017e.rb', line 11

def initialize(api_key: nil, config: {}, cache_filepath: 'cache')

  @config = ({
    'key' => api_key,
    'hl' => 'en-gb',
    'src' => 'hello world',
    'r' => '0',
    'c' => 'mp3',
    'f' => '44khz_16bit_stereo',
    'ssml' => 'false',
    'b64' => 'false'
  }).merge config
  
  @cache_filepath = cache_filepath
  
end

Instance Method Details

#tts(text = '', audiofile_out = 'output.mp3') ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/voicerss2017e.rb', line 28

def tts(text='', audiofile_out='output.mp3')

  FileUtils.mkdir_p @cache_filepath
  
  h = Digest::MD5.new << text
  filename = File.join(@cache_filepath, h.to_s + '.mp3')
  
  
  # attempts to find the audio file from a local cache instead of 
  # making a relatively expensive request through the web API
  
  if not File.exists? filename then
          
    voice = VoiceRSS.speech(@config.merge!({'src' => text}))
    File.write filename, voice['response']      

  end    
  
    FileUtils.cp filename, audiofile_out    

end