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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +42 -0
- data/README.md +146 -12
- data/lib/eleven_rb/callbacks.rb +25 -1
- data/lib/eleven_rb/client.rb +10 -0
- data/lib/eleven_rb/configuration.rb +7 -3
- data/lib/eleven_rb/http/client.rb +86 -70
- data/lib/eleven_rb/http/realtime_transcription.rb +88 -0
- data/lib/eleven_rb/model_capabilities.rb +103 -0
- data/lib/eleven_rb/objects/audio.rb +17 -5
- data/lib/eleven_rb/objects/cost_info.rb +3 -0
- data/lib/eleven_rb/objects/model.rb +10 -0
- data/lib/eleven_rb/objects/voice_settings.rb +35 -0
- data/lib/eleven_rb/resources/audio_effects.rb +11 -0
- data/lib/eleven_rb/resources/base.rb +62 -0
- data/lib/eleven_rb/resources/models.rb +32 -6
- 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 +143 -30
- data/lib/eleven_rb/resources/text_to_speech.rb +182 -57
- data/lib/eleven_rb/resources/voices.rb +16 -3
- data/lib/eleven_rb/version.rb +1 -1
- data/lib/eleven_rb.rb +4 -0
- metadata +47 -1
|
@@ -2,151 +2,120 @@
|
|
|
2
2
|
|
|
3
3
|
module ElevenRb
|
|
4
4
|
module Resources
|
|
5
|
-
# Music generation resource
|
|
6
|
-
#
|
|
7
|
-
# @example Generate music from a prompt
|
|
8
|
-
# audio = client.music.generate("upbeat jazz piano solo")
|
|
9
|
-
# audio.save_to_file("jazz.mp3")
|
|
10
|
-
#
|
|
11
|
-
# @example Generate music from a composition plan
|
|
12
|
-
# plan = client.music.create_plan("epic orchestral battle theme", music_length_ms: 30_000)
|
|
13
|
-
# audio = client.music.generate(composition_plan: plan)
|
|
14
|
-
# audio.save_to_file("battle.mp3")
|
|
15
|
-
#
|
|
16
|
-
# @example Stream music
|
|
17
|
-
# File.open("song.mp3", "wb") do |file|
|
|
18
|
-
# client.music.stream("lo-fi hip hop beats") { |chunk| file.write(chunk) }
|
|
19
|
-
# end
|
|
20
5
|
class Music < Base
|
|
21
|
-
DEFAULT_MODEL = '
|
|
22
|
-
|
|
23
|
-
# Generate music from a text prompt or composition plan
|
|
24
|
-
#
|
|
25
|
-
# @param prompt [String, nil] text description of the music to generate (mutually exclusive with composition_plan)
|
|
26
|
-
# @param composition_plan [Hash, nil] structured composition plan (mutually exclusive with prompt)
|
|
27
|
-
# @param music_length_ms [Integer, nil] duration in milliseconds (3000-600000, only with prompt)
|
|
28
|
-
# @param model_id [String] the model to use (default: music_v1)
|
|
29
|
-
# @param force_instrumental [Boolean, nil] whether to force instrumental output (only with prompt)
|
|
30
|
-
# @param respect_sections_durations [Boolean, nil] whether to respect section durations (only with compose)
|
|
31
|
-
# @param output_format [String] audio output format
|
|
32
|
-
# @return [Objects::Audio]
|
|
33
|
-
def generate(prompt = nil, composition_plan: nil, music_length_ms: nil,
|
|
34
|
-
model_id: DEFAULT_MODEL, force_instrumental: nil,
|
|
35
|
-
respect_sections_durations: nil, output_format: 'mp3_44100_128')
|
|
36
|
-
validate_prompt_or_plan!(prompt, composition_plan)
|
|
37
|
-
|
|
38
|
-
body = build_body(
|
|
39
|
-
prompt: prompt,
|
|
40
|
-
composition_plan: composition_plan,
|
|
41
|
-
music_length_ms: music_length_ms,
|
|
42
|
-
model_id: model_id,
|
|
43
|
-
force_instrumental: force_instrumental,
|
|
44
|
-
respect_sections_durations: respect_sections_durations
|
|
45
|
-
)
|
|
46
|
-
|
|
47
|
-
path = "/music?output_format=#{output_format}"
|
|
48
|
-
response = post_binary(path, body)
|
|
49
|
-
|
|
50
|
-
audio = Objects::Audio.new(
|
|
51
|
-
data: response,
|
|
52
|
-
format: output_format,
|
|
53
|
-
voice_id: nil,
|
|
54
|
-
text: prompt,
|
|
55
|
-
model_id: model_id
|
|
56
|
-
)
|
|
57
|
-
|
|
58
|
-
cost_info = Objects::CostInfo.new(text: prompt || '', voice_id: 'music', model_id: model_id)
|
|
59
|
-
http_client.config.trigger(
|
|
60
|
-
:on_audio_generated,
|
|
61
|
-
audio: audio,
|
|
62
|
-
voice_id: nil,
|
|
63
|
-
text: prompt,
|
|
64
|
-
cost_info: cost_info.to_h
|
|
65
|
-
)
|
|
6
|
+
DEFAULT_MODEL = 'music_v2_5'
|
|
66
7
|
|
|
67
|
-
|
|
8
|
+
def generate(prompt = nil, composition_plan: nil, music_length_ms: nil,
|
|
9
|
+
model_id: DEFAULT_MODEL, force_instrumental: nil, seed: nil,
|
|
10
|
+
respect_sections_durations: nil, store_for_inpainting: nil, output_format: 'mp3_44100_128')
|
|
11
|
+
body = compose_body(prompt, composition_plan, model_id, music_length_ms, force_instrumental,
|
|
12
|
+
seed: seed, respect_sections_durations: respect_sections_durations,
|
|
13
|
+
store_for_inpainting: store_for_inpainting)
|
|
14
|
+
response = post_binary_with_meta("/music?output_format=#{output_format}", body)
|
|
15
|
+
music_result(response, output_format, prompt, model_id, duration_seconds(body))
|
|
68
16
|
end
|
|
69
17
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
# @param output_format [String] audio output format
|
|
78
|
-
# @yield [String] each chunk of audio data
|
|
79
|
-
# @return [void]
|
|
80
|
-
def stream(prompt = nil, composition_plan: nil, music_length_ms: nil,
|
|
81
|
-
model_id: DEFAULT_MODEL, force_instrumental: nil,
|
|
82
|
-
output_format: 'mp3_44100_128', &block)
|
|
83
|
-
validate_prompt_or_plan!(prompt, composition_plan)
|
|
84
|
-
raise ArgumentError, 'Block required for streaming' unless block_given?
|
|
85
|
-
|
|
86
|
-
body = build_body(
|
|
87
|
-
prompt: prompt,
|
|
88
|
-
composition_plan: composition_plan,
|
|
89
|
-
music_length_ms: music_length_ms,
|
|
90
|
-
model_id: model_id,
|
|
91
|
-
force_instrumental: force_instrumental
|
|
92
|
-
)
|
|
93
|
-
|
|
94
|
-
path = "/music/stream?output_format=#{output_format}"
|
|
95
|
-
post_stream(path, body, &block)
|
|
96
|
-
|
|
97
|
-
cost_info = Objects::CostInfo.new(text: prompt || '', voice_id: 'music', model_id: model_id)
|
|
98
|
-
http_client.config.trigger(
|
|
99
|
-
:on_audio_generated,
|
|
100
|
-
audio: nil,
|
|
101
|
-
voice_id: nil,
|
|
102
|
-
text: prompt,
|
|
103
|
-
cost_info: cost_info.to_h
|
|
104
|
-
)
|
|
18
|
+
def stream(prompt = nil, composition_plan: nil, music_length_ms: nil, model_id: DEFAULT_MODEL,
|
|
19
|
+
force_instrumental: nil, output_format: 'mp3_44100_128', seed: nil, &block)
|
|
20
|
+
body = compose_body(prompt, composition_plan, model_id, music_length_ms, force_instrumental, seed: seed)
|
|
21
|
+
raise ArgumentError, 'Block required for streaming' unless block
|
|
22
|
+
|
|
23
|
+
headers = post_stream("/music/stream?output_format=#{output_format}", body, &block)
|
|
24
|
+
notify_media_usage(nil, prompt, model_id, duration_seconds(body), headers)
|
|
105
25
|
end
|
|
106
26
|
|
|
107
|
-
# Create a composition plan from a text prompt (free, no credits used)
|
|
108
|
-
#
|
|
109
|
-
# @param prompt [String] text description of the music
|
|
110
|
-
# @param music_length_ms [Integer, nil] desired duration in milliseconds
|
|
111
|
-
# @param model_id [String] the model to use
|
|
112
|
-
# @return [Hash] structured composition plan
|
|
113
27
|
def create_plan(prompt, music_length_ms: nil, model_id: DEFAULT_MODEL)
|
|
114
28
|
validate_presence!(prompt, 'prompt')
|
|
29
|
+
post('/music/plan', { prompt: prompt, music_length_ms: music_length_ms, model_id: model_id }.compact)
|
|
30
|
+
end
|
|
115
31
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
32
|
+
# Convert a saved v1 sections plan to v2 generation chunks. The global styles
|
|
33
|
+
# apply to every section, and each section keeps its duration, title and lyrics.
|
|
34
|
+
def migrate_plan(plan)
|
|
35
|
+
plan = plan.transform_keys(&:to_s)
|
|
36
|
+
return plan if plan.key?('chunks')
|
|
37
|
+
|
|
38
|
+
chunks = plan.fetch('sections').map do |section|
|
|
39
|
+
section = section.transform_keys(&:to_s)
|
|
40
|
+
next reference_chunk(section['source_from']) if section['source_from']
|
|
41
|
+
|
|
42
|
+
title = section['section_name']
|
|
43
|
+
{
|
|
44
|
+
'text' => [title && "[#{title}]", *section['lines']].compact.join("\n"),
|
|
45
|
+
'duration_ms' => section.fetch('duration_ms'),
|
|
46
|
+
'positive_styles' => (Array(plan['positive_global_styles']) + Array(section['positive_local_styles'])).uniq,
|
|
47
|
+
'negative_styles' => (Array(plan['negative_global_styles']) + Array(section['negative_local_styles'])).uniq
|
|
48
|
+
}
|
|
49
|
+
end
|
|
50
|
+
{ 'chunks' => chunks }
|
|
51
|
+
end
|
|
121
52
|
|
|
122
|
-
|
|
53
|
+
# Returns the reusable song_id and optionally the extracted composition plan.
|
|
54
|
+
def upload(file, extract_composition_plan: DEFAULT_MODEL)
|
|
55
|
+
with_uploads([file]) do |files|
|
|
56
|
+
post_multipart('/music/upload', { file: files.first, extract_composition_plan: extract_composition_plan })
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def video_to_music(videos:, description: nil, tags: nil, model_id: DEFAULT_MODEL, add_sfx: nil,
|
|
61
|
+
separate_tracks: false, use_audio_as_reference: nil, output_format: 'mp3_44100_128')
|
|
62
|
+
raise Errors::ValidationError, 'Supply one to ten video files' unless videos.is_a?(Array) && videos.length.between?(1, 10)
|
|
63
|
+
|
|
64
|
+
with_uploads(videos) do |files|
|
|
65
|
+
fields = { videos: files, description: description, tags: tags, model_id: model_id,
|
|
66
|
+
add_sfx: add_sfx, separate_tracks: separate_tracks, use_audio_as_reference: use_audio_as_reference }
|
|
67
|
+
path = "/music/video-to-music?output_format=#{output_format}"
|
|
68
|
+
response = http_client.post_multipart(path, fields.compact, response_type: :binary, with_meta: true)
|
|
69
|
+
music_result(response, separate_tracks ? 'zip' : output_format, description, model_id, nil)
|
|
70
|
+
end
|
|
123
71
|
end
|
|
124
72
|
|
|
125
73
|
private
|
|
126
74
|
|
|
127
|
-
def
|
|
128
|
-
|
|
75
|
+
def reference_chunk(source)
|
|
76
|
+
source = source.transform_keys(&:to_s)
|
|
77
|
+
unless Array(source['negative_ranges']).empty?
|
|
78
|
+
raise Errors::ValidationError, 'Cannot migrate source_from.negative_ranges. Use music_v1 or split the reference into chunks.'
|
|
79
|
+
end
|
|
129
80
|
|
|
130
|
-
|
|
81
|
+
{ 'song_id' => source.fetch('song_id'), 'range' => source.fetch('range') }
|
|
82
|
+
end
|
|
131
83
|
|
|
132
|
-
|
|
84
|
+
def compose_body(prompt, plan, model_id, duration, instrumental, **options)
|
|
85
|
+
raise Errors::ValidationError, 'Either prompt or composition_plan must be provided' unless prompt || plan
|
|
86
|
+
raise Errors::ValidationError, 'prompt and composition_plan are mutually exclusive' if prompt && plan
|
|
87
|
+
|
|
88
|
+
plan = migrate_plan(plan) if plan && model_id != 'music_v1' && (plan.key?('sections') || plan.key?(:sections))
|
|
89
|
+
{ prompt: prompt, composition_plan: plan, model_id: model_id, music_length_ms: prompt && duration,
|
|
90
|
+
force_instrumental: prompt && instrumental }.merge(options).compact
|
|
133
91
|
end
|
|
134
92
|
|
|
135
|
-
def
|
|
136
|
-
|
|
137
|
-
body = { model_id: model_id }
|
|
93
|
+
def duration_seconds(body)
|
|
94
|
+
return body[:music_length_ms] / 1000.0 if body[:music_length_ms]
|
|
138
95
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
body[:force_instrumental] = force_instrumental unless force_instrumental.nil?
|
|
143
|
-
else
|
|
144
|
-
body[:composition_plan] = composition_plan
|
|
145
|
-
end
|
|
96
|
+
plan = body[:composition_plan]&.transform_keys(&:to_s)
|
|
97
|
+
chunks = plan && (plan['chunks'] || plan['sections'])
|
|
98
|
+
return if chunks.nil? || chunks.empty?
|
|
146
99
|
|
|
147
|
-
|
|
100
|
+
durations = chunks.map { |chunk| chunk_duration_ms(chunk) }
|
|
101
|
+
durations.sum / 1000.0 if durations.all?
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def chunk_duration_ms(chunk)
|
|
105
|
+
fields = chunk.transform_keys(&:to_s)
|
|
106
|
+
return fields['duration_ms'] if fields['duration_ms']
|
|
148
107
|
|
|
149
|
-
|
|
108
|
+
range = fields['range']&.transform_keys(&:to_s)
|
|
109
|
+
range.fetch('end_ms') - range.fetch('start_ms') if range
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def music_result(response, format, text, model_id, seconds)
|
|
113
|
+
headers = response[:headers]
|
|
114
|
+
audio = Objects::Audio.new(data: response[:body], format: format, voice_id: nil, text: text, model_id: model_id,
|
|
115
|
+
song_id: headers['song-id'], request_id: headers['request-id'],
|
|
116
|
+
character_cost: Integer(headers['character-cost'], exception: false))
|
|
117
|
+
notify_media_usage(audio, text, model_id, seconds, headers)
|
|
118
|
+
audio
|
|
150
119
|
end
|
|
151
120
|
end
|
|
152
121
|
end
|
|
@@ -15,12 +15,12 @@ module ElevenRb
|
|
|
15
15
|
# prompt_influence: 0.5
|
|
16
16
|
# )
|
|
17
17
|
class SoundEffects < Base
|
|
18
|
-
DEFAULT_MODEL = '
|
|
18
|
+
DEFAULT_MODEL = 'eleven_text_to_sound_v3'
|
|
19
19
|
|
|
20
20
|
# Generate a sound effect from a text description
|
|
21
21
|
#
|
|
22
22
|
# @param text [String] description of the sound effect to generate
|
|
23
|
-
# @param model_id [String] the model to use (default:
|
|
23
|
+
# @param model_id [String] the model to use (default: eleven_text_to_sound_v3)
|
|
24
24
|
# @param duration_seconds [Float, nil] desired duration in seconds (optional, API decides if omitted)
|
|
25
25
|
# @param prompt_influence [Float, nil] how closely to follow the prompt (0.0-1.0)
|
|
26
26
|
# @param loop [Boolean, nil] whether to generate a loopable sound effect
|
|
@@ -39,25 +39,20 @@ module ElevenRb
|
|
|
39
39
|
body[:loop] = loop unless loop.nil?
|
|
40
40
|
|
|
41
41
|
path = "/sound-generation?output_format=#{output_format}"
|
|
42
|
-
response =
|
|
42
|
+
response = post_binary_with_meta(path, body)
|
|
43
|
+
headers = response[:headers]
|
|
43
44
|
|
|
44
45
|
audio = Objects::Audio.new(
|
|
45
|
-
data: response,
|
|
46
|
+
data: response[:body],
|
|
47
|
+
request_id: headers['request-id'],
|
|
48
|
+
character_cost: Integer(headers['character-cost'], exception: false),
|
|
46
49
|
format: output_format,
|
|
47
50
|
voice_id: nil,
|
|
48
51
|
text: text,
|
|
49
52
|
model_id: model_id
|
|
50
53
|
)
|
|
51
54
|
|
|
52
|
-
|
|
53
|
-
http_client.config.trigger(
|
|
54
|
-
:on_audio_generated,
|
|
55
|
-
audio: audio,
|
|
56
|
-
voice_id: nil,
|
|
57
|
-
text: text,
|
|
58
|
-
cost_info: cost_info.to_h
|
|
59
|
-
)
|
|
60
|
-
|
|
55
|
+
notify_media_usage(audio, text, model_id, duration_seconds, headers)
|
|
61
56
|
audio
|
|
62
57
|
end
|
|
63
58
|
end
|
|
@@ -35,15 +35,8 @@ module ElevenRb
|
|
|
35
35
|
|
|
36
36
|
file = prepare_upload(audio_input)
|
|
37
37
|
|
|
38
|
-
params =
|
|
39
|
-
|
|
40
|
-
model_id: model_id
|
|
41
|
-
}
|
|
42
|
-
params[:voice_settings] = voice_settings.to_json if voice_settings
|
|
43
|
-
params[:remove_background_noise] = remove_background_noise.to_s
|
|
44
|
-
params[:seed] = seed.to_s if seed
|
|
45
|
-
|
|
46
|
-
path = "/speech-to-speech/#{voice_id}?output_format=#{output_format}"
|
|
38
|
+
params = conversion_fields(file, model_id, voice_settings, remove_background_noise, seed)
|
|
39
|
+
path = "/voice-changer/#{voice_id}?output_format=#{output_format}"
|
|
47
40
|
response = post_multipart(path, params, response_type: :binary)
|
|
48
41
|
|
|
49
42
|
audio = Objects::Audio.new(
|
|
@@ -60,8 +53,27 @@ module ElevenRb
|
|
|
60
53
|
file&.close if file.respond_to?(:close) && audio_input.is_a?(String)
|
|
61
54
|
end
|
|
62
55
|
|
|
56
|
+
def stream(audio_input, voice_id:, model_id: DEFAULT_MODEL, voice_settings: nil,
|
|
57
|
+
remove_background_noise: false, output_format: 'mp3_44100_128', seed: nil, &block)
|
|
58
|
+
raise ArgumentError, 'Block required for streaming' unless block
|
|
59
|
+
|
|
60
|
+
validate_presence!(voice_id, 'voice_id')
|
|
61
|
+
file = prepare_upload(audio_input)
|
|
62
|
+
fields = conversion_fields(file, model_id, voice_settings, remove_background_noise, seed)
|
|
63
|
+
path = "/voice-changer/#{voice_id}/stream?output_format=#{output_format}"
|
|
64
|
+
http_client.post_multipart(path, fields, &block)
|
|
65
|
+
notify_audio_generated(nil, voice_id: voice_id, model_id: model_id)
|
|
66
|
+
ensure
|
|
67
|
+
file&.close if audio_input.is_a?(String)
|
|
68
|
+
end
|
|
69
|
+
|
|
63
70
|
private
|
|
64
71
|
|
|
72
|
+
def conversion_fields(file, model_id, voice_settings, remove_background_noise, seed)
|
|
73
|
+
{ audio: file, model_id: model_id, voice_settings: voice_settings&.to_json,
|
|
74
|
+
remove_background_noise: remove_background_noise.to_s, seed: seed }.compact
|
|
75
|
+
end
|
|
76
|
+
|
|
65
77
|
def notify_audio_generated(audio, voice_id:, model_id:)
|
|
66
78
|
cost_info = Objects::CostInfo.new(text: '[sts]', voice_id: voice_id, model_id: model_id)
|
|
67
79
|
http_client.config.trigger(
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../http/realtime_transcription'
|
|
4
|
+
|
|
5
|
+
module ElevenRb
|
|
6
|
+
module Resources
|
|
7
|
+
class SpeechToText < Base
|
|
8
|
+
def transcribe(file = nil, source_url: nil, model_id: 'scribe_v3', enable_logging: nil, **options)
|
|
9
|
+
raise Errors::ValidationError, 'Provide exactly one of file or source_url' if file.nil? == source_url.nil?
|
|
10
|
+
|
|
11
|
+
params = options.merge(model_id: model_id, source_url: source_url).compact
|
|
12
|
+
%i[additional_formats webhook_metadata entity_detection entity_redaction].each do |name|
|
|
13
|
+
params[name] = JSON.generate(params[name]) if params.key?(name)
|
|
14
|
+
end
|
|
15
|
+
path = '/speech-to-text'
|
|
16
|
+
path += "?#{URI.encode_www_form(enable_logging: enable_logging)}" unless enable_logging.nil?
|
|
17
|
+
return post_multipart(path, params) if source_url
|
|
18
|
+
|
|
19
|
+
with_uploads([file]) { |files| post_multipart(path, params.merge(file: files.first)) }
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# A session can also be managed explicitly with #close.
|
|
23
|
+
def realtime(model_id: 'scribe_v3_live', **options)
|
|
24
|
+
config = http_client.config
|
|
25
|
+
config.validate!
|
|
26
|
+
session = HTTP::RealtimeTranscription.new(config, options.merge(model_id: model_id))
|
|
27
|
+
return session unless block_given?
|
|
28
|
+
|
|
29
|
+
begin
|
|
30
|
+
yield session
|
|
31
|
+
ensure
|
|
32
|
+
session.close
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -10,69 +10,172 @@ module ElevenRb
|
|
|
10
10
|
# { text: "[laughs] Thanks!", voice_id: "voice_xyz" }
|
|
11
11
|
# ])
|
|
12
12
|
# audio.save_to_file("dialogue.mp3")
|
|
13
|
+
#
|
|
14
|
+
# @example Dialogue with timestamps and per-speaker segments
|
|
15
|
+
# result = client.dialogue.generate_with_timestamps(inputs, seed: 7)
|
|
16
|
+
# result[:voice_segments] # => [{ "voice_id" => ..., "start_time_seconds" => ... }, ...]
|
|
13
17
|
class TextToDialogue < Base
|
|
14
|
-
DEFAULT_MODEL = '
|
|
18
|
+
DEFAULT_MODEL = 'eleven_v4'
|
|
15
19
|
MAX_VOICES_PER_REQUEST = 10
|
|
20
|
+
|
|
21
|
+
# Compatibility constant for the original v3 dialogue endpoint.
|
|
16
22
|
MAX_TEXT_LENGTH = 5000
|
|
17
23
|
|
|
24
|
+
# Above this many characters the API recommends splitting the dialogue;
|
|
25
|
+
# the gem logs a warning but still sends the request.
|
|
26
|
+
RECOMMENDED_MAX_TEXT_LENGTH = 2_000
|
|
27
|
+
|
|
28
|
+
# Optional request-body keys, in the order they are written to the body.
|
|
29
|
+
# Each is omitted from the body when nil.
|
|
30
|
+
OPTIONAL_BODY_KEYS = %i[
|
|
31
|
+
language_code
|
|
32
|
+
audio_effects
|
|
33
|
+
settings
|
|
34
|
+
seed
|
|
35
|
+
use_pvc_as_ivc
|
|
36
|
+
previous_text
|
|
37
|
+
future_text
|
|
38
|
+
previous_request_ids
|
|
39
|
+
next_request_ids
|
|
40
|
+
pronunciation_dictionary_locators
|
|
41
|
+
].freeze
|
|
42
|
+
|
|
18
43
|
# Generate dialogue audio from multiple speaker inputs
|
|
19
44
|
#
|
|
20
45
|
# @param inputs [Array<Hash>] Array of { text:, voice_id: } hashes
|
|
21
|
-
# @param model_id [String] Model to use (
|
|
46
|
+
# @param model_id [String] Model to use (default: eleven_v4)
|
|
22
47
|
# @param language_code [String, nil] ISO 639-1 language code
|
|
23
|
-
# @param settings [Hash, nil] Generation settings (stability
|
|
48
|
+
# @param settings [Hash, nil] Generation settings, sent unchanged (e.g. stability, similarity)
|
|
24
49
|
# @param seed [Integer, nil] Seed for reproducibility
|
|
25
50
|
# @param output_format [String] Audio output format
|
|
26
51
|
# @param apply_text_normalization [String] "auto", "on", or "off"
|
|
52
|
+
# @param use_pvc_as_ivc [Boolean, nil] use the IVC version of professional voices
|
|
53
|
+
# @param previous_text [String, nil] text that comes before this dialogue (continuity)
|
|
54
|
+
# @param future_text [String, nil] text that comes after this dialogue (continuity)
|
|
55
|
+
# @param previous_request_ids [Array<String>, nil] request IDs of preceding generations
|
|
56
|
+
# @param next_request_ids [Array<String>, nil] request IDs of following generations
|
|
57
|
+
# @param pronunciation_dictionary_locators [Array<Hash>, nil] pronunciation dictionaries to apply
|
|
27
58
|
# @return [Objects::Audio]
|
|
28
59
|
def generate(
|
|
29
60
|
inputs,
|
|
30
61
|
model_id: DEFAULT_MODEL,
|
|
31
62
|
language_code: nil,
|
|
63
|
+
audio_effects: nil,
|
|
32
64
|
settings: nil,
|
|
33
65
|
seed: nil,
|
|
34
66
|
output_format: 'mp3_44100_128',
|
|
35
|
-
apply_text_normalization: 'auto'
|
|
67
|
+
apply_text_normalization: 'auto',
|
|
68
|
+
use_pvc_as_ivc: nil,
|
|
69
|
+
previous_text: nil,
|
|
70
|
+
future_text: nil,
|
|
71
|
+
previous_request_ids: nil,
|
|
72
|
+
next_request_ids: nil,
|
|
73
|
+
pronunciation_dictionary_locators: nil
|
|
36
74
|
)
|
|
37
|
-
validate_inputs!(inputs)
|
|
75
|
+
validate_inputs!(inputs, model_id)
|
|
38
76
|
|
|
39
|
-
body = build_request_body(inputs, model_id,
|
|
40
|
-
|
|
77
|
+
body = build_request_body(inputs, model_id, apply_text_normalization, optional_values(binding))
|
|
78
|
+
response = post_binary_with_meta("/text-to-dialogue?output_format=#{output_format}", body)
|
|
41
79
|
|
|
42
|
-
response
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
80
|
+
build_audio_response(response[:body], inputs, output_format, model_id,
|
|
81
|
+
request_id: response[:headers]['request-id'])
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# Generate dialogue audio with character timestamps and per-voice segments
|
|
85
|
+
#
|
|
86
|
+
# Takes the same keywords as {#generate}.
|
|
87
|
+
#
|
|
88
|
+
# @param inputs [Array<Hash>] Array of { text:, voice_id: } hashes
|
|
89
|
+
# @return [Hash] `{ audio:, alignment:, normalized_alignment:, voice_segments:, request_id: }`
|
|
90
|
+
def generate_with_timestamps(
|
|
91
|
+
inputs,
|
|
92
|
+
model_id: DEFAULT_MODEL,
|
|
93
|
+
language_code: nil,
|
|
94
|
+
audio_effects: nil,
|
|
95
|
+
settings: nil,
|
|
96
|
+
seed: nil,
|
|
97
|
+
output_format: 'mp3_44100_128',
|
|
98
|
+
apply_text_normalization: 'auto',
|
|
99
|
+
use_pvc_as_ivc: nil,
|
|
100
|
+
previous_text: nil,
|
|
101
|
+
future_text: nil,
|
|
102
|
+
previous_request_ids: nil,
|
|
103
|
+
next_request_ids: nil,
|
|
104
|
+
pronunciation_dictionary_locators: nil
|
|
105
|
+
)
|
|
106
|
+
validate_inputs!(inputs, model_id)
|
|
107
|
+
|
|
108
|
+
body = build_request_body(inputs, model_id, apply_text_normalization, optional_values(binding))
|
|
109
|
+
result = post_with_meta("/text-to-dialogue/with-timestamps?output_format=#{output_format}", body)
|
|
110
|
+
response = result[:body]
|
|
111
|
+
request_id = result[:headers]['request-id']
|
|
112
|
+
|
|
113
|
+
audio_data = Base64.decode64(response['audio_base64']) if response['audio_base64']
|
|
114
|
+
audio = (build_audio_response(audio_data, inputs, output_format, model_id, request_id: request_id) if audio_data)
|
|
115
|
+
|
|
116
|
+
{
|
|
117
|
+
audio: audio,
|
|
118
|
+
alignment: response['alignment'],
|
|
119
|
+
normalized_alignment: response['normalized_alignment'],
|
|
120
|
+
voice_segments: response['voice_segments'],
|
|
121
|
+
request_id: request_id
|
|
122
|
+
}
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def stream(inputs, model_id: DEFAULT_MODEL, output_format: 'mp3_44100_128',
|
|
126
|
+
apply_text_normalization: 'auto', timestamps: false, **options, &block)
|
|
127
|
+
raise ArgumentError, 'Block required for streaming' unless block
|
|
128
|
+
|
|
129
|
+
validate_inputs!(inputs, model_id)
|
|
130
|
+
body = build_request_body(inputs, model_id, apply_text_normalization, options)
|
|
131
|
+
suffix = timestamps ? '/stream/with-timestamps' : '/stream'
|
|
132
|
+
path = "/text-to-dialogue#{suffix}?output_format=#{output_format}"
|
|
133
|
+
headers = timestamps ? stream_timestamps(path, body, &block) : post_stream(path, body, &block)
|
|
134
|
+
notify_audio_generated(nil, inputs, model_id, headers['request-id'])
|
|
135
|
+
end
|
|
46
136
|
|
|
47
|
-
|
|
137
|
+
def stream_with_timestamps(inputs, **options, &block)
|
|
138
|
+
stream(inputs, **options, timestamps: true, &block)
|
|
48
139
|
end
|
|
49
140
|
|
|
50
141
|
private
|
|
51
142
|
|
|
52
|
-
|
|
53
|
-
|
|
143
|
+
# The optional keyword values of the calling method, keyed by OPTIONAL_BODY_KEYS
|
|
144
|
+
def optional_values(caller_binding)
|
|
145
|
+
OPTIONAL_BODY_KEYS.to_h { |key| [key, caller_binding.local_variable_get(key)] }
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def build_request_body(inputs, model_id, apply_text_normalization, options)
|
|
54
149
|
body = {
|
|
55
150
|
inputs: inputs.map { |i| { text: i[:text], voice_id: i[:voice_id] } },
|
|
56
151
|
model_id: model_id,
|
|
57
152
|
apply_text_normalization: apply_text_normalization
|
|
58
153
|
}
|
|
59
154
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
155
|
+
OPTIONAL_BODY_KEYS.each do |key|
|
|
156
|
+
body[key] = options[key] unless options[key].nil?
|
|
157
|
+
end
|
|
63
158
|
body
|
|
64
159
|
end
|
|
65
160
|
|
|
66
|
-
def build_audio_response(
|
|
161
|
+
def build_audio_response(data, inputs, output_format, model_id, request_id: nil)
|
|
67
162
|
total_text = inputs.map { |i| i[:text] }.join("\n")
|
|
68
|
-
total_chars = inputs.sum { |i| i[:text].length }
|
|
69
163
|
primary_voice = inputs.first[:voice_id]
|
|
70
164
|
|
|
71
165
|
audio = Objects::Audio.new(
|
|
72
|
-
data:
|
|
73
|
-
voice_id: primary_voice, text: total_text, model_id: model_id
|
|
166
|
+
data: data, format: output_format,
|
|
167
|
+
voice_id: primary_voice, text: total_text, model_id: model_id,
|
|
168
|
+
request_id: request_id
|
|
74
169
|
)
|
|
75
170
|
|
|
171
|
+
notify_audio_generated(audio, inputs, model_id, request_id)
|
|
172
|
+
audio
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
def notify_audio_generated(audio, inputs, model_id, request_id)
|
|
176
|
+
primary_voice = inputs.first[:voice_id]
|
|
177
|
+
total_text = inputs.map { |i| i[:text] }.join("\n")
|
|
178
|
+
total_chars = inputs.sum { |i| i[:text].length }
|
|
76
179
|
cost_info = Objects::CostInfo.new(
|
|
77
180
|
character_count: total_chars, voice_id: primary_voice, model_id: model_id
|
|
78
181
|
)
|
|
@@ -80,13 +183,12 @@ module ElevenRb
|
|
|
80
183
|
http_client.config.trigger(
|
|
81
184
|
:on_audio_generated,
|
|
82
185
|
audio: audio, voice_id: primary_voice,
|
|
83
|
-
text: total_text, cost_info: cost_info.to_h
|
|
186
|
+
text: total_text, cost_info: cost_info.to_h,
|
|
187
|
+
request_id: request_id
|
|
84
188
|
)
|
|
85
|
-
|
|
86
|
-
audio
|
|
87
189
|
end
|
|
88
190
|
|
|
89
|
-
def validate_inputs!(inputs)
|
|
191
|
+
def validate_inputs!(inputs, model_id = DEFAULT_MODEL)
|
|
90
192
|
raise Errors::ValidationError, 'inputs must be a non-empty array' unless inputs.is_a?(Array) && !inputs.empty?
|
|
91
193
|
|
|
92
194
|
inputs.each_with_index do |input, i|
|
|
@@ -101,12 +203,23 @@ module ElevenRb
|
|
|
101
203
|
"(got #{unique_voices.length})"
|
|
102
204
|
end
|
|
103
205
|
|
|
104
|
-
|
|
105
|
-
|
|
206
|
+
validate_text_length!(inputs.sum { |i| i[:text].length }, model_id)
|
|
207
|
+
end
|
|
106
208
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
209
|
+
def validate_text_length!(total_chars, model_id)
|
|
210
|
+
max_length = model_id.to_s.start_with?('eleven_v4') ? 10_000 : ModelCapabilities.max_text_length(model_id)
|
|
211
|
+
if total_chars > max_length
|
|
212
|
+
raise Errors::ValidationError,
|
|
213
|
+
"Total text length #{total_chars} exceeds maximum " \
|
|
214
|
+
"#{max_length} characters for #{model_id}"
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
return unless total_chars > RECOMMENDED_MAX_TEXT_LENGTH
|
|
218
|
+
|
|
219
|
+
http_client.config.logger&.warn(
|
|
220
|
+
"[ElevenRb] text-to-dialogue text is #{total_chars} characters; " \
|
|
221
|
+
"#{RECOMMENDED_MAX_TEXT_LENGTH} or fewer per request is recommended"
|
|
222
|
+
)
|
|
110
223
|
end
|
|
111
224
|
end
|
|
112
225
|
end
|