Commit 3574df84b for llama.cpp

commit 3574df84b776611928d9b227bf614be71fbb9849
Author: Xuan-Son Nguyen <son@huggingface.co>
Date:   Wed Nov 18 00:04:15 2026 +0100

    mtmd: support tiny-voca (#32427)

    * mtmd: support tiny-voca

    * improve G2P

diff --git a/common/chat-tts-norm.cpp b/common/chat-tts-norm.cpp
index 2ec93a204..b828ed9b4 100644
--- a/common/chat-tts-norm.cpp
+++ b/common/chat-tts-norm.cpp
@@ -871,7 +871,7 @@ bool common_tts_dict_find(const common_tts_dict & dict, const std::string & key,
     return true;
 }

-void common_chat_tts_norm_add_builtins(context & ctx, std::shared_ptr<const common_tts_dicts> dicts) {
+void common_chat_tts_norm_add_builtins(context & ctx, std::shared_ptr<const common_tts_dicts> dicts, common_tts_g2p g2p) {
     ctx.set_val("tts_norm_scan", mk_val<value_func>("tts_norm_scan", tts_norm_builtin_scan));
     ctx.set_val("tts_norm_sub",  mk_val<value_func>("tts_norm_sub",  tts_norm_builtin_sub));
     ctx.set_val("tts_dict",      mk_val<value_func>("tts_dict", [dicts](const func_args & args) -> value {
@@ -888,5 +888,15 @@ void common_chat_tts_norm_add_builtins(context & ctx, std::shared_ptr<const comm
         }
         return value_from_json(common_json::parse(doc), false);
     }));
+    // none when the caller gives no letter-to-sound model, or the model has no pronunciation
+    ctx.set_val("tts_g2p",       mk_val<value_func>("tts_g2p", [g2p](const func_args & args) -> value {
+        args.ensure_count(1, 1);
+        args.ensure_vals<value_string>();
+        const std::string out = g2p ? g2p(args.get_pos(0)->as_string().str()) : std::string();
+        if (out.empty()) {
+            return mk_val<value_none>();
+        }
+        return mk_val<value_string>(out);
+    }));
 }

diff --git a/common/chat-tts-norm.h b/common/chat-tts-norm.h
index 84b16018c..b96486078 100644
--- a/common/chat-tts-norm.h
+++ b/common/chat-tts-norm.h
@@ -1,6 +1,7 @@
 #pragma once

 #include <cstdint>
+#include <functional>
 #include <map>
 #include <memory>
 #include <string>
@@ -70,6 +71,11 @@ using common_tts_dicts = std::map<std::string, common_tts_dict>;
 // binary search of key in a dictionary; on success, value is set to the JSON document
 bool common_tts_dict_find(const common_tts_dict & dict, const std::string & key, std::string & value);

-// add tts_norm_scan(), tts_norm_sub() and tts_dict() to the context of a task template
+// pronunciation of a word by a letter-to-sound model, as phonemes separated by spaces (e.g. CMUdict notation)
+// empty when there is none; the notation is the one of the model, its "tts" template knows it
+using common_tts_g2p = std::function<std::string(const std::string & word)>;
+
+// add tts_norm_scan(), tts_norm_sub(), tts_dict() and tts_g2p() to the context of a task template
 // they are not jinja global builtins on purpose: chat templates are not affected
-void common_chat_tts_norm_add_builtins(jinja::context & ctx, std::shared_ptr<const common_tts_dicts> dicts = nullptr);
+void common_chat_tts_norm_add_builtins(jinja::context & ctx, std::shared_ptr<const common_tts_dicts> dicts = nullptr,
+                                       common_tts_g2p g2p = nullptr);
diff --git a/common/chat.cpp b/common/chat.cpp
index 4816973c4..de5f1c490 100644
--- a/common/chat.cpp
+++ b/common/chat.cpp
@@ -807,10 +807,11 @@ bool common_chat_templates_was_explicit(const struct common_chat_templates * tmp
 }

 // renders a task template (e.g. "tts") with the given variables
-static std::string common_chat_template_render(const common_chat_template & tmpl, const json & vars, const std::shared_ptr<common_tts_dicts> & tts_dicts) {
+static std::string common_chat_template_render(const common_chat_template & tmpl, const json & vars, const std::shared_ptr<common_tts_dicts> & tts_dicts,
+                                               const common_tts_g2p & g2p = nullptr) {
     jinja::context ctx(tmpl.source());
-    // text normalization helpers: tts_norm_scan(), tts_norm_sub() and tts_dict(), see docs/tts-norm-patterns.md
-    common_chat_tts_norm_add_builtins(ctx, tts_dicts);
+    // text normalization helpers: tts_norm_scan(), tts_norm_sub(), tts_dict() and tts_g2p(), see docs/tts-norm-patterns.md
+    common_chat_tts_norm_add_builtins(ctx, tts_dicts, g2p);
     jinja::global_from_json(ctx, vars, false);
     ctx.set_val("bos_token", jinja::mk_val<jinja::value_string>(tmpl.bos_token()));
     ctx.set_val("eos_token", jinja::mk_val<jinja::value_string>(tmpl.eos_token()));
@@ -886,7 +887,7 @@ common_chat_prompt_preset common_chat_get_asr_prompt(const common_chat_templates
 }

 std::string common_chat_get_tts_prompt(const common_chat_templates * chat_templates, const std::string & text,
-                                       const std::string & language, const std::string & voice) {
+                                       const std::string & language, const std::string & voice, const common_tts_g2p & g2p) {
     if (!chat_templates || !chat_templates->template_tts) {
         return "";
     }
@@ -894,7 +895,7 @@ std::string common_chat_get_tts_prompt(const common_chat_templates * chat_templa
         {"messages", json::array({{{"role", "user"}, {"content", text}}})},
         {"language", language},
         {"voice",    voice},
-    }, chat_templates->tts_dicts);
+    }, chat_templates->tts_dicts, g2p);
 }

 std::string common_chat_get_speech_system_prompt(const common_chat_templates * chat_templates,
diff --git a/common/chat.h b/common/chat.h
index d66afaef7..43331b16d 100644
--- a/common/chat.h
+++ b/common/chat.h
@@ -504,8 +504,10 @@ common_chat_prompt_preset common_chat_get_asr_prompt(const common_chat_templates

 // prompt of the model's "tts" chat template for the text to speak, empty if the model has none
 // language and voice are optional, the template defines their defaults
+// g2p (optional): the letter-to-sound model of the template's tts_g2p(), e.g. mtmd_gen_audio_g2p()
 std::string common_chat_get_tts_prompt(const common_chat_templates * chat_templates, const std::string & text,
-                                       const std::string & language = "", const std::string & voice = "");
+                                       const std::string & language = "", const std::string & voice = "",
+                                       const std::function<std::string(const std::string & word)> & g2p = nullptr);

 // system prompt line for the speech language and voice (e.g. LFM3-Audio), empty if not used
 std::string common_chat_get_speech_system_prompt(const common_chat_templates * chat_templates,
diff --git a/conversion/__init__.py b/conversion/__init__.py
index 9fc95f797..881111e9f 100644
--- a/conversion/__init__.py
+++ b/conversion/__init__.py
@@ -284,6 +284,7 @@ TEXT_MODEL_MAP: dict[str, str] = {
     "Qwen3OmniMoeForConditionalGeneration": "qwen3vl",
     "PocketTTSModel": "pockettts",
     "KokoroModel": "kokoro",
+    "TinyVocaModel": "tiny_voca",
     "Zonos3Model": "zonos3",
     "YuE2ForCausalLM": "yue2",
     "MiniMaxMusic3ForConditionalGeneration": "minimax_music3",
@@ -425,6 +426,7 @@ MMPROJ_MODEL_MAP: dict[str, str] = {
     "Qwen3OmniMoeForConditionalGeneration": "qwen3vl",
     "PocketTTSModel": "pockettts",
     "KokoroModel": "kokoro",
+    "TinyVocaModel": "tiny_voca",
     "SopranoModel": "soprano",
     "Qwen3TTSForConditionalGeneration": "qwen3tts",
     "Qwen3VLForConditionalGeneration": "qwen3vl",
diff --git a/conversion/tiny_voca.py b/conversion/tiny_voca.py
new file mode 100644
index 000000000..6bfa54907
--- /dev/null
+++ b/conversion/tiny_voca.py
@@ -0,0 +1,443 @@
+from __future__ import annotations
+
+import importlib.util
+import json
+import re
+import unicodedata
+from pathlib import Path
+from typing import Any, Callable, Iterable
+
+import numpy as np
+import torch
+from torch import Tensor
+
+from .base import ModelBase, TextModel, MmprojModel, gguf, logger
+
+# tiny-voca, https://huggingface.co/n4ze3m/tiny-voca
+#
+# text model: phoneme encoder (4 blocks), the input rows are built by the audio pipeline (token + stress + voice)
+# mmproj:     input tables, voices, duration predictor, prosody and acoustic stacks, source-filter heads
+#
+# the text to phonemes step (dizione) is the "tts" chat template, see models/templates/n4ze3m-tiny-voca-tts.jinja
+# its lexicon comes from the phonemizer/ directory of the model, the other tables from the dizione python package
+#
+# the prosody and acoustic stacks have the same blocks as the encoder, they are stored with the libllama names
+# of the tiny-voca arch under a.gen.prosody. and a.gen.acoustic., mtmd runs them on llama contexts of its own
+
+CHECKPOINT = "tiny-voca.pt"
+DEFAULT_VOICE = "M2"
+
+# phone classes of the reference (text.py): the phones get at least one frame, the vowels are always voiced
+VOWELS = ("ə", "ɐ", "ɚ", "ᵻ", "ɪ", "i", "iː", "ɛ", "æ", "ʌ", "ɑː", "ɔ", "ɔː", "ʊ", "uː", "ɜː",
+          "eɪ", "aɪ", "ɔɪ", "oʊ", "aʊ", "iə", "oː",
+          "ɑːɹ", "ɔːɹ", "oːɹ", "ɪɹ", "ɛɹ", "ʊɹ", "aɪɚ", "aɪə", "əl", "n̩")
+CONSONANTS = ("p", "b", "t", "d", "k", "ɡ", "f", "v", "θ", "ð", "s", "z", "ʃ", "ʒ", "h",
+              "m", "n", "ŋ", "l", "ɹ", "j", "w", "tʃ", "dʒ", "ɾ", "ʔ")
+
+# input letters and output phonemes of the unknown-word model, fixed in mtmd (see mtmd_graph_tiny_voca)
+G2P_LETTERS = "abcdefghijklmnopqrstuvwxyz'"
+G2P_SYMBOLS = [v + d for v in ("AA", "AE", "AH", "AO", "AW", "AY") for d in "012"] + ["B", "CH", "D", "DH"] + \
+    [v + d for v in ("EH", "ER", "EY") for d in "012"] + ["F", "G", "HH"] + [v + d for v in ("IH", "IY") for d in "012"] + \
+    ["JH", "K", "L", "M", "N", "NG"] + [v + d for v in ("OW", "OY") for d in "012"] + ["P", "R", "S", "SH", "T", "TH"] + \
+    [v + d for v in ("UH", "UW") for d in "012"] + ["V", "W", "Y", "Z", "ZH"]
+
+# block tensors: reference name -> libllama name
+BLOCK_MAP = {
+    "norm1":          "attn_norm",
+    "attention.qkv":  "attn_qkv",
+    "attention.out":  "attn_output",
+    "norm2":          "ffn_norm",
+    "depthwise":      "ssm_conv1d",
+    "expand":         "ffn_up",
+    "project":        "ffn_down",
+}
+
+# mmproj stacks: reference module -> prefix
+STACK_PREFIX = {
+    "prosody_stack":  "a.gen.prosody.",
+    "acoustic_stack": "a.gen.acoustic.",
+}
+
+# other mmproj tensors: reference module -> name
+MMPROJ_MAP = {
+    "tokens":             "a.gen.tok_embd",
+    "stress":             "a.gen.stress_embd",
+    "duration.conv1":     "a.gen.dur.conv1",
+    "duration.conv2":     "a.gen.dur.conv2",
+    "duration.norm1":     "a.gen.dur.norm1",
+    "duration.norm2":     "a.gen.dur.norm2",
+    "duration.out":       "a.gen.dur.out",
+    "frame_position":     "a.gen.frame_pos",
+    "prosody_head":       "a.gen.prosody_head",
+    "prosody_projection": "a.gen.prosody_proj",
+    "vocoder.harmonic":   "a.gen.voc.harmonic",
+    "vocoder.noise":      "a.gen.voc.noise",
+}
+
+
+def _is_tiny_voca_checkpoint(dir_model: Path) -> bool:
+    if not (dir_model / CHECKPOINT).is_file() or not (dir_model / "config.json").is_file():
+        return False
+    with open(dir_model / "config.json", encoding="utf-8") as f:
+        config = json.load(f)
+    return config.get("phonemizer", {}).get("name") == "dizione" and "vocab" in config and "model" in config
+
+
+@ModelBase.register_hparams_loader(_is_tiny_voca_checkpoint)
+def _load_tiny_voca_hparams(dir_model: Path) -> dict[str, Any]:
+    logger.info("gguf: detected tiny-voca checkpoint")
+    with open(dir_model / "config.json", encoding="utf-8") as f:
+        config = json.load(f)
+    c = config["model"]
+    return {
+        "architectures": ["TinyVocaModel"],
+        "model_type": "tiny-voca",
+        "num_hidden_layers": c["encoder_layers"],
+        "hidden_size": c["channels"],
+        "intermediate_size": c["feedforward"],
+        "num_attention_heads": c["heads"],
+        "num_key_value_heads": c["heads"],
+        "max_position_embeddings": c["max_tokens"],
+        "vocab_size": c["vocab_size"],
+        "layer_norm_eps": 1e-5,  # torch LayerNorm default
+        "rope_theta": 10000.0,
+        "tiny_voca": config,
+        # the mmproj tensors are named explicitly, they do not go through the block tensor map
+        "audio_config": {"num_hidden_layers": 0},
+    }
+
+
+def _table_rows(path: Path) -> Iterable[list[str]]:
+    # dizione tables: columns separated by tabs or two or more spaces, "#" comments
+    for line in path.read_text(encoding="utf-8").splitlines():
+        line = line.split("#", 1)[0].rstrip() if line.lstrip().startswith("#") else line.rstrip()
+        if line.strip():
+            yield [c.strip() for c in re.split(r"\t+| {2,}", line.strip())]
+
+
+def _g2p_dicts(dir_model: Path) -> dict[str, dict[str, Any]] | None:
+    lexicon_path = dir_model / "phonemizer" / "lexicon.tsv"
+    spec = importlib.util.find_spec("dizione")
+    if not lexicon_path.is_file() or spec is None or spec.origin is None:
+        logger.warning("dizione or phonemizer/lexicon.tsv not found, the model will take phonemes as prompt "
+                       "(pip install git+https://github.com/n4ze3m/dizione)")
+        return None
+    data = Path(spec.origin).parent / "data"
+    lexicon: dict[str, str] = {}
+    for line in lexicon_path.read_text(encoding="utf-8").splitlines():
+        if "\t" in line:
+            word, phones = line.split("\t", 1)
+            lexicon[word] = phones.strip()
+
+    # word -> [stress class, form, final form or ""], see function_words.txt
+    functions = {r[0]: [r[1], r[2], r[3] if len(r) > 3 else ""] for r in _table_rows(data / "function_words.txt")}
+    # words -> [class, forms (one, or one per word), final form or ""], see phrases.txt
+    phrases = {r[0]: [r[1], [f.strip() for f in r[2].split("|")], r[3] if len(r) > 3 else ""] for r in _table_rows(data / "phrases.txt")}
+    letters = {r[0]: r[1] for r in _table_rows(data / "letters.txt")}
+    # a value is phones, or CMUdict notation (converted by the template, like the output of the unknown-word model)
+    exceptions = {r[0].lower(): r[1] for r in _table_rows(data / "exceptions.txt") if " " not in r[0]}
+    # a name of several words: "w1 w2 ..." -> one pronunciation per word; first word -> the longest name it starts
+    names: dict[str, Any] = {}
+    name_starts: dict[str, int] = {}
+    for r in _table_rows(data / "exceptions.txt"):
+        words = r[0].lower().split()
+        if len(words) > 1:
+            names[" ".join(words)] = [part.strip() for part in r[1].split("|")]
+            name_starts[words[0]] = max(name_starts.get(words[0], 0), len(words))
+    # phoneme (CMUdict, no stress) -> {letters: log probability}: aligns the letters of a word to its phonemes
+    with open(dir_model / "phonemizer" / "align.json", encoding="utf-8") as f:
+        align = json.load(f)
+    # abbreviation -> [when, words]
+    abbreviations = {r[0]: [r[1], r[2]] for r in _table_rows(data / "abbreviations.txt")}
+    # chat spelling -> [words to say ("*LOL": letter by letter, "-": nothing), only as a reply by itself], see chat.txt
+    chat = {r[0].lower(): [r[1] if len(r) > 1 else "-", len(r) > 2] for r in _table_rows(data / "chat.txt")}
+    # accented latin letter -> plain letter(s): "café" is in the dictionary as "cafe" (dizione: _plain_letter)
+    accents = {"ß": "ss", "æ": "ae", "Æ": "Ae", "œ": "oe", "Œ": "Oe", "ø": "o", "Ø": "O", "đ": "d", "ł": "l", "Ł": "L"}
+    for cp in range(0x80, 0x2000):
+        char = chr(cp)
+        if char.isalpha() and char not in accents:
+            plain = "".join(c for c in unicodedata.normalize("NFKD", char) if not unicodedata.combining(c))
+            if plain.isascii() and plain.isalpha():
+                accents[char] = plain
+    # compatibility forms of the text (NFKC), a character at a time: "…" is "...", "fi" is "fi", "№" is "No", full
+    # width letters. Sequences that NFKC would compose are left as they are
+    nfkc: dict[str, str] = {}
+    for cp in list(range(0x80, 0x3000)) + list(range(0xFB00, 0xFB07)) + list(range(0xFF01, 0xFF5F)):
+        if unicodedata.normalize("NFKC", chr(cp)) != chr(cp):
+            nfkc[chr(cp)] = unicodedata.normalize("NFKC", chr(cp))
+    # units after a number: name -> [one, many]; "exact" ones are written in one letter case only ("m", not "M")
+    units: dict[str, Any] = {}
+    exact_units: dict[str, Any] = {}
+    for r in _table_rows(data / "units.txt"):
+        if len(r) > 3 and r[3] == "exact":
+            exact_units[r[0]] = [r[1], r[2]]
+        else:
+            units[r[0].lower()] = [r[1], r[2]]
+    # the unit names as a pattern of tts_norm_scan(), the longest first; a unit does not stop inside a word
+    unit_names = sorted(set(units) | set(exact_units), key=lambda name: (-len(name), name))
+    # (the pattern ignores the case of ASCII letters only, the greek letter is written in both)
+    unit_pattern = "|".join("".join("[μΜ]" if c == "μ" else c if c.isalnum() else "%" + c for c in name) + "(?![A-Za-z])" for name in unit_names)
+    # endings of file names and addresses: "word" (dot com) or "letters" (dot P D F)
+    endings = {r[0]: (r[1] if len(r) > 1 else "") for r in _table_rows(data / "endings.txt")}
+    out = {
+        "lexicon":       lexicon,
+        "functions":     functions,
+        "phrases":       phrases,
+        "letters":       letters,
+        "exceptions":    exceptions,
+        "abbreviations": abbreviations,
+        "align":         align,
+        "names":         names,
+        "chat":          chat,
+        "accents":       accents,
+        "nfkc":          nfkc,
+        "units":         units,
+        "exact_units":   exact_units,
+        "endings":       endings,
+        "patterns":      {"units": unit_pattern},
+        "name_starts":   name_starts,
+    }
+    out.update(_homograph_dicts(dir_model, data))
+    return out
+
+
+def _homograph_dicts(dir_model: Path, data: Path) -> dict[str, dict[str, Any]]:
+    # words spelled alike and pronounced differently (dizione homographs.py): a linear classifier on the neighbouring
+    # words, and hand-written rules that are tried first. Weights are stored x10000 (they have four decimals), so the
+    # template adds integers.
+    path = dir_model / "phonemizer" / "homographs.json"
+    if not path.is_file():
+        return {"homographs": {}, "homograph_shared": {}, "homograph_plurals": {}, "homograph_rules": {}}
+    with open(path, encoding="utf-8") as f:
+        source = json.load(f)
+
+    def fixed(x: float) -> int:
+        value = round(x * 10000)
+        if abs(value - x * 10000) > 1e-6:
+            raise ValueError(f"homographs.json: weight {x} has more than four decimals")
+        return value
+
+    words: dict[str, Any] = {}
+    for word, entry in source["words"].items():
+        model = entry["model"]
+        noun_verb = model["kind"] == "noun-verb"
+        if not noun_verb and model["kind"] != "senses":
+            raise ValueError(f"homographs.json: unknown model kind {model['kind']!r}")
+        words[word] = {
+            # sense: [id, role, phones]
+            "senses": [[s["id"], s["role"], s["phones"]] for s in entry["senses"]],
+            # noun-verb: one score for the verb reading; else one score per sense
+            "noun_verb": noun_verb,
+            "bias":    [fixed(model["bias"])] if noun_verb else [fixed(b) for b in model["bias"]],
+            "weights": [{k: fixed(v) for k, v in w.items()} for w in ([model["weights"]] if noun_verb else model["weights"])],
+        }
+    # the weights all noun-or-verb homographs share; "" is the bias
+    shared = {k: fixed(v) for k, v in source["pooled"]["weights"].items()}
+    shared[""] = fixed(source["pooled"]["bias"])
+    # word -> [[sense id, [[condition, [words]], ...]], ...], see homograph_rules.txt
+    rules: dict[str, Any] = {}
+    for r in _table_rows(data / "homograph_rules.txt"):
+        conditions = []
+        for part in r[2].split(";"):
+            name, _, listed = part.strip().partition(":")
+            conditions.append([name, listed.split()])
+        rules.setdefault(r[0], []).append([r[1], conditions])
+    return {
+        "homographs":        words,
+        "homograph_shared":  shared,
+        "homograph_plurals": {base: 1 for base in source.get("plural_bases", ())},
+        "homograph_rules":   rules,
+    }
+
+
+def _load_state(dir_model: Path) -> dict[str, Tensor]:
+    ckpt = torch.load(dir_model / CHECKPOINT, map_location="cpu", weights_only=True)
+    if ckpt.get("format_version") != 4:
+        raise ValueError(f"unsupported tiny-voca checkpoint format {ckpt.get('format_version')}")
+    return ckpt["model"]
+
+
+def _block_name(name: str) -> tuple[int, str] | None:
+    # "blocks.<i>.<module>.<weight|bias>" -> (i, "blk.<i>.<libllama name>.<weight|bias>")
+    parts = name.split(".")
+    if len(parts) < 4 or parts[0] != "blocks":
+        return None
+    bid = int(parts[1])
+    module, suffix = ".".join(parts[2:-1]), parts[-1]
+    return bid, f"blk.{bid}.{BLOCK_MAP[module]}.{suffix}"
+
+
+def _block_tensors(new_name: str, t: Tensor) -> Iterable[tuple[str, Tensor]]:
+    # depthwise conv [C, 1, K] -> [C, K], the layout of ggml_ssm_conv
+    if ".ssm_conv1d.weight" in new_name:
+        yield (new_name, t.squeeze(1))
+        return
+    # separate q, k, v: a model that libllama builds from metadata (the mmproj stacks) has no fused qkv
+    if ".attn_qkv." in new_name:
+        for part, chunk in zip(("attn_q", "attn_k", "attn_v"), t.chunk(3, dim=0)):
+            yield (new_name.replace("attn_qkv", part), chunk.contiguous())
+        return
+    yield (new_name, t)
+
+
+@ModelBase.register("TinyVocaModel")
+@ModelBase.example("n4ze3m/tiny-voca")
+class TinyVocaTextModel(TextModel):
+    model_arch = gguf.MODEL_ARCH.TINY_VOCA
+
+    def index_tensors(self, remote_hf_model_id: str | None = None) -> dict[str, Callable[[], Tensor]]:
+        del remote_hf_model_id
+        state = _load_state(self.dir_model)
+        out: dict[str, Callable[[], Tensor]] = {}
+        for name, t in state.items():
+            if name == "tokens.weight" or name.startswith("encoder."):
+                out[name] = (lambda t=t: t)
+        return out
+
+    def set_vocab(self):
+        config = self.hparams["tiny_voca"]
+        n_vocab = config["model"]["vocab_size"]
+        tokens: list[str] = [""] * n_vocab
+        toktypes = [gguf.TokenType.NORMAL] * n_vocab
+        for text, i in config["vocab"].items():
+            tokens[i] = text
+            if text.startswith("<") and text.endswith(">"):
+                toktypes[i] = gguf.TokenType.CONTROL
+        assert all(tokens)
+
+        # the prompt is split into phonemes by the audio pipeline, see mtmd-helper-gen.cpp
+        self.gguf_writer.add_tokenizer_model("llama")
+        self.gguf_writer.add_tokenizer_pre("default")
+        self.gguf_writer.add_token_list(tokens)
+        self.gguf_writer.add_token_scores([0.0] * n_vocab)
+        self.gguf_writer.add_token_types(toktypes)
+        self.gguf_writer.add_bos_token_id(config["vocab"]["<bos>"])
+        self.gguf_writer.add_eos_token_id(config["vocab"]["<eos>"])
+        self.gguf_writer.add_pad_token_id(config["vocab"]["<pad>"])
+        self.gguf_writer.add_unk_token_id(config["vocab"]["<unk>"])
+        self.gguf_writer.add_add_bos_token(False)
+        self.gguf_writer.add_add_eos_token(False)
+
+        dicts = _g2p_dicts(self.dir_model)
+        if dicts is not None:
+            template_path = Path(__file__).parent.parent / "models" / "templates" / "n4ze3m-tiny-voca-tts.jinja"
+            with open(template_path, encoding="utf-8") as f:
+                self.gguf_writer.add_chat_template([{"name": "tts", "template": f.read()}])
+            self.gguf_writer.add_tts_dicts(dicts)
+
+    def set_gguf_parameters(self):
+        super().set_gguf_parameters()
+        self.gguf_writer.add_causal_attention(False)
+        self.gguf_writer.add_pooling_type(gguf.PoolingType.NONE)
+
+    def modify_tensors(self, data_torch: Tensor, name: str, bid: int | None) -> Iterable[tuple[str, Tensor]]:
+        del bid
+        if name == "tokens.weight":
+            yield ("token_embd.weight", data_torch)
+            return
+        if name.startswith("encoder.norm."):
+            yield ("output_norm." + name.rsplit(".", 1)[1], data_torch)
+            return
+        res = _block_name(name.removeprefix("encoder."))
+        if res is None:
+            raise ValueError(f"unexpected tensor {name}")
+        yield from _block_tensors(res[1], data_torch)
+
+    def tensor_force_quant(self, name, new_name, bid, n_dims):
+        del name, bid, n_dims
+        # ggml_ssm_conv takes F32 weights; token_embd is also read row by row on the host side
+        if ".ssm_conv1d." in new_name or new_name == "token_embd.weight":
+            return gguf.GGMLQuantizationType.F32
+        return False
+
+
+@ModelBase.register("TinyVocaModel")
+@ModelBase.example("n4ze3m/tiny-voca")
+class TinyVocaMmprojModel(MmprojModel):
+    has_vision_encoder = False
+    has_audio_encoder = False
+
+    def get_audio_config(self) -> dict[str, Any] | None:
+        return self.global_config.get("audio_config")
+
+    def index_tensors(self, remote_hf_model_id: str | None = None) -> dict[str, Callable[[], Tensor]]:
+        del remote_hf_model_id
+        state = _load_state(self.dir_model)
+        out: dict[str, Callable[[], Tensor]] = {}
+        for name, t in state.items():
+            if name.startswith("encoder.") or name in ("voices.weight", "frame_voices.weight"):
+                continue
+            out[name] = (lambda t=t: t)
+        return out
+
+    @classmethod
+    def filter_tensors(cls, item: tuple[str, Callable[[], Tensor]]) -> tuple[str, Callable[[], Tensor]] | None:
+        return item
+
+    def set_gguf_parameters(self):
+        c = self.global_config["tiny_voca"]["model"]
+        self.gguf_writer.add_file_type(self.ftype)
+        self.gguf_writer.add_clip_has_gen_audio_encoder(True)
+        self.gguf_writer.add_clip_gen_audio_projector_type(gguf.VisionProjectorType.TINY_VOCA)
+        self.gguf_writer.add_gen_audio_projection_dim(self.n_embd_text)
+        self.gguf_writer.add_gen_audio_embedding_length(c["channels"])
+        self.gguf_writer.add_gen_audio_feed_forward_length(c["feedforward"])
+        self.gguf_writer.add_gen_audio_block_count(c["acoustic_layers"])
+        self.gguf_writer.add_gen_audio_head_count(c["heads"])
+        self.gguf_writer.add_gen_audio_attention_layernorm_eps(1e-5)
+        self.gguf_writer.add_gen_audio_default_voice(DEFAULT_VOICE)
+
+    def modify_tensors(self, data_torch: Tensor, name: str, bid: int | None) -> Iterable[tuple[str, Tensor]]:
+        del bid
+        # from generate_extra_tensors(), named already
+        if name.startswith(("voice.", "a.gen.")):
+            yield (name, data_torch)
+            return
+        stack, _, rest = name.partition(".")
+        if stack in STACK_PREFIX:
+            prefix = STACK_PREFIX[stack]
+            if rest.startswith("norm."):
+                yield (prefix + "output_norm." + rest.rsplit(".", 1)[1], data_torch)
+                return
+            res = _block_name(rest)
+            if res is None:
+                raise ValueError(f"unexpected tensor {name}")
+            yield from _block_tensors(prefix + res[1], data_torch)
+            return
+        module, suffix = name.rsplit(".", 1)
+        if module not in MMPROJ_MAP:
+            raise ValueError(f"unexpected tensor {name}")
+        yield (MMPROJ_MAP[module] + "." + suffix, data_torch)
+
+    def tensor_force_quant(self, name, new_name, bid, n_dims):
+        del name, bid
+        # the blocks of the stacks are loaded by libllama, like the text model
+        if new_name.startswith(tuple(STACK_PREFIX.values())) and ".ssm_conv1d." not in new_name and n_dims >= 2:
+            return False
+        # the rest is small and partly read on the host
+        return gguf.GGMLQuantizationType.F32
+
+    def generate_extra_tensors(self) -> Iterable[tuple[str, Tensor]]:
+        # the unknown-word model of dizione (letters -> CMUdict phonemes), run on the host by mtmd_gen_audio_g2p()
+        g2p_dir = self.dir_model / "phonemizer"
+        if (g2p_dir / "g2p.npz").is_file():
+            with open(g2p_dir / "g2p.json", encoding="utf-8") as f:
+                settings = json.load(f)
+            if settings["letters"] != G2P_LETTERS or settings["symbols"] != G2P_SYMBOLS or settings["convs"] != 0 or settings["slots"] != 2:
+                raise ValueError("unsupported g2p model in phonemizer/g2p.json")
+            with np.load(g2p_dir / "g2p.npz") as arrays:
+                for name in arrays.files:
+                    yield ("a.gen.g2p." + name.replace("__", "."), torch.from_numpy(arrays[name].astype(np.float32)))
+        config = self.global_config["tiny_voca"]
+        state = _load_state(self.dir_model)
+        # a voice row is the embedding added to the encoder input, then the one added to the frames
+        for i, voice in enumerate(config["voices"]):
+            yield (f"voice.{voice}", torch.cat([state["voices.weight"][i], state["frame_voices.weight"][i]]).float())
+        # class of each token: 0 = pause (special or punctuation), 1 = consonant or <unk>, 2 = vowel
+        vocab = config["vocab"]
+        cls = torch.zeros(len(vocab), dtype=torch.float32)
+        for p in CONSONANTS + ("<unk>",):
+            cls[vocab[p]] = 1.0
+        for p in VOWELS:
+            cls[vocab[p]] = 2.0
+        yield ("a.gen.token_class", cls)
diff --git a/docs/multimodal/tts.md b/docs/multimodal/tts.md
index 45e838da5..5d0cc6fdb 100644
--- a/docs/multimodal/tts.md
+++ b/docs/multimodal/tts.md
@@ -16,6 +16,7 @@ Supported models:
 - [YuE2](#yue2) (songs)
 - [MiniMax Music 3](#minimax-music-3) (songs)
 - [Kokoro](#kokoro)
+- [tiny-voca](#tiny-voca)

 The sections below list the params supported by each model, with `llama-tts` examples. In `llama-server`, the same options are set with `--tts-lang` and `--tts-voice`, or per request (see the [`/tts` endpoint](../../tools/server/README.md#post-tts-generate-speech-audio-from-text)).

@@ -423,3 +424,46 @@ python -m nltk.downloader averaged_perceptron_tagger_eng
 ```

 Without them the converter writes no `tts` template, and the prompt must be a phoneme string (as produced by misaki).
+
+## tiny-voca
+
+[tiny-voca](https://huggingface.co/n4ze3m/tiny-voca): a 4.5M-parameter English TTS model. A phoneme encoder (the text model), then a duration predictor, two frame transformers and a source-filter synthesizer (in the mmproj).
+
+Available params:
+- `--tts-voice` can be `M2` (default) or `F2`
+- Note: `--tts-speaker-file` is not supported, the model has no voice cloning
+- Note: sampling params are not used, the model is not autoregressive. `--seed` only changes the noise of the synthesizer
+- `speed` (`llama-server` only) changes the speaking rate, from `0.25` to `4`; `loudness` is not supported
+
+Example usage:
+
+```sh
+llama-tts -m tiny-voca.gguf \
+    -mm mmproj-tiny-voca.gguf \
+    -p "Hello! The meeting moved to 3:30 pm." \
+    --tts-voice F2 \
+    --output out.wav
+```
+
+The model speaks phonemes. The text to phonemes step is the model's `tts` chat template, a port of [dizione](https://github.com/n4ze3m/dizione) (the reference one), with its pronunciation dictionary and tables stored in the model:
+- the choice of a pronunciation for each word: exceptions and names, the dictionary, function words and their sentence stress, words spelled alike and read differently ("I read it yesterday", "to read"), hyphenated words, capitals read letter by letter, mixed case ("iPhone"), stretched words ("soooo"), accents, chat spellings ("lol", "btw")
+- a word missing from the dictionary is built from a known word when it can be (endings, prefixes, compounds), else it goes through the unknown-word model of dizione (stored in the mmproj) and its conversion rules
+- a pronunciation can be given in the text, in CMUdict notation or as phonemes: `Say {HH AH0 L OW1} to Samir{s ə m ˈɪɹ}`
+- the text rules are ported too: numbers, money, times, dates, telephone numbers, versions, codes, units, fractions, ranges, math signs and comparisons, web addresses and file names, abbreviations, chat spellings
+
+With `llama-server`, send `"raw_prompt": true` to pass phonemes directly, in the format printed by dizione: symbols separated by spaces, `<sp>` between words, a stress mark (`ˈ` or `ˌ`) before a stressed vowel, e.g. `h ə l ˈoʊ <sp> w ˈɜː l d .`
+
+Each sentence is spoken as one utterance. The template splits the text as the reference does (before a capital or a figure, not after a title or an initial), ends each sentence with `<eos>`, and adds a full stop when the text ends without a mark. Phonemes without `<eos>` are cut after each `.`, `?`, `!` or `…`, and a sentence without a final mark gets a full stop.
+
+**Note for GGUF conversion:**
+
+Convert the root of the repository (it needs `config.json`, `tiny-voca.pt` and `phonemizer/lexicon.tsv`). The other tables of the `tts` template come from the `dizione` python package; without it the converter writes no `tts` template, and the prompt must be phonemes:
+
+```sh
+pip install git+https://github.com/n4ze3m/dizione
+```
+
+```sh
+python convert_hf_to_gguf.py path/to/tiny-voca --outfile tiny-voca.gguf
+python convert_hf_to_gguf.py path/to/tiny-voca --mmproj --outfile mmproj-tiny-voca.gguf
+```
diff --git a/docs/tts-norm-patterns.md b/docs/tts-norm-patterns.md
index 563e8509a..c377652c2 100644
--- a/docs/tts-norm-patterns.md
+++ b/docs/tts-norm-patterns.md
@@ -9,6 +9,8 @@ Jinja has no regex support, and exposing `std::regex` / Python `re` would not he

 A third function, `tts_dict(name, key)`, gives the template access to dictionaries stored in the model (e.g. a pronunciation lexicon).

+A fourth one, `tts_g2p(word)`, gives the pronunciation of a word by the letter-to-sound model of the mmproj, when the caller provides one (see below).
+
 Implementations:
 - C++: [common/chat-tts-norm.cpp](../common/chat-tts-norm.cpp), injected into task templates by `common_chat_template_render()` in [common/chat.cpp](../common/chat.cpp). They are not available to chat templates.
 - Python reference: [scripts/chat_tts_norm.py](../scripts/chat_tts_norm.py), for use with `jinja2` (`env.globals["tts_norm_scan"] = chat_tts_norm.tts_norm_scan`)
@@ -87,6 +89,17 @@ The lookup is exact: no case folding and no normalization, do them in the templa

 The GGUF lists the names of the dictionaries in `tokenizer.tts_dicts`. Each one is stored as two arrays of strings of the same length: `tokenizer.tts_dict.<name>.keys`, sorted by the UTF-8 bytes of the key, and `tokenizer.tts_dict.<name>.values`. A value is a JSON document (string, number, list, object, ...) and is returned decoded. `GGUFWriter.add_tts_dicts(dicts)` writes all of this.

+### `tts_g2p(word)`
+
+Pronunciation of `word` by a letter-to-sound model, as phonemes separated by spaces, in the notation of the model (e.g. CMUdict with stress digits for tiny-voca), or `none`. The template converts it to the symbols of the model. It is `none` when the model has no letter-to-sound model, or when the caller gives none: `common_chat_get_tts_prompt()` takes it as an optional callback, `llama-tts` and `llama-server` pass `mtmd_gen_audio_g2p()`. A template must keep working without it, e.g. by spelling the word.
+
+```jinja
+{%- set g = tts_g2p(word) -%}
+{%- if g is none -%}{{ spell(word) }}{%- else -%}{{ from_cmudict(word, g.split(' ')) }}{%- endif -%}
+```
+
+There is no Python reference for it: a test passes its own function (`env.globals["tts_g2p"]`).
+
 ## Pattern syntax

 Patterns operate on Unicode codepoints. Invalid UTF-8 bytes (C++ only) are seen as U+FFFD, one per byte, and preserved in the output.
diff --git a/gguf-py/gguf/constants.py b/gguf-py/gguf/constants.py
index d55e5a96f..c33a579b0 100644
--- a/gguf-py/gguf/constants.py
+++ b/gguf-py/gguf/constants.py
@@ -771,6 +771,7 @@ class MODEL_ARCH(IntEnum):
     POCKETTTS        = auto()
     ZONOS3           = auto()
     KOKORO           = auto()
+    TINY_VOCA        = auto()
     K2HORIZON        = auto()
     KOLIBRI1         = auto()
     LIMITE           = auto()
@@ -1758,6 +1759,7 @@ MODEL_ARCH_NAMES: dict[MODEL_ARCH, str] = {
     MODEL_ARCH.POCKETTTS:        "pockettts",
     MODEL_ARCH.ZONOS3:           "zonos3",
     MODEL_ARCH.KOKORO:           "kokoro",
+    MODEL_ARCH.TINY_VOCA:        "tiny-voca",
     MODEL_ARCH.K2HORIZON:        "k2-horizon",
     MODEL_ARCH.KOLIBRI1:         "kolibri1",
     MODEL_ARCH.LIMITE:           "limite",
@@ -6866,6 +6868,19 @@ MODEL_TENSORS: dict[MODEL_ARCH, list[MODEL_TENSOR]] = {
         MODEL_TENSOR.FFN_DOWN,
         MODEL_TENSOR.FFN_UP,
     ],
+    MODEL_ARCH.TINY_VOCA: [
+        MODEL_TENSOR.TOKEN_EMBD,
+        MODEL_TENSOR.OUTPUT_NORM,
+        MODEL_TENSOR.ATTN_NORM,
+        MODEL_TENSOR.ATTN_Q,
+        MODEL_TENSOR.ATTN_K,
+        MODEL_TENSOR.ATTN_V,
+        MODEL_TENSOR.ATTN_OUT,
+        MODEL_TENSOR.FFN_NORM,
+        MODEL_TENSOR.SSM_CONV1D,
+        MODEL_TENSOR.FFN_UP,
+        MODEL_TENSOR.FFN_DOWN,
+    ],
     MODEL_ARCH.KOKORO: [
         MODEL_TENSOR.TOKEN_EMBD,
         MODEL_TENSOR.TOKEN_EMBD_NORM,
@@ -7356,6 +7371,7 @@ class VisionProjectorType:
     FISH_S2_GEN = "fish_s2_gen" # audio generation: fast AR code predictor + Fish DAC decoder
     FISH_S2_SPKENC = "fish_s2_spkenc" # audio: Fish DAC encoder + RVQ, reference clip -> prompt rows
     KOKORO = "kokoro" # audio generation: StyleTTS2 prosody predictor + iSTFTNet decoder
+    TINY_VOCA = "tiny_voca" # audio generation: duration predictor, frame transformers, source-filter vocoder
     HUNYUANVL      = "hunyuanvl"
     PARAKEET       = "parakeet"  # audio
     MINIMAXM3      = "minimax_m3"
diff --git a/models/templates/n4ze3m-tiny-voca-tts.jinja b/models/templates/n4ze3m-tiny-voca-tts.jinja
new file mode 100644
index 000000000..a5eb18a68
--- /dev/null
+++ b/models/templates/n4ze3m-tiny-voca-tts.jinja
@@ -0,0 +1,2672 @@
+{#- tiny-voca "tts" template: english text -> phonemes, a port of dizione (the reference text to phonemes step)
+    output: symbols separated by spaces, <sp> between words, a stress mark (ˈ or ˌ) before a stressed vowel,
+    <eos> after each sentence (one utterance of the model)
+    dictionaries, written by conversion/tiny_voca.py:
+    - lexicon:       word -> phones (CMUdict, converted by dizione)
+    - functions:     word -> [stress class, form, final form], see function_words.txt of dizione
+    - phrases:       words -> [stress class, forms, final form], function-word groups spoken as one unit
+    - exceptions:    word -> phones or CMUdict notation, they win over the lexicon
+    - names, name_starts: names of several words, one pronunciation per word
+    - letters:       letter -> phones, for initialisms
+    - align:         CMUdict phoneme -> {letters: log probability}, to align a spelling with a pronunciation
+    - homographs, homograph_shared, homograph_plurals, homograph_rules: words read in two ways ("read", "live")
+    - chat:          chat spelling -> [words, only as a reply]
+    - accents:       accented letter -> plain letters
+    - abbreviations: abbreviation -> [when, words]
+    - units, exact_units, patterns: unit after a number -> [one, many], and the unit names as one pattern
+    - endings:       ending of a file name or an address -> "word" or "letters"
+    - nfkc:          character -> its compatibility form ("…" -> "...")
+    tts_g2p() is the unknown-word model (CMUdict notation); without it an unknown word is spelled
+    ported: the text rules (normalize.py), the choice of a pronunciation for each word (engine.py, morphology.py,
+    align.py, convert.py, homographs.py, sentence.py) and the split into sentences of tiny-voca (infer.py)
+    not ported: the composition of combining marks (NFKC of a sequence)
+    llama.cpp runs this with its own jinja engine: strings are sliced and measured in bytes, a negative string index
+    is undefined, a string cannot be looped over, and a variable set in a macro is seen by the macros it calls
+    (it must not have the name of a macro) -#}
+{%- set VOWELS = ['ə', 'ɐ', 'ɚ', 'ᵻ', 'ɪ', 'i', 'iː', 'ɛ', 'æ', 'ʌ', 'ɑː', 'ɔ', 'ɔː', 'ʊ', 'uː', 'ɜː', 'eɪ', 'aɪ', 'ɔɪ', 'oʊ', 'aʊ', 'iə', 'oː',
+                  'ɑːɹ', 'ɔːɹ', 'oːɹ', 'ɪɹ', 'ɛɹ', 'ʊɹ', 'aɪɚ', 'aɪə', 'əl', 'n̩'] -%}
+{%- set SPLIT = {'ɑːɹ': 'ɑː', 'ɔːɹ': 'ɔː', 'oːɹ': 'oː', 'ɪɹ': 'ɪ', 'ɛɹ': 'ɛ', 'ʊɹ': 'ʊ'} -%}
+{%- set RHOTIC = ['ɚ', 'ɜː', 'aɪɚ'] -%}
+{%- set ONES = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve',
+                'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'] -%}
+{%- set TENS = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'] -%}
+{%- set ORDINALS = {'one': 'first', 'two': 'second', 'three': 'third', 'five': 'fifth', 'eight': 'eighth', 'nine': 'ninth', 'twelve': 'twelfth'} -%}
+
+{#- ---------- phones ---------- -#}
+{%- macro unstress(ps) -%}{{ ps | replace('ˈ', '') | replace('ˌ', '') }}{%- endmacro -%}
+
+{#- primary stress on the marked vowel (the last secondary one), else on the first vowel -#}
+{%- macro promote(ps) -%}
+{%- if 'ˈ' in ps -%}{{ ps }}
+{%- elif 'ˌ' in ps -%}{{ tts_norm_sub(ps, 'ˌ([^ˌ]*)$', 'ˈ%1') }}
+{%- else -%}
+  {%- set ns = namespace(out=[], done=false) -%}
+  {%- for t in ps.split(' ') -%}
+    {%- if not ns.done and t in VOWELS -%}{%- set ns.out = ns.out + ['ˈ' ~ t] -%}{%- set ns.done = true -%}
+    {%- else -%}{%- set ns.out = ns.out + [t] -%}
+    {%- endif -%}
+  {%- endfor -%}
+  {{ ns.out | join(' ') }}
+{%- endif -%}
+{%- endmacro -%}
+
+{#- letter names, the last letter takes the primary stress; a possessive takes the plural ending -#}
+{%- macro spell(word) -%}
+{%- set possessive = (word | lower).endswith("'s") -%}
+{%- set ns = namespace(out=[]) -%}
+{%- for c in tts_norm_scan((word[:-2] if possessive else word) | lower, '[a-z]') -%}
+  {%- if c.rule -%}{%- set ns.out = ns.out + [tts_dict('letters', c.text)] -%}{%- endif -%}
+{%- endfor -%}
+{%- set ns2 = namespace(out=[]) -%}
+{%- for p in ns.out -%}{%- set ns2.out = ns2.out + [p if loop.last else (p | replace('ˈ', 'ˌ'))] -%}{%- endfor -%}
+{{ plural(ns2.out | join(' ')) if possessive and ns2.out else ns2.out | join(' ') }}
+{%- endmacro -%}
+
+{#- ---------- one word: the order of dizione (engine.py) ----------
+    exceptions, lexicon; regular endings, prefixes and compounds of known words; "-leigh" and "-virus" words;
+    the unknown-word model (tts_g2p) + the conversion rules; else the word is spelled
+    a pronunciation is a string of phones separated by spaces, a stress mark before a stressed vowel -#}
+{%- set ARPA_VOWELS = ['AA', 'AE', 'AH', 'AO', 'AW', 'AY', 'EH', 'ER', 'EY', 'IH', 'IY', 'OW', 'OY', 'UH', 'UW'] -%}
+{%- set ARPA_CONSONANTS = {'B': 'b', 'CH': 'tʃ', 'D': 'd', 'DH': 'ð', 'F': 'f', 'G': 'ɡ', 'HH': 'h', 'JH': 'dʒ', 'K': 'k', 'L': 'l',
+                           'M': 'm', 'N': 'n', 'NG': 'ŋ', 'P': 'p', 'R': 'ɹ', 'S': 's', 'SH': 'ʃ', 'T': 't', 'TH': 'θ', 'V': 'v',
+                           'W': 'w', 'Y': 'j', 'Z': 'z', 'ZH': 'ʒ'} -%}
+{%- set FULL_VOWELS = {'AA': 'ɑː', 'AE': 'æ', 'AH': 'ʌ', 'AO': 'ɔː', 'AW': 'aʊ', 'AY': 'aɪ', 'EH': 'ɛ', 'ER': 'ɜː', 'EY': 'eɪ',
+                       'IH': 'ɪ', 'IY': 'iː', 'OW': 'oʊ', 'OY': 'ɔɪ', 'UH': 'ʊ', 'UW': 'uː'} -%}
+{%- set SIBILANTS = ['S', 'Z', 'SH', 'ZH', 'CH', 'JH'] -%}
+{%- set FORCE_SPELLINGS = ['forc', 'ford', 'sword', 'worn', 'torn', 'sworn', 'borne', 'shorn', 'pork', 'divorc'] -%}
+{%- set CONV_PREFIXES = ['dis', 'mis', 'un', 'in', 're', 'non', 'over', 'pre'] -%}
+{%- set WEAK_PREFIXES = ['be', 'de', 're', 'pre'] -%}
+{%- set IRE_ENDINGS = ['ing', 'ings', 'ingly', 'able', 'y', 'er', 'ers', 'est'] -%}
+{%- set VOWEL_LETTERS = 'aeiouy' -%}
+{%- set CONSONANT_LETTERS = 'bcdfghjklmnpqrstvwxz' -%}
+
+{#- phone of a symbol without its stress mark, and the stress (0, 1, 2) -#}
+{#- startswith, not t[:1]: llama.cpp slices strings by bytes, a stress mark has two -#}
+{%- macro sz(t) -%}{{ 1 if t.startswith('ˈ') else (2 if t.startswith('ˌ') else 0) }}{%- endmacro -%}
+{%- macro mk(phone, stress) -%}{{ 'ˈ' if stress | int == 1 else ('ˌ' if stress | int == 2 else '') }}{{ phone }}{%- endmacro -%}
+
+{#- one primary stress per word, no secondary stress next to the primary one (convert.py: _settle_stress) -#}
+{%- macro settle(ps) -%}
+{%- set toks = ps.split(' ') -%}
+{%- set ns = namespace(v=[], prim=[], ph=[], st=[]) -%}
+{%- for t in toks -%}
+  {%- set ns.ph = ns.ph + [unstress(t)] -%}{%- set ns.st = ns.st + [sz(t) | int] -%}
+  {%- if unstress(t) in VOWELS -%}{%- set ns.v = ns.v + [loop.index0] -%}{%- if sz(t) | int == 1 -%}{%- set ns.prim = ns.prim + [loop.index0] -%}{%- endif -%}{%- endif -%}
+{%- endfor -%}
+{%- set st = namespace(s=ns.st) -%}
+{%- for k in ns.prim[:-1] -%}{%- set st.s = st.s[:k] + [2] + st.s[k + 1:] -%}{%- endfor -%}
+{%- for k in ns.v -%}
+  {%- set p = loop.index0 -%}
+  {%- if st.s[k] == 2 -%}
+    {%- set hit = namespace(v=false) -%}
+    {%- for q in [p - 1, p + 1] -%}{%- if q >= 0 and q < ns.v | length and st.s[ns.v[q]] == 1 -%}{%- set hit.v = true -%}{%- endif -%}{%- endfor -%}
+    {%- if hit.v -%}{%- set st.s = st.s[:k] + [0] + st.s[k + 1:] -%}{%- endif -%}
+  {%- endif -%}
+{%- endfor -%}
+{%- set out = namespace(t=[]) -%}
+{%- for p in ns.ph -%}{%- set out.t = out.t + [mk(p, st.s[loop.index0])] -%}{%- endfor -%}
+{{ out.t | join(' ') }}
+{%- endmacro -%}
+
+{#- ---------- word endings (morphology.py) ---------- -#}
+{#- stem + ending, adjusted at the seam -#}
+{%- macro mjoin(stem, ending) -%}
+{%- if not stem or not ending -%}{{ (stem ~ ' ' ~ ending) | trim }}
+{%- else -%}
+  {%- set s = stem.split(' ') -%}{%- set e = ending.split(' ') -%}
+  {%- set last = unstress(s[-1]) -%}{%- set ls = sz(s[-1]) | int -%}
+  {%- set first = unstress(e[0]) -%}{%- set fs = sz(e[0]) | int -%}
+  {%- set r = namespace(s=s) -%}
+  {%- if first in VOWELS -%}
+    {%- if last in ['ɑːɹ', 'ɔːɹ', 'oːɹ', 'ɪɹ', 'ɛɹ'] -%}
+      {%- set r.s = s[:-1] + [mk({'ɑːɹ': 'ɑː', 'ɔːɹ': 'ɔː', 'oːɹ': 'oː', 'ɪɹ': 'ɪ', 'ɛɹ': 'ɛ'}[last], ls), 'ɹ'] -%}
+    {%- elif last in ['ɚ', 'ɜː', 'aɪɚ', 'ʊɹ'] -%}{%- set r.s = s + ['ɹ'] -%}
+    {%- elif last == 't' and s | length > 1 and (unstress(s[-2]) in VOWELS or unstress(s[-2]) == 'ɹ') and not fs
+            and not (first == 'ə' and e | length > 1 and unstress(e[1]) == 'n') -%}{%- set r.s = s[:-1] + ['ɾ'] -%}
+    {%- elif last == 'i' -%}{%- set r.s = s[:-1] + [mk('ɪ' if first != 'ɪ' else 'i', ls)] -%}
+    {%- endif -%}
+  {%- elif last == 'i' and first in ['n', 'l', 'f', 'm'] -%}{%- set r.s = s[:-1] + [mk('ɪ', ls)] -%}
+  {%- elif last == 'n' and first in ['k', 'ɡ'] -%}{%- set r.s = s[:-1] + ['ŋ'] -%}
+  {%- endif -%}
+  {{ (r.s + e) | join(' ') }}
+{%- endif -%}
+{%- endmacro -%}
+
+{%- macro plural(stem) -%}
+{%- set last = unstress(stem.split(' ')[-1]) -%}
+{{ mjoin(stem, 'ᵻ z' if last in ['s', 'z', 'ʃ', 'ʒ', 'tʃ', 'dʒ'] else ('s' if last in ['p', 't', 'k', 'f', 'θ', 'ʔ'] else 'z')) }}
+{%- endmacro -%}
+{%- macro past(stem) -%}
+{%- set last = unstress(stem.split(' ')[-1]) -%}
+{{ mjoin(stem, 'ᵻ d' if last in ['t', 'd', 'ɾ'] else ('t' if last in ['p', 't', 'k', 'f', 'θ', 'ʔ', 's', 'ʃ', 'tʃ'] else 'd')) }}
+{%- endmacro -%}
+{%- macro adverb(stem) -%}
+{%- set last = unstress(stem.split(' ')[-1]) -%}
+{{ mjoin(stem, 'i' if last in ['əl', 'l'] else 'l i') }}
+{%- endmacro -%}
+{%- macro compound(first, second) -%}{{ mjoin(first, second | replace('ˈ', 'ˌ')) }}{%- endmacro -%}
+
+{#- a short vowel before a single consonant at the end: "hop" (it doubles the consonant before -ed) -#}
+{%- macro short_stem(body) -%}
+{%- set n = body | length -%}
+{%- set c = 2 if body.endswith('th') else 1 -%}
+{%- if n > c and (body.endswith('th') or body[-1:] in 'bcdfgklmnprstvz') and body[n - c - 1] in 'aeiou'
+        and (n - c - 1 == 0 or body[n - c - 2] not in 'aeiou') -%}1{%- endif -%}
+{%- endmacro -%}
+
+{#- spellings the stem may have had before an ending starting with a vowel, separated by '|' -#}
+{%- macro stems(body) -%}
+{%- set ns = namespace(out=[body]) -%}
+{#- negative indexes are slices: llama.cpp gives undefined for a negative string index -#}
+{%- if body | length > 2 and body[-1:] == body[-2:-1] and body[-1:] in CONSONANT_LETTERS -%}{%- set ns.out = ns.out + [body[:-1]] -%}{%- endif -%}
+{%- if body and body[-1:] in CONSONANT_LETTERS ~ 'u' -%}
+  {%- set ns.out = ([body ~ 'e'] + ns.out) if short_stem(body) else (ns.out + [body ~ 'e']) -%}
+{%- endif -%}
+{%- if body.endswith('i') and body | length > 2 -%}{%- set ns.out = ns.out + [body[:-1] ~ 'y'] -%}{%- endif -%}
+{{ ns.out | join('|') }}
+{%- endmacro -%}
+
+{%- set ENDINGS = [['fully', 'f əl i'], ['lessly', 'l ə s l i'], ['ness', 'n ə s'], ['less', 'l ə s'], ['ments', 'm ə n t s'],
+                   ['ment', 'm ə n t'], ['ful', 'f əl'], ['ship', 'ʃ ˌɪ p'], ['hood', 'h ˌʊ d'], ['like', 'l ˌaɪ k'], ['dom', 'd ə m'],
+                   ['ish', 'ɪ ʃ'], ['able', 'ə b əl'], ['ism', 'ɪ z ə m'], ['ist', 'ɪ s t'], ['wards', 'w ɚ d z'], ['ward', 'w ɚ d'],
+                   ['age', 'ɪ dʒ'], ['ers', 'ɚ z'], ['er', 'ɚ'], ['est', 'ɪ s t'], ['y', 'i']] -%}
+{#- prefix -> [form before a stem stressed on its first syllable, form otherwise] -#}
+{%- set MORPH_PREFIXES = [['under', 'ˌʌ n d ɚ', 'ˌʌ n d ɚ'], ['over', 'ˌoʊ v ɚ', 'ˌoʊ v ɚ'], ['anti', 'ˌæ n t i', 'ˌæ n t i'],
+                          ['semi', 's ˌɛ m i', 's ˌɛ m i'], ['non', 'n ˌɑː n', 'n ˌɑː n'], ['out', 'ˌaʊ t', 'ˌaʊ t'],
+                          ['dis', 'd ɪ s', 'd ˌɪ s'], ['mis', 'm ɪ s', 'm ˌɪ s'], ['un', 'ʌ n', 'ˌʌ n'], ['in', 'ɪ n', 'ˌɪ n'],
+                          ['re', 'ɹ ᵻ', 'ɹ ˌiː'], ['pre', 'p ɹ ˌiː', 'p ɹ ˌiː']] -%}
+{%- set BRITISH = [['isation', 'ization'], ['ising', 'izing'], ['ised', 'ized'], ['ises', 'izes'], ['ise', 'ize'], ['yse', 'yze'],
+                   ['our', 'or'], ['ourable', 'orable'], ['ours', 'ors'], ['oured', 'ored'], ['ouring', 'oring'], ['tre', 'ter'],
+                   ['tres', 'ters'], ['bre', 'ber'], ['ence', 'ense'], ['ogue', 'og'], ['mme', 'm'], ['lled', 'led'],
+                   ['lling', 'ling'], ['ller', 'ler'], ['oul', 'ol'], ['ae', 'e'], ['enqui', 'inqui']] -%}
+
+{#- a dictionary word that may be half of a compound: it is also listed with a regular ending ("webs") -#}
+{%- macro ordinary(w) -%}
+{%- if w | length >= 3 and (tts_dict('lexicon', w ~ 's') is not none or tts_dict('lexicon', w ~ 'es') is not none
+        or tts_dict('lexicon', w ~ 'ed') is not none or tts_dict('lexicon', w ~ 'ing') is not none) -%}{{ known(w) }}{%- endif -%}
+{%- endmacro -%}
+
+{%- macro dstem(s, depth) -%}
+{%- if s | length >= 2 -%}
+  {%- set k = known(s) -%}
+  {%- if k -%}{{ k }}{%- elif depth < 2 -%}{{ derive(s, depth + 1) }}{%- endif -%}
+{%- endif -%}
+{%- endmacro -%}
+
+{#- the first candidate stem that is known, built with the ending; 'build' is the name of the ending -#}
+{%- macro dfirst(candidates, build, depth) -%}
+{%- set ns = namespace(done=false) -%}
+{%- for c in candidates -%}
+  {%- if not ns.done -%}
+    {%- set f = dstem(c, depth) -%}
+    {%- if f -%}
+      {%- set ns.done = true -%}
+      {%- if build == 'plural' -%}{{ plural(f) }}
+      {%- elif build == 'past' -%}{{ past(f) }}
+      {%- elif build == 'adverb' -%}{{ adverb(f) }}
+      {%- elif build == 'same' -%}{{ f }}
+      {%- elif build == 'ves' -%}{%- if unstress(f.split(' ')[-1]) == 'f' -%}{{ mjoin((f.split(' ')[:-1] + ['v']) | join(' '), 'z') }}{%- endif -%}
+      {%- else -%}{{ mjoin(f, build) }}
+      {%- endif -%}
+    {%- endif -%}
+  {%- endif -%}
+{%- endfor -%}
+{%- endmacro -%}
+
+{#- pronunciation of a word from a shorter dictionary word, or '' (morphology.derive) -#}
+{%- macro derive(w, depth) -%}
+{%- set n = w | length -%}
+{%- set r = namespace(v='', done=false) -%}
+{%- if w.endswith("'s") -%}{%- set r.v = dfirst([w[:-2]], 'plural', depth) -%}{%- set r.done = true -%}
+{%- elif w.endswith("s'") -%}{%- set r.v = dfirst([w[:-1]], 'same', depth) -%}{%- set r.done = true -%}
+{%- endif -%}
+{%- if not r.done and w.endswith('ies') and n > 4 -%}{%- set r.v = dfirst([w[:-3] ~ 'y'], 'plural', depth) -%}{%- endif -%}
+{%- if not r.done and not r.v and w.endswith('ves') and n > 4 -%}{%- set r.v = dfirst([w[:-3] ~ 'f', w[:-3] ~ 'fe'], 'ves', depth) -%}{%- endif -%}
+{%- if not r.done and not r.v and w.endswith('es') and n > 3 and w[-3:-2] in 'sxzh' -%}{%- set r.v = dfirst([w[:-2]], 'plural', depth) -%}{%- endif -%}
+{%- if not r.done and not r.v and w.endswith('s') and not (w.endswith('ss') or w.endswith('us') or w.endswith('is')) and n > 3 -%}{%- set r.v = dfirst([w[:-1]], 'plural', depth) -%}{%- endif -%}
+{%- if not r.done and not r.v and w.endswith('ied') and n > 4 -%}{%- set r.v = dfirst([w[:-3] ~ 'y'], 'past', depth) -%}{%- endif -%}
+{%- if not r.done and not r.v and w.endswith('ed') and n > 3 -%}{%- set r.v = dfirst(stems(w[:-2]).split('|'), 'past', depth) -%}{%- endif -%}
+{%- if not r.done and not r.v and w.endswith('ing') and n > 4 -%}{%- set r.v = dfirst(stems(w[:-3]).split('|'), 'ɪ ŋ', depth) -%}{%- endif -%}
+{%- if not r.done and not r.v and w.endswith('ily') and n > 4 -%}{%- set r.v = dfirst([w[:-3] ~ 'y'], 'adverb', depth) -%}{%- endif -%}
+{%- if not r.done and not r.v and w.endswith('ly') and n > 3 -%}
+  {%- set r.v = dfirst([w[:-2], w[:-2] ~ 'le', w[:-1] ~ 'e'] if (w.endswith('bly') or w.endswith('ply')) else [w[:-2]], 'adverb', depth) -%}
+{%- endif -%}
+{%- if not r.done and not r.v -%}
+  {%- for e in ENDINGS -%}
+    {%- if not r.v and w.endswith(e[0]) and n - e[0] | length > 2 -%}
+      {%- set body = w[:n - e[0] | length] -%}
+      {%- if not (e[0] == 'y' and body[-1:] not in CONSONANT_LETTERS) -%}
+        {%- set cands = stems(body).split('|') if unstress(e[1].split(' ')[0]) in VOWELS else ([body[:-1] ~ 'y', body] if body.endswith('i') else [body]) -%}
+        {%- set r.v = dfirst(cands, e[1], depth) -%}
+      {%- endif -%}
+    {%- endif -%}
+  {%- endfor -%}
+{%- endif -%}
+{%- if not r.done and not r.v -%}
+  {%- for p in MORPH_PREFIXES -%}
+    {%- if not r.v and w.startswith(p[0]) and n - p[0] | length > 2 -%}
+      {%- set f = dstem(w[p[0] | length:], depth) -%}
+      {%- if f -%}
+        {%- set fv = namespace(s=none) -%}
+        {%- for t in f.split(' ') -%}{%- if fv.s is none and unstress(t) in VOWELS -%}{%- set fv.s = sz(t) | int -%}{%- endif -%}{%- endfor -%}
+        {%- set r.v = mjoin(p[1] if fv.s == 1 else p[2], f) -%}
+      {%- endif -%}
+    {%- endif -%}
+  {%- endfor -%}
+{%- endif -%}
+{%- if not r.done and not r.v -%}
+  {%- for b in BRITISH -%}
+    {%- if not r.v and b[0] in w -%}{%- set halves = w.split(b[0]) -%}{%- set r.v = dstem(halves[0] ~ b[1] ~ halves[1:] | join(b[0]), depth) -%}{%- endif -%}
+  {%- endfor -%}
+{%- endif -%}
+{#- two dictionary words of four letters or more: "sandpiper" -#}
+{%- if not r.done and not r.v and depth == 0 -%}
+  {%- set best = namespace(score=-1, l='', r='') -%}
+  {%- for cut in range(4, n - 3) -%}
+    {%- set left = ordinary(w[:cut]) -%}
+    {%- if left -%}
+      {%- set right = ordinary(w[cut:]) -%}
+      {%- if right and [cut, n - cut] | min > best.score -%}{%- set best.score = [cut, n - cut] | min -%}{%- set best.l = left -%}{%- set best.r = right -%}{%- endif -%}
+    {%- endif -%}
+  {%- endfor -%}
+  {%- if best.l -%}{%- set r.v = compound(best.l, best.r) -%}{%- endif -%}
+{%- endif -%}
+{{ r.v }}
+{%- endmacro -%}
+
+{#- ---------- CMUdict notation -> phones (align.py, convert.py) ---------- -#}
+{#- letters of word spelling each phoneme: runs separated by '|', or 'NONE' (Viterbi with the table of align.json) -#}
+{%- macro align(word, bases) -%}
+{%- set L = word | length -%}{%- set C = bases | length -%}
+{%- if C == 0 or L > 4 * C -%}NONE
+{%- else -%}
+  {%- set LOW = -1000000000.0 -%}
+  {%- set first = namespace(r=[0.0]) -%}
+  {%- for i in range(L) -%}{%- set first.r = first.r + [LOW] -%}{%- endfor -%}
+  {%- set dp = namespace(best=[first.r], step=[[]]) -%}
+  {%- for j in range(1, C + 1) -%}
+    {%- set tbl = tts_dict('align', bases[j - 1]) -%}
+    {%- set prev = dp.best[j - 1] -%}
+    {%- set rw = namespace(row=[], tr=[]) -%}
+    {%- for i in range(L + 1) -%}
+      {%- set cell = namespace(v=LOW, k=0) -%}
+      {%- for k in range([4, i] | min + 1) -%}
+        {%- if prev[i - k] > LOW -%}
+          {%- set run = word[i - k:i] -%}
+          {%- set sc = tbl[run] if (tbl is not none and run in tbl) else (-14.0 - 2.0 * ((run | length) - 1) | abs) -%}
+          {%- set v = prev[i - k] + sc -%}
+          {%- if v > cell.v -%}{%- set cell.v = v -%}{%- set cell.k = k -%}{%- endif -%}
+        {%- endif -%}
+      {%- endfor -%}
+      {%- set rw.row = rw.row + [cell.v] -%}{%- set rw.tr = rw.tr + [cell.k] -%}
+    {%- endfor -%}
+    {%- set dp.best = dp.best + [rw.row] -%}{%- set dp.step = dp.step + [rw.tr] -%}
+  {%- endfor -%}
+  {%- if dp.best[C][L] <= LOW -%}NONE
+  {%- else -%}
+    {%- set bt = namespace(runs=[], i=L) -%}
+    {%- for j in range(C, 0, -1) -%}
+      {%- set k = dp.step[j][bt.i] -%}
+      {%- set bt.runs = [word[bt.i - k:bt.i]] + bt.runs -%}
+      {%- set bt.i = bt.i - k -%}
+    {%- endfor -%}
+    {{ bt.runs | join('|') }}
+  {%- endif -%}
+{%- endif -%}
+{%- endmacro -%}
+
+{#- the questions the conversion rules ask about segment i of the aligned word w (convert.py: _Word) -#}
+{%- macro cb(w, i) -%}{{ w.b[i] if i >= 0 and i < w.n else '' }}{%- endmacro -%}
+{%- macro cvowel(w, i) -%}{{ '1' if i >= 0 and i < w.n and w.b[i] in ARPA_VOWELS else '' }}{%- endmacro -%}
+{%- macro cunstressed(w, i) -%}{{ '1' if cvowel(w, i) and not w.s[i] else '' }}{%- endmacro -%}
+{%- macro cletters(w, i) -%}{%- for c in tts_norm_scan(w.r[i], '[aeiouy]+') -%}{{ c.text if c.rule else '' }}{%- endfor -%}{%- endmacro -%}
+{%- macro cbefore(w, i) -%}{{ w.word[:w.st[i]] if w.al else '' }}{%- endmacro -%}
+{%- macro cafter(w, i) -%}{{ w.word[w.st[i] + w.r[i] | length:] if w.al else '' }}{%- endmacro -%}
+{%- macro cends(w, i, syms) -%}{{ '1' if w.b[i + 1:] == syms else '' }}{%- endmacro -%}
+{%- macro cfirst_vowel(w, i) -%}{%- set ns = namespace(v=false) -%}{%- for b in w.b[:i] -%}{%- if b in ARPA_VOWELS -%}{%- set ns.v = true -%}{%- endif -%}{%- endfor -%}{{ '' if ns.v else '1' }}{%- endmacro -%}
+{%- macro cnext_stress(w, i) -%}
+{%- set ns = namespace(s='') -%}
+{%- for j in range(i + 1, w.n) -%}{%- if ns.s == '' and w.b[j] in ARPA_VOWELS -%}{%- set ns.s = w.s[j] | string -%}{%- endif -%}{%- endfor -%}
+{{ ns.s }}
+{%- endmacro -%}
+{%- macro cweak_prefix(w, i) -%}
+{%- set wp = namespace(v=false) -%}{%- for p in WEAK_PREFIXES -%}{%- if w.word.startswith(p) -%}{%- set wp.v = true -%}{%- endif -%}{%- endfor -%}
+{{ '1' if w.al and cfirst_vowel(w, i) and cletters(w, i) == 'e' and wp.v and cbefore(w, i) in ['b', 'd', 'r', 'pr'] else '' }}
+{%- endmacro -%}
+
+{%- macro north_or_force(w, i, before_vowel) -%}
+{%- set spelled = cletters(w, i) -%}
+{%- if not w.al -%}ɔː
+{%- elif spelled in ['oa', 'oo', 'ou'] -%}oː
+{%- elif spelled != 'o' -%}ɔː
+{%- else -%}
+  {%- set tail = w.r[i].split('o', 1)[1] ~ cafter(w, i) -%}
+  {%- set rest = tail[1:] -%}
+  {%- if not before_vowel -%}
+    {%- set fs = namespace(v=false) -%}{%- for s in FORCE_SPELLINGS -%}{%- if s in w.word -%}{%- set fs.v = true -%}{%- endif -%}{%- endfor -%}
+    {%- if rest.startswith('e') or (cbefore(w, i).endswith('p') and rest.startswith('t')) or fs.v -%}oː{%- else -%}ɔː{%- endif -%}
+  {%- elif rest.startswith('r') -%}ɔː
+  {%- elif rest in ['y', 'ies', 'ied', 'a', 'as', 'ah', 'er', 'ers'] or rest.startswith('ia') or rest.startswith('io') or rest.startswith('iu')
+          or rest.startswith('ing') or rest.startswith('age') -%}oː
+  {%- else -%}ɔː
+  {%- endif -%}
+{%- endif -%}
+{%- endmacro -%}
+
+{%- macro weak_i(w, i) -%}
+{%- set nxt = cb(w, i + 1) -%}{%- set nxt2 = cb(w, i + 2) -%}
+{%- if nxt == 'F' and nxt2 == 'AH' and cb(w, i + 3) == 'L' -%}i
+{%- elif (nxt == 'T' and nxt2 == 'IY') or (nxt == 'B' and nxt2 in ['AH', 'L']) or (nxt == 'F' and nxt2 == 'AY') -%}ᵻ
+{%- elif nxt == 'T' and cends(w, i, ['T']) and cb(w, i - 1) in ['R', 'ER'] and w.word.endswith('ite') -%}ᵻ
+{%- else -%}ɪ
+{%- endif -%}
+{%- endmacro -%}
+
+{%- macro weak_e(w, i) -%}
+{%- set nxt = cb(w, i + 1) -%}{%- set prev = cb(w, i - 1) -%}
+{%- if nxt == 'T' and (cends(w, i, ['T']) or cends(w, i, ['T', 'S'])) -%}{{ 'ə' if prev == 'L' else ('ᵻ' if prev == 'R' else 'ɪ') }}
+{%- elif nxt == 'S' and (cends(w, i, ['S', 'T']) or cends(w, i, ['S', 'T', 'L', 'IY'])) -%}ɪ
+{%- elif nxt == 'JH' -%}ɪ
+{%- elif nxt == 'K' and cb(w, i + 2) == 'T' -%}ɛ
+{%- elif nxt == 'D' and cends(w, i, ['D']) -%}ɪ
+{%- else -%}ə
+{%- endif -%}
+{%- endmacro -%}
+
+{%- macro inflection(w, i) -%}
+{%- if i + 1 < w.n and not (w.al and 'e' not in w.r[i] ~ w.r[i + 1]) -%}
+  {%- set nxt = cb(w, i + 1) -%}{%- set prev = cb(w, i - 1) -%}
+  {%- if nxt == 'D' and prev in ['T', 'D'] and (cends(w, i, ['D']) or cends(w, i, ['D', 'L', 'IY'])) -%}1
+  {%- elif nxt == 'Z' and prev in SIBILANTS and cends(w, i, ['Z']) -%}1
+  {%- endif -%}
+{%- endif -%}
+{%- endmacro -%}
+
+{%- macro first_syllable(w, i) -%}
+{%- set word = w.word -%}
+{%- if w.al and cfirst_vowel(w, i) and cnext_stress(w, i) in ['1', '2'] -%}
+  {%- if i == 0 and word.startswith('e') -%}
+    {%- if word.startswith('ex') -%}ɛ
+    {%- elif word[1:2] in ['n', 'm', 's'] and word[2:3] and word[2] not in VOWEL_LETTERS and word[2] != word[1] -%}ɛ
+    {%- else -%}ɪ
+    {%- endif -%}
+  {%- elif cweak_prefix(w, i) -%}
+    {%- if word.startswith('pre') or (word.startswith('be') and word[2:3] in ['c', 'g']) -%}ɪ{%- else -%}ᵻ{%- endif -%}
+  {%- elif cletters(w, i) == 'i' and word.startswith('di') and word | length > 3 and word[2] not in VOWEL_LETTERS and word[2] != 's'
+          and word[3] in VOWEL_LETTERS -%}ᵻ
+  {%- endif -%}
+{%- endif -%}
+{%- endmacro -%}
+
+{%- macro reduced(w, i) -%}
+{%- set name = w.b[i] -%}{%- set spelled = cletters(w, i) -%}
+{%- set nxt = cb(w, i + 1) -%}{%- set prev = cb(w, i - 1) -%}
+{%- set pre = first_syllable(w, i) -%}
+{%- if name == 'IH' and nxt == 'NG' -%}ɪ
+{%- elif inflection(w, i) -%}ᵻ
+{%- elif name == 'AH' and prev == 'Y' and 'u' in w.r[i - 1] ~ w.r[i] -%}ʊ
+{%- elif name == 'AH' and i == 0 and w.word.startswith('a') -%}ɐ
+{%- elif name == 'AH' and i == 0 and nxt == 'N' and w.word.startswith('un') -%}ʌ
+{%- elif name == 'AH' and spelled == 'a' and cbefore(w, i) in CONV_PREFIXES and w.r[i].startswith('a') -%}ɐ
+{%- elif pre -%}{{ pre }}
+{%- elif spelled == 'e' and nxt in ['K', 'G'] and w.r[i + 1].startswith('x') and cnext_stress(w, i) in ['1', '2'] -%}ɛ
+{%- elif spelled in ['i', 'ai', 'ui', 'y'] -%}{{ weak_i(w, i) }}
+{%- elif spelled == 'e' -%}{{ weak_e(w, i) }}
+{%- elif spelled == 'a' -%}
+  {%- if nxt == 'JH' or (nxt == 'N' and cb(w, i + 2) == 'JH') -%}ɪ
+  {%- elif nxt == 'S' and cends(w, i, ['S']) and w.word.endswith('ace') -%}ɪ
+  {%- elif name == 'IH' and not (nxt == 'T' and cends(w, i, ['T'])) -%}ɪ
+  {%- else -%}ə
+  {%- endif -%}
+{%- elif name == 'AH' and nxt == 'M' and cends(w, i, ['M']) and w.word.endswith('ome') -%}ʌ
+{%- elif name == 'IH' and spelled not in ['o', 'u', 'ou'] -%}ɪ
+{%- else -%}ə
+{%- endif -%}
+{%- endmacro -%}
+
+{#- phone and stress, as "phone|stress" -#}
+{%- macro unstressed_iy(w, i) -%}
+{%- set nxt = cb(w, i + 1) -%}
+{%- if nxt == '' or cends(w, i, ['Z']) or cends(w, i, ['D']) -%}i|0
+{%- elif cvowel(w, i + 1) -%}{{ 'i' if nxt == 'IH' and cb(w, i + 2) == 'NG' else 'ɪ' }}|0
+{%- elif cweak_prefix(w, i) -%}{{ 'ᵻ|0' if cnext_stress(w, i) == '1' else 'iː|2' }}
+{%- else -%}ɪ|0
+{%- endif -%}
+{%- endmacro -%}
+
+{#- citation form of word from its CMUdict pronunciation (a list of symbols with stress digits) -#}
+{%- macro convert(word, arpa) -%}
+{%- set bs = namespace(b=[], s=[]) -%}
+{%- for a in arpa -%}
+  {%- if a[-1:] in ['0', '1', '2'] -%}{%- set bs.b = bs.b + [a[:-1]] -%}{%- set bs.s = bs.s + [a[-1:] | int] -%}
+  {%- else -%}{%- set bs.b = bs.b + [a] -%}{%- set bs.s = bs.s + [0] -%}
+  {%- endif -%}
+{%- endfor -%}
+{%- set runs = align(word, bs.b) -%}
+{%- set al = runs != 'NONE' -%}
+{%- set empty = namespace(r=[]) -%}
+{%- for a in arpa -%}{%- set empty.r = empty.r + [''] -%}{%- endfor -%}
+{%- set rl = runs.split('|') if al else empty.r -%}
+{%- set pos = namespace(st=[], p=0) -%}
+{%- for r in rl -%}{%- set pos.st = pos.st + [pos.p] -%}{%- set pos.p = pos.p + r | length -%}{%- endfor -%}
+{%- set w = {'word': word, 'b': bs.b, 's': bs.s, 'al': al, 'r': rl, 'st': pos.st, 'n': bs.b | length} -%}
+{%- set o = namespace(ph=[], st=[], i=0) -%}
+{%- for _ in range(w.n) -%}
+  {%- if o.i < w.n -%}
+    {%- set i = o.i -%}
+    {%- set name = w.b[i] -%}{%- set stress = w.s[i] -%}
+    {%- set nxt = cb(w, i + 1) -%}
+    {%- if name not in ARPA_VOWELS -%}
+      {%- if name == 'Y' and nxt == 'AH' and cunstressed(w, i + 1) and cb(w, i - 1) in ['N', 'L'] and cb(w, i + 2) != 'L'
+              and (not al or 'i' in w.r[i] ~ w.r[i + 1]) -%}
+        {%- set o.ph = o.ph + ['iə'] -%}{%- set o.st = o.st + [0] -%}{%- set o.i = o.i + 1 -%}
+      {%- else -%}
+        {%- set o.ph = o.ph + [ARPA_CONSONANTS[name]] -%}{%- set o.st = o.st + [0] -%}
+      {%- endif -%}
+    {%- else -%}
+      {%- set before_vowel = cvowel(w, i + 2) -%}
+      {%- if name == 'ER' -%}
+        {%- set o.ph = o.ph + ['ɜː' if stress else 'ɚ'] -%}{%- set o.st = o.st + [stress] -%}
+        {%- if nxt == 'R' -%}{%- set o.i = o.i + 1 -%}{%- endif -%}
+        {%- if cvowel(w, o.i + 1) -%}{%- set o.ph = o.ph + ['ɹ'] -%}{%- set o.st = o.st + [0] -%}{%- endif -%}
+      {%- elif nxt == 'R' and name == 'AW' -%}
+        {%- set o.ph = o.ph + ['aʊ', 'ɚ'] -%}{%- set o.st = o.st + [stress, 0] -%}
+        {%- if before_vowel -%}{%- set o.ph = o.ph + ['ɹ'] -%}{%- set o.st = o.st + [0] -%}{%- endif -%}
+        {%- set o.i = o.i + 1 -%}
+      {%- elif nxt == 'R' and name in ['AA', 'AO', 'IH', 'IY', 'EH', 'UH', 'UW', 'AY'] -%}
+        {%- if name == 'AA' -%}{%- set q = 'ɑː' -%}{%- set single = 'ɑːɹ' -%}
+        {%- elif name == 'AO' -%}{%- set q = north_or_force(w, i, before_vowel) -%}{%- set single = q ~ 'ɹ' -%}
+        {%- elif name in ['IH', 'IY'] -%}{%- set q = 'iə' if stress and cletters(w, i) in ['e', 'eo'] else 'ɪ' -%}{%- set single = 'ɪɹ' -%}
+        {%- elif name == 'EH' -%}
+          {%- set doubled = (w.r[i + 1] ~ cafter(w, i + 1)).startswith('rr') or (w.r[i].endswith('r') and w.r[i + 1].startswith('r')) -%}
+          {%- set q = 'æ' if cletters(w, i) == 'a' and doubled else 'ɛ' -%}{%- set single = 'ɛɹ' -%}
+        {%- elif name in ['UH', 'UW'] -%}{%- set q = 'ʊɹ' -%}{%- set single = 'ʊɹ' -%}
+        {%- else -%}{%- set q = 'aɪɚ' if not al or cafter(w, i + 1) in IRE_ENDINGS else 'aɪ' -%}{%- set single = 'aɪɚ' -%}
+        {%- endif -%}
+        {%- if before_vowel -%}{%- set o.ph = o.ph + [q, 'ɹ'] -%}{%- set o.st = o.st + [stress, 0] -%}
+        {%- else -%}{%- set o.ph = o.ph + [single] -%}{%- set o.st = o.st + [stress] -%}
+        {%- endif -%}
+        {%- set o.i = o.i + 1 -%}
+      {%- elif name == 'AY' and nxt == 'ER' and cunstressed(w, i + 1) and w.r[i + 1] == 'er' and w.r[i] != 'i' -%}
+        {%- set o.ph = o.ph + ['aɪ'] -%}{%- set o.st = o.st + [stress] -%}
+      {%- elif name == 'AY' and nxt == 'ER' and cunstressed(w, i + 1) -%}
+        {%- set o.ph = o.ph + ['aɪɚ'] -%}{%- set o.st = o.st + [stress] -%}{%- set o.i = o.i + 1 -%}
+        {%- if cvowel(w, o.i + 1) -%}{%- set o.ph = o.ph + ['ɹ'] -%}{%- set o.st = o.st + [0] -%}{%- endif -%}
+      {%- elif name == 'AY' and nxt == 'AH' and cunstressed(w, i + 1) -%}
+        {%- set o.ph = o.ph + ['aɪə'] -%}{%- set o.st = o.st + [stress] -%}{%- set o.i = o.i + 1 -%}
+      {%- elif name == 'IY' and nxt == 'AH' and cunstressed(w, i + 1) and not cweak_prefix(w, i) -%}
+        {%- set after = cb(w, i + 2) -%}
+        {%- if stress != 1 and (after in ['L', 'D', 'T'] or (after == 'N' and cb(w, i + 3) == 'S')) -%}
+          {%- set o.ph = o.ph + ['ɪ'] -%}{%- set o.st = o.st + [0] -%}
+        {%- else -%}
+          {%- set o.ph = o.ph + ['iə'] -%}{%- set o.st = o.st + [stress] -%}{%- set o.i = o.i + 1 -%}
+        {%- endif -%}
+      {%- elif not stress and name == 'AH' and nxt == 'N' and cb(w, i - 1) == 'T' and cb(w, i + 2) != 'T'
+              and o.ph | length >= 2 and o.ph[-1] == 't' and o.ph[-2] in VOWELS and o.st[-2] -%}
+        {%- set o.ph = o.ph[:-1] + ['ʔ', 'n̩'] -%}{%- set o.st = o.st + [0] -%}{%- set o.i = o.i + 1 -%}
+      {%- elif not stress and name == 'AH' and nxt == 'W' and cb(w, i + 2) == 'AH' and cunstressed(w, i + 2) and 'u' in w.r[i] ~ w.r[i + 1] -%}
+        {%- set o.ph = o.ph + ['uː'] -%}{%- set o.st = o.st + [0] -%}{%- set o.i = o.i + 1 -%}
+      {%- elif not stress and name == 'AH' and nxt == 'L' and i > 0 and not (cvowel(w, i + 2) and w.s[i + 2]) -%}
+        {%- if cletters(w, i) == 'i' and cvowel(w, i + 2) -%}{%- set o.ph = o.ph + ['ɪ'] -%}{%- set o.st = o.st + [0] -%}
+        {%- elif cb(w, i - 1) == 'Y' and 'u' in w.r[i - 1] ~ w.r[i] -%}{%- set o.ph = o.ph + ['ʊ'] -%}{%- set o.st = o.st + [0] -%}
+        {%- else -%}{%- set o.ph = o.ph + ['əl'] -%}{%- set o.st = o.st + [0] -%}{%- set o.i = o.i + 1 -%}
+        {%- endif -%}
+      {%- elif not stress and name in ['AH', 'IH'] -%}
+        {%- set o.ph = o.ph + [reduced(w, i)] -%}{%- set o.st = o.st + [0] -%}
+      {%- elif name == 'IY' and (not stress or (stress == 2 and nxt == '')) -%}
+        {%- set u = unstressed_iy(w, i).split('|') -%}
+        {%- set o.ph = o.ph + [u[0]] -%}{%- set o.st = o.st + [u[1] | int] -%}
+      {%- elif not stress and name == 'AE' and i == 0 and (w.word.startswith('ad') or w.word.startswith('acc')) and nxt in ['D', 'K'] -%}
+        {%- set o.ph = o.ph + ['ɐ'] -%}{%- set o.st = o.st + [0] -%}
+      {%- elif name in ['AA', 'AO'] and cletters(w, i) == 'a' and (cbefore(w, i).endswith('w') or cbefore(w, i).endswith('qu')) and nxt == 'N' -%}
+        {%- set o.ph = o.ph + ['ɔ'] -%}{%- set o.st = o.st + [stress] -%}
+      {%- elif name == 'AO' and cletters(w, i) == 'o' -%}
+        {%- set o.ph = o.ph + ['ɔ'] -%}{%- set o.st = o.st + [stress] -%}
+      {%- else -%}
+        {%- set o.ph = o.ph + [FULL_VOWELS[name]] -%}{%- set o.st = o.st + [stress] -%}
+      {%- endif -%}
+    {%- endif -%}
+    {%- set o.i = o.i + 1 -%}
+  {%- endif -%}
+{%- endfor -%}
+{#- the flap: t between a vowel (or r) and an unstressed vowel -#}
+{%- set f = namespace(ph=o.ph) -%}
+{%- set n = o.ph | length -%}
+{%- for k in range(1, n - 1) -%}
+  {%- if f.ph[k] == 't' -%}
+    {%- set bf = f.ph[k - 1] -%}{%- set af = f.ph[k + 1] -%}{%- set ast = o.st[k + 1] -%}
+    {%- if not ((bf not in VOWELS and bf != 'ɹ') or bf in ['n̩', 'əl'] or af not in VOWELS or af == 'n̩') -%}
+      {%- set final_o = af == 'oʊ' and (k + 2 == n or (k + 3 == n and f.ph[k + 2] == 'z')) -%}
+      {%- if not (ast and not final_o) and not (af in ['ə', 'ɚ'] and k + 2 < n and f.ph[k + 2] == 'n') -%}
+        {%- set f.ph = f.ph[:k] + ['ɾ'] + f.ph[k + 1:] -%}
+      {%- endif -%}
+    {%- endif -%}
+  {%- endif -%}
+{%- endfor -%}
+{%- for k in range(n - 1) -%}
+  {%- if f.ph[k] == 'n' and f.ph[k + 1] in ['k', 'ɡ'] -%}{%- set f.ph = f.ph[:k] + ['ŋ'] + f.ph[k + 1:] -%}{%- endif -%}
+{%- endfor -%}
+{%- set out = namespace(t=[]) -%}
+{%- for p in f.ph -%}{%- set out.t = out.t + [mk(p, o.st[loop.index0])] -%}{%- endfor -%}
+{{ settle(out.t | join(' ')) }}
+{%- endmacro -%}
+
+{#- ---------- the lookup chain of one word ---------- -#}
+{#- exceptions (phones, or CMUdict notation), then the lexicon -#}
+{#- a pronunciation of the exception table: phones, or CMUdict notation -#}
+{%- macro entry(w, e) -%}
+{%- set first = e.split(' ')[0] -%}
+{%- set is_arpa = first in ARPA_CONSONANTS or (first[-1:] in ['0', '1', '2'] and first[:-1] in ARPA_VOWELS) -%}
+{{ convert(w, e.split(' ')) if is_arpa else e }}
+{%- endmacro -%}
+
+{%- macro known(w) -%}
+{%- set e = tts_dict('exceptions', w) -%}
+{%- if e is not none -%}{{ entry(w, e) }}
+{%- else -%}{{ tts_dict('lexicon', w) or '' }}
+{%- endif -%}
+{%- endmacro -%}
+
+{#- the letters the unknown-word model knows (accents removed: not done, the letter is dropped) -#}
+{%- macro spellable(w) -%}
+{%- set ns = namespace(s='') -%}
+{%- for c in tts_norm_scan(w | lower, "[a-z']") -%}{%- if c.rule -%}{%- set ns.s = ns.s ~ c.text -%}{%- endif -%}{%- endfor -%}
+{{ ns.s.strip("'") }}
+{%- endmacro -%}
+
+{#- the unknown-word model (tts_g2p, CMUdict notation), then the conversion rules; spelled without the model -#}
+{%- macro guess(w) -%}
+{%- set s = spellable(w) -%}
+{%- set g = tts_g2p(s) if s else none -%}
+{%- if g is none or not g -%}{{ spell(w) }}
+{%- else -%}
+  {%- set syms = g.split(' ') -%}
+  {#- an r with no vowel beside it is a syllable -#}
+  {%- set fx = namespace(s=[]) -%}
+  {%- for x in syms -%}
+    {%- set k = loop.index0 -%}
+    {%- set near = namespace(v=false) -%}
+    {#- the symbol before as already rewritten: of two such r in a row only the first becomes a syllable -#}
+    {%- if (k > 0 and fx.s[k - 1][-1:] in ['0', '1', '2']) or (k + 1 < syms | length and syms[k + 1][-1:] in ['0', '1', '2']) -%}{%- set near.v = true -%}{%- endif -%}
+    {%- set fx.s = fx.s + ['ER0' if x == 'R' and not near.v else x] -%}
+  {%- endfor -%}
+  {%- set hv = namespace(v=false) -%}
+  {%- for x in fx.s -%}{%- if x[-1:] in ['0', '1', '2'] -%}{%- set hv.v = true -%}{%- endif -%}{%- endfor -%}
+  {%- if not hv.v -%}{{ spell(w) }}{%- else -%}{{ promote(convert(s, fx.s)) }}{%- endif -%}
+{%- endif -%}
+{%- endmacro -%}
+
+{%- macro word(w) -%}
+{%- set k = known(w) -%}
+{%- if k -%}{{ k }}
+{%- else -%}
+  {%- set d = derive(w, 0) -%}
+  {%- if d -%}{{ settle(d) }}
+  {%- else -%}
+    {%- set p = from_parts(w) -%}
+    {%- if p -%}{{ p }}
+    {%- elif w.endswith("'s") and w | length > 3 -%}{{ plural(guess(w[:-2])) }}
+    {%- else -%}{{ guess(w) }}
+    {%- endif -%}
+  {%- endif -%}
+{%- endif -%}
+{%- endmacro -%}
+
+{#- a name in "-leigh" is said like the one in "-ley"; a word in "-virus" is its first part, then "virus" -#}
+{%- macro from_parts(w) -%}
+{%- set n = w | length -%}
+{%- if w.endswith("'s") and n > 3 -%}
+  {%- set f = from_parts(w[:-2]) -%}{%- if f -%}{{ plural(f) }}{%- endif -%}
+{%- else -%}
+  {%- set base = w[:-1] if w.endswith('s') else w -%}
+  {%- if base.endswith('leigh') and base | length >= 7 and base[-7:-6] in 'abcdefghijklmnopqrstuvwxyz' and base[-6:-5] in 'abcdefghijklmnopqrstuvwxyz' -%}
+    {{ word(base[:-5] ~ 'ley' ~ w[base | length:]) }}
+  {%- else -%}
+    {%- set ns = namespace(v='') -%}
+    {%- for tail in ['viruses', 'virus'] -%}
+      {%- if not ns.v and w.endswith(tail) and n - tail | length > 2 -%}
+        {%- set second = known(tail) -%}
+        {%- if second -%}{%- set ns.v = promote(settle(compound(word(w[:n - tail | length]), second))) -%}{%- endif -%}
+      {%- endif -%}
+    {%- endfor -%}
+    {{ ns.v }}
+  {%- endif -%}
+{%- endif -%}
+{%- endmacro -%}
+
+{#- ---------- homographs (homographs.py): "I read it yesterday" / "to read" ----------
+    hand-written rules first, then a linear classifier on the neighbouring words; toks is one sentence as written,
+    punctuation marks as their own tokens. Weights are integers (x10000) -#}
+{%- set UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' -%}
+{%- set LOWER = 'abcdefghijklmnopqrstuvwxyz' -%}
+{%- set HG_CLASSES = [
+    ['TO', ' to '],
+    ['DET', ' the a an this that these those every each no some any another such what which whose '],
+    ['POSS', ' my your his her its our their '],
+    ['SUBJ', ' i you he she it we they who '],
+    ['OBJ', ' me him us them '],
+    ['MODAL', " will would shall should can could may might must 'll 'd won't can't cannot couldn't shouldn't wouldn't let's please "],
+    ['DO', " do does did don't doesn't didn't "],
+    ['BE', " am is are was were be been being isn't aren't wasn't weren't i'm you're we're they're he's she's it's that's there's "],
+    ['HAVE', " have has had having haven't hasn't hadn't i've you've we've they've "],
+    ['NEG', ' not never '],
+    ['PREP', ' of in on at by for from with into onto over under about after before between through during without within against among around across behind beside near upon toward towards as than like '],
+    ['CONJ', ' and or but nor so yet if because although though while when where unless until since whether '],
+    ['ADV', ' very too quite rather more most less least also just only even still always often sometimes usually again now then here there soon already almost '],
+] -%}
+{%- set HG_SKIP_BEFORE = ['ADV', 'NEG', 'LY'] -%}
+{%- set HG_SKIP_AUX = ['ADV', 'NEG', 'LY', 'SUBJ', 'OBJ', 'DET', 'POSS'] -%}
+
+{#- coarse grammatical class of a neighbouring token, from closed lists and endings -#}
+{%- macro hg_tag(t) -%}
+{%- set c = t[:1] -%}
+{%- if t in ['<s>', '</s>'] -%}{{ t }}
+{%- elif c not in UPPER and c not in LOWER -%}{{ 'NUM' if c in '0123456789' else 'PUNCT' }}
+{%- else -%}
+  {%- set lower = t | lower -%}
+  {%- set n = lower | length -%}
+  {%- set ns = namespace(v='') -%}
+  {%- for cl in HG_CLASSES -%}{%- if not ns.v and (' ' ~ lower ~ ' ') in cl[1] -%}{%- set ns.v = cl[0] -%}{%- endif -%}{%- endfor -%}
+  {%- if ns.v -%}{{ ns.v }}
+  {%- elif lower.endswith("'s") and n > 3 -%}POS
+  {%- elif lower.endswith('ly') and n > 3 -%}LY
+  {%- elif lower.endswith('ing') and n > 4 -%}ING
+  {%- elif lower.endswith('ed') and n > 3 -%}ED
+  {%- elif lower.endswith('s') and not lower.endswith('ss') and n > 3 -%}S
+  {%- else -%}{{ 'CAP' if c in UPPER else 'W' }}
+  {%- endif -%}
+{%- endif -%}
+{%- endmacro -%}
+
+{#- the nearest token to the left that is not of a skipped class; "'ll" and "'ve" read as "will" and "have" -#}
+{%- macro hg_left(toks, index, skip) -%}
+{%- set ns = namespace(v='<s>', done=false) -%}
+{%- for p in range(index - 1, [index - 6, -1] | max, -1) -%}
+  {%- if not ns.done and hg_tag(toks[p]) not in skip -%}
+    {%- set lower = toks[p] | lower -%}
+    {%- set ns.v = 'will' if lower.endswith("'ll") else ('have' if lower.endswith("'ve") else lower) -%}
+    {%- set ns.done = true -%}
+  {%- endif -%}
+{%- endfor -%}
+{{ ns.v }}
+{%- endmacro -%}
+
+{#- one condition of a hand-written rule, see homograph_rules.txt -#}
+{%- macro hg_holds(name, words, toks, index, joined) -%}
+{%- if name == 'first' -%}{{ '1' if index == 0 else '' }}
+{%- elif name == 'joined' -%}{{ '1' if joined and index > 0 and toks[index - 1] | lower in words else '' }}
+{%- elif name == 'aux' -%}{{ '1' if hg_left(toks, index, HG_SKIP_AUX) in words else '' }}
+{%- elif name == 'before' -%}{{ '1' if hg_left(toks, index, HG_SKIP_BEFORE) in words else '' }}
+{%- elif name == 'next' -%}{{ '1' if (toks[index + 1] | lower if index + 1 < toks | length else '</s>') in words else '' }}
+{%- else -%}
+  {%- set ns = namespace(v='') -%}
+  {%- for t in toks[[0, index - 6] | max:index + 7] -%}{%- if t | lower in words -%}{%- set ns.v = '1' -%}{%- endif -%}{%- endfor -%}
+  {{ ns.v }}
+{%- endif -%}
+{%- endmacro -%}
+
+{#- index of the sense meant by written[place]; entry is the dictionary entry of the homograph "word" -#}
+{%- macro hg_choose(word, hg, written, place, joined) -%}
+{#- the sentence around the word -#}
+{%- set b = namespace(s=place, e=place) -%}
+{%- for _ in range(place) -%}{%- if b.s > 0 and written[b.s - 1] not in ['.', '?', '!'] -%}{%- set b.s = b.s - 1 -%}{%- endif -%}{%- endfor -%}
+{%- for _ in range(written | length - place) -%}{%- if b.e < written | length and written[b.e] not in ['.', '?', '!'] -%}{%- set b.e = b.e + 1 -%}{%- endif -%}{%- endfor -%}
+{%- set toks = written[b.s:b.e + 1] -%}
+{%- set index = place - b.s -%}
+{%- set n = toks | length -%}
+{%- set pick = namespace(i=-1, v=0) -%}
+{%- set rules = tts_dict('homograph_rules', word) -%}
+{%- for rule in (rules if rules is not none else []) -%}
+  {%- if pick.i < 0 -%}
+    {%- set ok = namespace(v=true) -%}
+    {%- for cond in rule[1] -%}{%- if ok.v and not hg_holds(cond[0], cond[1], toks, index, joined) -%}{%- set ok.v = false -%}{%- endif -%}{%- endfor -%}
+    {%- if ok.v -%}
+      {%- for s in hg.senses -%}{%- if pick.i < 0 and s[0] == rule[0] -%}{%- set pick.i = loop.index0 -%}{%- endif -%}{%- endfor -%}
+    {%- endif -%}
+  {%- endif -%}
+{%- endfor -%}
+{%- if pick.i >= 0 -%}{{ pick.i }}
+{%- else -%}
+  {#- the features that are on -#}
+  {%- set p2 = toks[index - 2] if index >= 2 else '<s>' -%}
+  {%- set p1 = toks[index - 1] if index >= 1 else '<s>' -%}
+  {%- set n1 = toks[index + 1] if index + 1 < n else '</s>' -%}
+  {%- set n2 = toks[index + 2] if index + 2 < n else '</s>' -%}
+  {%- set tp2 = hg_tag(p2) -%}{%- set tp1 = hg_tag(p1) -%}{%- set tn1 = hg_tag(n1) -%}{%- set tn2 = hg_tag(n2) -%}
+  {%- set f = namespace(shared=['tp1=' ~ tp1, 'tp2=' ~ tp2, 'tn1=' ~ tn1, 'tn2=' ~ tn2, 'tp2p1=' ~ tp2 ~ '+' ~ tp1,
+                                'tp1n1=' ~ tp1 ~ '+' ~ tn1, 'tn1n2=' ~ tn1 ~ '+' ~ tn2], own=[]) -%}
+  {%- if index == 0 -%}{%- set f.shared = f.shared + ['first'] -%}
+  {%- elif toks[index][:1] in UPPER -%}{%- set f.shared = f.shared + ['cap'] -%}
+  {%- endif -%}
+  {%- set f.own = ['p1=' ~ p1 | lower, 'p2=' ~ p2 | lower, 'n1=' ~ n1 | lower, 'n2=' ~ n2 | lower] -%}
+  {%- set target = toks[index] | lower -%}
+  {%- set near = namespace(w=[]) -%}
+  {%- for t in toks[[0, index - 6] | max:index + 7] -%}
+    {%- set lower = t | lower -%}
+    {%- if (t[:1] in UPPER or t[:1] in LOWER) and lower != target and lower not in near.w -%}
+      {%- set near.w = near.w + [lower] -%}{%- set f.own = f.own + ['w=' ~ lower] -%}
+    {%- endif -%}
+  {%- endfor -%}
+  {%- if hg.noun_verb -%}
+    {#- one score for the verb reading: the weights all such words share, and the word's own -#}
+    {%- set shared = tts_dict('homograph_shared', '') -%}
+    {%- set sc = namespace(v=shared + hg.bias[0]) -%}
+    {%- set own = hg.weights[0] -%}
+    {%- for name in f.shared -%}
+      {%- set sw = tts_dict('homograph_shared', name) -%}
+      {%- set sc.v = sc.v + (sw if sw is not none else 0) + (own[name] if name in own else 0) -%}
+    {%- endfor -%}
+    {%- for name in f.own -%}{%- set sc.v = sc.v + (own[name] if name in own else 0) -%}{%- endfor -%}
+    {#- the first of the highest scores: verb senses have the score, the others its opposite -#}
+    {%- for s in hg.senses -%}
+      {%- set v = sc.v if s[1] == 'v' else 0 - sc.v -%}
+      {%- if pick.i < 0 or v > pick.v -%}{%- set pick.i = loop.index0 -%}{%- set pick.v = v -%}{%- endif -%}
+    {%- endfor -%}
+  {%- else -%}
+    {%- for s in hg.senses -%}
+      {%- set own = hg.weights[loop.index0] -%}
+      {%- set sc = namespace(v=hg.bias[loop.index0]) -%}
+      {%- for name in f.shared + f.own -%}{%- set sc.v = sc.v + (own[name] if name in own else 0) -%}{%- endfor -%}
+      {%- if pick.i < 0 or sc.v > pick.v -%}{%- set pick.i = loop.index0 -%}{%- set pick.v = sc.v -%}{%- endif -%}
+    {%- endfor -%}
+  {%- endif -%}
+  {{ pick.i }}
+{%- endif -%}
+{%- endmacro -%}
+
+{#- a homograph, or the regular -s form of a noun-or-verb one ("records") -#}
+{%- macro hg_covers(w) -%}
+{{ '1' if tts_dict('homographs', w) is not none or (w.endswith('s') and tts_dict('homograph_plurals', w[:-1]) is not none) else '' }}
+{%- endmacro -%}
+
+{%- macro hg_pronounce(w, written, place, joined) -%}
+{%- set hg = tts_dict('homographs', w) -%}
+{%- if hg is not none -%}{{ hg.senses[hg_choose(w, hg, written, place, joined) | int][2] }}
+{%- else -%}
+  {%- set base = tts_dict('homographs', w[:-1]) -%}
+  {%- set ns = namespace(n=-1, v=-1) -%}
+  {%- for s in base.senses -%}
+    {%- if ns.n < 0 and s[1] == 'n' -%}{%- set ns.n = loop.index0 -%}{%- endif -%}
+    {%- if ns.v < 0 and s[1] == 'v' -%}{%- set ns.v = loop.index0 -%}{%- endif -%}
+  {%- endfor -%}
+  {#- "separates" can only be the verb -#}
+  {%- if ns.n >= 0 and base.senses[ns.n][0] == 'adj' -%}{{ plural(base.senses[ns.v][2]) }}
+  {%- else -%}{{ plural(base.senses[hg_choose(w[:-1], base, written, place, joined) | int][2]) }}
+  {%- endif -%}
+{%- endif -%}
+{%- endmacro -%}
+
+{#- ---------- one clause: words between two punctuation marks ----------
+    words: items {w: lower case, o: as written, a: joined to the word before (hyphen), spell: letter by letter}
+    written: the whole text as tokens, places: where each word sits in it (for the homographs) -#}
+{%- macro clause(words, written, places) -%}
+{#- units, in the order of dizione: letters, names of several words, function-word groups, homographs, function words, words -#}
+{%- set ns = namespace(units=[], i=0) -%}
+{%- set nw = words | length -%}
+{%- for _ in words -%}
+  {%- if ns.i < nw -%}
+    {%- set x = words[ns.i] -%}
+    {%- set w = x.w -%}
+    {%- set at = x.a -%}
+    {%- set found = namespace(name=0, phrase=0) -%}
+    {%- if not x.spell and not x.ps -%}
+      {#- the longest name, then the longest group, of plain words not joined by a hyphen -#}
+      {%- set longest = tts_dict('name_starts', w) -%}
+      {%- for size in (range(longest, 1, -1) | list if longest is not none else []) + [-2] -%}
+        {%- if not found.name and not found.phrase and ns.i + (size | abs) <= nw -%}
+          {%- set plain = namespace(v=true) -%}
+          {%- for y in words[ns.i + 1:ns.i + (size | abs)] -%}{%- if y.spell or y.a or y.ps -%}{%- set plain.v = false -%}{%- endif -%}{%- endfor -%}
+          {%- if plain.v -%}
+            {%- set key = words[ns.i:ns.i + (size | abs)] | map(attribute='w') | join(' ') -%}
+            {%- if size > 0 and tts_dict('names', key) is not none -%}{%- set found.name = size -%}
+            {%- elif size < 0 and tts_dict('phrases', key) is not none -%}{%- set found.phrase = 0 - size -%}
+            {%- endif -%}
+          {%- endif -%}
+        {%- endif -%}
+      {%- endfor -%}
+    {%- endif -%}
+    {%- if x.ps or x.spell -%}
+      {%- set ns.units = ns.units + [{'w': w, 'ps': x.ps if x.ps else spell(w), 'k': 'c', 'f': '', 'a': at}] -%}
+      {%- set ns.i = ns.i + 1 -%}
+    {%- elif found.name -%}
+      {%- set key = words[ns.i:ns.i + found.name] | map(attribute='w') | join(' ') -%}
+      {%- set forms = tts_dict('names', key) -%}
+      {%- for part in key.split(' ') -%}
+        {%- set ns.units = ns.units + [{'w': part, 'ps': entry(part, forms[loop.index0]), 'k': 'c', 'f': '', 'a': at and loop.first}] -%}
+      {%- endfor -%}
+      {%- set ns.i = ns.i + found.name -%}
+    {%- elif found.phrase -%}
+      {%- set key = words[ns.i:ns.i + found.phrase] | map(attribute='w') | join(' ') -%}
+      {%- set ph = tts_dict('phrases', key) -%}
+      {%- if ph[1] | length == 1 -%}
+        {%- set ns.units = ns.units + [{'w': key, 'ps': ph[1][0], 'k': ph[0], 'f': ph[2], 'a': at}] -%}
+      {%- else -%}
+        {%- for part in key.split(' ') -%}
+          {%- set ns.units = ns.units + [{'w': part, 'ps': ph[1][loop.index0], 'k': ph[0], 'f': ph[2], 'a': at and loop.first}] -%}
+        {%- endfor -%}
+      {%- endif -%}
+      {%- set ns.i = ns.i + found.phrase -%}
+    {%- else -%}
+      {%- set fw = tts_dict('functions', w) -%}
+      {%- if hg_covers(w) and tts_dict('exceptions', w) is none -%}
+        {%- set ns.units = ns.units + [{'w': w, 'ps': hg_pronounce(w, written, places[ns.i], at), 'k': 'c', 'f': '', 'a': at}] -%}
+      {%- elif fw is not none -%}
+        {%- set ns.units = ns.units + [{'w': w, 'ps': fw[1], 'k': fw[0], 'f': fw[2], 'a': at}] -%}
+      {%- else -%}
+        {%- set ns.units = ns.units + [{'w': w, 'ps': word(w), 'k': 'c', 'f': '', 'a': at}] -%}
+      {%- endif -%}
+      {%- set ns.i = ns.i + 1 -%}
+    {%- endif -%}
+  {%- endif -%}
+{%- endfor -%}
+{#- sentence stress, right to left -#}
+{%- set n = ns.units | length -%}
+{%- set st = namespace(out=[], pf=false) -%}
+{%- for j in range(n - 1, -1, -1) -%}
+  {%- set u = ns.units[j] -%}
+  {%- set promoted = u.f if u.f else promote(u.ps) -%}
+  {%- if u.k == 'u' -%}{%- set ps = unstress(u.ps) -%}
+  {%- elif u.k == 'uf' -%}{%- set ps = promoted if j == n - 1 else unstress(u.ps) -%}
+  {%- elif u.k == 'ur' -%}{%- set ps = unstress(u.ps) if st.pf else promoted -%}
+  {%- elif u.k == 's' -%}{%- set ps = u.ps if st.pf else promoted -%}
+  {%- elif u.k == 'si' -%}{%- set ps = promoted if not st.pf else (u.ps if j == 0 else unstress(u.ps)) -%}
+  {%- elif u.k == 'pf' -%}{%- set ps = promoted if j == n - 1 else u.ps -%}
+  {%- else -%}{%- set ps = u.ps -%}
+  {%- endif -%}
+  {%- set st.out = [{'w': u.w, 'ps': ps, 'a': u.a}] + st.out -%}
+  {%- set st.pf = st.pf or 'ˈ' in ps -%}
+{%- endfor -%}
+{#- a clause is never without any stress, primary or secondary -#}
+{%- set marks = st.out | map(attribute='ps') | join(' ') -%}
+{%- if 'ˈ' not in marks and 'ˌ' not in marks -%}
+  {%- set fix = namespace(out=[], done=false) -%}
+  {%- for u in st.out | reverse -%}
+    {%- set hv = namespace(v=false) -%}
+    {%- for t in u.ps.split(' ') -%}{%- if t in VOWELS -%}{%- set hv.v = true -%}{%- endif -%}{%- endfor -%}
+    {%- if not fix.done and hv.v -%}
+      {%- set fix.out = [{'w': u.w, 'ps': promote(u.ps), 'a': u.a}] + fix.out -%}{%- set fix.done = true -%}
+    {%- else -%}{%- set fix.out = [u] + fix.out -%}
+    {%- endif -%}
+  {%- endfor -%}
+  {%- set st.out = fix.out -%}
+{%- endif -%}
+{#- the end of each word adjusted to the beginning of the next -#}
+{#- a unit joined to the one before continues its spoken word: no <sp> -#}
+{%- set jn = namespace(s='') -%}
+{%- for u in st.out -%}
+  {%- set ps = u.ps -%}
+  {%- if not loop.last -%}
+    {%- set nxt = st.out[loop.index0 + 1] -%}
+    {%- set toks = ps.split(' ') -%}
+    {%- set last = toks[-1] -%}
+    {%- set mark = 'ˈ' if last.startswith('ˈ') else ('ˌ' if last.startswith('ˌ') else '') -%}
+    {%- set base = unstress(last) -%}
+    {%- set lw = u.w.split(' ')[-1] -%}
+    {%- if unstress(nxt.ps.split(' ')[0]) in VOWELS -%}
+      {%- if lw == 'the' and base == 'ə' -%}{%- set ps = (toks[:-1] + [mark ~ 'ɪ']) | join(' ') -%}
+      {%- elif lw == 'to' and base == 'ə' -%}{%- set ps = (toks[:-1] + [mark ~ 'ʊ']) | join(' ') -%}
+      {%- elif nxt.w.split(' ')[0] in ['and', 'or', 'if'] -%}
+      {%- elif base in SPLIT -%}{%- set ps = (toks[:-1] + [mark ~ SPLIT[base], 'ɹ']) | join(' ') -%}
+      {%- elif base in RHOTIC -%}{%- set ps = ps ~ ' ɹ' -%}
+      {%- elif base == 't' and u.w in ['it', 'at'] and toks | length > 1 and unstress(toks[-2]) in VOWELS -%}{%- set ps = (toks[:-1] + ['ɾ']) | join(' ') -%}
+      {%- endif -%}
+    {%- endif -%}
+  {%- endif -%}
+  {%- if ps -%}{%- set jn.s = jn.s ~ ((' ' if u.a else ' <sp> ') if jn.s else '') ~ ps -%}{%- endif -%}
+{%- endfor -%}
+{{ jn.s }}
+{%- endmacro -%}
+
+{#- ---------- text -> items (normalize.py: tokens, _word_tokens) ----------
+    a word item: {w: lower case, r: as written, a: joined to the word before, spell: letter by letter, ps: phones given};
+    a punctuation mark: {p: mark} -#}
+{%- set INLINE_KEYS = ['ⓐ', 'ⓑ', 'ⓒ', 'ⓓ', 'ⓔ', 'ⓕ', 'ⓖ', 'ⓗ', 'ⓘ', 'ⓙ', 'ⓚ', 'ⓛ', 'ⓜ', 'ⓝ', 'ⓞ', 'ⓟ', 'ⓠ', 'ⓡ', 'ⓢ', 'ⓣ', 'ⓤ', 'ⓥ', 'ⓦ', 'ⓧ', 'ⓨ', 'ⓩ'] -%}
+{%- set INITIALISMS = ['US', 'IT', 'AI', 'ID', 'UN', 'EU', 'UK', 'LA', 'AD', 'BC', 'AM', 'PM', 'OS', 'PE', 'PA', 'MA', 'LED', 'SAT', 'RIP', 'ER'] -%}
+{%- set SOUNDS = ['sh', 'shh', 'ss', 'zz', 'hm', 'hmm', 'mm', 'tsk', 'psst', 'pfft', 'hmph', 'psh', 'pssh'] -%}
+{%- set CARDINAL_AFTER = ['war', 'chapter', 'part', 'book', 'volume', 'act', 'scene', 'section', 'phase', 'type', 'class', 'level', 'stage',
+                          'title', 'article', 'appendix', 'world'] -%}
+{%- set LABELLED = ['plan', 'vitamin', 'grade', 'type', 'option', 'class', 'section', 'part', 'team', 'group', 'exhibit', 'appendix', 'point',
+                    'model', 'series', 'hepatitis', 'level'] -%}
+{%- set ROMAN = {'II': 2, 'III': 3, 'IV': 4, 'VI': 6, 'VII': 7, 'VIII': 8, 'IX': 9, 'XI': 11, 'XII': 12, 'XIII': 13, 'XIV': 14,
+                 'XV': 15, 'XVI': 16, 'XVIII': 18, 'XX': 20} -%}
+{#- words whose accent is the only sign of how they are read: without it "résumé" is "resume" -#}
+{%- set ACCENT_DECIDES = {'résumé': 'R EH1 Z AH0 M EY2', 'resumé': 'R EH1 Z AH0 M EY2', 'exposé': 'EH2 K S P OW0 Z EY1',
+                          'rosé': 'R OW0 Z EY1', 'pâté': 'P AA0 T EY1', 'maté': 'M AA1 T EY2', 'saké': 'S AA1 K EY2',
+                          'lamé': 'L AA0 M EY1'} -%}
+{%- set CONSONANTS = ['p', 'b', 't', 'd', 'k', 'ɡ', 'f', 'v', 'θ', 'ð', 's', 'z', 'ʃ', 'ʒ', 'h', 'm', 'n', 'ŋ', 'l', 'ɹ', 'j', 'w', 'tʃ', 'dʒ', 'ɾ', 'ʔ'] -%}
+
+{%- macro is_word(w) -%}
+{{ '1' if tts_dict('lexicon', w) is not none or tts_dict('functions', w) is not none or tts_dict('exceptions', w) is not none else '' }}
+{%- endmacro -%}
+{#- s has capitals and no small letter -#}
+{%- macro is_upper(s) -%}{{ '1' if s == s | upper and s != s | lower else '' }}{%- endmacro -%}
+{#- the whole of s matches the pattern -#}
+{%- macro fullmatch(s, pattern) -%}
+{%- set r = tts_norm_scan(s, '^(?:' ~ pattern ~ ')$') -%}{{ '1' if r | length == 1 and r[0].rule else '' }}
+{%- endmacro -%}
+{#- the text after segment k starts with the pattern (a space stands for the end of the text) -#}
+{%- macro ahead(segs, k, pattern) -%}
+{%- set r = tts_norm_scan((segs[k + 1:k + 13] | map(attribute='text') | join('')) ~ ' ', '^(?:' ~ pattern ~ ')') -%}
+{{ '1' if r and r[0].rule else '' }}
+{%- endmacro -%}
+
+{#- spellings a word stretched for emphasis may stand for, separated by spaces: a letter typed three times or more
+    is cut to one or to two ("sooo" -> "so", "goood" -> "good") -#}
+{%- macro unstretched(word) -%}
+{%- set rl = namespace(runs=[], long=false) -%}
+{%- for c in tts_norm_scan(word, '.') -%}
+  {%- if rl.runs and rl.runs[-1][0] == c.text -%}
+    {%- set rl.runs = rl.runs[:-1] + [[c.text, rl.runs[-1][1] + 1]] -%}
+    {%- if rl.runs[-1][1] >= 3 -%}{%- set rl.long = true -%}{%- endif -%}
+  {%- else -%}{%- set rl.runs = rl.runs + [[c.text, 1]] -%}
+  {%- endif -%}
+{%- endfor -%}
+{%- if rl.long -%}
+  {%- set last = rl.runs[-1] -%}
+  {%- set liked = (last[1] >= 3 and last[0] in 'lfsz') or (rl.runs | length == 1 and last[0] == 'm')
+               or (rl.runs | length == 2 and rl.runs[0] == ['t', 1] and last[0] == 'o' and last[1] >= 3) -%}
+  {%- set out = namespace(c=[]) -%}
+  {%- for inside, final in [[true, liked], [true, not liked], [false, liked], [false, not liked]] -%}
+    {%- set cut = namespace(s='') -%}
+    {%- for run in rl.runs -%}
+      {%- set n = run[1] if run[1] < 3 else (2 if (final if loop.last else inside) else 1) -%}
+      {%- for _ in range(n) -%}{%- set cut.s = cut.s ~ run[0] -%}{%- endfor -%}
+    {%- endfor -%}
+    {%- if cut.s not in out.c -%}{%- set out.c = out.c + [cut.s] -%}{%- endif -%}
+  {%- endfor -%}
+  {{ out.c | join(' ') }}
+{%- endif -%}
+{%- endmacro -%}
+
+{#- "{...}": a pronunciation written by the user, as phones or in CMUdict notation; '' when it is something else -#}
+{%- macro inline_phones(body) -%}
+{%- set sy = namespace(s=[], letters=true, arpa=true, ok=true) -%}
+{%- for x in tts_norm_scan(body, '%S+') -%}
+  {%- if x.rule -%}
+    {%- set sy.s = sy.s + [x.text] -%}
+    {%- if not fullmatch(x.text, '[ -~]') -%}{%- set sy.letters = false -%}{%- endif -%}
+    {%- if not (x.text in ARPA_CONSONANTS or (x.text[-1:] in ['0', '1', '2'] and x.text[:-1] in ARPA_VOWELS)) -%}{%- set sy.arpa = false -%}{%- endif -%}
+    {%- set phone = x.text | replace('ˈ', '') | replace('ˌ', '') -%}
+    {%- set marked = x.text.startswith('ˈ') or x.text.startswith('ˌ') -%}
+    {%- if not (phone in VOWELS or (phone in CONSONANTS and not marked)) or (('ˈ' in x.text or 'ˌ' in x.text) and ('ˈ' ~ phone) != x.text and ('ˌ' ~ phone) != x.text) -%}{%- set sy.ok = false -%}{%- endif -%}
+  {%- endif -%}
+{%- endfor -%}
+{%- if not sy.s or sy.letters -%}
+{%- elif sy.arpa -%}{{ convert('', sy.s) }}
+{%- elif sy.ok -%}{{ sy.s | join(' ') }}
+{%- endif -%}
+{%- endmacro -%}
+
+{#- ---------- text rules (normalize.py: rewrite) ----------
+    every figure, sign and abbreviation of a sentence is written out in words, one macro per pass, in the order of
+    dizione: each macro takes the text and gives the text rewritten. Marks put in the text for the passes that follow
+    (they are removed from the text given by the user):
+      ⟦..⟧  letters to name          ⟪ⓐ⟫  a pronunciation given inline, ⓐ ⓑ ... for the first, second ...
+      ⟡     "is" before a comparison  ⟐   a "$" that closes a formula      ⟣   an angle bracket that encloses something
+    the patterns do not backtrack: the longer alternative comes first or carries its own lookahead, a lookbehind of
+    more than one character is checked in the macro, and "\w" is written [%w_] (%w takes the marks too) -#}
+{%- set SCALES = ['', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion'] -%}
+{%- set MONTHS = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] -%}
+{%- set MONTH_NUMBER = {'jan': 1, 'feb': 2, 'mar': 3, 'apr': 4, 'may': 5, 'jun': 6, 'jul': 7, 'aug': 8, 'sep': 9, 'oct': 10, 'nov': 11, 'dec': 12} -%}
+{%- set NOT_WORD = '(?![%w_])' -%}
+{#- a name of a month or of a day, the longest first, not followed by a letter -#}
+{%- set MONTH_NAMES = 'January|February|March|April|May|June|July|August|September|October|November|December|Jan|Feb|Mar|Apr|Jun|Jul|Aug|Sept|Sep|Oct|Nov|Dec' -%}
+{%- set DAY_NAMES_ALL = 'Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday|Thurs|Thur|Tues|Mon|Tue|Wed|Thu|Fri|Sat|Sun' -%}
+{%- set MONTH_PATTERN = '(?:' ~ MONTH_NAMES.split('|') | join(NOT_WORD ~ '|') ~ NOT_WORD ~ ')' -%}
+{%- set DAY_OR_MONTH = '(?:' ~ (DAY_NAMES_ALL ~ '|' ~ MONTH_NAMES).split('|') | join(NOT_WORD ~ '|') ~ NOT_WORD ~ ')' -%}
+{%- set DAY_NAMES = {'mon': 'Monday', 'tue': 'Tuesday', 'wed': 'Wednesday', 'thu': 'Thursday', 'fri': 'Friday', 'sat': 'Saturday', 'sun': 'Sunday'} -%}
+{%- set CURRENCY = {'$': ['dollar', 'dollars', 'cent', 'cents'], '£': ['pound', 'pounds', 'penny', 'pence'],
+                    '€': ['euro', 'euros', 'cent', 'cents'], '¥': ['yen', 'yen', '', ''], '₹': ['rupee', 'rupees', 'paisa', 'paise'],
+                    'USD': ['dollar', 'dollars', 'cent', 'cents'], 'EUR': ['euro', 'euros', 'cent', 'cents'],
+                    'GBP': ['pound', 'pounds', 'penny', 'pence'], 'JPY': ['yen', 'yen', '', ''], 'INR': ['rupee', 'rupees', 'paisa', 'paise'],
+                    'CHF': ['Swiss franc', 'Swiss francs', 'centime', 'centimes'], 'CAD': ['Canadian dollar', 'Canadian dollars', 'cent', 'cents'],
+                    'AUD': ['Australian dollar', 'Australian dollars', 'cent', 'cents'], 'CNY': ['yuan', 'yuan', '', '']} -%}
+{%- set CURRENCY_CODES = '(?:USD|EUR|GBP|JPY|INR|CHF|CAD|AUD|CNY)' -%}
+{%- set SCALE_LETTER = {'k': 'thousand', 'm': 'million', 'b': 'billion', 'bn': 'billion', 't': 'trillion'} -%}
+{%- set SYMBOL_WORDS = {'&': 'and', '@': 'at', '+': 'plus', '=': 'equals', '%': 'percent', '×': 'times', '÷': 'divided by',
+                        '±': 'plus or minus', '©': 'copyright', '§': 'section', '°': 'degrees',
+                        '≥': 'greater than or equal to', '≤': 'less than or equal to', '≠': 'not equal to', '≈': 'approximately',
+                        '∞': 'infinity', '√': 'square root of', 'π': 'pi'} -%}
+{%- set COMPARISONS = {'>=': 'greater than or equal to', '<=': 'less than or equal to', '!=': 'not equal to'} -%}
+{%- set CLAUSE_OPENERS = 'if elif while when whenever where whether that because since as and or but so then think thinks thought know knows say says said suppose assume means implies hence thus therefore although though unless until once else also clearly obviously indeed here now again first next finally similarly otherwise however moreover'.split(' ') -%}
+{%- set SENTENCE_STARTERS = 'the he she it i we they this that there but and in on at a an if when what why how who my our you then so now after before however'.split(' ') -%}
+{%- set YEAR_CONTEXT = 'in since by from until till before after during around circa of year early late mid summer winter spring fall autumn class'.split(' ') -%}
+{%- set PHONE_WORDS = 'call dial ring phone telephone tel mobile cell fax hotline text sms whatsapp'.split(' ') -%}
+{%- set PHONE_LINKS = 'to on at or us me him her them back'.split(' ') -%}
+{%- set PIN_WORDS = 'code pin extension ext passcode password zip postcode cvv cvc otp'.split(' ') -%}
+{%- set STRONG_WORDS = 'number id reference ref serial account tracking booking confirmation'.split(' ') -%}
+{%- set WEAK_WORDS = 'order ticket invoice case claim policy receipt transaction parcel shipment container payment reservation membership customer batch lot item job issue bug request'.split(' ') -%}
+{%- set LINK_WORDS = 'is was are were ends ending in with starts starting begins beginning'.split(' ') -%}
+{%- set NOT_COUNTED = 'is was has does goes ships arrives departs leaves shows needs blocks remains includes contains says reads starts ends closes opens runs takes comes this his its us as yes plus minus less'.split(' ') -%}
+{%- set NUMBERED = 'ref reference serial account order invoice tracking part model phone tel fax id room flight case ticket policy licence license booking customer vat tax reg registration'.split(' ') -%}
+{%- set DATE_BEFORE = 'on dated due until till by since from before after for shipped delivered arriving arrives arrive arrived expires expired ends ending starts starting begins beginning monday tuesday wednesday thursday friday saturday sunday mon tue tues wed thu thur thurs fri sat sun'.split(' ') -%}
+{%- set CLASSIC_FRACTIONS = [2, 3, 4, 5, 6, 8, 10, 16, 32, 64, 100] -%}
+{%- set RATIO_WORDS = 'ratio ratios scale odds aspect mix split proportion'.split(' ') -%}
+{%- set DURATION_WORDS = 'lasted took duration runtime recording video clip track lap pace length long'.split(' ') -%}
+{%- set CLOCK_WORDS = 'window slot open opens opening hours shift session served service available booked appointment delivery arrive arrives arriving'.split(' ') -%}
+{%- set CAPITAL_WORDS = 'IN ON AT TO OF BY OR IS AS IF NO SO WE HE MY ME DO GO UP AN AM PM BE IT US THE AND FOR BUT NOT ALL'.split(' ') -%}
+{%- set SHORT_WORDS = 'my to in on of go up no do me we us it is at by be so or an as if he hi ok'.split(' ') -%}
+{%- set ZONES = '(?:AEST|AEDT|CEST|EST|EDT|CST|CDT|MST|MDT|PST|PDT|GMT|UTC|BST|CET|IST|JST|ET|CT|MT|PT)' -%}
+{%- set SIZES = '(?:XXXL|XXL|XXS|XL|XS|S|M|L)' -%}
+{%- set SEPARATORS = {'.': 'dot', '/': 'slash', '@': 'at', '-': 'dash', '_': 'underscore', ':': 'colon', '=': 'equals', '&': 'and', '+': 'plus'} -%}
+{%- set VULGAR = {'½': [1, 2], '⅓': [1, 3], '⅔': [2, 3], '¼': [1, 4], '¾': [3, 4], '⅕': [1, 5], '⅖': [2, 5], '⅗': [3, 5], '⅘': [4, 5],
+                  '⅙': [1, 6], '⅚': [5, 6], '⅐': [1, 7], '⅛': [1, 8], '⅜': [3, 8], '⅝': [5, 8], '⅞': [7, 8], '⅑': [1, 9], '⅒': [1, 10]} -%}
+{#- " A M" or " P M": the two forms of a half of the day after a time -#}
+{%- set HALF_DAY_TIME = '(?:%s?([AaPp])%.[Mm](?:%.(?![%d:]))?(?![%d:])|%s?([AaPp])[Mm]' ~ NOT_WORD ~ '(?![%d:]))' -%}
+{%- set HALF_DAY_HOUR = '(?:%s?([AaPp])%.[Mm](?:%.(?![A-Za-z]))?(?![A-Za-z])|%s?([AaPp])[Mm]' ~ NOT_WORD ~ '(?![A-Za-z]))' -%}
+{%- set SIGN_AFTER = '(?:[$£€¥₹](?![%w_$£€¥₹])|' ~ CURRENCY_CODES ~ '(?![A-Za-z]))' -%}
+
+{#- ---------- helpers ---------- -#}
+{#- s ends with the pattern / s starts with the pattern -#}
+{%- macro ends(s, pattern) -%}
+{%- set r = tts_norm_scan(s, '(?:' ~ pattern ~ ')$') -%}{{ '1' if r | length > 0 and r[-1].rule else '' }}
+{%- endmacro -%}
+{%- macro starts(s, pattern) -%}
+{%- set r = tts_norm_scan(s, '^(?:' ~ pattern ~ ')') -%}{{ '1' if r | length > 0 and r[0].rule else '' }}
+{%- endmacro -%}
+{#- the same, any letter case -#}
+{%- macro starts_i(s, pattern) -%}
+{%- set r = tts_norm_scan(s, '(?i)^(?:' ~ pattern ~ ')') -%}{{ '1' if r | length > 0 and r[0].rule else '' }}
+{%- endmacro -%}
+{#- group number g of the pattern at the start of s, or '' -#}
+{%- macro at_start(s, pattern, g) -%}
+{%- set r = tts_norm_scan(s, '^(?:' ~ pattern ~ ')') -%}{{ r[0].groups[g] if r | length > 0 and r[0].rule else '' }}
+{%- endmacro -%}
+{#- the letters between the marks that say: name these letters -#}
+{%- macro spell_mark(letters) -%}⟦{{ letters }}⟧{%- endmacro -%}
+{#- the first word after here, when the text after a full stop starts a new sentence: nothing follows, or a capital
+    opens a word that often starts one -#}
+{%- macro new_sentence(after) -%}
+{%- set following = after | trim -%}
+{%- if not following -%}1
+{%- else -%}
+  {%- set first = at_start(following, "([A-Za-z']+)", 0) -%}
+  {{ '1' if following[:1] in UPPER and first and first | lower in SENTENCE_STARTERS else '' }}
+{%- endif -%}
+{%- endmacro -%}
+{#- the words just before position pos in the same clause, in lower case, separated by spaces (_before) -#}
+{%- macro words_before(text, pos, count) -%}
+{%- set ns = namespace(clause='', words=[]) -%}
+{%- for m in tts_norm_scan(text[[0, pos - 80] | max:pos], '[.!?;…]%s|[()"]') -%}
+  {%- set ns.clause = '' if m.rule else m.text -%}
+{%- endfor -%}
+{%- for m in tts_norm_scan(ns.clause, '[A-Za-z]+') -%}{%- if m.rule -%}{%- set ns.words = ns.words + [m.text | lower] -%}{%- endif -%}{%- endfor -%}
+{{ ns.words[[0, ns.words | length - count] | max:] | join(' ') }}
+{%- endmacro -%}
+{#- what the words before a number say it is: phone, pin, strong (a number, a reference), weak (an order), enter, or '' -#}
+{%- macro identifier(text, pos) -%}
+{%- set joined = words_before(text, pos, 4) -%}
+{%- if joined -%}
+  {%- set words = joined.split(' ') -%}
+  {%- set n = words | length -%}
+  {%- set linked = namespace(v=false) -%}
+  {%- for w in words[[0, n - 3] | max:n - 1] -%}{%- if w in PHONE_WORDS -%}{%- set linked.v = true -%}{%- endif -%}{%- endfor -%}
+  {%- if words[n - 1] in PHONE_WORDS or (words[n - 1] in PHONE_LINKS and linked.v) -%}phone
+  {%- else -%}
+    {%- set at = namespace(i=n - 1) -%}
+    {%- for _ in words -%}{%- if at.i > 0 and words[at.i] in LINK_WORDS -%}{%- set at.i = at.i - 1 -%}{%- endif -%}{%- endfor -%}
+    {%- set w = words[at.i] -%}
+    {%- if w in PHONE_WORDS -%}phone
+    {%- elif w in PIN_WORDS -%}pin
+    {%- elif w in STRONG_WORDS -%}strong
+    {%- elif w in WEAK_WORDS -%}weak
+    {%- elif words[n - 1] == 'enter' -%}enter
+    {%- endif -%}
+  {%- endif -%}
+{%- endif -%}
+{%- endmacro -%}
+{#- a plural noun follows a number: it is then an amount, not a reference -#}
+{%- macro counted(after) -%}
+{%- set noun = at_start(after, '%s*([A-Za-z]+)', 0) | lower -%}
+{%- if noun in ['people', 'men', 'women', 'children', 'feet', 'miles'] -%}1
+{%- elif noun | length > 3 and noun.endswith('s') and not noun.endswith('ss') and noun not in NOT_COUNTED and is_word(noun[:-1]) -%}1
+{%- endif -%}
+{%- endmacro -%}
+
+{#- ---------- numbers as words ---------- -#}
+{%- macro below_1000(n) -%}
+{%- if n >= 100 -%}{{ ONES[n // 100] }} hundred{{ (' ' ~ below_1000(n % 100)) if n % 100 else '' }}
+{%- elif n >= 20 -%}{{ TENS[n // 10] }}{{ (' ' ~ ONES[n % 10]) if n % 10 else '' }}
+{%- else -%}{{ ONES[n] }}
+{%- endif -%}
+{%- endmacro -%}
+{#- a whole number, given as a number or as its digits: three digits at a time, so that no large number is computed
+    (llama.cpp does its arithmetic in floating point) -#}
+{%- macro cardinal(n) -%}
+{%- set s = n | string | trim -%}
+{%- if s.startswith('-') -%}minus {{ cardinal(s[1:]) }}
+{%- else -%}
+  {%- set s = tts_norm_sub(s, '^(?:0(?=%d))+', '') -%}
+  {%- if s | length > 21 -%}{{ digits(s, 'zero') }}
+  {%- else -%}
+    {%- set ns = namespace(out=[], rest=s, k=0) -%}
+    {%- for _ in range(7) -%}
+      {%- if ns.rest -%}
+        {%- set part = ns.rest[-3:] | int -%}
+        {%- set ns.rest = ns.rest[:-3] -%}
+        {%- if part -%}{%- set ns.out = [below_1000(part) ~ ((' ' ~ SCALES[ns.k]) if ns.k else '')] + ns.out -%}{%- endif -%}
+        {%- set ns.k = ns.k + 1 -%}
+      {%- endif -%}
+    {%- endfor -%}
+    {{ ns.out | join(' ') if ns.out else 'zero' }}
+  {%- endif -%}
+{%- endif -%}
+{%- endmacro -%}
+{%- macro ordinal(n) -%}
+{%- set words = cardinal(n).split(' ') -%}
+{%- set last = words[-1] -%}
+{%- set o = ORDINALS[last] if last in ORDINALS else ((last[:-1] ~ 'ieth') if last.endswith('y') else (last ~ 'th')) -%}
+{{ (words[:-1] + [o]) | join(' ') }}
+{%- endmacro -%}
+{#- the digits of a text one by one; zero is the word for "0" ("zero" or "oh") -#}
+{%- macro digits(s, zero) -%}
+{%- set ns = namespace(out=[]) -%}
+{%- for c in tts_norm_scan(s, '%d') -%}{%- if c.rule -%}{%- set ns.out = ns.out + [zero if c.text == '0' else ONES[c.text | int]] -%}{%- endif -%}{%- endfor -%}
+{{ ns.out | join(' ') }}
+{%- endmacro -%}
+{#- the way a year is said: 1984, 1905, 1900, 2005, 2026 -#}
+{%- macro year(n) -%}
+{%- set n = n | int -%}
+{%- set high = n // 100 -%}{%- set low = n % 100 -%}
+{%- if n < 1000 or n > 2999 -%}{{ cardinal(n) }}
+{%- elif low == 0 -%}{{ cardinal(n) if high % 10 == 0 else cardinal(high) ~ ' hundred' }}
+{%- elif high == 20 and low < 10 -%}two thousand {{ ONES[low] }}
+{%- else -%}{{ cardinal(high) }}{{ (' oh ' ~ ONES[low]) if low < 10 else (' ' ~ cardinal(low)) }}
+{%- endif -%}
+{%- endmacro -%}
+{#- a number read two digits at a time, the way model numbers are: 4070 "forty seventy", 720 "seven twenty" -#}
+{%- macro pairs(n) -%}
+{%- set n = n | int -%}
+{%- set high = n // 100 -%}{%- set low = n % 100 -%}
+{%- if n < 100 or n > 9999 or (low == 0 and (high < 10 or high % 10 == 0)) -%}{{ cardinal(n) }}
+{%- elif low == 0 -%}{{ cardinal(high) }} hundred
+{%- else -%}{{ cardinal(high) }}{{ (' oh ' ~ ONES[low]) if low < 10 else (' ' ~ cardinal(low)) }}
+{%- endif -%}
+{%- endmacro -%}
+{#- an unsigned number as written: 1,234 3.14 007 -#}
+{%- macro number_words(text) -%}
+{%- set parts = (text | replace(',', '')).split('.') -%}
+{%- set whole = parts[0] -%}
+{%- set fraction = parts[1:] | join('.') -%}
+{%- if fraction -%}{{ cardinal(whole) if whole else '' }} point {{ digits(fraction, 'zero') }}
+{%- elif (whole | length > 1 and whole.startswith('0')) or whole | length > 18 -%}{{ digits(whole, 'zero') }}
+{%- else -%}{{ cardinal(whole) }}
+{%- endif -%}
+{%- endmacro -%}
+{#- 2:03:45 as a length of time -#}
+{%- macro duration(hours, minutes, seconds) -%}
+{%- set ns = namespace(parts=[]) -%}
+{%- for count, name in [[hours, 'hour'], [minutes, 'minute'], [seconds, 'second']] -%}
+  {%- if count -%}{%- set ns.parts = ns.parts + [cardinal(count) ~ ' ' ~ (name if count == 1 else name ~ 's')] -%}{%- endif -%}
+{%- endfor -%}
+{%- if ns.parts | length < 2 -%}{{ ns.parts[0] if ns.parts else 'zero seconds' }}
+{%- else -%}{{ ns.parts[:-1] | join(', ') }} and {{ ns.parts[-1] }}
+{%- endif -%}
+{%- endmacro -%}
+{#- plural of a spelled number, for decades: "nineteen nineties" -#}
+{%- macro plural_of(words) -%}
+{{ (words[:-1] ~ 'ies') if words.endswith('y') else (words ~ 's') }}
+{%- endmacro -%}
+{#- "three quarters", "one half"; after a whole number "a half", "an eighth" -#}
+{%- macro fraction_words(top, bottom, article) -%}
+{%- set one = 'half' if bottom == 2 else ('quarter' if bottom == 4 else ordinal(bottom)) -%}
+{%- set many = 'halves' if bottom == 2 else ('quarters' if bottom == 4 else ordinal(bottom) ~ 's') -%}
+{%- if top != 1 -%}{{ cardinal(top) }} {{ many }}
+{%- elif article -%}{{ 'an ' if one[:1] in 'aeiou' else 'a ' }}{{ one }}
+{%- else -%}one {{ one }}
+{%- endif -%}
+{%- endmacro -%}
+{#- one part of a version number: "4" four, "12" twelve, "03" zero three, "129" one two nine -#}
+{%- macro version_part(part) -%}{{ digits(part, 'zero') if part | length > 2 else number_words(part) }}{%- endmacro -%}
+{#- the part after the point of a version: "12" twelve, "04" oh four -#}
+{%- macro minor(part) -%}{{ digits(part, 'oh') if part.startswith('0') else cardinal(part) }}{%- endmacro -%}
+{#- a number in a code that is said as a number: one or two digits, or a round one (B-12, KX-300) -#}
+{%- macro short_number(text) -%}
+{{ '1' if not text.startswith('0') and (text | length <= 2 or text[1:] | replace('0', '') == '') else '' }}
+{%- endmacro -%}
+{#- "1,5" before a unit is one number -#}
+{%- macro decimal_comma(text) -%}{{ text | replace(',', '.') if fullmatch(text, '%d+,%d%d?') else text }}{%- endmacro -%}
+{#- digits inside a file name or an address: 3, 2026, 0412 -#}
+{%- macro address_digits(text) -%}
+{%- if text | length <= 2 and not (text | length == 2 and text.startswith('0')) -%}{{ cardinal(text) }}
+{%- elif text | length == 4 and text | int >= 1900 and text | int <= 2099 -%}{{ year(text) }}
+{%- else -%}{{ digits(text, 'zero') }}
+{%- endif -%}
+{%- endmacro -%}
+{#- groups of digits with a short break between them; small numbers stay numbers: 9-1-1 -#}
+{%- macro digit_groups(groups, zero) -%}
+{%- set ns = namespace(small=true, out=[]) -%}
+{%- for g in groups -%}{%- if not (short_number(g) and g | length <= 2) -%}{%- set ns.small = false -%}{%- endif -%}{%- endfor -%}
+{%- for g in groups -%}{%- set ns.out = ns.out + [cardinal(g) if ns.small else digits(g, zero)] -%}{%- endfor -%}
+{{ ns.out | join(' , ') }}
+{%- endmacro -%}
+{#- a group that mixes letters and digits: the letters are named, the digits read one by one -#}
+{%- macro alnum(group, zero) -%}
+{%- set ns = namespace(out=[]) -%}
+{%- for m in tts_norm_scan(group, [['d', '%d+'], ['l', '[A-Za-z]+']]) -%}
+  {%- if m.rule == 'd' -%}{%- set ns.out = ns.out + [digits(m.text, zero)] -%}
+  {%- elif m.rule == 'l' -%}{%- set ns.out = ns.out + [spell_mark(m.text)] -%}
+  {%- endif -%}
+{%- endfor -%}
+{{ ns.out | join(' ') }}
+{%- endmacro -%}
+{#- letters inside a code that are a word to say, not letters to name: SAVE-20 -#}
+{%- macro reads_as_word(letters) -%}
+{%- set lower = letters | lower -%}
+{%- if lower | length >= 3 and is_word(lower) -%}
+  {{ '1' if lower | length > 3 or is_word(lower ~ 's') or is_word(lower ~ 'es') or is_word(lower ~ 'ed') or is_word(lower ~ 'ing') else '' }}
+{%- endif -%}
+{%- endmacro -%}
+{#- a date in words, or '' when it is none -#}
+{%- macro date(month, day, year_text, the) -%}
+{%- set month = month | int -%}{%- set day = day | int -%}
+{%- if month >= 1 and month <= 12 and day >= 1 and day <= 31 -%}
+  {%- set y = '' if not year_text else (' ' ~ (year(year_text) if year_text | length == 4 else (('oh ' if year_text | int < 10 else '') ~ cardinal(year_text | int)))) -%}
+  {{ ' ' }}{{ ('the ' ~ ordinal(day) ~ ' of ' ~ MONTHS[month - 1]) if the else (MONTHS[month - 1] ~ ' ' ~ ordinal(day)) }}{{ y }}{{ ' ' }}
+{%- endif -%}
+{%- endmacro -%}
+{#- an amount of money in words -#}
+{%- macro amount(symbol, whole, fraction, scale) -%}
+{%- set names = CURRENCY[symbol] -%}
+{%- if scale -%}{{ number_words(whole ~ (('.' ~ fraction) if fraction else '')) }} {{ SCALE_LETTER[scale | lower] if scale | lower in SCALE_LETTER else scale | lower }} {{ names[1] }}
+{%- elif fraction and (fraction | length > 2 or not names[2]) -%}{{ number_words(whole ~ '.' ~ fraction) }} {{ names[1] }}
+{%- else -%}
+  {%- set units = tts_norm_sub(whole, '^(?:0(?=%d))+', '') -%}
+  {%- set change = ((fraction ~ '0')[:2] | int) if fraction else 0 -%}
+  {%- set ns = namespace(parts=[]) -%}
+  {%- if units != '0' or not change -%}{%- set ns.parts = [cardinal(units) ~ ' ' ~ (names[0] if units == '1' else names[1])] -%}{%- endif -%}
+  {%- if change and names[2] -%}{%- set ns.parts = ns.parts + [cardinal(change) ~ ' ' ~ (names[2] if change == 1 else names[3])] -%}{%- endif -%}
+  {{ ns.parts | join(' and ') }}
+{%- endif -%}
+{%- endmacro -%}
+{#- " A M" or " P M" after a time; the full stop that closes "p.m." is kept as a sentence end only when a new sentence
+    follows. dotted, plain: the letter of the two written forms; whole: the text matched; after: the text after it -#}
+{%- macro half_day(dotted, plain, whole, after) -%}
+{%- if dotted or plain -%}
+  {{ spell_mark((dotted or plain) | upper ~ 'M') }}{{ ' .' if dotted and whole.endswith('.') and new_sentence(after) else '' }}{{ ' ' }}
+{%- endif -%}
+{%- endmacro -%}
+
+{#- ---------- passes on figures (normalize.py: _figures), in the order they run ---------- -#}
+{#- "½" and "1½" -#}
+{%- macro nz_vulgar(text) -%}
+{%- set o = namespace(s='') -%}
+{%- for m in tts_norm_scan(text, '(?<![%d.,/])(?:(%d+)%s?)?([½⅓⅔¼¾⅕⅖⅗⅘⅙⅚⅐⅛⅜⅝⅞⅑⅒])') -%}
+  {%- if m.rule -%}
+    {%- set f = VULGAR[m.groups[1]] -%}
+    {%- set o.s = o.s ~ ((' ' ~ cardinal(m.groups[0]) ~ ' and ' ~ fraction_words(f[0], f[1], true) ~ ' ') if m.groups[0] else (' ' ~ f[0] ~ '/' ~ f[1] ~ ' ')) -%}
+  {%- else -%}{%- set o.s = o.s ~ m.text -%}
+  {%- endif -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{#- "120 m²", "3 m3": square and cubic measures; plain: the power is written as a plain figure -#}
+{%- macro nz_area(text, plain) -%}
+{%- set o = namespace(s='') -%}
+{%- set pattern = '(?<![%w_.])(%d[%d,]*(?:%.%d+)?)%s?(km|cm|mm|m|ft)([23])(?![%w_])' if plain
+                  else '(?<![%w_.²³¹¼-¾⁰-₟⅐-↏①-⓿])(%d[%d,]*(?:%.%d+)?)%s?(km|cm|mm|mi|m|ft|yd|in)([²³])' -%}
+{%- for m in tts_norm_scan(text, pattern) -%}
+  {%- if m.rule -%}
+    {%- set unit = m.groups[1] -%}
+    {%- set names = ['inch', 'inches'] if unit == 'in' else (tts_dict('exact_units', unit) or tts_dict('units', unit)) -%}
+    {%- set value = decimal_comma(m.groups[0]) -%}
+    {%- set o.s = o.s ~ ' ' ~ number_words(value) ~ (' square ' if m.groups[2] in '²2' else ' cubic ') ~ (names[0] if value in ['1', '1.0'] else names[1]) ~ ' ' -%}
+  {%- else -%}{%- set o.s = o.s ~ m.text -%}
+  {%- endif -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{#- "Mon.-Fri.", "Jan-Mar": from one day or month to another -#}
+{%- macro nz_day_range(text) -%}
+{%- set o = namespace(s='', pos=0) -%}
+{%- for m in tts_norm_scan(text, '(?<![%w_])(' ~ DAY_OR_MONTH ~ ')(%.?)%s?[-–]%s?(' ~ DAY_OR_MONTH ~ ')(%.?)') -%}
+  {%- if m.rule -%}
+    {%- set names = namespace(n=[]) -%}
+    {%- for name in [m.groups[0], m.groups[2]] -%}
+      {%- set key = name | lower -%}
+      {%- set a = tts_dict('abbreviations', key) -%}
+      {%- set names.n = names.n + [a[1] if a is not none else (DAY_NAMES[key] if key in DAY_NAMES else name)] -%}
+    {%- endfor -%}
+    {%- set o.s = o.s ~ names.n[0] ~ ' to ' ~ names.n[1] ~ (' .' if m.groups[3] and new_sentence(text[o.pos + m.text | length:]) else '') -%}
+  {%- else -%}{%- set o.s = o.s ~ m.text -%}
+  {%- endif -%}
+  {%- set o.pos = o.pos + m.text | length -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{#- "$x = 2$": the sign after the last figure closes a formula, it is not a currency written after an amount. A formula
+    opens with a sign before a letter, or before a figure or a space when a "=" or the like follows -#}
+{%- macro nz_formula(text) -%}
+{%- set parts = text.split('$') -%}
+{%- set n = parts | length -%}
+{%- set o = namespace(s=parts[0], skip=false, close=false) -%}
+{%- for i in range(1, n) -%}
+  {#- the "$" before parts[i]: the closing one of a formula becomes the mark -#}
+  {%- set sign = '⟐' if o.close else '$' -%}
+  {%- set o.close = false -%}
+  {%- if o.skip -%}{%- set o.skip = false -%}
+  {%- elif i + 1 < n -%}
+    {#- does it open a formula that the next "$" closes? -#}
+    {%- set before = parts[i - 1] -%}{%- set inner = parts[i] -%}{%- set after = parts[i + 1] -%}
+    {%- set body = tts_norm_sub(inner, '%d%s?$', '') -%}
+    {%- set first = inner[:1] -%}
+    {%- set free = (i == 1 and not before) or (before and not ends(before, '[%w_]') and not ends(before, '%d%s')) -%}
+    {%- set closed = body != inner and not starts(after, '[%w_]') and (after or i + 2 >= n) -%}
+    {#- a sign with at most 80 characters before it and after it; slash: "/" counts as one -#}
+    {%- set cs = tts_norm_scan(body, '.') | map(attribute='text') | list -%}
+    {%- set sg = namespace(any=false, noslash=false) -%}
+    {%- for ch in cs -%}
+      {%- if ch in '=^_\\{}+*/<>' and loop.index0 <= 80 and cs | length - loop.index0 - 1 <= 80 -%}
+        {%- set sg.any = true -%}{%- if ch != '/' -%}{%- set sg.noslash = true -%}{%- endif -%}
+      {%- endif -%}
+    {%- endfor -%}
+    {%- if free and closed and (
+          (inner and first not in ' 0123456789.,;:!?)' and cs | length <= 80)
+          or (first in '0123456789' and inner and sg.any)
+          or (first == ' ' and sg.noslash)) -%}
+      {%- set o.close = true -%}{%- set o.skip = true -%}
+    {%- endif -%}
+  {%- endif -%}
+  {%- set o.s = o.s ~ sign ~ parts[i] -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{#- "1.299,50" as "1,299.50"; four groups that could be an internet address are left alone -#}
+{%- macro nz_continental(text) -%}
+{%- set o = namespace(s='') -%}
+{%- for m in tts_norm_scan(text, '(?<![%w_.,])([1-9]%d{0,2}(?:%.%d%d%d)+),(%d%d?)(?!%d|[.,]%d)') -%}
+  {%- if m.rule -%}
+    {%- set groups = m.groups[0].split('.') -%}
+    {%- set small = namespace(v=true) -%}
+    {%- for g in groups -%}{%- if g | int > 255 -%}{%- set small.v = false -%}{%- endif -%}{%- endfor -%}
+    {%- set o.s = o.s ~ (m.text if groups | length == 4 and small.v else (groups | join(',') ~ '.' ~ m.groups[1])) -%}
+  {%- else -%}{%- set o.s = o.s ~ m.text -%}
+  {%- endif -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{#- money: the sign before the amount ($5, USD 20, -$3, $2.5 million, $10k), or after it (20 €, 750 000 $) -#}
+{%- macro nz_money(text, after_amount) -%}
+{%- set o = namespace(s='') -%}
+{%- set pattern = ('(?:(?<![%w_.])(-)%s?)?(?<![%w_.,^])(%d{1,3}(?:,%d%d%d)+|[1-9]%d{0,2}(?: %d%d%d)+|%d+)(?:%.(%d+))?%s?(' ~ SIGN_AFTER ~ ')') if after_amount
+                  else ('(?:(?<![%w_.])(-)%s?)?([$£€¥₹]|(?<![A-Za-z])' ~ CURRENCY_CODES ~ '(?![A-Za-z]))%s?(-)?(%d{1,3}(?:,%d%d%d)+|%d+)(?:%.(%d+))?(?:%s?(thousand|million|billion|trillion|bn|[kKmMbB])(?![A-Za-z]))?') -%}
+{%- for m in tts_norm_scan(text, pattern) -%}
+  {%- if m.rule -%}
+    {%- set g = m.groups -%}
+    {%- if after_amount -%}
+      {%- set o.s = o.s ~ (' minus ' if g[0] else ' ') ~ amount(g[3], g[1] | replace(',', '') | replace(' ', ''), g[2], '') ~ ' ' -%}
+    {%- else -%}
+      {%- set o.s = o.s ~ (' minus ' if g[0] or g[2] else ' ') ~ amount(g[1], g[3] | replace(',', ''), g[4], g[5]) ~ ' ' -%}
+    {%- endif -%}
+  {%- else -%}{%- set o.s = o.s ~ m.text -%}
+  {%- endif -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{%- macro nz_cents(text) -%}
+{%- set o = namespace(s='') -%}
+{%- for m in tts_norm_scan(text, '(?<![%w_.])(%d[%d,]*(?:%.%d+)?)%s?¢') -%}
+  {%- set o.s = o.s ~ ((' ' ~ number_words(m.groups[0]) ~ (' cent ' if m.groups[0] == '1' else ' cents ')) if m.rule else m.text) -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{#- "9.30 to 11.30" where the words before say it is a time of day -#}
+{%- macro nz_dot_times(text) -%}
+{%- set o = namespace(s='', pos=0) -%}
+{%- for m in tts_norm_scan(text, '(?<![%d.,:])(%d%d?)%.(00|15|30|45)(%s?(?:to|until|till|and|[-—])%s?)(%d%d?)%.(00|15|30|45)(?!%d|%.%d)') -%}
+  {%- if m.rule -%}
+    {%- set g = m.groups -%}
+    {%- set clock = namespace(v=false) -%}
+    {%- for w in words_before(text, o.pos, 4).split(' ') -%}{%- if w in CLOCK_WORDS -%}{%- set clock.v = true -%}{%- endif -%}{%- endfor -%}
+    {%- if g[0] | int > 23 or g[3] | int > 23 or not clock.v
+          or starts_i(text[o.pos + m.text | length:], '%s?(?:%%|percent(?![A-Za-z])|million(?![A-Za-z])|billion(?![A-Za-z])|' ~ tts_dict('patterns', 'units') ~ ')') -%}
+      {%- set o.s = o.s ~ m.text -%}
+    {%- else -%}{%- set o.s = o.s ~ g[0] ~ ':' ~ g[1] ~ g[2] ~ g[3] ~ ':' ~ g[4] -%}
+    {%- endif -%}
+  {%- else -%}{%- set o.s = o.s ~ m.text -%}
+  {%- endif -%}
+  {%- set o.pos = o.pos + m.text | length -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{#- 16:9, 3:1, 1:25,000. Two digits after the colon are a time unless the words say "ratio" -#}
+{%- macro nz_ratio(text) -%}
+{%- set o = namespace(s='', pos=0) -%}
+{%- for m in tts_norm_scan(text, '(?<![%d:.,])(%d{1,3}):(%d{1,3}(?:,%d%d%d)+|%d+)(?![%d:]|[.,]%d)') -%}
+  {%- if m.rule -%}
+    {%- set first = m.groups[0] -%}{%- set second = m.groups[1] -%}
+    {%- set said = namespace(v=true) -%}
+    {%- if first | length <= 2 and second | length == 2 and second[:1] in '012345' -%}
+      {%- set said.v = at_start(text[o.pos + m.text | length:], '%s*([A-Za-z]+)', 0) | lower in RATIO_WORDS -%}
+      {%- for w in words_before(text, o.pos, 3).split(' ') -%}{%- if w in RATIO_WORDS -%}{%- set said.v = true -%}{%- endif -%}{%- endfor -%}
+    {%- endif -%}
+    {%- set o.s = o.s ~ ((' ' ~ number_words(first) ~ ' to ' ~ number_words(second) ~ ' ') if said.v else m.text) -%}
+  {%- else -%}{%- set o.s = o.s ~ m.text -%}
+  {%- endif -%}
+  {%- set o.pos = o.pos + m.text | length -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{#- 3:45 PM, 20:00, 2:03:45 (a length of time), 48:12 -#}
+{%- macro nz_time(text) -%}
+{%- set o = namespace(s='', pos=0) -%}
+{%- for m in tts_norm_scan(text, '(?<![%d:])(%d%d?):([0-5]%d)(?::([0-5]%d))?' ~ HALF_DAY_TIME ~ '?(?![%d:])') -%}
+  {%- if m.rule -%}
+    {%- set hour = m.groups[0] | int -%}{%- set minute = m.groups[1] | int -%}{%- set second = m.groups[2] -%}
+    {%- set half = m.groups[3] or m.groups[4] -%}
+    {%- set after = text[o.pos + m.text | length:] -%}
+    {%- set before = words_before(text, o.pos, 3) -%}
+    {%- set words = before.split(' ') if before else [] -%}
+    {%- if second and not half and not (words and words[-1] == 'at') -%}
+      {%- set o.s = o.s ~ ' ' ~ duration(hour, minute, second | int) ~ ' ' -%}
+    {%- elif hour > 24 and not second and not half -%}
+      {%- set long = namespace(v=at_start(after, '%s*([A-Za-z]+)', 0) | lower in DURATION_WORDS) -%}
+      {%- for w in words -%}{%- if w in DURATION_WORDS -%}{%- set long.v = true -%}{%- endif -%}{%- endfor -%}
+      {%- set o.s = o.s ~ ' ' ~ (duration(0, hour, minute) if long.v else (cardinal(hour) ~ (' oh ' if minute < 10 else ' ') ~ cardinal(minute))) ~ ' ' -%}
+    {%- elif hour > 24 -%}{%- set o.s = o.s ~ m.text -%}
+    {%- else -%}
+      {%- if minute == 0 -%}
+        {%- set said = cardinal(hour) if half else ('midnight' if hour == 0 else (cardinal(hour) ~ (' hundred' if hour > 12 else " o'clock"))) -%}
+      {%- else -%}{%- set said = cardinal(hour) ~ (' oh ' if minute < 10 else ' ') ~ cardinal(minute) -%}
+      {%- endif -%}
+      {%- set secs = (' and ' ~ cardinal(second | int) ~ (' second' if second | int == 1 else ' seconds')) if second else '' -%}
+      {%- set o.s = o.s ~ ' ' ~ said ~ secs ~ ' ' ~ half_day(m.groups[3], m.groups[4], m.text, after) -%}
+    {%- endif -%}
+  {%- else -%}{%- set o.s = o.s ~ m.text -%}
+  {%- endif -%}
+  {%- set o.pos = o.pos + m.text | length -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{#- 9am, 10 p.m. -#}
+{%- macro nz_hour(text) -%}
+{%- set o = namespace(s='', pos=0) -%}
+{%- for m in tts_norm_scan(text, '(?<![%d.:,])(%d%d?)' ~ HALF_DAY_HOUR) -%}
+  {%- if m.rule -%}
+    {%- set o.s = o.s ~ ' ' ~ cardinal(m.groups[0]) ~ ' ' ~ half_day(m.groups[1], m.groups[2], m.text, text[o.pos + m.text | length:]) -%}
+  {%- else -%}{%- set o.s = o.s ~ m.text -%}
+  {%- endif -%}
+  {%- set o.pos = o.pos + m.text | length -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{#- how the dates of the text are written: "day first" when one of them can only be read that way and none the other
+    way; "dated" when one has a year. Gives two letters, '1' or '0' each -#}
+{%- macro date_habits(text) -%}
+{%- set h = namespace(day=false, month=false, dated=false) -%}
+{%- for m in tts_norm_scan(text, '(?<![%d/.])(%d%d?)/(%d%d?)(?:/(?:%d%d%d%d|%d%d))?(?![%d/])') -%}
+  {%- if m.rule -%}
+    {%- set a = m.groups[0] | int -%}{%- set b = m.groups[1] | int -%}
+    {%- if not (a == 24 and b == 7) -%}
+      {%- if a > 12 and b <= 12 -%}{%- set h.day = true -%}{%- endif -%}
+      {%- if b > 12 and a <= 12 -%}{%- set h.month = true -%}{%- endif -%}
+    {%- endif -%}
+  {%- endif -%}
+{%- endfor -%}
+{%- for m in tts_norm_scan(text, '(?<![%d/])%d%d?/%d%d?/(?:%d%d%d%d|%d%d)(?![%d/])') -%}{%- if m.rule -%}{%- set h.dated = true -%}{%- endif -%}{%- endfor -%}
+{{ '1' if h.day and not h.month else '0' }}{{ '1' if h.dated else '0' }}
+{%- endmacro -%}
+
+{#- dates in figures: kind is iso (2026-11-14, 2026/11/14), dot (15.11.2026) or slash (12/09/2026) -#}
+{%- macro nz_date(text, kind, day_first) -%}
+{%- set o = namespace(s='') -%}
+{%- set pattern = [['a', '(?<![%d-])(%d%d%d%d)-(%d%d?)-(%d%d?)(?![%d-])'], ['a', '(?<![%d/])(%d%d%d%d)/(%d%d?)/(%d%d?)(?![%d/])']] if kind == 'iso'
+                  else ('(?<![%d.])(%d%d?)%.(%d%d?)%.(%d%d%d%d)(?!%d|%.%d)' if kind == 'dot' else '(?<![%d/])(%d%d?)/(%d%d?)/(%d%d%d%d|%d%d)(?![%d/])') -%}
+{%- for m in tts_norm_scan(text, pattern) -%}
+  {%- if m.rule -%}
+    {%- set g = m.groups -%}
+    {%- if kind == 'iso' -%}{%- set said = date(g[1], g[2], g[0], false) -%}
+    {%- elif kind == 'dot' -%}
+      {#- dates written with full stops put the day first -#}
+      {%- set swap = g[1] | int > 12 and g[0] | int <= 12 -%}
+      {%- set said = date(g[0] if swap else g[1], g[1] if swap else g[0], g[2], false) -%}
+    {%- else -%}
+      {%- set swap = (g[0] | int > 12 and g[1] | int <= 12) or (day_first and g[1] | int <= 12) -%}
+      {%- set said = date(g[1] if swap else g[0], g[0] if swap else g[1], g[2], false) -%}
+    {%- endif -%}
+    {%- set o.s = o.s ~ (said if said else m.text) -%}
+  {%- else -%}{%- set o.s = o.s ~ m.text -%}
+  {%- endif -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{#- "November 3-17" (month first) and "3-17 November" -#}
+{%- macro nz_days_of_month(text, month_first) -%}
+{%- set o = namespace(s='') -%}
+{%- set pattern = ('(?<![%w_])(' ~ MONTH_PATTERN ~ ')%.?%s+(%d%d?)(?:st|nd|rd|th)?%s?[-—]%s?(%d%d?)(?:(?:st|nd|rd|th)(?=,?%s+%d%d%d%d(?![%d:])|(?![%d:])))?(?:,?%s+(%d%d%d%d)(?![%d:]))?(?![%d:])') if month_first
+                  else ('(?<![%d.,/:%-])(%d%d?)(?:st|nd|rd|th)?%s?[-—]%s?(%d%d?)(?:st|nd|rd|th)?%s+(' ~ MONTH_PATTERN ~ ')(?:,?%s+(%d%d%d%d))?') -%}
+{%- for m in tts_norm_scan(text, pattern) -%}
+  {%- if m.rule -%}
+    {%- set g = m.groups -%}
+    {%- set month = g[0] if month_first else g[2] -%}
+    {%- set first = (g[1] if month_first else g[0]) | int -%}{%- set second = (g[2] if month_first else g[1]) | int -%}
+    {%- if first >= 1 and first <= 31 and second >= 1 and second <= 31 -%}
+      {%- set name = MONTHS[MONTH_NUMBER[month[:3] | lower] - 1] -%}
+      {%- set o.s = o.s ~ ' ' ~ ((name ~ ' ' ~ ordinal(first) ~ ' to ' ~ ordinal(second)) if month_first else ('the ' ~ ordinal(first) ~ ' to the ' ~ ordinal(second) ~ ' of ' ~ name))
+                    ~ ((' ' ~ year(g[3])) if g[3] else '') ~ ' ' -%}
+    {%- else -%}{%- set o.s = o.s ~ m.text -%}
+    {%- endif -%}
+  {%- else -%}{%- set o.s = o.s ~ m.text -%}
+  {%- endif -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{#- "January 5, 1990" (month first) and "22 February", "5 Jan. 2026" -#}
+{%- macro nz_month_day(text, month_first) -%}
+{%- set o = namespace(s='') -%}
+{%- set pattern = ('(?<![%w_])(' ~ MONTH_PATTERN ~ ')%.?%s+(%d%d?)(?:(?:st|nd|rd|th)(?=,?%s+%d%d%d%d(?![%d:])|(?![%d:])))?(?:,?%s+(%d%d%d%d)(?![%d:]))?(?![%d:])') if month_first
+                  else ('(?<![%d.,/:%-])(%d%d?)(?:st|nd|rd|th)?%s+(' ~ MONTH_PATTERN ~ ')(%.(?=%s+[a-z%d]))?(?:,?%s+(%d%d%d%d))?') -%}
+{%- for m in tts_norm_scan(text, pattern) -%}
+  {%- if m.rule -%}
+    {%- set g = m.groups -%}
+    {%- if month_first -%}{%- set said = date(MONTH_NUMBER[g[0][:3] | lower], g[1], g[2], false) -%}
+    {%- else -%}
+      {#- the stop after a short month name is dropped; any other one ends the sentence -#}
+      {%- set said = date(MONTH_NUMBER[g[1][:3] | lower], g[0], g[3], true) -%}
+      {%- set said = (said ~ '. ') if said and g[2] and g[1] in MONTHS else said -%}
+    {%- endif -%}
+    {%- set o.s = o.s ~ (said if said else m.text) -%}
+  {%- else -%}{%- set o.s = o.s ~ m.text -%}
+  {%- endif -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{#- telephone numbers: +1 415 555 2671, (415) 555-2671, 555-0147 -#}
+{%- macro nz_phone(text) -%}
+{%- set o = namespace(s='') -%}
+{%- for m in tts_norm_scan(text, [['long', '(?<![%d%-])(?:%+?(1)[-.%s])?%(?(%d%d%d)%)?[-.%s](%d%d%d)[-.%s](%d%d%d%d)(?![%d%-])'],
+                                  ['short', '(?<![%w_.,%-])(%d%d%d)-(%d%d%d%d)(?![%w_%-])']]) -%}
+  {%- if m.rule -%}
+    {%- set said = namespace(g=[]) -%}
+    {%- for g in m.groups -%}{%- if g -%}{%- set said.g = said.g + ['eight hundred' if g == '800' else digits(g, 'oh')] -%}{%- endif -%}{%- endfor -%}
+    {%- set o.s = o.s ~ ' ' ~ said.g | join(', ') ~ ' ' -%}
+  {%- else -%}{%- set o.s = o.s ~ m.text -%}
+  {%- endif -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{#- versions: 2.4.1, v2.3.1-14 (three parts or more), v2.12 -#}
+{%- macro nz_version(text) -%}
+{%- set o = namespace(s='') -%}
+{%- for m in tts_norm_scan(text, '(?<![%w_.])([vV])?(%d+%.%d+(?:%.%d+(?=%.%d))*%.%d+(?![%w_])|%d+%.%d+(?:%.%d+(?=%.%d))+)(?:-(%d+)(?![%w_%-]|%.%d))?') -%}
+  {%- if m.rule -%}
+    {%- set parts = namespace(p=[]) -%}
+    {%- for part in m.groups[1].split('.') -%}{%- set parts.p = parts.p + [version_part(part)] -%}{%- endfor -%}
+    {%- set o.s = o.s ~ ' ' ~ ((spell_mark(m.groups[0]) ~ ' ') if m.groups[0] else '') ~ parts.p | join(' point ')
+                  ~ ((' dash ' ~ number_words(m.groups[2])) if m.groups[2] else '') ~ ' ' -%}
+  {%- else -%}{%- set o.s = o.s ~ m.text -%}
+  {%- endif -%}
+{%- endfor -%}
+{%- set o2 = namespace(s='') -%}
+{%- for m in tts_norm_scan(o.s, '(?<![%w_.])([vV])(%d+)%.(%d%d%d?)(?!%d|%.%d|[%w_])') -%}
+  {%- set o2.s = o2.s ~ ((' ' ~ spell_mark(m.groups[0]) ~ ' ' ~ cardinal(m.groups[1]) ~ ' point ' ~ minor(m.groups[2]) ~ ' ') if m.rule else m.text) -%}
+{%- endfor -%}
+{{ o2.s }}
+{%- endmacro -%}
+
+{#- before a number these mean a version: its parts are read as whole numbers ("Python three point twelve") -#}
+{%- set VERSIONED = 'version|ver|release|firmware|patch|kernel|build|python|node|nodejs|ios|ipados|macos|watchos|tvos|android|chrome|firefox|safari|java|php|ruby|rust|swift|kotlin|golang|django|rails|react|angular|vue|docker|kubernetes|ubuntu|debian|fedora|windows|linux|postgres|postgresql|mysql|redis|numpy|pytorch|tensorflow|cuda|xcode|gcc|clang|unity|minecraft|typescript|bluetooth|usb|hdmi|opengl|vulkan|directx|upgrade[ds]?|update[ds]?|downgrade[ds]?' -%}
+{%- set VERSIONED_PATTERN = '(?:' ~ VERSIONED.split('|') | join('(?=%.?%s)|') ~ '(?=%.?%s))' -%}
+{%- set NO_AMOUNT = '(?!%d|%.%d|%s?%%|%s(?:percent|million|billion)(?![%w_]))' -%}
+{%- macro nz_versioned(text) -%}
+{%- set o = namespace(s='') -%}
+{%- for m in tts_norm_scan(text, '(?i)(?<![%w_])(' ~ VERSIONED_PATTERN ~ ')(%.?%s(?:(?:from|to)%s)?)(%d+)%.(%d%d%d?)(?:(%s(?:to|or|and)%s)(%d+)%.(%d%d%d?)' ~ NO_AMOUNT ~ ')?' ~ NO_AMOUNT) -%}
+  {%- if m.rule -%}
+    {%- set g = m.groups -%}
+    {%- set o.s = o.s ~ g[0] ~ g[1] ~ cardinal(g[2]) ~ ' point ' ~ minor(g[3]) ~ ((g[4] ~ cardinal(g[5]) ~ ' point ' ~ minor(g[6])) if g[4] else '') ~ ' ' -%}
+  {%- else -%}{%- set o.s = o.s ~ m.text -%}
+  {%- endif -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{#- the words for a code written with hyphens, or '' when it is none (5-year, COVID-19, a range) -#}
+{%- macro code_words(groups, numbered, text, pos) -%}
+{%- set token = groups | join('-') -%}
+{%- if starts(token, '[^%d]*%d') -%}
+  {%- set k = namespace(kinds=[], digits=true, upper=false) -%}
+  {%- for g in groups -%}
+    {%- set kind = 'd' if fullmatch(g, '%d+') else ('m' if starts(g, '[^%d]*%d') else ('u' if is_upper(g) else 'l')) -%}
+    {%- set k.kinds = k.kinds + [kind] -%}
+    {%- if kind != 'd' -%}{%- set k.digits = false -%}{%- endif -%}
+    {%- if g != g | lower -%}{%- set k.upper = true -%}{%- endif -%}
+  {%- endfor -%}
+  {%- set kinds = k.kinds -%}
+  {%- set n = groups | length -%}
+  {%- if k.digits -%}
+    {%- set rng = namespace(v=false) -%}
+    {%- if n == 2 -%}
+      {%- set years = groups[0] | length == 4 and groups[1] | length == 4 and groups[0] | int >= 1000 and groups[0] | int <= 2100 and groups[1] | int >= 1000 and groups[1] | int <= 2100 -%}
+      {%- set zero = (groups[0] | length > 1 and groups[0].startswith('0')) or (groups[1] | length > 1 and groups[1].startswith('0')) -%}
+      {%- set long = groups[0] | length >= 4 and groups[1] | length >= 4 and not years -%}
+      {#- 10-15 and 2019-2021 are ranges -#}
+      {%- set rng.v = not (numbered or zero or long or identifier(text, pos) in ['strong', 'pin']) -%}
+    {%- endif -%}
+    {%- if not rng.v -%}{{ digit_groups(groups, 'zero') }}{%- endif -%}
+  {%- elif n == 2 and kinds[1] == 'd' and kinds[0] in 'ul' -%}
+    {#- pre-2020, COVID-19, SAVE-20 are words; B-12, KX-300 -#}
+    {%- if not ((kinds[0] == 'l' and groups[0] | length > 1) or reads_as_word(groups[0])) -%}
+      {{ spell_mark(groups[0]) }} {{ cardinal(groups[1]) if short_number(groups[1]) else (', ' ~ digits(groups[1], 'zero')) }}
+    {%- endif -%}
+  {%- elif n == 2 and kinds[0] == 'd' and (kinds[1] == 'l' or groups[1] | length == 1 or fullmatch(groups[1], '%d+[A-Za-z]+')) -%}
+    {#- 5-year, 3-D, 5-10MB -#}
+  {%- else -%}
+    {%- set stop = namespace(v=false) -%}
+    {%- for g in groups -%}
+      {#- 20-year-old, F-16s -#}
+      {%- if (kinds[loop.index0] == 'l' and g | length > 2) or (kinds[loop.index0] == 'm' and fullmatch(g, '%d+(?:st|nd|rd|th|s)')) -%}{%- set stop.v = true -%}{%- endif -%}
+    {%- endfor -%}
+    {#- 3-in-1, 50s-60s -#}
+    {%- if not k.upper and ('l' in kinds or 'm' in kinds) -%}{%- set stop.v = true -%}{%- endif -%}
+    {%- if not stop.v -%}
+      {%- set parts = namespace(p=[]) -%}
+      {%- for g in groups -%}
+        {%- set kind = kinds[loop.index0] -%}
+        {%- if kind == 'd' -%}{%- set parts.p = parts.p + [digits(g, 'zero')] -%}
+        {%- elif kind == 'm' -%}{%- set parts.p = parts.p + [alnum(g, 'zero')] -%}
+        {%- elif g | length > 3 and is_word(g | lower) -%}{%- set parts.p = parts.p + [g] -%}
+        {%- else -%}{%- set parts.p = parts.p + [spell_mark(g) if g | length <= 5 else g] -%}
+        {%- endif -%}
+      {%- endfor -%}
+      {{ parts.p | join(' , ') }}
+    {%- endif -%}
+  {%- endif -%}
+{%- endif -%}
+{%- endmacro -%}
+
+{#- codes written with hyphens: AB-4471-Z, 91-220-0017, #A7-4419 -#}
+{%- macro nz_code(text) -%}
+{%- set o = namespace(s='', pos=0) -%}
+{%- for m in tts_norm_scan(text, '(?<![%w_.,/%-])(#)?([A-Za-z0-9]+(?:-[A-Za-z0-9]+)+)(?![%w_%-]|[.,]%d)') -%}
+  {%- if m.rule -%}
+    {%- set words = code_words(m.groups[1].split('-'), m.groups[0], text, o.pos) -%}
+    {%- set o.s = o.s ~ (((' number ' if m.groups[0] else ' ') ~ words ~ ' ') if words else m.text) -%}
+  {%- else -%}{%- set o.s = o.s ~ m.text -%}
+  {%- endif -%}
+  {%- set o.pos = o.pos + m.text | length -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{%- macro nz_slashed(text) -%}
+{%- set o = namespace(s='') -%}
+{%- for m in tts_norm_scan(text, '(?<![%w_.,/%-])(%d+(?:/%d+){2,})(?![%w_/]|[.,]%d)') -%}
+  {%- set o.s = o.s ~ ((' ' ~ digit_groups(m.groups[0].split('/'), 'zero') ~ ' ') if m.rule else m.text) -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{#- digits written in groups with spaces: a telephone number, a card or tracking number, or a large number with
+    spaces between the thousands. T is one group: capitals and digits with a digit in it -#}
+{%- set GROUP = '[A-Z]*%d[A-Z0-9]*' -%}
+{%- set GROUP_END = '[%w_]|[.,:/%-]%d|%s?[%%°]' -%}
+{%- macro nz_grouped(text) -%}
+{%- set o = namespace(s='', pos=0) -%}
+{%- for m in tts_norm_scan(text, [['lead', '(?<![%w_.,:/#+%-])(%+)?([A-Z][A-Z]) (' ~ GROUP ~ '(?: ' ~ GROUP ~ ')+)(?: ([A-Z][A-Z])(?![%w_]))?'],
+                                  ['plain', '(?<![%w_.,:/#+%-])(%+)?(' ~ GROUP ~ '(?: ' ~ GROUP ~ ')+)']]) -%}
+  {%- if m.rule -%}
+    {%- set plus = m.groups[0] -%}
+    {%- set all = (m.groups[2] if m.rule == 'lead' else m.groups[1]).split(' ') -%}
+    {%- set after = text[o.pos + m.text | length:] -%}
+    {#- the longest run of groups after which the number can end (dizione's pattern gives groups back) -#}
+    {%- set c = namespace(n=0, trail='', rest='') -%}
+    {%- if m.rule == 'lead' and m.groups[3] and not starts(after, GROUP_END) -%}{%- set c.n = all | length -%}{%- set c.trail = m.groups[3] -%}
+    {%- else -%}
+      {%- set held = (' ' ~ m.groups[3]) if m.rule == 'lead' and m.groups[3] else '' -%}
+      {%- for count in range(all | length, 1, -1) -%}
+        {%- set rest = (' ' if count < all | length else '') ~ all[count:] | join(' ') ~ held -%}
+        {%- if not c.n and not starts(rest ~ after, GROUP_END) -%}{%- set c.n = count -%}{%- set c.rest = rest -%}{%- endif -%}
+      {%- endfor -%}
+    {%- endif -%}
+    {%- if not c.n -%}{%- set o.s = o.s ~ m.text -%}
+    {%- else -%}
+      {%- set core = all[:c.n] -%}
+      {%- set following = c.rest ~ after -%}
+      {%- set whole = m.text[:m.text | length - c.rest | length] -%}
+      {%- set lead = m.groups[1] if m.rule == 'lead' else '' -%}
+      {#- "IN 2019 150 ..." in capitals: the two letters are a word -#}
+      {%- set word_lead = lead in CAPITAL_WORDS -%}
+      {%- set head = (lead ~ ' ') if word_lead else '' -%}
+      {%- set tail = (' ' ~ c.trail) if word_lead and c.trail else '' -%}
+      {%- set lead = '' if word_lead else lead -%}
+      {%- set trail = '' if word_lead else c.trail -%}
+      {%- set kind = identifier(text, o.pos) -%}
+      {%- set p = namespace(pure=true, three=true, long=false, total=0) -%}
+      {%- for part in core -%}
+        {%- if not fullmatch(part, '%d+') -%}{%- set p.pure = false -%}{%- endif -%}
+        {%- if not loop.first and part | length != 3 -%}{%- set p.three = false -%}{%- endif -%}
+        {%- if part | length > 2 -%}{%- set p.long = true -%}{%- endif -%}
+        {%- set p.total = p.total + part | length -%}
+      {%- endfor -%}
+      {%- set said = namespace(v='', done=false) -%}
+      {%- if p.pure and not plus and not lead -%}
+        {%- set is_amount = counted(following) or starts_i(following, '%s?(?:' ~ tts_dict('patterns', 'units') ~ ')') -%}
+        {%- if not kind and not core[0].startswith('0') and core[0] | length <= 3 and p.three and (core[-1] == '000' or (is_amount and core | length > 2)) -%}
+          {#- 1 000 000: the number rules read it -#}
+          {%- set said.v = head ~ core | join(',') ~ tail -%}{%- set said.done = true -%}
+        {%- elif not (kind or (core[0] | length > 1 and core[0].startswith('0')) or (core | length > 2 and p.long)) -%}
+          {%- set said.v = whole -%}{%- set said.done = true -%}
+        {%- endif -%}
+      {%- elif not p.pure and not lead and not plus and not kind -%}
+        {%- set said.v = whole -%}{%- set said.done = true -%}
+      {%- endif -%}
+      {%- if not said.done -%}
+        {%- set zero = 'oh' if plus or kind == 'phone' or (p.pure and core[0].startswith('0') and p.total >= 10) else 'zero' -%}
+        {%- if p.pure -%}{%- set spoken = digit_groups(core, zero) -%}
+        {%- else -%}
+          {%- set sp = namespace(p=[]) -%}
+          {%- for part in core -%}{%- set sp.p = sp.p + [digits(part, zero) if fullmatch(part, '%d+') else alnum(part, zero)] -%}{%- endfor -%}
+          {%- set spoken = sp.p | join(' , ') -%}
+        {%- endif -%}
+        {%- set spoken = ((spell_mark(lead) ~ ' , ') if lead else '') ~ spoken ~ ((' , ' ~ spell_mark(trail)) if trail else '') -%}
+        {%- set said.v = head ~ (' plus ' if plus else ' ') ~ spoken ~ ' ' ~ tail -%}
+      {%- endif -%}
+      {%- set o.s = o.s ~ said.v ~ (nz_grouped(c.rest) if c.rest else '') -%}
+    {%- endif -%}
+  {%- else -%}{%- set o.s = o.s ~ m.text -%}
+  {%- endif -%}
+  {%- set o.pos = o.pos + m.text | length -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{#- chat spellings that contain a digit or a slash: gr8, b4, 2day -#}
+{%- macro nz_chat_forms(text) -%}
+{%- set o = namespace(s='') -%}
+{%- for m in tts_norm_scan(text, "(?<![%w_/'])[A-Za-z0-9/]+(?![%w_/'])") -%}
+  {%- set said = tts_dict('chat', m.text | lower) if m.rule and starts(m.text, '[A-Za-z]*[0-9/]') else none -%}
+  {%- if said is none -%}{%- set o.s = o.s ~ m.text -%}
+  {%- elif said[0] == '-' -%}{%- set o.s = o.s ~ ' ' -%}
+  {%- else -%}
+    {%- set w = namespace(p=[]) -%}
+    {%- for part in said[0].split(' ') -%}{%- set w.p = w.p + [spell_mark(part[1:]) if part.startswith('*') else part] -%}{%- endfor -%}
+    {%- set o.s = o.s ~ ' ' ~ w.p | join(' ') ~ ' ' -%}
+  {%- endif -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{#- the 1990s, the '80s; 21st; 1080p; 50% -#}
+{%- macro nz_small_figures(text) -%}
+{%- set o = namespace(s='') -%}
+{%- for m in tts_norm_scan(text, "(?<![%w_.])'?(%d%d%d%d|%d%d)'?s(?![%w_])") -%}
+  {%- set o.s = o.s ~ ((' ' ~ plural_of(year(m.groups[0]) if m.groups[0] | length == 4 else cardinal(m.groups[0])) ~ ' ') if m.rule else m.text) -%}
+{%- endfor -%}
+{%- set o2 = namespace(s='') -%}
+{%- for m in tts_norm_scan(o.s, '(?<![%w_.])(%d+)(?:st|nd|rd|th)(?![%w_])') -%}
+  {%- set o2.s = o2.s ~ ((' ' ~ ordinal(m.groups[0]) ~ ' ') if m.rule else m.text) -%}
+{%- endfor -%}
+{%- set o3 = namespace(s='') -%}
+{%- for m in tts_norm_scan(o2.s, '(?<![%w_.])(1080|1440|2160|4320|144|240|360|480|540|576|720|900)([pi])(?![%w_])') -%}
+  {%- set o3.s = o3.s ~ ((' ' ~ pairs(m.groups[0]) ~ ' ' ~ spell_mark(m.groups[1]) ~ ' ') if m.rule else m.text) -%}
+{%- endfor -%}
+{%- set o4 = namespace(s='') -%}
+{%- for m in tts_norm_scan(o3.s, '(%d[%d,]*(?:%.%d+)?)%s?%%') -%}
+  {%- set o4.s = o4.s ~ ((' ' ~ number_words(m.groups[0]) ~ ' percent ') if m.rule else m.text) -%}
+{%- endfor -%}
+{{ o4.s }}
+{%- endmacro -%}
+
+{#- a number and its unit: 5 km, 10-15 kg, 60 mph, 72°F -#}
+{%- macro nz_measure(text) -%}
+{%- set o = namespace(s='', pos=0) -%}
+{%- for m in tts_norm_scan(text, '(?i)(?<![%w_.])(%d[%d,]*(?:%.%d+)?)(?:[-–](%d[%d,]*(?:%.%d+)?))?%s?(' ~ tts_dict('patterns', 'units') ~ ')(%.(?=%s+[a-z]))?') -%}
+  {%- if m.rule -%}
+    {%- set unit = m.groups[2] -%}
+    {%- set after = text[o.pos + m.text | length:] -%}
+    {#- the stop of "5 min. more" belongs to the unit only before a word in small letters -#}
+    {%- set stop = '.' if m.groups[3] and not starts(after, '%s+[a-z]') else '' -%}
+    {%- set names = tts_dict('exact_units', unit) or tts_dict('units', unit | lower | replace('Μ', 'μ')) -%}
+    {%- set nxt = (stop ~ after)[:1] -%}
+    {%- if names is none or not names or (fullmatch(unit, '.') and nxt and nxt in "-'/") -%}{%- set o.s = o.s ~ m.text -%}
+    {%- else -%}
+      {%- set value = decimal_comma(m.groups[0]) -%}
+      {%- set upper = decimal_comma(m.groups[1]) if m.groups[1] else '' -%}
+      {%- set o.s = o.s ~ ' ' ~ ((number_words(value) ~ ' to ' ~ number_words(upper) ~ ' ' ~ names[1]) if upper
+                                 else (number_words(value) ~ ' ' ~ (names[0] if value in ['1', '1.0'] else names[1]))) ~ ' ' ~ stop -%}
+    {%- endif -%}
+  {%- else -%}{%- set o.s = o.s ~ m.text -%}
+  {%- endif -%}
+  {%- set o.pos = o.pos + m.text | length -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{#- "2 1/2": two and a half -#}
+{%- macro nz_mixed_fraction(text) -%}
+{%- set o = namespace(s='') -%}
+{%- for m in tts_norm_scan(text, '(?<![%d/.,%-])(%d+) (%d)/(10|16|2|3|4|5|6|8)(?![%d/]|%.%d)') -%}
+  {%- if m.rule and m.groups[1] | int > 0 and m.groups[1] | int < m.groups[2] | int -%}
+    {%- set o.s = o.s ~ ' ' ~ number_words(m.groups[0]) ~ ' and ' ~ fraction_words(m.groups[1] | int, m.groups[2] | int, true) ~ ' ' -%}
+  {%- else -%}{%- set o.s = o.s ~ m.text -%}
+  {%- endif -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{#- 3/4, 7/10; "14/03" and "12/10" as a day of the year where the text says so -#}
+{%- macro nz_fraction(text, day_first, dated) -%}
+{%- set o = namespace(s='', pos=0) -%}
+{%- for m in tts_norm_scan(text, '(?<![%d/.])(%d+)/(%d+)(?![%d/]|%.%d)') -%}
+  {%- if m.rule -%}
+    {%- set a = m.groups[0] -%}{%- set b = m.groups[1] -%}
+    {%- set said = namespace(v='') -%}
+    {%- if a | length <= 2 and b | length <= 2 -%}
+      {%- set first = a | int -%}{%- set second = b | int -%}
+      {%- set before = words_before(text, o.pos, 4) -%}
+      {%- set words = before.split(' ') if before else [] -%}
+      {%- if first >= 1 and first <= 12 and second >= 20 and second <= 49 and 'card' in words -%}
+        {#- a card that expires 08/27 -#}
+        {%- set said.v = ' ' ~ MONTHS[first - 1] ~ ' ' ~ year(2000 + second) ~ ' ' -%}
+      {%- else -%}
+        {%- set last = words[-1] if words else '' -%}
+        {#- 1/2, 3/4, 7/10 -#}
+        {%- set classic = first < second and second in CLASSIC_FRACTIONS -%}
+        {%- set zero = (a | length == 2 and a.startswith('0')) or (b | length == 2 and b.startswith('0')) -%}
+        {%- if zero or (first > 12 and first <= 31 and second <= 12 and not (first == 24 and second == 7))
+              or (last in DATE_BEFORE and (not classic or last == 'on')) or (dated and not classic and first != second) -%}
+          {%- set swap = first > 12 or (day_first and second <= 12) -%}
+          {%- set said.v = date(second if swap else first, first if swap else second, '', false) -%}
+        {%- endif -%}
+      {%- endif -%}
+    {%- endif -%}
+    {%- if not said.v -%}
+      {%- set top = a | int -%}{%- set bottom = b | int -%}
+      {#- twenty four seven, fifty fifty -#}
+      {%- if (top == 24 and bottom == 7) or top == bottom -%}{%- set said.v = ' ' ~ cardinal(a) ~ ' ' ~ cardinal(b) ~ ' ' -%}
+      {%- elif top <= 0 or top >= 100 or bottom <= 1 or bottom > 100 -%}{%- set said.v = ' ' ~ cardinal(a) ~ ' over ' ~ cardinal(b) ~ ' ' -%}
+      {%- else -%}{%- set said.v = ' ' ~ fraction_words(top, bottom, false) ~ ' ' -%}
+      {%- endif -%}
+    {%- endif -%}
+    {%- set o.s = o.s ~ said.v -%}
+  {%- else -%}{%- set o.s = o.s ~ m.text -%}
+  {%- endif -%}
+  {%- set o.pos = o.pos + m.text | length -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{#- 2019-2021, 10-15, 5x -#}
+{%- macro nz_ranges(text) -%}
+{%- set o = namespace(s='') -%}
+{%- for m in tts_norm_scan(text, '(?<![%w_.,%-])(%d%d%d%d)[-–](%d%d%d%d)(?![%w_.,%-])') -%}
+  {%- set o.s = o.s ~ ((' ' ~ year(m.groups[0]) ~ ' to ' ~ year(m.groups[1]) ~ ' ') if m.rule else m.text) -%}
+{%- endfor -%}
+{%- set o2 = namespace(s='') -%}
+{%- for m in tts_norm_scan(o.s, '(?<![%w_.,%-])(%d+)[-–](%d+)(?![%w_%-]|[.,]%d)') -%}
+  {%- set o2.s = o2.s ~ ((' ' ~ number_words(m.groups[0]) ~ ' to ' ~ number_words(m.groups[1]) ~ ' ') if m.rule else m.text) -%}
+{%- endfor -%}
+{%- set o3 = namespace(s='') -%}
+{%- for m in tts_norm_scan(o2.s, '(?<![%w_.])(%d+)x(?![%w_])') -%}
+  {%- set o3.s = o3.s ~ ((' ' ~ number_words(m.groups[0]) ~ ' times ') if m.rule else m.text) -%}
+{%- endfor -%}
+{{ o3.s }}
+{%- endmacro -%}
+
+{#- the passes that need a digit, in the order of dizione -#}
+{%- macro nz_figures(text) -%}
+{%- set text = nz_formula(text) if '$' in text else text -%}
+{#- continental figures become American ones, so every rule below sees one notation -#}
+{%- set text = nz_continental(text) -%}
+{%- set text = tts_norm_sub(text, '((?:[$£€¥₹]|(?<![A-Za-z])' ~ CURRENCY_CODES ~ ')%s?-?%d+),(%d%d?)(?!%d|[.,]%d)', '%1.%2') -%}
+{%- set text = tts_norm_sub(text, '(?<![%w_.,])(%d+),(%d%d?)(?=%s?(?:%%|' ~ SIGN_AFTER ~ '))', '%1.%2') -%}
+{%- set text = nz_money(text, false) -%}
+{%- set text = tts_norm_sub(text, '(?<![%w_.,%-])(%d[%d,]*(?:%.%d+)?)-(?=%d[%d,]*(?:%.%d+)?%s?' ~ SIGN_AFTER ~ ')', '%1 to ') -%}
+{%- set text = nz_money(text, true) -%}
+{%- set text = nz_cents(text) -%}
+{#- times of day, lengths of time, ratios -#}
+{%- set CLOCK = '(?:%d%d?:[0-5]%d(?:%s?[AaPp]%.?[Mm]%.?)?|(?<![%w_])%d%d?%s?[AaPp]%.?[Mm]%.?)' -%}
+{%- set text = tts_norm_sub(text, '(' ~ CLOCK ~ ')%s(' ~ ZONES ~ ')' ~ NOT_WORD, '%1 ⟦%2⟧') -%}
+{%- set text = tts_norm_sub(text, '(?<![%d.,:])(%d%d?)%.([0-5]%d)(?=%s?[AaPp]%.?[Mm]' ~ NOT_WORD ~ ')', '%1:%2') -%}
+{%- set text = nz_dot_times(text) -%}
+{%- set text = tts_norm_sub(text, '(' ~ CLOCK ~ ')%s?[-—]%s?(?=%d%d?(?::[0-5]%d|%s?[AaPp]%.?[Mm]' ~ NOT_WORD ~ '))', '%1 to ') -%}
+{%- set text = nz_ratio(text) -%}
+{%- set text = nz_time(text) -%}
+{%- set text = nz_hour(text) -%}
+{#- dates -#}
+{%- set habits = date_habits(text) -%}
+{%- set day_first = habits[:1] == '1' -%}
+{%- set text = nz_date(text, 'iso', false) -%}
+{%- set text = nz_date(text, 'dot', false) -%}
+{%- set text = nz_date(text, 'slash', day_first) -%}
+{%- set text = nz_days_of_month(text, true) -%}
+{%- set text = nz_days_of_month(text, false) -%}
+{%- set text = nz_month_day(text, true) -%}
+{%- set text = nz_month_day(text, false) -%}
+{#- telephone numbers, versions, codes -#}
+{%- set text = nz_phone(text) -%}
+{%- set text = nz_version(text) -%}
+{%- set text = nz_versioned(text) -%}
+{%- set text = nz_code(text) -%}
+{%- set text = nz_slashed(text) -%}
+{%- set text = nz_grouped(text) -%}
+{%- set text = tts_norm_sub(text, '(?<![%w_.])-(?=%d)', ' minus ') -%}
+{#- 3 × 4 metres -#}
+{%- set text = tts_norm_sub(text, '(?:(?<=%d)%s?[xX×]|×)%s?(?=%d[%d.,]*%s?(?:inches|metres?|meters?|pixels?|yards?|inch|feet|foot|mm|cm|km|ft|in|yd|px|m)' ~ NOT_WORD ~ ')', ' by ') -%}
+{%- set text = tts_norm_sub(text, '(?<=%d)%s[xX×]%s(?=%d)', ' times ') -%}
+{%- set text = nz_chat_forms(text) -%}
+{%- set text = nz_small_figures(text) -%}
+{%- set text = nz_area(text, true) -%}
+{%- set text = nz_measure(text) -%}
+{%- set text = nz_mixed_fraction(text) -%}
+{%- set text = nz_fraction(text, day_first, habits[1:] == '1') -%}
+{%- set text = nz_ranges(text) -%}
+{{ tts_norm_sub(text, '(?<=%d)%^(?=%d)', ' to the power of ') }}
+{%- endmacro -%}
+
+{#- ---------- signs, comparisons, abbreviations and the numbers that are left (normalize.py: _signs) ---------- -#}
+{#- the pattern is found somewhere in s -#}
+{%- macro has(s, pattern) -%}
+{%- set r = tts_norm_scan(s, pattern) -%}{{ '1' if r | length > 1 or (r | length == 1 and r[0].rule) else '' }}
+{%- endmacro -%}
+
+{#- text with the angle brackets that enclose something replaced by a mark of the same size, the inner ones first:
+    "<b>", "</p>", "<your name>", "Vec<u64>". The "<" and ">" that are left may compare two things -#}
+{%- macro without_angles(text) -%}
+{%- set a = namespace(s=text, done=false) -%}
+{%- for _ in range(8) -%}
+  {%- if not a.done and '<' in a.s and '>' in a.s -%}
+    {%- set marked = tts_norm_sub(a.s, '(?<![%w_)⟧⟡])<([A-Za-z0-9/!?][^<>(){}+*;|]*)>|(?<=[%w_⟧⟡])<([^<>(){}+*;|]{1,80})>(?![%w_$])|<(/?[A-Za-z][%w_%-]+/?)>', '\x01%1%2%3\x01') -%}
+    {%- set a.done = marked == a.s -%}
+    {%- set a.s = marked -%}
+  {%- endif -%}
+{%- endfor -%}
+{{ a.s }}
+{%- endmacro -%}
+
+{#- the words for a comparison sign, after "is" where the mark for it stands before the sign -#}
+{%- macro compared(marked, words) -%}{{ ' is ' if marked else ' ' }}{{ words }}{{ ' ' }}{%- endmacro -%}
+
+{#- put the mark for "is" after the first value of each comparison between two values: "67 > 66", "b>a", "f(x) >= 0".
+    Not where a word that opens no clause stands before that value: "for n > 4", "any q > 0" -#}
+{%- macro nz_mark_comparisons(text) -%}
+{%- set plain = without_angles(text) -%}
+{%- set o = namespace(s='', pos=0) -%}
+{%- for m in tts_norm_scan(text, [['spaced', ' (>=|<=|!=|[<>≥≤≠]) '], ['joined', '(>=|<=|!=|[<>≥≤≠])']]) -%}
+  {%- set mark = namespace(v=false) -%}
+  {%- if m.rule -%}
+    {%- set at = o.pos + (1 if m.rule == 'spaced' else 0) -%}
+    {%- set before = plain[:o.pos] -%}
+    {%- set value = tts_norm_scan(before, "(?:(?<![%w_.,])%d[%d.,]*%%?|(?<![%w_'’.%-])[A-Za-z]|%))$") -%}
+    {%- if plain[at:at + 1] == m.groups[0][:1] and value | length > 0 and value[-1].rule
+          and starts(plain[o.pos + m.text | length:], "[-+]?[$£€¥₹]?%d+(?:[.,%%]|(?![%w_]))|%.%d|[A-Za-z](?![%w_'’])|%(") -%}
+      {%- if value[-1].text == ')' -%}
+        {#- "f(x) > 0": the whole of it is the value -#}
+        {%- set cs = tts_norm_scan(before, '.') | map(attribute='text') | list -%}
+        {%- set st = namespace(i=cs | length - 1, depth=0, done=false) -%}
+        {%- for _ in cs -%}
+          {%- if not st.done and st.i >= 0 -%}
+            {%- set st.depth = st.depth + (1 if cs[st.i] == ')' else 0) - (1 if cs[st.i] == '(' else 0) -%}
+            {%- if st.depth == 0 -%}{%- set st.done = true -%}{%- else -%}{%- set st.i = st.i - 1 -%}{%- endif -%}
+          {%- endif -%}
+        {%- endfor -%}
+        {%- for _ in cs -%}
+          {%- if st.i > 0 and cs[st.i - 1] in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.' -%}{%- set st.i = st.i - 1 -%}{%- endif -%}
+        {%- endfor -%}
+        {%- set head = cs[:[st.i, 0] | max] | join('') -%}
+      {%- else -%}{%- set head = before[:before | length - value[-1].text | length] -%}
+      {%- endif -%}
+      {%- set opener = tts_norm_scan(head | trim, "[A-Za-z']+$") -%}
+      {%- set mark.v = not (opener | length > 0 and opener[-1].rule and opener[-1].text | lower not in CLAUSE_OPENERS) -%}
+    {%- endif -%}
+  {%- endif -%}
+  {%- set o.s = o.s ~ ('⟡' if mark.v else '') ~ m.text -%}
+  {%- set o.pos = o.pos + m.text | length -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{#- "b>a", "x<5", "p<.05": the words for "<" and ">" written with no spaces. A comparison when a figure stands on one
+    side, or a single letter or a bracket on both; "File>Open" is none -#}
+{%- macro nz_joined_comparisons(text) -%}
+{%- set plain = without_angles(text) -%}
+{%- set o = namespace(s='', pos=0) -%}
+{%- for m in tts_norm_scan(text, '(?<=[%w_)⟧⟡])[<>](?=[%w_(⟦]|%.%d| minus %d)') -%}
+  {%- set read = namespace(v=false) -%}
+  {%- if m.rule and plain[o.pos:o.pos + 1] == m.text -%}
+    {%- set before = tts_norm_sub(text[:o.pos], '⟡+$', '') -%}
+    {%- set after = text[o.pos + 1:] -%}
+    {%- set read.v = ends(before, '(?<![%w_.,])%d[%d.,]*%%?') or starts(after, '%d+(?:[.,%%]|(?![%w_]))|%.%d| minus %d')
+                     or (ends(before, "(?<![%w_'’.])[A-Za-z]|[)⟧]") and starts(after, "[A-Za-z](?![%w_'’])|[(⟦]")) -%}
+  {%- endif -%}
+  {%- set o.s = o.s ~ (compared(ends(text[:o.pos], '⟡ *'), 'less than' if m.text == '<' else 'greater than') if read.v else m.text) -%}
+  {%- set o.pos = o.pos + m.text | length -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{#- mark the letters that stand for something, to be named: "a=10", "b>a", "(k)". "A", "a" and the small letters
+    chat writes for a word ("k", "u", "r") are read as words elsewhere -#}
+{%- set LETTER_SIGN = '[<>+*/^≤≥≠≈×÷±]' -%}
+{%- set LETTER_SPACED = ' [<>+*≤≥≠≈×÷±] ' -%}
+{#- after "=" or a spaced sign the letter must not open a phrase ("mode = a set of notes") -#}
+{%- set NO_PHRASE = "(?![%w_'’%-]|%s+(?!(?:and|or|then|if|when|where|while|for|is|are|was|but|so|in|to)(?![%w_]))[A-Za-z])" -%}
+{%- set LETTER_PLACES = [
+    ['plain', "(?<![%w_'’.%-])[Aakur](?=⟡?%s?(?:[<>!=]=|=))"],
+    ['plain', "(?:[<>!=]=|=)%s?[Aakur]" ~ NO_PHRASE],
+    ['plain', "(?<![%w_'’.%-])[Aakur](?=⟡?" ~ LETTER_SIGN ~ "(?:[%w_($]|%.%d))"],
+    ['after_sign', "(?<=" ~ LETTER_SIGN ~ ")[Aakur](?![%w_'’%-])"],
+    ['plain', "(?<![%w_'’.%-])[Aakur](?=⟡?" ~ LETTER_SPACED ~ "(?:[A-Za-z](?![%w_'’])|%d+(?:[.,]|(?![%w_'’]))))"],
+    ['plain', "(?<![%w_'’.])(?:[A-Za-z]|%d[%d.,]*)⟡?" ~ LETTER_SPACED ~ "[Aakur]" ~ NO_PHRASE],
+    ['plain', "(?<=%()[Aakur](?=%)|, )"],
+    ['after_comma', "(?<= )[Aakur](?=%))"],
+    ['plain', "(?<![^%s])[akur](?=%)%s)"],
+] -%}
+{%- macro nz_named_letters(text) -%}
+{%- if has(text, '[=<>+*/^()≤≥≠≈×÷±]') -%}
+  {%- set plain = without_angles(text) -%}
+  {%- set found = namespace(at=[]) -%}
+  {%- for place in LETTER_PLACES -%}
+    {%- set sc = namespace(pos=0) -%}
+    {%- for m in tts_norm_scan(plain, place[1]) -%}
+      {%- set sc.pos = sc.pos + m.text | length -%}
+      {%- if m.rule -%}
+        {#- the letter is the last character matched -#}
+        {%- set before = plain[:sc.pos - 1] -%}
+        {%- if (place[0] == 'plain' or (place[0] == 'after_sign' and ends(before, '[%w_)⟡]' ~ LETTER_SIGN) and not ends(before, '[Ww]/'))
+               or (place[0] == 'after_comma' and before.endswith(', '))) and sc.pos - 1 not in found.at -%}
+          {%- set found.at = found.at + [sc.pos - 1] -%}
+        {%- endif -%}
+      {%- endif -%}
+    {%- endfor -%}
+  {%- endfor -%}
+  {%- set o = namespace(s='', pos=0) -%}
+  {%- for at in found.at | sort -%}
+    {%- set o.s = o.s ~ text[o.pos:at] ~ spell_mark(text[at:at + 1] | upper) -%}
+    {%- set o.pos = at + 1 -%}
+  {%- endfor -%}
+  {{ o.s ~ text[o.pos:] }}
+{%- else -%}{{ text }}
+{%- endif -%}
+{%- endmacro -%}
+
+{#- U.S., e.g., p.m.: letters with full stops -#}
+{%- macro nz_dotted(text) -%}
+{%- set o = namespace(s='', pos=0) -%}
+{%- for m in tts_norm_scan(text, '(?<![%w_])((?:[A-Za-z]%.){2,})') -%}
+  {%- if m.rule -%}
+    {%- set letters = m.text | replace('.', '') -%}
+    {%- set key = letters | lower -%}
+    {%- set o.s = o.s ~ ' ' ~ ('for example' if key == 'eg' else ('that is' if key == 'ie' else spell_mark(letters | upper)))
+                  ~ (' .' if new_sentence(text[o.pos + m.text | length:]) else '') ~ ' ' -%}
+  {%- else -%}{%- set o.s = o.s ~ m.text -%}
+  {%- endif -%}
+  {%- set o.pos = o.pos + m.text | length -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{#- abbreviations written with a full stop (abbreviations.txt): the stop is not a sentence end. "St." and "Dr." are
+    Saint or Doctor before a name and Street or Drive after one -#}
+{%- macro nz_abbreviations(text) -%}
+{%- set o = namespace(s='', pos=0) -%}
+{%- for m in tts_norm_scan(text, "(?<![%w_])([A-Za-z]{1,6})(?![%w_])(%.?)(?![A-Za-z'’])") -%}
+  {%- set said = namespace(v=m.text) -%}
+  {%- if m.rule -%}
+    {%- set token = m.groups[0] -%}{%- set stop = m.groups[1] -%}
+    {%- set key = token | lower -%}
+    {%- set entry_ = tts_dict('abbreviations', key) -%}
+    {%- if entry_ is not none or (key in ['st', 'dr'] and token[:1] in UPPER) -%}
+      {%- set following = text[o.pos + m.text | length:] -%}
+      {%- set next_word = at_start(following, '%s*([A-Za-z]+|%d)', 0) -%}
+      {%- set capital_next = next_word[:1] in UPPER if next_word else false -%}
+      {%- set number_next = next_word[:1] in '0123456789' if next_word else false -%}
+      {%- if key in ['st', 'dr'] and token[:1] in UPPER -%}
+        {%- if capital_next and (stop or key == 'dr') -%}{%- set said.v = ' saint ' if key == 'st' else ' doctor ' -%}
+        {%- elif stop and ends(text[:o.pos], '[A-Z][a-z]+%s+') -%}
+          {%- set said.v = (' street' if key == 'st' else ' drive') ~ (' ' if following | trim else ' .') -%}
+        {%- endif -%}
+      {%- elif entry_ is not none -%}
+        {%- set when = entry_[0] -%}
+        {%- set use = namespace(v=true) -%}
+        {%- if when == 'title' -%}{%- set use.v = token[:1] in UPPER and (capital_next or stop) -%}
+        {%- elif when == 'number' -%}
+          {%- set previous = tts_norm_scan(text[:o.pos], '([A-Za-z]+)%.?%s+$') -%}
+          {%- set numbered = previous | length > 0 and previous[-1].rule and previous[-1].groups[0] | lower in NUMBERED -%}
+          {%- set use.v = stop and (number_next or (key == 'no' and numbered)) -%}
+        {%- elif not stop or (when == 'small' and is_upper(token)) -%}{%- set use.v = false -%}
+        {%- elif key in ['mar', 'sun', 'sat', 'jan', 'dec', 'max', 'min', 'est', 'no'] and token[:1] not in UPPER and not number_next -%}
+          {#- also words; "max. load" and "est. time" are the abbreviation, "he sat." is not -#}
+          {%- set use.v = key in ['max', 'min', 'est'] and next_word and next_word[:1] in LOWER -%}
+        {%- endif -%}
+        {%- if use.v -%}
+          {#- "Est. arrival"; "est. 1885" is "established" -#}
+          {%- set words = 'estimated' if key == 'est' and not number_next else entry_[1] -%}
+          {%- set closes = when != 'title' and (not following | trim or (capital_next and next_word | lower in SENTENCE_STARTERS)) -%}
+          {%- set said.v = ' ' ~ words ~ (' .' if closes else '') ~ ' ' -%}
+        {%- endif -%}
+      {%- endif -%}
+    {%- endif -%}
+  {%- endif -%}
+  {%- set o.s = o.s ~ said.v -%}
+  {%- set o.pos = o.pos + m.text | length -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{#- letters and digits written together: B12, 14C, X7K, PS5. A single letter is always named -#}
+{%- macro nz_glued(text) -%}
+{%- set o = namespace(s='') -%}
+{%- for m in tts_norm_scan(text, "(?<![%w_'.])(?=%d*[A-Za-z])(?=[A-Za-z]*%d)[A-Za-z0-9]+(?![%w_']|%.%d)") -%}
+  {%- if m.rule -%}
+    {%- set out = namespace(p=[]) -%}
+    {%- for run in tts_norm_scan(m.text, [['d', '%d+'], ['l', '[A-Za-z]+']]) -%}
+      {%- if run.rule == 'd' -%}{%- set out.p = out.p + [cardinal(run.text) if short_number(run.text) else digits(run.text, 'zero')] -%}
+      {%- elif run.text | length == 1 or ((is_upper(run.text) or run.text | length <= 3) and not reads_as_word(run.text)) -%}{%- set out.p = out.p + [spell_mark(run.text)] -%}
+      {%- else -%}{%- set out.p = out.p + [run.text] -%}
+      {%- endif -%}
+    {%- endfor -%}
+    {%- set o.s = o.s ~ ' ' ~ out.p | join(' ') ~ ' ' -%}
+  {%- else -%}{%- set o.s = o.s ~ m.text -%}
+  {%- endif -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{#- the numbers that are left: a reference is read digit by digit, a year as a year, a model number in pairs -#}
+{%- macro nz_numbers(text) -%}
+{%- set o = namespace(s='', pos=0) -%}
+{%- for m in tts_norm_scan(text, '(?<![%w_.])%d{1,3}(?:,%d%d%d)+(?:%.%d+)?|%d+(?:%.%d+)?|(?<![%w_.])%.%d+') -%}
+  {%- if m.rule -%}
+    {%- set value = m.text -%}
+    {%- set plain = value | replace(',', '') -%}
+    {%- set whole = '.' not in plain and ',' not in value and not plain.startswith('0') -%}
+    {%- set after = text[o.pos + value | length:] -%}
+    {%- set said = namespace(v='') -%}
+    {%- if whole and plain | length > 2 -%}
+      {%- set kind = identifier(text, o.pos) -%}
+      {%- set limit = 3 if kind in ['phone', 'pin'] else (4 if kind in ['strong', 'enter'] else (5 if kind == 'weak' else 7)) -%}
+      {#- a reference, not an amount -#}
+      {%- if plain | length >= limit and not (not kind and plain.endswith('000'))
+            and not (kind == 'enter' and plain | length == 4 and plain | int >= 1900 and plain | int <= 2099)
+            and not (kind in ['', 'phone', 'weak'] and counted(after)) -%}
+        {%- set said.v = digits(plain, 'oh' if kind == 'phone' else 'zero') -%}
+      {%- endif -%}
+    {%- endif -%}
+    {%- if not said.v and whole and plain | length == 4 -%}
+      {%- set number = plain | int -%}
+      {%- set before = text[:o.pos] -%}
+      {%- set near = namespace(last='') -%}
+      {%- for w in tts_norm_scan(before[[0, before | length - 24] | max:], '[A-Za-z]+') -%}{%- if w.rule -%}{%- set near.last = w.text | lower -%}{%- endif -%}{%- endfor -%}
+      {%- set next_word = at_start(after, '%s*([A-Za-z]+)', 0) | lower -%}
+      {%- set many = next_word and ((next_word.endswith('s') and not next_word.endswith('ss') and next_word | length > 1)
+                                    or next_word.endswith('people') or next_word.endswith('men') or next_word.endswith('children')
+                                    or next_word.endswith('feet') or next_word.endswith('miles')) -%}
+      {%- set dated = near.last and (near.last in YEAR_CONTEXT or near.last[:3] in MONTH_NUMBER) -%}
+      {%- set model = tts_norm_scan(before, '(?<![A-Za-z])([A-Z]{2,5}) $') -%}
+      {#- RTX 4070, ISO 9001 -#}
+      {%- if model | length > 0 and model[-1].rule and model[-1].groups[0] not in CAPITAL_WORDS and not many -%}{%- set said.v = pairs(number) -%}
+      {%- elif (number >= 1100 and number <= 1999) or (number >= 2000 and number <= 2099 and (dated or not many)) -%}{%- set said.v = year(number) -%}
+      {%- endif -%}
+    {%- endif -%}
+    {%- set o.s = o.s ~ ' ' ~ (said.v if said.v else number_words(value)) ~ ' ' -%}
+  {%- else -%}{%- set o.s = o.s ~ m.text -%}
+  {%- endif -%}
+  {%- set o.pos = o.pos + m.text | length -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{%- macro nz_signs(text) -%}
+{%- set text = nz_named_letters(text) -%}
+{%- set text = nz_joined_comparisons(text) if '<' in text or '>' in text else text -%}
+{%- set o = namespace(s='') -%}
+{%- for m in tts_norm_scan(text, '(?<![%w_%-])(' ~ SIZES ~ ')-(' ~ SIZES ~ ')(?![%w_%-])') -%}
+  {%- set o.s = o.s ~ ((' ' ~ spell_mark(m.groups[0]) ~ ' to ' ~ spell_mark(m.groups[1]) ~ ' ') if m.rule else m.text) -%}
+{%- endfor -%}
+{%- set text = tts_norm_sub(o.s, '#(?=%d)', ' number ') -%}
+{#- Order #A7 -#}
+{%- set text = tts_norm_sub(text, '#(?=[A-Z]{1,4}-?%d)', ' number ') -%}
+{%- set text = tts_norm_sub(text, '#(?=[A-Za-z])', ' hashtag ') -%}
+{%- set text = tts_norm_sub(text, '~(?=%s?%d)', ' about ') -%}
+{%- set o = namespace(s='', pos=0) -%}
+{%- for m in tts_norm_scan(text, '(?<=[%w_%s⟧⟡])(>=|<=|!=)(?=[%w_%s⟦])') -%}
+  {%- set o.s = o.s ~ (compared(ends(text[:o.pos], '⟡ *'), COMPARISONS[m.text]) if m.rule else m.text) -%}
+  {%- set o.pos = o.pos + m.text | length -%}
+{%- endfor -%}
+{#- RTX 4070 Ti -#}
+{%- set text = tts_norm_sub(o.s, '(?<=%d) Ti' ~ NOT_WORD, ' ⟦TI⟧') -%}
+{%- set text = tts_norm_sub(text, '(?<![%w_])([Rr])ef%.%s?[Nn]o%.(?=%s)', '%1eference number') -%}
+{%- set text = nz_dotted(text) if '.' in text else text -%}
+{%- set text = nz_abbreviations(text) -%}
+{%- if has(text, '%d') -%}
+  {%- set text = nz_glued(text) -%}
+  {%- set text = nz_numbers(text) -%}
+{%- endif -%}
+{%- set text = tts_norm_sub(text, '(?<=[A-Za-z])/(?=[A-Za-z])', ' ') -%}
+{%- set o = namespace(s='', pos=0) -%}
+{%- for m in tts_norm_scan(text, '(?<=%d)⟡?%s?[<>]%s?(?=%d)|(?<=%s)[<>](?=%s)') -%}
+  {%- set o.s = o.s ~ (compared(m.text.startswith('⟡') or ends(text[:o.pos], '⟡ *'), 'less than' if '<' in m.text else 'greater than') if m.rule else m.text) -%}
+  {%- set o.pos = o.pos + m.text | length -%}
+{%- endfor -%}
+{%- set text = o.s -%}
+{%- set o = namespace(s='', pos=0) -%}
+{%- for m in tts_norm_scan(text, '[&@+=%%×÷±©§°≥≤≠≈∞√π]') -%}
+  {%- if m.rule -%}
+    {%- set o.s = o.s ~ (compared(ends(text[:o.pos], '⟡ *'), SYMBOL_WORDS[m.text]) if m.text in '≥≤≠' else (' ' ~ SYMBOL_WORDS[m.text] ~ ' ')) -%}
+  {%- else -%}{%- set o.s = o.s ~ m.text -%}
+  {%- endif -%}
+  {%- set o.pos = o.pos + m.text | length -%}
+{%- endfor -%}
+{{ o.s | replace('⟡', '') }}
+{%- endmacro -%}
+
+{#- ---------- web addresses and file names ---------- -#}
+{#- one run of letters inside an address: a word, or letters to name -#}
+{%- macro address_part(part) -%}
+{%- set lower = part | lower -%}
+{%- if part | length > 1 and is_word(lower) and (part | length > 2 or lower in SHORT_WORDS) -%}{{ part }}
+{%- else -%}{{ spell_mark(part) if part | length <= 3 else part }}
+{%- endif -%}
+{%- endmacro -%}
+
+{%- macro spoken_address(core) -%}
+{%- set pieces = tts_norm_scan(core, [['l', '[A-Za-z]+'], ['d', '%d+'], ['o', '.']]) -%}
+{%- set mail = '@' in core -%}
+{%- set a = namespace(out=[], host=false) -%}
+{%- for p in pieces -%}
+  {%- set piece = p.text -%}
+  {%- set following = pieces[loop.index0 + 1].text if not loop.last else '' -%}
+  {%- if piece == '@' -%}{%- set a.host = true -%}{%- endif -%}
+  {%- if p.rule == 'd' -%}{%- set a.out = a.out + [address_digits(piece)] -%}
+  {%- elif p.rule != 'l' -%}{%- set a.out = a.out + [SEPARATORS[piece] if piece in SEPARATORS else ''] -%}
+  {%- elif loop.index0 > 0 and pieces[loop.index0 - 1].text == '.' and (a.host or not mail)
+          and (following in ['', '/', ':', '?', '#'] or (piece | length <= 3 and (tts_dict('endings', piece | lower) is not none or not is_word(piece | lower)))) -%}
+    {#- the ending of a file name or an address: a word if the table says so, otherwise letters -#}
+    {%- set a.out = a.out + [piece | lower if tts_dict('endings', piece | lower) == 'word' else spell_mark(piece)] -%}
+  {%- else -%}
+    {%- set run = namespace(letters='') -%}
+    {%- for part in tts_norm_scan(piece, '[A-Z]?[a-z]+|(?:[A-Z](?![a-z]))+') -%}
+      {%- if part.rule -%}
+        {%- set said = address_part(part.text) -%}
+        {%- if said.startswith('⟦') -%}{%- set run.letters = run.letters ~ part.text -%}
+        {%- else -%}
+          {%- if run.letters -%}{%- set a.out = a.out + [spell_mark(run.letters)] -%}{%- set run.letters = '' -%}{%- endif -%}
+          {%- set a.out = a.out + [said] -%}
+        {%- endif -%}
+      {%- endif -%}
+    {%- endfor -%}
+    {%- if run.letters -%}{%- set a.out = a.out + [spell_mark(run.letters)] -%}{%- endif -%}
+  {%- endif -%}
+{%- endfor -%}
+{%- set kept = namespace(p=[]) -%}
+{%- for item in a.out -%}{%- if item -%}{%- set kept.p = kept.p + [item] -%}{%- endif -%}{%- endfor -%}
+{{ kept.p | join(' ') }}
+{%- endmacro -%}
+
+{#- web and mail addresses, file names and paths: the dots, slashes, underscores and @ are spoken. The ending is a
+    word when the table says so ("dot com") and letters otherwise ("dot P D F") -#}
+{#- the ending of a file name: a dot, a letter, up to nine letters or figures; a part of the name that leaves an
+    ending after it. So the name stops before its last possible ending, as in dizione, and a name with no possible
+    ending is no match at all (the search goes on inside it: "1.0bpmhttp://...") -#}
+{%- set FILE_ENDING = '%.[A-Za-z][A-Za-z0-9]{0,9}(?![%w_%-])' -%}
+{%- set FILE_PART = '%.[%w_%-]+(?=(?:(?!' ~ FILE_ENDING ~ ')%.[%w_%-]+)*' ~ FILE_ENDING ~ ')' -%}
+{%- macro nz_address(text) -%}
+{%- set o = namespace(s='') -%}
+{%- for m in tts_norm_scan(text, [
+    ['url',  "(?:https?://|www%.)[^%s<>\"']+"],
+    ['mail', "[%w_.+%-]+@[%w_%-]+(?:%.[%w_%-]+)+"],
+    ['file', "(?<![%w_@./%-])(/?(?:[%w_~%-]+/)+)?([%w_%-]+(?:" ~ FILE_PART ~ ")*)%.([A-Za-z][A-Za-z0-9]{0,9})(?![%w_%-])(/[^%s<>\"']*)?"],
+]) -%}
+  {%- set said = namespace(v=m.text) -%}
+  {%- if m.rule -%}
+    {%- set ok = namespace(v=true) -%}
+    {%- if m.rule == 'file' -%}
+      {%- set stem = m.groups[1] -%}{%- set ending = m.groups[2] -%}
+      {%- set listed = tts_dict('endings', ending | lower) is not none -%}
+      {%- set marked = m.groups[0] or m.groups[3] or '_' in stem -%}
+      {#- "the end.Then": a missing space, not a file -#}
+      {%- set ok.v = listed or (marked and ending | length <= 5 and (ending == ending | lower or ending == ending | upper) and not is_word(ending | lower)) -%}
+    {%- endif -%}
+    {%- if ok.v -%}
+      {#- punctuation of the sentence, not of the address -#}
+      {%- set body = tts_norm_sub(m.text, '[.,;:!?)]+$', '') -%}
+      {%- set core = tts_norm_sub(tts_norm_sub(tts_norm_sub(body, '^https?://', ''), '^www%.', ''), '/+$', '') -%}
+      {%- set said.v = ' ' ~ spoken_address(core) ~ ' ' ~ m.text[body | length:] -%}
+    {%- endif -%}
+  {%- endif -%}
+  {%- set o.s = o.s ~ said.v -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{#- the text of one sentence with every non-word written out, marks included (normalize.py: rewrite). The text has
+    had its accents, quotation marks, brackets and inline pronunciations dealt with -#}
+{%- macro nz_rewrite(text) -%}
+{%- set text = nz_address(text) -%}
+{%- set text = tts_norm_sub(text | replace('→', ' to '), '[🀀-🫿☀-➿←-⇿⬀-⯿️‍⃣]+', ' ') -%}
+{#- faces are not read and make no pause: ":)", ";-)", "<3", "^_^"; a heart joined to
+    a word of two letters or more: "you<3" -#}
+{%- set o = namespace(s='', pos=0) -%}
+{%- for m in tts_norm_scan(text, [
+    ['mouth', "(?<![A-Za-z0-9])([:;=]['^%-]?)([DPpOo3|/\\*><]+)"],
+    ['face',  "(?<![A-Za-z0-9])(?:[)(][%-%^']?[:;=]|<3+|</3|%^_?%^|-_-|[oO]_[oO]|>_<|T_T|\\o/|:'[()])(?![A-Za-z0-9])|[:;]['^%-]?[)(]+|(?<![A-Za-z0-9)])=['^%-]?[)(]+|=['^%-]?[)(]+(?![A-Za-z0-9(.$%-])"],
+    ['heart', '(?<=[A-Za-z])<3+(?![.,]?%d)'],
+]) -%}
+  {%- if m.rule == 'mouth' -%}
+    {#- the longest mouth after which no letter or figure comes -#}
+    {%- set run = m.groups[1] -%}
+    {%- set after = text[o.pos + m.text | length:] -%}
+    {%- set cutm = namespace(n=0) -%}
+    {%- for k in range(run | length, 0, -1) -%}
+      {%- if not cutm.n and not starts(run[k:] ~ after, '[A-Za-z0-9]') -%}{%- set cutm.n = k -%}{%- endif -%}
+    {%- endfor -%}
+    {%- set o.s = o.s ~ ((' ' ~ run[cutm.n:]) if cutm.n else m.text) -%}
+  {%- else -%}
+    {%- set o.s = o.s ~ (' ' if m.rule == 'face' or (m.rule == 'heart' and ends(text[:o.pos], '[A-Za-z][A-Za-z]')) else m.text) -%}
+  {%- endif -%}
+  {%- set o.pos = o.pos + m.text | length -%}
+{%- endfor -%}
+{%- set text = o.s -%}
+{%- set text = tts_norm_sub(text, '%.%.%.+', '…') -%}
+{%- set text = tts_norm_sub(text, '(?<=%d)%s-%s(?=%d[%d.,]*%s?=)', ' minus ') -%}
+{#- 10-3=7, not the date 2024-11-15= -#}
+{%- set text = tts_norm_sub(text, '(?<![%d.,/%-])(%d[%d.,]*)-(?=%d[%d.,]*=)', '%1 minus ') -%}
+{#- a hyphen between spaces or two hyphens are a dash -#}
+{%- set text = tts_norm_sub(text, '%-%-+|%s%-%s', '—') -%}
+{%- set text = tts_norm_sub(text, '(?i)(?<![%w_])w/o(?![%w_])', ' without ') -%}
+{%- set text = tts_norm_sub(text, '(?i)(?<![%w_])w/(?=%s|$)', ' with ') -%}
+{%- set text = tts_norm_sub(text, '(?i)(?<![%w_])n/a(?![%w_])', ' not applicable ') -%}
+{%- set text = text | replace("'n'", " 'n ") -%}
+{%- set text = nz_mark_comparisons(text) if has(text, '[<>≥≤≠]|!=') else text -%}
+{#- most text has no digit and skips these passes -#}
+{%- set text = nz_figures(text) if has(text, '%d') else text -%}
+{{ nz_signs(text) }}
+{%- endmacro -%}
+
+{#- ---------- sentences (tiny-voca infer.py: sentences): each one is read by itself ----------
+    no break after a title or an initial, and only before a capital or a figure -#}
+{%- set whole = tts_norm_sub(messages[0].content if messages is defined and messages else '', '%s+', ' ') | trim -%}
+{#- a text that ends without a mark gets a full stop: without one the audio breaks off (infer.py: finished).
+    A face or another sign after the last word is no mark ("see you :)"), a comma or a colon is one -#}
+{%- set last = namespace(w='') -%}
+{%- for seg in tts_norm_scan(whole, '%S+') -%}{%- if seg.rule -%}{%- set last.w = seg.text -%}{%- endif -%}{%- endfor -%}
+{%- if last.w -%}
+  {%- set lw = namespace(word=false, stop=false) -%}
+  {%- for seg in tts_norm_scan(last.w, [['word', '[%w²³¹¼½¾⅐-⅞]'], ['stop', '[.!?…]']]) -%}
+    {%- if seg.rule == 'word' -%}{%- set lw.word = true -%}{%- elif seg.rule == 'stop' -%}{%- set lw.stop = true -%}{%- endif -%}
+  {%- endfor -%}
+  {%- set closed = tts_norm_scan(last.w, '[.!?…,;:]["\'”’)%]»]*$') -%}
+  {%- set marked = (closed | length > 0 and closed[-1].rule is not none) if lw.word else (lw.stop or last.w in [',', ';', ':']) -%}
+  {%- if not marked -%}{%- set whole = whole ~ '.' -%}{%- endif -%}
+{%- endif -%}
+{%- set sp = namespace(parts=[], cur='') -%}
+{%- for seg in tts_norm_scan(whole,
+                             [['end', '([.!?…]["\')%]]?)%s+(?=["\'(%[]?[A-Z0-9])']]) -%}
+  {%- if seg.rule -%}
+    {%- set sp.cur = sp.cur ~ seg.groups[0] -%}
+    {%- set held = tts_norm_scan(sp.cur, '(?i)(?<![A-Za-z0-9_])(?:mrs|mr|ms|dr|prof|sr|jr|st|vs|etc|no|inc|ltd|co|e%.g|i%.e|a%.m|p%.m|[a-z])%.$') -%}
+    {%- if held and held[-1].rule -%}{%- set sp.cur = sp.cur ~ ' ' -%}
+    {%- else -%}{%- set sp.parts = sp.parts + [sp.cur] -%}{%- set sp.cur = '' -%}
+    {%- endif -%}
+  {%- else -%}{%- set sp.cur = sp.cur ~ seg.text -%}
+  {%- endif -%}
+{%- endfor -%}
+{%- if sp.cur -%}{%- set sp.parts = sp.parts + [sp.cur] -%}{%- endif -%}
+{%- set spoken_text = namespace(s='') -%}
+{%- for sentence in sp.parts -%}
+{%- if has(sentence, '[%w²³¹¼½¾⅐-⅞]') -%}
+{#- "1½" before the fraction is taken apart, "m²" before the power becomes a word -#}
+{%- set text = nz_vulgar(sentence) -%}
+{%- set text = nz_area(text, false) | replace('²', ' squared ') | replace('³', ' cubed ') -%}
+{#- compatibility forms (NFKC, a character at a time): "…" is three full stops, "fi" is "fi" -#}
+{%- set forms = namespace(s='') -%}
+{%- for seg in tts_norm_scan(text, '[^ -~%s]') -%}
+  {%- set form = tts_dict('nfkc', seg.text) if seg.rule else none -%}
+  {%- set forms.s = forms.s ~ (form if form is not none else seg.text) -%}
+{%- endfor -%}
+{#- the marks of the text rules and control characters are not part of a text -#}
+{%- set text = tts_norm_sub(forms.s, '[⟦⟧⟪⟫⟡⟐ⓐ-ⓩ\x01]', '') | replace('⁄', '/') | replace('−', ' minus ') -%}
+{#- accented latin letters: a few words keep their reading, the others lose the accent ("café" is in the dictionary as "cafe") -#}
+{%- set fold = namespace(s='') -%}
+{%- for seg in tts_norm_scan(text, [
+    ['decides', '%b([Rr][éÉ][Ss][Uu][Mm][éÉ]|[Rr][Ee][Ss][Uu][Mm][éÉ]|[Ee][Xx][Pp][Oo][Ss][éÉ]|[Rr][Oo][Ss][éÉ]|[Pp][âÂ][Tt][éÉ]|[Mm][Aa][Tt][éÉ]|[Ss][Aa][Kk][éÉ]|[Ll][Aa][Mm][éÉ])([Ss]?)%b'],
+    ['letter',  '[^ -~%s]'],
+]) -%}
+  {%- if seg.rule == 'decides' -%}
+    {%- set fold.s = fold.s ~ '{' ~ ACCENT_DECIDES[seg.groups[0] | lower | replace('É', 'é') | replace('Â', 'â')] ~ (' Z' if seg.groups[1] else '') ~ '}' -%}
+  {%- elif seg.rule == 'letter' -%}
+    {%- set plain = tts_dict('accents', seg.text) -%}
+    {%- set fold.s = fold.s ~ (plain if plain is not none else seg.text) -%}
+  {%- else -%}{%- set fold.s = fold.s ~ seg.text -%}
+  {%- endif -%}
+{%- endfor -%}
+{#- 10–15 is a range, not a pause -#}
+{%- set text = nz_day_range(tts_norm_sub(fold.s, '(?<=%d)–(?=%d)', '-')) -%}
+{%- set text = text | replace('‘', "'") | replace('’', "'") | replace('‚', "'") | replace('‛', "'") | replace('`', "'") | replace('´', "'")
+                    | replace('“', '"') | replace('”', '"') | replace('„', '"') | replace('‟', '"') | replace('«', '"') | replace('»', '"')
+                    | replace('–', '—') | replace('―', '—') | replace('[', '(') | replace(']', ')') -%}
+{#- inline pronunciations become a mark, the other braces are read like brackets -#}
+{%- set il = namespace(s='', list=[]) -%}
+{%- for seg in tts_norm_scan(text, [['inline', "([A-Za-z']*)%{([^{}]*)%}"]]) -%}
+  {%- set ph = inline_phones(seg.groups[1]) if seg.rule and il.list | length < 26 else '' -%}
+  {%- if ph -%}
+    {%- set il.s = il.s ~ ' ⟪' ~ INLINE_KEYS[il.list | length] ~ '⟫ ' -%}
+    {%- set il.list = il.list + [{'w': seg.groups[0] | lower, 'r': seg.groups[0], 'a': false, 'spell': false, 'ps': ph}] -%}
+  {%- else -%}{%- set il.s = il.s ~ seg.text | replace('{', '(') | replace('}', ')') -%}
+  {%- endif -%}
+{%- endfor -%}
+{#- figures, signs and abbreviations in words -#}
+{%- set text = nz_rewrite(il.s) -%}
+{#- a text mostly in capitals: its capitals do not mark initialisms -#}
+{%- set caps = namespace(n=0, up=0) -%}
+{%- for seg in tts_norm_scan(text, '[A-Za-z][A-Za-z]+') -%}
+  {%- if seg.rule -%}{%- set caps.n = caps.n + 1 -%}{%- if seg.text == seg.text | upper -%}{%- set caps.up = caps.up + 1 -%}{%- endif -%}{%- endif -%}
+{%- endfor -%}
+{%- set all_caps = caps.n >= 3 and 5 * caps.up >= 3 * caps.n -%}
+
+{%- set segs = tts_norm_scan(text, [
+    ['spelled', '⟦([A-Za-z ]+)⟧'],
+    ['inline',  '⟪(.)⟫'],
+    ['aword',   "(?i)'(?:em|tis|twas|til|cause|bout|round|n)(?![A-Za-z]|'[A-Za-z])"],
+    ['word',    "[A-Za-z]+(?:'[A-Za-z]+)*"],
+    ['number',  '%d+'],
+    ['join',    '(?<=%S)-(?=%w)'],
+    ['hyphen',  '-'],
+    ['inside',  "(?<=%w)'(?=%w)(?![A-Za-z])"],
+    ['punct',   '[,.?!;:"\'()—…]'],
+]) -%}
+{#- attach: a hyphen joins the next word; previous: the word before as written; fresh: a sentence starts here;
+    short: where "Ss" or "Zz" stands apart (perhaps a hiss); replies: where a chat spelling read only as a reply stands -#}
+{%- set it = namespace(items=[], attach=false, previous='', fresh=true, short=[], replies=[]) -%}
+{%- for seg in segs -%}
+  {%- set k = loop.index0 -%}
+  {%- set t = namespace(new=[], word='', done=false) -%}
+  {%- if seg.rule == 'join' -%}
+    {%- set it.attach = it.items | length > 0 and it.items[-1].w is defined -%}
+  {%- elif seg.rule == 'hyphen' -%}
+    {%- set it.attach = false -%}
+  {%- elif seg.rule == 'punct' -%}
+    {%- set it.items = it.items + [{'p': seg.text}] -%}
+    {%- if seg.text in '.!?;…' -%}{%- set it.fresh = true -%}{%- endif -%}
+    {%- set it.attach = false -%}
+  {%- elif seg.rule -%}
+    {%- if seg.rule == 'word' -%}{%- set t.word = seg.text -%}
+    {%- elif seg.rule == 'aword' -%}{%- set t.word = seg.text if is_word(seg.text | lower) else seg.text[1:] -%}
+    {%- elif seg.rule == 'inline' -%}
+      {%- for key in INLINE_KEYS -%}{%- if key == seg.groups[0] and loop.index0 < il.list | length -%}{%- set t.new = [il.list[loop.index0]] -%}{%- endif -%}{%- endfor -%}
+    {%- elif seg.rule == 'spelled' -%}
+      {%- set t.new = [{'w': seg.groups[0] | replace(' ', '') | lower, 'r': seg.groups[0], 'a': false, 'spell': true, 'ps': ''}] -%}
+    {%- elif seg.rule == 'number' -%}
+      {%- for x in number_words(seg.text).split(' ') -%}{%- if x -%}{%- set t.new = t.new + [{'w': x, 'r': seg.text, 'a': false, 'spell': false, 'ps': ''}] -%}{%- endif -%}{%- endfor -%}
+    {%- endif -%}
+    {%- if t.word -%}
+      {#- one run of letters and apostrophes as written -#}
+      {%- set raw = t.word -%}
+      {%- set lower = raw | lower -%}
+      {%- set chat = tts_dict('chat', lower) -%}
+      {%- set possessive = lower.endswith("'s") -%}
+      {%- set stem = raw[:-2] if possessive else raw -%}
+      {%- set n = stem | length -%}
+      {%- if chat is not none and (raw | length > 1 or raw == lower) and not chat[1] -%}
+        {%- if chat[0] != '-' -%}
+          {%- for part in chat[0].split(' ') -%}
+            {%- set t.new = t.new + [{'w': part[1:] | lower, 'r': raw, 'a': false, 'spell': true, 'ps': ''} if part.startswith('*')
+                                     else {'w': part | lower, 'r': part, 'a': false, 'spell': false, 'ps': ''}] -%}
+          {%- endfor -%}
+        {%- endif -%}
+      {%- elif raw in ROMAN and it.previous and it.previous[:1] in UPPER -%}
+        {#- "World War II", "Henry VIII" -#}
+        {%- set said = cardinal(ROMAN[raw]) if it.previous | lower in CARDINAL_AFTER else 'the ' ~ ordinal(ROMAN[raw] | string) -%}
+        {%- for x in said.split(' ') -%}{%- set t.new = t.new + [{'w': x, 'r': raw, 'a': false, 'spell': false, 'ps': ''}] -%}{%- endfor -%}
+      {%- elif n == 1 -%}
+        {#- "A" is the article unless it labels something: "Plan A.", "vitamin A" -#}
+        {%- set label = stem == 'A' and it.previous and (not ahead(segs, k, '%s*[A-Za-z]') or (not it.fresh and it.previous | lower in LABELLED)) -%}
+        {%- set t.new = [{'w': lower, 'r': raw, 'a': false, 'spell': not (stem in ['a', 'A', 'I', 'i'] and not possessive and not label), 'ps': ''}] -%}
+      {%- elif n > 2 and stem[:1] in 'SsZz' and lower | replace(lower[:1], '') == '' and (all_caps or not is_upper(stem)) -%}
+        {#- "Sss", "Zzzz": a hiss, a snore -#}
+        {%- set t.new = [{'w': lower, 'r': raw, 'a': false, 'spell': false, 'ps': lower[:1]}] -%}
+      {%- else -%}
+        {%- set known_stem = is_word(stem | lower) -%}
+        {%- if is_upper(stem) and "'" not in stem and n >= 2 and n <= 5 and (not all_caps or not known_stem) and (stem in INITIALISMS or not known_stem) -%}
+          {%- set t.new = [{'w': lower, 'r': raw, 'a': false, 'spell': true, 'ps': ''}] -%}{%- set t.done = true -%}
+        {%- endif -%}
+        {#- iOS, macOS, eSIM: small letters, then capitals that are named. The dictionary's "ios" is another word -#}
+        {%- set capitals = fullmatch(raw, "[a-z]+(?:[A-Z][a-z]+)*[A-Z]{2,}(?:'s)?") -%}
+        {%- if not t.done and (capitals or not is_word(lower)) -%}
+          {%- if not capitals -%}
+            {%- for candidate in unstretched(lower).split(' ') -%}
+              {%- if not t.done and candidate and is_word(candidate) -%}
+                {%- set t.new = [{'w': candidate, 'r': raw, 'a': false, 'spell': false, 'ps': ''}] -%}{%- set t.done = true -%}
+              {%- endif -%}
+            {%- endfor -%}
+          {%- endif -%}
+          {%- if not t.done -%}
+            {#- iPhone, YouTube, JavaScript -#}
+            {%- set parts = tts_norm_scan(raw, "[A-Z]?[a-z']+|(?:[A-Z](?![a-z]))+") -%}
+            {%- set whole = namespace(v=true) -%}
+            {%- for part in parts -%}{%- if not part.rule -%}{%- set whole.v = false -%}{%- endif -%}{%- endfor -%}
+            {%- if whole.v and parts | length > 1 -%}
+              {%- for p in parts -%}
+                {%- set part = p.text -%}
+                {%- set named = capitals and is_upper(part) and part in INITIALISMS -%}
+                {%- set spelled = part | length == 1 or named or (is_upper(part) and not is_word(part | lower)) -%}
+                {%- set spelled = true if spelled else false -%}
+                {%- if capitals and t.new and t.new[-1].spell and (spelled or part == "'s") -%}
+                  {%- set before = t.new[-1] -%}
+                  {%- set t.new = t.new[:-1] + [{'w': before.w ~ part | lower, 'r': before.r ~ part, 'a': before.a, 'spell': true, 'ps': ''}] -%}
+                {%- else -%}
+                  {%- set t.new = t.new + [{'w': part | lower, 'r': part, 'a': t.new | length > 0, 'spell': spelled, 'ps': ''}] -%}
+                {%- endif -%}
+              {%- endfor -%}
+              {%- set t.done = true -%}
+            {%- endif -%}
+          {%- endif -%}
+        {%- endif -%}
+        {%- if not t.done -%}{%- set t.new = [{'w': lower, 'r': raw, 'a': false, 'spell': false, 'ps': ''}] -%}{%- endif -%}
+      {%- endif -%}
+      {%- set it.previous = raw -%}
+      {%- set kb = (k - 2) if seg.rule == 'word' and k > 0 and segs[k - 1].rule == 'punct' and segs[k - 1].text == "'" else (k - 1) -%}
+      {%- set before = segs[kb].text[-1:] if kb >= 0 else ' ' -%}
+      {%- set apart = before in [' ', '\t', '\n', '\r', '"', "'"] -%}
+      {%- if raw in ['Ss', 'ss', 'Zz', 'zz'] and t.new | length == 1 and (apart or before == '(') and ahead(segs, k, '[.!?…,;:—"\')]*%s') -%}
+        {%- set it.short = it.short + [it.items | length] -%}
+      {%- endif -%}
+      {%- if chat is not none and chat[1] and (raw | length > 1 or raw == lower) and t.new | length == 1 and apart
+            and ahead(segs, k, '[.?…,;—]*["\']?%s|!+["\']?%s*$') -%}
+        {%- set it.replies = it.replies + [it.items | length] -%}
+      {%- endif -%}
+    {%- endif -%}
+    {%- if t.new and it.attach -%}
+      {%- set y = t.new[0] -%}
+      {%- set t.new = [{'w': y.w, 'r': y.r, 'a': true, 'spell': y.spell, 'ps': y.ps}] + t.new[1:] -%}
+    {%- endif -%}
+    {%- set it.items = it.items + t.new -%}
+    {%- set it.attach = false -%}
+    {%- set it.fresh = it.fresh and not t.new -%}
+  {%- endif -%}
+{%- endfor -%}
+{#- "Ss!" and "Sh, ss, tsk" are a hiss. Beside an ordinary word the two letters stay letters ("the ss", "mm:ss");
+    quotation marks and brackets do not part them from that word -#}
+{%- for place in it.short -%}
+  {%- set r = namespace(first=place, last=place + 1, sounds=true) -%}
+  {%- for _ in range(place) -%}
+    {%- if r.first > 0 and (it.items[r.first - 1].w is defined or it.items[r.first - 1].p in ['"', "'", '(', ')']) -%}{%- set r.first = r.first - 1 -%}{%- endif -%}
+  {%- endfor -%}
+  {%- for _ in range(it.items | length - place) -%}
+    {%- if r.last < it.items | length and (it.items[r.last].w is defined or it.items[r.last].p in ['"', "'", '(', ')']) -%}{%- set r.last = r.last + 1 -%}{%- endif -%}
+  {%- endfor -%}
+  {%- for x in it.items[r.first:r.last] -%}
+    {%- if x.w is defined and not (x.w in SOUNDS or (x.ps and x.w and x.w | replace(x.w[:1], '') == '')) -%}{%- set r.sounds = false -%}{%- endif -%}
+  {%- endfor -%}
+  {%- if r.sounds -%}
+    {%- set y = it.items[place] -%}
+    {%- set it.items = it.items[:place] + [{'w': y.w, 'r': y.r, 'a': y.a, 'spell': false, 'ps': y.w[:1]}] + it.items[place + 1:] -%}
+  {%- endif -%}
+{%- endfor -%}
+{#- "k" is "okay" as a reply by itself ("k", "k, thanks"); beside a word or a figure it is the letter, and between two
+    commas, or with a single letter next to it across a comma, it is one of a list ("i, j, k") -#}
+{%- for place in it.replies | reverse -%}
+  {%- set r = namespace(reply=true, marks=[]) -%}
+  {%- for step in [-1, 1] -%}
+    {%- set at = namespace(i=place + step) -%}
+    {%- for _ in it.items -%}
+      {%- if at.i >= 0 and at.i < it.items | length and it.items[at.i].p is defined and it.items[at.i].p in ['"', "'", '(', ')'] -%}{%- set at.i = at.i + step -%}{%- endif -%}
+    {%- endfor -%}
+    {%- if at.i < 0 or at.i >= it.items | length -%}{%- set r.marks = r.marks + [''] -%}
+    {%- elif it.items[at.i].w is defined -%}{%- set r.reply = false -%}
+    {%- else -%}
+      {%- set r.marks = r.marks + [it.items[at.i].p] -%}
+      {%- set j = at.i + step -%}
+      {%- if it.items[at.i].p == ',' and j >= 0 and j < it.items | length and it.items[j].w is defined and it.items[j].w | length == 1 and it.items[j].w != 'i' -%}
+        {%- set r.reply = false -%}
+      {%- endif -%}
+    {%- endif -%}
+  {%- endfor -%}
+  {%- if r.reply and r.marks != [',', ','] -%}
+    {%- set y = it.items[place] -%}
+    {%- set said = namespace(new=[]) -%}
+    {%- set chat = tts_dict('chat', y.r | lower) -%}
+    {%- if chat[0] != '-' -%}
+      {%- for part in chat[0].split(' ') -%}
+        {%- set said.new = said.new + [{'w': part[1:] | lower, 'r': y.r, 'a': false, 'spell': true, 'ps': ''} if part.startswith('*')
+                                       else {'w': part | lower, 'r': part, 'a': false, 'spell': false, 'ps': ''}] -%}
+      {%- endfor -%}
+    {%- endif -%}
+    {%- set it.items = it.items[:place] + said.new + it.items[place + 1:] -%}
+  {%- endif -%}
+{%- endfor -%}
+
+{#- ---------- clauses and marks -> symbols; repeated marks are collapsed ---------- -#}
+{#- the text as the homograph rules see it -#}
+{%- set wr = namespace(toks=[]) -%}
+{%- for x in it.items -%}{%- set wr.toks = wr.toks + [x.p if x.p is defined else (x.r if x.r else 'x')] -%}{%- endfor -%}
+{%- set out = namespace(s='', words=[], places=[], last='', phones=false) -%}
+{%- for x in it.items + [{'p': ''}] -%}
+  {%- if x.p is defined -%}
+    {%- if out.words -%}
+      {%- set said = clause(out.words, wr.toks, out.places) -%}
+      {%- set out.words = [] -%}{%- set out.places = [] -%}
+      {%- if said -%}
+        {%- set out.s = out.s ~ (' ' if out.s else '') ~ said -%}
+        {%- set out.last = '' -%}{%- set out.phones = true -%}
+      {%- endif -%}
+    {%- endif -%}
+    {%- if x.p and x.p != out.last -%}
+      {%- set out.s = out.s ~ (' ' if out.s else '') ~ x.p -%}
+      {%- set out.last = x.p -%}
+    {%- endif -%}
+  {%- else -%}
+    {%- set out.words = out.words + [x] -%}{%- set out.places = out.places + [loop.index0] -%}
+  {%- endif -%}
+{%- endfor -%}
+{#- <eos>: the end of an utterance -#}
+{%- if out.s and out.phones -%}{%- set spoken_text.s = spoken_text.s ~ (' ' if spoken_text.s else '') ~ out.s ~ ' <eos>' -%}{%- endif -%}
+{%- endif -%}
+{%- endfor -%}
+{{ spoken_text.s }}
diff --git a/src/llama-arch.cpp b/src/llama-arch.cpp
index 530fc8e12..feee62bd4 100644
--- a/src/llama-arch.cpp
+++ b/src/llama-arch.cpp
@@ -177,6 +177,7 @@ static const std::map<llm_arch, const char *> LLM_ARCH_NAMES = {
     { LLM_ARCH_POCKETTTS,        "pockettts"        },
     { LLM_ARCH_ZONOS3,           "zonos3"           },
     { LLM_ARCH_KOKORO,           "kokoro"           },
+    { LLM_ARCH_TINY_VOCA,        "tiny-voca"        },
     { LLM_ARCH_K2_HORIZON,       "k2-horizon"       },
     { LLM_ARCH_BEAM,             "beam"             },
     { LLM_ARCH_KOLIBRI1,         "kolibri1"         },
diff --git a/src/llama-arch.h b/src/llama-arch.h
index 05a00c3ac..4187a0d08 100644
--- a/src/llama-arch.h
+++ b/src/llama-arch.h
@@ -178,6 +178,7 @@ enum llm_arch {
     LLM_ARCH_POCKETTTS,
     LLM_ARCH_ZONOS3,
     LLM_ARCH_KOKORO,
+    LLM_ARCH_TINY_VOCA,
     LLM_ARCH_MINIMAX_01,
     LLM_ARCH_HRM_TEXT,
     LLM_ARCH_K2_HORIZON,
diff --git a/src/llama-model.cpp b/src/llama-model.cpp
index 9adb96364..728473fff 100644
--- a/src/llama-model.cpp
+++ b/src/llama-model.cpp
@@ -130,6 +130,8 @@ static llama_model * llama_model_mapping(llm_arch arch, const llama_model_params
             return new llama_model_zonos3(params);
         case LLM_ARCH_KOKORO:
             return new llama_model_kokoro(params);
+        case LLM_ARCH_TINY_VOCA:
+            return new llama_model_tiny_voca(params);
         case LLM_ARCH_PHI2:
             return new llama_model_phi2(params);
         case LLM_ARCH_PHI3:
@@ -2990,6 +2992,7 @@ llama_memory_i * llama_model::create_memory(const llama_memory_params & params,
         case LLM_ARCH_RND1:
         case LLM_ARCH_CLEF:
         case LLM_ARCH_KOKORO:
+        case LLM_ARCH_TINY_VOCA:
             {
                 res = nullptr;
             } break;
@@ -3774,6 +3777,7 @@ llama_rope_type llama_model_rope_type(const llama_model * model) {
         case LLM_ARCH_POCKETTTS:
         case LLM_ARCH_ZONOS3:
         case LLM_ARCH_LIMITE:
+        case LLM_ARCH_TINY_VOCA:
             return LLAMA_ROPE_TYPE_NORM;
         case LLM_ARCH_BAILINGMOE3:
             // VL files carry mrope sections; text-only files keep NORM rope
diff --git a/src/models/models.h b/src/models/models.h
index ccbd1be62..8da3bcf29 100644
--- a/src/models/models.h
+++ b/src/models/models.h
@@ -785,6 +785,18 @@ struct llama_model_kokoro : public llama_model_base {
     std::unique_ptr<llm_graph_context> build_arch_graph(const llm_graph_params & params) const override;
 };

+struct llama_model_tiny_voca : public llama_model_base {
+    llama_model_tiny_voca(const struct llama_model_params & params) : llama_model_base(params) {}
+    void load_arch_hparams(llama_model_loader & ml) override;
+    void load_arch_tensors(llama_model_loader & ml) override;
+
+    struct graph : public llm_graph_context {
+        graph(const llm_graph_params & params);
+    };
+
+    std::unique_ptr<llm_graph_context> build_arch_graph(const llm_graph_params & params) const override;
+};
+
 struct llama_model_zonos3 : public llama_model_base {
     llama_model_zonos3(const struct llama_model_params & params) : llama_model_base(params) {}
     void load_arch_hparams(llama_model_loader & ml) override;
diff --git a/src/models/tiny-voca.cpp b/src/models/tiny-voca.cpp
new file mode 100644
index 000000000..c7541f5e9
--- /dev/null
+++ b/src/models/tiny-voca.cpp
@@ -0,0 +1,171 @@
+#include "models.h"
+
+#include <map>
+
+// one transformer stack of the tiny-voca TTS model, bidirectional, no KV cache
+// - the text model is the phoneme encoder, its input rows are built by the audio pipeline (token + stress + voice)
+// - mtmd runs the prosody and acoustic stacks of the mmproj with this arch too
+// block: x + attn(LN(x)), then x + up/down(gelu(dwconv(LN(x)))), LN at the end
+
+void llama_model_tiny_voca::load_arch_hparams(llama_model_loader & ml) {
+    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
+
+    type = LLM_TYPE_UNKNOWN;
+}
+
+void llama_model_tiny_voca::load_arch_tensors(llama_model_loader & ml) {
+    LLAMA_LOAD_LOCALS;
+    GGML_UNUSED(ml);
+
+    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
+
+    output_norm   = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
+    output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"),   {n_embd}, 0);
+
+    // no output head, the logits are unused; reuse the embedding table so a sampler can still run
+    output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
+
+    for (int i = 0; i < n_layer; ++i) {
+        auto & layer = layers[i];
+
+        layer.attn_norm   = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
+        layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i),   {n_embd}, 0);
+
+        create_tensor_qkv(layer, i, n_embd, n_embd, n_embd_gqa, n_embd_gqa, 0);
+
+        layer.wo   = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
+        layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i),   {n_embd}, 0);
+
+        layer.ffn_norm   = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
+        layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i),   {n_embd}, 0);
+
+        // depthwise conv over time, 7 taps
+        layer.ssm_conv1d   = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {7, n_embd}, 0);
+        layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i),   {n_embd}, 0);
+
+        layer.ffn_up     = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd, n_ff}, 0);
+        layer.ffn_up_b   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "bias", i),   {n_ff}, 0);
+        layer.ffn_down   = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
+        layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i),   {n_embd}, 0);
+    }
+}
+
+// row of each tap of the depthwise conv: for token t and tap k, the token of the same sequence at pos(t) + k - pad,
+// or the zero row n_tokens past the edges, so sequences that share a ubatch do not mix
+class llm_graph_input_tiny_voca_conv : public llm_graph_input_i {
+public:
+    llm_graph_input_tiny_voca_conv(int d_conv) : d_conv(d_conv) {}
+    virtual ~llm_graph_input_tiny_voca_conv() = default;
+
+    void set_input(const llama_ubatch * ubatch) override {
+        const int64_t n_tokens = ubatch->n_tokens;
+        const int     pad      = (d_conv - 1) / 2;
+        GGML_ASSERT(ubatch->pos && ubatch->seq_id);
+        std::map<std::pair<llama_seq_id, llama_pos>, int32_t> row_of;
+        for (int64_t i = 0; i < n_tokens; ++i) {
+            row_of[{ ubatch->seq_id[i][0], ubatch->pos[i] }] = (int32_t) i;
+        }
+        std::vector<int32_t> rows((size_t) n_tokens * d_conv);
+        for (int64_t i = 0; i < n_tokens; ++i) {
+            for (int k = 0; k < d_conv; ++k) {
+                const auto it = row_of.find({ ubatch->seq_id[i][0], ubatch->pos[i] + k - pad });
+                rows[i * d_conv + k] = it == row_of.end() ? (int32_t) n_tokens : it->second;
+            }
+        }
+        ggml_backend_tensor_set(idx, rows.data(), 0, ggml_nbytes(idx));
+    }
+
+    ggml_tensor * idx = nullptr; // I32 [d_conv * n_tokens]
+    const int d_conv;
+};
+
+std::unique_ptr<llm_graph_context> llama_model_tiny_voca::build_arch_graph(const llm_graph_params & params) const {
+    return std::make_unique<graph>(params);
+}
+
+llama_model_tiny_voca::graph::graph(const llm_graph_params & params) : llm_graph_context(params) {
+    const int64_t n_embd_head = hparams.n_embd_head_v();
+
+    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
+    ggml_tensor * cur;
+    ggml_tensor * inpL = build_inp_embd(model.tok_embd);
+
+    ggml_tensor * inp_pos = build_inp_pos();
+
+    auto * inp_attn = build_attn_inp_no_cache();
+
+    ggml_tensor * inp_out_ids = build_inp_out_ids();
+
+    const int d_conv = (int) model.layers[0].ssm_conv1d->ne[0];
+    auto inp_conv = std::make_unique<llm_graph_input_tiny_voca_conv>(d_conv);
+    inp_conv->idx = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, (int64_t) d_conv * n_tokens);
+    ggml_set_input(inp_conv->idx);
+    ggml_tensor * conv_idx = inp_conv->idx;
+    res->add_input(std::move(inp_conv));
+
+    for (int il = 0; il < n_layer; ++il) {
+        const auto & layer = model.layers[il];
+
+        cur = build_norm(inpL, layer.attn_norm, layer.attn_norm_b, LLM_NORM, il);
+        cb(cur, "attn_norm", il);
+
+        {
+            auto [Qcur, Kcur, Vcur] = build_qkv(layer, cur, n_embd_head, n_head, n_head_kv, il);
+
+            Qcur = build_rope(Qcur, inp_pos, il);
+            Kcur = build_rope(Kcur, inp_pos, il);
+
+            cb(Qcur, "Qcur", il);
+            cb(Kcur, "Kcur", il);
+            cb(Vcur, "Vcur", il);
+
+            cur = build_attn(inp_attn,
+                    layer.wo, layer.wo_b, layer.wo_s,
+                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
+            cb(cur, "kqv_out", il);
+        }
+
+        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
+        cb(ffn_inp, "ffn_inp", il);
+
+        cur = build_norm(ffn_inp, layer.ffn_norm, layer.ffn_norm_b, LLM_NORM, il);
+        cb(cur, "ffn_norm", il);
+
+        // same-length depthwise conv over the positions of each sequence: gather the taps, weight them, sum
+        {
+            ggml_tensor * x = ggml_pad(ctx0, cur, 0, 1, 0, 0);                    // [n_embd, n_tokens + 1], last row 0
+            x = ggml_get_rows(ctx0, x, conv_idx);                                 // [n_embd, d_conv * n_tokens]
+            x = ggml_reshape_3d(ctx0, x, n_embd, d_conv, n_tokens);
+            ggml_tensor * w = ggml_cont(ctx0, ggml_transpose(ctx0, layer.ssm_conv1d)); // [n_embd, d_conv]
+            x = ggml_mul(ctx0, x, ggml_reshape_3d(ctx0, w, n_embd, d_conv, 1));
+            x = ggml_sum_rows(ctx0, ggml_cont(ctx0, ggml_permute(ctx0, x, 1, 0, 2, 3))); // [1, n_embd, n_tokens]
+            cur = ggml_add(ctx0, ggml_reshape_2d(ctx0, x, n_embd, n_tokens), layer.ssm_conv1d_b);
+            cb(cur, "dwconv", il);
+        }
+
+        cur = build_ffn(cur,
+                layer.ffn_up,   layer.ffn_up_b,   NULL,
+                NULL,           NULL,             NULL,
+                layer.ffn_down, layer.ffn_down_b, NULL, NULL,
+                LLM_FFN_GELU_ERF, LLM_FFN_SEQ, il);
+        cb(cur, "ffn_out", il);
+
+        cur = ggml_add(ctx0, cur, ffn_inp);
+
+        inpL = cur;
+    }
+
+    cur = build_norm(inpL, model.output_norm, model.output_norm_b, LLM_NORM, -1);
+
+    if (inp_out_ids) {
+        cur = ggml_get_rows(ctx0, cur, inp_out_ids);
+    }
+
+    cb(cur, "result_embd", -1);
+    res->t_embd = cur;
+
+    cur = build_lora_mm(model.output, cur);
+
+    cb(cur, "result_output", -1);
+    res->t_logits = cur;
+}
diff --git a/tests/test-llama-archs.cpp b/tests/test-llama-archs.cpp
index 92123211a..a2cba05aa 100644
--- a/tests/test-llama-archs.cpp
+++ b/tests/test-llama-archs.cpp
@@ -1184,7 +1184,7 @@ static bool arch_supported(const llm_arch arch) {
             arch == LLM_ARCH_NEO_BERT || arch == LLM_ARCH_JINA_BERT_V2 || arch == LLM_ARCH_JINA_BERT_V3 || arch == LLM_ARCH_EUROBERT) {
         return false; // TODO vocab
     }
-    if (arch == LLM_ARCH_KOKORO) {
+    if (arch == LLM_ARCH_KOKORO || arch == LLM_ARCH_TINY_VOCA) {
         return false; // TODO encoder without memory, a sequence must fit in one ubatch
     }
     if (arch == LLM_ARCH_PLM) {
diff --git a/tools/mtmd/CMakeLists.txt b/tools/mtmd/CMakeLists.txt
index de8c473dd..25a5549c4 100644
--- a/tools/mtmd/CMakeLists.txt
+++ b/tools/mtmd/CMakeLists.txt
@@ -96,6 +96,7 @@ add_library(mtmd
             models/pockettts-gen.cpp
             models/soprano.cpp
             models/kokoro.cpp
+            models/tiny-voca.cpp
             models/step3vl.cpp
             models/siglip.cpp
             models/whisper-enc.cpp
diff --git a/tools/mtmd/models/models.h b/tools/mtmd/models/models.h
index 802561765..e5e568b1b 100644
--- a/tools/mtmd/models/models.h
+++ b/tools/mtmd/models/models.h
@@ -780,6 +780,52 @@ struct mtmd_graph_music3_gen : mtmd_graph_snake_vae {
     ggml_cgraph * build_vocoder();
 };

+// tiny-voca: small graphs around the stacks, the stacks run on libllama (see mtmd_gen_wav_tiny_voca())
+// LATENT_IN:  token states [n_embd, n_rows] -> duration outputs [2, n_rows]
+// LATENT_OUT: acoustic states [n_embd, n_rows] -> source-filter params [n_voc, n_rows]
+struct mtmd_graph_tiny_voca : mtmd_graph {
+    mtmd_graph_tiny_voca(mtmd_subcontext * ctx, const mtmd_image_f32 & img, mtmd_subctx_gen_process gen_process, int n_rows)
+        : mtmd_graph(ctx, img), gen_process(gen_process), n_rows(n_rows) {}
+    ggml_cgraph * build() override;
+
+    static constexpr int   n_prosody_layer  = 2;
+    static constexpr int   n_acoustic_layer = 5;
+    static constexpr int   n_max_frames     = 3000;
+    static constexpr int   hop              = 256;  // samples per frame
+    static constexpr int   n_cep_causal     = 80;
+    static constexpr int   n_cep_anti       = 16;
+    static constexpr int   n_noise_cep      = 32;
+    static constexpr int   harm_fft         = 512;
+    static constexpr int   harm_hop         = 128;
+    static constexpr int   harm_steps       = hop / harm_hop;
+    static constexpr int   noise_fft        = 256;
+    static constexpr int   noise_hop        = 64;
+    static constexpr int   noise_steps      = hop / noise_hop;
+    static constexpr int   n_voc            = n_cep_causal + n_cep_anti + harm_steps + n_noise_cep + noise_steps;
+    static constexpr float rope_theta       = 10000.0f;
+    static constexpr float sample_rate      = 24000.0f;
+    static constexpr float harmonic_limit   = 0.47f;  // highest harmonic, relative to the sample rate
+    static constexpr float f0_ref           = 200.0f; // Hz, log F0 is predicted relative to it
+    static constexpr float f0_min           = 50.0f;
+    static constexpr float f0_max           = 600.0f;
+    static constexpr float max_dur          = 400.0f; // frames per token before the speed
+
+    mtmd_subctx_gen_process gen_process;
+    int n_rows;
+
+    // host: frame params [n_frames, n_voc] and F0 [n_frames] in Hz -> waveform [n_frames * hop]
+    static void synth(const std::vector<float> & voc, const std::vector<float> & f0, std::mt19937 & rng, std::vector<float> & audio);
+
+    // unknown-word model: letters (a-z and ') -> CMUdict phonemes separated by spaces, other characters are skipped
+    static constexpr int n_g2p_slot   = 2;  // positions per letter
+    static constexpr int n_g2p_letter = 27;
+    static constexpr int n_g2p_symbol = 69;
+    static std::string g2p(const mtmd_tiny_voca & tv, const std::string & word);
+
+  private:
+    ggml_tensor * conv3(ggml_tensor * x, ggml_tensor * w, ggml_tensor * b) const;
+};
+
 struct mtmd_graph_soprano : mtmd_graph {
     mtmd_graph_soprano(mtmd_subcontext * ctx, const mtmd_image_f32 & img, int n_frames) : mtmd_graph(ctx, img), n_frames(n_frames) {}
     ggml_cgraph * build() override;
diff --git a/tools/mtmd/models/tiny-voca.cpp b/tools/mtmd/models/tiny-voca.cpp
new file mode 100644
index 000000000..ab155b93f
--- /dev/null
+++ b/tools/mtmd/models/tiny-voca.cpp
@@ -0,0 +1,307 @@
+#include "models.h"
+#include "../mtmd-audio.h"
+
+#include <algorithm>
+#include <cctype>
+#include <cmath>
+#include <cstring>
+#include <string>
+
+// same-length conv with 3 taps; x: [T, IC], w: [3, IC, OC], returns [T, OC]
+ggml_tensor * mtmd_graph_tiny_voca::conv3(ggml_tensor * x, ggml_tensor * w, ggml_tensor * b) const {
+    ggml_tensor * col = ggml_im2col(ctx0, w, x, 1, 0, 1, 0, 1, 0, false, GGML_TYPE_F32);
+    ggml_tensor * y = ggml_mul_mat(ctx0,
+            ggml_reshape_2d(ctx0, col, col->ne[0], col->ne[1] * col->ne[2]),
+            ggml_reshape_2d(ctx0, w, w->ne[0] * w->ne[1], w->ne[2]));
+    return ggml_add(ctx0, y, ggml_reshape_2d(ctx0, b, 1, b->ne[0]));
+}
+
+ggml_cgraph * mtmd_graph_tiny_voca::build() {
+    const auto & tv = model.tiny_voca;
+
+    ggml_tensor * inp = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, hparams.n_embd, n_rows);
+    ggml_set_name(inp, "inp_feats");
+    ggml_set_input(inp);
+
+    ggml_tensor * cur;
+    if (gen_process == MTMD_SUBCTX_GEN_PROCESS_LATENT_IN) {
+        // duration predictor: (conv, gelu, layer norm) x 2, then a linear layer
+        cur = ggml_cont(ctx0, ggml_transpose(ctx0, inp));
+        cur = conv3(cur, tv.dur_conv1_w, tv.dur_conv1_b);
+        cur = ggml_gelu_erf(ctx0, ggml_cont(ctx0, ggml_transpose(ctx0, cur)));
+        cur = build_norm(cur, tv.dur_norm1_w, tv.dur_norm1_b, NORM_TYPE_NORMAL, eps, -1);
+        cur = conv3(ggml_cont(ctx0, ggml_transpose(ctx0, cur)), tv.dur_conv2_w, tv.dur_conv2_b);
+        cur = ggml_gelu_erf(ctx0, ggml_cont(ctx0, ggml_transpose(ctx0, cur)));
+        cur = build_norm(cur, tv.dur_norm2_w, tv.dur_norm2_b, NORM_TYPE_NORMAL, eps, -1);
+        cur = ggml_add(ctx0, build_mm(tv.dur_out_w, cur), tv.dur_out_b);
+    } else {
+        GGML_ASSERT(gen_process == MTMD_SUBCTX_GEN_PROCESS_LATENT_OUT);
+        ggml_tensor * h = ggml_add(ctx0, build_mm(tv.harm_w,  inp), tv.harm_b);
+        ggml_tensor * n = ggml_add(ctx0, build_mm(tv.noise_w, inp), tv.noise_b);
+        cur = ggml_concat(ctx0, h, n, 0);
+    }
+    ggml_set_name(cur, "out_feats");
+    ggml_set_output(cur);
+    ggml_build_forward_expand(gf, cur);
+    return gf;
+}
+
+//
+// host: harmonic plus noise synthesis, see SourceFilter in the reference (model.py)
+//
+
+// band-limited pulse train with unit power, F0 given per frame and interpolated in the log domain
+static void tiny_voca_pulse_train(const std::vector<float> & f0, std::vector<float> & out) {
+    using g = mtmd_graph_tiny_voca;
+    const int64_t n_frames = (int64_t) f0.size();
+    std::vector<double> knots(n_frames + 1);
+    for (int64_t i = 0; i < n_frames; i++) {
+        knots[i] = std::log(std::max((double) f0[i], 1.0));
+    }
+    knots[n_frames] = knots[n_frames - 1];
+
+    out.resize((size_t) (n_frames * g::hop));
+    double acc = 0.0;
+    for (int64_t n = 0; n < n_frames * g::hop; n++) {
+        const int64_t low = n / g::hop;
+        const double  w   = (double) (n - low * g::hop) / g::hop;
+        const double  freq = std::exp(knots[low] * (1.0 - w) + knots[low + 1] * w);
+        acc += freq / g::sample_rate;
+        const double theta = std::fmod(2.0 * M_PI * acc, 2.0 * M_PI);
+        const double harmonics = std::max((double) g::harmonic_limit * g::sample_rate / freq, 1.0);
+        const double whole = std::floor(harmonics);
+        const double denom = 2.0 * std::sin(theta / 2.0);
+        const double core  = std::fabs(denom) < 1e-7 ? whole : std::sin((whole + 0.5) * theta) / denom - 0.5;
+        const double wave  = core + (harmonics - whole) * std::cos((whole + 1.0) * theta);
+        out[n] = (float) (wave * std::sqrt(2.0 / harmonics));
+    }
+}
+
+// time-varying filter: STFT of the source (center, zero padding), times exp(rfft(cepstrum)) of each frame, ISTFT
+// cep(j, buf) writes the n_fft cepstrum of STFT frame j, the source gets n_fft / 2 trailing zeros first
+static void tiny_voca_filter(const std::vector<float> & src, int n_fft, int hop,
+        const std::function<void(int64_t, float *)> & cep, std::vector<float> & out) {
+    const int     n_bins = n_fft / 2 + 1;
+    const int64_t n_src  = (int64_t) src.size();
+    const int64_t n_pad  = n_src + n_fft / 2;      // length given to the STFT
+    const int64_t n_stft = 1 + n_pad / hop;        // frames, center = true
+    const int64_t n_full = n_pad + n_fft;          // with the center padding
+
+    mtmd_audio_cache cache;
+    cache.fill_sin_cos_table(n_fft);
+    cache.fill_hann_window(n_fft, true);
+    const float * win = cache.hann_window.data();
+
+    std::vector<float> x((size_t) n_full, 0.0f);
+    std::copy(src.begin(), src.end(), x.begin() + n_fft / 2);
+
+    std::vector<float> ola((size_t) n_full, 0.0f), env((size_t) n_full, 0.0f);
+    std::vector<float> fin(8 * n_fft), fout(8 * n_fft), rin(8 * n_fft), rout(8 * n_fft), cbuf(n_fft);
+    std::vector<float> resp(2 * n_bins);
+    for (int64_t j = 0; j < n_stft; j++) {
+        // response of this frame
+        std::fill(cbuf.begin(), cbuf.end(), 0.0f);
+        cep(j, cbuf.data());
+        std::copy(cbuf.begin(), cbuf.end(), rin.begin());
+        mtmd_audio_fft(cache, rin.data(), n_fft, rout.data());
+        for (int k = 0; k < n_bins; k++) {
+            const float mag = std::exp(std::clamp(rout[2 * k], -14.0f, 6.0f));
+            resp[2 * k + 0] = mag * std::cos(rout[2 * k + 1]);
+            resp[2 * k + 1] = mag * std::sin(rout[2 * k + 1]);
+        }
+
+        // spectrum of the source frame
+        const float * seg = x.data() + j * hop;
+        for (int i = 0; i < n_fft; i++) {
+            fin[i] = seg[i] * win[i];
+        }
+        mtmd_audio_fft(cache, fin.data(), n_fft, fout.data());
+
+        // filtered spectrum, mirrored for the complex inverse FFT; irfft ignores the imaginary parts of DC and Nyquist
+        for (int k = 0; k < n_bins; k++) {
+            const float a = fout[2 * k], b = fout[2 * k + 1];
+            const float c = resp[2 * k], d = resp[2 * k + 1];
+            rin[2 * k + 0] = a * c - b * d;
+            rin[2 * k + 1] = (k == 0 || k == n_fft / 2) ? 0.0f : a * d + b * c;
+        }
+        for (int k = 1; k < n_fft - n_bins + 1; k++) {
+            rin[2 * (n_fft - k) + 0] =  rin[2 * k + 0];
+            rin[2 * (n_fft - k) + 1] = -rin[2 * k + 1];
+        }
+        mtmd_audio_ifft(cache, rin.data(), n_fft, rout.data());
+
+        float * o = ola.data() + j * hop;
+        float * e = env.data() + j * hop;
+        for (int i = 0; i < n_fft; i++) {
+            o[i] += rout[2 * i] * win[i];
+            e[i] += win[i] * win[i];
+        }
+    }
+
+    out.resize((size_t) n_src);
+    for (int64_t i = 0; i < n_src; i++) {
+        const int64_t k = i + n_fft / 2; // drop the center padding
+        out[i] = env[k] > 1e-11f ? ola[k] / env[k] : 0.0f;
+    }
+}
+
+void mtmd_graph_tiny_voca::synth(const std::vector<float> & voc, const std::vector<float> & f0, std::mt19937 & rng, std::vector<float> & audio) {
+    const int64_t n_frames = (int64_t) f0.size();
+    GGML_ASSERT(n_frames > 0 && voc.size() == (size_t) (n_frames * n_voc));
+
+    // liftering: coefficient i scaled by 1 / (1 + i / 4), the gain (coefficient 0) by 2
+    auto lifter = [](int i, int first) { return i == 0 && first == 0 ? 2.0f : 1.0f / (1.0f + (float) (i + first) / 4.0f); };
+
+    // harmonic branch: envelope interpolated between frames, one gain per sub-frame
+    const int split = n_cep_causal + n_cep_anti;
+    auto harm_cep = [&](int64_t j, float * buf) {
+        const int64_t s    = std::min(j, harm_steps * n_frames - 1);
+        const float * here = voc.data() + (s / harm_steps) * n_voc;
+        const float * next = voc.data() + std::min((s + 1) / harm_steps, n_frames - 1) * n_voc;
+        for (int i = 0; i < n_cep_causal; i++) {
+            buf[i] = 0.5f * (here[i] + next[i]) * lifter(i, 0);
+        }
+        buf[0] += here[split + s % harm_steps];
+        for (int i = 0; i < n_cep_anti; i++) {
+            buf[harm_fft - 1 - i] = 0.5f * (here[n_cep_causal + i] + next[n_cep_causal + i]) * lifter(i, 1);
+        }
+    };
+    // noise branch: one envelope per frame, one gain per sub-frame
+    auto noise_cep = [&](int64_t j, float * buf) {
+        const int64_t s   = std::min(j, noise_steps * n_frames - 1);
+        const float * row = voc.data() + (s / noise_steps) * n_voc + split + harm_steps;
+        for (int i = 0; i < n_noise_cep; i++) {
+            buf[i] = row[i] * lifter(i, 0);
+        }
+        buf[0] += row[n_noise_cep + s % noise_steps];
+    };
+
+    std::vector<float> pulses, noise((size_t) (n_frames * hop)), harm_out, noise_out;
+    tiny_voca_pulse_train(f0, pulses);
+    std::normal_distribution<float> dist(0.0f, 1.0f);
+    for (auto & v : noise) {
+        v = dist(rng);
+    }
+    tiny_voca_filter(pulses, harm_fft,  harm_hop,  harm_cep,  harm_out);
+    tiny_voca_filter(noise,  noise_fft, noise_hop, noise_cep, noise_out);
+
+    audio.resize(pulses.size());
+    for (size_t i = 0; i < audio.size(); i++) {
+        audio[i] = harm_out[i] + noise_out[i];
+    }
+}
+
+//
+// host: unknown-word model of the phonemizer, see g2p.py of dizione
+//
+
+static const char * const tiny_voca_g2p_letters = "abcdefghijklmnopqrstuvwxyz'";
+static const char * const tiny_voca_g2p_symbols[mtmd_graph_tiny_voca::n_g2p_symbol] = {
+    "AA0", "AA1", "AA2", "AE0", "AE1", "AE2", "AH0", "AH1", "AH2", "AO0", "AO1", "AO2", "AW0", "AW1", "AW2", "AY0", "AY1", "AY2",
+    "B", "CH", "D", "DH", "EH0", "EH1", "EH2", "ER0", "ER1", "ER2", "EY0", "EY1", "EY2", "F", "G", "HH",
+    "IH0", "IH1", "IH2", "IY0", "IY1", "IY2", "JH", "K", "L", "M", "N", "NG", "OW0", "OW1", "OW2", "OY0", "OY1", "OY2",
+    "P", "R", "S", "SH", "T", "TH", "UH0", "UH1", "UH2", "UW0", "UW1", "UW2", "V", "W", "Y", "Z", "ZH",
+};
+
+// one direction of a GRU layer over x [n, n_in] -> y [n, n_hidden], written at column offset of rows of width stride
+// the products are summed in double and rounded once, as close as can be to the reference (numpy, float32): two
+// symbols are sometimes almost as likely, and the order of a float32 sum would decide between them
+static void tiny_voca_gru(const mtmd_tiny_voca::g2p_gru & g, const std::vector<float> & x, int64_t n, int64_t n_in, int64_t n_hidden,
+        bool reverse, std::vector<float> & y, int64_t offset, int64_t stride) {
+    const int64_t h3 = 3 * n_hidden;
+    std::vector<float> inp((size_t) (n * h3));
+    for (int64_t t = 0; t < n; t++) {
+        for (int64_t r = 0; r < h3; r++) {
+            const float * w = g.w_ih.data() + r * n_in;
+            const float * xt = x.data() + t * n_in;
+            double acc = 0.0;
+            for (int64_t c = 0; c < n_in; c++) {
+                acc += (double) w[c] * xt[c];
+            }
+            inp[t * h3 + r] = (float) acc + g.b_ih[r];
+        }
+    }
+    std::vector<float> state((size_t) n_hidden, 0.0f), rec((size_t) h3);
+    for (int64_t s = 0; s < n; s++) {
+        const int64_t t = reverse ? n - 1 - s : s;
+        for (int64_t r = 0; r < h3; r++) {
+            const float * w = g.w_hh.data() + r * n_hidden;
+            double acc = 0.0;
+            for (int64_t c = 0; c < n_hidden; c++) {
+                acc += (double) w[c] * state[c];
+            }
+            rec[r] = (float) acc + g.b_hh[r];
+        }
+        const float * in = inp.data() + t * h3;
+        for (int64_t k = 0; k < n_hidden; k++) {
+            const float reset  = 1.0f / (1.0f + std::exp(-(in[k] + rec[k])));
+            const float update = 1.0f / (1.0f + std::exp(-(in[n_hidden + k] + rec[n_hidden + k])));
+            const float cand   = std::tanh(in[2 * n_hidden + k] + reset * rec[2 * n_hidden + k]);
+            state[k] = (1.0f - update) * cand + update * state[k];
+        }
+        std::copy(state.begin(), state.end(), y.begin() + t * stride + offset);
+    }
+}
+
+std::string mtmd_graph_tiny_voca::g2p(const mtmd_tiny_voca & tv, const std::string & word) {
+    const int64_t w = tv.g2p_n_width;
+    const int64_t h = tv.g2p_n_hidden;
+    if (w == 0 || tv.g2p_layers.empty()) {
+        return "";
+    }
+    std::vector<int> ids;
+    for (char c : word) {
+        const char * p = strchr(tiny_voca_g2p_letters, std::tolower((unsigned char) c));
+        if (c != '\0' && p) {
+            ids.push_back((int) (p - tiny_voca_g2p_letters) + 1);
+        }
+    }
+    if (ids.empty()) {
+        return "";
+    }
+
+    // each letter takes n_g2p_slot positions: its embedding plus the embedding of the slot
+    const int64_t n = (int64_t) ids.size() * n_g2p_slot;
+    std::vector<float> x((size_t) (n * w));
+    for (size_t l = 0; l < ids.size(); l++) {
+        for (int s = 0; s < n_g2p_slot; s++) {
+            float * row = x.data() + ((int64_t) l * n_g2p_slot + s) * w;
+            for (int64_t c = 0; c < w; c++) {
+                row[c] = tv.g2p_embd[ids[l] * w + c] + tv.g2p_slot[s * w + c];
+            }
+        }
+    }
+    int64_t n_in = w;
+    for (size_t il = 0; il + 1 < tv.g2p_layers.size(); il += 2) {
+        std::vector<float> y((size_t) (n * 2 * h));
+        tiny_voca_gru(tv.g2p_layers[il],     x, n, n_in, h, false, y, 0, 2 * h);
+        tiny_voca_gru(tv.g2p_layers[il + 1], x, n, n_in, h, true,  y, h, 2 * h);
+        x    = std::move(y);
+        n_in = 2 * h;
+    }
+
+    // CTC, read greedily: a label repeated or the blank (0) is no new symbol
+    std::string out;
+    int prev = 0;
+    for (int64_t t = 0; t < n; t++) {
+        int best = 0;
+        float best_v = -INFINITY;
+        for (int k = 0; k <= n_g2p_symbol; k++) {
+            const float * wr = tv.g2p_out_w.data() + k * n_in;
+            double sum = 0.0;
+            for (int64_t c = 0; c < n_in; c++) {
+                sum += (double) wr[c] * x[t * n_in + c];
+            }
+            const float acc = (float) sum + tv.g2p_out_b[k];
+            if (acc > best_v) {
+                best_v = acc;
+                best   = k;
+            }
+        }
+        if (best != prev && best != 0) {
+            out += (out.empty() ? "" : " ") + std::string(tiny_voca_g2p_symbols[best - 1]);
+        }
+        prev = best;
+    }
+    return out;
+}
diff --git a/tools/mtmd/mtmd-audio.cpp b/tools/mtmd/mtmd-audio.cpp
index 419bbede1..638648ab9 100644
--- a/tools/mtmd/mtmd-audio.cpp
+++ b/tools/mtmd/mtmd-audio.cpp
@@ -270,6 +270,14 @@ static void ifft(const mtmd_audio_cache & cache, float * in, int N, float * out)
     fft_impl<true, false>(cache, in, N, out);
 }

+void mtmd_audio_fft(const mtmd_audio_cache & cache, float * in, int N, float * out) {
+    fft(cache, in, N, out);
+}
+
+void mtmd_audio_ifft(const mtmd_audio_cache & cache, float * in, int N, float * out) {
+    ifft(cache, in, N, out);
+}
+
 static void mtmd_audio_fill_entries(std::vector<mtmd_audio_mel> & mels, bool shape_only, std::vector<mtmd_image_f32> & entries) {
     entries.clear();
     for (auto & mel : mels) {
diff --git a/tools/mtmd/mtmd-audio.h b/tools/mtmd/mtmd-audio.h
index 893739d93..117d2bb77 100644
--- a/tools/mtmd/mtmd-audio.h
+++ b/tools/mtmd/mtmd-audio.h
@@ -52,6 +52,11 @@ struct mtmd_audio_cache {
     );
 };

+// FFT of N real samples -> N complex values (interleaved), in needs 2 * N floats and out 8 * N floats (scratch)
+void mtmd_audio_fft(const mtmd_audio_cache & cache, float * in, int N, float * out);
+// inverse FFT of N complex values (interleaved), scaled by 1 / N; in and out need 8 * N floats (scratch)
+void mtmd_audio_ifft(const mtmd_audio_cache & cache, float * in, int N, float * out);
+
 // compute one log10-mel frame from n_fft windowed samples, cache must be initialized for n_fft
 void mtmd_audio_log_mel_frame(const mtmd_audio_cache & cache, const float * samples, int n_fft, int n_mel, float * out);

diff --git a/tools/mtmd/mtmd-graph.cpp b/tools/mtmd/mtmd-graph.cpp
index c57549d6e..24469c14e 100644
--- a/tools/mtmd/mtmd-graph.cpp
+++ b/tools/mtmd/mtmd-graph.cpp
@@ -1104,6 +1104,17 @@ std::unique_ptr<mtmd_graph> mtmd_get_graph_builder(mtmd_subcontext * ctx, const
                 builder = io ? std::make_unique<mtmd_graph_kokoro>(ctx, img, (mtmd_graph_kokoro::stage_type) io->stage, io->n_tok, io->n_frames, io->n_har)
                              : std::make_unique<mtmd_graph_kokoro>(ctx, img, mtmd_graph_kokoro::STAGE_DEC, 0, 2, 241);
             } break;
+        case PROJECTOR_TYPE_TINY_VOCA:
+            {
+                const auto gen_process = params ? params->gen_process : MTMD_SUBCTX_GEN_PROCESS_LATENT_OUT;
+                int n_rows = 1;
+                if (params && params->feats) {
+                    const size_t n_embd = (size_t) ctx->model.hparams.n_embd;
+                    GGML_ASSERT(!params->feats->empty() && params->feats->size() % n_embd == 0);
+                    n_rows = (int) (params->feats->size() / n_embd);
+                }
+                builder = std::make_unique<mtmd_graph_tiny_voca>(ctx, img, gen_process, n_rows);
+            } break;
         case PROJECTOR_TYPE_YOUTUVL:
             {
                 builder = std::make_unique<mtmd_graph_youtuvl>(ctx, img);
diff --git a/tools/mtmd/mtmd-helper-gen.cpp b/tools/mtmd/mtmd-helper-gen.cpp
index bd7ee1885..ef9cfeefb 100644
--- a/tools/mtmd/mtmd-helper-gen.cpp
+++ b/tools/mtmd/mtmd-helper-gen.cpp
@@ -1361,6 +1361,366 @@ private:
     bool sent    = false;
 };

+// tiny-voca is not autoregressive: the backbone encodes the phonemes of one sentence, the mmproj turns its states into audio
+// the prompt is a phoneme string, symbols separated by spaces, a stress mark (ˈ or ˌ) before a stressed vowel
+// each sentence is one utterance, like the reference pipeline: the "tts" template ends each one with <eos>
+// a prompt without <eos> (phonemes given by the user) is cut after each sentence mark, and a missing final mark is added
+class tiny_voca_gen_audio_pipeline : public mtmd_gen_audio_pipeline {
+public:
+    using mtmd_gen_audio_pipeline::mtmd_gen_audio_pipeline;
+
+    // the predicted durations are divided by the speed
+    bool supports_speed() const override { return true; }
+
+    void reset() override {
+        segments.clear();
+        audio.clear();
+        wav.clear();
+        n_encoded = 0;
+        decoded   = false;
+        sent      = false;
+    }
+
+    int32_t set_input(const mtmd_helper_gen_audio_inp * inp) override {
+        reset();
+        if (!inp->prompt || inp->prompt_len == 0 || inp->seq_id < 0 || (uint32_t) inp->seq_id >= llama_n_seq_max(lctx)) {
+            LOG_ERR("mtmd_helper_gen_audio: tiny-voca requires a prompt\n");
+            return 1;
+        }
+        if (inp->speaker_ref && !mtmd_bitmap_is_voice(inp->speaker_ref)) {
+            LOG_ERR("mtmd_helper_gen_audio: tiny-voca has no voice cloning, use one of its voices\n");
+            return 1;
+        }
+        seq_id   = inp->seq_id;
+        out_type = inp->out_type;
+        stream   = inp->stream;
+        speed    = inp->speed;
+
+        if (!load_tables()) {
+            return 1;
+        }
+
+        const char * voice = inp->speaker_ref ? mtmd_bitmap_get_id(inp->speaker_ref) : mtmd_gen_audio_voice_name(mctx, 0);
+        std::vector<float> voice_row(2 * (size_t) n_embd);
+        const std::string name = std::string("voice.") + (voice ? voice : "");
+        if (!voice || mtmd_gen_audio_read_tensor(mctx, name.c_str(), voice_row.data(), voice_row.size()) != voice_row.size()) {
+            LOG_ERR("mtmd_helper_gen_audio: unknown voice \"%s\"\n", voice ? voice : "");
+            return 1;
+        }
+
+        // symbols -> (token, stress); the begin and end tokens are added per sentence
+        std::vector<std::pair<llama_token, int32_t>> phones;
+        explicit_ends = false;
+        const std::string prompt(inp->prompt, inp->prompt_len);
+        size_t n_unknown = 0;
+        for (size_t i = 0; i < prompt.size();) {
+            while (i < prompt.size() && isspace((unsigned char) prompt[i])) {
+                i++;
+            }
+            size_t j = i;
+            while (j < prompt.size() && !isspace((unsigned char) prompt[j])) {
+                j++;
+            }
+            if (j == i) {
+                break;
+            }
+            std::string sym = prompt.substr(i, j - i);
+            i = j;
+            int32_t stress = 0;
+            if (sym.rfind("\xcb\x88", 0) == 0) {        // ˈ primary
+                stress = 1;
+            } else if (sym.rfind("\xcb\x8c", 0) == 0) { // ˌ secondary
+                stress = 2;
+            }
+            if (stress > 0) {
+                sym = sym.substr(2);
+            }
+            if (sym == "<eos>") {
+                // the end of a sentence, as the text was split by the template
+                explicit_ends = true;
+                phones.push_back({ tok_eos, 0 });
+                continue;
+            }
+            if (sym == "<bos>" || sym == "<pad>") {
+                continue;
+            }
+            auto it = token_of.find(sym);
+            if (it == token_of.end()) {
+                n_unknown++;
+                phones.push_back({ tok_unk, 0 });
+                continue;
+            }
+            phones.push_back({ it->second, stress });
+        }
+        if (n_unknown > 0) {
+            LOG_WRN("mtmd_helper_gen_audio: %zu symbols of the prompt are not phonemes of the model, they are read as <unk>\n", n_unknown);
+        }
+
+        // cut after each sentence end, then cut long sentences at weaker breaks
+        const size_t n_max = std::min({ (size_t) llama_n_ctx_seq(lctx), (size_t) llama_n_ubatch(lctx), (size_t) llama_model_n_ctx_train(model) });
+        if (n_max < 8) {
+            LOG_ERR("mtmd_helper_gen_audio: the context is too small\n");
+            return 1;
+        }
+        for (size_t pos = 0; pos < phones.size();) {
+            while (pos < phones.size() && (phones[pos].first == tok_sp || phones[pos].first == tok_eos)) {
+                pos++;
+            }
+            if (pos == phones.size()) {
+                break;
+            }
+            const size_t len = split_len(phones, pos, n_max - 3);
+            add_segment(phones, pos, len, voice_row);
+            pos += len;
+        }
+        if (segments.empty()) {
+            LOG_ERR("mtmd_helper_gen_audio: the prompt has no phoneme\n");
+            return 1;
+        }
+
+        if (!init_gen_state(inp->seed)) {
+            return 1;
+        }
+        return 0;
+    }
+
+    // the encoder is not causal, it takes one whole sentence per call
+    int32_t step_prompt(int32_t) override {
+        if (segments.empty()) {
+            return -1;
+        }
+        if (n_encoded == segments.size()) {
+            return 0;
+        }
+        auto & seg = segments[n_encoded];
+        const int n = (int) seg.tokens.size();
+        decode_embd_batch batch(seg.embd.data(), n, 1, n_embd);
+        batch.set_position_normal(0, seq_id);
+        std::fill(batch.logits.begin(), batch.logits.end(), 1);
+        if (llama_process(lctx, LLAMA_PROCESS_TYPE_DECODE, batch.render(lctx, 0, n)) != 0) {
+            LOG_ERR("mtmd_helper_gen_audio: tiny-voca encoder failed\n");
+            return -1;
+        }
+        for (int i = 0; i < n; ++i) {
+            const float * h = llama_get_embeddings_ith(lctx, i);
+            if (!h) {
+                return -1;
+            }
+            seg.feats.insert(seg.feats.end(), h, h + n_embd);
+        }
+        seg.embd.clear();
+        n_encoded++;
+        return (int32_t) (segments.size() - n_encoded);
+    }
+
+    // nothing is generated step by step
+    int32_t step_gen(llama_token, const float *, const float ** h_state_out, bool * out_stop) override {
+        *h_state_out = nullptr;
+        *out_stop = true;
+        return !segments.empty() && n_encoded == segments.size() ? 0 : 1;
+    }
+
+    int32_t get_output(int32_t * sample_rate, const char ** data, size_t * data_len, int64_t * n_samples) override {
+        *sample_rate = info.sample_rate;
+        *data_len = 0;
+        if (n_samples) { *n_samples = 0; }
+        if (stream) {
+            // the audio comes out in one chunk after flush()
+            if (!decoded || sent) {
+                return 0;
+            }
+            sent = true;
+        } else if (!decode_audio()) {
+            return 1;
+        }
+        if (n_samples) { *n_samples = audio.size(); }
+        if (out_type == MTMD_HELPER_GEN_AUDIO_OUTTYPE_PCM) {
+            *data = (const char *) audio.data();
+            *data_len = audio.size() * sizeof(float);
+        } else {
+            wav.clear();
+            if (!write_wav16(wav, audio, info.sample_rate)) {
+                return 1;
+            }
+            *data = wav.data();
+            *data_len = wav.size();
+        }
+        return 0;
+    }
+
+    int32_t flush() override {
+        return decode_audio() ? 0 : 1;
+    }
+
+private:
+    struct segment {
+        std::vector<llama_token> tokens;
+        std::vector<float> embd;  // encoder input rows
+        std::vector<float> feats; // the voice row, then the encoder state of each token
+    };
+
+    bool load_tables() {
+        if (!token_of.empty()) {
+            return true;
+        }
+        const llama_token n_vocab = llama_vocab_n_tokens(vocab);
+        for (llama_token id = 0; id < n_vocab; id++) {
+            token_of[llama_vocab_get_text(vocab, id)] = id;
+        }
+        auto find = [&](const char * text) {
+            auto it = token_of.find(text);
+            return it == token_of.end() ? LLAMA_TOKEN_NULL : it->second;
+        };
+        tok_bos = find("<bos>");
+        tok_eos = find("<eos>");
+        tok_sp  = find("<sp>");
+        tok_unk = find("<unk>");
+        tok_dot = find(".");
+        tok_emb.resize((size_t) n_vocab * n_embd);
+        str_emb.resize(3 * (size_t) n_embd);
+        if (tok_bos == LLAMA_TOKEN_NULL || tok_eos == LLAMA_TOKEN_NULL || tok_sp == LLAMA_TOKEN_NULL || tok_unk == LLAMA_TOKEN_NULL || tok_dot == LLAMA_TOKEN_NULL ||
+                mtmd_gen_audio_read_tensor(mctx, "a.gen.tok_embd.weight",    tok_emb.data(), tok_emb.size()) != tok_emb.size() ||
+                mtmd_gen_audio_read_tensor(mctx, "a.gen.stress_embd.weight", str_emb.data(), str_emb.size()) != str_emb.size()) {
+            LOG_ERR("mtmd_helper_gen_audio: the backbone and the mmproj are not a tiny-voca pair\n");
+            token_of.clear();
+            return false;
+        }
+        return true;
+    }
+
+    bool is_any(llama_token id, const char * chars) const {
+        const char * text = llama_vocab_get_text(vocab, id);
+        return text[0] != '\0' && strstr(chars, text) != nullptr;
+    }
+
+    // phones of the sentence that starts at pos, cut after a weaker break if it is longer than n_max
+    size_t split_len(const std::vector<std::pair<llama_token, int32_t>> & phones, size_t pos, size_t n_max) const {
+        size_t n_left = phones.size() - pos;
+        if (explicit_ends) {
+            // up to the next <eos>
+            for (size_t len = 0; len < n_left; len++) {
+                if (phones[pos + len].first == tok_eos) {
+                    n_left = len;
+                    break;
+                }
+            }
+        } else {
+            for (size_t len = 1; len <= std::min(n_left, n_max); len++) {
+                if (is_any(phones[pos + len - 1].first, "!.?\xe2\x80\xa6")) {
+                    return len;
+                }
+            }
+        }
+        if (n_left <= n_max) {
+            return n_left;
+        }
+        for (const char * breaks : {":;", ",\xe2\x80\x94"}) {
+            for (size_t len = n_max; len > 1; len--) {
+                if (is_any(phones[pos + len - 1].first, breaks)) {
+                    return len;
+                }
+            }
+        }
+        for (size_t len = n_max; len > 1; len--) {
+            if (phones[pos + len - 1].first == tok_sp) {
+                return len;
+            }
+        }
+        return n_max;
+    }
+
+    // <bos> phones <eos>; without a final mark the last word breaks off, so a full stop is added
+    // (not when the template has split the text: it adds the stop where the reference does)
+    void add_segment(const std::vector<std::pair<llama_token, int32_t>> & phones, size_t pos, size_t len, const std::vector<float> & voice_row) {
+        segment seg;
+        std::vector<int32_t> stress;
+        seg.tokens.push_back(tok_bos);
+        stress.push_back(0);
+        size_t end = pos + len;
+        while (end > pos && phones[end - 1].first == tok_sp) {
+            end--;
+        }
+        for (size_t i = pos; i < end; i++) {
+            seg.tokens.push_back(phones[i].first);
+            stress.push_back(phones[i].second);
+        }
+        const char * last = llama_vocab_get_text(vocab, seg.tokens.back());
+        if (!explicit_ends && (!is_any(seg.tokens.back(), "!.?\xe2\x80\xa6,;:") || last[0] == '\0')) {
+            seg.tokens.push_back(tok_dot);
+            stress.push_back(0);
+        }
+        seg.tokens.push_back(tok_eos);
+        stress.push_back(0);
+
+        seg.embd.resize(seg.tokens.size() * n_embd);
+        for (size_t i = 0; i < seg.tokens.size(); i++) {
+            const float * t = tok_emb.data() + (size_t) seg.tokens[i] * n_embd;
+            const float * s = str_emb.data() + (size_t) stress[i] * n_embd;
+            for (int c = 0; c < n_embd; c++) {
+                seg.embd[i * n_embd + c] = t[c] + s[c] + voice_row[c];
+            }
+        }
+        seg.feats = voice_row;
+        segments.push_back(std::move(seg));
+    }
+
+    bool decode_audio() {
+        if (decoded) {
+            return true;
+        }
+        if (segments.empty() || n_encoded != segments.size()) {
+            return false;
+        }
+        for (const auto & seg : segments) {
+            std::vector<int32_t> codes(seg.tokens.begin(), seg.tokens.end());
+            mtmd_gen_inp inp = mtmd_gen_inp_default(mctx);
+            inp.type    = MTMD_GEN_PROCESS_TYPE_GEN_WAV;
+            inp.codes   = codes.data();
+            inp.n_codes = codes.size();
+            inp.feats   = seg.feats.data();
+            inp.n_feats = seg.feats.size();
+            inp.speed   = speed;
+            mtmd_gen_out out{};
+            if (mtmd_gen_audio_process(mctx, gen_state.get(), &inp, &out) != 0) {
+                return false;
+            }
+            audio.insert(audio.end(), out.audio, out.audio + out.n_samples);
+        }
+        // the model can peak above 1.0, the reference scales the whole text down to a peak of 0.99
+        float peak = 0.0f;
+        for (float v : audio) {
+            peak = std::max(peak, std::fabs(v));
+        }
+        if (peak > 0.99f) {
+            for (float & v : audio) {
+                v *= 0.99f / peak;
+            }
+        }
+        decoded = true;
+        return true;
+    }
+
+    llama_seq_id seq_id = 0;
+    std::unordered_map<std::string, llama_token> token_of;
+    llama_token tok_bos = LLAMA_TOKEN_NULL;
+    llama_token tok_eos = LLAMA_TOKEN_NULL;
+    llama_token tok_sp  = LLAMA_TOKEN_NULL;
+    llama_token tok_unk = LLAMA_TOKEN_NULL;
+    llama_token tok_dot = LLAMA_TOKEN_NULL;
+    bool explicit_ends  = false; // the prompt marks the end of each sentence with <eos>
+    std::vector<float> tok_emb; // [n_vocab, n_embd]
+    std::vector<float> str_emb; // [3, n_embd]
+    std::vector<segment> segments;
+    size_t n_encoded = 0;
+    float speed = 1.0f;
+    std::vector<float> audio;
+    std::vector<char> wav;
+    mtmd_helper_gen_audio_outtype out_type = MTMD_HELPER_GEN_AUDIO_OUTTYPE_WAV;
+    bool stream  = false;
+    bool decoded = false;
+    bool sent    = false;
+};
+
 // Soprano uses the hidden state that predicts each non-EOS token, including the prompt's last state.
 class soprano_gen_audio_pipeline : public mtmd_gen_audio_pipeline {
 public:
@@ -4564,6 +4924,8 @@ static std::unique_ptr<mtmd_gen_audio_pipeline> make_pipeline(llama_context * lc
             return std::unique_ptr<mtmd_gen_audio_pipeline>(new soprano_gen_audio_pipeline(lctx, mctx));
         case MTMD_GEN_AUDIO_TYPE_KOKORO:
             return std::unique_ptr<mtmd_gen_audio_pipeline>(new kokoro_gen_audio_pipeline(lctx, mctx));
+        case MTMD_GEN_AUDIO_TYPE_TINY_VOCA:
+            return std::unique_ptr<mtmd_gen_audio_pipeline>(new tiny_voca_gen_audio_pipeline(lctx, mctx));
         case MTMD_GEN_AUDIO_TYPE_LFM3A:
             return std::unique_ptr<mtmd_gen_audio_pipeline>(new lfm3a_gen_audio_pipeline(lctx, mctx));
         case MTMD_GEN_AUDIO_TYPE_CHATTERBOX:
diff --git a/tools/mtmd/mtmd-impl.h b/tools/mtmd/mtmd-impl.h
index 142b56e8d..c28a13d00 100644
--- a/tools/mtmd/mtmd-impl.h
+++ b/tools/mtmd/mtmd-impl.h
@@ -616,6 +616,7 @@ enum projector_type {
     PROJECTOR_TYPE_POCKETTTS_GEN,
     PROJECTOR_TYPE_SOPRANO,
     PROJECTOR_TYPE_KOKORO,
+    PROJECTOR_TYPE_TINY_VOCA,
     PROJECTOR_TYPE_LFM3A_GEN,
     PROJECTOR_TYPE_DRAMA3_GEN,
     PROJECTOR_TYPE_ZONOS3_GEN,
@@ -713,6 +714,7 @@ static std::map<projector_type, std::string> PROJECTOR_TYPE_NAMES = {
     { PROJECTOR_TYPE_POCKETTTS_GEN,     "pockettts_gen"},
     { PROJECTOR_TYPE_SOPRANO,           "soprano"},
     { PROJECTOR_TYPE_KOKORO,            "kokoro"},
+    { PROJECTOR_TYPE_TINY_VOCA,         "tiny_voca"},
     { PROJECTOR_TYPE_LFM3A_GEN,         "lfm3a_gen"},
     { PROJECTOR_TYPE_DRAMA3_GEN,        "drama3_gen"},
     { PROJECTOR_TYPE_ZONOS3_GEN,        "zonos3_gen"},
diff --git a/tools/mtmd/mtmd-model.cpp b/tools/mtmd/mtmd-model.cpp
index 38992233d..35f9d1c2d 100644
--- a/tools/mtmd/mtmd-model.cpp
+++ b/tools/mtmd/mtmd-model.cpp
@@ -1173,6 +1173,7 @@ struct mtmd_model_loader {
                         hparams.audio_hop_len     = 512;
                     } break;
                 case PROJECTOR_TYPE_KOKORO:
+                case PROJECTOR_TYPE_TINY_VOCA:
                     {
                         hparams.audio_sample_rate = 24000;
                     } break;
@@ -1583,6 +1584,7 @@ struct mtmd_model_loader {
             model.proj_type != PROJECTOR_TYPE_POCKETTTS_GEN &&
             model.proj_type != PROJECTOR_TYPE_SOPRANO &&
             model.proj_type != PROJECTOR_TYPE_KOKORO &&
+            model.proj_type != PROJECTOR_TYPE_TINY_VOCA &&
             model.proj_type != PROJECTOR_TYPE_MUSE_SPARK_AUDIO &&
             model.proj_type != PROJECTOR_TYPE_VIBEVOICE_ASR &&
             model.proj_type != PROJECTOR_TYPE_CHATTERBOX &&
@@ -3479,6 +3481,97 @@ struct mtmd_model_loader {
                     }
                     LOG_INF("%s: %zu predefined voices\n", __func__, model.gen_voices.size());
                 } break;
+            case PROJECTOR_TYPE_TINY_VOCA:
+                {
+                    using g = mtmd_graph_tiny_voca;
+                    auto & tv = model.tiny_voca;
+                    tv.dur_conv1_w = get_tensor("a.gen.dur.conv1.weight");
+                    tv.dur_conv1_b = get_tensor("a.gen.dur.conv1.bias");
+                    tv.dur_norm1_w = get_tensor("a.gen.dur.norm1.weight");
+                    tv.dur_norm1_b = get_tensor("a.gen.dur.norm1.bias");
+                    tv.dur_conv2_w = get_tensor("a.gen.dur.conv2.weight");
+                    tv.dur_conv2_b = get_tensor("a.gen.dur.conv2.bias");
+                    tv.dur_norm2_w = get_tensor("a.gen.dur.norm2.weight");
+                    tv.dur_norm2_b = get_tensor("a.gen.dur.norm2.bias");
+                    tv.dur_out_w   = get_tensor("a.gen.dur.out.weight");
+                    tv.dur_out_b   = get_tensor("a.gen.dur.out.bias");
+                    tv.harm_w      = get_tensor("a.gen.voc.harmonic.weight");
+                    tv.harm_b      = get_tensor("a.gen.voc.harmonic.bias");
+                    tv.noise_w     = get_tensor("a.gen.voc.noise.weight");
+                    tv.noise_b     = get_tensor("a.gen.voc.noise.bias");
+                    if (tv.harm_w->ne[0] != hparams.n_embd || tv.harm_w->ne[1] != g::n_cep_causal + g::n_cep_anti + g::harm_steps ||
+                            tv.noise_w->ne[0] != hparams.n_embd || tv.noise_w->ne[1] != g::n_noise_cep + g::noise_steps ||
+                            tv.dur_out_w->ne[1] != 2) {
+                        throw std::runtime_error(mtmd_format("%s: tiny_voca: unexpected head shape\n", __func__));
+                    }
+
+                    auto read_host = [&](const std::string & name, std::vector<float> & dst, int64_t ne0, int64_t ne1) {
+                        const ggml_tensor * t = ggml_get_tensor(ctx_meta.get(), name.c_str());
+                        if (!t || t->type != GGML_TYPE_F32 || t->ne[0] != ne0 || t->ne[1] != ne1 || ggml_n_dims(t) > 2) {
+                            throw std::runtime_error(mtmd_format("%s: invalid or missing tensor %s\n", __func__, name.c_str()));
+                        }
+                        dst.resize(ggml_nelements(t));
+                        fin.seekg(tensor_offset.at(name), std::ios::beg);
+                        if (!fin.read(reinterpret_cast<char *>(dst.data()), ggml_nbytes(t))) {
+                            throw std::runtime_error(mtmd_format("%s: failed to read %s\n", __func__, name.c_str()));
+                        }
+                    };
+                    const int64_t n_embd = hparams.n_embd;
+                    const ggml_tensor * tok = ggml_get_tensor(ctx_meta.get(), "a.gen.tok_embd.weight");
+                    if (!tok) {
+                        throw std::runtime_error(mtmd_format("%s: tiny_voca: missing token table\n", __func__));
+                    }
+                    read_host("a.gen.token_class",         tv.token_class,    tok->ne[1], 1);
+                    read_host("a.gen.frame_pos.weight",    tv.frame_pos_w,    3, n_embd);
+                    read_host("a.gen.frame_pos.bias",      tv.frame_pos_b,    n_embd, 1);
+                    read_host("a.gen.prosody_head.weight", tv.prosody_head_w, n_embd, 3);
+                    read_host("a.gen.prosody_head.bias",   tv.prosody_head_b, 3, 1);
+                    read_host("a.gen.prosody_proj.weight", tv.prosody_proj_w, 3, n_embd);
+                    read_host("a.gen.prosody_proj.bias",   tv.prosody_proj_b, n_embd, 1);
+
+                    // unknown-word model, optional
+                    if (const ggml_tensor * e = ggml_get_tensor(ctx_meta.get(), "a.gen.g2p.embed.weight")) {
+                        const ggml_tensor * hh = ggml_get_tensor(ctx_meta.get(), "a.gen.g2p.gru.weight_hh_l0");
+                        if (!hh || e->ne[1] != g::n_g2p_letter + 1) {
+                            throw std::runtime_error(mtmd_format("%s: tiny_voca: unexpected g2p model\n", __func__));
+                        }
+                        const int64_t w = e->ne[0];
+                        const int64_t h = hh->ne[0];
+                        tv.g2p_n_width  = w;
+                        tv.g2p_n_hidden = h;
+                        read_host("a.gen.g2p.embed.weight", tv.g2p_embd, w, g::n_g2p_letter + 1);
+                        read_host("a.gen.g2p.slot",         tv.g2p_slot, w, g::n_g2p_slot);
+                        for (int il = 0; ggml_get_tensor(ctx_meta.get(), ("a.gen.g2p.gru.weight_ih_l" + std::to_string(il)).c_str()); il++) {
+                            for (const char * dir : { "", "_reverse" }) {
+                                const std::string sfx = "_l" + std::to_string(il) + dir;
+                                mtmd_tiny_voca::g2p_gru gru;
+                                read_host("a.gen.g2p.gru.weight_ih" + sfx, gru.w_ih, il == 0 ? w : 2 * h, 3 * h);
+                                read_host("a.gen.g2p.gru.weight_hh" + sfx, gru.w_hh, h, 3 * h);
+                                read_host("a.gen.g2p.gru.bias_ih"   + sfx, gru.b_ih, 3 * h, 1);
+                                read_host("a.gen.g2p.gru.bias_hh"   + sfx, gru.b_hh, 3 * h, 1);
+                                tv.g2p_layers.push_back(std::move(gru));
+                            }
+                        }
+                        read_host("a.gen.g2p.out.weight", tv.g2p_out_w, 2 * h, g::n_g2p_symbol + 1);
+                        read_host("a.gen.g2p.out.bias",   tv.g2p_out_b, g::n_g2p_symbol + 1, 1);
+                    }
+
+                    // read by the audio pipeline: input tables and voices (encoder row, then frame row)
+                    for (const char * name : { "a.gen.tok_embd.weight", "a.gen.stress_embd.weight" }) {
+                        model.cbx_tensors[name] = get_tensor(name);
+                    }
+                    for (ggml_tensor * t = ggml_get_first_tensor(ctx_meta.get()); t; t = ggml_get_next_tensor(ctx_meta.get(), t)) {
+                        const std::string name = t->name;
+                        if (name.rfind("voice.", 0) == 0) {
+                            if (t->ne[0] != 2 * n_embd) {
+                                throw std::runtime_error(mtmd_format("%s: tiny_voca: unexpected voice shape\n", __func__));
+                            }
+                            model.cbx_tensors[name] = get_tensor(name);
+                            model.gen_voices[name.substr(6)] = {};
+                        }
+                    }
+                    LOG_INF("%s: %zu predefined voices\n", __func__, model.gen_voices.size());
+                } break;
             case PROJECTOR_TYPE_SOPRANO:
                 {
                     auto & v = model.vocos;
@@ -4976,6 +5069,7 @@ int mtmd_model::n_mmproj_embd() const {
             return cbx_tensors.at("a.cenc.spkr_enc.weight")->ne[1];
         case PROJECTOR_TYPE_SOPRANO:
         case PROJECTOR_TYPE_KOKORO:
+        case PROJECTOR_TYPE_TINY_VOCA:
             return hparams.projection_dim;
         case PROJECTOR_TYPE_PARAKEET:
             return mm_1_w->ne[1];
diff --git a/tools/mtmd/mtmd-model.h b/tools/mtmd/mtmd-model.h
index 636bde14a..2dcf19021 100644
--- a/tools/mtmd/mtmd-model.h
+++ b/tools/mtmd/mtmd-model.h
@@ -945,6 +945,50 @@ struct mtmd_zonos3_wav {
     int64_t rs_kernel_len = 0;
 };

+// tiny-voca: duration predictor and source-filter heads (graphs), the small linear layers between the stacks (host)
+// the prosody and acoustic stacks are loaded by libllama, see mtmd_owned_llama_load()
+struct mtmd_tiny_voca {
+    ggml_tensor * dur_conv1_w = nullptr; // [3, n_embd, n_pred]
+    ggml_tensor * dur_conv1_b = nullptr;
+    ggml_tensor * dur_norm1_w = nullptr;
+    ggml_tensor * dur_norm1_b = nullptr;
+    ggml_tensor * dur_conv2_w = nullptr; // [3, n_pred, n_pred]
+    ggml_tensor * dur_conv2_b = nullptr;
+    ggml_tensor * dur_norm2_w = nullptr;
+    ggml_tensor * dur_norm2_b = nullptr;
+    ggml_tensor * dur_out_w   = nullptr; // [n_pred, 2]: log(1 + frames), pause logit
+    ggml_tensor * dur_out_b   = nullptr;
+    ggml_tensor * harm_w      = nullptr; // [n_embd, n_cep_causal + n_cep_anti + harm_steps]
+    ggml_tensor * harm_b      = nullptr;
+    ggml_tensor * noise_w     = nullptr; // [n_embd, n_noise_cep + noise_steps]
+    ggml_tensor * noise_b     = nullptr;
+
+    // read on the host, row-major as in the reference
+    std::vector<float> token_class;    // [n_vocab]: 0 = pause, 1 = consonant, 2 = vowel
+    std::vector<float> frame_pos_w;    // [n_embd, 3]
+    std::vector<float> frame_pos_b;    // [n_embd]
+    std::vector<float> prosody_head_w; // [3, n_embd]: log(F0 / 200 Hz), voiced logit, energy
+    std::vector<float> prosody_head_b;
+    std::vector<float> prosody_proj_w; // [n_embd, 3]
+    std::vector<float> prosody_proj_b;
+
+    // unknown-word model of the phonemizer, run on the host by mtmd_gen_audio_g2p(): letters -> CMUdict phonemes
+    // embedding (each letter takes n_slot positions), bidirectional GRU layers, CTC output read greedily
+    struct g2p_gru {
+        std::vector<float> w_ih; // [3 * n_hidden, n_in]: reset, update, new
+        std::vector<float> w_hh; // [3 * n_hidden, n_hidden]
+        std::vector<float> b_ih;
+        std::vector<float> b_hh;
+    };
+    std::vector<float> g2p_embd;     // [n_letter + 1, n_width], row 0 unused
+    std::vector<float> g2p_slot;     // [n_slot, n_width]
+    std::vector<g2p_gru> g2p_layers; // forward, reverse, for each layer
+    std::vector<float> g2p_out_w;    // [n_symbol + 1, 2 * n_hidden], output 0 is the CTC blank
+    std::vector<float> g2p_out_b;
+    int64_t g2p_n_width  = 0;
+    int64_t g2p_n_hidden = 0;
+};
+
 // snake-activated conv decoder of the DAC / Oobleck family (a.gen.vae.*): yue2 VAE, minimax_music3 vocoder
 struct mtmd_snake_vae {
     // x + beta * sin(alpha * x)^2, alpha and beta folded at conversion
@@ -1320,6 +1364,7 @@ struct mtmd_model {
     mtmd_yue2 yue2;
     mtmd_music3 music3;
     mtmd_snake_vae snake_vae; // yue2, minimax_music3
+    mtmd_tiny_voca tiny_voca;

     // pocket-tts: SEANet stack, shared by the encoder (speaker path) and the decoder (gen path)
     mtmd_seanet seanet;
diff --git a/tools/mtmd/mtmd-subcontext.cpp b/tools/mtmd/mtmd-subcontext.cpp
index 4e4d418e4..4f3a24037 100644
--- a/tools/mtmd/mtmd-subcontext.cpp
+++ b/tools/mtmd/mtmd-subcontext.cpp
@@ -1596,6 +1596,7 @@ bool mtmd_subcontext::encode(mtmd_subctx_encode_params * params) {
             } break;
         case PROJECTOR_TYPE_SOPRANO:
         case PROJECTOR_TYPE_YUE2_GEN:
+        case PROJECTOR_TYPE_TINY_VOCA:
             {
                 set_input_f32("inp_feats", *params->feats);
             } break;
diff --git a/tools/mtmd/mtmd-subcontext.h b/tools/mtmd/mtmd-subcontext.h
index e92ef01ee..0dfceddd8 100644
--- a/tools/mtmd/mtmd-subcontext.h
+++ b/tools/mtmd/mtmd-subcontext.h
@@ -69,8 +69,8 @@ enum mtmd_subctx_gen_process {
     MTMD_SUBCTX_GEN_PROCESS_TTS,        // full utterance of codes to raw PCM audio
     MTMD_SUBCTX_GEN_PROCESS_TTS_VOCODE, // internal: mel + source stft to istft input
     MTMD_SUBCTX_GEN_PROCESS_TOKENIZE,   // raw PCM audio to semantic speech tokens
-    MTMD_SUBCTX_GEN_PROCESS_LATENT_IN,  // internal: flow latent rows to backbone embd rows
-    MTMD_SUBCTX_GEN_PROCESS_LATENT_OUT, // internal: backbone hidden rows to flow velocity rows
+    MTMD_SUBCTX_GEN_PROCESS_LATENT_IN,  // internal: flow latent rows to backbone embd rows (tiny-voca: token states to durations)
+    MTMD_SUBCTX_GEN_PROCESS_LATENT_OUT, // internal: backbone hidden rows to flow velocity rows (tiny-voca: to source-filter params)
     MTMD_SUBCTX_GEN_PROCESS_FLOW_COND,  // internal: frame rows to flow condition rows
     MTMD_SUBCTX_GEN_PROCESS_FLOW_STEP,  // internal: one velocity pass of the flow transformer
 };
diff --git a/tools/mtmd/mtmd.cpp b/tools/mtmd/mtmd.cpp
index a7bfe8f49..a4fee15f4 100644
--- a/tools/mtmd/mtmd.cpp
+++ b/tools/mtmd/mtmd.cpp
@@ -615,29 +615,39 @@ static bool cbx_is_mtl(const mtmd_model & model) {
     return model.cbx_read_tensor("a.cenc.perceiver.pre_attention_query", nullptr, 0) > 0;
 }

-// yue2: the NAR layers are a qwen3 text model, libllama loads them from a synthetic GGUF
-// the tensor data is read from the mmproj file, mtmd keeps no copy
-struct yue2_nar_source {
+// a stage of the mmproj that is a libllama text model: its layers are stored under a prefix, with the libllama names
+// libllama loads them from a synthetic GGUF: gdst holds the arch hparams, the tensor data is read from the mmproj file
+struct mtmd_owned_llama_source {
     std::ifstream f;
     std::map<std::string, std::pair<size_t, size_t>> tensors; // name in the synthetic model -> offset and size in the mmproj
 };

-static void yue2_nar_set_tensor_data(ggml_tensor * t, void * user_data) {
-    auto * src = (yue2_nar_source *) user_data;
+static void mtmd_owned_llama_set_tensor_data(ggml_tensor * t, void * user_data) {
+    auto * src = (mtmd_owned_llama_source *) user_data;
     std::vector<uint8_t> buf(ggml_nbytes(t), 0);
     const std::string name = t->name;
     auto it = src->tensors.find(name);
     if (it != src->tensors.end()) {
         if (it->second.second != buf.size()) {
-            throw std::runtime_error(mtmd_format("yue2: unexpected size of %s", t->name));
+            throw std::runtime_error(mtmd_format("unexpected size of %s", t->name));
         }
         src->f.seekg((std::streamoff) it->second.first, std::ios::beg);
         if (!src->f.read((char *) buf.data(), (std::streamsize) buf.size())) {
-            throw std::runtime_error(mtmd_format("yue2: failed to read %s", t->name));
+            throw std::runtime_error(mtmd_format("failed to read %s", t->name));
         }
     } else {
         // optional tensors that libllama creates by itself, give them neutral values (scale = 1, dense_2 = identity, others = 0)
-        GGML_ASSERT(t->type == GGML_TYPE_F32);
+        // any other missing tensor is an error: a weight left at 0 gives wrong output without any error
+        // (e.g. a model built from metadata has separate q, k, v and no fused qkv)
+        auto ends_with = [&](const char * suffix) {
+            const size_t n = strlen(suffix);
+            return name.size() >= n && name.compare(name.size() - n, n, suffix) == 0;
+        };
+        const bool optional = ends_with("scale") || ends_with(".bias") || name == "dense_2.weight" ||
+                              name == "token_embd.weight" || name == "output.weight"; // inputs are embedding rows, outputs are hidden states
+        if (!optional || t->type != GGML_TYPE_F32) {
+            throw std::runtime_error(mtmd_format("tensor %s is missing from the mmproj", t->name));
+        }
         float * w = (float *) buf.data();
         if (name.size() >= 5 && name.compare(name.size() - 5, 5, "scale") == 0) {
             std::fill(w, w + ggml_nelements(t), 1.0f);
@@ -651,49 +661,32 @@ static void yue2_nar_set_tensor_data(ggml_tensor * t, void * user_data) {
     ggml_backend_tensor_set(t, buf.data(), 0, buf.size());
 }

-static llama_model * mtmd_yue2_nar_load(const char * fname, ggml_backend_dev_t device, const mtmd_hparams & hparams) {
+// the layers are the tensors "<prefix>blk.*" and "<prefix>output_norm.*" of the mmproj, n_tensors receives their count
+static llama_model * mtmd_owned_llama_load(const char * fname, ggml_backend_dev_t device, gguf_context * gdst, const std::string & prefix, size_t & n_tensors) {
     ggml_context * meta = nullptr;
     gguf_init_params gparams = { /*no_alloc =*/ true, /*ctx =*/ &meta };
     gguf_context_ptr gsrc(gguf_init_from_file(fname, gparams));
     ggml_context_ptr meta_ptr(meta);
     if (!gsrc) {
-        throw std::runtime_error(mtmd_format("yue2: failed to read %s", fname));
+        throw std::runtime_error(mtmd_format("failed to read %s", fname));
     }

-    const llm_arch arch = LLM_ARCH_QWEN3;
-    gguf_context_ptr gdst(gguf_init_empty());
-    auto key = [&](llm_kv kv) { return llama_arch_kv_name(arch, kv); };
-    gguf_set_val_str(gdst.get(), key(LLM_KV_GENERAL_ARCHITECTURE).c_str(), llama_arch_name(arch));
-    gguf_set_val_str(gdst.get(), key(LLM_KV_TOKENIZER_MODEL).c_str(),       "none");
-    gguf_set_val_u32(gdst.get(), key(LLM_KV_VOCAB_SIZE).c_str(),                    1);
-    gguf_set_val_u32(gdst.get(), key(LLM_KV_CONTEXT_LENGTH).c_str(),                mtmd_graph_yue2_gen::n_ctx_max);
-    gguf_set_val_u32(gdst.get(), key(LLM_KV_EMBEDDING_LENGTH).c_str(),              hparams.n_embd);
-    gguf_set_val_u32(gdst.get(), key(LLM_KV_FEED_FORWARD_LENGTH).c_str(),           hparams.n_ff);
-    gguf_set_val_u32(gdst.get(), key(LLM_KV_BLOCK_COUNT).c_str(),                   hparams.n_layer);
-    gguf_set_val_u32(gdst.get(), key(LLM_KV_ATTENTION_HEAD_COUNT).c_str(),          hparams.n_head);
-    gguf_set_val_u32(gdst.get(), key(LLM_KV_ATTENTION_HEAD_COUNT_KV).c_str(),       hparams.n_head_kv);
-    gguf_set_val_f32(gdst.get(), key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS).c_str(),   hparams.eps);
-    gguf_set_val_f32(gdst.get(), key(LLM_KV_ROPE_FREQ_BASE).c_str(),                mtmd_graph_yue2_gen::rope_theta);
-
-    yue2_nar_source src;
-    const std::string prefix = "a.gen.nar.";
+    mtmd_owned_llama_source src;
     for (int64_t i = 0; i < gguf_get_n_tensors(gsrc.get()); i++) {
         const std::string name = gguf_get_tensor_name(gsrc.get(), i);
-        if (name.rfind(prefix + "blk.", 0) != 0 && name != prefix + "output_norm.weight") {
+        if (name.rfind(prefix + "blk.", 0) != 0 && name.rfind(prefix + "output_norm.", 0) != 0) {
             continue;
         }
         // the synthetic model only takes the type from here, the shapes come from the hparams
         ggml_tensor t = *ggml_get_tensor(meta, name.c_str());
         ggml_set_name(&t, name.substr(prefix.size()).c_str());
-        gguf_add_tensor(gdst.get(), &t);
+        gguf_add_tensor(gdst, &t);
         src.tensors[t.name] = { gguf_get_data_offset(gsrc.get()) + gguf_get_tensor_offset(gsrc.get(), i), ggml_nbytes(&t) };
     }
-    if (src.tensors.size() != (size_t) hparams.n_layer * 11 + 1) {
-        throw std::runtime_error("yue2: unexpected NAR tensors in the mmproj");
-    }
+    n_tensors = src.tensors.size();
     src.f = open_ifstream_binary(fname);
     if (!src.f) {
-        throw std::runtime_error(mtmd_format("yue2: failed to open %s", fname));
+        throw std::runtime_error(mtmd_format("failed to open %s", fname));
     }

     llama_model_params mparams = llama_model_default_params();
@@ -705,9 +698,62 @@ static llama_model * mtmd_yue2_nar_load(const char * fname, ggml_backend_dev_t d
     } else {
         mparams.n_gpu_layers = 0;
     }
-    llama_model * model = llama_model_init_from_user(gdst.get(), yue2_nar_set_tensor_data, &src, mparams);
+    llama_model * model = llama_model_init_from_user(gdst, mtmd_owned_llama_set_tensor_data, &src, mparams);
     if (!model) {
-        throw std::runtime_error("yue2: failed to load the NAR layers");
+        throw std::runtime_error(mtmd_format("failed to load the layers under %s", prefix.c_str()));
+    }
+    return model;
+}
+
+// yue2: the NAR layers are a qwen3 text model
+static llama_model * mtmd_yue2_nar_load(const char * fname, ggml_backend_dev_t device, const mtmd_hparams & hparams) {
+    const llm_arch arch = LLM_ARCH_QWEN3;
+    gguf_context_ptr gdst(gguf_init_empty());
+    auto key = [&](llm_kv kv) { return llama_arch_kv_name(arch, kv); };
+    gguf_set_val_str(gdst.get(), key(LLM_KV_GENERAL_ARCHITECTURE).c_str(), llama_arch_name(arch));
+    gguf_set_val_str(gdst.get(), key(LLM_KV_TOKENIZER_MODEL).c_str(),       "none");
+    gguf_set_val_u32(gdst.get(), key(LLM_KV_VOCAB_SIZE).c_str(),                    1);
+    gguf_set_val_u32(gdst.get(), key(LLM_KV_CONTEXT_LENGTH).c_str(),                mtmd_graph_yue2_gen::n_ctx_max);
+    gguf_set_val_u32(gdst.get(), key(LLM_KV_EMBEDDING_LENGTH).c_str(),              hparams.n_embd);
+    gguf_set_val_u32(gdst.get(), key(LLM_KV_FEED_FORWARD_LENGTH).c_str(),           hparams.n_ff);
+    gguf_set_val_u32(gdst.get(), key(LLM_KV_BLOCK_COUNT).c_str(),                   hparams.n_layer);
+    gguf_set_val_u32(gdst.get(), key(LLM_KV_ATTENTION_HEAD_COUNT).c_str(),          hparams.n_head);
+    gguf_set_val_u32(gdst.get(), key(LLM_KV_ATTENTION_HEAD_COUNT_KV).c_str(),       hparams.n_head_kv);
+    gguf_set_val_f32(gdst.get(), key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS).c_str(),   hparams.eps);
+    gguf_set_val_f32(gdst.get(), key(LLM_KV_ROPE_FREQ_BASE).c_str(),                mtmd_graph_yue2_gen::rope_theta);
+
+    size_t n_tensors = 0;
+    llama_model * model = mtmd_owned_llama_load(fname, device, gdst.get(), "a.gen.nar.", n_tensors);
+    if (n_tensors != (size_t) hparams.n_layer * 11 + 1) {
+        llama_model_free(model);
+        throw std::runtime_error("yue2: unexpected NAR tensors in the mmproj");
+    }
+    return model;
+}
+
+// tiny-voca: the prosody and acoustic stacks, same arch as the text model
+static llama_model * mtmd_tiny_voca_stack_load(const char * fname, ggml_backend_dev_t device, const mtmd_hparams & hparams, const char * prefix, int n_layer) {
+    const llm_arch arch = LLM_ARCH_TINY_VOCA;
+    gguf_context_ptr gdst(gguf_init_empty());
+    auto key = [&](llm_kv kv) { return llama_arch_kv_name(arch, kv); };
+    gguf_set_val_str(gdst.get(), key(LLM_KV_GENERAL_ARCHITECTURE).c_str(), llama_arch_name(arch));
+    gguf_set_val_str(gdst.get(), key(LLM_KV_TOKENIZER_MODEL).c_str(),       "none");
+    gguf_set_val_u32(gdst.get(), key(LLM_KV_VOCAB_SIZE).c_str(),                    1);
+    gguf_set_val_u32(gdst.get(), key(LLM_KV_CONTEXT_LENGTH).c_str(),                mtmd_graph_tiny_voca::n_max_frames);
+    gguf_set_val_u32(gdst.get(), key(LLM_KV_EMBEDDING_LENGTH).c_str(),              hparams.n_embd);
+    gguf_set_val_u32(gdst.get(), key(LLM_KV_FEED_FORWARD_LENGTH).c_str(),           hparams.n_ff);
+    gguf_set_val_u32(gdst.get(), key(LLM_KV_BLOCK_COUNT).c_str(),                   n_layer);
+    gguf_set_val_u32(gdst.get(), key(LLM_KV_ATTENTION_HEAD_COUNT).c_str(),          hparams.n_head);
+    gguf_set_val_u32(gdst.get(), key(LLM_KV_ATTENTION_HEAD_COUNT_KV).c_str(),       hparams.n_head);
+    gguf_set_val_f32(gdst.get(), key(LLM_KV_ATTENTION_LAYERNORM_EPS).c_str(),       hparams.eps);
+    gguf_set_val_f32(gdst.get(), key(LLM_KV_ROPE_FREQ_BASE).c_str(),                mtmd_graph_tiny_voca::rope_theta);
+    gguf_set_val_bool(gdst.get(), key(LLM_KV_ATTENTION_CAUSAL).c_str(),             false);
+
+    size_t n_tensors = 0;
+    llama_model * model = mtmd_owned_llama_load(fname, device, gdst.get(), prefix, n_tensors);
+    if (n_tensors != (size_t) n_layer * 18 + 2) {
+        llama_model_free(model);
+        throw std::runtime_error(mtmd_format("tiny_voca: unexpected tensors under %s in the mmproj", prefix));
     }
     return model;
 }
@@ -731,6 +777,11 @@ struct mtmd_context {
     llama_model_ptr       yue2_nar;
     llama_flash_attn_type yue2_flash_attn = LLAMA_FLASH_ATTN_TYPE_AUTO;

+    // tiny-voca: the frame stacks, run on llama_contexts of their own, see mtmd_gen_wav_tiny_voca()
+    llama_model_ptr       tiny_voca_prosody;
+    llama_model_ptr       tiny_voca_acoustic;
+    llama_flash_attn_type tiny_voca_flash_attn = LLAMA_FLASH_ATTN_TYPE_AUTO;
+
     // gen-side reference data of the last encoded chunk (chatterbox), see mtmd_gen_state_set_ref()
     mtmd_gen_ref gen_ref;

@@ -868,6 +919,13 @@ struct mtmd_context {
             yue2_nar.reset(mtmd_yue2_nar_load(mmproj_fname, device, models.gen_a->hparams));
             yue2_flash_attn = ctx_params.flash_attn_type;
         }
+        if (models.gen_a && models.gen_a->proj_type == PROJECTOR_TYPE_TINY_VOCA && !no_alloc) {
+            using g = mtmd_graph_tiny_voca;
+            const auto & hp = models.gen_a->hparams;
+            tiny_voca_prosody .reset(mtmd_tiny_voca_stack_load(mmproj_fname, device, hp, "a.gen.prosody.",  g::n_prosody_layer));
+            tiny_voca_acoustic.reset(mtmd_tiny_voca_stack_load(mmproj_fname, device, hp, "a.gen.acoustic.", g::n_acoustic_layer));
+            tiny_voca_flash_attn = ctx_params.flash_attn_type;
+        }
         for (size_t i = 0; i < backends->ptrs.size(); ++i) {
             const size_t size = ggml_backend_sched_get_buffer_size(backends->sched.get(), backends->ptrs[i]);
             if (size > 1) {
@@ -2680,6 +2738,8 @@ mtmd_gen_audio_info mtmd_gen_audio_get_info(const mtmd_context * ctx) {
             info.type = MTMD_GEN_AUDIO_TYPE_ZONOS3;
             info.sample_rate = 44100;
             break;
+            info.sample_rate = 24000;
+            break;
         case PROJECTOR_TYPE_YUE2_GEN:
             info.type = MTMD_GEN_AUDIO_TYPE_YUE2;
             info.sample_rate = 48000;
@@ -2698,6 +2758,10 @@ mtmd_gen_audio_info mtmd_gen_audio_get_info(const mtmd_context * ctx) {
             info.type = MTMD_GEN_AUDIO_TYPE_KOKORO;
             info.sample_rate = 24000;
             break;
+        case PROJECTOR_TYPE_TINY_VOCA:
+            info.type = MTMD_GEN_AUDIO_TYPE_TINY_VOCA;
+            info.sample_rate = 24000;
+            break;
         default:
             info.type = MTMD_GEN_AUDIO_TYPE_NONE;
             break;
@@ -3054,6 +3118,21 @@ static int32_t cbx_ref_encode(mtmd_context * ctx, const mtmd_audio_tokens * audi
     return 0;
 }

+int32_t mtmd_gen_audio_g2p(const mtmd_context * ctx, const char * word, char * buf, size_t buf_size) {
+    if (!ctx || !ctx->ctx_gen_a || !word || !buf || buf_size == 0) {
+        return 0;
+    }
+    std::string out;
+    if (ctx->ctx_gen_a->model.proj_type == PROJECTOR_TYPE_TINY_VOCA) {
+        out = mtmd_graph_tiny_voca::g2p(ctx->ctx_gen_a->model.tiny_voca, word);
+    }
+    if (out.size() + 1 > buf_size) {
+        return -1;
+    }
+    memcpy(buf, out.c_str(), out.size() + 1);
+    return (int32_t) out.size();
+}
+
 const char * mtmd_gen_audio_voice_name(const mtmd_context * ctx, int32_t idx) {
     if (!ctx->ctx_gen_a || idx < 0) {
         return nullptr;
@@ -3116,6 +3195,7 @@ mtmd_gen_inp mtmd_gen_inp_default(const mtmd_context * ctx) {
             break;
         case PROJECTOR_TYPE_SOPRANO:
         case PROJECTOR_TYPE_KOKORO:
+        case PROJECTOR_TYPE_TINY_VOCA:
             inp.type = MTMD_GEN_PROCESS_TYPE_GEN_WAV;
             break;
         case PROJECTOR_TYPE_MINIMAX_MUSIC3_GEN:
@@ -3338,8 +3418,8 @@ static int32_t mtmd_gen_wav_lfm3a(mtmd_context * ctx, mtmd_gen_state * state, co
     return 0;
 }

-// yue2: one pass of an mtmd graph, rows in -> rows out (PCM for GEN_WAV)
-static bool yue2_encode(mtmd_context * ctx, mtmd_gen_state * state, mtmd_subctx_gen_process proc, const std::vector<float> & in, std::vector<float> & out) {
+// one pass of an mtmd graph, rows in -> rows out (PCM for GEN_WAV), used by yue2 and tiny-voca
+static bool gen_encode_rows(mtmd_context * ctx, mtmd_gen_state * state, mtmd_subctx_gen_process proc, const std::vector<float> & in, std::vector<float> & out) {
     mtmd_image_f32 dummy;
     dummy.set_size({1, 1}, false, true);
     dummy.cpy_buf(std::vector<float>(1, 0.0f));
@@ -3464,7 +3544,7 @@ static int32_t mtmd_gen_flow_yue2(mtmd_context * ctx, mtmd_gen_state * state, co
     std::vector<float> embd, hidden, vel;
     auto velocity = [&](const std::vector<float> & x, double t, std::vector<float> & v) -> bool {
         std::copy(x.begin(), x.end(), x_rows.begin() + g::n_latent); // the start and end rows stay 0
-        if (!yue2_encode(ctx, state, MTMD_SUBCTX_GEN_PROCESS_LATENT_IN, x_rows, embd)) {
+        if (!gen_encode_rows(ctx, state, MTMD_SUBCTX_GEN_PROCESS_LATENT_IN, x_rows, embd)) {
             return false;
         }
         const std::vector<float> temb = time_embd(t);
@@ -3490,7 +3570,7 @@ static int32_t mtmd_gen_flow_yue2(mtmd_context * ctx, mtmd_gen_state * state, co
         const float * h = llama_get_embeddings(lctx.get());
         hidden.assign(h, h + n_rows * n_embd);
         llama_memory_seq_rm(mem, 0, (llama_pos) n_past, -1);
-        if (!yue2_encode(ctx, state, MTMD_SUBCTX_GEN_PROCESS_LATENT_OUT, hidden, vel)) {
+        if (!gen_encode_rows(ctx, state, MTMD_SUBCTX_GEN_PROCESS_LATENT_OUT, hidden, vel)) {
             return false;
         }
         v.assign(vel.begin() + g::n_latent, vel.end() - g::n_latent);
@@ -3529,6 +3609,160 @@ static int32_t mtmd_gen_flow_yue2(mtmd_context * ctx, mtmd_gen_state * state, co
     return 0;
 }

+// tiny-voca: one pass of a frame stack, embedding rows in -> hidden rows out
+static bool tiny_voca_stack(mtmd_context * ctx, llama_model * model, const std::vector<float> & in, std::vector<float> & out) {
+    const int64_t n_embd = llama_model_n_embd(model);
+    const int32_t n_rows = (int32_t) (in.size() / n_embd);
+
+    llama_context_params cparams = llama_context_default_params();
+    cparams.n_ctx           = (uint32_t) n_rows;
+    cparams.n_batch         = (uint32_t) n_rows;
+    cparams.n_ubatch        = (uint32_t) n_rows; // non-causal, all rows must be in one ubatch
+    cparams.n_seq_max       = 1;
+    cparams.n_threads       = ctx->n_threads;
+    cparams.n_threads_batch = ctx->n_threads;
+    cparams.embeddings      = true;
+    cparams.pooling_type    = LLAMA_POOLING_TYPE_NONE;
+    cparams.attention_type  = LLAMA_ATTENTION_TYPE_NON_CAUSAL;
+    cparams.flash_attn_type = ctx->tiny_voca_flash_attn;
+    cparams.no_perf         = true;
+    llama_context_ptr lctx(llama_init_from_model(model, cparams));
+    if (!lctx) {
+        LOG_ERR("%s: failed to create the context of a frame stack\n", __func__);
+        return false;
+    }
+
+    llama_batch_ext_ptr batch(llama_batch_ext_init(lctx.get()));
+    for (int32_t i = 0; i < n_rows; i++) {
+        const llama_embd row = { in.data() + (size_t) i * n_embd, 1, (size_t) n_embd };
+        const llama_pos  p   = i;
+        const int32_t    idx = llama_batch_ext_add_embd(batch.get(), 0, row);
+        if (idx < 0) {
+            LOG_ERR("%s: failed to add a row to the batch (error %d)\n", __func__, idx);
+            return false;
+        }
+        llama_batch_ext_set_pos(batch.get(), idx, &p);
+        llama_batch_ext_set_output_embd(batch.get(), idx, true);
+    }
+    if (llama_process(lctx.get(), LLAMA_PROCESS_TYPE_DECODE, batch.get()) != 0) {
+        LOG_ERR("%s: frame stack decode failed\n", __func__);
+        return false;
+    }
+    const float * h = llama_get_embeddings(lctx.get());
+    out.assign(h, h + (size_t) n_rows * n_embd);
+    return true;
+}
+
+// tiny-voca GEN_WAV: phoneme tokens + encoder states -> waveform, see TinyVoca.synthesize() in the reference (model.py)
+// codes: the tokens; feats: the voice row (encoder half, frame half), then the encoder state of each token
+static int32_t mtmd_gen_wav_tiny_voca(mtmd_context * ctx, mtmd_gen_state * state, const mtmd_gen_inp * inp, mtmd_gen_out * out) {
+    using g = mtmd_graph_tiny_voca;
+    const auto &  tv     = ctx->ctx_gen_a->model.tiny_voca;
+    const int64_t n_embd = ctx->ctx_gen_a->model.hparams.n_embd;
+    const int64_t n_tok  = (int64_t) inp->n_codes;
+
+    if (inp->type != MTMD_GEN_PROCESS_TYPE_GEN_WAV || !inp->codes || n_tok == 0 || !inp->feats ||
+            inp->n_feats != (size_t) ((2 + n_tok) * n_embd)) {
+        LOG_ERR("%s: tiny_voca requires the phoneme tokens, the voice row and the encoder state of each token\n", __func__);
+        return 1;
+    }
+    if (!std::isfinite(inp->speed) || inp->speed < 0.25f || inp->speed > 4.0f) {
+        LOG_ERR("%s: speed must be between 0.25 and 4\n", __func__);
+        return 1;
+    }
+    for (int64_t i = 0; i < n_tok; i++) {
+        if (inp->codes[i] < 0 || inp->codes[i] >= (int32_t) tv.token_class.size()) {
+            LOG_ERR("%s: token out of range (%d)\n", __func__, inp->codes[i]);
+            return 1;
+        }
+    }
+    const float * frame_voice = inp->feats + n_embd;
+    const std::vector<float> hidden(inp->feats + 2 * n_embd, inp->feats + inp->n_feats);
+
+    std::vector<float> dur_out;
+    if (!gen_encode_rows(ctx, state, MTMD_SUBCTX_GEN_PROCESS_LATENT_IN, hidden, dur_out) || dur_out.size() != (size_t) (2 * n_tok)) {
+        LOG_ERR("%s: duration predictor failed\n", __func__);
+        return 1;
+    }
+
+    // frames per token: a phone lasts at least one frame, a pause token only when its presence logit is > 0
+    std::vector<int64_t> dur(n_tok);
+    int64_t n_frames = 0;
+    for (int64_t i = 0; i < n_tok; i++) {
+        const float  o0     = std::clamp(dur_out[2 * i], 0.0f, std::log1p(g::max_dur));
+        const double frames = std::max(1.0, std::nearbyint((double) std::expm1(o0) / inp->speed));
+        const bool   phone  = tv.token_class[inp->codes[i]] > 0.0f;
+        dur[i] = phone || dur_out[2 * i + 1] > 0.0f ? (int64_t) frames : 0;
+        n_frames += dur[i];
+    }
+    if (n_frames < 1 || n_frames > g::n_max_frames) {
+        LOG_ERR("%s: the utterance has %lld frames, it must have 1 to %d, split the text\n", __func__, (long long) n_frames, g::n_max_frames);
+        return 1;
+    }
+
+    // token states repeated by their duration, plus the position inside the token and the frame voice
+    std::vector<float>   x((size_t) (n_frames * n_embd));
+    std::vector<int64_t> owner(n_frames);
+    for (int64_t i = 0, f = 0; i < n_tok; i++) {
+        for (int64_t o = 0; o < dur[i]; o++, f++) {
+            const float rel  = ((float) o + 0.5f) / (float) dur[i];
+            const float feat[3] = { rel, 1.0f - rel, std::log1p((float) dur[i]) / 4.0f };
+            float * row = x.data() + f * n_embd;
+            for (int64_t c = 0; c < n_embd; c++) {
+                const float * w = tv.frame_pos_w.data() + c * 3;
+                row[c] = hidden[i * n_embd + c] + tv.frame_pos_b[c] + w[0] * feat[0] + w[1] * feat[1] + w[2] * feat[2] + frame_voice[c];
+            }
+            owner[f] = i;
+        }
+    }
+
+    std::vector<float> h_pro;
+    if (!tiny_voca_stack(ctx, ctx->tiny_voca_prosody.get(), x, h_pro)) {
+        return 1;
+    }
+
+    // pitch, voicing and energy; the frames of a vowel are always voiced
+    std::vector<float> f0(n_frames);
+    for (int64_t f = 0; f < n_frames; f++) {
+        float p[3];
+        const float * h = h_pro.data() + f * n_embd;
+        for (int k = 0; k < 3; k++) {
+            const float * w = tv.prosody_head_w.data() + k * n_embd;
+            float acc = tv.prosody_head_b[k];
+            for (int64_t c = 0; c < n_embd; c++) {
+                acc += w[c] * h[c];
+            }
+            p[k] = acc;
+        }
+        if (tv.token_class[inp->codes[owner[f]]] == 2.0f) {
+            p[1] = std::max(p[1], 2.0f);
+        }
+        f0[f] = std::clamp(g::f0_ref * std::exp(p[0]), g::f0_min, g::f0_max);
+
+        // the acoustic stack is conditioned on the clamped pitch and energy and the hard voicing decision
+        const float cond[3] = { std::clamp(p[0], -2.0f, 1.5f), p[1] > 0.0f ? 1.0f : 0.0f, std::clamp(p[2], -4.5f, 3.0f) };
+        float * row = x.data() + f * n_embd;
+        for (int64_t c = 0; c < n_embd; c++) {
+            const float * w = tv.prosody_proj_w.data() + c * 3;
+            row[c] = h[c] + tv.prosody_proj_b[c] + w[0] * cond[0] + w[1] * cond[1] + w[2] * cond[2];
+        }
+    }
+
+    std::vector<float> h_ac, voc;
+    if (!tiny_voca_stack(ctx, ctx->tiny_voca_acoustic.get(), x, h_ac)) {
+        return 1;
+    }
+    if (!gen_encode_rows(ctx, state, MTMD_SUBCTX_GEN_PROCESS_LATENT_OUT, h_ac, voc) || voc.size() != (size_t) (n_frames * g::n_voc)) {
+        LOG_ERR("%s: source-filter heads failed\n", __func__);
+        return 1;
+    }
+
+    g::synth(voc, f0, state->gen_state->rng, state->out_audio);
+    out->audio     = state->out_audio.data();
+    out->n_samples = state->out_audio.size();
+    return 0;
+}
+
 // yue2 GEN_WAV: latents -> interleaved stereo PCM in one VAE pass, the caller splits a long song into tiles
 static int32_t mtmd_gen_wav_yue2(mtmd_context * ctx, mtmd_gen_state * state, const mtmd_gen_inp * inp, mtmd_gen_out * out) {
     using g = mtmd_graph_yue2_gen;
@@ -3538,7 +3772,7 @@ static int32_t mtmd_gen_wav_yue2(mtmd_context * ctx, mtmd_gen_state * state, con
     }
     const std::vector<float> feats(inp->feats, inp->feats + inp->n_feats);
     auto & audio = state->out_audio;
-    if (!yue2_encode(ctx, state, MTMD_SUBCTX_GEN_PROCESS_GEN_WAV, feats, audio)) {
+    if (!gen_encode_rows(ctx, state, MTMD_SUBCTX_GEN_PROCESS_GEN_WAV, feats, audio)) {
         LOG_ERR("%s: encode failed (vae)\n", __func__);
         return 1;
     }
@@ -3736,6 +3970,10 @@ static int32_t mtmd_gen_audio_process_impl(mtmd_context * ctx, mtmd_gen_state *
                                                            : mtmd_gen_wav_music3(ctx, state, inp, out);
     }

+    if (sctx->model.proj_type == PROJECTOR_TYPE_TINY_VOCA) {
+        return mtmd_gen_wav_tiny_voca(ctx, state, inp, out);
+    }
+
     if (sctx->model.proj_type == PROJECTOR_TYPE_SOPRANO) {
         const size_t n_embd = (size_t) sctx->model.n_mmproj_embd();
         if (inp->type != MTMD_GEN_PROCESS_TYPE_GEN_WAV || !inp->feats || inp->n_feats < 2 * n_embd || inp->n_feats % n_embd != 0 || inp->codes) {
diff --git a/tools/mtmd/mtmd.h b/tools/mtmd/mtmd.h
index 2ed43c38f..7a15f470c 100644
--- a/tools/mtmd/mtmd.h
+++ b/tools/mtmd/mtmd.h
@@ -479,6 +479,7 @@ enum mtmd_gen_audio_type {
     MTMD_GEN_AUDIO_TYPE_MINIMAX_MUSIC3,
     MTMD_GEN_AUDIO_TYPE_FISH_S2,
     MTMD_GEN_AUDIO_TYPE_KOKORO,
+    MTMD_GEN_AUDIO_TYPE_TINY_VOCA,
 };

 struct mtmd_gen_audio_info {
@@ -496,6 +497,12 @@ MTMD_API size_t mtmd_gen_audio_read_tensor(mtmd_context * ctx, const char * name
 // name of the idx-th predefined voice, nullptr when idx is out of range; idx 0 is the default voice
 MTMD_API const char * mtmd_gen_audio_voice_name(const mtmd_context * ctx, int32_t idx);

+// pronunciation of a word by the letter-to-sound model of the mmproj, for the tts_g2p() of the "tts" chat template
+// (see common_chat_get_tts_prompt()); the phonemes are separated by spaces, in the notation of the model (tiny-voca: CMUdict)
+// returns the length written to buf (NUL terminated), 0 if the model has no letter-to-sound model or no pronunciation,
+// -1 if buf is too small; does not run a graph, can be called from any thread
+MTMD_API int32_t mtmd_gen_audio_g2p(const mtmd_context * ctx, const char * word, char * buf, size_t buf_size);
+
 // predefined voice with this name as a voice bitmap, to pass as speaker reference
 // returns nullptr if there is no such voice, the caller must free it
 MTMD_API mtmd_bitmap * mtmd_gen_audio_get_voice(const mtmd_context * ctx, const char * name);
diff --git a/tools/server/server-context.cpp b/tools/server/server-context.cpp
index 145cfa047..0d4a9e977 100644
--- a/tools/server/server-context.cpp
+++ b/tools/server/server-context.cpp
@@ -6986,7 +6986,11 @@ std::unique_ptr<server_res_generator> server_routes::handle_tts_impl(const serve
     const std::string voice      = json_value(body, "voice", has_speaker_ref ? std::string() : params.tts_voice);
     // raw_prompt: the input goes to the model as is (e.g. phonemes), the "tts" chat template is skipped
     const bool raw_prompt        = json_value(body, "raw_prompt", false);
-    const std::string tts_prompt = raw_prompt ? std::string() : common_chat_get_tts_prompt(meta->chat_params.tmpls.get(), prompt, lang, voice);
+    const auto g2p = [this](const std::string & word) {
+        char buf[512];
+        return mtmd_gen_audio_g2p(ctx_server.mctx, word.c_str(), buf, sizeof(buf)) > 0 ? std::string(buf) : std::string();
+    };
+    const std::string tts_prompt = raw_prompt ? std::string() : common_chat_get_tts_prompt(meta->chat_params.tmpls.get(), prompt, lang, voice, g2p);
     // a model with predefined voices only speaks with one of them
     mtmd::bitmap_ptr voice_ref;
     if (!voice.empty() && info.n_voices > 0) {
diff --git a/tools/tts/tts.cpp b/tools/tts/tts.cpp
index 68e4d09ae..dbf618ac2 100644
--- a/tools/tts/tts.cpp
+++ b/tools/tts/tts.cpp
@@ -137,7 +137,10 @@ int main(int argc, char ** argv) {
     auto tmpls = common_chat_templates_init(model, "");
     std::string prompt;
     try {
-        prompt = common_chat_get_tts_prompt(tmpls.get(), params.prompt, params.tts_lang, params.tts_voice);
+        prompt = common_chat_get_tts_prompt(tmpls.get(), params.prompt, params.tts_lang, params.tts_voice, [&](const std::string & word) {
+            char buf[512];
+            return mtmd_gen_audio_g2p(mctx.get(), word.c_str(), buf, sizeof(buf)) > 0 ? std::string(buf) : std::string();
+        });
     } catch (const std::exception & e) {
         LOG_ERR("failed to prepare the text to speak: %s\n", e.what());
         return 1;