eleven_rb 1.1.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/README.md +62 -9
- data/lib/eleven_rb/client.rb +10 -0
- data/lib/eleven_rb/http/client.rb +55 -59
- data/lib/eleven_rb/http/realtime_transcription.rb +88 -0
- data/lib/eleven_rb/model_capabilities.rb +2 -2
- data/lib/eleven_rb/objects/audio.rb +6 -3
- data/lib/eleven_rb/objects/voice_settings.rb +3 -1
- data/lib/eleven_rb/resources/audio_effects.rb +11 -0
- data/lib/eleven_rb/resources/base.rb +44 -0
- data/lib/eleven_rb/resources/music.rb +93 -124
- data/lib/eleven_rb/resources/sound_effects.rb +8 -13
- data/lib/eleven_rb/resources/speech_to_speech.rb +21 -9
- data/lib/eleven_rb/resources/speech_to_text.rb +37 -0
- data/lib/eleven_rb/resources/text_to_dialogue.rb +29 -6
- data/lib/eleven_rb/resources/text_to_speech.rb +39 -21
- data/lib/eleven_rb/resources/voices.rb +16 -3
- data/lib/eleven_rb/version.rb +1 -1
- data/lib/eleven_rb.rb +2 -0
- metadata +46 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 34de9ec2892de8dec87b1bfe6337516288f37db698f1ad4f5919a0c3ff8d5bb9
|
|
4
|
+
data.tar.gz: 829ad63126553c0828253b41e58f25b1141751a617ed57ee6fcb2e48c47e662f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 980e55ff51d0c558deffa4c543f3da5db31a9e46cd339af314d481807d105cf5469bc3510e5fde9b3d63ff27a7b049078adfde6984830ebf53484115e8956bf2
|
|
7
|
+
data.tar.gz: 0a4638a7215d5cda2a539be65e863db94a8713e0d761c40067e1304305388158e781c63c633a24ab4f072011b558bfe5cd101a4c646927f003ccd8c57fde978d
|
data/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [1.2.0] - 2026-12-02
|
|
11
|
+
|
|
12
|
+
- Fixed streaming chunk delivery, error propagation and response callbacks. Speech and dialogue support `stream_with_timestamps`.
|
|
13
|
+
- Raised v4 and v4 Turbo Text to Speech limits to 100,000 characters. Added professional clone strength, audio effects and the effects catalog.
|
|
14
|
+
- Migrated voice listing to cursor-paginated v2. Voice conversion uses the Voice Changer HTTP endpoints and supports streaming.
|
|
15
|
+
- Upgraded music to `music_v2_5` and sound effects to `eleven_text_to_sound_v3`. Saved v1 plans convert to chunks for v2 generation. Music supports song IDs, audio references, inpainting uploads and video scoring with optional ZIP output.
|
|
16
|
+
- Music and sound-effect callback data now includes requested seconds and provider-reported credits. The USD estimate is nil for these endpoints.
|
|
17
|
+
- Added Scribe v3 recorded transcription and Scribe v3 Live WebSocket sessions.
|
|
18
|
+
|
|
10
19
|
## [1.1.0] - 2026-09-30
|
|
11
20
|
|
|
12
21
|
### Added
|
data/README.md
CHANGED
|
@@ -90,7 +90,7 @@ File.open("output.mp3", "wb") do |file|
|
|
|
90
90
|
end
|
|
91
91
|
end
|
|
92
92
|
|
|
93
|
-
#
|
|
93
|
+
# Character timestamps
|
|
94
94
|
result = client.tts.generate_with_timestamps("Hello world", voice_id: "voice_id")
|
|
95
95
|
result[:audio] # => ElevenRb::Objects::Audio
|
|
96
96
|
result[:alignment] # => { "characters" => [...], "character_start_times_seconds" => [...], ... }
|
|
@@ -103,9 +103,20 @@ Optional keywords on `generate`, `stream` and `generate_with_timestamps` (each i
|
|
|
103
103
|
`language_code`, `apply_text_normalization`, `seed`, `previous_text`, `next_text`, `previous_request_ids`,
|
|
104
104
|
`next_request_ids`, `pronunciation_dictionary_locators`, `use_pvc_as_ivc`.
|
|
105
105
|
|
|
106
|
+
The `audio_effects:` keyword on speech and dialogue calls accepts ElevenLabs’ effect settings.
|
|
107
|
+
Read the available presets with `client.audio_effects.catalog`. For incremental alignment use
|
|
108
|
+
`stream_with_timestamps` on either resource. Each yielded event is a hash with string keys.
|
|
109
|
+
|
|
110
|
+
```ruby
|
|
111
|
+
client.tts.stream_with_timestamps("Hello world", voice_id: "voice_id") do |event|
|
|
112
|
+
output_io.write(Base64.strict_decode64(event.fetch("audio_base64")))
|
|
113
|
+
collect_alignment(event["alignment"])
|
|
114
|
+
end
|
|
115
|
+
```
|
|
116
|
+
|
|
106
117
|
### Eleven v4
|
|
107
118
|
|
|
108
|
-
`eleven_v4` (and the cheaper, faster `eleven_v4_turbo`)
|
|
119
|
+
`eleven_v4` (and the cheaper, faster `eleven_v4_turbo`) accept up to 100,000 characters per Text to Speech request.
|
|
109
120
|
|
|
110
121
|
```ruby
|
|
111
122
|
audio = client.tts.generate(
|
|
@@ -113,7 +124,7 @@ audio = client.tts.generate(
|
|
|
113
124
|
voice_id: "voice_id",
|
|
114
125
|
model_id: "eleven_v4",
|
|
115
126
|
voice_settings: { stability: 0.5, similarity_boost: 0.75 },
|
|
116
|
-
seed: 42, #
|
|
127
|
+
seed: 42, # more consistent re-rolls
|
|
117
128
|
previous_text: "That did not go to plan.", # continuity with the take before
|
|
118
129
|
next_text: "Here we go." # ...and the one after
|
|
119
130
|
)
|
|
@@ -122,7 +133,7 @@ audio.character_cost
|
|
|
122
133
|
audio.dropped_settings # => [] (voice settings the gem removed, see below)
|
|
123
134
|
```
|
|
124
135
|
|
|
125
|
-
- **
|
|
136
|
+
- **Use `stability` and `similarity_boost` for v4.** For a professional voice clone fine-tuned on v4, `clone_strength` (0–1) controls how closely the voice follows its training samples. Omitting it uses the voice’s stored setting. The API accepts `speed`, `style` and
|
|
126
137
|
`use_speaker_boost` on v4 and silently ignores them, so the gem drops them from the request, logs a
|
|
127
138
|
warning through `logger`, and lists them on `audio.dropped_settings`. Set
|
|
128
139
|
`strict_voice_settings: true` on the client to raise `ElevenRb::Errors::ValidationError` instead.
|
|
@@ -135,12 +146,12 @@ audio.dropped_settings # => [] (voice settings the gem removed, see below)
|
|
|
135
146
|
`[short pause]`. Note that tags appear in the returned alignment like any other characters.
|
|
136
147
|
- **Continuity**: `previous_text` / `next_text` (or `previous_request_ids` / `next_request_ids`) tell the
|
|
137
148
|
model what surrounds a take, so stitched takes keep a consistent delivery.
|
|
138
|
-
- **`seed`**
|
|
149
|
+
- **`seed`** can improve consistency across takes.
|
|
139
150
|
|
|
140
151
|
`ElevenRb::ModelCapabilities` answers these questions for any model id:
|
|
141
152
|
|
|
142
153
|
```ruby
|
|
143
|
-
ElevenRb::ModelCapabilities.supported_voice_settings("eleven_v4") # => [:stability, :similarity_boost]
|
|
154
|
+
ElevenRb::ModelCapabilities.supported_voice_settings("eleven_v4") # => [:stability, :similarity_boost, :clone_strength]
|
|
144
155
|
ElevenRb::ModelCapabilities.max_text_length("eleven_v3") # => 5000
|
|
145
156
|
ElevenRb::ModelCapabilities.supports?("eleven_v4", :audio_tags) # => true
|
|
146
157
|
ElevenRb::ModelCapabilities.supports?("eleven_v4", :ssml_break) # => false
|
|
@@ -148,7 +159,7 @@ ElevenRb::ModelCapabilities.supports?("eleven_v4", :ssml_break) # => false
|
|
|
148
159
|
|
|
149
160
|
| Model family | Voice settings honoured | Max chars | Audio tags | SSML breaks |
|
|
150
161
|
|---|---|---|---|---|
|
|
151
|
-
| `eleven_v4`, `eleven_v4_turbo` | stability, similarity_boost |
|
|
162
|
+
| `eleven_v4`, `eleven_v4_turbo` | stability, similarity_boost, clone_strength | 100,000 | yes | no |
|
|
152
163
|
| `eleven_v3` | stability, similarity_boost | 5,000 | yes | no |
|
|
153
164
|
| `eleven_v3_conversational` | stability, similarity_boost, use_speaker_boost | 5,000 | yes | no |
|
|
154
165
|
| `eleven_flash_v2_5`, `eleven_turbo_v2_5` | all five (incl. style, speed) | 40,000 | no | yes |
|
|
@@ -207,7 +218,7 @@ result[:voice_segments] # which voice speaks when
|
|
|
207
218
|
result[:request_id]
|
|
208
219
|
```
|
|
209
220
|
|
|
210
|
-
The
|
|
221
|
+
The gem applies a separate dialogue cap (10,000 characters for `eleven_v4`, 5,000 for `eleven_v3`); above
|
|
211
222
|
2,000 characters the gem logs a warning, since shorter dialogue requests give more reliable results.
|
|
212
223
|
|
|
213
224
|
### Audio Tags
|
|
@@ -275,10 +286,52 @@ end
|
|
|
275
286
|
audio = client.generate_music("chill acoustic guitar")
|
|
276
287
|
```
|
|
277
288
|
|
|
289
|
+
Music defaults to `music_v2_5` and sound effects to `eleven_text_to_sound_v3`. Pass
|
|
290
|
+
`model_id:` to request a particular version. A saved v1 plan with `sections` converts
|
|
291
|
+
to v2 `chunks` during generation. Use `music.migrate_plan(plan)` to inspect the
|
|
292
|
+
converted plan. Source audio intervals become reference chunks. For a source with exclusion ranges, split the reference into chunks or pass `model_id: "music_v1"` to use the original format while available.
|
|
293
|
+
|
|
294
|
+
```ruby
|
|
295
|
+
song = client.music.generate("Acoustic intro and chorus", store_for_inpainting: true)
|
|
296
|
+
edited = client.music.generate(composition_plan: { chunks: [
|
|
297
|
+
{ song_id: song.song_id, range: { start_ms: 0, end_ms: 3000 } },
|
|
298
|
+
{ text: "[Chorus]\nCome back home", duration_ms: 8000, positive_styles: ["acoustic"] }
|
|
299
|
+
] })
|
|
300
|
+
uploaded = client.music.upload("previous-song.mp3") # song_id and extracted composition plan
|
|
301
|
+
tracks = client.music.video_to_music(videos: ["clip.mp4"], add_sfx: true, separate_tracks: true)
|
|
302
|
+
tracks.save_to_file("soundtrack.zip")
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
Music and sound-effect callbacks include `credits` when the provider returns a charge
|
|
306
|
+
and `requested_duration_seconds` when the request specifies a duration. Their USD
|
|
307
|
+
`estimated_cost` is nil because per-account credit prices vary.
|
|
308
|
+
|
|
309
|
+
### Transcription
|
|
310
|
+
|
|
311
|
+
```ruby
|
|
312
|
+
transcript = client.scribe.transcribe("interview.mp3", diarize: true)
|
|
313
|
+
transcript.fetch("words").each { |word| puts [word["speaker_id"], word["text"]].join(" ") }
|
|
314
|
+
|
|
315
|
+
client.scribe.realtime(audio_format: "pcm_16000", include_timestamps: true) do |session|
|
|
316
|
+
session.send_audio(pcm_bytes, sample_rate: 16_000, commit: true)
|
|
317
|
+
loop do
|
|
318
|
+
event = session.read_event
|
|
319
|
+
break unless event
|
|
320
|
+
puts event["text"]
|
|
321
|
+
break if event["message_type"] == "committed_transcript_with_timestamps"
|
|
322
|
+
end
|
|
323
|
+
end
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
`transcribe` accepts a path, a rewindable IO, or `source_url:`. The default models
|
|
327
|
+
are `scribe_v3` for recordings and `scribe_v3_live` for live transcription. A block
|
|
328
|
+
closes the live session automatically. A caller that opens a session without a block
|
|
329
|
+
must call `close`. The live API returns provider errors as events with a `message_type`.
|
|
330
|
+
|
|
278
331
|
### Voice Management
|
|
279
332
|
|
|
280
333
|
```ruby
|
|
281
|
-
#
|
|
334
|
+
# Collect all pages from the v2 voices endpoint
|
|
282
335
|
voices = client.voices.list
|
|
283
336
|
voices.each { |v| puts v.display_name }
|
|
284
337
|
|
data/lib/eleven_rb/client.rb
CHANGED
|
@@ -86,6 +86,16 @@ module ElevenRb
|
|
|
86
86
|
@speech_to_speech ||= Resources::SpeechToSpeech.new(http_client)
|
|
87
87
|
end
|
|
88
88
|
alias sts speech_to_speech
|
|
89
|
+
alias voice_changer speech_to_speech
|
|
90
|
+
|
|
91
|
+
def audio_effects
|
|
92
|
+
@audio_effects ||= Resources::AudioEffects.new(http_client)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def speech_to_text
|
|
96
|
+
@speech_to_text ||= Resources::SpeechToText.new(http_client)
|
|
97
|
+
end
|
|
98
|
+
alias scribe speech_to_text
|
|
89
99
|
|
|
90
100
|
# Sound effects resource
|
|
91
101
|
#
|
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
require 'httparty'
|
|
4
4
|
require 'json'
|
|
5
|
+
require 'net/http/post/multipart'
|
|
6
|
+
require 'mini_mime'
|
|
5
7
|
|
|
6
8
|
module ElevenRb
|
|
7
9
|
module HTTP
|
|
@@ -53,8 +55,9 @@ module ElevenRb
|
|
|
53
55
|
# @param params [Hash] form parameters including files
|
|
54
56
|
# @param response_type [Symbol] :json or :binary
|
|
55
57
|
# @return [Hash, String] parsed JSON or binary response
|
|
56
|
-
def post_multipart(path, params, response_type: :json)
|
|
57
|
-
request(:post, path, body: params, multipart: true, response_type: response_type
|
|
58
|
+
def post_multipart(path, params, response_type: :json, with_meta: false, &block)
|
|
59
|
+
request(:post, path, body: params, multipart: true, response_type: response_type,
|
|
60
|
+
with_meta: with_meta, stream: !block.nil?, &block)
|
|
58
61
|
end
|
|
59
62
|
|
|
60
63
|
# Make a streaming POST request
|
|
@@ -72,7 +75,7 @@ module ElevenRb
|
|
|
72
75
|
def request(method, path, body: nil, params: nil, response_type: :json, multipart: false, stream: false,
|
|
73
76
|
attempt: 1, with_meta: false, &block)
|
|
74
77
|
config.validate!
|
|
75
|
-
url =
|
|
78
|
+
url = url_for(path)
|
|
76
79
|
start_time = Time.now
|
|
77
80
|
|
|
78
81
|
# Trigger before request callback
|
|
@@ -82,12 +85,11 @@ module ElevenRb
|
|
|
82
85
|
response = execute_request(method, url, body, params, multipart, stream, &block)
|
|
83
86
|
duration = ((Time.now - start_time) * 1000).round(2)
|
|
84
87
|
|
|
85
|
-
# For streaming, we've already processed the data
|
|
86
|
-
return nil if stream
|
|
87
|
-
|
|
88
88
|
# Trigger after response callback
|
|
89
89
|
config.trigger(:on_response, method: method, path: path, response: response, duration: duration)
|
|
90
90
|
|
|
91
|
+
return normalize_headers(response) if stream
|
|
92
|
+
|
|
91
93
|
build_result(response, response_type, with_meta)
|
|
92
94
|
rescue Errors::RateLimitError => e
|
|
93
95
|
config.trigger(:on_rate_limit, retry_after: e.retry_after, error: e)
|
|
@@ -106,6 +108,12 @@ module ElevenRb
|
|
|
106
108
|
end
|
|
107
109
|
end
|
|
108
110
|
|
|
111
|
+
def url_for(path)
|
|
112
|
+
base = config.base_url.delete_suffix('/')
|
|
113
|
+
base = base.sub(%r{/v\d+\z}, '') if path.match?(%r{\A/v\d+/})
|
|
114
|
+
"#{base}#{path}"
|
|
115
|
+
end
|
|
116
|
+
|
|
109
117
|
def build_result(response, response_type, with_meta)
|
|
110
118
|
# Return binary data directly
|
|
111
119
|
result = if response_type == :binary && response.success?
|
|
@@ -118,21 +126,24 @@ module ElevenRb
|
|
|
118
126
|
end
|
|
119
127
|
|
|
120
128
|
def execute_request(method, url, body, params, multipart, stream, &block)
|
|
121
|
-
options = build_options(body, params, multipart
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
129
|
+
options = build_options(body, params, multipart)
|
|
130
|
+
if stream
|
|
131
|
+
error_body = +''.b
|
|
132
|
+
response = self.class.post(url, options.merge(stream_body: true)) do |fragment|
|
|
133
|
+
if (200..299).cover?(fragment.code)
|
|
134
|
+
block.call(fragment.to_s)
|
|
135
|
+
elsif error_body.bytesize < 65_536
|
|
136
|
+
error_body << fragment.to_s.b.byteslice(0, 65_536 - error_body.bytesize)
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
handle_error_response(response, error_body) unless response.success?
|
|
140
|
+
return response
|
|
132
141
|
end
|
|
142
|
+
|
|
143
|
+
self.class.public_send(method, url, options)
|
|
133
144
|
end
|
|
134
145
|
|
|
135
|
-
def build_options(body, params, multipart
|
|
146
|
+
def build_options(body, params, multipart)
|
|
136
147
|
options = {
|
|
137
148
|
headers: headers(multipart),
|
|
138
149
|
timeout: config.timeout,
|
|
@@ -141,17 +152,17 @@ module ElevenRb
|
|
|
141
152
|
|
|
142
153
|
options[:query] = params if params && !params.empty?
|
|
143
154
|
|
|
144
|
-
if
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
options[:
|
|
155
|
+
if multipart
|
|
156
|
+
fields = body.transform_values do |value|
|
|
157
|
+
value.is_a?(Array) ? value.map { |item| multipart_value(item) } : multipart_value(value)
|
|
158
|
+
end
|
|
159
|
+
parts = fields.key?(:additional_formats) ? { additional_formats: { 'Content-Type' => 'application/json' } } : {}
|
|
160
|
+
form = Net::HTTP::Post::Multipart.new('/', fields, parts: parts)
|
|
161
|
+
options[:body_stream] = form.body_stream
|
|
162
|
+
options[:headers]['Content-Type'] = form['Content-Type']
|
|
163
|
+
options[:headers]['Content-Length'] = form['Content-Length']
|
|
164
|
+
elsif body && !body.empty?
|
|
165
|
+
options[:body] = body.to_json
|
|
155
166
|
end
|
|
156
167
|
|
|
157
168
|
options
|
|
@@ -166,22 +177,14 @@ module ElevenRb
|
|
|
166
177
|
h
|
|
167
178
|
end
|
|
168
179
|
|
|
169
|
-
def
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
params.each do |key, value|
|
|
173
|
-
case key.to_sym
|
|
174
|
-
when :files
|
|
175
|
-
# Handle file array
|
|
176
|
-
Array(value).each_with_index do |file, index|
|
|
177
|
-
body["files[#{index}]"] = file
|
|
178
|
-
end
|
|
179
|
-
else
|
|
180
|
-
body[key.to_s] = value
|
|
181
|
-
end
|
|
182
|
-
end
|
|
180
|
+
def multipart_value(value)
|
|
181
|
+
return value unless value.respond_to?(:read)
|
|
183
182
|
|
|
184
|
-
|
|
183
|
+
value.rewind
|
|
184
|
+
path = value.respond_to?(:path) && value.path ? File.basename(value.path) : 'upload.bin'
|
|
185
|
+
filename = path.gsub(/["\r\n]/, '_')
|
|
186
|
+
type = MiniMime.lookup_by_filename(filename)&.content_type || 'application/octet-stream'
|
|
187
|
+
Multipart::Post::UploadIO.new(value, type, filename)
|
|
185
188
|
end
|
|
186
189
|
|
|
187
190
|
def handle_response(response)
|
|
@@ -190,9 +193,10 @@ module ElevenRb
|
|
|
190
193
|
handle_error_response(response)
|
|
191
194
|
end
|
|
192
195
|
|
|
193
|
-
def handle_error_response(response)
|
|
196
|
+
def handle_error_response(response, raw_body = response.body)
|
|
194
197
|
status = response.code
|
|
195
|
-
body =
|
|
198
|
+
body = parse_json(raw_body)
|
|
199
|
+
body = { 'message' => body.to_s } unless body.is_a?(Hash)
|
|
196
200
|
message = extract_error_message(body)
|
|
197
201
|
|
|
198
202
|
error_class = case status
|
|
@@ -266,10 +270,6 @@ module ElevenRb
|
|
|
266
270
|
{}
|
|
267
271
|
end
|
|
268
272
|
|
|
269
|
-
def parse_error_body(body)
|
|
270
|
-
parse_json(body)
|
|
271
|
-
end
|
|
272
|
-
|
|
273
273
|
def extract_error_message(body)
|
|
274
274
|
detail = body['detail']
|
|
275
275
|
case detail
|
|
@@ -283,16 +283,12 @@ module ElevenRb
|
|
|
283
283
|
end
|
|
284
284
|
|
|
285
285
|
def sanitize_body_for_logging(body)
|
|
286
|
-
return nil if body.nil?
|
|
287
|
-
|
|
288
286
|
# Remove any sensitive data or large binary content
|
|
289
|
-
if body.is_a?(
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
body
|
|
295
|
-
end
|
|
287
|
+
return body.map { |item| sanitize_body_for_logging(item) } if body.is_a?(Array)
|
|
288
|
+
return body.transform_values { |item| sanitize_body_for_logging(item) } if body.is_a?(Hash)
|
|
289
|
+
return '[FILE]' if body.respond_to?(:read)
|
|
290
|
+
|
|
291
|
+
body
|
|
296
292
|
end
|
|
297
293
|
end
|
|
298
294
|
end
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'openssl'
|
|
4
|
+
require 'socket'
|
|
5
|
+
require 'timeout'
|
|
6
|
+
require 'websocket/driver'
|
|
7
|
+
|
|
8
|
+
module ElevenRb
|
|
9
|
+
module HTTP
|
|
10
|
+
# A synchronous full-duplex Scribe connection. Read transcript events with
|
|
11
|
+
# #read_event and write PCM audio with #send_audio from a separate producer.
|
|
12
|
+
class RealtimeTranscription
|
|
13
|
+
attr_reader :url
|
|
14
|
+
|
|
15
|
+
def initialize(config, query)
|
|
16
|
+
uri = URI("#{config.base_url.delete_suffix('/')}/speech-to-text/realtime")
|
|
17
|
+
@timeout = config.timeout
|
|
18
|
+
@write_lock = Mutex.new
|
|
19
|
+
@events = []
|
|
20
|
+
@socket = Socket.tcp(uri.host, uri.port, connect_timeout: config.open_timeout)
|
|
21
|
+
Timeout.timeout(config.open_timeout, Errors::TimeoutError) do
|
|
22
|
+
start_tls(uri.host) if uri.scheme == 'https'
|
|
23
|
+
uri.query = URI.encode_www_form(query.compact)
|
|
24
|
+
@url = uri.to_s.sub(/\Ahttp/, 'ws')
|
|
25
|
+
start_driver(config.api_key)
|
|
26
|
+
read_frame until @driver.state == :open
|
|
27
|
+
end
|
|
28
|
+
rescue StandardError
|
|
29
|
+
@socket&.close
|
|
30
|
+
raise
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Transport method used by websocket-driver for data and control frames.
|
|
34
|
+
def write(bytes)
|
|
35
|
+
@write_lock.synchronize { @socket.write(bytes) }
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def send_audio(bytes, sample_rate: 16_000, commit: false, previous_text: nil)
|
|
39
|
+
frame = { 'message_type' => 'input_audio_chunk', 'audio_base_64' => Base64.strict_encode64(bytes),
|
|
40
|
+
'sample_rate' => sample_rate, 'commit' => commit, 'previous_text' => previous_text }.compact
|
|
41
|
+
raise IOError, 'Scribe session is closed' unless @driver.text(JSON.generate(frame))
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def read_event
|
|
45
|
+
while @events.empty?
|
|
46
|
+
return if @driver.state == :closed
|
|
47
|
+
|
|
48
|
+
Timeout.timeout(@timeout, Errors::TimeoutError) { read_frame }
|
|
49
|
+
end
|
|
50
|
+
@events.shift
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def close
|
|
54
|
+
return if @socket.closed?
|
|
55
|
+
|
|
56
|
+
@driver.close
|
|
57
|
+
ensure
|
|
58
|
+
@socket.close unless @socket.closed?
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
private
|
|
62
|
+
|
|
63
|
+
def start_driver(api_key)
|
|
64
|
+
@driver = WebSocket::Driver.client(self)
|
|
65
|
+
@driver.set_header('xi-api-key', api_key)
|
|
66
|
+
@driver.on(:message) { |event| @events << JSON.parse(event.data) }
|
|
67
|
+
@driver.on(:error) { |error| raise Errors::ConnectionError, error.message }
|
|
68
|
+
@driver.start
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def start_tls(host)
|
|
72
|
+
context = OpenSSL::SSL::SSLContext.new
|
|
73
|
+
context.set_params
|
|
74
|
+
ssl = OpenSSL::SSL::SSLSocket.new(@socket, context)
|
|
75
|
+
ssl.sync_close = true
|
|
76
|
+
ssl.hostname = host
|
|
77
|
+
@socket = ssl
|
|
78
|
+
ssl.connect
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def read_frame
|
|
82
|
+
@driver.parse(@socket.readpartial(16_384))
|
|
83
|
+
rescue EOFError
|
|
84
|
+
raise Errors::ConnectionError, 'Scribe connection closed without a WebSocket close frame'
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
@@ -11,7 +11,7 @@ module ElevenRb
|
|
|
11
11
|
#
|
|
12
12
|
# @example
|
|
13
13
|
# ElevenRb::ModelCapabilities.supported_voice_settings('eleven_v4')
|
|
14
|
-
# # => [:stability, :similarity_boost]
|
|
14
|
+
# # => [:stability, :similarity_boost, :clone_strength]
|
|
15
15
|
# ElevenRb::ModelCapabilities.max_text_length('eleven_flash_v2_5') # => 40_000
|
|
16
16
|
# ElevenRb::ModelCapabilities.supports?('eleven_v4', :audio_tags) # => true
|
|
17
17
|
module ModelCapabilities
|
|
@@ -36,7 +36,7 @@ module ElevenRb
|
|
|
36
36
|
end
|
|
37
37
|
private_class_method :build
|
|
38
38
|
|
|
39
|
-
V4 = build(voice_settings: %i[stability similarity_boost], max_text_length:
|
|
39
|
+
V4 = build(voice_settings: %i[stability similarity_boost clone_strength], max_text_length: 100_000,
|
|
40
40
|
ssml_break: false, audio_tags: true, continuity: true)
|
|
41
41
|
V3_CONVERSATIONAL = build(voice_settings: %i[stability similarity_boost use_speaker_boost], max_text_length: 5_000,
|
|
42
42
|
ssml_break: false, audio_tags: true, continuity: true)
|
|
@@ -7,7 +7,7 @@ module ElevenRb
|
|
|
7
7
|
# Represents generated audio data
|
|
8
8
|
class Audio
|
|
9
9
|
attr_reader :data, :format, :voice_id, :text, :model_id,
|
|
10
|
-
:request_id, :character_cost, :dropped_settings
|
|
10
|
+
:request_id, :character_cost, :dropped_settings, :song_id
|
|
11
11
|
|
|
12
12
|
# Initialize audio object
|
|
13
13
|
#
|
|
@@ -21,7 +21,8 @@ module ElevenRb
|
|
|
21
21
|
# @param character_cost [Integer, nil] the API's `character-cost` response header
|
|
22
22
|
# @param dropped_settings [Array<Symbol>] voice settings the gem dropped because the model ignores them
|
|
23
23
|
def initialize(data:, format:, voice_id:, text:, model_id: nil, request_id: nil, character_cost: nil,
|
|
24
|
-
dropped_settings: [])
|
|
24
|
+
dropped_settings: [], song_id: nil)
|
|
25
|
+
@song_id = song_id
|
|
25
26
|
@data = data
|
|
26
27
|
@format = format
|
|
27
28
|
@voice_id = voice_id
|
|
@@ -67,6 +68,7 @@ module ElevenRb
|
|
|
67
68
|
# @return [String]
|
|
68
69
|
def content_type
|
|
69
70
|
case format
|
|
71
|
+
when 'zip' then 'application/zip'
|
|
70
72
|
when /mp3/
|
|
71
73
|
'audio/mpeg'
|
|
72
74
|
when /pcm/
|
|
@@ -87,6 +89,7 @@ module ElevenRb
|
|
|
87
89
|
# @return [String]
|
|
88
90
|
def extension
|
|
89
91
|
case format
|
|
92
|
+
when 'zip' then 'zip'
|
|
90
93
|
when /mp3/
|
|
91
94
|
'mp3'
|
|
92
95
|
when /pcm/
|
|
@@ -106,7 +109,7 @@ module ElevenRb
|
|
|
106
109
|
#
|
|
107
110
|
# @return [Integer]
|
|
108
111
|
def character_count
|
|
109
|
-
text
|
|
112
|
+
text&.length || 0
|
|
110
113
|
end
|
|
111
114
|
|
|
112
115
|
# Check if data is present
|
|
@@ -6,6 +6,7 @@ module ElevenRb
|
|
|
6
6
|
class VoiceSettings < Base
|
|
7
7
|
attribute :stability
|
|
8
8
|
attribute :similarity_boost
|
|
9
|
+
attribute :clone_strength
|
|
9
10
|
attribute :style
|
|
10
11
|
attribute :use_speaker_boost, type: :boolean
|
|
11
12
|
|
|
@@ -28,7 +29,7 @@ module ElevenRb
|
|
|
28
29
|
# Voice-setting keys the capability table knows about. Only these can be
|
|
29
30
|
# dropped for a model; any other key is passed through untouched so a
|
|
30
31
|
# future API field is never swallowed.
|
|
31
|
-
KNOWN_KEYS = ModelCapabilities::ALL_VOICE_SETTINGS
|
|
32
|
+
KNOWN_KEYS = (ModelCapabilities::ALL_VOICE_SETTINGS + [:clone_strength]).freeze
|
|
32
33
|
|
|
33
34
|
# Build the voice_settings hash a model actually honours
|
|
34
35
|
#
|
|
@@ -66,6 +67,7 @@ module ElevenRb
|
|
|
66
67
|
stability: stability,
|
|
67
68
|
similarity_boost: similarity_boost,
|
|
68
69
|
style: style,
|
|
70
|
+
clone_strength: clone_strength,
|
|
69
71
|
use_speaker_boost: use_speaker_boost
|
|
70
72
|
}.compact
|
|
71
73
|
end
|
|
@@ -78,6 +78,22 @@ module ElevenRb
|
|
|
78
78
|
http_client.post_stream(path, body, &block)
|
|
79
79
|
end
|
|
80
80
|
|
|
81
|
+
# Each JSON line is a complete audio/alignment event. HTTP chunks may split any line.
|
|
82
|
+
def stream_timestamps(path, body, &block)
|
|
83
|
+
buffer = +''.b
|
|
84
|
+
headers = post_stream(path, body) do |chunk|
|
|
85
|
+
buffer << chunk.b
|
|
86
|
+
while (ending = buffer.index("\n"))
|
|
87
|
+
line = buffer.slice!(0, ending + 1)
|
|
88
|
+
block.call(JSON.parse(line)) unless line.strip.empty?
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
block.call(JSON.parse(buffer)) unless buffer.strip.empty?
|
|
92
|
+
headers
|
|
93
|
+
rescue JSON::ParserError => e
|
|
94
|
+
raise Errors::APIError, "Invalid timestamp stream: #{e.message}"
|
|
95
|
+
end
|
|
96
|
+
|
|
81
97
|
# Make a multipart POST request
|
|
82
98
|
#
|
|
83
99
|
# @param path [String]
|
|
@@ -88,6 +104,34 @@ module ElevenRb
|
|
|
88
104
|
http_client.post_multipart(path, params, response_type: response_type)
|
|
89
105
|
end
|
|
90
106
|
|
|
107
|
+
def with_uploads(inputs)
|
|
108
|
+
opened = []
|
|
109
|
+
files = inputs.map do |input|
|
|
110
|
+
next input if input.respond_to?(:read)
|
|
111
|
+
|
|
112
|
+
raise Errors::ValidationError, "File not found: #{input}" unless File.file?(input)
|
|
113
|
+
|
|
114
|
+
file = File.open(input, 'rb')
|
|
115
|
+
opened << file
|
|
116
|
+
file
|
|
117
|
+
end
|
|
118
|
+
yield files
|
|
119
|
+
ensure
|
|
120
|
+
opened.each(&:close)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def notify_media_usage(audio, text, model_id, seconds, headers)
|
|
124
|
+
# The provider's credit charge depends on the account and endpoint.
|
|
125
|
+
# Preserve the old callback keys and expose the provider's units separately.
|
|
126
|
+
cost_info = {
|
|
127
|
+
character_count: text&.length || 0, voice_id: nil, model_id: model_id,
|
|
128
|
+
estimated_cost: nil, cost_per_character: nil, requested_duration_seconds: seconds,
|
|
129
|
+
credits: Integer(headers['character-cost'], exception: false)
|
|
130
|
+
}
|
|
131
|
+
http_client.config.trigger(:on_audio_generated, audio: audio, voice_id: nil, text: text,
|
|
132
|
+
cost_info: cost_info, request_id: headers['request-id'])
|
|
133
|
+
end
|
|
134
|
+
|
|
91
135
|
# Validate presence of a value
|
|
92
136
|
#
|
|
93
137
|
# @param value [Object]
|