KeywordSpotter.java
1.7 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
// Copyright 2024 Xiaomi Corporation
package com.k2fsa.sherpa.onnx;
public class KeywordSpotter {
static {
System.loadLibrary("sherpa-onnx-jni");
}
private long ptr = 0;
public KeywordSpotter(KeywordSpotterConfig config) {
ptr = newFromFile(config);
}
public OnlineStream createStream(String keywords) {
long p = createStream(ptr, keywords);
return new OnlineStream(p);
}
public OnlineStream createStream() {
long p = createStream(ptr, "");
return new OnlineStream(p);
}
public void decode(OnlineStream s) {
decode(ptr, s.getPtr());
}
public boolean isReady(OnlineStream s) {
return isReady(ptr, s.getPtr());
}
public KeywordSpotterResult getResult(OnlineStream s) {
Object[] arr = getResult(ptr, s.getPtr());
String keyword = (String) arr[0];
String[] tokens = (String[]) arr[1];
float[] timestamps = (float[]) arr[2];
return new KeywordSpotterResult(keyword, tokens, timestamps);
}
protected void finalize() throws Throwable {
release();
}
// You'd better call it manually if it is not used anymore
public void release() {
if (this.ptr == 0) {
return;
}
delete(this.ptr);
this.ptr = 0;
}
private native long newFromFile(KeywordSpotterConfig config);
private native void delete(long ptr);
private native long createStream(long ptr, String keywords);
private native void decode(long ptr, long streamPtr);
private native boolean isReady(long ptr, long streamPtr);
private native Object[] getResult(long ptr, long streamPtr);
}