Class: Telnyx::Resources::TextToSpeech

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

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:



161
162
163
# File 'lib/telnyx/resources/text_to_speech.rb', line 161

def initialize(client:)
  @client = client
end

Instance Method Details

#generate(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, request_options: {}) ⇒ Telnyx::Models::TextToSpeechGenerateResponse

Some parameter documentations has been truncated, see Models::TextToSpeechGenerateParams 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`). Alternatively, specify `provider` explicitly along with provider-specific parameters.

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

Parameters:

Returns:

See Also:



61
62
63
64
65
66
67
68
69
70
# File 'lib/telnyx/resources/text_to_speech.rb', line 61

def generate(params = {})
  parsed, options = Telnyx::TextToSpeechGenerateParams.dump_request(params)
  @client.request(
    method: :post,
    path: "text-to-speech/speech",
    body: parsed,
    model: Telnyx::Models::TextToSpeechGenerateResponse,
    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:



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/telnyx/resources/text_to_speech.rb', line 89

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

#stream(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::TextToSpeechStreamParams 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`.

**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.

Parameters:

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

    Audio output format override. Supported for Telnyx ‘Natural`/`NaturalHD` models

  • 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` (Teln

  • provider (Symbol, Telnyx::Models::TextToSpeechStreamParams::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:



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/telnyx/resources/text_to_speech.rb', line 146

def stream(params = {})
  parsed, options = Telnyx::TextToSpeechStreamParams.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