2024-04-24 18:41:48 +08:00
|
|
|
// Copyright 2022-2023 by zhaoming
|
|
|
|
|
// Copyright 2024 Xiaomi Corporation
|
2023-04-15 22:17:28 +08:00
|
|
|
|
2024-04-28 22:26:04 +08:00
|
|
|
package com.k2fsa.sherpa.onnx;
|
2023-04-15 22:17:28 +08:00
|
|
|
|
|
|
|
|
public class OnlineRecognizer {
|
2024-04-24 18:41:48 +08:00
|
|
|
static {
|
|
|
|
|
System.loadLibrary("sherpa-onnx-jni");
|
2023-04-15 22:17:28 +08:00
|
|
|
}
|
2024-02-26 13:49:37 +08:00
|
|
|
|
2024-04-29 21:23:56 +08:00
|
|
|
private long ptr = 0;
|
2024-02-26 13:49:37 +08:00
|
|
|
|
2024-04-24 18:41:48 +08:00
|
|
|
public OnlineRecognizer(OnlineRecognizerConfig config) {
|
|
|
|
|
ptr = newFromFile(config);
|
2023-04-15 22:17:28 +08:00
|
|
|
}
|
2024-02-26 13:49:37 +08:00
|
|
|
|
2024-04-24 18:41:48 +08:00
|
|
|
public void decode(OnlineStream s) {
|
|
|
|
|
decode(ptr, s.getPtr());
|
2024-02-26 13:49:37 +08:00
|
|
|
}
|
2023-04-15 22:17:28 +08:00
|
|
|
|
2024-04-24 18:41:48 +08:00
|
|
|
public boolean isReady(OnlineStream s) {
|
|
|
|
|
return isReady(ptr, s.getPtr());
|
2024-02-26 13:49:37 +08:00
|
|
|
}
|
2023-04-15 22:17:28 +08:00
|
|
|
|
2024-04-24 18:41:48 +08:00
|
|
|
public boolean isEndpoint(OnlineStream s) {
|
|
|
|
|
return isEndpoint(ptr, s.getPtr());
|
2024-02-26 13:49:37 +08:00
|
|
|
}
|
2023-04-15 22:17:28 +08:00
|
|
|
|
2024-04-24 18:41:48 +08:00
|
|
|
public void reset(OnlineStream s) {
|
|
|
|
|
reset(ptr, s.getPtr());
|
2024-02-26 13:49:37 +08:00
|
|
|
}
|
2023-04-15 22:17:28 +08:00
|
|
|
|
2024-04-24 18:41:48 +08:00
|
|
|
public OnlineStream createStream() {
|
|
|
|
|
long p = createStream(ptr, "");
|
|
|
|
|
return new OnlineStream(p);
|
2024-02-26 13:49:37 +08:00
|
|
|
}
|
2023-04-15 22:17:28 +08:00
|
|
|
|
2024-04-24 18:41:48 +08:00
|
|
|
@Override
|
2024-02-26 13:49:37 +08:00
|
|
|
protected void finalize() throws Throwable {
|
|
|
|
|
release();
|
|
|
|
|
}
|
2023-04-15 22:17:28 +08:00
|
|
|
|
2024-04-24 21:03:26 +08:00
|
|
|
// You'd better call it manually if it is not used anymore
|
2024-02-26 13:49:37 +08:00
|
|
|
public void release() {
|
2024-04-24 18:41:48 +08:00
|
|
|
if (this.ptr == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
delete(this.ptr);
|
2024-02-26 13:49:37 +08:00
|
|
|
this.ptr = 0;
|
|
|
|
|
}
|
2023-07-30 17:04:18 +08:00
|
|
|
|
2024-04-24 18:41:48 +08:00
|
|
|
public OnlineRecognizerResult getResult(OnlineStream s) {
|
|
|
|
|
Object[] arr = getResult(ptr, s.getPtr());
|
|
|
|
|
String text = (String) arr[0];
|
|
|
|
|
String[] tokens = (String[]) arr[1];
|
|
|
|
|
float[] timestamps = (float[]) arr[2];
|
|
|
|
|
return new OnlineRecognizerResult(text, tokens, timestamps);
|
2024-02-26 13:49:37 +08:00
|
|
|
}
|
2023-04-15 22:17:28 +08:00
|
|
|
|
|
|
|
|
|
2024-04-24 18:41:48 +08:00
|
|
|
private native void delete(long ptr);
|
2023-04-15 22:17:28 +08:00
|
|
|
|
2024-04-24 18:41:48 +08:00
|
|
|
private native long newFromFile(OnlineRecognizerConfig config);
|
2023-04-15 22:17:28 +08:00
|
|
|
|
2024-04-24 18:41:48 +08:00
|
|
|
private native long createStream(long ptr, String hotwords);
|
2023-04-15 22:17:28 +08:00
|
|
|
|
2024-04-24 18:41:48 +08:00
|
|
|
private native void reset(long ptr, long streamPtr);
|
2023-04-15 22:17:28 +08:00
|
|
|
|
2024-04-24 18:41:48 +08:00
|
|
|
private native void decode(long ptr, long streamPtr);
|
2023-04-15 22:17:28 +08:00
|
|
|
|
2024-02-26 13:49:37 +08:00
|
|
|
private native boolean isEndpoint(long ptr, long streamPtr);
|
2023-04-15 22:17:28 +08:00
|
|
|
|
2024-04-24 18:41:48 +08:00
|
|
|
private native boolean isReady(long ptr, long streamPtr);
|
|
|
|
|
|
|
|
|
|
private native Object[] getResult(long ptr, long streamPtr);
|
|
|
|
|
}
|