Explorar el Código

Fix an Android bug. (#265)

* Fix an Android bug.

Recreate the stream after restart.

* Release v2.1.2
Fangjun Kuang hace 1 año
padre
commit
033e936b8b

+ 1 - 1
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
 #

+ 1 - 1
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

+ 3 - 3
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 {

+ 9 - 3
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<sherpa_ncnn::SherpaNcnn *>(ptr);
-  model->Reset();
+  model->Reset(recreate);
 }
 
 SHERPA_EXTERN_C