OfflineTts.java
2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2024 Xiaomi Corporation
package com.k2fsa.sherpa.onnx;
public class OfflineTts {
static {
System.loadLibrary("sherpa-onnx-jni");
}
private long ptr = 0;
public OfflineTts(OfflineTtsConfig config) {
ptr = newFromFile(config);
}
public int getSampleRate() {
return getSampleRate(ptr);
}
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);
}
public GeneratedAudio generateWithCallback(String text, OfflineTtsCallback callback) {
return generateWithCallback(text, 0, 1.0f, callback);
}
public GeneratedAudio generateWithCallback(String text, int sid, OfflineTtsCallback callback) {
return generateWithCallback(text, sid, 1.0f, callback);
}
public GeneratedAudio generateWithCallback(String text, int sid, float speed, OfflineTtsCallback callback) {
Object[] arr = generateWithCallbackImpl(ptr, text, sid, speed, callback);
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 Object[] generateWithCallbackImpl(long ptr, String text, int sid, float speed, OfflineTtsCallback callback);
private native long newFromFile(OfflineTtsConfig config);
}