Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
Loading