Class: PollySpeech
- Inherits:
-
Object
- Object
- PollySpeech
- Defined in:
- lib/pollyspeech.rb
Instance Method Summary collapse
-
#initialize(access_key: '', secret_key: '', region: 'us-east-1', voice_id: 'Emma', cache_filepath: 'cache') ⇒ PollySpeech
constructor
A new instance of PollySpeech.
- #tts(text = '', audiofile_out = '/tmp/polly.ogg') ⇒ Object
Constructor Details
#initialize(access_key: '', secret_key: '', region: 'us-east-1', voice_id: 'Emma', cache_filepath: 'cache') ⇒ PollySpeech
Returns a new instance of PollySpeech.
13 14 15 16 17 18 19 20 21 |
# File 'lib/pollyspeech.rb', line 13 def initialize(access_key: '', secret_key: '', region: 'us-east-1', voice_id: 'Emma', cache_filepath: 'cache') @polly = Aws::Polly::Client.new(region: region, credentials: Aws::Credentials.new(access_key, secret_key)) @voice_id = voice_id @cache_filepath = File.join(cache_filepath, voice_id) end |
Instance Method Details
#tts(text = '', audiofile_out = '/tmp/polly.ogg') ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/pollyspeech.rb', line 23 def tts(text='', audiofile_out='/tmp/polly.ogg') ext = File.extname(audiofile_out) out_format = {'.ogg' => 'ogg_vorbis', '.mp3' => 'mp3'}[ext] FileUtils.mkdir_p @cache_filepath h = Digest::MD5.new << text filename = File.join(@cache_filepath, h.to_s + ext) # 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 @polly.synthesize_speech(response_target: filename, text: text, output_format: out_format, voice_id: @voice_id) end FileUtils.cp filename, audiofile_out end |