[android] add build.gradle and rename model name (#88)
This commit is contained in:
parent
9f29e033aa
commit
508938f537
@ -1,119 +1,119 @@
|
|||||||
// Copyright (c) 2022 Zhendong Peng (pzd17@tsinghua.org.cn)
|
// Copyright (c) 2022 Zhendong Peng (pzd17@tsinghua.org.cn)
|
||||||
//
|
//
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
// you may not use this file except in compliance with the License.
|
// you may not use this file except in compliance with the License.
|
||||||
// You may obtain a copy of the License at
|
// You may obtain a copy of the License at
|
||||||
//
|
//
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
//
|
//
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
#include <jni.h>
|
#include <jni.h>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
#include "frontend/feature_pipeline.h"
|
#include "frontend/feature_pipeline.h"
|
||||||
#include "kws/keyword_spotting.h"
|
#include "kws/keyword_spotting.h"
|
||||||
#include "utils/log.h"
|
#include "utils/log.h"
|
||||||
|
|
||||||
namespace wekws {
|
namespace wekws {
|
||||||
std::shared_ptr<KeywordSpotting> spotter;
|
std::shared_ptr<KeywordSpotting> spotter;
|
||||||
std::shared_ptr<wenet::FeaturePipelineConfig> feature_config;
|
std::shared_ptr<wenet::FeaturePipelineConfig> feature_config;
|
||||||
std::shared_ptr<wenet::FeaturePipeline> feature_pipeline;
|
std::shared_ptr<wenet::FeaturePipeline> feature_pipeline;
|
||||||
std::string result; // NOLINT
|
std::string result; // NOLINT
|
||||||
int offset;
|
int offset;
|
||||||
|
|
||||||
void init(JNIEnv* env, jobject, jstring jModelDir) {
|
void init(JNIEnv* env, jobject, jstring jModelDir) {
|
||||||
const char* pModelDir = env->GetStringUTFChars(jModelDir, nullptr);
|
const char* pModelDir = env->GetStringUTFChars(jModelDir, nullptr);
|
||||||
|
|
||||||
std::string modelPath = std::string(pModelDir) + "/wenwen.ort";
|
std::string modelPath = std::string(pModelDir) + "/kws.ort";
|
||||||
spotter = std::make_shared<KeywordSpotting>(modelPath);
|
spotter = std::make_shared<KeywordSpotting>(modelPath);
|
||||||
|
|
||||||
feature_config = std::make_shared<wenet::FeaturePipelineConfig>(40, 16000);
|
feature_config = std::make_shared<wenet::FeaturePipelineConfig>(40, 16000);
|
||||||
feature_pipeline = std::make_shared<wenet::FeaturePipeline>(*feature_config);
|
feature_pipeline = std::make_shared<wenet::FeaturePipeline>(*feature_config);
|
||||||
}
|
}
|
||||||
|
|
||||||
void reset(JNIEnv *env, jobject) {
|
void reset(JNIEnv* env, jobject) {
|
||||||
offset = 0;
|
offset = 0;
|
||||||
result = "";
|
result = "";
|
||||||
spotter->Reset();
|
spotter->Reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
void accept_waveform(JNIEnv *env, jobject, jshortArray jWaveform) {
|
void accept_waveform(JNIEnv* env, jobject, jshortArray jWaveform) {
|
||||||
jsize size = env->GetArrayLength(jWaveform);
|
jsize size = env->GetArrayLength(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);
|
||||||
LOG(INFO) << "wekws accept waveform in ms: " << int(size / 16);
|
LOG(INFO) << "wekws accept waveform in ms: " << int(size / 16);
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_input_finished() {
|
void set_input_finished() {
|
||||||
LOG(INFO) << "wekws input finished";
|
LOG(INFO) << "wekws 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;
|
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;
|
||||||
spotter->Forward(feats, &prob);
|
spotter->Forward(feats, &prob);
|
||||||
|
|
||||||
float max_hi_xiaowen = 0;
|
float max_hi_xiaowen = 0;
|
||||||
float max_nihao_wenwen = 0;
|
float max_nihao_wenwen = 0;
|
||||||
for (int t = 0; t < prob.size(); t++) {
|
for (int t = 0; t < prob.size(); t++) {
|
||||||
max_hi_xiaowen = std::max(prob[t][0], max_hi_xiaowen);
|
max_hi_xiaowen = std::max(prob[t][0], max_hi_xiaowen);
|
||||||
max_nihao_wenwen = std::max(prob[t][1], max_nihao_wenwen);
|
max_nihao_wenwen = std::max(prob[t][1], max_nihao_wenwen);
|
||||||
}
|
}
|
||||||
float max_prob = max_hi_xiaowen + max_nihao_wenwen;
|
float max_prob = max_hi_xiaowen + max_nihao_wenwen;
|
||||||
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() {
|
void start_spot() {
|
||||||
std::thread decode_thread(spot_thread_func);
|
std::thread decode_thread(spot_thread_func);
|
||||||
decode_thread.detach();
|
decode_thread.detach();
|
||||||
}
|
}
|
||||||
|
|
||||||
jstring get_result(JNIEnv *env, jobject) {
|
jstring get_result(JNIEnv* env, jobject) {
|
||||||
LOG(INFO) << "wekws ui result: " << result;
|
LOG(INFO) << "wekws ui result: " << result;
|
||||||
return env->NewStringUTF(result.c_str());
|
return env->NewStringUTF(result.c_str());
|
||||||
}
|
}
|
||||||
} // namespace wekws
|
} // namespace wekws
|
||||||
|
|
||||||
JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *) {
|
JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void*) {
|
||||||
JNIEnv *env;
|
JNIEnv* env;
|
||||||
if (vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6) != JNI_OK) {
|
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
|
||||||
return JNI_ERR;
|
return JNI_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
jclass c = env->FindClass("cn/org/wenet/wekws/Spot");
|
jclass c = env->FindClass("cn/org/wenet/wekws/Spot");
|
||||||
if (c == nullptr) {
|
if (c == nullptr) {
|
||||||
return JNI_ERR;
|
return JNI_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const JNINativeMethod methods[] = {
|
static const JNINativeMethod methods[] = {
|
||||||
{"init", "(Ljava/lang/String;)V", reinterpret_cast<void*>(wekws::init)},
|
{"init", "(Ljava/lang/String;)V", reinterpret_cast<void*>(wekws::init)},
|
||||||
{"reset", "()V", reinterpret_cast<void *>(wekws::reset)},
|
{"reset", "()V", reinterpret_cast<void*>(wekws::reset)},
|
||||||
{"acceptWaveform", "([S)V",
|
{"acceptWaveform", "([S)V",
|
||||||
reinterpret_cast<void *>(wekws::accept_waveform)},
|
reinterpret_cast<void*>(wekws::accept_waveform)},
|
||||||
{"setInputFinished", "()V",
|
{"setInputFinished", "()V",
|
||||||
reinterpret_cast<void *>(wekws::set_input_finished)},
|
reinterpret_cast<void*>(wekws::set_input_finished)},
|
||||||
{"startSpot", "()V", reinterpret_cast<void *>(wekws::start_spot)},
|
{"startSpot", "()V", reinterpret_cast<void*>(wekws::start_spot)},
|
||||||
{"getResult", "()Ljava/lang/String;",
|
{"getResult", "()Ljava/lang/String;",
|
||||||
reinterpret_cast<void *>(wekws::get_result)},
|
reinterpret_cast<void*>(wekws::get_result)},
|
||||||
};
|
};
|
||||||
int rc = env->RegisterNatives(c, methods,
|
int rc = env->RegisterNatives(c, methods,
|
||||||
sizeof(methods) / sizeof(JNINativeMethod));
|
sizeof(methods) / sizeof(JNINativeMethod));
|
||||||
|
|
||||||
if (rc != JNI_OK) {
|
if (rc != JNI_OK) {
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
return JNI_VERSION_1_6;
|
return JNI_VERSION_1_6;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -34,7 +34,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
private static final String LOG_TAG = "WEKWS";
|
private static final String LOG_TAG = "WEKWS";
|
||||||
private static final int SAMPLE_RATE = 16000; // The sampling rate
|
private static final int SAMPLE_RATE = 16000; // The sampling rate
|
||||||
private static final int MAX_QUEUE_SIZE = 2500; // 100 seconds audio, 1 / 0.04 * 100
|
private static final int MAX_QUEUE_SIZE = 2500; // 100 seconds audio, 1 / 0.04 * 100
|
||||||
private static final List<String> resource = Arrays.asList("wenwen.ort");
|
private static final List<String> resource = Arrays.asList("kws.ort");
|
||||||
|
|
||||||
private boolean startRecord = false;
|
private boolean startRecord = false;
|
||||||
private AudioRecord record = null;
|
private AudioRecord record = null;
|
||||||
|
|||||||
9
runtime/android/build.gradle
Normal file
9
runtime/android/build.gradle
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
||||||
|
plugins {
|
||||||
|
id 'com.android.application' version '7.2.2' apply false
|
||||||
|
id 'com.android.library' version '7.2.2' apply false
|
||||||
|
}
|
||||||
|
|
||||||
|
task clean(type: Delete) {
|
||||||
|
delete rootProject.buildDir
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user