Files
sherpa-onnx/java-api-examples/NonStreamingTtsKokoroEnLexicon.java
T
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

65 lines
2.4 KiB
Java

// Copyright 2025 Xiaomi Corporation
// This file shows how to use a Kokoro English model
// to convert text to speech
import com.k2fsa.sherpa.onnx.*;
public class NonStreamingTtsKokoroEnLexicon {
public static void main(String[] args) {
// please visit
// https://k2-fsa.github.io/sherpa/onnx/tts/pretrained_models/kokoro.html
// to download model files
String model = "./kokoro-en-v0_19/model.onnx";
String voices = "./kokoro-en-v0_19/voices.bin";
String tokens = "./kokoro-en-v0_19/tokens.txt";
String lexicon = "./lexicon-en-us.txt";
String text =
"Today as always, men fall into two groups: slaves and free men. Whoever does not have"
+ " two-thirds of his day for himself, is a slave, whatever he may be: a statesman, a"
+ " businessman, an official, or a scholar.";
OfflineTtsKokoroModelConfig kokoroModelConfig =
OfflineTtsKokoroModelConfig.builder()
.setModel(model)
.setVoices(voices)
.setTokens(tokens)
.setLexicon(lexicon)
.build();
OfflineTtsModelConfig modelConfig =
OfflineTtsModelConfig.builder()
.setKokoro(kokoroModelConfig)
.setNumThreads(2)
.setDebug(true)
.build();
OfflineTtsConfig config = OfflineTtsConfig.builder().setModel(modelConfig).build();
OfflineTts tts = new OfflineTts(config);
int sid = 0;
float speed = 1.0f;
GenerationConfig genConfig = new GenerationConfig();
genConfig.setSid(sid);
genConfig.setSpeed(speed);
genConfig.setSilenceScale(0.2f);
long start = System.currentTimeMillis();
GeneratedAudio audio = tts.generateWithConfigAndCallback(text, genConfig, samples -> {});
long stop = System.currentTimeMillis();
float timeElapsedSeconds = (stop - start) / 1000.0f;
float audioDuration = audio.getSamples().length / (float) audio.getSampleRate();
float realTimeFactor = timeElapsedSeconds / audioDuration;
String waveFilename = "tts-kokoro-en-lexicon.wav";
audio.save(waveFilename);
System.out.printf("-- elapsed : %.3f seconds\n", timeElapsedSeconds);
System.out.printf("-- audio duration: %.3f seconds\n", audioDuration);
System.out.printf("-- real-time factor (RTF): %.3f\n", realTimeFactor);
System.out.printf("-- text: %s\n", text);
System.out.printf("-- Saved to %s\n", waveFilename);
tts.release();
}
}