eleven_rb 1.1.0 → 1.2.1
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 +14 -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 +40 -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
|
@@ -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
|
|
@@ -18,8 +18,7 @@ module ElevenRb
|
|
|
18
18
|
DEFAULT_MODEL = 'eleven_v4'
|
|
19
19
|
MAX_VOICES_PER_REQUEST = 10
|
|
20
20
|
|
|
21
|
-
#
|
|
22
|
-
# ModelCapabilities.max_text_length(model_id) (5,000 for eleven_v3).
|
|
21
|
+
# Compatibility constant for the original v3 dialogue endpoint.
|
|
23
22
|
MAX_TEXT_LENGTH = 5000
|
|
24
23
|
|
|
25
24
|
# Above this many characters the API recommends splitting the dialogue;
|
|
@@ -30,6 +29,7 @@ module ElevenRb
|
|
|
30
29
|
# Each is omitted from the body when nil.
|
|
31
30
|
OPTIONAL_BODY_KEYS = %i[
|
|
32
31
|
language_code
|
|
32
|
+
audio_effects
|
|
33
33
|
settings
|
|
34
34
|
seed
|
|
35
35
|
use_pvc_as_ivc
|
|
@@ -60,6 +60,7 @@ module ElevenRb
|
|
|
60
60
|
inputs,
|
|
61
61
|
model_id: DEFAULT_MODEL,
|
|
62
62
|
language_code: nil,
|
|
63
|
+
audio_effects: nil,
|
|
63
64
|
settings: nil,
|
|
64
65
|
seed: nil,
|
|
65
66
|
output_format: 'mp3_44100_128',
|
|
@@ -90,6 +91,7 @@ module ElevenRb
|
|
|
90
91
|
inputs,
|
|
91
92
|
model_id: DEFAULT_MODEL,
|
|
92
93
|
language_code: nil,
|
|
94
|
+
audio_effects: nil,
|
|
93
95
|
settings: nil,
|
|
94
96
|
seed: nil,
|
|
95
97
|
output_format: 'mp3_44100_128',
|
|
@@ -120,6 +122,22 @@ module ElevenRb
|
|
|
120
122
|
}
|
|
121
123
|
end
|
|
122
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
|
|
136
|
+
|
|
137
|
+
def stream_with_timestamps(inputs, **options, &block)
|
|
138
|
+
stream(inputs, **options, timestamps: true, &block)
|
|
139
|
+
end
|
|
140
|
+
|
|
123
141
|
private
|
|
124
142
|
|
|
125
143
|
# The optional keyword values of the calling method, keyed by OPTIONAL_BODY_KEYS
|
|
@@ -142,7 +160,6 @@ module ElevenRb
|
|
|
142
160
|
|
|
143
161
|
def build_audio_response(data, inputs, output_format, model_id, request_id: nil)
|
|
144
162
|
total_text = inputs.map { |i| i[:text] }.join("\n")
|
|
145
|
-
total_chars = inputs.sum { |i| i[:text].length }
|
|
146
163
|
primary_voice = inputs.first[:voice_id]
|
|
147
164
|
|
|
148
165
|
audio = Objects::Audio.new(
|
|
@@ -151,6 +168,14 @@ module ElevenRb
|
|
|
151
168
|
request_id: request_id
|
|
152
169
|
)
|
|
153
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 }
|
|
154
179
|
cost_info = Objects::CostInfo.new(
|
|
155
180
|
character_count: total_chars, voice_id: primary_voice, model_id: model_id
|
|
156
181
|
)
|
|
@@ -161,8 +186,6 @@ module ElevenRb
|
|
|
161
186
|
text: total_text, cost_info: cost_info.to_h,
|
|
162
187
|
request_id: request_id
|
|
163
188
|
)
|
|
164
|
-
|
|
165
|
-
audio
|
|
166
189
|
end
|
|
167
190
|
|
|
168
191
|
def validate_inputs!(inputs, model_id = DEFAULT_MODEL)
|
|
@@ -184,7 +207,7 @@ module ElevenRb
|
|
|
184
207
|
end
|
|
185
208
|
|
|
186
209
|
def validate_text_length!(total_chars, model_id)
|
|
187
|
-
max_length = ModelCapabilities.max_text_length(model_id)
|
|
210
|
+
max_length = model_id.to_s.start_with?('eleven_v4') ? 10_000 : ModelCapabilities.max_text_length(model_id)
|
|
188
211
|
if total_chars > max_length
|
|
189
212
|
raise Errors::ValidationError,
|
|
190
213
|
"Total text length #{total_chars} exceeds maximum " \
|
|
@@ -65,6 +65,7 @@ module ElevenRb
|
|
|
65
65
|
# Each is omitted from the body when nil.
|
|
66
66
|
OPTIONAL_BODY_KEYS = %i[
|
|
67
67
|
language_code
|
|
68
|
+
audio_effects
|
|
68
69
|
apply_text_normalization
|
|
69
70
|
seed
|
|
70
71
|
previous_text
|
|
@@ -95,7 +96,7 @@ module ElevenRb
|
|
|
95
96
|
def generate(text, voice_id:, model_id: DEFAULT_MODEL, voice_settings: {}, output_format: 'mp3_44100_128',
|
|
96
97
|
language_code: nil, apply_text_normalization: nil, seed: nil, previous_text: nil,
|
|
97
98
|
next_text: nil, previous_request_ids: nil, next_request_ids: nil,
|
|
98
|
-
pronunciation_dictionary_locators: nil, use_pvc_as_ivc: nil)
|
|
99
|
+
pronunciation_dictionary_locators: nil, use_pvc_as_ivc: nil, audio_effects: nil)
|
|
99
100
|
validate_text!(text, model_id)
|
|
100
101
|
validate_presence!(voice_id, 'voice_id')
|
|
101
102
|
body, dropped = build_body(text, model_id, voice_settings, optional_values(binding))
|
|
@@ -143,26 +144,13 @@ module ElevenRb
|
|
|
143
144
|
def stream(text, voice_id:, model_id: DEFAULT_MODEL, voice_settings: {}, output_format: 'mp3_44100_128',
|
|
144
145
|
language_code: nil, apply_text_normalization: nil, seed: nil, previous_text: nil,
|
|
145
146
|
next_text: nil, previous_request_ids: nil, next_request_ids: nil,
|
|
146
|
-
pronunciation_dictionary_locators: nil, use_pvc_as_ivc: nil, &block)
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
body, = build_body(text, model_id, voice_settings, optional_values(binding))
|
|
152
|
-
|
|
153
|
-
path = "/text-to-speech/#{voice_id}/stream?output_format=#{output_format}"
|
|
154
|
-
post_stream(path, body, &block)
|
|
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
|
|
155
151
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
http_client.config.trigger(
|
|
159
|
-
:on_audio_generated,
|
|
160
|
-
audio: nil, # No audio object for streaming
|
|
161
|
-
voice_id: voice_id,
|
|
162
|
-
text: text,
|
|
163
|
-
cost_info: cost_info.to_h,
|
|
164
|
-
request_id: nil
|
|
165
|
-
)
|
|
152
|
+
def stream_with_timestamps(text, **options, &block)
|
|
153
|
+
deliver_stream(text, **options, timestamps: true, &block)
|
|
166
154
|
end
|
|
167
155
|
|
|
168
156
|
# Generate audio with timestamps
|
|
@@ -179,7 +167,7 @@ module ElevenRb
|
|
|
179
167
|
output_format: 'mp3_44100_128', language_code: nil,
|
|
180
168
|
apply_text_normalization: nil, seed: nil, previous_text: nil,
|
|
181
169
|
next_text: nil, previous_request_ids: nil, next_request_ids: nil,
|
|
182
|
-
pronunciation_dictionary_locators: nil, use_pvc_as_ivc: nil)
|
|
170
|
+
pronunciation_dictionary_locators: nil, use_pvc_as_ivc: nil, audio_effects: nil)
|
|
183
171
|
validate_text!(text, model_id)
|
|
184
172
|
validate_presence!(voice_id, 'voice_id')
|
|
185
173
|
body, dropped = build_body(text, model_id, voice_settings, optional_values(binding))
|
|
@@ -217,6 +205,36 @@ module ElevenRb
|
|
|
217
205
|
|
|
218
206
|
private
|
|
219
207
|
|
|
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
|
+
|
|
220
238
|
# Shared request body for generate / stream / generate_with_timestamps
|
|
221
239
|
#
|
|
222
240
|
# @return [Array(Hash, Array<Symbol>)] the body and the dropped voice-setting keys
|
|
@@ -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
|
-
|
|
21
|
-
|
|
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
|
data/lib/eleven_rb/version.rb
CHANGED
data/lib/eleven_rb.rb
CHANGED
|
@@ -103,6 +103,8 @@ require_relative 'eleven_rb/collections/library_voice_collection'
|
|
|
103
103
|
|
|
104
104
|
# Resources
|
|
105
105
|
require_relative 'eleven_rb/resources/base'
|
|
106
|
+
require_relative 'eleven_rb/resources/audio_effects'
|
|
107
|
+
require_relative 'eleven_rb/resources/speech_to_text'
|
|
106
108
|
require_relative 'eleven_rb/resources/voices'
|
|
107
109
|
require_relative 'eleven_rb/resources/text_to_speech'
|
|
108
110
|
require_relative 'eleven_rb/resources/voice_library'
|