正在显示
15 个修改的文件
包含
78 行增加
和
49 行删除
java-api-examples/.gitignore
0 → 100644
| @@ -9,10 +9,11 @@ LIB_FILES = \ | @@ -9,10 +9,11 @@ LIB_FILES = \ | ||
| 9 | $(LIB_SRC_DIR)/OnlineLMConfig.java \ | 9 | $(LIB_SRC_DIR)/OnlineLMConfig.java \ |
| 10 | $(LIB_SRC_DIR)/OnlineTransducerModelConfig.java \ | 10 | $(LIB_SRC_DIR)/OnlineTransducerModelConfig.java \ |
| 11 | $(LIB_SRC_DIR)/OnlineParaformerModelConfig.java \ | 11 | $(LIB_SRC_DIR)/OnlineParaformerModelConfig.java \ |
| 12 | + $(LIB_SRC_DIR)/OnlineZipformer2CtcModelConfig.java \ | ||
| 12 | $(LIB_SRC_DIR)/OnlineModelConfig.java \ | 13 | $(LIB_SRC_DIR)/OnlineModelConfig.java \ |
| 13 | $(LIB_SRC_DIR)/OnlineRecognizerConfig.java \ | 14 | $(LIB_SRC_DIR)/OnlineRecognizerConfig.java \ |
| 14 | $(LIB_SRC_DIR)/OnlineStream.java \ | 15 | $(LIB_SRC_DIR)/OnlineStream.java \ |
| 15 | - $(LIB_SRC_DIR)/OnlineRecognizer.java \ | 16 | + $(LIB_SRC_DIR)/OnlineRecognizer.java |
| 16 | 17 | ||
| 17 | WEBSOCKET_DIR:= ./src/websocketsrv | 18 | WEBSOCKET_DIR:= ./src/websocketsrv |
| 18 | WEBSOCKET_FILES = \ | 19 | WEBSOCKET_FILES = \ |
| @@ -63,7 +64,7 @@ clean: | @@ -63,7 +64,7 @@ clean: | ||
| 63 | mkdir -p $(BUILD_DIR) | 64 | mkdir -p $(BUILD_DIR) |
| 64 | mkdir -p ./lib | 65 | mkdir -p ./lib |
| 65 | 66 | ||
| 66 | -runfile: | 67 | +runfile: packjar buildfile |
| 67 | java -cp ./lib/sherpaonnx.jar:build $(RUNJFLAGS) DecodeFile test.wav | 68 | java -cp ./lib/sherpaonnx.jar:build $(RUNJFLAGS) DecodeFile test.wav |
| 68 | 69 | ||
| 69 | runhotwords: | 70 | runhotwords: |
| @@ -85,7 +86,6 @@ buildlib: $(LIB_FILES:.java=.class) | @@ -85,7 +86,6 @@ buildlib: $(LIB_FILES:.java=.class) | ||
| 85 | 86 | ||
| 86 | 87 | ||
| 87 | %.class: %.java | 88 | %.class: %.java |
| 88 | - | ||
| 89 | $(JAVAC) -cp $(BUILD_DIR) -d $(BUILD_DIR) -encoding UTF-8 $< | 89 | $(JAVAC) -cp $(BUILD_DIR) -d $(BUILD_DIR) -encoding UTF-8 $< |
| 90 | 90 | ||
| 91 | buildwebsocket: $(WEBSOCKET_FILES:.java=.class) | 91 | buildwebsocket: $(WEBSOCKET_FILES:.java=.class) |
| @@ -95,7 +95,7 @@ buildwebsocket: $(WEBSOCKET_FILES:.java=.class) | @@ -95,7 +95,7 @@ buildwebsocket: $(WEBSOCKET_FILES:.java=.class) | ||
| 95 | 95 | ||
| 96 | $(JAVAC) -cp $(BUILD_DIR):lib/slf4j-simple-1.7.25.jar:lib/slf4j-api-1.7.25.jar:lib/Java-WebSocket-1.5.3.jar:../lib/sherpaonnx.jar -d $(BUILD_DIR) -encoding UTF-8 $< | 96 | $(JAVAC) -cp $(BUILD_DIR):lib/slf4j-simple-1.7.25.jar:lib/slf4j-api-1.7.25.jar:lib/Java-WebSocket-1.5.3.jar:../lib/sherpaonnx.jar -d $(BUILD_DIR) -encoding UTF-8 $< |
| 97 | 97 | ||
| 98 | -packjar: | 98 | +packjar: buildlib |
| 99 | jar cvfe lib/sherpaonnx.jar . -C $(BUILD_DIR) . | 99 | jar cvfe lib/sherpaonnx.jar . -C $(BUILD_DIR) . |
| 100 | 100 | ||
| 101 | all: clean buildlib packjar buildfile buildmic downjar buildwebsocket | 101 | all: clean buildlib packjar buildfile buildmic downjar buildwebsocket |
sherpa-onnx/java-api/.gitignore
0 → 100644
| @@ -7,6 +7,7 @@ package com.k2fsa.sherpa.onnx; | @@ -7,6 +7,7 @@ package com.k2fsa.sherpa.onnx; | ||
| 7 | public class OnlineModelConfig { | 7 | public class OnlineModelConfig { |
| 8 | private final OnlineParaformerModelConfig paraformer; | 8 | private final OnlineParaformerModelConfig paraformer; |
| 9 | private final OnlineTransducerModelConfig transducer; | 9 | private final OnlineTransducerModelConfig transducer; |
| 10 | + private final OnlineZipformer2CtcModelConfig zipformer2Ctc; | ||
| 10 | private final String tokens; | 11 | private final String tokens; |
| 11 | private final int numThreads; | 12 | private final int numThreads; |
| 12 | private final boolean debug; | 13 | private final boolean debug; |
| @@ -19,7 +20,9 @@ public class OnlineModelConfig { | @@ -19,7 +20,9 @@ public class OnlineModelConfig { | ||
| 19 | boolean debug, | 20 | boolean debug, |
| 20 | String modelType, | 21 | String modelType, |
| 21 | OnlineParaformerModelConfig paraformer, | 22 | OnlineParaformerModelConfig paraformer, |
| 22 | - OnlineTransducerModelConfig transducer) { | 23 | + OnlineTransducerModelConfig transducer, |
| 24 | + OnlineZipformer2CtcModelConfig zipformer2Ctc | ||
| 25 | + ) { | ||
| 23 | 26 | ||
| 24 | this.tokens = tokens; | 27 | this.tokens = tokens; |
| 25 | this.numThreads = numThreads; | 28 | this.numThreads = numThreads; |
| @@ -27,6 +30,7 @@ public class OnlineModelConfig { | @@ -27,6 +30,7 @@ public class OnlineModelConfig { | ||
| 27 | this.modelType = modelType; | 30 | this.modelType = modelType; |
| 28 | this.paraformer = paraformer; | 31 | this.paraformer = paraformer; |
| 29 | this.transducer = transducer; | 32 | this.transducer = transducer; |
| 33 | + this.zipformer2Ctc = zipformer2Ctc; | ||
| 30 | } | 34 | } |
| 31 | 35 | ||
| 32 | public OnlineParaformerModelConfig getParaformer() { | 36 | public OnlineParaformerModelConfig getParaformer() { |
| @@ -32,8 +32,14 @@ usage example: | @@ -32,8 +32,14 @@ usage example: | ||
| 32 | */ | 32 | */ |
| 33 | package com.k2fsa.sherpa.onnx; | 33 | package com.k2fsa.sherpa.onnx; |
| 34 | 34 | ||
| 35 | -import java.io.*; | ||
| 36 | -import java.util.*; | 35 | +import java.io.BufferedInputStream; |
| 36 | +import java.io.File; | ||
| 37 | +import java.io.FileInputStream; | ||
| 38 | +import java.io.InputStream; | ||
| 39 | +import java.util.Enumeration; | ||
| 40 | +import java.util.HashMap; | ||
| 41 | +import java.util.Map; | ||
| 42 | +import java.util.Properties; | ||
| 37 | 43 | ||
| 38 | public class OnlineRecognizer { | 44 | public class OnlineRecognizer { |
| 39 | private long ptr = 0; // this is the asr engine ptrss | 45 | private long ptr = 0; // this is the asr engine ptrss |
| @@ -71,6 +77,7 @@ public class OnlineRecognizer { | @@ -71,6 +77,7 @@ public class OnlineRecognizer { | ||
| 71 | proMap.getOrDefault("encoder", "").trim(), | 77 | proMap.getOrDefault("encoder", "").trim(), |
| 72 | proMap.getOrDefault("decoder", "").trim(), | 78 | proMap.getOrDefault("decoder", "").trim(), |
| 73 | proMap.getOrDefault("joiner", "").trim()); | 79 | proMap.getOrDefault("joiner", "").trim()); |
| 80 | + OnlineZipformer2CtcModelConfig zipformer2CtcConfig = new OnlineZipformer2CtcModelConfig(""); | ||
| 74 | OnlineModelConfig modelCfg = | 81 | OnlineModelConfig modelCfg = |
| 75 | new OnlineModelConfig( | 82 | new OnlineModelConfig( |
| 76 | proMap.getOrDefault("tokens", "").trim(), | 83 | proMap.getOrDefault("tokens", "").trim(), |
| @@ -78,7 +85,7 @@ public class OnlineRecognizer { | @@ -78,7 +85,7 @@ public class OnlineRecognizer { | ||
| 78 | false, | 85 | false, |
| 79 | proMap.getOrDefault("model_type", "zipformer").trim(), | 86 | proMap.getOrDefault("model_type", "zipformer").trim(), |
| 80 | modelParaCfg, | 87 | modelParaCfg, |
| 81 | - modelTranCfg); | 88 | + modelTranCfg, zipformer2CtcConfig); |
| 82 | FeatureConfig featConfig = | 89 | FeatureConfig featConfig = |
| 83 | new FeatureConfig( | 90 | new FeatureConfig( |
| 84 | sampleRate, Integer.parseInt(proMap.getOrDefault("feature_dim", "80").trim())); | 91 | sampleRate, Integer.parseInt(proMap.getOrDefault("feature_dim", "80").trim())); |
| @@ -136,6 +143,7 @@ public class OnlineRecognizer { | @@ -136,6 +143,7 @@ public class OnlineRecognizer { | ||
| 136 | proMap.getOrDefault("encoder", "").trim(), | 143 | proMap.getOrDefault("encoder", "").trim(), |
| 137 | proMap.getOrDefault("decoder", "").trim(), | 144 | proMap.getOrDefault("decoder", "").trim(), |
| 138 | proMap.getOrDefault("joiner", "").trim()); | 145 | proMap.getOrDefault("joiner", "").trim()); |
| 146 | + OnlineZipformer2CtcModelConfig zipformer2CtcConfig = new OnlineZipformer2CtcModelConfig(""); | ||
| 139 | 147 | ||
| 140 | OnlineModelConfig modelCfg = | 148 | OnlineModelConfig modelCfg = |
| 141 | new OnlineModelConfig( | 149 | new OnlineModelConfig( |
| @@ -144,7 +152,7 @@ public class OnlineRecognizer { | @@ -144,7 +152,7 @@ public class OnlineRecognizer { | ||
| 144 | false, | 152 | false, |
| 145 | proMap.getOrDefault("model_type", "zipformer").trim(), | 153 | proMap.getOrDefault("model_type", "zipformer").trim(), |
| 146 | modelParaCfg, | 154 | modelParaCfg, |
| 147 | - modelTranCfg); | 155 | + modelTranCfg, zipformer2CtcConfig); |
| 148 | FeatureConfig featConfig = | 156 | FeatureConfig featConfig = |
| 149 | new FeatureConfig( | 157 | new FeatureConfig( |
| 150 | sampleRate, Integer.parseInt(proMap.getOrDefault("feature_dim", "80").trim())); | 158 | sampleRate, Integer.parseInt(proMap.getOrDefault("feature_dim", "80").trim())); |
| @@ -201,8 +209,9 @@ public class OnlineRecognizer { | @@ -201,8 +209,9 @@ public class OnlineRecognizer { | ||
| 201 | OnlineParaformerModelConfig modelParaCfg = new OnlineParaformerModelConfig(encoder, decoder); | 209 | OnlineParaformerModelConfig modelParaCfg = new OnlineParaformerModelConfig(encoder, decoder); |
| 202 | OnlineTransducerModelConfig modelTranCfg = | 210 | OnlineTransducerModelConfig modelTranCfg = |
| 203 | new OnlineTransducerModelConfig(encoder, decoder, joiner); | 211 | new OnlineTransducerModelConfig(encoder, decoder, joiner); |
| 212 | + OnlineZipformer2CtcModelConfig zipformer2CtcConfig = new OnlineZipformer2CtcModelConfig(""); | ||
| 204 | OnlineModelConfig modelCfg = | 213 | OnlineModelConfig modelCfg = |
| 205 | - new OnlineModelConfig(tokens, numThreads, false, modelType, modelParaCfg, modelTranCfg); | 214 | + new OnlineModelConfig(tokens, numThreads, false, modelType, modelParaCfg, modelTranCfg, zipformer2CtcConfig); |
| 206 | FeatureConfig featConfig = new FeatureConfig(sampleRate, featureDim); | 215 | FeatureConfig featConfig = new FeatureConfig(sampleRate, featureDim); |
| 207 | OnlineLMConfig onlineLmConfig = new OnlineLMConfig(lm_model, lm_scale); | 216 | OnlineLMConfig onlineLmConfig = new OnlineLMConfig(lm_model, lm_scale); |
| 208 | OnlineRecognizerConfig rcgCfg = | 217 | OnlineRecognizerConfig rcgCfg = |
| @@ -220,6 +229,32 @@ public class OnlineRecognizer { | @@ -220,6 +229,32 @@ public class OnlineRecognizer { | ||
| 220 | this.ptr = createOnlineRecognizer(new Object(), rcgCfg); | 229 | this.ptr = createOnlineRecognizer(new Object(), rcgCfg); |
| 221 | } | 230 | } |
| 222 | 231 | ||
| 232 | + public static float[] readWavFile(String fileName) { | ||
| 233 | + // read data from the filename | ||
| 234 | + Object[] wavdata = readWave(fileName); | ||
| 235 | + Object data = wavdata[0]; // data[0] is float data, data[1] sample rate | ||
| 236 | + | ||
| 237 | + float[] floatData = (float[]) data; | ||
| 238 | + | ||
| 239 | + return floatData; | ||
| 240 | + } | ||
| 241 | + | ||
| 242 | + // load the libsherpa-onnx-jni.so lib | ||
| 243 | + public static void loadSoLib(String soPath) { | ||
| 244 | + // load libsherpa-onnx-jni.so lib from the path | ||
| 245 | + | ||
| 246 | + System.out.println("so lib path=" + soPath + "\n"); | ||
| 247 | + System.load(soPath.trim()); | ||
| 248 | + System.out.println("load so lib succeed\n"); | ||
| 249 | + } | ||
| 250 | + | ||
| 251 | + public static void setSoPath(String soPath) { | ||
| 252 | + OnlineRecognizer.loadSoLib(soPath); | ||
| 253 | + OnlineStream.loadSoLib(soPath); | ||
| 254 | + } | ||
| 255 | + | ||
| 256 | + private static native Object[] readWave(String fileName); // static | ||
| 257 | + | ||
| 223 | private Map<String, String> readProperties(String modelCfgPath) { | 258 | private Map<String, String> readProperties(String modelCfgPath) { |
| 224 | // read and parse config file | 259 | // read and parse config file |
| 225 | Properties props = new Properties(); | 260 | Properties props = new Properties(); |
| @@ -302,30 +337,6 @@ public class OnlineRecognizer { | @@ -302,30 +337,6 @@ public class OnlineRecognizer { | ||
| 302 | return stream; | 337 | return stream; |
| 303 | } | 338 | } |
| 304 | 339 | ||
| 305 | - public static float[] readWavFile(String fileName) { | ||
| 306 | - // read data from the filename | ||
| 307 | - Object[] wavdata = readWave(fileName); | ||
| 308 | - Object data = wavdata[0]; // data[0] is float data, data[1] sample rate | ||
| 309 | - | ||
| 310 | - float[] floatData = (float[]) data; | ||
| 311 | - | ||
| 312 | - return floatData; | ||
| 313 | - } | ||
| 314 | - | ||
| 315 | - // load the libsherpa-onnx-jni.so lib | ||
| 316 | - public static void loadSoLib(String soPath) { | ||
| 317 | - // load libsherpa-onnx-jni.so lib from the path | ||
| 318 | - | ||
| 319 | - System.out.println("so lib path=" + soPath + "\n"); | ||
| 320 | - System.load(soPath.trim()); | ||
| 321 | - System.out.println("load so lib succeed\n"); | ||
| 322 | - } | ||
| 323 | - | ||
| 324 | - public static void setSoPath(String soPath) { | ||
| 325 | - OnlineRecognizer.loadSoLib(soPath); | ||
| 326 | - OnlineStream.loadSoLib(soPath); | ||
| 327 | - } | ||
| 328 | - | ||
| 329 | protected void finalize() throws Throwable { | 340 | protected void finalize() throws Throwable { |
| 330 | release(); | 341 | release(); |
| 331 | } | 342 | } |
| @@ -337,15 +348,13 @@ public class OnlineRecognizer { | @@ -337,15 +348,13 @@ public class OnlineRecognizer { | ||
| 337 | this.ptr = 0; | 348 | this.ptr = 0; |
| 338 | } | 349 | } |
| 339 | 350 | ||
| 351 | + // JNI interface libsherpa-onnx-jni.so | ||
| 352 | + | ||
| 340 | // stream release, you'd better call it manually if not use anymore | 353 | // stream release, you'd better call it manually if not use anymore |
| 341 | public void releaseStream(OnlineStream s) { | 354 | public void releaseStream(OnlineStream s) { |
| 342 | s.release(); | 355 | s.release(); |
| 343 | } | 356 | } |
| 344 | 357 | ||
| 345 | - // JNI interface libsherpa-onnx-jni.so | ||
| 346 | - | ||
| 347 | - private static native Object[] readWave(String fileName); // static | ||
| 348 | - | ||
| 349 | private native String getResult(long ptr, long streamPtr); | 358 | private native String getResult(long ptr, long streamPtr); |
| 350 | 359 | ||
| 351 | private native void decodeStream(long ptr, long streamPtr); | 360 | private native void decodeStream(long ptr, long streamPtr); |
| @@ -4,19 +4,22 @@ | @@ -4,19 +4,22 @@ | ||
| 4 | // Stream is used for feeding data to the asr engine | 4 | // Stream is used for feeding data to the asr engine |
| 5 | package com.k2fsa.sherpa.onnx; | 5 | package com.k2fsa.sherpa.onnx; |
| 6 | 6 | ||
| 7 | -import java.io.*; | ||
| 8 | -import java.util.*; | ||
| 9 | - | ||
| 10 | public class OnlineStream { | 7 | public class OnlineStream { |
| 11 | private long ptr = 0; // this is the stream ptr | 8 | private long ptr = 0; // this is the stream ptr |
| 12 | 9 | ||
| 13 | private int sampleRate = 16000; | 10 | private int sampleRate = 16000; |
| 11 | + | ||
| 14 | // assign ptr to this stream in construction | 12 | // assign ptr to this stream in construction |
| 15 | public OnlineStream(long ptr, int sampleRate) { | 13 | public OnlineStream(long ptr, int sampleRate) { |
| 16 | this.ptr = ptr; | 14 | this.ptr = ptr; |
| 17 | this.sampleRate = sampleRate; | 15 | this.sampleRate = sampleRate; |
| 18 | } | 16 | } |
| 19 | 17 | ||
| 18 | + public static void loadSoLib(String soPath) { | ||
| 19 | + // load .so lib from the path | ||
| 20 | + System.load(soPath.trim()); // ("sherpa-onnx-jni-java"); | ||
| 21 | + } | ||
| 22 | + | ||
| 20 | public long getPtr() { | 23 | public long getPtr() { |
| 21 | return ptr; | 24 | return ptr; |
| 22 | } | 25 | } |
| @@ -31,18 +34,13 @@ public class OnlineStream { | @@ -31,18 +34,13 @@ public class OnlineStream { | ||
| 31 | public void inputFinished() { | 34 | public void inputFinished() { |
| 32 | // add some tail padding | 35 | // add some tail padding |
| 33 | int padLen = (int) (this.sampleRate * 0.3); // 0.3 seconds at 16 kHz sample rate | 36 | int padLen = (int) (this.sampleRate * 0.3); // 0.3 seconds at 16 kHz sample rate |
| 34 | - float tailPaddings[] = new float[padLen]; // default value is 0 | 37 | + float[] tailPaddings = new float[padLen]; // default value is 0 |
| 35 | acceptWaveform(this.ptr, this.sampleRate, tailPaddings); | 38 | acceptWaveform(this.ptr, this.sampleRate, tailPaddings); |
| 36 | 39 | ||
| 37 | // tell the engine all data are feeded | 40 | // tell the engine all data are feeded |
| 38 | inputFinished(this.ptr); | 41 | inputFinished(this.ptr); |
| 39 | } | 42 | } |
| 40 | 43 | ||
| 41 | - public static void loadSoLib(String soPath) { | ||
| 42 | - // load .so lib from the path | ||
| 43 | - System.load(soPath.trim()); // ("sherpa-onnx-jni-java"); | ||
| 44 | - } | ||
| 45 | - | ||
| 46 | public void release() { | 44 | public void release() { |
| 47 | // stream object must be release after used | 45 | // stream object must be release after used |
| 48 | if (this.ptr == 0) return; | 46 | if (this.ptr == 0) return; |
| @@ -1522,8 +1522,8 @@ JNIEXPORT void JNICALL Java_com_k2fsa_sherpa_onnx_SherpaOnnxOffline_delete( | @@ -1522,8 +1522,8 @@ JNIEXPORT void JNICALL Java_com_k2fsa_sherpa_onnx_SherpaOnnxOffline_delete( | ||
| 1522 | 1522 | ||
| 1523 | SHERPA_ONNX_EXTERN_C | 1523 | SHERPA_ONNX_EXTERN_C |
| 1524 | JNIEXPORT void JNICALL Java_com_k2fsa_sherpa_onnx_SherpaOnnx_reset( | 1524 | JNIEXPORT void JNICALL Java_com_k2fsa_sherpa_onnx_SherpaOnnx_reset( |
| 1525 | - JNIEnv *env, jobject /*obj*/, | ||
| 1526 | - jlong ptr, jboolean recreate, jstring keywords) { | 1525 | + JNIEnv *env, jobject /*obj*/, jlong ptr, jboolean recreate, |
| 1526 | + jstring keywords) { | ||
| 1527 | auto model = reinterpret_cast<sherpa_onnx::SherpaOnnx *>(ptr); | 1527 | auto model = reinterpret_cast<sherpa_onnx::SherpaOnnx *>(ptr); |
| 1528 | const char *p_keywords = env->GetStringUTFChars(keywords, nullptr); | 1528 | const char *p_keywords = env->GetStringUTFChars(keywords, nullptr); |
| 1529 | model->Reset(recreate, p_keywords); | 1529 | model->Reset(recreate, p_keywords); |
-
请 注册 或 登录 后发表评论