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;
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<int16_t> 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<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;
feature_pipeline->Read(80, &feats);
std::vector<std::vector<float>> prob;
@ -71,12 +95,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) {
LOG(INFO) << "wekws ui result: " << result;

View File

@ -101,12 +101,12 @@ public class MainActivity extends AppCompatActivity {
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 @@ public class MainActivity extends AppCompatActivity {
return energy;
}
private void startSpotThread() {
private void startAcceptWaveThread() {
new Thread(() -> {
// Send all data
while (startRecord || bufferQueue.size() > 0) {
@ -210,4 +210,15 @@ public class MainActivity extends AppCompatActivity {
}
}).start();
}
private void startSpotThread() {
new Thread(() -> {
Spot.reset();
// Send all data
while (startRecord) {
Spot.startSpot();
Log.i(LOG_TAG, Spot.getResult());
}
}).start();
}
}