mirror of
https://github.com/k2-fsa/sherpa-onnx.git
synced 2026-11-11 05:20:52 +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
68 lines
2.5 KiB
Java
68 lines
2.5 KiB
Java
// Copyright 2024 Xiaomi Corporation
|
|
|
|
// This file shows how to use a VITS Chinese TTS model
|
|
// to convert text to speech.
|
|
//
|
|
// You can use https://github.com/Plachtaa/VITS-fast-fine-tuning
|
|
// to train your model
|
|
import com.k2fsa.sherpa.onnx.*;
|
|
|
|
public class NonStreamingTtsVitsZhLexicon {
|
|
public static void main(String[] args) {
|
|
// please visit
|
|
// https://github.com/k2-fsa/sherpa-onnx/releases/tag/tts-models
|
|
// to download model files
|
|
String model = "./vits-zh-hf-fanchen-C/vits-zh-hf-fanchen-C.onnx";
|
|
String tokens = "./vits-zh-hf-fanchen-C/tokens.txt";
|
|
String lexicon = "./vits-zh-hf-fanchen-C/lexicon.txt";
|
|
String ruleFsts =
|
|
"./vits-zh-hf-fanchen-C/phone.fst,./vits-zh-hf-fanchen-C/date.fst,./vits-zh-hf-fanchen-C/number.fst";
|
|
String text = "有问题,请拨打110或者手机18601239876。我们的价值观是真诚热爱!";
|
|
|
|
OfflineTtsVitsModelConfig vitsModelConfig =
|
|
OfflineTtsVitsModelConfig.builder()
|
|
.setModel(model)
|
|
.setTokens(tokens)
|
|
.setLexicon(lexicon)
|
|
.build();
|
|
|
|
OfflineTtsModelConfig modelConfig =
|
|
OfflineTtsModelConfig.builder()
|
|
.setVits(vitsModelConfig)
|
|
.setNumThreads(1)
|
|
.setDebug(true)
|
|
.build();
|
|
|
|
OfflineTtsConfig config =
|
|
OfflineTtsConfig.builder().setModel(modelConfig).setRuleFsts(ruleFsts).build();
|
|
|
|
OfflineTts tts = new OfflineTts(config);
|
|
|
|
int sid = 100;
|
|
float speed = 1.0f;
|
|
GenerationConfig genConfig = new GenerationConfig();
|
|
genConfig.setSid(sid);
|
|
genConfig.setSpeed(speed);
|
|
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-vits-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("-- Saved to %s\n", waveFilename);
|
|
|
|
tts.release();
|
|
}
|
|
}
|