Class: Pollynomial::Synthesizer
- Inherits:
-
Object
- Object
- Pollynomial::Synthesizer
- Defined in:
- lib/pollynomial/synthesizer.rb
Constant Summary collapse
- POLLY_TEXT_LIMIT_SIZE =
1500
- DEFAULT_DELIMITER =
'[.。]'
- COMMA =
'[,、]'
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#output_format ⇒ Object
readonly
Returns the value of attribute output_format.
-
#sample_rate ⇒ Object
readonly
Returns the value of attribute sample_rate.
-
#voice_id ⇒ Object
readonly
Returns the value of attribute voice_id.
Instance Method Summary collapse
- #available_voices_in(language_code: 'en-US') ⇒ Object
-
#initialize(options = {}) ⇒ Synthesizer
constructor
A new instance of Synthesizer.
- #split_text(raw_text) ⇒ Object
- #synthesize(text, file_name: "tmp.mp3") ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Synthesizer
Returns a new instance of Synthesizer.
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/pollynomial/synthesizer.rb', line 11 def initialize(={}) [:region] ||= 'us-east-1' # You can use voice IDs http://docs.aws.amazon.com/polly/latest/dg/API_Voice.html # If you want to synthesize Japanese voice, you can use "Mizuki" @voice_id = .delete(:voice_id) || 'Joanna' @delimiter = .delete(:delimiter)|| DEFAULT_DELIMITER @comma = .delete(:comma) || COMMA @sample_rate = .delete(:sample_rate) || '16000' @output_format = .delete(:output_format) || 'mp3' @client = Aws::Polly::Client.new() end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
9 10 11 |
# File 'lib/pollynomial/synthesizer.rb', line 9 def client @client end |
#output_format ⇒ Object (readonly)
Returns the value of attribute output_format.
9 10 11 |
# File 'lib/pollynomial/synthesizer.rb', line 9 def output_format @output_format end |
#sample_rate ⇒ Object (readonly)
Returns the value of attribute sample_rate.
9 10 11 |
# File 'lib/pollynomial/synthesizer.rb', line 9 def sample_rate @sample_rate end |
#voice_id ⇒ Object (readonly)
Returns the value of attribute voice_id.
9 10 11 |
# File 'lib/pollynomial/synthesizer.rb', line 9 def voice_id @voice_id end |
Instance Method Details
#available_voices_in(language_code: 'en-US') ⇒ Object
41 42 43 44 |
# File 'lib/pollynomial/synthesizer.rb', line 41 def available_voices_in(language_code: 'en-US') voices = client.describe_voices(language_code: language_code) voices.voices if voices end |
#split_text(raw_text) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/pollynomial/synthesizer.rb', line 46 def split_text(raw_text) combined_texts = [] tmp_string = "" raw_text.split(/\n|(?<=#{@delimiter}) ?/).each do |text| if tmp_string.size + text.size > POLLY_TEXT_LIMIT_SIZE if tmp_string.size > POLLY_TEXT_LIMIT_SIZE combined_texts << tmp_string.split(/(?<=#{@comma})/) else combined_texts << tmp_string end tmp_string = text else tmp_string << " #{text}" end end combined_texts << tmp_string.lstrip combined_texts.flatten! combined_texts end |
#synthesize(text, file_name: "tmp.mp3") ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/pollynomial/synthesizer.rb', line 23 def synthesize(text, file_name: "tmp.mp3") File.delete(file_name) if File.exist?(file_name) File.open(file_name, 'ab') do |file| split_text(text).each do |_text| tmp_file = Tempfile.new client.synthesize_speech( response_target: tmp_file, text: _text, output_format: output_format, sample_rate: sample_rate, voice_id: voice_id ) IO.copy_stream(tmp_file, file) sleep(0.1) end end end |