Class: Typecast::SpeechComposer
- Inherits:
-
Object
- Object
- Typecast::SpeechComposer
- Defined in:
- lib/typecast/composer.rb
Instance Method Summary collapse
- #defaults(voice_id: nil, model: nil, language: nil, prompt: nil, output: nil, seed: nil) ⇒ Object
- #generate ⇒ Object
-
#initialize(compose) ⇒ SpeechComposer
constructor
A new instance of SpeechComposer.
-
#pause(seconds) ⇒ Object
Inserts silence between speech segments.
- #say(text, voice_id: nil, model: nil, language: nil, prompt: nil, output: nil, seed: nil) ⇒ Object
Constructor Details
#initialize(compose) ⇒ SpeechComposer
Returns a new instance of SpeechComposer.
29 30 31 32 33 |
# File 'lib/typecast/composer.rb', line 29 def initialize(compose) @compose = compose @defaults = {} @parts = [] end |
Instance Method Details
#defaults(voice_id: nil, model: nil, language: nil, prompt: nil, output: nil, seed: nil) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/typecast/composer.rb', line 35 def defaults(voice_id: nil, model: nil, language: nil, prompt: nil, output: nil, seed: nil) @defaults = merge_settings(@defaults, settings_hash( voice_id: voice_id, model: model, language: language, prompt: prompt, output: output, seed: seed )) self end |
#generate ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/typecast/composer.rb', line 75 def generate plan = build_plan unless plan.any? { |part| part.is_a?(Hash) && part[:kind] == "speech" } raise ArgumentError, "at least one speech segment is required" end formats = plan.each_with_object([]) do |part, values| format = part.is_a?(Hash) && part[:kind] == "speech" ? part.dig(:settings, :output, :audio_format) : nil values << format if format end.uniq raise ArgumentError, "composed speech segments must use one audio format" if formats.length > 1 output_format = formats.first || Models::AUDIO_WAV unless [Models::AUDIO_WAV, Models::AUDIO_MP3].include?(output_format) raise ArgumentError, "unsupported composed speech output format: #{output_format}" end segments = plan.map do |part| part.is_a?(PausePart) ? { type: "pause", duration_seconds: part.seconds } : { type: "tts", **request_from_settings(part[:text], part[:settings], output_format).to_h } end @compose.call(segments) end |
#pause(seconds) ⇒ Object
Inserts silence between speech segments.
seconds is a duration in seconds. Use 0.3 for 300 ms, 3 for 3 seconds.
66 67 68 69 70 71 72 73 |
# File 'lib/typecast/composer.rb', line 66 def pause(seconds) unless seconds.is_a?(Numeric) && seconds.finite? && seconds.positive? raise ArgumentError, "pause seconds must be greater than 0" end @parts << PausePart.new(kind: "pause", seconds: seconds.to_f) self end |
#say(text, voice_id: nil, model: nil, language: nil, prompt: nil, output: nil, seed: nil) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/typecast/composer.rb', line 47 def say(text, voice_id: nil, model: nil, language: nil, prompt: nil, output: nil, seed: nil) @parts << { kind: "speech", text: text.to_s, settings: merge_settings(@defaults, settings_hash( voice_id: voice_id, model: model, language: language, prompt: prompt, output: output, seed: seed )) } self end |