an.audio.elevenlabs_tts#

ElevenLabsTTS — real speech via the ElevenLabs API. Requires ELEVEN_API_KEY.

Lazily imports the elevenlabs SDK so the rest of an works without it. If you want real speech, pip install elevenlabs and set ELEVEN_API_KEY (or ELEVENLABS_API_KEY) in your environment.

Expressive voices (an#209). A voice document in the voices store may declare, beside voice_id, the model_id it speaks with, its voice_settings and a sampling seed; a dialogue line’s [emotion] and {direction} reach a model that takes inline audio tags (eleven_v3, eleven_v4 and their variants) as [excited] Hi!. Which models those are is voxy’s knowledge, not this module’s (an#822): voxy.dialect_for("elevenlabs", model_id) names each model’s dialect, and a model outside the inline-tags one still honours a line’s pause cues ({beat}, {pause}) as real silence, rendered by voxy.direction.plan_directions; its other cues are dropped with a warning. ElevenLabsTTS.synthesis_options() turns those into the keyword arguments ElevenLabsTTS.synthesize() takes — the audio pipeline keys its cache on exactly that dict, and a voice declaring none of them yields {}, so no existing cache key moves.

>>> tts = ElevenLabsTTS(api_key="unused")
>>> tts.synthesis_options({})
{}
>>> tts.synthesis_options({"model_id": "eleven_v3"}, emotion="happy", direction=["sighs"])
{'model_id': 'eleven_v3', 'audio_tags': ['happy', 'sighs']}
>>> tts.synthesis_options({"voice_settings": {"stability": 0.3, "speed": 1.1}})
{'voice_settings': {'speed': 1.1, 'stability': 0.3}}
>>> tagged_text("Hi!", ["excited"])
'[excited] Hi!'

Module Attributes

ELEVENLABS_STOCK_CATEGORY

The category ElevenLabs gives its stock voices, the ones any account holds and nobody designed: VoiceMeta.is_stock (an#712).

VOICE_SETTINGS_RANGES

The voice_settings keys the API takes, with the range each accepts (None = a boolean).

Functions

model_dialect(model_id, *[, backend])

How backend's model model_id takes delivery direction: voxy's answer.

model_native_break_s(model_id)

The longest SSML <break> ElevenLabs model model_id renders itself, in seconds: voxy's table (0 for a model it does not list, and for v3/v4, which take no SSML).

normalize_voice_settings(raw)

The canonical voice_settings dict — only what was declared, key-sorted.

tagged_text(text, tags)

text with each tag prefixed as [tag] — what an audio-tag model reads.

takes_audio_tags(model_id, *[, backend])

Whether model_id reads inline audio tags.

Classes

ElevenLabsTTS(*[, api_key, model_id, ...])

ElevenLabs-backed TTSProvider.

Exceptions

ElevenLabsUnavailableError

A request cannot be sent: no API key is configured.

ElevenLabsVoiceError

A voice document declares ElevenLabs settings that are malformed.

an.audio.elevenlabs_tts.ELEVENLABS_STOCK_CATEGORY: str = 'premade'#

The category ElevenLabs gives its stock voices, the ones any account holds and nobody designed: VoiceMeta.is_stock (an#712). A voice an account added from ElevenLabs’ shared Voice Library has another category, and is not stock.

class an.audio.elevenlabs_tts.ElevenLabsTTS(*, api_key=None, model_id='eleven_turbo_v2_5', output_format='mp3_44100_128', dialect=<function model_dialect>, native_break_s=<function model_native_break_s>, client_factory=None)[source]#

Bases: object

ElevenLabs-backed TTSProvider. Constructor takes an optional api_key (falls back to ELEVEN_API_KEY / ELEVENLABS_API_KEY).

Implements the TTSProvider protocol, plus the optional synthesis_options hook the audio pipeline reads (an#209).

billed: bool = True#

what may never run in memory).

Type:

Every request is charged (an#311

billed_characters(text, *, audio_tags=None, model_id=None, **_options)[source]#

Characters one request bills: the text as sent, audio tags included.

>>> tts = ElevenLabsTTS(api_key="unused")
>>> tts.billed_characters("Hi!", audio_tags=["excited"], model_id="eleven_v3")
13
Return type:

int

check_available()[source]#

Raise ElevenLabsUnavailableError when a request could not be sent (no API key) — what the audio pipeline asks before the first request of a render, so nothing is half-synthesized (an#305).

Return type:

None

client_factory#

api_key -> client; tests inject a fake so nothing reaches the API.

dialect#

how a model takes direction (voxy’s registry).

Type:

model_id -> Dialect

list_voices(*, search=None)[source]#

The account’s voices (its own plus the ones it saved), newest API.

search filters by name, description and labels on the server. An absent key or SDK yields []; a key that is present and a call that fails RAISES — an empty listing must mean “no voices”, not “it broke”.

Return type:

Iterable[VoiceMeta]

native_break_s#

the longest SSML break a model renders (voxy’s table).

Type:

model_id -> seconds

synthesis_options(voice, *, emotion=None, direction=None)[source]#

The synthesize keyword arguments a voice document and a line imply.

Only what is declared appears ({} for a plain voice), so the audio cache key — which includes this dict when it is non-empty — is unchanged for every voice that declares nothing new. audio_tags are the line’s [emotion] (unless neutral) then its direction cues, all of them on a model that reads tags. Elsewhere only the direction’s pause cues are kept (sent as SSML breaks, when the model renders a break that long: voxy’s table), any other direction cue is dropped with a warning, and the emotion stays a face-only cue, as before.

Return type:

dict[str, Any]

synthesize(text, voice_id=None, *, model_id=None, voice_settings=None, seed=None, audio_tags=None, **kw)[source]#

Speak text. The clip’s transcript is text WITHOUT the tags, so alignment and captions never read a cue.

Return type:

AudioClip

take_options(options, take)[source]#

The request for candidate take of a best-of-N line (an.audio.takes).

A declared seed is offset by the take, so a model that does honour it (every model but eleven_v3, measured) still returns different takes; without a seed the request is unchanged and the model’s own sampling varies the take.

>>> ElevenLabsTTS(api_key="unused").take_options({"seed": 11}, 2)
{'seed': 13}
Return type:

dict[str, Any]

exception an.audio.elevenlabs_tts.ElevenLabsUnavailableError[source]#

Bases: RuntimeError

A request cannot be sent: no API key is configured.

exception an.audio.elevenlabs_tts.ElevenLabsVoiceError[source]#

Bases: ValueError

A voice document declares ElevenLabs settings that are malformed.

an.audio.elevenlabs_tts.VOICE_SETTINGS_RANGES: dict[str, tuple[float, float] | None] = {'similarity_boost': (0.0, 1.0), 'speed': (0.7, 1.2), 'stability': (0.0, 1.0), 'style': (0.0, 1.0), 'use_speaker_boost': None}#

The voice_settings keys the API takes, with the range each accepts (None = a boolean). speed is the API’s documented 0.7–1.2.

an.audio.elevenlabs_tts.model_dialect(model_id, *, backend='elevenlabs')[source]#

How backend’s model model_id takes delivery direction: voxy’s answer.

>>> model_dialect("eleven_v3").name, model_dialect("eleven_turbo_v2_5").name
('inline-tags', 'plain')
Return type:

Dialect

an.audio.elevenlabs_tts.model_native_break_s(model_id)[source]#

The longest SSML <break> ElevenLabs model model_id renders itself, in seconds: voxy’s table (0 for a model it does not list, and for v3/v4, which take no SSML).

>>> model_native_break_s("eleven_turbo_v2_5"), model_native_break_s("eleven_v3")
(3.0, 0.0)
Return type:

float

an.audio.elevenlabs_tts.normalize_voice_settings(raw)[source]#

The canonical voice_settings dict — only what was declared, key-sorted.

Omit-when-unset: None and {} give {}. Unknown keys and values out of range raise, so a typo cannot silently fall back to the account default.

>>> normalize_voice_settings({"style": 1, "stability": 0.25})
{'stability': 0.25, 'style': 1.0}
>>> normalize_voice_settings({"stabilty": 0.5})
Traceback (most recent call last):
    ...
an.audio.elevenlabs_tts.ElevenLabsVoiceError: unknown voice_settings key(s) ['stabilty']; known: ['similarity_boost', 'speed', 'stability', 'style', 'use_speaker_boost']
Return type:

dict[str, Any]

an.audio.elevenlabs_tts.tagged_text(text, tags)[source]#

text with each tag prefixed as [tag] — what an audio-tag model reads.

Return type:

str

an.audio.elevenlabs_tts.takes_audio_tags(model_id, *, backend='elevenlabs')[source]#

Whether model_id reads inline audio tags.

>>> takes_audio_tags("eleven_v3"), takes_audio_tags("eleven_v4_turbo")
(True, True)
>>> takes_audio_tags("eleven_turbo_v2_5")
False
Return type:

bool