Class: ElevenlabsClient::WebSocketTextToSpeech
- Inherits:
-
Object
- Object
- ElevenlabsClient::WebSocketTextToSpeech
- Defined in:
- lib/elevenlabs_client/endpoints/websocket_text_to_speech.rb
Instance Method Summary collapse
-
#connect_multi_stream_input(voice_id, **options) ⇒ WebSocket::Client::Simple::Client
(also: #connect_multi_context)
Creates a WebSocket connection for multi-context text-to-speech streaming Documentation: https://elevenlabs.io/docs/api-reference/websockets/multi-context.
-
#connect_stream_input(voice_id, **options) ⇒ WebSocket::Client::Simple::Client
(also: #connect_single_stream)
Creates a WebSocket connection for real-time text-to-speech streaming Documentation: https://elevenlabs.io/docs/api-reference/websockets/text-to-speech.
-
#initialize(client) ⇒ WebSocketTextToSpeech
constructor
A new instance of WebSocketTextToSpeech.
-
#send_close_connection(ws) ⇒ Object
Helper method to close connection for single stream.
-
#send_close_context(ws, context_id) ⇒ Object
Helper method to close a specific context.
-
#send_close_socket(ws) ⇒ Object
Helper method to close the entire socket.
-
#send_flush_context(ws, context_id) ⇒ Object
Helper method to flush a context.
-
#send_initialize_connection(ws, **options) ⇒ Object
Helper method to send initialization message for single stream.
-
#send_initialize_connection_multi(ws, context_id, **options) ⇒ Object
Helper method to send initialization message for multi-context stream.
-
#send_initialize_context(ws, context_id, **options) ⇒ Object
Helper method to initialize a new context in multi-stream.
-
#send_keep_context_alive(ws, context_id) ⇒ Object
Helper method to keep a context alive.
-
#send_text(ws, text, **options) ⇒ Object
Helper method to send text for single stream.
-
#send_text_multi(ws, context_id, text, **options) ⇒ Object
Helper method to send text for multi-context stream.
-
#stream_text_to_speech(voice_id, text_chunks, **options, &block) ⇒ Object
Convenience method to create a complete streaming session.
Constructor Details
#initialize(client) ⇒ WebSocketTextToSpeech
Returns a new instance of WebSocketTextToSpeech.
8 9 10 11 |
# File 'lib/elevenlabs_client/endpoints/websocket_text_to_speech.rb', line 8 def initialize(client) @client = client @base_url = client.base_url.gsub('https://', 'wss://').gsub('http://', 'ws://') end |
Instance Method Details
#connect_multi_stream_input(voice_id, **options) ⇒ WebSocket::Client::Simple::Client Also known as: connect_multi_context
Creates a WebSocket connection for multi-context text-to-speech streaming Documentation: https://elevenlabs.io/docs/api-reference/websockets/multi-context
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/elevenlabs_client/endpoints/websocket_text_to_speech.rb', line 58 def connect_multi_stream_input(voice_id, **) endpoint = "/v1/text-to-speech/#{voice_id}/multi-stream-input" # Build query parameters in the same order as provided in options allowed_keys = [:model_id, :language_code, :enable_logging, :enable_ssml_parsing, :output_format, :inactivity_timeout, :sync_alignment, :auto_mode, :apply_text_normalization, :seed] pairs = [] .each do |k, v| next unless allowed_keys.include?(k) next if v.nil? next if (k == :language_code || k == :apply_text_normalization) && v.to_s.empty? pairs << [k, v] end if pairs.any? query_string = pairs.map { |k, v| "#{k}=#{v}" }.join("&") endpoint += "?#{query_string}" end url = "#{@base_url}#{endpoint}" headers = { "xi-api-key" => @client.api_key } WebSocket::Client::Simple.connect(url, headers: headers) end |
#connect_stream_input(voice_id, **options) ⇒ WebSocket::Client::Simple::Client Also known as: connect_single_stream
Creates a WebSocket connection for real-time text-to-speech streaming Documentation: https://elevenlabs.io/docs/api-reference/websockets/text-to-speech
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/elevenlabs_client/endpoints/websocket_text_to_speech.rb', line 29 def connect_stream_input(voice_id, **) endpoint = "/v1/text-to-speech/#{voice_id}/stream-input" # Build query parameters in the same order as provided in options allowed_keys = [:model_id, :language_code, :enable_logging, :enable_ssml_parsing, :output_format, :inactivity_timeout, :sync_alignment, :auto_mode, :apply_text_normalization, :seed] pairs = [] .each do |k, v| next unless allowed_keys.include?(k) next if v.nil? next if (k == :language_code || k == :apply_text_normalization) && v.to_s.empty? pairs << [k, v] end if pairs.any? query_string = pairs.map { |k, v| "#{k}=#{v}" }.join("&") endpoint += "?#{query_string}" end url = "#{@base_url}#{endpoint}" headers = { "xi-api-key" => @client.api_key } WebSocket::Client::Simple.connect(url, headers: headers) end |
#send_close_connection(ws) ⇒ Object
Helper method to close connection for single stream
113 114 115 116 |
# File 'lib/elevenlabs_client/endpoints/websocket_text_to_speech.rb', line 113 def send_close_connection(ws) = { text: "" } ws.send(.to_json) end |
#send_close_context(ws, context_id) ⇒ Object
Helper method to close a specific context
177 178 179 180 181 182 183 184 |
# File 'lib/elevenlabs_client/endpoints/websocket_text_to_speech.rb', line 177 def send_close_context(ws, context_id) = { context_id: context_id, close_context: true } ws.send(.to_json) end |
#send_close_socket(ws) ⇒ Object
Helper method to close the entire socket
200 201 202 203 |
# File 'lib/elevenlabs_client/endpoints/websocket_text_to_speech.rb', line 200 def send_close_socket(ws) = { close_socket: true } ws.send(.to_json) end |
#send_flush_context(ws, context_id) ⇒ Object
Helper method to flush a context
165 166 167 168 169 170 171 172 |
# File 'lib/elevenlabs_client/endpoints/websocket_text_to_speech.rb', line 165 def send_flush_context(ws, context_id) = { context_id: context_id, flush: true } ws.send(.to_json) end |
#send_initialize_connection(ws, **options) ⇒ Object
Helper method to send initialization message for single stream
87 88 89 90 91 92 93 94 95 |
# File 'lib/elevenlabs_client/endpoints/websocket_text_to_speech.rb', line 87 def send_initialize_connection(ws, **) = { text: [:text] || " ", voice_settings: [:voice_settings] || {}, xi_api_key: [:xi_api_key] || @client.api_key } ws.send(.to_json) end |
#send_initialize_connection_multi(ws, context_id, **options) ⇒ Object
Helper method to send initialization message for multi-context stream
122 123 124 125 126 127 128 129 130 |
# File 'lib/elevenlabs_client/endpoints/websocket_text_to_speech.rb', line 122 def send_initialize_connection_multi(ws, context_id, **) = { text: [:text] || " ", voice_settings: [:voice_settings] || {}, context_id: context_id } ws.send(.to_json) end |
#send_initialize_context(ws, context_id, **options) ⇒ Object
Helper method to initialize a new context in multi-stream
136 137 138 139 140 141 142 143 144 145 |
# File 'lib/elevenlabs_client/endpoints/websocket_text_to_speech.rb', line 136 def send_initialize_context(ws, context_id, **) = { context_id: context_id, voice_settings: [:voice_settings] || {} } [:model_id] = [:model_id] if [:model_id] [:language_code] = [:language_code] if [:language_code] ws.send(.to_json) end |
#send_keep_context_alive(ws, context_id) ⇒ Object
Helper method to keep a context alive
189 190 191 192 193 194 195 196 |
# File 'lib/elevenlabs_client/endpoints/websocket_text_to_speech.rb', line 189 def send_keep_context_alive(ws, context_id) = { context_id: context_id, keep_context_alive: true } ws.send(.to_json) end |
#send_text(ws, text, **options) ⇒ Object
Helper method to send text for single stream
103 104 105 106 107 108 109 |
# File 'lib/elevenlabs_client/endpoints/websocket_text_to_speech.rb', line 103 def send_text(ws, text, **) = { text: text } [:try_trigger_generation] = [:try_trigger_generation] unless [:try_trigger_generation].nil? [:voice_settings] = [:voice_settings] if [:voice_settings] ws.send(.to_json) end |
#send_text_multi(ws, context_id, text, **options) ⇒ Object
Helper method to send text for multi-context stream
152 153 154 155 156 157 158 159 160 |
# File 'lib/elevenlabs_client/endpoints/websocket_text_to_speech.rb', line 152 def send_text_multi(ws, context_id, text, **) = { text: text, context_id: context_id } [:flush] = [:flush] unless [:flush].nil? ws.send(.to_json) end |
#stream_text_to_speech(voice_id, text_chunks, **options, &block) ⇒ Object
Convenience method to create a complete streaming session
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/elevenlabs_client/endpoints/websocket_text_to_speech.rb', line 210 def stream_text_to_speech(voice_id, text_chunks, **, &block) ws = connect_stream_input(voice_id, **) ws.on :open do # Initialize connection send_initialize_connection(ws, **) # Send text chunks text_chunks.each_with_index do |chunk, index| send_text(ws, chunk, try_trigger_generation: (index == text_chunks.length - 1)) end # Close connection send_close_connection(ws) end ws.on :message do |msg| data = JSON.parse(msg.data) if data['audio'] && block_given? # Decode base64 audio and yield to block audio_data = Base64.decode64(data['audio']) block.call(audio_data, data) end end ws.on :error do |e| raise APIError, "WebSocket error: #{e.}" end ws end |