-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add Kotlin and Java API for ZipVoice models #3344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| // Copyright 2026 Xiaomi Corporation | ||
|
|
||
| // This file shows how to use a ZipVoice Chinese/English model | ||
| // for zero-shot text to speech. | ||
| import com.k2fsa.sherpa.onnx.*; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class ZipVoiceTts { | ||
| public static void main(String[] args) { | ||
| LibraryUtils.enableDebug(); | ||
| // please visit | ||
| // https://k2-fsa.github.io/sherpa/onnx/tts/zipvoice.html | ||
| // to download model files | ||
| String modelDir = "./sherpa-onnx-zipvoice-distill-int8-zh-en-emilia"; | ||
| String referenceAudioFilename = modelDir + "/test_wavs/leijun-1.wav"; | ||
| String text = "小米的价值观是真诚, 热爱. 真诚,就是不欺人也不自欺. 热爱, 就是全心投入并享受其中."; | ||
| String referenceText = "那还是三十六年前, 一九八七年. 我呢考上了武汉大学的计算机系."; | ||
|
|
||
| OfflineTtsZipVoiceModelConfig zipvoiceModelConfig = | ||
| OfflineTtsZipVoiceModelConfig.builder() | ||
| .setTokens(modelDir + "/tokens.txt") | ||
| .setEncoder(modelDir + "/encoder.int8.onnx") | ||
| .setDecoder(modelDir + "/decoder.int8.onnx") | ||
| .setVocoder("./vocos_24khz.onnx") | ||
| .setDataDir(modelDir + "/espeak-ng-data") | ||
| .setLexicon(modelDir + "/lexicon.txt") | ||
| .build(); | ||
|
|
||
| OfflineTtsModelConfig modelConfig = | ||
| OfflineTtsModelConfig.builder() | ||
| .setZipvoice(zipvoiceModelConfig) | ||
| .setNumThreads(2) | ||
| .setDebug(false) | ||
| .build(); | ||
|
|
||
| OfflineTtsConfig config = OfflineTtsConfig.builder().setModel(modelConfig).build(); | ||
| OfflineTts tts = new OfflineTts(config); | ||
|
|
||
| WaveReader reader = new WaveReader(referenceAudioFilename); | ||
|
|
||
| GenerationConfig genConfig = new GenerationConfig(); | ||
| genConfig.setReferenceAudio(reader.getSamples()); | ||
| genConfig.setReferenceSampleRate(reader.getSampleRate()); | ||
| genConfig.setReferenceText(referenceText); | ||
| genConfig.setNumSteps(4); | ||
|
|
||
| Map<String, String> extra = new HashMap<>(); | ||
| extra.put("min_char_in_sentence", "10"); | ||
| genConfig.setExtra(extra); | ||
|
|
||
| long start = System.currentTimeMillis(); | ||
| GeneratedAudio audio = | ||
| tts.generateWithConfigAndCallback( | ||
| text, | ||
| genConfig, | ||
| samples -> { | ||
| System.out.println("callback got called with " + samples.length + " samples"); | ||
| return 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 = "generated-zipvoice-zh-en-java.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(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| set -ex | ||
|
|
||
| if [[ ! -f ../build/lib/libsherpa-onnx-jni.dylib && ! -f ../build/lib/libsherpa-onnx-jni.so ]]; then | ||
| mkdir -p ../build | ||
| pushd ../build | ||
| cmake \ | ||
| -DSHERPA_ONNX_ENABLE_PYTHON=OFF \ | ||
| -DSHERPA_ONNX_ENABLE_TESTS=OFF \ | ||
| -DSHERPA_ONNX_ENABLE_CHECK=OFF \ | ||
| -DBUILD_SHARED_LIBS=ON \ | ||
| -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \ | ||
| -DSHERPA_ONNX_ENABLE_JNI=ON \ | ||
| .. | ||
|
|
||
| make -j4 | ||
| ls -lh lib | ||
| popd | ||
| fi | ||
|
|
||
| if [ ! -f ../sherpa-onnx/java-api/build/sherpa-onnx.jar ]; then | ||
| pushd ../sherpa-onnx/java-api | ||
| make | ||
| popd | ||
| fi | ||
|
|
||
| # please visit | ||
| # https://k2-fsa.github.io/sherpa/onnx/tts/zipvoice.html | ||
| # to download more models | ||
| if [ ! -f ./sherpa-onnx-zipvoice-distill-int8-zh-en-emilia/encoder.int8.onnx ]; then | ||
| curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/sherpa-onnx-zipvoice-distill-int8-zh-en-emilia.tar.bz2 | ||
| tar xf sherpa-onnx-zipvoice-distill-int8-zh-en-emilia.tar.bz2 | ||
| rm sherpa-onnx-zipvoice-distill-int8-zh-en-emilia.tar.bz2 | ||
| fi | ||
|
|
||
| if [ ! -f ./vocos_24khz.onnx ]; then | ||
| curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/vocoder-models/vocos_24khz.onnx | ||
| fi | ||
|
|
||
| java \ | ||
| -Djava.library.path=$PWD/../build/lib \ | ||
| -cp ../sherpa-onnx/java-api/build/sherpa-onnx.jar \ | ||
| ZipVoiceTts.java | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package com.k2fsa.sherpa.onnx | ||
|
|
||
| fun main() { | ||
| testZipVoiceTts() | ||
| } | ||
|
|
||
| fun testZipVoiceTts() { | ||
| val modelDir = "./sherpa-onnx-zipvoice-distill-int8-zh-en-emilia" | ||
| val referenceAudioFilename = "$modelDir/test_wavs/leijun-1.wav" | ||
| val wave = WaveReader.readWave(filename = referenceAudioFilename) | ||
|
|
||
| val config = OfflineTtsConfig( | ||
| model = OfflineTtsModelConfig( | ||
| zipvoice = OfflineTtsZipVoiceModelConfig( | ||
| tokens = "$modelDir/tokens.txt", | ||
| encoder = "$modelDir/encoder.int8.onnx", | ||
| decoder = "$modelDir/decoder.int8.onnx", | ||
| vocoder = "./vocos_24khz.onnx", | ||
| dataDir = "$modelDir/espeak-ng-data", | ||
| lexicon = "$modelDir/lexicon.txt", | ||
| ), | ||
| numThreads = 2, | ||
| debug = false, | ||
| ), | ||
| ) | ||
|
|
||
| val tts = OfflineTts(config = config) | ||
| val text = "小米的价值观是真诚, 热爱. 真诚,就是不欺人也不自欺. 热爱, 就是全心投入并享受其中." | ||
| val referenceText = "那还是三十六年前, 一九八七年. 我呢考上了武汉大学的计算机系." | ||
| val genConfig = GenerationConfig( | ||
| referenceAudio = wave.samples, | ||
| referenceSampleRate = wave.sampleRate, | ||
| referenceText = referenceText, | ||
| numSteps = 4, | ||
| extra = mapOf("min_char_in_sentence" to "10"), | ||
| ) | ||
|
|
||
| val audio = tts.generateWithConfigAndCallback(text = text, config = genConfig, callback = ::callback) | ||
| audio.save(filename = "test-zipvoice-zh-en.wav") | ||
| tts.release() | ||
| println("Saved to test-zipvoice-zh-en.wav") | ||
| } | ||
|
|
||
| fun callback(samples: FloatArray): Int { | ||
| println("callback got called with ${samples.size} samples") | ||
|
|
||
| // 1 means to continue | ||
| // 0 means to stop | ||
| return 1 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency with other scripts in the repository, such as
kotlin-api-examples/run.sh, it is recommended to usetar xvfinstead oftar xf. Thevflag provides verbose output, which can be helpful for debugging during file extraction.