mirror of
https://github.com/k2-fsa/sherpa-onnx.git
synced 2026-11-10 21:11:03 +08:00
This pull request enhances the Text-to-Speech capabilities by integrating the Supertonic TTS model into the existing Java and Kotlin APIs. It provides developers with new tools and clear examples to generate speech from text using this model, expanding the range of available TTS options and simplifying their adoption within Java and Kotlin projects.
63 lines
2.4 KiB
Java
63 lines
2.4 KiB
Java
// Copyright 2025 Xiaomi Corporation
|
|
|
|
// This file shows how to use a Kokoro multi-lingual model
|
|
// to convert Chinese and English text to speech
|
|
import com.k2fsa.sherpa.onnx.*;
|
|
|
|
public class NonStreamingTtsKokoroZhEn {
|
|
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-multi-lang-v1_0/model.onnx";
|
|
String voices = "./kokoro-multi-lang-v1_0/voices.bin";
|
|
String tokens = "./kokoro-multi-lang-v1_0/tokens.txt";
|
|
String dataDir = "./kokoro-multi-lang-v1_0/espeak-ng-data";
|
|
String lexicon =
|
|
"./kokoro-multi-lang-v1_0/lexicon-us-en.txt,./kokoro-multi-lang-v1_0/lexicon-zh.txt";
|
|
String text =
|
|
"中英文语音合成测试。This is generated by next generation Kaldi using Kokoro without Misaki."
|
|
+ " 你觉得中英文说的如何呢?";
|
|
|
|
OfflineTtsKokoroModelConfig kokoroModelConfig =
|
|
OfflineTtsKokoroModelConfig.builder()
|
|
.setModel(model)
|
|
.setVoices(voices)
|
|
.setTokens(tokens)
|
|
.setDataDir(dataDir)
|
|
.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; // this model has 53 speakers. You can use sid in the range 0-52
|
|
float speed = 1.0f;
|
|
long start = System.currentTimeMillis();
|
|
GeneratedAudio audio = tts.generate(text, sid, speed);
|
|
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-zh-en.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();
|
|
}
|
|
}
|