Class: Telnyx::Lib::WebSocket::TtsServerEvent
- Inherits:
-
Object
- Object
- Telnyx::Lib::WebSocket::TtsServerEvent
- Defined in:
- lib/telnyx/lib/websocket/tts_server_event.rb
Overview
Server event types for Text-to-Speech WebSocket streaming.
These events are received from the Telnyx TTS WebSocket API during real-time speech synthesis.
Example usage:
ws.on(:audio_chunk) do |event|
audio_data = Base64.decode64(event.audio)
# Process audio chunk
end
ws.on(:final) do |event|
puts "Stream complete"
end
Instance Attribute Summary collapse
-
#audio ⇒ String?
Base64-encoded audio data.
-
#cached ⇒ Boolean?
Whether the response was served from cache.
-
#encoding ⇒ String?
Audio encoding format (e.g., “mp3”, “pcm”).
-
#error ⇒ String?
Error message if type is “error”.
-
#is_final ⇒ Boolean?
Whether this is the final event.
-
#raw ⇒ Hash?
Original raw event data.
-
#sample_rate ⇒ Integer?
Sample rate in Hz.
-
#sequence ⇒ Integer?
Sequence number for ordering.
-
#text ⇒ String?
The text being synthesized.
-
#time_to_first_audio_frame_ms ⇒ Integer?
Time in milliseconds to first audio frame.
-
#type ⇒ String
The event type (“audio_chunk”, “final”, or “error”).
Class Method Summary collapse
-
.from_hash(data) ⇒ TtsServerEvent
Create a TtsServerEvent from a parsed JSON hash.
Instance Method Summary collapse
-
#audio_chunk? ⇒ Boolean
Check if this is an audio chunk.
-
#decode_audio ⇒ String?
Decode the audio data from Base64.
-
#error? ⇒ Boolean
Check if this is an error event.
-
#final? ⇒ Boolean
Check if this is the final event.
Instance Attribute Details
#audio ⇒ String?
Returns Base64-encoded audio data.
27 28 29 |
# File 'lib/telnyx/lib/websocket/tts_server_event.rb', line 27 def audio @audio end |
#cached ⇒ Boolean?
Returns Whether the response was served from cache.
36 37 38 |
# File 'lib/telnyx/lib/websocket/tts_server_event.rb', line 36 def cached @cached end |
#encoding ⇒ String?
Returns Audio encoding format (e.g., “mp3”, “pcm”).
45 46 47 |
# File 'lib/telnyx/lib/websocket/tts_server_event.rb', line 45 def encoding @encoding end |
#error ⇒ String?
Returns Error message if type is “error”.
42 43 44 |
# File 'lib/telnyx/lib/websocket/tts_server_event.rb', line 42 def error @error end |
#is_final ⇒ Boolean?
Returns Whether this is the final event.
33 34 35 |
# File 'lib/telnyx/lib/websocket/tts_server_event.rb', line 33 def is_final @is_final end |
#raw ⇒ Hash?
Returns Original raw event data.
54 55 56 |
# File 'lib/telnyx/lib/websocket/tts_server_event.rb', line 54 def raw @raw end |
#sample_rate ⇒ Integer?
Returns Sample rate in Hz.
48 49 50 |
# File 'lib/telnyx/lib/websocket/tts_server_event.rb', line 48 def sample_rate @sample_rate end |
#sequence ⇒ Integer?
Returns Sequence number for ordering.
51 52 53 |
# File 'lib/telnyx/lib/websocket/tts_server_event.rb', line 51 def sequence @sequence end |
#text ⇒ String?
Returns The text being synthesized.
30 31 32 |
# File 'lib/telnyx/lib/websocket/tts_server_event.rb', line 30 def text @text end |
#time_to_first_audio_frame_ms ⇒ Integer?
Returns Time in milliseconds to first audio frame.
39 40 41 |
# File 'lib/telnyx/lib/websocket/tts_server_event.rb', line 39 def time_to_first_audio_frame_ms @time_to_first_audio_frame_ms end |
#type ⇒ String
Returns The event type (“audio_chunk”, “final”, or “error”).
24 25 26 |
# File 'lib/telnyx/lib/websocket/tts_server_event.rb', line 24 def type @type end |
Class Method Details
.from_hash(data) ⇒ TtsServerEvent
Create a TtsServerEvent from a parsed JSON hash
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/telnyx/lib/websocket/tts_server_event.rb', line 60 def self.from_hash(data) event = new event.raw = data event.type = data["type"] event.audio = data["audio"] event.text = data["text"] event.is_final = data["is_final"] event.cached = data["cached"] event.time_to_first_audio_frame_ms = data["time_to_first_audio_frame_ms"] event.error = data["error"] event.encoding = data["encoding"] event.sample_rate = data["sample_rate"] event.sequence = data["sequence"] event end |
Instance Method Details
#audio_chunk? ⇒ Boolean
Check if this is an audio chunk
93 94 95 |
# File 'lib/telnyx/lib/websocket/tts_server_event.rb', line 93 def audio_chunk? type == "audio_chunk" end |
#decode_audio ⇒ String?
Decode the audio data from Base64
100 101 102 103 104 |
# File 'lib/telnyx/lib/websocket/tts_server_event.rb', line 100 def decode_audio return nil unless audio require("base64") Base64.decode64(audio) end |
#error? ⇒ Boolean
Check if this is an error event
86 87 88 |
# File 'lib/telnyx/lib/websocket/tts_server_event.rb', line 86 def error? type == "error" end |
#final? ⇒ Boolean
Check if this is the final event
79 80 81 |
# File 'lib/telnyx/lib/websocket/tts_server_event.rb', line 79 def final? type == "final" || is_final == true end |