Class: ElevenlabsClient::TextToSpeech
- Inherits:
-
Object
- Object
- ElevenlabsClient::TextToSpeech
- Defined in:
- lib/elevenlabs_client/endpoints/text_to_speech.rb
Instance Method Summary collapse
-
#convert(voice_id, text, **options) ⇒ String
(also: #text_to_speech)
POST /v1/text-to-speech/voice_id Convert text to speech and retrieve audio (binary data) Documentation: https://elevenlabs.io/docs/api-reference/text-to-speech/convert.
-
#convert_with_timestamps(voice_id, text, **options) ⇒ Hash
(also: #text_to_speech_with_timestamps)
POST /v1/text-to-speech/voice_id/with-timestamps Generate speech from text with precise character-level timing information Documentation: https://elevenlabs.io/docs/api-reference/text-to-speech/with-timestamps.
-
#initialize(client) ⇒ TextToSpeech
constructor
A new instance of TextToSpeech.
-
#stream(voice_id, text, **options, &block) ⇒ Faraday::Response
(also: #text_to_speech_stream)
POST /v1/text-to-speech/voice_id/stream Stream text-to-speech audio in real-time chunks Documentation: https://elevenlabs.io/docs/api-reference/text-to-speech/stream.
-
#stream_with_timestamps(voice_id, text, **options, &block) ⇒ Faraday::Response
(also: #text_to_speech_stream_with_timestamps)
POST /v1/text-to-speech/voice_id/stream/with-timestamps Stream text-to-speech audio with character-level timing information Documentation: https://elevenlabs.io/docs/api-reference/text-to-speech/stream-with-timestamps.
Constructor Details
#initialize(client) ⇒ TextToSpeech
Returns a new instance of TextToSpeech.
5 6 7 |
# File 'lib/elevenlabs_client/endpoints/text_to_speech.rb', line 5 def initialize(client) @client = client end |
Instance Method Details
#convert(voice_id, text, **options) ⇒ String Also known as: text_to_speech
POST /v1/text-to-speech/voice_id Convert text to speech and retrieve audio (binary data) Documentation: https://elevenlabs.io/docs/api-reference/text-to-speech/convert
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/elevenlabs_client/endpoints/text_to_speech.rb', line 20 def convert(voice_id, text, **) endpoint = "/v1/text-to-speech/#{voice_id}" request_body = { text: text } # Add optional parameters request_body[:model_id] = [:model_id] if [:model_id] request_body[:voice_settings] = [:voice_settings] if [:voice_settings] # Handle streaming optimization if [:optimize_streaming] @client.post_with_custom_headers(endpoint, request_body, streaming_headers) else @client.post_binary(endpoint, request_body) end end |
#convert_with_timestamps(voice_id, text, **options) ⇒ Hash Also known as: text_to_speech_with_timestamps
POST /v1/text-to-speech/voice_id/with-timestamps Generate speech from text with precise character-level timing information Documentation: https://elevenlabs.io/docs/api-reference/text-to-speech/with-timestamps
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/elevenlabs_client/endpoints/text_to_speech.rb', line 59 def (voice_id, text, **) # Build query parameters query_params = {} query_params[:enable_logging] = [:enable_logging] unless [:enable_logging].nil? query_params[:optimize_streaming_latency] = [:optimize_streaming_latency] if [:optimize_streaming_latency] query_params[:output_format] = [:output_format] if [:output_format] # Build endpoint with query parameters endpoint = "/v1/text-to-speech/#{voice_id}/with-timestamps" if query_params.any? query_string = query_params.map { |k, v| "#{k}=#{v}" }.join("&") endpoint += "?#{query_string}" end # Build request body request_body = { text: text } # Add optional body parameters request_body[:model_id] = [:model_id] if [:model_id] request_body[:language_code] = [:language_code] if [:language_code] request_body[:voice_settings] = [:voice_settings] if [:voice_settings] request_body[:pronunciation_dictionary_locators] = [:pronunciation_dictionary_locators] if [:pronunciation_dictionary_locators] request_body[:seed] = [:seed] if [:seed] request_body[:previous_text] = [:previous_text] if [:previous_text] request_body[:next_text] = [:next_text] if [:next_text] request_body[:previous_request_ids] = [:previous_request_ids] if [:previous_request_ids] request_body[:next_request_ids] = [:next_request_ids] if [:next_request_ids] request_body[:apply_text_normalization] = [:apply_text_normalization] if [:apply_text_normalization] request_body[:apply_language_text_normalization] = [:apply_language_text_normalization] unless [:apply_language_text_normalization].nil? request_body[:use_pvc_as_ivc] = [:use_pvc_as_ivc] unless [:use_pvc_as_ivc].nil? @client.post(endpoint, request_body) end |
#stream(voice_id, text, **options, &block) ⇒ Faraday::Response Also known as: text_to_speech_stream
POST /v1/text-to-speech/voice_id/stream Stream text-to-speech audio in real-time chunks Documentation: https://elevenlabs.io/docs/api-reference/text-to-speech/stream
107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/elevenlabs_client/endpoints/text_to_speech.rb', line 107 def stream(voice_id, text, **, &block) output_format = [:output_format] || "mp3_44100_128" endpoint = "/v1/text-to-speech/#{voice_id}/stream?output_format=#{output_format}" request_body = { text: text, model_id: [:model_id] || "eleven_multilingual_v2" } # Add voice_settings if provided request_body[:voice_settings] = [:voice_settings] if [:voice_settings] @client.post_streaming(endpoint, request_body, &block) end |
#stream_with_timestamps(voice_id, text, **options, &block) ⇒ Faraday::Response Also known as: text_to_speech_stream_with_timestamps
POST /v1/text-to-speech/voice_id/stream/with-timestamps Stream text-to-speech audio with character-level timing information Documentation: https://elevenlabs.io/docs/api-reference/text-to-speech/stream-with-timestamps
146 147 148 149 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 175 176 177 178 179 |
# File 'lib/elevenlabs_client/endpoints/text_to_speech.rb', line 146 def (voice_id, text, **, &block) # Build query parameters query_params = {} query_params[:enable_logging] = [:enable_logging] unless [:enable_logging].nil? query_params[:optimize_streaming_latency] = [:optimize_streaming_latency] if [:optimize_streaming_latency] query_params[:output_format] = [:output_format] if [:output_format] # Build endpoint with query parameters endpoint = "/v1/text-to-speech/#{voice_id}/stream/with-timestamps" if query_params.any? query_string = query_params.map { |k, v| "#{k}=#{v}" }.join("&") endpoint += "?#{query_string}" end # Build request body request_body = { text: text } # Add optional body parameters request_body[:model_id] = [:model_id] if [:model_id] request_body[:language_code] = [:language_code] if [:language_code] request_body[:voice_settings] = [:voice_settings] if [:voice_settings] request_body[:pronunciation_dictionary_locators] = [:pronunciation_dictionary_locators] if [:pronunciation_dictionary_locators] request_body[:seed] = [:seed] if [:seed] request_body[:previous_text] = [:previous_text] if [:previous_text] request_body[:next_text] = [:next_text] if [:next_text] request_body[:previous_request_ids] = [:previous_request_ids] if [:previous_request_ids] request_body[:next_request_ids] = [:next_request_ids] if [:next_request_ids] request_body[:apply_text_normalization] = [:apply_text_normalization] if [:apply_text_normalization] request_body[:apply_language_text_normalization] = [:apply_language_text_normalization] unless [:apply_language_text_normalization].nil? request_body[:use_pvc_as_ivc] = [:use_pvc_as_ivc] unless [:use_pvc_as_ivc].nil? # Use streaming method with JSON parsing for timestamp data @client.(endpoint, request_body, &block) end |