Class: Telnyx::Lib::WebSocket::SpeechToTextWS

Inherits:
Base
  • Object
show all
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.message}"
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

Attributes inherited from Base

#ready_state, #url

Instance Method Summary collapse

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.

Parameters:

  • client (Telnyx::Client)

    The Telnyx client

  • params (Hash, SpeechToTextStreamParams) (defaults to: nil)

    Stream configuration parameters

  • options (Hash) (defaults to: {})

    Additional WebSocket options

Options Hash (options):

  • :headers (Hash)

    Additional HTTP headers



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, options = {})
  super()
  @client = client
  @params = normalize_params(params)
  @options = options

  @url = build_url(client, API_PATH, @params&.to_hash)
  connect
end

Instance Attribute Details

#clientTelnyx::Client (readonly)

Returns The Telnyx client.

Returns:



52
53
54
# File 'lib/telnyx/lib/websocket/speech_to_text_ws.rb', line 52

def client
  @client
end

#paramsSpeechToTextStreamParams (readonly)

Returns The stream parameters.

Returns:



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.

Parameters:

  • data (String)

    Raw audio bytes (mp3, wav, or raw format)

Raises:



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