mstar.model.kokoro.g2p#

Grapheme-to-phoneme front end and sentence chunking for Kokoro.

Runs on the CPU in the API data worker (process_prompt). English uses misaki’s lexicon G2P with, when the optional misaki[en] extra (which bundles espeak-ng) is installed, its espeak fallback for out-of-dictionary words; Japanese and Mandarin use misaki[ja] / misaki[zh]; the other languages Kokoro ships voices for go through espeak-ng.

Text is cut into chunks at sentence boundaries. A chunk never exceeds the model’s 510-phoneme window; sentences are packed up to a target size so the first chunk is small (time-to-first-audio) while later ones amortize per-step cost.

Functions

chunk_phoneme_strings(pairs, target, hard_max)

Pack (text, phonemes) sentences produced by a string-level G2P.

chunk_tokens(tokens, target, hard_max[, ...])

install_hint(lang)

normalize_lang_code(lang)

pack_pieces(pieces, target[, first_target])

Greedily merge consecutive pieces while the phoneme count stays within target (first_target for the first chunk); a single piece may exceed it.

split_phonemes(phonemes, hard_max)

Cut a phoneme string into pieces of at most hard_max: at the last space that fits, else hard.

split_sentences(tokens)

Group misaki tokens into sentences, keeping closing quotes/brackets with the sentence they end.

Classes

Chunk(text, phonemes)

One synthesis unit: the source text and its phoneme string.

G2PFrontend(chunk_target, max_phonemes[, ...])

Lazily constructed per-language phonemizers plus chunking.

class mstar.model.kokoro.g2p.Chunk(text, phonemes)[source]#

Bases: object

One synthesis unit: the source text and its phoneme string.

Parameters:
phonemes: str#
text: str#
class mstar.model.kokoro.g2p.G2PFrontend(chunk_target, max_phonemes, espeak_fallback=True, first_chunk_target=None)[source]#

Bases: object

Lazily constructed per-language phonemizers plus chunking.

Parameters:
  • chunk_target (int)

  • max_phonemes (int)

  • espeak_fallback (bool)

  • first_chunk_target (int | None)

available(lang)[source]#

Whether this process can phonemize lang.

English is judged by the presence of misaki.en (its backend loads spaCy, which the process answering /v1/audio/voices never needs); the other languages by constructing their backend, which is cheap.

Parameters:

lang (str)

Return type:

bool

backend(lang)[source]#
Parameters:

lang (str)

chunk(text, lang)[source]#

Phonemize text and cut it into synthesis chunks.

Parameters:
Return type:

list[Chunk]

chunk_phonemes(phonemes)[source]#

Caller-supplied phonemes bypass G2P; still bounded by the window.

Parameters:

phonemes (str)

Return type:

list[Chunk]

mstar.model.kokoro.g2p.chunk_phoneme_strings(pairs, target, hard_max, first_target=None)[source]#

Pack (text, phonemes) sentences produced by a string-level G2P.

Parameters:
Return type:

list[Chunk]

mstar.model.kokoro.g2p.chunk_tokens(tokens, target, hard_max, first_target=None)[source]#
Parameters:
  • target (int)

  • hard_max (int)

  • first_target (int | None)

Return type:

list[Chunk]

mstar.model.kokoro.g2p.install_hint(lang)[source]#
Parameters:

lang (str)

Return type:

str

mstar.model.kokoro.g2p.normalize_lang_code(lang)[source]#
Parameters:

lang (str)

Return type:

str

mstar.model.kokoro.g2p.pack_pieces(pieces, target, first_target=None)[source]#

Greedily merge consecutive pieces while the phoneme count stays within target (first_target for the first chunk); a single piece may exceed it.

Parameters:
Return type:

list[list]

mstar.model.kokoro.g2p.split_phonemes(phonemes, hard_max)[source]#

Cut a phoneme string into pieces of at most hard_max: at the last space that fits, else hard. For a single word too long for the window (a long number, an ID, a hyphenated compound).

Parameters:
  • phonemes (str)

  • hard_max (int)

Return type:

list[str]

mstar.model.kokoro.g2p.split_sentences(tokens)[source]#

Group misaki tokens into sentences, keeping closing quotes/brackets with the sentence they end.

Return type:

list[list]