eleven_rb 1.0.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,18 +12,68 @@ module ElevenRb
12
12
  # client.tts.stream("Hello world", voice_id: "voice_id") do |chunk|
13
13
  # io.write(chunk)
14
14
  # end
15
+ #
16
+ # @example Eleven v4 with continuity and a fixed seed
17
+ # audio = client.tts.generate(
18
+ # "[sighs] Right. Let's try that again.",
19
+ # voice_id: "voice_id",
20
+ # model_id: "eleven_v4",
21
+ # seed: 42,
22
+ # previous_text: "That did not go to plan."
23
+ # )
24
+ # audio.request_id # => "abc123" (from the request-id response header)
15
25
  class TextToSpeech < Base
16
26
  DEFAULT_MODEL = 'eleven_multilingual_v2'
27
+
28
+ # Kept for compatibility. The per-request cap now comes from
29
+ # ModelCapabilities.max_text_length(model_id) (5,000 for eleven_v3).
17
30
  MAX_TEXT_LENGTH = 5000
18
31
 
32
+ # Output formats the API accepts (documentation only; not validated)
19
33
  OUTPUT_FORMATS = %w[
34
+ mp3_22050_32
35
+ mp3_24000_48
36
+ mp3_44100_32
37
+ mp3_44100_64
38
+ mp3_44100_96
20
39
  mp3_44100_128
21
40
  mp3_44100_192
41
+ opus_48000_32
42
+ opus_48000_64
43
+ opus_48000_96
44
+ opus_48000_128
45
+ opus_48000_192
46
+ pcm_8000
22
47
  pcm_16000
23
48
  pcm_22050
24
49
  pcm_24000
50
+ pcm_32000
25
51
  pcm_44100
52
+ pcm_48000
53
+ wav_8000
54
+ wav_16000
55
+ wav_22050
56
+ wav_24000
57
+ wav_32000
58
+ wav_44100
59
+ wav_48000
26
60
  ulaw_8000
61
+ alaw_8000
62
+ ].freeze
63
+
64
+ # Optional request-body keys, in the order they are written to the body.
65
+ # Each is omitted from the body when nil.
66
+ OPTIONAL_BODY_KEYS = %i[
67
+ language_code
68
+ audio_effects
69
+ apply_text_normalization
70
+ seed
71
+ previous_text
72
+ next_text
73
+ previous_request_ids
74
+ next_request_ids
75
+ pronunciation_dictionary_locators
76
+ use_pvc_as_ivc
27
77
  ].freeze
28
78
 
29
79
  # Generate audio from text
@@ -31,30 +81,39 @@ module ElevenRb
31
81
  # @param text [String] the text to convert
32
82
  # @param voice_id [String] the voice ID to use
33
83
  # @param model_id [String] the model to use (default: eleven_multilingual_v2)
34
- # @param voice_settings [Hash] voice settings overrides
84
+ # @param voice_settings [Hash] voice settings overrides (keys the model ignores are dropped)
35
85
  # @param output_format [String] audio output format
36
- # @return [Objects::Audio]
37
- def generate(text, voice_id:, model_id: DEFAULT_MODEL, voice_settings: {}, output_format: 'mp3_44100_128')
38
- validate_text!(text)
86
+ # @param language_code [String, nil] ISO 639-1 language code to enforce
87
+ # @param apply_text_normalization [String, nil] "auto", "on" or "off"
88
+ # @param seed [Integer, nil] seed for reproducible generation
89
+ # @param previous_text [String, nil] text that comes before this request (continuity)
90
+ # @param next_text [String, nil] text that comes after this request (continuity)
91
+ # @param previous_request_ids [Array<String>, nil] request IDs of preceding generations
92
+ # @param next_request_ids [Array<String>, nil] request IDs of following generations
93
+ # @param pronunciation_dictionary_locators [Array<Hash>, nil] `{ pronunciation_dictionary_id:, version_id: }`
94
+ # @param use_pvc_as_ivc [Boolean, nil] use the IVC version of a professional voice
95
+ # @return [Objects::Audio] with request_id, character_cost and dropped_settings
96
+ def generate(text, voice_id:, model_id: DEFAULT_MODEL, voice_settings: {}, output_format: 'mp3_44100_128',
97
+ language_code: nil, apply_text_normalization: nil, seed: nil, previous_text: nil,
98
+ next_text: nil, previous_request_ids: nil, next_request_ids: nil,
99
+ pronunciation_dictionary_locators: nil, use_pvc_as_ivc: nil, audio_effects: nil)
100
+ validate_text!(text, model_id)
39
101
  validate_presence!(voice_id, 'voice_id')
40
-
41
- settings = Objects::VoiceSettings::DEFAULTS.merge(voice_settings)
42
-
43
- body = {
44
- text: text,
45
- model_id: model_id,
46
- voice_settings: settings
47
- }
102
+ body, dropped = build_body(text, model_id, voice_settings, optional_values(binding))
48
103
 
49
104
  path = "/text-to-speech/#{voice_id}?output_format=#{output_format}"
50
- response = post_binary(path, body)
105
+ response = post_binary_with_meta(path, body)
106
+ headers = response[:headers]
51
107
 
52
108
  audio = Objects::Audio.new(
53
- data: response,
109
+ data: response[:body],
54
110
  format: output_format,
55
111
  voice_id: voice_id,
56
112
  text: text,
57
- model_id: model_id
113
+ model_id: model_id,
114
+ request_id: headers['request-id'],
115
+ character_cost: integer_header(headers, 'character-cost'),
116
+ dropped_settings: dropped
58
117
  )
59
118
 
60
119
  # Trigger cost tracking callback
@@ -64,7 +123,8 @@ module ElevenRb
64
123
  audio: audio,
65
124
  voice_id: voice_id,
66
125
  text: text,
67
- cost_info: cost_info.to_h
126
+ cost_info: cost_info.to_h,
127
+ request_id: audio.request_id
68
128
  )
69
129
 
70
130
  audio
@@ -72,6 +132,8 @@ module ElevenRb
72
132
 
73
133
  # Stream audio from text
74
134
  #
135
+ # Takes the same optional keywords as {#generate}.
136
+ #
75
137
  # @param text [String] the text to convert
76
138
  # @param voice_id [String] the voice ID to use
77
139
  # @param model_id [String] the model to use
@@ -79,56 +141,42 @@ module ElevenRb
79
141
  # @param output_format [String] audio output format
80
142
  # @yield [String] each chunk of audio data
81
143
  # @return [void]
82
- def stream(text, voice_id:, model_id: DEFAULT_MODEL, voice_settings: {}, output_format: 'mp3_44100_128', &block)
83
- validate_text!(text)
84
- validate_presence!(voice_id, 'voice_id')
85
- raise ArgumentError, 'Block required for streaming' unless block_given?
86
-
87
- settings = Objects::VoiceSettings::DEFAULTS.merge(voice_settings)
88
-
89
- body = {
90
- text: text,
91
- model_id: model_id,
92
- voice_settings: settings
93
- }
94
-
95
- path = "/text-to-speech/#{voice_id}/stream?output_format=#{output_format}"
96
- post_stream(path, body, &block)
144
+ def stream(text, voice_id:, model_id: DEFAULT_MODEL, voice_settings: {}, output_format: 'mp3_44100_128',
145
+ language_code: nil, apply_text_normalization: nil, seed: nil, previous_text: nil,
146
+ next_text: nil, previous_request_ids: nil, next_request_ids: nil,
147
+ pronunciation_dictionary_locators: nil, use_pvc_as_ivc: nil, audio_effects: nil, &block)
148
+ deliver_stream(text, voice_id: voice_id, model_id: model_id, voice_settings: voice_settings,
149
+ output_format: output_format, timestamps: false, **optional_values(binding), &block)
150
+ end
97
151
 
98
- # Trigger cost tracking callback after streaming completes
99
- cost_info = Objects::CostInfo.new(text: text, voice_id: voice_id, model_id: model_id)
100
- http_client.config.trigger(
101
- :on_audio_generated,
102
- audio: nil, # No audio object for streaming
103
- voice_id: voice_id,
104
- text: text,
105
- cost_info: cost_info.to_h
106
- )
152
+ def stream_with_timestamps(text, **options, &block)
153
+ deliver_stream(text, **options, timestamps: true, &block)
107
154
  end
108
155
 
109
156
  # Generate audio with timestamps
110
157
  #
158
+ # Takes the same optional keywords as {#generate}.
159
+ #
111
160
  # @param text [String] the text to convert
112
161
  # @param voice_id [String] the voice ID to use
113
162
  # @param model_id [String] the model to use
114
163
  # @param voice_settings [Hash] voice settings overrides
115
164
  # @param output_format [String] audio output format
116
- # @return [Hash] contains :audio and :alignment data
165
+ # @return [Hash] `{ audio:, alignment:, normalized_alignment:, request_id:, character_cost: }`
117
166
  def generate_with_timestamps(text, voice_id:, model_id: DEFAULT_MODEL, voice_settings: {},
118
- output_format: 'mp3_44100_128')
119
- validate_text!(text)
167
+ output_format: 'mp3_44100_128', language_code: nil,
168
+ apply_text_normalization: nil, seed: nil, previous_text: nil,
169
+ next_text: nil, previous_request_ids: nil, next_request_ids: nil,
170
+ pronunciation_dictionary_locators: nil, use_pvc_as_ivc: nil, audio_effects: nil)
171
+ validate_text!(text, model_id)
120
172
  validate_presence!(voice_id, 'voice_id')
121
-
122
- settings = Objects::VoiceSettings::DEFAULTS.merge(voice_settings)
123
-
124
- body = {
125
- text: text,
126
- model_id: model_id,
127
- voice_settings: settings
128
- }
173
+ body, dropped = build_body(text, model_id, voice_settings, optional_values(binding))
129
174
 
130
175
  path = "/text-to-speech/#{voice_id}/with-timestamps?output_format=#{output_format}"
131
- response = post(path, body)
176
+ result = post_with_meta(path, body)
177
+ response = result[:body]
178
+ request_id = result[:headers]['request-id']
179
+ character_cost = integer_header(result[:headers], 'character-cost')
132
180
 
133
181
  # Decode base64 audio
134
182
  audio_data = Base64.decode64(response['audio_base64']) if response['audio_base64']
@@ -139,25 +187,102 @@ module ElevenRb
139
187
  format: output_format,
140
188
  voice_id: voice_id,
141
189
  text: text,
142
- model_id: model_id
190
+ model_id: model_id,
191
+ request_id: request_id,
192
+ character_cost: character_cost,
193
+ dropped_settings: dropped
143
194
  )
144
195
  end
145
196
 
146
197
  {
147
198
  audio: audio,
148
- alignment: response['alignment']
199
+ alignment: response['alignment'],
200
+ normalized_alignment: response['normalized_alignment'],
201
+ request_id: request_id,
202
+ character_cost: character_cost
149
203
  }
150
204
  end
151
205
 
152
206
  private
153
207
 
154
- def validate_text!(text)
208
+ def deliver_stream(text, voice_id:, timestamps:, model_id: DEFAULT_MODEL, voice_settings: {},
209
+ output_format: 'mp3_44100_128', **options, &block)
210
+ unknown = options.keys - OPTIONAL_BODY_KEYS
211
+ raise ArgumentError, "Unknown keywords: #{unknown.join(', ')}" unless unknown.empty?
212
+
213
+ validate_text!(text, model_id)
214
+ validate_presence!(voice_id, 'voice_id')
215
+ raise ArgumentError, 'Block required for streaming' unless block_given?
216
+
217
+ body, = build_body(text, model_id, voice_settings, options)
218
+
219
+ path = "/text-to-speech/#{voice_id}/stream?output_format=#{output_format}"
220
+ headers = if timestamps
221
+ stream_timestamps(path.sub('/stream?', '/stream/with-timestamps?'), body, &block)
222
+ else
223
+ post_stream(path, body, &block)
224
+ end
225
+
226
+ # Trigger cost tracking callback after streaming completes
227
+ cost_info = Objects::CostInfo.new(text: text, voice_id: voice_id, model_id: model_id)
228
+ http_client.config.trigger(
229
+ :on_audio_generated,
230
+ audio: nil, # No audio object for streaming
231
+ voice_id: voice_id,
232
+ text: text,
233
+ cost_info: cost_info.to_h,
234
+ request_id: headers['request-id']
235
+ )
236
+ end
237
+
238
+ # Shared request body for generate / stream / generate_with_timestamps
239
+ #
240
+ # @return [Array(Hash, Array<Symbol>)] the body and the dropped voice-setting keys
241
+ def build_body(text, model_id, voice_settings, options)
242
+ settings, dropped = resolve_voice_settings(model_id, voice_settings)
243
+
244
+ body = { text: text, model_id: model_id, voice_settings: settings }
245
+ OPTIONAL_BODY_KEYS.each do |key|
246
+ body[key] = options[key] unless options[key].nil?
247
+ end
248
+
249
+ [body, dropped]
250
+ end
251
+
252
+ # The optional keyword values of the calling method, keyed by OPTIONAL_BODY_KEYS
253
+ def optional_values(caller_binding)
254
+ OPTIONAL_BODY_KEYS.to_h { |key| [key, caller_binding.local_variable_get(key)] }
255
+ end
256
+
257
+ def resolve_voice_settings(model_id, voice_settings)
258
+ settings, dropped = Objects::VoiceSettings.for_model(model_id, voice_settings)
259
+ return [settings, dropped] if dropped.empty?
260
+
261
+ message = "voice settings #{dropped.join(', ')} are not supported by #{model_id} " \
262
+ "(supported: #{ModelCapabilities.supported_voice_settings(model_id).join(', ')})"
263
+ raise Errors::ValidationError, message if http_client.config.strict_voice_settings
264
+
265
+ http_client.config.logger&.warn("[ElevenRb] #{message}; dropped from the request")
266
+ [settings, dropped]
267
+ end
268
+
269
+ def integer_header(headers, name)
270
+ value = headers[name]
271
+ return nil if value.nil? || value.to_s.strip.empty?
272
+
273
+ Integer(value.to_s.strip, 10)
274
+ rescue ArgumentError
275
+ nil
276
+ end
277
+
278
+ def validate_text!(text, model_id = DEFAULT_MODEL)
155
279
  validate_presence!(text, 'text')
156
280
 
157
- return unless text.length > MAX_TEXT_LENGTH
281
+ max_length = ModelCapabilities.max_text_length(model_id)
282
+ return unless text.length > max_length
158
283
 
159
284
  raise Errors::ValidationError,
160
- "text exceeds maximum length of #{MAX_TEXT_LENGTH} characters (got #{text.length})"
285
+ "text exceeds maximum length of #{max_length} characters for #{model_id} (got #{text.length})"
161
286
  end
162
287
  end
163
288
  end
@@ -16,9 +16,22 @@ module ElevenRb
16
16
  # List all voices in your account
17
17
  #
18
18
  # @return [Collections::VoiceCollection]
19
- def list
20
- response = get('/voices')
21
- Collections::VoiceCollection.from_response(response)
19
+ def list(**filters)
20
+ voices = []
21
+ query = filters.dup
22
+ seen_tokens = {}
23
+ loop do
24
+ page = get('/v2/voices', query)
25
+ voices.concat(page.fetch('voices'))
26
+ break unless page['has_more']
27
+
28
+ token = page['next_page_token']
29
+ raise Errors::APIError, 'Invalid voice pagination cursor' if token.to_s.empty? || seen_tokens[token]
30
+
31
+ seen_tokens[token] = true
32
+ query[:next_page_token] = token
33
+ end
34
+ Collections::VoiceCollection.from_response('voices' => voices)
22
35
  end
23
36
 
24
37
  # Get details for a specific voice
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ElevenRb
4
- VERSION = '1.0.0'
4
+ VERSION = '1.2.0'
5
5
  end
data/lib/eleven_rb.rb CHANGED
@@ -60,6 +60,7 @@ module ElevenRb
60
60
  retry_delay: config.retry_delay,
61
61
  retry_statuses: config.retry_statuses,
62
62
  logger: config.logger,
63
+ strict_voice_settings: config.strict_voice_settings,
63
64
  on_request: config.on_request,
64
65
  on_response: config.on_response,
65
66
  on_error: config.on_error,
@@ -79,6 +80,7 @@ require_relative 'eleven_rb/errors'
79
80
  require_relative 'eleven_rb/callbacks'
80
81
  require_relative 'eleven_rb/instrumentation'
81
82
  require_relative 'eleven_rb/configuration'
83
+ require_relative 'eleven_rb/model_capabilities'
82
84
 
83
85
  # HTTP layer
84
86
  require_relative 'eleven_rb/http/client'
@@ -101,6 +103,8 @@ require_relative 'eleven_rb/collections/library_voice_collection'
101
103
 
102
104
  # Resources
103
105
  require_relative 'eleven_rb/resources/base'
106
+ require_relative 'eleven_rb/resources/audio_effects'
107
+ require_relative 'eleven_rb/resources/speech_to_text'
104
108
  require_relative 'eleven_rb/resources/voices'
105
109
  require_relative 'eleven_rb/resources/text_to_speech'
106
110
  require_relative 'eleven_rb/resources/voice_library'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eleven_rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Web Ventures Ltd
@@ -37,6 +37,48 @@ dependencies:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
39
  version: '0.21'
40
+ - !ruby/object:Gem::Dependency
41
+ name: mini_mime
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.1'
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.1'
54
+ - !ruby/object:Gem::Dependency
55
+ name: multipart-post
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '2.4'
61
+ type: :runtime
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '2.4'
68
+ - !ruby/object:Gem::Dependency
69
+ name: websocket-driver
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '0.8'
75
+ type: :runtime
76
+ prerelease: false
77
+ version_requirements: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '0.8'
40
82
  - !ruby/object:Gem::Dependency
41
83
  name: rake
42
84
  requirement: !ruby/object:Gem::Requirement
@@ -143,7 +185,9 @@ files:
143
185
  - lib/eleven_rb/configuration.rb
144
186
  - lib/eleven_rb/errors.rb
145
187
  - lib/eleven_rb/http/client.rb
188
+ - lib/eleven_rb/http/realtime_transcription.rb
146
189
  - lib/eleven_rb/instrumentation.rb
190
+ - lib/eleven_rb/model_capabilities.rb
147
191
  - lib/eleven_rb/objects/audio.rb
148
192
  - lib/eleven_rb/objects/base.rb
149
193
  - lib/eleven_rb/objects/cost_info.rb
@@ -153,11 +197,13 @@ files:
153
197
  - lib/eleven_rb/objects/user_info.rb
154
198
  - lib/eleven_rb/objects/voice.rb
155
199
  - lib/eleven_rb/objects/voice_settings.rb
200
+ - lib/eleven_rb/resources/audio_effects.rb
156
201
  - lib/eleven_rb/resources/base.rb
157
202
  - lib/eleven_rb/resources/models.rb
158
203
  - lib/eleven_rb/resources/music.rb
159
204
  - lib/eleven_rb/resources/sound_effects.rb
160
205
  - lib/eleven_rb/resources/speech_to_speech.rb
206
+ - lib/eleven_rb/resources/speech_to_text.rb
161
207
  - lib/eleven_rb/resources/text_to_dialogue.rb
162
208
  - lib/eleven_rb/resources/text_to_speech.rb
163
209
  - lib/eleven_rb/resources/user.rb