Add Java API for text-to-speech (#811)
This commit is contained in:
@@ -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