From 033e936b8bb716d6e46972d9a4382762d791cfe2 Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Tue, 5 Sep 2023 14:53:44 +0800 Subject: [PATCH] Fix an Android bug. (#265) * Fix an Android bug. Recreate the stream after restart. * Release v2.1.2 --- CMakeLists.txt | 2 +- .../main/java/com/k2fsa/sherpa/ncnn/MainActivity.kt | 2 +- .../main/java/com/k2fsa/sherpa/ncnn/SherpaNcnn.kt | 6 +++--- sherpa-ncnn/jni/jni.cc | 12 +++++++++--- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 08082d8c..60b130f2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.13 FATAL_ERROR) project(sherpa-ncnn) -set(SHERPA_NCNN_VERSION "2.1.1") +set(SHERPA_NCNN_VERSION "2.1.2") # Disable warning about # diff --git a/android/SherpaNcnn/app/src/main/java/com/k2fsa/sherpa/ncnn/MainActivity.kt b/android/SherpaNcnn/app/src/main/java/com/k2fsa/sherpa/ncnn/MainActivity.kt index 28cdbddd..2b7d3712 100644 --- a/android/SherpaNcnn/app/src/main/java/com/k2fsa/sherpa/ncnn/MainActivity.kt +++ b/android/SherpaNcnn/app/src/main/java/com/k2fsa/sherpa/ncnn/MainActivity.kt @@ -92,7 +92,7 @@ class MainActivity : AppCompatActivity() { audioRecord!!.startRecording() recordButton.setText(R.string.stop) isRecording = true - model.reset() + model.reset(true) textView.text = "" lastText = "" idx = 0 diff --git a/android/SherpaNcnn/app/src/main/java/com/k2fsa/sherpa/ncnn/SherpaNcnn.kt b/android/SherpaNcnn/app/src/main/java/com/k2fsa/sherpa/ncnn/SherpaNcnn.kt index 594b5791..a47d3fa9 100644 --- a/android/SherpaNcnn/app/src/main/java/com/k2fsa/sherpa/ncnn/SherpaNcnn.kt +++ b/android/SherpaNcnn/app/src/main/java/com/k2fsa/sherpa/ncnn/SherpaNcnn.kt @@ -64,7 +64,7 @@ class SherpaNcnn( fun inputFinished() = inputFinished(ptr) fun isEndpoint(): Boolean = isEndpoint(ptr) - fun reset() = reset(ptr) + fun reset(recreate: Boolean = false) = reset(ptr, recreate = recreate) val text: String get() = getText(ptr) @@ -82,9 +82,9 @@ class SherpaNcnn( private external fun acceptWaveform(ptr: Long, samples: FloatArray, sampleRate: Float) private external fun inputFinished(ptr: Long) private external fun isReady(ptr: Long): Boolean - private external fun decode(ptr: Long): Boolean + private external fun decode(ptr: Long) private external fun isEndpoint(ptr: Long): Boolean - private external fun reset(ptr: Long): Boolean + private external fun reset(ptr: Long, recreate: Boolean) private external fun getText(ptr: Long): String companion object { diff --git a/sherpa-ncnn/jni/jni.cc b/sherpa-ncnn/jni/jni.cc index 74b39940..519de810 100644 --- a/sherpa-ncnn/jni/jni.cc +++ b/sherpa-ncnn/jni/jni.cc @@ -74,7 +74,13 @@ class SherpaNcnn { bool IsEndpoint() const { return recognizer_.IsEndpoint(stream_.get()); } - void Reset() { return recognizer_.Reset(stream_.get()); } + void Reset(bool recreate) { + if (recreate) { + stream_ = recognizer_.CreateStream(); + } else { + recognizer_.Reset(stream_.get()); + } + } private: Recognizer recognizer_; @@ -319,9 +325,9 @@ JNIEXPORT void JNICALL Java_com_k2fsa_sherpa_ncnn_SherpaNcnn_decode( SHERPA_EXTERN_C JNIEXPORT void JNICALL Java_com_k2fsa_sherpa_ncnn_SherpaNcnn_reset( - JNIEnv *env, jobject /*obj*/, jlong ptr) { + JNIEnv *env, jobject /*obj*/, jlong ptr, jboolean recreate) { auto model = reinterpret_cast(ptr); - model->Reset(); + model->Reset(recreate); } SHERPA_EXTERN_C