Class: Pollynomial::Synthesizer

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

Constant Summary collapse

POLLY_TEXT_LIMIT_SIZE =
1500
DEFAULT_DELIMITER =
'[.。]'
COMMA =
'[,、]'

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options={})
  options[: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 = options.delete(:voice_id) || 'Joanna'
  @delimiter = options.delete(:delimiter)|| DEFAULT_DELIMITER
  @comma = options.delete(:comma) || COMMA
  @sample_rate = options.delete(:sample_rate) || '16000'
  @output_format = options.delete(:output_format) || 'mp3'
  @client = Aws::Polly::Client.new(options)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/pollynomial/synthesizer.rb', line 9

def client
  @client
end

#output_formatObject (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_rateObject (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_idObject (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