Class: Telnyx::Resources::TextToSpeech

Inherits:
Object
  • Object
show all
Defined in:
lib/telnyx/resources/text_to_speech.rb

Overview

Text to speech streaming command operations

Instance Method Summary collapse

Constructor Details

#initialize(client:) ⇒ TextToSpeech

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of TextToSpeech.

Parameters:



171
172
173
# File 'lib/telnyx/resources/text_to_speech.rb', line 171

def initialize(client:)
  @client = client
end

Instance Method Details

#generate_speech(aws: nil, azure: nil, disable_cache: nil, elevenlabs: nil, language: nil, minimax: nil, output_type: nil, provider: nil, resemble: nil, rime: nil, telnyx: nil, text: nil, text_type: nil, voice: nil, voice_settings: nil, xai: nil, request_options: {}) ⇒ Telnyx::Models::TextToSpeechGenerateSpeechResponse

Some parameter documentations has been truncated, see Models::TextToSpeechGenerateSpeechParams for more details.

Generate synthesized speech audio from text input. Returns audio in the requested format (binary audio stream, base64-encoded JSON, or an audio URL for later retrieval).

Authentication is provided via the standard ‘Authorization: Bearer <API_KEY>` header.

The ‘voice` parameter provides a convenient shorthand to specify provider, model, and voice in a single string (e.g. `telnyx.NaturalHD.Alloy` or `Telnyx.Ultra.<voice_id>`). Alternatively, specify `provider` explicitly along with provider-specific parameters.

Supported providers: ‘aws`, `telnyx`, `azure`, `elevenlabs`, `minimax`, `rime`, `resemble`, `xai`.

The Telnyx ‘Ultra` model supports 44 languages with emotion control, speed adjustment, and volume control. Use the `telnyx` provider-specific parameters to configure these features.

Parameters:

Returns:

See Also:



68
69
70
71
72
73
74
75
76
77
# File 'lib/telnyx/resources/text_to_speech.rb', line 68

def generate_speech(params = {})
  parsed, options = Telnyx::TextToSpeechGenerateSpeechParams.dump_request(params)
  @client.request(
    method: :post,
    path: "text-to-speech/speech",
    body: parsed,
    model: Telnyx::Models::TextToSpeechGenerateSpeechResponse,
    options: options
  )
end

#list_voices(api_key: nil, provider: nil, request_options: {}) ⇒ Telnyx::Models::TextToSpeechListVoicesResponse

Retrieve a list of available voices from one or all TTS providers. When ‘provider` is specified, returns voices for that provider only. Otherwise, returns voices from all providers.

Some providers (ElevenLabs, Resemble) require an API key to list voices.

Parameters:

Returns:

See Also:



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/telnyx/resources/text_to_speech.rb', line 96

def list_voices(params = {})
  parsed, options = Telnyx::TextToSpeechListVoicesParams.dump_request(params)
  query = Telnyx::Internal::Util.encode_query_params(parsed)
  @client.request(
    method: :get,
    path: "text-to-speech/voices",
    query: query,
    model: Telnyx::Models::TextToSpeechListVoicesResponse,
    options: options
  )
end

#retrieve_speech(audio_format: nil, disable_cache: nil, model_id: nil, provider: nil, socket_id: nil, voice: nil, voice_id: nil, request_options: {}) ⇒ nil

Some parameter documentations has been truncated, see Models::TextToSpeechRetrieveSpeechParams for more details.

Open a WebSocket connection to stream text and receive synthesized audio in real time. Authentication is provided via the standard ‘Authorization: Bearer <API_KEY>` header. Send JSON frames with text to synthesize; receive JSON frames containing base64-encoded audio chunks.

Supported providers: ‘aws`, `telnyx`, `azure`, `murfai`, `minimax`, `rime`, `resemble`, `elevenlabs`, `xai`.

**Connection flow:**

  1. Open WebSocket with query parameters specifying provider, voice, and model.

  2. Send an initial handshake message ‘“ ”` (single space) with optional `voice_settings` to initialize the session.

  3. Send text messages as ‘“Hello world”`.

  4. Receive audio chunks as JSON frames with base64-encoded audio.

  5. A final frame with ‘isFinal: true` indicates the end of audio for the current text.

To interrupt and restart synthesis mid-stream, send ‘true` — the current worker is stopped and a new one is started.

Note: The Telnyx ‘Ultra` model is not available over WebSocket. Use the HTTP POST `/text-to-speech/speech` endpoint instead.

Parameters:

  • audio_format (Symbol, Telnyx::Models::TextToSpeechRetrieveSpeechParams::AudioFormat)

    Audio output format override. Supported for Telnyx models. ‘pcm` and `wav` are a

  • disable_cache (Boolean)

    When ‘true`, bypass the audio cache and generate fresh audio.

  • model_id (String)

    Model identifier for the chosen provider. Examples: ‘Natural`, `NaturalHD`, `Ult

  • provider (Symbol, Telnyx::Models::TextToSpeechRetrieveSpeechParams::Provider)

    TTS provider. Defaults to ‘telnyx` if not specified. Ignored when `voice` is pro

  • socket_id (String)

    Client-provided socket identifier for tracking. If not provided, one is generate

  • voice (String)

    Voice identifier in the format ‘provider.model_id.voice_id` or `provider.voice_i

  • voice_id (String)

    Voice identifier for the chosen provider.

  • request_options (Telnyx::RequestOptions, Hash{Symbol=>Object}, nil)

Returns:

  • (nil)

See Also:



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/telnyx/resources/text_to_speech.rb', line 156

def retrieve_speech(params = {})
  parsed, options = Telnyx::TextToSpeechRetrieveSpeechParams.dump_request(params)
  query = Telnyx::Internal::Util.encode_query_params(parsed)
  @client.request(
    method: :get,
    path: "text-to-speech/speech",
    query: query,
    model: NilClass,
    options: options
  )
end