Skip to content

Commit

Permalink
fix: Android Demo detect invalid when the button is pressed again (#142)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
HonglinChu and chuhonglin authored Aug 31, 2023
1 parent b233d46 commit 694928d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
34 changes: 26 additions & 8 deletions runtime/android/app/src/main/cpp/wekws.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@ void reset(JNIEnv* env, jobject) {
offset = 0;
result = "";
spotter->Reset();
feature_pipeline->Reset();
}

void accept_waveform(JNIEnv* env, jobject, jshortArray jWaveform) {
jsize size = env->GetArrayLength(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);
}

Expand All @@ -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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
});
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
}
}

0 comments on commit 694928d

Please sign in to comment.