Skip to content

An 8M-parameter English text-to-speech model distilled from Kokoro-82M. 9 MB, runs 18x real time on one CPU thread.

British phonemes for the British voices (bf_isabella, bm_fable)

Sahil Mahendrakard37d2531dcc7
diff --git a/paradee/tts.py b/paradee/tts.py
index 64c6f4f..0ec0247 100644
--- a/paradee/tts.py
+++ b/paradee/tts.py
@@ -28,11 +28,19 @@ class Paradee:
so.intra_op_num_threads = threads
so.inter_op_num_threads = 1
self.session = ort.InferenceSession(model_path, so, providers=["CPUExecutionProvider"])
- from misaki import en, espeak # the same G2P setup as kokoro.KPipeline(lang_code="a"), which made the training data
- self.g2p = en.G2P(trf=False, british=False, fallback=espeak.EspeakFallback(british=False), unk="")
+ self._g2p = {}
- def phonemize(self, text):
- phonemes, _ = self.g2p(text)
+ def g2p(self, british=False):
+ """The same G2P setup as kokoro.KPipeline, which made the training data: lang_code "a" (American) for
+ the af_/am_ voices, "b" (British) for the bf_/bm_ ones. Each is loaded the first time it is needed."""
+ if british not in self._g2p:
+ from misaki import en, espeak
+ self._g2p[british] = en.G2P(trf=False, british=british, fallback=espeak.EspeakFallback(british=british), unk="")
+ return self._g2p[british]
+
+ def phonemize(self, text, voice="af_heart"):
+ """British phonemes for the British voices (bf_isabella, bm_fable), American for the rest."""
+ phonemes, _ = self.g2p(british=voice.startswith("b"))(text)
return phonemes
def generate_from_phonemes(self, phonemes, speed=1.0):
@@ -40,16 +48,16 @@ class Paradee:
feed = {"input_ids": np.array([ids], dtype=np.int64), "speed": np.array([speed], dtype=np.float32)}
return self.session.run(None, feed)[0][0]
- def __call__(self, text, speed=1.0):
+ def __call__(self, text, speed=1.0, voice="af_heart"):
"""speed > 1 speaks faster. Long text is read one sentence at a time."""
- parts = [self.generate_from_phonemes(ps, speed) for ps in self._chunks(text)]
+ parts = [self.generate_from_phonemes(ps, speed) for ps in self._chunks(text, voice)]
return np.concatenate(parts) if parts else np.zeros(0, dtype=np.float32)
- def _chunks(self, text):
+ def _chunks(self, text, voice="af_heart"):
for sentence in re.split(r"(?<=[.!?…])\s+|\n+", text.strip()):
if not sentence.strip():
continue
- ps = self.phonemize(sentence)
+ ps = self.phonemize(sentence, voice)
while len(ps) > MAX_PHONEMES: # a very long sentence: cut at the last space that fits
cut = ps.rfind(" ", 0, MAX_PHONEMES)
cut = cut if cut > 0 else MAX_PHONEMES
diff --git a/web/misaki.js b/web/misaki.js
index 7a0cf41..31a2fbb 100644
--- a/web/misaki.js
+++ b/web/misaki.js
@@ -2,7 +2,8 @@
// misaki (Kokoro's own G2P) in training. The two spell the same sounds differently
// ("aɪ" vs "I", "oʊ" vs "O", length marks, flaps), and fed raw eSpeak phonemes
// Paradee mumbles (Whisper word error rate 31% vs 3%). This is misaki's own
-// eSpeak-to-misaki table (misaki/espeak.py, EspeakFallback, American English),
+// eSpeak-to-misaki table (misaki/espeak.py, EspeakFallback), for American English and, with
+// british set, British English (misaki's lang 'b', which the bf_/bm_ voices use),
// adapted to eSpeak output that has no tie marks between the two halves of a
// diphthong.
const E2M = [
@@ -20,18 +21,28 @@ const E2M = [
['̃', ''],
];
-export function toMisaki(ps) {
+export function toMisaki(ps, british = false) {
+ // British (bf_/bm_ voices): eSpeak's "eə" is misaki's ɛː and has to go before
+ // the e -> A rule below. Here British vowels keep their length marks.
+ if (british) ps = ps.split('eə').join('ɛː');
for (const [a, b] of E2M) ps = ps.split(a).join(b);
ps = ps.replace(/(\S)̩/gu, 'ᵊ$1').replace(/̩/g, '');
// misaki writes a syllabic l at the end of a word as ᵊl (naval -> nˈAvᵊl).
// eSpeak marks it with a tie that kokoro-js drops, so key on word-final əl.
ps = ps.replace(/əl(?=[\s;:,.!?—…"“”()]|$)/gu, 'ᵊl');
// misaki's table maps ɐ to ə, but misaki's own lexicon keeps ɐ for the article "a"
- ps = ps.replace(/ɐ(?![\s;:,.!?—…"“”()]|$)/gu, 'ə');
- ps = ps.split('oʊ').join('O')
- .split('ɜːɹ').join('ɜɹ').split('ɜː').join('ɜɹ')
- .split('ɪə').join('iə')
- .split('ː').join('');
+ // (and in British, for "an" too)
+ ps = british ? ps.replace(/ɐ(?!n?(?:[\s;:,.!?—…"“”()]|$))/gu, 'ə')
+ : ps.replace(/ɐ(?![\s;:,.!?—…"“”()]|$)/gu, 'ə');
+ if (british) {
+ ps = ps.split('iə').join('ɪə').split('əʊ').join('Q');
+ ps = ps.replace(/(^|\s)ðɪ(?=\s|$)/gu, '$1ði'); // "the" before a vowel
+ } else {
+ ps = ps.split('oʊ').join('O')
+ .split('ɜːɹ').join('ɜɹ').split('ɜː').join('ɜɹ')
+ .split('ɪə').join('iə')
+ .split('ː').join('');
+ }
ps = ps.split('o').join('ɔ');
return ps.split('ɾ').join('T').split('ʔ').join('t');
}