Add Java API for text-to-speech (#811)
This commit is contained in:
18
.github/workflows/run-java-test.yaml
vendored
18
.github/workflows/run-java-test.yaml
vendored
@@ -138,3 +138,21 @@ jobs:
|
||||
|
||||
./run-non-streaming-decode-file-nemo.sh
|
||||
rm -rf sherpa-onnx-nemo-*
|
||||
|
||||
- name: Run java test (Non-Streaming TTS)
|
||||
shell: bash
|
||||
run: |
|
||||
cd ./java-api-examples
|
||||
./run-non-streaming-tts-piper-en.sh
|
||||
rm -rf vits-piper-*
|
||||
|
||||
./run-non-streaming-tts-coqui-de.sh
|
||||
rm -rf vits-coqui-*
|
||||
|
||||
./run-non-streaming-tts-vits-zh.sh
|
||||
rm -rf vits-zh-*
|
||||
|
||||
- uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: tts-wav-files-${{ matrix.os }}
|
||||
path: java-api-examples/*.wav
|
||||
|
||||
50
java-api-examples/NonStreamingTtsCoquiDe.java
Normal file
50
java-api-examples/NonStreamingTtsCoquiDe.java
Normal file
@@ -0,0 +1,50 @@
|
||||
// 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 real_time_factor = 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", timeElapsedSeconds);
|
||||
System.out.printf("-- real-time factor (RTF): %.3f\n", real_time_factor);
|
||||
System.out.printf("-- text: %s\n", text);
|
||||
System.out.printf("-- Saved to %s\n", waveFilename);
|
||||
|
||||
tts.release();
|
||||
}
|
||||
}
|
||||
58
java-api-examples/NonStreamingTtsPiperEn.java
Normal file
58
java-api-examples/NonStreamingTtsPiperEn.java
Normal file
@@ -0,0 +1,58 @@
|
||||
// Copyright 2024 Xiaomi Corporation
|
||||
|
||||
// This file shows how to use a piper VITS English TTS model
|
||||
// to convert text to speech
|
||||
import com.k2fsa.sherpa.onnx.*;
|
||||
|
||||
public class NonStreamingTtsPiperEn {
|
||||
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-piper-en_GB-cori-medium/en_GB-cori-medium.onnx";
|
||||
String tokens = "./vits-piper-en_GB-cori-medium/tokens.txt";
|
||||
String dataDir = "./vits-piper-en_GB-cori-medium/espeak-ng-data";
|
||||
String text =
|
||||
"Today as always, men fall into two groups: slaves and free men. Whoever does not have"
|
||||
+ " two-thirds of his day for himself, is a slave, whatever he may be: a statesman, a"
|
||||
+ " businessman, an official, or a scholar.";
|
||||
|
||||
OfflineTtsVitsModelConfig vitsModelConfig =
|
||||
OfflineTtsVitsModelConfig.builder()
|
||||
.setModel(model)
|
||||
.setTokens(tokens)
|
||||
.setDataDir(dataDir)
|
||||
.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 real_time_factor = timeElapsedSeconds / audioDuration;
|
||||
|
||||
String waveFilename = "tts-piper-en.wav";
|
||||
audio.save(waveFilename);
|
||||
System.out.printf("-- elapsed : %.3f seconds\n", timeElapsedSeconds);
|
||||
System.out.printf("-- audio duration: %.3f seconds\n", timeElapsedSeconds);
|
||||
System.out.printf("-- real-time factor (RTF): %.3f\n", real_time_factor);
|
||||
System.out.printf("-- text: %s\n", text);
|
||||
System.out.printf("-- Saved to %s\n", waveFilename);
|
||||
|
||||
tts.release();
|
||||
}
|
||||
}
|
||||
64
java-api-examples/NonStreamingTtsVitsZh.java
Normal file
64
java-api-examples/NonStreamingTtsVitsZh.java
Normal file
@@ -0,0 +1,64 @@
|
||||
// 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 NonStreamingTtsPiperEn {
|
||||
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 dictDir = "./vits-zh-hf-fanchen-C/dict";
|
||||
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)
|
||||
.setDictDir(dictDir)
|
||||
.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;
|
||||
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 real_time_factor = timeElapsedSeconds / audioDuration;
|
||||
|
||||
String waveFilename = "tts-vits-zh.wav";
|
||||
audio.save(waveFilename);
|
||||
System.out.printf("-- elapsed : %.3f seconds\n", timeElapsedSeconds);
|
||||
System.out.printf("-- audio duration: %.3f seconds\n", timeElapsedSeconds);
|
||||
System.out.printf("-- real-time factor (RTF): %.3f\n", real_time_factor);
|
||||
System.out.printf("-- text: %s\n", text);
|
||||
System.out.printf("-- Saved to %s\n", waveFilename);
|
||||
|
||||
tts.release();
|
||||
}
|
||||
}
|
||||
@@ -21,3 +21,11 @@ This directory contains examples for the JAVA API of sherpa-onnx.
|
||||
./run-non-streaming-decode-file-whisper.sh
|
||||
./run-non-streaming-decode-file-nemo.sh
|
||||
```
|
||||
|
||||
## Non-Streaming text-to-speech
|
||||
|
||||
```bash
|
||||
./run-non-streaming-tts-piper-en.sh
|
||||
./run-non-streaming-tts-coqui-de.sh
|
||||
./run-non-streaming-tts-vits-zh.sh
|
||||
```
|
||||
|
||||
54
java-api-examples/run-non-streaming-tts-coqui-de.sh
Executable file
54
java-api-examples/run-non-streaming-tts-coqui-de.sh
Executable file
@@ -0,0 +1,54 @@
|
||||
#!/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
|
||||
|
||||
if [[ ! -f ../build/lib/libsherpa-onnx-jni.dylib && ! -f ../build/lib/libsherpa-onnx-jni.so ]]; then
|
||||
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
|
||||
fi
|
||||
|
||||
# please visit
|
||||
# https://github.com/k2-fsa/sherpa-onnx/releases/tag/tts-models
|
||||
# to download more models
|
||||
if [ ! -f ./vits-coqui-de-css10/tokens.txt ]; then
|
||||
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/vits-coqui-de-css10.tar.bz2
|
||||
tar xf vits-coqui-de-css10.tar.bz2
|
||||
rm vits-coqui-de-css10.tar.bz2
|
||||
fi
|
||||
|
||||
java \
|
||||
-Djava.library.path=$PWD/../build/lib \
|
||||
-cp ../sherpa-onnx/java-api/build/sherpa-onnx.jar \
|
||||
NonStreamingTtsCoquiDe.java
|
||||
54
java-api-examples/run-non-streaming-tts-piper-en.sh
Executable file
54
java-api-examples/run-non-streaming-tts-piper-en.sh
Executable file
@@ -0,0 +1,54 @@
|
||||
#!/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
|
||||
|
||||
if [[ ! -f ../build/lib/libsherpa-onnx-jni.dylib && ! -f ../build/lib/libsherpa-onnx-jni.so ]]; then
|
||||
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
|
||||
fi
|
||||
|
||||
# please visit
|
||||
# https://github.com/k2-fsa/sherpa-onnx/releases/tag/tts-models
|
||||
# to download more models
|
||||
if [ ! -f ./vits-piper-en_GB-cori-medium/tokens.txt ]; then
|
||||
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/vits-piper-en_GB-cori-medium.tar.bz2
|
||||
tar xf vits-piper-en_GB-cori-medium.tar.bz2
|
||||
rm vits-piper-en_GB-cori-medium.tar.bz2
|
||||
fi
|
||||
|
||||
java \
|
||||
-Djava.library.path=$PWD/../build/lib \
|
||||
-cp ../sherpa-onnx/java-api/build/sherpa-onnx.jar \
|
||||
NonStreamingTtsPiperEn.java
|
||||
54
java-api-examples/run-non-streaming-tts-vits-zh.sh
Executable file
54
java-api-examples/run-non-streaming-tts-vits-zh.sh
Executable file
@@ -0,0 +1,54 @@
|
||||
#!/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
|
||||
|
||||
if [[ ! -f ../build/lib/libsherpa-onnx-jni.dylib && ! -f ../build/lib/libsherpa-onnx-jni.so ]]; then
|
||||
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
|
||||
fi
|
||||
|
||||
# please visit
|
||||
# https://github.com/k2-fsa/sherpa-onnx/releases/tag/tts-models
|
||||
# to download more models
|
||||
if [ ! -f ./vits-zh-hf-fanchen-C/tokens.txt ]; then
|
||||
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/vits-zh-hf-fanchen-C.tar.bz2
|
||||
tar xf vits-zh-hf-fanchen-C.tar.bz2
|
||||
rm vits-zh-hf-fanchen-C.tar.bz2
|
||||
fi
|
||||
|
||||
java \
|
||||
-Djava.library.path=$PWD/../build/lib \
|
||||
-cp ../sherpa-onnx/java-api/build/sherpa-onnx.jar \
|
||||
NonStreamingTtsVitsZh.java
|
||||
@@ -30,6 +30,12 @@ java_files += OfflineRecognizerResult.java
|
||||
java_files += OfflineStream.java
|
||||
java_files += OfflineRecognizer.java
|
||||
|
||||
java_files += OfflineTtsVitsModelConfig.java
|
||||
java_files += OfflineTtsModelConfig.java
|
||||
java_files += OfflineTtsConfig.java
|
||||
java_files += GeneratedAudio.java
|
||||
java_files += OfflineTts.java
|
||||
|
||||
class_files := $(java_files:%.java=%.class)
|
||||
|
||||
java_files := $(addprefix src/$(package_dir)/,$(java_files))
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
// Copyright 2024 Xiaomi Corporation
|
||||
|
||||
package com.k2fsa.sherpa.onnx;
|
||||
|
||||
public class GeneratedAudio {
|
||||
static {
|
||||
System.loadLibrary("sherpa-onnx-jni");
|
||||
}
|
||||
|
||||
private final float[] samples;
|
||||
private final int sampleRate;
|
||||
|
||||
public GeneratedAudio(float[] samples, int sampleRate) {
|
||||
this.samples = samples;
|
||||
this.sampleRate = sampleRate;
|
||||
}
|
||||
|
||||
public int getSampleRate() {
|
||||
return sampleRate;
|
||||
}
|
||||
|
||||
public float[] getSamples() {
|
||||
return samples;
|
||||
}
|
||||
|
||||
// return true if saved successfully.
|
||||
public boolean save(String filename) {
|
||||
return saveImpl(filename, samples, sampleRate);
|
||||
}
|
||||
|
||||
private native boolean saveImpl(String filename, float[] samples, int sampleRate);
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
// Copyright 2024 Xiaomi Corporation
|
||||
|
||||
package com.k2fsa.sherpa.onnx;
|
||||
|
||||
public class OfflineTts {
|
||||
static {
|
||||
System.loadLibrary("sherpa-onnx-jni");
|
||||
}
|
||||
|
||||
private long ptr = 0; // this is the asr engine ptrss
|
||||
|
||||
public OfflineTts(OfflineTtsConfig config) {
|
||||
ptr = newFromFile(config);
|
||||
}
|
||||
|
||||
public GeneratedAudio generate(String text) {
|
||||
return generate(text, 0, 1.0f);
|
||||
}
|
||||
|
||||
public GeneratedAudio generate(String text, int sid) {
|
||||
return generate(text, sid, 1.0f);
|
||||
}
|
||||
|
||||
public GeneratedAudio generate(String text, int sid, float speed) {
|
||||
Object[] arr = generateImpl(ptr, text, sid, speed);
|
||||
float[] samples = (float[]) arr[0];
|
||||
int sampleRate = (int) arr[1];
|
||||
return new GeneratedAudio(samples, sampleRate);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
release();
|
||||
}
|
||||
|
||||
public void release() {
|
||||
if (this.ptr == 0) {
|
||||
return;
|
||||
}
|
||||
delete(this.ptr);
|
||||
this.ptr = 0;
|
||||
}
|
||||
|
||||
private native void delete(long ptr);
|
||||
|
||||
private native int getSampleRate(long ptr);
|
||||
|
||||
private native int getNumSpeakers(long ptr);
|
||||
|
||||
private native Object[] generateImpl(long ptr, String text, int sid, float speed);
|
||||
|
||||
private native long newFromFile(OfflineTtsConfig config);
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
// Copyright 2024 Xiaomi Corporation
|
||||
|
||||
package com.k2fsa.sherpa.onnx;
|
||||
|
||||
public class OfflineTtsConfig {
|
||||
private final OfflineTtsModelConfig model;
|
||||
private final String ruleFsts;
|
||||
private final String ruleFars;
|
||||
private final int maxNumSentences;
|
||||
|
||||
private OfflineTtsConfig(Builder builder) {
|
||||
this.model = builder.model;
|
||||
this.ruleFsts = builder.ruleFsts;
|
||||
this.ruleFars = builder.ruleFars;
|
||||
this.maxNumSentences = builder.maxNumSentences;
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public OfflineTtsModelConfig getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public String getRuleFsts() {
|
||||
return ruleFsts;
|
||||
}
|
||||
|
||||
public String getRuleFars() {
|
||||
return ruleFars;
|
||||
}
|
||||
|
||||
public int getMaxNumSentences() {
|
||||
return maxNumSentences;
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private OfflineTtsModelConfig model = OfflineTtsModelConfig.builder().build();
|
||||
private String ruleFsts = "";
|
||||
private String ruleFars = "";
|
||||
private int maxNumSentences = 1;
|
||||
|
||||
public OfflineTtsConfig build() {
|
||||
return new OfflineTtsConfig(this);
|
||||
}
|
||||
|
||||
public Builder setModel(OfflineTtsModelConfig model) {
|
||||
this.model = model;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setRuleFsts(String ruleFsts) {
|
||||
this.ruleFsts = ruleFsts;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setRuleFars(String ruleFars) {
|
||||
this.ruleFars = ruleFars;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setMaxNumSentences(int maxNumSentences) {
|
||||
this.maxNumSentences = maxNumSentences;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
// Copyright 2024 Xiaomi Corporation
|
||||
|
||||
package com.k2fsa.sherpa.onnx;
|
||||
|
||||
public class OfflineTtsModelConfig {
|
||||
private final OfflineTtsVitsModelConfig vits;
|
||||
private final int numThreads;
|
||||
private final boolean debug;
|
||||
private final String provider;
|
||||
|
||||
private OfflineTtsModelConfig(Builder builder) {
|
||||
this.vits = builder.vits;
|
||||
this.numThreads = builder.numThreads;
|
||||
this.debug = builder.debug;
|
||||
this.provider = builder.provider;
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public OfflineTtsVitsModelConfig getVits() {
|
||||
return vits;
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private OfflineTtsVitsModelConfig vits = OfflineTtsVitsModelConfig.builder().build();
|
||||
private int numThreads = 1;
|
||||
private boolean debug = true;
|
||||
private String provider = "cpu";
|
||||
|
||||
public OfflineTtsModelConfig build() {
|
||||
return new OfflineTtsModelConfig(this);
|
||||
}
|
||||
|
||||
public Builder setVits(OfflineTtsVitsModelConfig vits) {
|
||||
this.vits = vits;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setNumThreads(int numThreads) {
|
||||
this.numThreads = numThreads;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setDebug(boolean debug) {
|
||||
this.debug = debug;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setProvider(String provider) {
|
||||
this.provider = provider;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
// Copyright 2024 Xiaomi Corporation
|
||||
|
||||
package com.k2fsa.sherpa.onnx;
|
||||
|
||||
public class OfflineTtsVitsModelConfig {
|
||||
private final String model;
|
||||
private final String lexicon;
|
||||
private final String tokens;
|
||||
private final String dataDir;
|
||||
private final String dictDir;
|
||||
private final float noiseScale;
|
||||
private final float noiseScaleW;
|
||||
private final float lengthScale;
|
||||
|
||||
private OfflineTtsVitsModelConfig(Builder builder) {
|
||||
this.model = builder.model;
|
||||
this.lexicon = builder.lexicon;
|
||||
this.tokens = builder.tokens;
|
||||
this.dataDir = builder.dataDir;
|
||||
this.dictDir = builder.dictDir;
|
||||
this.noiseScale = builder.noiseScale;
|
||||
this.noiseScaleW = builder.noiseScaleW;
|
||||
this.lengthScale = builder.lengthScale;
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public String getLexicon() {
|
||||
return lexicon;
|
||||
}
|
||||
|
||||
public String getTokens() {
|
||||
return tokens;
|
||||
}
|
||||
|
||||
public String getDataDir() {
|
||||
return dataDir;
|
||||
}
|
||||
|
||||
public String getDictDir() {
|
||||
return dictDir;
|
||||
}
|
||||
|
||||
public float getLengthScale() {
|
||||
return lengthScale;
|
||||
}
|
||||
|
||||
public float getNoiseScale() {
|
||||
return noiseScale;
|
||||
}
|
||||
|
||||
public float getNoiseScaleW() {
|
||||
return noiseScaleW;
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private String model;
|
||||
private String lexicon = "";
|
||||
private String tokens;
|
||||
private String dataDir = "";
|
||||
private String dictDir = "";
|
||||
private float noiseScale = 0.667f;
|
||||
private float noiseScaleW = 0.8f;
|
||||
private float lengthScale = 1.0f;
|
||||
|
||||
public OfflineTtsVitsModelConfig build() {
|
||||
return new OfflineTtsVitsModelConfig(this);
|
||||
}
|
||||
|
||||
public Builder setModel(String model) {
|
||||
this.model = model;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setTokens(String tokens) {
|
||||
this.tokens = tokens;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setLexicon(String lexicon) {
|
||||
this.lexicon = lexicon;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setDataDir(String dataDir) {
|
||||
this.dataDir = dataDir;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setDictDir(String dictDir) {
|
||||
this.dictDir = dictDir;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setNoiseScale(float noiseScale) {
|
||||
this.noiseScale = noiseScale;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setNoiseScaleW(float noiseScaleW) {
|
||||
this.noiseScaleW = noiseScaleW;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setLengthScale(float lengthScale) {
|
||||
this.lengthScale = lengthScale;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
// Copyright 2024 Xiaomi Corporation
|
||||
|
||||
package com.k2fsa.sherpa.onnx;
|
||||
|
||||
public class WaveReader {
|
||||
|
||||
Reference in New Issue
Block a user