Skip to content

The animation engine for agents: blazingly fast math and 3D videos with Manim's Python API, on a Rust GPU core.

Python

Add speech audio provider

Sina Atalay75555e38e8fd
diff --git a/src/manimgx/audio/fal.py b/src/manimgx/audio/fal.py
new file mode 100644
index 00000000..13fd72d0
--- /dev/null
+++ b/src/manimgx/audio/fal.py
@@ -0,0 +1,623 @@
+"""fal.ai's text-to-speech models, as voices: `Fal(model, **settings)`.
+
+[fal](https://fal.ai) runs text-to-speech models behind one API and one key. `Fal` speaks
+through the five most popular there (fal's "Trending" order, September 2026), one from each
+maker:
+
+| Model | Its settings | Words timed |
+| --- | --- | --- |
+| `fal-ai/elevenlabs/tts/eleven-v3` (the default) | [`ElevenV3`][manimgx.audio.fal.ElevenV3] | by the model |
+| `fal-ai/minimax/speech-2.8-hd` | [`MiniMax`][manimgx.audio.fal.MiniMax] | estimated |
+| `google/gemini-3.8-flash-tts` | [`Gemini`][manimgx.audio.fal.Gemini] | estimated |
+| `fal-ai/inworld-tts` | [`Inworld`][manimgx.audio.fal.Inworld] | estimated |
+| `fal-ai/qwen-3-tts/text-to-speech/1.7b` | [`Qwen`][manimgx.audio.fal.Qwen] | estimated |
+
+A model takes settings of its own, typed per model: a type checker knows which settings a
+model takes and which values (its voices, say), and `Fal` checks their names as it is made.
+The key is `FAL_KEY`'s (fal's dashboard gives one), or `key=`; it never names the voice in the
+cache.
+"""
+
+import json
+import os
+import urllib.error
+import urllib.request
+from dataclasses import dataclass, field
+from typing import Literal, TypedDict, Unpack, overload
+
+from manimgx.audio.sound import Speech, timed
+
+# ── the models' settings, as their schemas on fal have them ────────────────────────────────
+
+
+class ElevenV3(TypedDict, total=False):
+ """The settings of ElevenLabs' Eleven v3 (`fal-ai/elevenlabs/tts/eleven-v3`), which times
+ its words. Its text may carry audio tags: `[whispers]`, `[laughs]`, `[excited]`…"""
+
+ voice: str
+ """A voice's name or ID in ElevenLabs' library: Aria, Roger, Sarah, Laura, Charlie,
+ George, Callum, River, Liam, Charlotte, Alice, Matilda, Will, Jessica, Eric, Chris, Brian,
+ Daniel, Lily, Bill… (default "Rachel")."""
+ stability: float
+ """How steady the voice is, from 0 (expressive) to 1 (steady) (default 0.5)."""
+ language_code: str | None
+ """The language, as an ISO 639-1 code ("en", "de"…) (default: the text's)."""
+ apply_text_normalization: Literal["auto", "on", "off"]
+ """Whether numbers and the like are spelled out before they are said (default "auto":
+ as the model sees fit)."""
+
+
+class MiniMaxVoice(TypedDict, total=False):
+ """A MiniMax voice and how it speaks."""
+
+ voice_id: str
+ """The voice: Wise_Woman, Friendly_Person, Inspirational_girl, Deep_Voice_Man, Calm_Woman,
+ Casual_Guy, Lively_Girl, Patient_Man, Young_Knight, Determined_Man, Lovely_Girl,
+ Decent_Boy, Imposing_Manner, Elegant_Man, Abbess, Sweet_Girl_2, Exuberant_Girl, or a
+ cloned voice's ID (default "Wise_Woman")."""
+ speed: float
+ """How fast, from 0.5 to 2 (default 1)."""
+ vol: float
+ """How loud, from 0.01 to 10 (default 1)."""
+ pitch: int
+ """How high, in semitones from -12 to 12 (default 0)."""
+ emotion: (
+ Literal["happy", "sad", "angry", "fearful", "disgusted", "surprised", "neutral"]
+ | None
+ )
+ """The emotion it speaks with (default: as the text reads)."""
+ english_normalization: bool
+ """Whether English numbers are read more carefully, a little slower (default False)."""
+
+
+class MiniMaxAudio(TypedDict, total=False):
+ """The audio MiniMax makes."""
+
+ sample_rate: Literal[8000, 16000, 22050, 24000, 32000, 44100]
+ """Samples a second (default 32000)."""
+ bitrate: Literal[32000, 64000, 128000, 256000]
+ """Bits a second, for MP3 (default 128000)."""
+ format: Literal["mp3", "flac"]
+ """The file's format (default "mp3")."""
+ channel: Literal[1, 2]
+ """Channels: 1 (mono) or 2 (stereo) (default 1)."""
+
+
+class MiniMaxLoudness(TypedDict, total=False):
+ """How MiniMax evens out the audio's loudness."""
+
+ enabled: bool
+ """Whether it does (default True)."""
+ target_loudness: float
+ """The loudness it aims at, in LUFS, from -70 to -10 (default -18)."""
+ target_range: float
+ """The loudness range, in LU, from 0 to 20 (default 8)."""
+ target_peak: float
+ """The highest peak, in dBTP, from -3 to 0 (default -0.5)."""
+
+
+class MiniMaxTimbre(TypedDict, total=False):
+ """How MiniMax changes the voice itself."""
+
+ pitch: int
+ """Higher or lower, from -100 to 100 (default 0)."""
+ intensity: int
+ """More or less energetic, from -100 to 100 (default 0)."""
+ timbre: int
+ """Its tone's color, from -100 to 100 (default 0)."""
+
+
+class MiniMaxPronunciation(TypedDict, total=False):
+ """How MiniMax pronounces particular words."""
+
+ tone_list: list[str]
+ """Each word and its pronunciation: `"text/(pronunciation)"` (Chinese tones 1 to 5:
+ `"燕少飞/(yan4)(shao3)(fei1)"`)."""
+
+
+class MiniMax(TypedDict, total=False):
+ """The settings of MiniMax Speech 2.8 HD (`fal-ai/minimax/speech-2.8-hd`). Its text may
+ carry pauses, `<#0.5#>` (in seconds), and interjections: `(laughs)`, `(sighs)`, `(coughs)`,
+ `(clears throat)`, `(gasps)`, `(sniffs)`, `(groans)`, `(yawns)`."""
+
+ voice_setting: MiniMaxVoice
+ """The voice and how it speaks ([`MiniMaxVoice`][manimgx.audio.fal.MiniMaxVoice])."""
+ audio_setting: MiniMaxAudio
+ """The audio it makes ([`MiniMaxAudio`][manimgx.audio.fal.MiniMaxAudio])."""
+ language_boost: (
+ Literal[
+ "Chinese",
+ "Chinese,Yue",
+ "English",
+ "Arabic",
+ "Russian",
+ "Spanish",
+ "French",
+ "Portuguese",
+ "German",
+ "Turkish",
+ "Dutch",
+ "Ukrainian",
+ "Vietnamese",
+ "Indonesian",
+ "Japanese",
+ "Italian",
+ "Korean",
+ "Thai",
+ "Polish",
+ "Romanian",
+ "Greek",
+ "Czech",
+ "Finnish",
+ "Hindi",
+ "Bulgarian",
+ "Danish",
+ "Hebrew",
+ "Malay",
+ "Slovak",
+ "Swedish",
+ "Croatian",
+ "Hungarian",
+ "Norwegian",
+ "Slovenian",
+ "Catalan",
+ "Nynorsk",
+ "Afrikaans",
+ "auto",
+ ]
+ | None
+ )
+ """A language or dialect it listens for (default: none)."""
+ normalization_setting: MiniMaxLoudness
+ """How it evens out the loudness
+ ([`MiniMaxLoudness`][manimgx.audio.fal.MiniMaxLoudness])."""
+ voice_modify: MiniMaxTimbre | None
+ """How it changes the voice itself ([`MiniMaxTimbre`][manimgx.audio.fal.MiniMaxTimbre])."""
+ pronunciation_dict: MiniMaxPronunciation | None
+ """How it pronounces particular words
+ ([`MiniMaxPronunciation`][manimgx.audio.fal.MiniMaxPronunciation])."""
+
+
+class Gemini(TypedDict, total=False):
+ """The settings of Google's Gemini 3.8 Flash TTS (`google/gemini-3.8-flash-tts`). Its text
+ may carry vocal events: `<laugh>`, `<sigh>`."""
+
+ voice: Literal[
+ "Achernar",
+ "Achird",
+ "Algenib",
+ "Algieba",
+ "Alnilam",
+ "Aoede",
+ "Autonoe",
+ "Callirrhoe",
+ "Charon",
+ "Despina",
+ "Enceladus",
+ "Erinome",
+ "Fenrir",
+ "Gacrux",
+ "Iapetus",
+ "Kore",
+ "Laomedeia",
+ "Leda",
+ "Orus",
+ "Pulcherrima",
+ "Puck",
+ "Rasalgethi",
+ "Sadachbia",
+ "Sadaltager",
+ "Schedar",
+ "Sulafat",
+ "Umbriel",
+ "Vindemiatrix",
+ "Zephyr",
+ "Zubenelgenubi",
+ ]
+ """The voice (default "Kore")."""
+ style_instructions: str | None
+ """How to say it, in words: "Warm and unhurried, like a patient teacher." (default:
+ none)."""
+
+
+class Inworld(TypedDict, total=False):
+ """The settings of Inworld TTS-1.5 Max (`fal-ai/inworld-tts`), which speaks up to 2,000
+ characters a line."""
+
+ voice: Literal[
+ "Loretta (en)",
+ "Darlene (en)",
+ "Marlene (en)",
+ "Hank (en)",
+ "Evelyn (en)",
+ "Celeste (en)",
+ "Pippa (en)",
+ "Tessa (en)",
+ "Liam (en)",
+ "Callum (en)",
+ "Hamish (en)",
+ "Abby (en)",
+ "Graham (en)",
+ "Rupert (en)",
+ "Mortimer (en)",
+ "Snik (en)",
+ "Anjali (en)",
+ "Saanvi (en)",
+ "Arjun (en)",
+ "Claire (en)",
+ "Oliver (en)",
+ "Simon (en)",
+ "Elliot (en)",
+ "James (en)",
+ "Serena (en)",
+ "Gareth (en)",
+ "Vinny (en)",
+ "Lauren (en)",
+ "Jessica (en)",
+ "Ethan (en)",
+ "Tyler (en)",
+ "Jason (en)",
+ "Chloe (en)",
+ "Veronica (en)",
+ "Victoria (en)",
+ "Miranda (en)",
+ "Sebastian (en)",
+ "Victor (en)",
+ "Malcolm (en)",
+ "Kayla (en)",
+ "Nate (en)",
+ "Jake (en)",
+ "Brian (en)",
+ "Amina (en)",
+ "Kelsey (en)",
+ "Derek (en)",
+ "Grant (en)",
+ "Evan (en)",
+ "Alex (en)",
+ "Ashley (en)",
+ "Craig (en)",
+ "Deborah (en)",
+ "Dennis (en)",
+ "Edward (en)",
+ "Elizabeth (en)",
+ "Hades (en)",
+ "Julia (en)",
+ "Pixie (en)",
+ "Mark (en)",
+ "Olivia (en)",
+ "Priya (en)",
+ "Ronald (en)",
+ "Sarah (en)",
+ "Shaun (en)",
+ "Theodore (en)",
+ "Timothy (en)",
+ "Wendy (en)",
+ "Dominus (en)",
+ "Hana (en)",
+ "Clive (en)",
+ "Carter (en)",
+ "Blake (en)",
+ "Luna (en)",
+ "Yichen (zh)",
+ "Xiaoyin (zh)",
+ "Xinyi (zh)",
+ "Jing (zh)",
+ "Erik (nl)",
+ "Katrien (nl)",
+ "Lennart (nl)",
+ "Lore (nl)",
+ "Alain (fr)",
+ "Hélène (fr)",
+ "Mathieu (fr)",
+ "Étienne (fr)",
+ "Johanna (de)",
+ "Josef (de)",
+ "Gianni (it)",
+ "Orietta (it)",
+ "Asuka (ja)",
+ "Satoshi (ja)",
+ "Hyunwoo (ko)",
+ "Minji (ko)",
+ "Seojun (ko)",
+ "Yoona (ko)",
+ "Szymon (pl)",
+ "Wojciech (pl)",
+ "Heitor (pt)",
+ "Maitê (pt)",
+ "Diego (es)",
+ "Lupita (es)",
+ "Miguel (es)",
+ "Rafael (es)",
+ "Svetlana (ru)",
+ "Elena (ru)",
+ "Dmitry (ru)",
+ "Nikolai (ru)",
+ "Riya (hi)",
+ "Manoj (hi)",
+ "Yael (he)",
+ "Oren (he)",
+ "Nour (ar)",
+ "Omar (ar)",
+ ]
+ """The voice, with its language (default "Craig (en)")."""
+ sample_rate_hertz: Literal[8000, 16000, 24000, 32000, 40000, 48000]
+ """Samples a second (default 48000)."""
+
+
+class Qwen(TypedDict, total=False):
+ """The settings of Alibaba's Qwen3-TTS 1.7B (`fal-ai/qwen-3-tts/text-to-speech/1.7b`)."""
+
+ voice: (
+ Literal[
+ "Vivian",
+ "Serena",
+ "Uncle_Fu",
+ "Dylan",
+ "Eric",
+ "Ryan",
+ "Aiden",
+ "Ono_Anna",
+ "Sohee",
+ ]
+ | None
+ )
+ """The voice (default: the model's)."""
+ language: Literal[
+ "Auto",
+ "English",
+ "Chinese",
+ "Spanish",
+ "French",
+ "German",
+ "Italian",
+ "Japanese",
+ "Korean",
+ "Portuguese",
+ "Russian",
+ ]
+ """The language (default "Auto": the text's)."""
+ prompt: str | None
+ """How to say it, in words (not what to say) (default: none)."""
+ speaker_voice_embedding_file_url: str | None
+ """A cloned voice: the URL of the speaker embedding `fal-ai/qwen-3-tts/clone-voice`
+ made; it overrides `voice` and `prompt` (default: none)."""
+ reference_text: str | None
+ """What the cloned voice's recording says, which helps it sound like it (default:
+ none)."""
+ temperature: float | None
+ """How varied the delivery is, above 0 and up to 1 (default 0.9)."""
+ top_k: int | None
+ """Sampling: from the k likeliest sounds (default 50)."""
+ top_p: float | None
+ """Sampling: from the likeliest sounds that make up p of the chance, 0 to 1 (default 1)."""
+ repetition_penalty: float | None
+ """How strongly it avoids repeating itself (default 1.05)."""
+ subtalker_dosample: bool | None
+ """Whether its second stage samples (default True)."""
+ subtalker_temperature: float | None
+ """Its second stage's temperature, 0 to 1 (default 0.9)."""
+ subtalker_top_k: int | None
+ """Its second stage's top k (default 50)."""
+ subtalker_top_p: float | None
+ """Its second stage's top p, 0 to 1 (default 1)."""
+ max_new_tokens: int | None
+ """The most sound it makes, in its codec's tokens, 1 to 8192 (default 8192 here: fal's
+ own, 200, can cut a long line short)."""
+
+
+type Model = Literal[
+ "fal-ai/elevenlabs/tts/eleven-v3",
+ "fal-ai/minimax/speech-2.8-hd",
+ "google/gemini-3.8-flash-tts",
+ "fal-ai/inworld-tts",
+ "fal-ai/qwen-3-tts/text-to-speech/1.7b",
+]
+"""The models `Fal` speaks through."""
+
+
+@dataclass(frozen=True, slots=True)
+class _Spec:
+ """How a model is asked: the input its text goes in, its settings, what is sent unless a
+ setting says otherwise, and what is always sent."""
+
+ text: str
+ settings: type
+ defaults: tuple[tuple[str, object], ...] = ()
+ fixed: tuple[tuple[str, object], ...] = ()
+
+
+_MODELS: dict[str, _Spec] = {
+ "fal-ai/elevenlabs/tts/eleven-v3": _Spec(
+ "text", ElevenV3, fixed=(("timestamps", True),)
+ ),
+ "fal-ai/minimax/speech-2.8-hd": _Spec("prompt", MiniMax),
+ "google/gemini-3.8-flash-tts": _Spec("prompt", Gemini),
+ "fal-ai/inworld-tts": _Spec("text", Inworld),
+ "fal-ai/qwen-3-tts/text-to-speech/1.7b": _Spec(
+ "text", Qwen, defaults=(("max_new_tokens", 8192),)
+ ),
+}
+
+
+# ── the voice ───────────────────────────────────────────────────────────────────────────────
+
+
+@dataclass(frozen=True, slots=True, init=False, repr=False)
+class Fal:
+ """A text-to-speech model on fal.ai, as a voice: `Fal(model, **settings)`, where the
+ settings are the model's own ([`ElevenV3`][manimgx.audio.fal.ElevenV3] for the default).
+
+ Args:
+ model: The model's ID on fal.
+ key: fal's API key; by default `FAL_KEY`.
+ **settings: The model's settings.
+
+ Examples:
+ A scene's voice, in its class:
+
+ ```py
+ voice = m.voices.Fal(
+ "fal-ai/minimax/speech-2.8-hd",
+ voice_setting={"voice_id": "Calm_Woman", "speed": 1.1},
+ )
+ ```
+ """
+
+ model: Model
+ """The model's ID on fal."""
+ settings: dict[str, object]
+ """The model's settings, as given."""
+ key: str | None = field(default=None, compare=False)
+ """fal's API key, if given; else `FAL_KEY` is read as the voice speaks."""
+
+ @overload
+ def __init__(
+ self,
+ model: Literal["fal-ai/elevenlabs/tts/eleven-v3"] = ...,
+ /,
+ *,
+ key: str | None = None,
+ **settings: Unpack[ElevenV3],
+ ) -> None: ...
+ @overload
+ def __init__(
+ self,
+ model: Literal["fal-ai/minimax/speech-2.8-hd"],
+ /,
+ *,
+ key: str | None = None,
+ **settings: Unpack[MiniMax],
+ ) -> None: ...
+ @overload
+ def __init__(
+ self,
+ model: Literal["google/gemini-3.8-flash-tts"],
+ /,
+ *,
+ key: str | None = None,
+ **settings: Unpack[Gemini],
+ ) -> None: ...
+ @overload
+ def __init__(
+ self,
+ model: Literal["fal-ai/inworld-tts"],
+ /,
+ *,
+ key: str | None = None,
+ **settings: Unpack[Inworld],
+ ) -> None: ...
+ @overload
+ def __init__(
+ self,
+ model: Literal["fal-ai/qwen-3-tts/text-to-speech/1.7b"],
+ /,
+ *,
+ key: str | None = None,
+ **settings: Unpack[Qwen],
+ ) -> None: ...
+ def __init__(
+ self,
+ model: Model = "fal-ai/elevenlabs/tts/eleven-v3",
+ /,
+ *,
+ key: str | None = None,
+ **settings: object,
+ ) -> None:
+ spec = _MODELS.get(model)
+ if spec is None:
+ raise ValueError(f"fal: no model {model!r} here: {', '.join(_MODELS)}")
+ known = spec.settings.__annotations__
+ if unknown := sorted(settings.keys() - known.keys()):
+ raise TypeError(
+ f"{model} takes {', '.join(known)}; not {', '.join(unknown)}"
+ )
+ object.__setattr__(self, "model", model)
+ object.__setattr__(
+ self, "settings", {k: _ordered(v) for k, v in sorted(settings.items())}
+ )
+ object.__setattr__(self, "key", key)
+
+ def __repr__(self) -> str:
+ """The call that makes it, without its key: what names it in the cache."""
+ given = "".join(f", {k}={v!r}" for k, v in self.settings.items())
+ return f"Fal({self.model!r}{given})"
+
+ def __call__(self, text: str) -> Speech:
+ key = self.key or os.environ.get("FAL_KEY")
+ if not key:
+ raise RuntimeError(
+ "no key for fal.ai: set FAL_KEY (fal's dashboard gives one), or give"
+ " `key=`"
+ )
+ spec = _MODELS[self.model]
+ body = {
+ **dict(spec.defaults),
+ **self.settings,
+ **dict(spec.fixed),
+ spec.text: text,
+ }
+ reply = json.loads(
+ _fetch(
+ f"https://fal.run/{self.model}", body, {"Authorization": f"Key {key}"}
+ )
+ )
+ audio = _fetch(reply["audio"]["url"]) # fal keeps it days, not forever: now
+ return Speech(
+ audio, text=text, words=timed(text, _pieces(reply.get("timestamps")))
+ )
+
+
+def _ordered(value: object) -> object:
+ """`value` with every dict in it ordered by its keys: one voice, one name, however its
+ settings were written."""
+ if isinstance(value, dict):
+ return {k: _ordered(v) for k, v in sorted(value.items())}
+ if isinstance(value, list):
+ return [_ordered(v) for v in value]
+ return value
+
+
+def _pieces(stamps: object) -> list[tuple[str, float, float]]:
+ """A model's timing of its text, as fal passes it on, as timed pieces of the text: items of
+ a text, a start and an end (words or characters), or ElevenLabs' alignments (characters,
+ with their starts and ends). None if there is none, or if it runs backwards: the words
+ are then estimated."""
+ pieces: list[tuple[str, float, float]] = []
+ for item in stamps if isinstance(stamps, list) else ():
+ if not isinstance(item, dict):
+ continue
+ if "characters" in item:
+ pieces += [
+ (str(c), float(a), float(b))
+ for c, a, b in zip(
+ item["characters"],
+ item["character_start_times_seconds"],
+ item["character_end_times_seconds"],
+ strict=True,
+ )
+ ]
+ elif {"text", "start", "end"} <= item.keys():
+ pieces.append((str(item["text"]), float(item["start"]), float(item["end"])))
+ starts = [a for _, a, _ in pieces]
+ return pieces if starts == sorted(starts) else []
+
+
+def _fetch(
+ url: str,
+ body: dict[str, object] | None = None,
+ headers: dict[str, str] | None = None,
+) -> bytes:
+ """GET `url`, or POST `body` to it as JSON; the response's bytes (an HTTP error says what
+ the service said)."""
+ request = urllib.request.Request(
+ url,
+ data=None if body is None else json.dumps(body).encode(),
+ headers={"Content-Type": "application/json", **(headers or {})},
+ method="GET" if body is None else "POST",
+ )
+ try:
+ with urllib.request.urlopen(request, timeout=300) as response:
+ return response.read()
+ except urllib.error.HTTPError as error:
+ detail = error.read().decode(errors="replace")[:500]
+ raise RuntimeError(f"{url}: HTTP {error.code}: {detail}") from None
diff --git a/tests/unit/audio/test_fal.py b/tests/unit/audio/test_fal.py
new file mode 100644
index 00000000..c5475484
--- /dev/null
+++ b/tests/unit/audio/test_fal.py
@@ -0,0 +1,220 @@
+"""fal.ai's models speak a text: `Fal(model, **settings)` asks fal for it and downloads the
+audio.
+
+- A model is asked at its endpoint with the key, its text in the model's own input, the settings
+ given (over what manimgx sends unless told otherwise), and what the model is always sent; what
+ it says is the audio at the URL fal answers with.
+- A model's settings are its own: another's are refused as the voice is made, and by a type
+ checker.
+- A voice is named by its model and its settings, however they were written, never by its key;
+ without a key, it says where to get one.
+- A model's timing of its text times the words; timing that runs backwards, or none, leaves
+ them estimated.
+- A scene with no voice speaks through fal's default model.
+"""
+
+import json
+from collections.abc import Callable
+from pathlib import Path
+from typing import cast
+
+import numpy as np
+import pytest
+from hypothesis import given
+from hypothesis import strategies as st
+
+import manimgx as m
+from manimgx.audio import fal
+from manimgx.audio import sound as voices
+from manimgx.audio.fal import Fal
+
+AUDIO_URL = "https://v3b.fal.media/files/b/speech.wav"
+SECOND_TENTH = 4800 # samples: a tenth of a second at the film's rate
+
+
+class Service:
+ """fal, faked: answers a model's request with an audio file's URL (and whatever else
+ `reply` holds), and that URL with a tenth of a second of sound."""
+
+ def __init__(self, reply: dict[str, object] | None = None) -> None:
+ self.reply = reply or {}
+ self.asked: list[
+ tuple[str, dict[str, object] | None, dict[str, str] | None]
+ ] = []
+
+ def __call__(
+ self,
+ url: str,
+ body: dict[str, object] | None = None,
+ headers: dict[str, str] | None = None,
+ ) -> bytes:
+ self.asked.append((url, body, headers))
+ if body is None:
+ return voices._wav(np.full((SECOND_TENTH, 1), 0.1, np.float32))
+ return json.dumps({"audio": {"url": AUDIO_URL}, **self.reply}).encode()
+
+ @property
+ def body(self) -> dict[str, object] | None:
+ """What the first request sent."""
+ return self.asked[0][1]
+
+
+def service(monkeypatch: pytest.MonkeyPatch) -> Service:
+ fake = Service()
+ monkeypatch.setattr(fal, "_fetch", fake)
+ monkeypatch.setenv("FAL_KEY", "k")
+ return fake
+
+
+def written_otherwise(value: object) -> object:
+ """The same settings, every dict's keys in the other order."""
+ if isinstance(value, dict):
+ return {k: written_otherwise(v) for k, v in reversed(value.items())}
+ return value
+
+
+models = st.sampled_from(sorted(fal._MODELS))
+
+
+def settings_of(model: str) -> st.SearchStrategy[dict[str, object]]:
+ """Any settings a model's schema allows."""
+ return cast(
+ "st.SearchStrategy[dict[str, object]]",
+ st.from_type(fal._MODELS[model].settings),
+ )
+
+
+class TestAsking:
+ @given(model=models, data=st.data(), text=st.text(min_size=1))
+ def test_a_model_is_asked_for_its_text(
+ self, model: str, data: st.DataObject, text: str
+ ) -> None:
+ spec = fal._MODELS[model]
+ settings = data.draw(settings_of(model), label="settings")
+ fake = Service()
+ with pytest.MonkeyPatch.context() as patch:
+ patch.setattr(fal, "_fetch", fake)
+ speech = Fal(model, key="k", **settings)(text) # type: ignore (a model drawn at run time)
+ (url, body, headers), (audio_url, nothing, _) = fake.asked
+ assert url == f"https://fal.run/{model}"
+ assert headers == {"Authorization": "Key k"}
+ # what manimgx sends unless told otherwise, the settings over it, what is always sent
+ assert body == {
+ **dict(spec.defaults),
+ **settings,
+ **dict(spec.fixed),
+ spec.text: text,
+ }
+ assert (audio_url, nothing) == (AUDIO_URL, None)
+ assert speech.text == text
+ assert speech.duration == pytest.approx(0.1)
+
+ def test_another_models_settings_are_refused(self) -> None:
+ untyped: Callable[..., Fal] = Fal # as a caller no type checker reads
+ with pytest.raises(TypeError, match="takes"):
+ untyped("google/gemini-3.8-flash-tts", stability=0.5)
+
+ def test_a_type_checker_knows_each_models_values(self) -> None:
+ # the suppression is the test: `just check` fails if it goes unused
+ voice = Fal("google/gemini-3.8-flash-tts", voice="Nobody") # type: ignore
+ assert voice.settings == {"voice": "Nobody"} # fal refuses it as it speaks
+
+ def test_without_a_key_it_says_where_to_get_one(
+ self, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ monkeypatch.delenv("FAL_KEY", raising=False)
+ with pytest.raises(RuntimeError, match="FAL_KEY"):
+ Fal()("hello")
+
+ def test_a_key_given_is_used_over_the_environments(self, service: Service) -> None:
+ Fal(key="given")("hello")
+ assert service.asked[0][2] == {"Authorization": "Key given"}
+
+
+class TestNaming:
+ @given(model=models, data=st.data(), key=st.text(min_size=1))
+ def test_a_voice_is_named_by_its_model_and_settings_never_its_key(
+ self, model: str, data: st.DataObject, key: str
+ ) -> None:
+ settings = data.draw(settings_of(model), label="settings")
+ other = data.draw(settings_of(model), label="other settings")
+ voice = Fal(model, key=key, **settings) # type: ignore (a model drawn at run time)
+ again = Fal(model, **written_otherwise(settings)) # type: ignore (the same)
+ assert voices._identity(voice) == voices._identity(again)
+ assert repr(voice) == repr(again)
+ assert voice == again
+ same = json.dumps(other, sort_keys=True) == json.dumps(settings, sort_keys=True)
+ named = voices._identity(Fal(model, **other)) # type: ignore (the same)
+ assert (named == voices._identity(voice)) == same
+ for another in fal._MODELS.keys() - {model}:
+ assert voices._identity(Fal(another)) != voices._identity(voice) # type: ignore
+
+
+class TestTiming:
+ TEXT = "hi you"
+
+ def spoken(
+ self, service: Service, stamps: object
+ ) -> tuple[tuple[str, float, float], ...]:
+ service.reply = {"timestamps": stamps}
+ speech = Fal()(self.TEXT)
+ return tuple((w.text, w.start, w.end) for w in speech.words)
+
+ def test_elevenlabs_alignment_times_the_words(self, service: Service) -> None:
+ alignment = {
+ "characters": list(self.TEXT),
+ "character_start_times_seconds": [i / 100 for i in range(len(self.TEXT))],
+ "character_end_times_seconds": [
+ (i + 1) / 100 for i in range(len(self.TEXT))
+ ],
+ }
+ assert self.spoken(service, [alignment]) == (
+ ("hi", 0.0, pytest.approx(0.02)),
+ ("you", pytest.approx(0.03), pytest.approx(0.06)),
+ )
+
+ def test_timed_words_time_the_words(self, service: Service) -> None:
+ items = [
+ {"text": "hi", "start": 0.0, "end": 0.02},
+ {"text": "you", "start": 0.03, "end": 0.06},
+ ]
+ assert self.spoken(service, items) == (
+ ("hi", 0.0, 0.02),
+ ("you", 0.03, 0.06),
+ )
+
+ @pytest.mark.parametrize(
+ "stamps",
+ [
+ None,
+ [],
+ [
+ {"text": "hi", "start": 0.05, "end": 0.07},
+ {"text": "you", "start": 0.0, "end": 0.02},
+ ],
+ [{"text": "bye", "start": 0.0, "end": 0.02}],
+ ],
+ ids=["none", "empty", "backwards", "other words"],
+ )
+ def test_without_usable_timing_the_words_are_estimated(
+ self, service: Service, stamps: object
+ ) -> None:
+ words = self.spoken(service, stamps)
+ assert [w for w, _, _ in words] == ["hi", "you"]
+ assert all(0 <= a <= b <= 0.1 + 1e-9 for _, a, b in words)
+
+
+class TestScene:
+ def test_a_scene_without_a_voice_speaks_through_fals_default(
+ self, service: Service, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
+ ) -> None:
+ monkeypatch.setattr("manimgx.scene._voice_folder", lambda scene: tmp_path)
+
+ class Narrated(m.Scene):
+ def construct(self) -> None:
+ self.say("hello there")
+
+ (clip,) = Narrated().render().clips
+ assert service.asked[0][0] == "https://fal.run/fal-ai/elevenlabs/tts/eleven-v3"
+ assert clip.sound.duration == pytest.approx(0.1)