From 630e9da0c118f779171656635caa39715a06f721 Mon Sep 17 00:00:00 2001 From: chuhonglin Date: Wed, 23 Aug 2023 17:14:47 +0800 Subject: [PATCH 1/2] fix: Android Demo detect invalid when the button is pressed again --- runtime/android/app/src/main/cpp/wekws.cc | 34 ++++++++++++++----- .../java/cn/org/wenet/wekws/MainActivity.java | 17 ++++++++-- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/runtime/android/app/src/main/cpp/wekws.cc b/runtime/android/app/src/main/cpp/wekws.cc index d1dba2a..26f55d0 100644 --- a/runtime/android/app/src/main/cpp/wekws.cc +++ b/runtime/android/app/src/main/cpp/wekws.cc @@ -41,6 +41,7 @@ void reset(JNIEnv* env, jobject) { offset = 0; result = ""; spotter->Reset(); + feature_pipeline->Reset(); } void accept_waveform(JNIEnv* env, jobject, jshortArray jWaveform) { @@ -48,6 +49,8 @@ void accept_waveform(JNIEnv* env, jobject, jshortArray jWaveform) { int16_t* waveform = env->GetShortArrayElements(jWaveform, 0); std::vector v(waveform, waveform + size); feature_pipeline->AcceptWaveform(v); + env->ReleaseShortArrayElements(jWaveform, waveform,0); + LOG(INFO) << "wekws accept waveform in ms: " << int(size / 16); } @@ -56,8 +59,29 @@ void set_input_finished() { feature_pipeline->set_input_finished(); } -void spot_thread_func() { - while (true) { +// void spot_thread_func() { +// while (true) { +// std::vector> feats; +// feature_pipeline->Read(80, &feats); +// std::vector> prob; +// spotter->Forward(feats, &prob); +// float max_prob = 0.0; +// for (int t = 0; t < prob.size(); t++) { +// for (int j = 0; j < prob[t].size(); j++) { +// max_prob = std::max(prob[t][j], max_prob); +// } +// } +// result = std::to_string(offset) + " prob: " + std::to_string(max_prob); +// offset += prob.size(); +// } +// } + +// void start_spot() { +// std::thread decode_thread(spot_thread_func); +// decode_thread.detach(); +// } + +void start_spot(){ std::vector> feats; feature_pipeline->Read(80, &feats); std::vector> prob; @@ -70,12 +94,6 @@ void spot_thread_func() { } result = std::to_string(offset) + " prob: " + std::to_string(max_prob); offset += prob.size(); - } -} - -void start_spot() { - std::thread decode_thread(spot_thread_func); - decode_thread.detach(); } jstring get_result(JNIEnv* env, jobject) { diff --git a/runtime/android/app/src/main/java/cn/org/wenet/wekws/MainActivity.java b/runtime/android/app/src/main/java/cn/org/wenet/wekws/MainActivity.java index c3dc31c..862d0a7 100644 --- a/runtime/android/app/src/main/java/cn/org/wenet/wekws/MainActivity.java +++ b/runtime/android/app/src/main/java/cn/org/wenet/wekws/MainActivity.java @@ -101,12 +101,12 @@ protected void onCreate(Bundle savedInstanceState) { if (!startRecord) { startRecord = true; startRecordThread(); + startAcceptWaveThread(); startSpotThread(); - Spot.reset(); - Spot.startSpot(); button.setText("Stop Record"); } else { startRecord = false; + Spot.setInputFinished(); button.setText("Start Record"); } }); @@ -191,7 +191,7 @@ private double calculateDb(short[] buffer) { return energy; } - private void startSpotThread() { + private void startAcceptWaveThread() { new Thread(() -> { // Send all data while (startRecord || bufferQueue.size() > 0) { @@ -210,4 +210,15 @@ private void startSpotThread() { } }).start(); } + + private void startSpotThread() { + new Thread(() -> { + Spot.reset(); + // Send all data + while (startRecord) { + Spot.startSpot(); + Log.i(LOG_TAG, Spot.getResult()); + } + }).start(); + } } From 2437b2511ac5928be43b271fae16343dfe6cf0a6 Mon Sep 17 00:00:00 2001 From: chuhonglin Date: Wed, 23 Aug 2023 17:30:33 +0800 Subject: [PATCH 2/2] fix: Android demo detect invalid when the button is pressed again --- runtime/android/app/src/main/cpp/wekws.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/runtime/android/app/src/main/cpp/wekws.cc b/runtime/android/app/src/main/cpp/wekws.cc index 26f55d0..18ea8e6 100644 --- a/runtime/android/app/src/main/cpp/wekws.cc +++ b/runtime/android/app/src/main/cpp/wekws.cc @@ -49,7 +49,7 @@ void accept_waveform(JNIEnv* env, jobject, jshortArray jWaveform) { int16_t* waveform = env->GetShortArrayElements(jWaveform, 0); std::vector v(waveform, waveform + size); feature_pipeline->AcceptWaveform(v); - env->ReleaseShortArrayElements(jWaveform, waveform,0); + env->ReleaseShortArrayElements(jWaveform, waveform, 0); LOG(INFO) << "wekws accept waveform in ms: " << int(size / 16); } @@ -81,7 +81,7 @@ void set_input_finished() { // decode_thread.detach(); // } -void start_spot(){ +void start_spot() { std::vector> feats; feature_pipeline->Read(80, &feats); std::vector> prob;