Class: Elevenlabs::Client
- Inherits:
-
Object
- Object
- Elevenlabs::Client
- Defined in:
- lib/elevenlabs/client.rb
Constant Summary collapse
- BASE_URL =
"https://api.elevenlabs.io"
Instance Method Summary collapse
-
#active?(voice_id) ⇒ Boolean
Checks if a voice_id is in list_voices.
-
#banned?(voice_id) ⇒ Boolean
Checks safety control on a single voice for “BAN”.
-
#compose_music(options = {}) ⇒ Object
1.
-
#compose_music_detailed(options = {}) ⇒ Object
3.
-
#compose_music_stream(options = {}, &block) ⇒ Object
2.
-
#create_from_generated_voice(voice_name, voice_description, generated_voice_id, labels: nil, played_not_selected_voice_ids: nil) ⇒ Hash
Create a Voice # (POST /v1/text-to-voice/create) #.
-
#create_music_plan(options = {}) ⇒ Object
4.
-
#create_voice(name, samples = [], options = {}) ⇒ Object
Creates a new voice NOTE: This method may require a multipart form request if you are uploading sample audio files.
-
#delete_voice(voice_id) ⇒ Hash
Deletes a voice from your account.
-
#design_voice(voice_description, options = {}) ⇒ Hash
Designs a voice based on a description Documentation: elevenlabs.io/docs/api-reference/text-to-voice/design.
-
#edit_voice(voice_id, samples = [], options = {}) ⇒ Object
Edit a Voice # (POST /v1/voices/voice_id/edit) #.
-
#get_voice(voice_id) ⇒ Hash
Retrieves details about a single voice.
-
#initialize(api_key: nil, open_timeout: 5, read_timeout: 120) ⇒ Client
constructor
Note the default param: ‘api_key: nil`.
-
#list_models ⇒ Hash
Gets a list of available models Documentation: elevenlabs.io/docs/api-reference/models/list.
-
#list_voices ⇒ Hash
Retrieves all voices associated with your Elevenlabs account Documentation: elevenlabs.io/docs/api-reference/voices.
-
#sound_generation(text, options = {}) ⇒ String
Convert text to sound effects and retrieve audio (binary data) Documentation: elevenlabs.io/docs/api-reference/sound-generation.
-
#text_to_dialogue(inputs, model_id = nil, settings = {}, seed = nil) ⇒ String
Converts a list of text and voice ID pairs into speech (dialogue) and returns audio.
-
#text_to_speech(voice_id, text, options = {}) ⇒ String
Convert text to speech and retrieve audio (binary data) Documentation: elevenlabs.io/docs/api-reference/text-to-speech/convert.
-
#text_to_speech_stream(voice_id, text, options = {}, &block) ⇒ Object
Text-to-Speech-Stream # (POST /v1/text-to-speech/voice_id)/stream #.
Constructor Details
#initialize(api_key: nil, open_timeout: 5, read_timeout: 120) ⇒ Client
Note the default param: ‘api_key: nil`
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/elevenlabs/client.rb', line 12 def initialize(api_key: nil, open_timeout: 5, read_timeout: 120) # If the caller doesn’t provide an api_key, use the gem-wide config @api_key = api_key || Elevenlabs.configuration&.api_key @connection = Faraday.new(url: BASE_URL) do |conn| conn.request :url_encoded conn.response :raise_error conn..open_timeout = open_timeout # time to open connection conn..timeout = read_timeout # time to wait for response conn.adapter Faraday.default_adapter end end |
Instance Method Details
#active?(voice_id) ⇒ Boolean
Checks if a voice_id is in list_voices
447 448 449 450 |
# File 'lib/elevenlabs/client.rb', line 447 def active?(voice_id) active_voices = list_voices["voices"].map{|voice| voice["voice_id"]} voice_id.in?(active_voices) end |
#banned?(voice_id) ⇒ Boolean
Checks safety control on a single voice for “BAN”
434 435 436 437 |
# File 'lib/elevenlabs/client.rb', line 434 def banned?(voice_id) voice = get_voice(voice_id) voice["safety_control"] == "BAN" end |
#compose_music(options = {}) ⇒ Object
-
Compose music (basic)
POST /v1/music
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 |
# File 'lib/elevenlabs/client.rb', line 458 def compose_music( = {}) endpoint = "/v1/music" request_body = { prompt: [:prompt], composition_plan: [:composition_plan], music_length_ms: [:music_length_ms], model_id: [:model_id] || "music_v1" }.compact headers = default_headers.merge("Accept" => "audio/mpeg") query = {} query[:output_format] = [:output_format] if [:output_format] response = @connection.post("#{endpoint}?#{URI.encode_www_form(query)}") do |req| req.headers = headers req.body = request_body.to_json end response.body # raw binary audio rescue Faraday::ClientError => e handle_error(e) end |
#compose_music_detailed(options = {}) ⇒ Object
-
Compose detailed music (metadata + audio)
POST /v1/music/detailed
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 |
# File 'lib/elevenlabs/client.rb', line 511 def compose_music_detailed( = {}) endpoint = "/v1/music/detailed" request_body = { prompt: [:prompt], composition_plan: [:composition_plan], music_length_ms: [:music_length_ms], model_id: [:model_id] || "music_v1" }.compact headers = default_headers query = {} query[:output_format] = [:output_format] if [:output_format] response = @connection.post("#{endpoint}?#{URI.encode_www_form(query)}") do |req| req.headers = headers req.body = request_body.to_json end response.body # multipart/mixed with JSON + binary audio rescue Faraday::ClientError => e handle_error(e) end |
#compose_music_stream(options = {}, &block) ⇒ Object
-
Stream music
POST /v1/music/stream
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 |
# File 'lib/elevenlabs/client.rb', line 483 def compose_music_stream( = {}, &block) endpoint = "/v1/music/stream" request_body = { prompt: [:prompt], composition_plan: [:composition_plan], music_length_ms: [:music_length_ms], model_id: [:model_id] || "music_v1" }.compact headers = default_headers.merge("Accept" => "audio/mpeg") query = {} query[:output_format] = [:output_format] if [:output_format] @connection.post("#{endpoint}?#{URI.encode_www_form(query)}") do |req| req..on_data = Proc.new do |chunk, _| block.call(chunk) if block end req.headers = headers req.body = request_body.to_json end nil # audio streamed via block rescue Faraday::ClientError => e handle_error(e) end |
#create_from_generated_voice(voice_name, voice_description, generated_voice_id, labels: nil, played_not_selected_voice_ids: nil) ⇒ Hash
Create a Voice #
(POST /v1/text-to-voice/create) #
Creates a voice from the designed voice generated_voice_id Documentation: elevenlabs.io/docs/api-reference/text-to-voice
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/elevenlabs/client.rb', line 244 def create_from_generated_voice(voice_name, voice_description, generated_voice_id, labels: nil, played_not_selected_voice_ids: nil) endpoint = "/v1/text-to-voice" request_body = { voice_name: voice_name, voice_description: voice_description, generated_voice_id: generated_voice_id, labels: labels, played_not_selected_voice_ids: played_not_selected_voice_ids }.compact response = @connection.post(endpoint) do |req| req.headers = default_headers req.body = request_body.to_json end JSON.parse(response.body) rescue Faraday::ClientError => e handle_error(e) end |
#create_music_plan(options = {}) ⇒ Object
-
Create a composition plan
POST /v1/music/plan
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 |
# File 'lib/elevenlabs/client.rb', line 537 def create_music_plan( = {}) endpoint = "/v1/music/plan" request_body = { prompt: [:prompt], music_length_ms: [:music_length_ms], source_composition_plan: [:source_composition_plan], model_id: [:model_id] || "music_v1" }.compact response = @connection.post(endpoint) do |req| req.headers = default_headers req.body = request_body.to_json end JSON.parse(response.body, symbolize_names: true) rescue Faraday::ClientError => e handle_error(e) end |
#create_voice(name, samples = [], options = {}) ⇒ Object
Creates a new voice NOTE: This method may require a multipart form request
if you are uploading sample audio files.
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/elevenlabs/client.rb', line 333 def create_voice(name, samples = [], = {}) endpoint = "/v1/voices/add" # Ensure Faraday handles multipart form data mp_connection = Faraday.new(url: BASE_URL) do |conn| conn.request :multipart conn.response :raise_error conn.adapter Faraday.default_adapter end # Build multipart form parameters form_params = { "name" => name, "description" => [:description] || "" } # Convert File objects to multipart upload format sample_files = [] samples.each_with_index do |sample_file, i| sample_files << ["files", Faraday::UploadIO.new(sample_file.path, "audio/mpeg")] end # Perform the POST request response = mp_connection.post(endpoint) do |req| req.headers["xi-api-key"] = @api_key req.body = form_params.merge(sample_files.to_h) end JSON.parse(response.body) rescue Faraday::ClientError => e handle_error(e) end |
#delete_voice(voice_id) ⇒ Hash
Deletes a voice from your account
415 416 417 418 419 420 421 422 423 424 |
# File 'lib/elevenlabs/client.rb', line 415 def delete_voice(voice_id) endpoint = "/v1/voices/#{voice_id}" response = @connection.delete(endpoint) do |req| req.headers = default_headers end JSON.parse(response.body) rescue Faraday::ClientError => e handle_error(e) end |
#design_voice(voice_description, options = {}) ⇒ Hash
Designs a voice based on a description Documentation: elevenlabs.io/docs/api-reference/text-to-voice/design
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/elevenlabs/client.rb', line 201 def design_voice(voice_description, = {}) endpoint = "/v1/text-to-voice/design" request_body = { voice_description: voice_description } # Add optional parameters if provided request_body[:output_format] = [:output_format] if [:output_format] request_body[:model_id] = [:model_id] if [:model_id] request_body[:text] = [:text] if [:text] request_body[:auto_generate_text] = [:auto_generate_text] unless [:auto_generate_text].nil? request_body[:loudness] = [:loudness] if [:loudness] request_body[:seed] = [:seed] if [:seed] request_body[:guidance_scale] = [:guidance_scale] if [:guidance_scale] request_body[:stream_previews] = [:stream_previews] unless [:stream_previews].nil? request_body[:remixing_session_id] = [:remixing_session_id] if [:remixing_session_id] request_body[:remixing_session_iteration_id] = [:remixing_session_iteration_id] if [:remixing_session_iteration_id] request_body[:quality] = [:quality] if [:quality] request_body[:reference_audio_base64] = [:reference_audio_base64] if [:reference_audio_base64] request_body[:prompt_strength] = [:prompt_strength] if [:prompt_strength] response = @connection.post(endpoint) do |req| req.headers = default_headers req.body = request_body.to_json end JSON.parse(response.body) rescue Faraday::ClientError => e handle_error(e) end |
#edit_voice(voice_id, samples = [], options = {}) ⇒ Object
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
# File 'lib/elevenlabs/client.rb', line 378 def edit_voice(voice_id, samples = [], = {}) endpoint = "/v1/voices/#{voice_id}/edit" # Force text fields to be strings. form_params = { "name" => [:name].to_s, "description" => ([:description] || "").to_s } form_params["files[]"] = samples.map do |sample_file| Faraday::UploadIO.new(sample_file.path, "audio/mpeg", File.basename(sample_file.path)) end mp_connection = Faraday.new(url: BASE_URL) do |conn| conn.request :multipart conn.response :raise_error conn.adapter Faraday.default_adapter end response = mp_connection.post(endpoint) do |req| req.headers["xi-api-key"] = @api_key req.body = form_params end JSON.parse(response.body) rescue Faraday::ClientError => e handle_error(e) end |
#get_voice(voice_id) ⇒ Hash
Retrieves details about a single voice
310 311 312 313 314 315 316 317 318 |
# File 'lib/elevenlabs/client.rb', line 310 def get_voice(voice_id) endpoint = "/v1/voices/#{voice_id}" response = @connection.get(endpoint) do |req| req.headers = default_headers end JSON.parse(response.body) rescue Faraday::ClientError => e handle_error(e) end |
#list_models ⇒ Hash
Gets a list of available models Documentation: elevenlabs.io/docs/api-reference/models/list
291 292 293 294 295 296 297 298 299 |
# File 'lib/elevenlabs/client.rb', line 291 def list_models endpoint = "/v1/models" response = @connection.get(endpoint) do |req| req.headers = default_headers end JSON.parse(response.body) rescue Faraday::ClientError => e handle_error(e) end |
#list_voices ⇒ Hash
Retrieves all voices associated with your Elevenlabs account Documentation: elevenlabs.io/docs/api-reference/voices
272 273 274 275 276 277 278 279 280 |
# File 'lib/elevenlabs/client.rb', line 272 def list_voices endpoint = "/v1/voices" response = @connection.get(endpoint) do |req| req.headers = default_headers end JSON.parse(response.body) rescue Faraday::ClientError => e handle_error(e) end |
#sound_generation(text, options = {}) ⇒ String
Convert text to sound effects and retrieve audio (binary data) Documentation: elevenlabs.io/docs/api-reference/sound-generation
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/elevenlabs/client.rb', line 150 def sound_generation(text, = {}) endpoint = "/v1/sound-generation" request_body = { text: text } # Add optional parameters if provided request_body[:loop] = [:loop] unless [:loop].nil? request_body[:duration_seconds] = [:duration_seconds] if [:duration_seconds] request_body[:prompt_influence] = [:prompt_influence] if [:prompt_influence] headers = default_headers headers["Accept"] = "audio/mpeg" query = {} query[:output_format] = [:output_format] if [:output_format] response = @connection.post("#{endpoint}?#{URI.encode_www_form(query)}") do |req| req.headers = headers req.body = request_body.to_json end # Returns raw binary data (often MP3) response.body rescue Faraday::ClientError => e handle_error(e) end |
#text_to_dialogue(inputs, model_id = nil, settings = {}, seed = nil) ⇒ String
Converts a list of text and voice ID pairs into speech (dialogue) and returns audio. Documentation: elevenlabs.io/docs/api-reference/text-to-dialogue/convert
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/elevenlabs/client.rb', line 111 def text_to_dialogue(inputs, model_id = nil, settings = {}, seed = nil) endpoint = "/v1/text-to-dialogue" request_body = {}.tap do |r| r[:inputs] = inputs r[:model_id] = model_id if model_id r[:settings] = settings unless settings.empty? r[:seed] = seed if seed end headers = default_headers headers["Accept"] = "audio/mpeg" response = @connection.post(endpoint) do |req| req.headers = headers req.body = request_body.to_json end # Returns raw binary data (often MP3) response.body rescue Faraday::ClientError => e handle_error(e) end |
#text_to_speech(voice_id, text, options = {}) ⇒ String
Convert text to speech and retrieve audio (binary data) Documentation: elevenlabs.io/docs/api-reference/text-to-speech/convert
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/elevenlabs/client.rb', line 41 def text_to_speech(voice_id, text, = {}) endpoint = "/v1/text-to-speech/#{voice_id}" request_body = { text: text } # If user provided voice_settings, add them if [:voice_settings] request_body[:voice_settings] = [:voice_settings] end # If user specified a model_id, add it request_body[:model_id] = [:model_id] if [:model_id] # If user wants streaming optimization headers = default_headers if [:optimize_streaming] headers["Accept"] = "audio/mpeg" headers["Transfer-Encoding"] = "chunked" end response = @connection.post(endpoint) do |req| req.headers = headers req.body = request_body.to_json end # Returns raw binary data (often MP3) response.body rescue Faraday::ClientError => e handle_error(e) end |
#text_to_speech_stream(voice_id, text, options = {}, &block) ⇒ Object
Text-to-Speech-Stream # (POST /v1/text-to-speech/voice_id)/stream #
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/elevenlabs/client.rb', line 75 def text_to_speech_stream(voice_id, text, = {}, &block) endpoint = "/v1/text-to-speech/#{voice_id}/stream?output_format=mp3_44100_128" request_body = { text: text, model_id: [:model_id] || "eleven_multilingual_v2" } headers = default_headers headers["Accept"] = "audio/mpeg" response = @connection.post(endpoint, request_body.to_json, headers) do |req| req..on_data = Proc.new do |chunk, _| block.call(chunk) if block_given? end end response rescue Faraday::ClientError => e handle_error(e) end |