fix: Android Demo detect invalid when the button is pressed again (#142)

* fix: Android Demo detect invalid when the button is pressed again

* fix: Android demo detect invalid when the button is pressed again

---------

Co-authored-by: chuhonglin <chuhonglin@papegames.com>
This commit is contained in:
linlin 2023-08-31 11:49:01 +08:00 committed by GitHub
parent b233d46552
commit 694928d706
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 11 deletions

View File

@ -41,6 +41,7 @@ void reset(JNIEnv* env, jobject) {
offset = 0; offset = 0;
result = ""; result = "";
spotter->Reset(); spotter->Reset();
feature_pipeline->Reset();
} }
void accept_waveform(JNIEnv* env, jobject, jshortArray jWaveform) { 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); int16_t* waveform = env->GetShortArrayElements(jWaveform, 0);
std::vector<int16_t> v(waveform, waveform + size); std::vector<int16_t> v(waveform, waveform + size);
feature_pipeline->AcceptWaveform(v); feature_pipeline->AcceptWaveform(v);
env->ReleaseShortArrayElements(jWaveform, waveform, 0);
LOG(INFO) << "wekws accept waveform in ms: " << int(size / 16); LOG(INFO) << "wekws accept waveform in ms: " << int(size / 16);
} }
@ -56,8 +59,29 @@ void set_input_finished() {
feature_pipeline->set_input_finished(); feature_pipeline->set_input_finished();
} }
void spot_thread_func() { // void spot_thread_func() {
while (true) { // while (true) {
// std::vector<std::vector<float>> feats;
// feature_pipeline->Read(80, &feats);
// std::vector<std::vector<float>> 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<std::vector<float>> feats; std::vector<std::vector<float>> feats;
feature_pipeline->Read(80, &feats); feature_pipeline->Read(80, &feats);
std::vector<std::vector<float>> prob; std::vector<std::vector<float>> prob;
@ -70,12 +94,6 @@ void spot_thread_func() {
} }
result = std::to_string(offset) + " prob: " + std::to_string(max_prob); result = std::to_string(offset) + " prob: " + std::to_string(max_prob);
offset += prob.size(); offset += prob.size();
}
}
void start_spot() {
std::thread decode_thread(spot_thread_func);
decode_thread.detach();
} }
jstring get_result(JNIEnv* env, jobject) { jstring get_result(JNIEnv* env, jobject) {

View File

@ -101,12 +101,12 @@ public class MainActivity extends AppCompatActivity {
if (!startRecord) { if (!startRecord) {
startRecord = true; startRecord = true;
startRecordThread(); startRecordThread();
startAcceptWaveThread();
startSpotThread(); startSpotThread();
Spot.reset();
Spot.startSpot();
button.setText("Stop Record"); button.setText("Stop Record");
} else { } else {
startRecord = false; startRecord = false;
Spot.setInputFinished();
button.setText("Start Record"); button.setText("Start Record");
} }
}); });
@ -191,7 +191,7 @@ public class MainActivity extends AppCompatActivity {
return energy; return energy;
} }
private void startSpotThread() { private void startAcceptWaveThread() {
new Thread(() -> { new Thread(() -> {
// Send all data // Send all data
while (startRecord || bufferQueue.size() > 0) { while (startRecord || bufferQueue.size() > 0) {
@ -210,4 +210,15 @@ public class MainActivity extends AppCompatActivity {
} }
}).start(); }).start();
} }
private void startSpotThread() {
new Thread(() -> {
Spot.reset();
// Send all data
while (startRecord) {
Spot.startSpot();
Log.i(LOG_TAG, Spot.getResult());
}
}).start();
}
} }