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.
51 lines
1.8 KiB
Java
51 lines
1.8 KiB
Java
// Copyright 2024 Xiaomi Corporation
|
|
|
|
// This file shows how to use a Coqui-ai VITS German TTS model
|
|
// to convert text to speech
|
|
import com.k2fsa.sherpa.onnx.*;
|
|
|
|
public class NonStreamingTtsCoquiDe {
|
|
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-coqui-de-css10/model.onnx";
|
|
String tokens = "./vits-coqui-de-css10/tokens.txt";
|
|
String text = "Alles hat ein Ende, nur die Wurst hat zwei.";
|
|
|
|
OfflineTtsVitsModelConfig vitsModelConfig =
|
|
OfflineTtsVitsModelConfig.builder().setModel(model).setTokens(tokens).build();
|
|
|
|
OfflineTtsModelConfig modelConfig =
|
|
OfflineTtsModelConfig.builder()
|
|
.setVits(vitsModelConfig)
|
|
.setNumThreads(1)
|
|
.setDebug(true)
|
|
.build();
|
|
|
|
OfflineTtsConfig config = OfflineTtsConfig.builder().setModel(modelConfig).build();
|
|
OfflineTts tts = new OfflineTts(config);
|
|
|
|
int sid = 0;
|
|
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-coqui-de.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();
|
|
}
|
|
}
|