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.

Word timestamps: with_timestamps returns when each word is spoken

Sayed Qutob8e43eb0fa50a
diff --git a/README.md b/README.md
index 819c73c..95cbf42 100644
--- a/README.md
+++ b/README.md
@@ -37,6 +37,14 @@ audio = tts("Paradee is a small voice that runs anywhere.", voice="bm_fable")
sf.write("hello.wav", audio, SAMPLE_RATE)
```
+`with_timestamps` also says when each word is spoken, for captions or for highlighting the word
+being read. The times come from the durations the audio was made from:
+
+```python
+audio, words = tts.with_timestamps("Paradee is a small voice that runs anywhere.")
+# words: [("Paradee", 0.25, 0.84), ("is", 0.84, 0.96), ("a", 0.96, 1.06), ...]: (text, start, end) in seconds
+```
+
Text is turned into phonemes with [misaki](https://github.com/hexgrad/misaki), Kokoro's own
grapheme-to-phoneme library, which is what the training data used. The first run also downloads
misaki's English dictionary data. The British voices use misaki's British dictionary, as Kokoro
@@ -138,6 +146,10 @@ The model is one ONNX graph that goes from token ids to audio:
input is `speed`, float32 `[1]`, where 1.0 is normal speed. In 1.2 the third input is `voice`,
int64 `[1]`, the voice's position in the `voices` list in `config.json` (0 is `af_heart`).
- **Output:** `waveform`, float32 `[1, samples]` at 24 kHz.
+- **Durations:** the graph also computes each token's length in frames of 600 samples, rounded,
+ as the tensor `/Clip_output_0`, float32 `[1, T]`, pads included. The frames add up to the
+ waveform's length exactly. It is not a graph output in the released files; `paradee/tts.py`
+ adds it as one when it loads the file, and `with_timestamps` turns it into word times.
[`web/misaki.js`](web/misaki.js) is for the browser. Browser phonemizers such as
[kokoro-js](https://github.com/hexgrad/kokoro/tree/main/kokoro.js) use eSpeak NG, which writes
diff --git a/paradee/tts.py b/paradee/tts.py
index e596a11..8869afe 100644
--- a/paradee/tts.py
+++ b/paradee/tts.py
@@ -9,6 +9,108 @@ REPOS = {"1.2": "sahilmahendrakar/Paradee-8M-v1.2", "1.1": "sahilmahendrakar/Par
VOICES = {"1.2": ["af_heart", "bf_isabella", "am_puck", "bm_fable"], "1.1": ["af_heart"], "1.0": ["af_heart"]}
SAMPLE_RATE = 24000
MAX_PHONEMES = 510 # the text side has 512 positions, two of which are the pad tokens at each end
+# Inside every released graph (1.0, 1.1, 1.2): each token's length in frames of 600 samples, rounded. The frames
+# add up to the waveform's length exactly. It is not a graph output; the loader adds it as one (_with_output).
+DURATIONS = "/Clip_output_0"
+
+
+def _with_output(model, name):
+ """The ONNX file `model` (bytes) with the float tensor `name` as one more graph output, so the durations can
+ be read without re-exporting the model and without the onnx package. An ONNX file is a protobuf: the graph
+ is field 7 of the model and its outputs are field 12 of the graph, so one ValueInfoProto is appended to the
+ graph and the graph's length prefix rewritten. Every other byte stays as it was."""
+ def varint(n):
+ out = bytearray()
+ while True:
+ out.append((n & 0x7F) | (0x80 if n > 0x7F else 0))
+ n >>= 7
+ if not n:
+ return bytes(out)
+
+ def read_varint(i):
+ n = shift = 0
+ while True:
+ b = model[i]
+ i += 1
+ n |= (b & 0x7F) << shift
+ shift += 7
+ if b < 0x80:
+ return n, i
+
+ def field(num, payload): # a length-delimited field
+ return varint(num << 3 | 2) + varint(len(payload)) + payload
+
+ i = 0
+ while i < len(model):
+ start = i
+ key, i = read_varint(i)
+ kind = key & 7
+ if kind == 0:
+ _, i = read_varint(i)
+ elif kind == 1:
+ i += 8
+ elif kind == 5:
+ i += 4
+ elif kind == 2:
+ n, body = read_varint(i)
+ i = body + n
+ if key >> 3 == 7:
+ # ValueInfoProto{name, type: TypeProto{tensor_type: {elem_type: FLOAT}}}
+ info = field(1, name.encode()) + field(2, field(1, b"\x08\x01"))
+ return model[:start] + field(7, model[body:i] + field(12, info)) + model[i:]
+ else:
+ break
+ raise ValueError("no graph found in the ONNX model")
+
+
+def _phonemes(tokens):
+ """misaki's tokens as the phoneme string the model reads, one space between words."""
+ return "".join(t.phonemes + (" " if t.whitespace else "") for t in tokens).strip()
+
+
+def _token_chunks(tokens):
+ """misaki's tokens in runs whose phonemes fit the model, cut between words, as _chunks cuts text."""
+ chunk = []
+ for t in tokens:
+ t.phonemes = t.phonemes or ""
+ if chunk and len(_phonemes(chunk + [t])) > MAX_PHONEMES:
+ yield chunk
+ chunk = []
+ chunk.append(t)
+ if chunk:
+ yield chunk
+
+
+def _word_times(tokens, durations):
+ """(text, start, end) in seconds for each word among `tokens`, from the frames of every token id, the pad at
+ each end included. This is Kokoro's KPipeline.join_timestamps (kokoro/pipeline.py, Apache-2.0): it counts in
+ half-frames, so the frames of a space are split between the words either side of it."""
+ words = []
+ if len(durations) < 3:
+ return words
+ left = right = 2 * max(0, int(durations[0]) - 3)
+ i = 1
+ for t in tokens:
+ if i >= len(durations) - 1:
+ break
+ if not t.phonemes:
+ if t.whitespace:
+ i += 1
+ left = right + int(durations[i])
+ right = left + int(durations[i])
+ i += 1
+ continue
+ j = i + len(t.phonemes)
+ if j >= len(durations):
+ break
+ start = left / 80
+ space = int(durations[j]) if t.whitespace else 0
+ left = right + 2 * int(durations[i:j].sum()) + space
+ right = left + space
+ if any(c.isalnum() for c in t.text): # punctuation has a duration too, but is not a word
+ words.append((t.text, start, left / 80))
+ i = j + (1 if t.whitespace else 0)
+ return words
class Paradee:
@@ -32,7 +134,9 @@ class Paradee:
so = ort.SessionOptions()
so.intra_op_num_threads = threads
so.inter_op_num_threads = 1
- self.session = ort.InferenceSession(model_path, so, providers=["CPUExecutionProvider"])
+ with open(model_path, "rb") as f:
+ model = _with_output(f.read(), DURATIONS)
+ self.session = ort.InferenceSession(model, so, providers=["CPUExecutionProvider"])
self._g2p = {}
def g2p(self, british=False):
@@ -53,12 +157,17 @@ class Paradee:
raise ValueError(f"Paradee {self.version} has no voice {voice!r}; its voices are {', '.join(self.voices)}")
def generate_from_phonemes(self, phonemes, speed=1.0, voice="af_heart"):
+ return self._run(phonemes, speed, voice)[0]
+
+ def _run(self, phonemes, speed, voice):
+ """(waveform, frames of every token id), for one run of the model."""
self.check_voice(voice)
ids = [0] + [self.vocab[c] for c in phonemes if c in self.vocab][:MAX_PHONEMES] + [0]
feed = {"input_ids": np.array([ids], dtype=np.int64), "speed": np.array([speed], dtype=np.float32)}
if len(self.voices) > 1:
feed["voice"] = np.array([self.voices.index(voice)], dtype=np.int64)
- return self.session.run(None, feed)[0][0]
+ waveform, durations = self.session.run(None, feed)
+ return waveform[0], np.rint(durations.reshape(-1)).astype(np.int64)
def __call__(self, text, speed=1.0, voice="af_heart"):
"""speed > 1 speaks faster. voice is one of self.voices. Long text is read one sentence at a time."""
@@ -66,10 +175,28 @@ class Paradee:
parts = [self.generate_from_phonemes(ps, speed, voice) for ps in self._chunks(text, voice)]
return np.concatenate(parts) if parts else np.zeros(0, dtype=np.float32)
+ def with_timestamps(self, text, speed=1.0, voice="af_heart"):
+ """Like calling the model, but also says when each word is spoken, for captions or for highlighting the
+ word being read. Returns (audio, words): each word is (text, start, end) in seconds of that audio, and
+ punctuation is left out. The times come from the durations the model made the audio from."""
+ self.check_voice(voice)
+ g2p = self.g2p(british=voice.startswith("b"))
+ parts, words, offset = [], [], 0.0
+ for sentence in self._sentences(text):
+ _, tokens = g2p(sentence)
+ for chunk in _token_chunks(tokens):
+ audio, durations = self._run(_phonemes(chunk), speed, voice)
+ words += [(w, offset + start, offset + end) for w, start, end in _word_times(chunk, durations)]
+ offset += len(audio) / SAMPLE_RATE
+ parts.append(audio)
+ return (np.concatenate(parts) if parts else np.zeros(0, dtype=np.float32)), words
+
+ @staticmethod
+ def _sentences(text):
+ return (s for s in re.split(r"(?<=[.!?…])\s+|\n+", text.strip()) if s.strip())
+
def _chunks(self, text, voice="af_heart"):
- for sentence in re.split(r"(?<=[.!?…])\s+|\n+", text.strip()):
- if not sentence.strip():
- continue
+ for sentence in self._sentences(text):
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)