ElevenLabs text-to-speech: full audio, audio with character timestamps, and streaming.

Functions

alignment-words

fn (alignment: Any): Vec

Group a character alignment into words: [{word, start, end}], splitting on whitespace. Returns [] for a null alignment.

Example

::elevenlabs::tts/alignment-words({
  characters: ["H", "i", " ", "y", "o"],
  character_start_times_seconds: [0.0, 0.1, 0.2, 0.3, 0.4],
  character_end_times_seconds: [0.1, 0.2, 0.3, 0.4, 0.5]
})  // [{word: "Hi", start: 0.0, end: 0.2}, {word: "yo", start: 0.3, end: 0.5}]
::elevenlabs::tts/alignment-words(null)  // []

decode-timestamped

fn (body: Map): Map

Decode a with-timestamps response body: audio_base64 becomes audio Bytes. Exposed for testing.

Example

out ::elevenlabs::tts/decode-timestamped({audio_base64: "aGk=", alignment: null})
Str(out.audio)  // "hi"

text-to-speech

fn (voice-id: Str, request: TextToSpeechRequest): Bytes
fn (voice-id: Str, request: TextToSpeechRequest, output: Any): Bytes
fn (voice-id: Str, request: TextToSpeechRequest, output: Any, opts: Map): Bytes

POST /v1/text-to-speech/{voice_id}: convert text to speech. Returns the audio as Bytes (mp3 44.1kHz 128kbps by default). The optional third argument is an output format such as "mp3_44100_192", "pcm_16000", "opus_48000_64", or "ulaw_8000" (telephony), or a Map of query params (output_format, enable_logging).

"eleven_multilingual_v2" is the quality model, "eleven_flash_v2_5" the low-latency one, "eleven_v3" the most expressive.

Example

::tts ::elevenlabs::tts

audio ::tts/text-to-speech(::elevenlabs/GEORGE_VOICE_ID, ::tts/TextToSpeechRequest({
  text: "It works!",
  model_id: "eleven_multilingual_v2",
  voice_settings: {stability: 0.5, similarity_boost: 0.75}
}))
::hot::file/write-file-bytes("speech.mp3", audio)
Context Vars: elevenlabs.api.key

text-to-speech-stream

fn (voice-id: Str, request: TextToSpeechRequest): ::hot::http/StreamingHttpResponse
fn (voice-id: Str, request: TextToSpeechRequest, output: Any): ::hot::http/StreamingHttpResponse
fn (voice-id: Str, request: TextToSpeechRequest, output: Any, opts: Map): ::hot::http/StreamingHttpResponse

POST /v1/text-to-speech/{voice_id}/stream: stream speech as it is generated. Returns a StreamingHttpResponse whose body iterates raw audio chunks (Bytes); collect them or forward them as they arrive. Takes the same optional output format as text-to-speech.

Example

response ::elevenlabs::tts/text-to-speech-stream(::elevenlabs/GEORGE_VOICE_ID, ::elevenlabs::tts/TextToSpeechRequest({
  text: "A long article, read aloud as it is generated.",
  model_id: "eleven_flash_v2_5"
}))
chunks collect(response.body)
Context Vars: elevenlabs.api.key

text-to-speech-with-timestamps

fn (voice-id: Str, request: TextToSpeechRequest): Map
fn (voice-id: Str, request: TextToSpeechRequest, output: Any): Map
fn (voice-id: Str, request: TextToSpeechRequest, output: Any, opts: Map): Map

POST /v1/text-to-speech/{voice_id}/with-timestamps: speech plus character-level timing. Returns {audio, alignment, normalized_alignment} where audio is the decoded Bytes and each alignment is {characters, character_start_times_seconds, character_end_times_seconds}. alignment-words turns one into word timings for captions.

Example

::tts ::elevenlabs::tts

out ::tts/text-to-speech-with-timestamps(::elevenlabs/GEORGE_VOICE_ID, ::tts/TextToSpeechRequest({text: "Hello there."}))
::hot::file/write-file-bytes("hello.mp3", out.audio)
::tts/alignment-words(out.alignment)  // [{word: "Hello", start: 0.0, end: 0.35}, ...]
Context Vars: elevenlabs.api.key

tts-path

fn (voice-id: Str, output: Any): Str
fn (voice-id: Str, suffix: Str, output: Any): Str

/v1/text-to-speech/{voice_id}{suffix} with the voice id encoded and the query from tts-query. Exposed for testing.

Example

::elevenlabs::tts/tts-path("JBFqnCBsd6RMkjVDRZzb", "ulaw_8000")
// "/v1/text-to-speech/JBFqnCBsd6RMkjVDRZzb?output_format=ulaw_8000"
::elevenlabs::tts/tts-path("JBFqnCBsd6RMkjVDRZzb", "/stream", null)
// "/v1/text-to-speech/JBFqnCBsd6RMkjVDRZzb/stream"

tts-query

fn (output: Any): Map

Query params for a TTS call: a Str is the output_format (e.g. "mp3_44100_128", "pcm_16000", "ulaw_8000"); a Map passes output_format, enable_logging, optimize_streaming_latency as given; null means none. Exposed for testing.

Example

::elevenlabs::tts/tts-query("ulaw_8000")  // {output_format: "ulaw_8000"}
::elevenlabs::tts/tts-query(null)  // {}

Types

TextToSpeechRequest

TextToSpeechRequest type {
    text: Str,
    model_id: Str?,
    voice_settings: Map?,
    language_code: Str?,
    pronunciation_dictionary_locators: Vec?,
    seed: Int?,
    previous_text: Str?,
    next_text: Str?,
    previous_request_ids: Vec?,
    next_request_ids: Vec?,
    apply_text_normalization: Str?,
    apply_language_text_normalization: Bool?
}

Body of the text-to-speech endpoints. Only text is required; unset fields are omitted from the request. model_id defaults to "eleven_multilingual_v2" server-side. voice_settings takes {stability, similarity_boost, style, use_speaker_boost, speed}. previous_request_ids/next_request_ids (up to 3) and previous_text/next_text keep prosody continuous across chunks.