Skip to content

Commit

Permalink
Fix an Android bug. (#265)
Browse files Browse the repository at this point in the history
* Fix an Android bug.

Recreate the stream after restart.

* Release v2.1.2
  • Loading branch information
csukuangfj authored Sep 5, 2023
1 parent 8be3e08 commit 033e936
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
12 changes: 9 additions & 3 deletions sherpa-ncnn/jni/jni.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 033e936

Please sign in to comment.