Class: Telnyx::Lib::WebSocket::SpeechToTextWS
- Defined in:
- lib/telnyx/lib/websocket/speech_to_text_ws.rb
Overview
WebSocket client for Speech-to-Text (STT) streaming transcription.
This client establishes a WebSocket connection to the Telnyx STT API for real-time audio transcription. Audio data is sent as binary frames and transcription results are received as JSON events.
Example usage:
client = Telnyx::Client.new(api_key: ENV["TELNYX_API_KEY"])
ws = Telnyx::Lib::WebSocket::SpeechToTextWS.new(client, {
transcription_engine: "Deepgram",
language: "en-US",
input_format: "wav"
})
ws.on(:transcript) do |event|
puts "Transcript: #{event.transcript}" if event.is_final
end
ws.on(:error) do |error|
puts "Error: #{error.}"
end
ws.wait_for_open
# Send audio data
File.open("audio.wav", "rb") do |f|
while (chunk = f.read(4096))
ws.send(chunk)
end
end
ws.close
Constant Summary collapse
- API_PATH =
The WebSocket API path for STT
"/v2/speech-to-text/transcription"
Constants inherited from Base
Base::CLOSED, Base::CLOSING, Base::CONNECTING, Base::OPEN
Instance Attribute Summary collapse
-
#client ⇒ Telnyx::Client
readonly
The Telnyx client.
-
#params ⇒ SpeechToTextStreamParams
readonly
The stream parameters.
Attributes inherited from Base
Instance Method Summary collapse
-
#initialize(client, params = nil, options = {}) ⇒ SpeechToTextWS
constructor
Create a new STT WebSocket connection.
-
#send(data) ⇒ void
Send binary audio data to the server for transcription.
Methods inherited from Base
#close, #closed?, #connecting?, #off, #on, #open?, #wait_for_open
Constructor Details
#initialize(client, params = nil, options = {}) ⇒ SpeechToTextWS
Create a new STT WebSocket connection.
63 64 65 66 67 68 69 70 71 |
# File 'lib/telnyx/lib/websocket/speech_to_text_ws.rb', line 63 def initialize(client, params = nil, = {}) super() @client = client @params = normalize_params(params) @options = @url = build_url(client, API_PATH, @params&.to_hash) connect end |
Instance Attribute Details
#client ⇒ Telnyx::Client (readonly)
Returns The Telnyx client.
52 53 54 |
# File 'lib/telnyx/lib/websocket/speech_to_text_ws.rb', line 52 def client @client end |
#params ⇒ SpeechToTextStreamParams (readonly)
Returns The stream parameters.
55 56 57 |
# File 'lib/telnyx/lib/websocket/speech_to_text_ws.rb', line 55 def params @params end |
Instance Method Details
#send(data) ⇒ void
This method returns an undefined value.
Send binary audio data to the server for transcription.
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/telnyx/lib/websocket/speech_to_text_ws.rb', line 78 def send(data) unless open? raise WebSocketError.new("Cannot send: WebSocket is not open") end begin @socket.send(data, type: :binary) rescue StandardError => e emit_error(nil, "could not send audio data", e) end end |