mirror of
https://github.com/k2-fsa/sherpa-onnx.git
synced 2026-11-10 21:11:03 +08:00
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
69 lines
2.7 KiB
Java
69 lines
2.7 KiB
Java
// Copyright 2025 Xiaomi Corporation
|
|
|
|
// This file shows how to use a matcha Chinese TTS model
|
|
// to convert text to speech
|
|
import com.k2fsa.sherpa.onnx.*;
|
|
|
|
public class NonStreamingTtsMatchaZhLexicon {
|
|
public static void main(String[] args) {
|
|
// please visit
|
|
// https://k2-fsa.github.io/sherpa/onnx/tts/pretrained_models/matcha.html#matcha-icefall-zh-baker-chinese-1-female-speaker
|
|
// to download model files
|
|
String acousticModel = "./matcha-icefall-zh-baker/model-steps-3.onnx";
|
|
String vocoder = "./vocos-22khz-univ.onnx";
|
|
String tokens = "./matcha-icefall-zh-baker/tokens.txt";
|
|
String lexicon = "./matcha-icefall-zh-baker/lexicon.txt";
|
|
String ruleFsts =
|
|
"./matcha-icefall-zh-baker/phone.fst,./matcha-icefall-zh-baker/date.fst,./matcha-icefall-zh-baker/number.fst";
|
|
String text =
|
|
"某某银行的副行长和一些行政领导表示,他们去过长江"
|
|
+ "和长白山; 经济不断增长。"
|
|
+ "2024年12月31号,拨打110或者18920240511。"
|
|
+ "123456块钱。";
|
|
|
|
OfflineTtsMatchaModelConfig matchaModelConfig =
|
|
OfflineTtsMatchaModelConfig.builder()
|
|
.setAcousticModel(acousticModel)
|
|
.setVocoder(vocoder)
|
|
.setTokens(tokens)
|
|
.setLexicon(lexicon)
|
|
.build();
|
|
|
|
OfflineTtsModelConfig modelConfig =
|
|
OfflineTtsModelConfig.builder()
|
|
.setMatcha(matchaModelConfig)
|
|
.setNumThreads(1)
|
|
.setDebug(true)
|
|
.build();
|
|
|
|
OfflineTtsConfig config =
|
|
OfflineTtsConfig.builder().setModel(modelConfig).setRuleFsts(ruleFsts).build();
|
|
OfflineTts tts = new OfflineTts(config);
|
|
|
|
GenerationConfig genConfig = new GenerationConfig();
|
|
genConfig.setSid(0);
|
|
genConfig.setSpeed(1.0f);
|
|
genConfig.setSilenceScale(config.getSilenceScale());
|
|
|
|
long start = System.currentTimeMillis();
|
|
GeneratedAudio audio = tts.generateWithConfigAndCallback(text, genConfig, (float[] samples) -> 1);
|
|
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-matcha-zh-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("-- speaker ID: %d\n", genConfig.getSid());
|
|
System.out.printf("-- Saved to %s\n", waveFilename);
|
|
|
|
tts.release();
|
|
}
|
|
}
|