Files
sherpa-onnx/python-api-examples/test-offline-tts-kitten-phonemize.py
Fangjun Kuang 0b3647632f Remove espeak-ng and piper-phonemize from sherpa-onnx (#4055)
Fixes #3731

espeak-ng and piper-phonemize are no longer built into sherpa-onnx.

Text frontends are now:

- **lexicon** — built in, no extra dependency
- **phonemize** — phonemize outside sherpa-onnx (e.g. [piper-phonemize](https://github.com/csukuangfj/piper-phonemize)) and pass the result in. That library is GPL, so it stays optional: sherpa-onnx is GPL-free after this PR.

## API

- `SherpaOnnxGenerationConfig` gains `phoneme_codepoints` and `tokens` (mutually exclusive alternatives to `text`)
- `data_dir` on the TTS model configs is deprecated and ignored; the ABI slot is kept
- all bindings updated: C/C++, Python, Java, Kotlin, Go, Rust, C#, Swift, Pascal, Dart/Flutter, node-addon, WASM, HarmonyOS
2026-10-20 11:09:04 +08:00

76 lines
2.3 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Test a Kitten English TTS model using piper_phonemize (no espeak-ng data).
If you want to use a lexicon instead, please see
test-offline-tts-kitten-lexicon.py.
Install piper_phonemize:
pip install piper_phonemize -f https://k2-fsa.github.io/icefall/piper_phonemize.html
Download model:
wget https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/kitten-micro-en-v0_8.tar.bz2
tar xf kitten-micro-en-v0_8.tar.bz2
"""
import time
import sherpa_onnx
import soundfile as sf
try:
from piper_phonemize import phonemize_espeak
except Exception as ex:
raise RuntimeError(
f"{ex}\nPlease run\n"
"pip install piper_phonemize -f https://k2-fsa.github.io/icefall/piper_phonemize.html"
)
tts_config = sherpa_onnx.OfflineTtsConfig(
model=sherpa_onnx.OfflineTtsModelConfig(
kitten=sherpa_onnx.OfflineTtsKittenModelConfig(
model="./kitten-micro-en-v0_8/model.onnx",
voices="./kitten-micro-en-v0_8/voices.bin",
tokens="./kitten-micro-en-v0_8/tokens.txt",
),
debug=True,
),
)
tts = sherpa_onnx.OfflineTts(tts_config)
text = "Today as always, men fall into two groups: slaves and free men."
# The espeak-ng voice for the phonemizer is taken from the model metadata.
# Fall back to en-us if the model has no language information.
lang = tts.lang
if not lang:
print("Warning: the model has no language information. Falling back to en-us.")
lang = "en-us"
print("Phonemizer language:", lang)
sentences = phonemize_espeak(text, lang)
phoneme_codepoints = [[ord(c) for c in sentence] for sentence in sentences]
gen_config = sherpa_onnx.GenerationConfig()
gen_config.sid = 0
gen_config.speed = 1.0
gen_config.phoneme_codepoints = phoneme_codepoints
start = time.time()
audio = tts.generate("", gen_config)
end = time.time()
assert len(audio.samples) > 0, "No audio generated!"
assert audio.sample_rate > 0, "Invalid sample rate!"
filename = "test-kitten-phonemize.wav"
sf.write(filename, audio.samples, samplerate=audio.sample_rate)
elapsed_seconds = end - start
audio_duration = len(audio.samples) / audio.sample_rate
rtf = elapsed_seconds / audio_duration
print(f"Saved to {filename}")
print(f"Elapsed seconds: {elapsed_seconds:.3f}")
print(f"Audio duration in seconds: {audio_duration:.3f}")
print(f"RTF: {elapsed_seconds:.3f}/{audio_duration:.3f} = {rtf:.3f}")