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

JNI Exception Handling #1452

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 16 additions & 5 deletions sherpa-onnx/jni/offline-recognizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,22 @@ Java_com_k2fsa_sherpa_onnx_OfflineRecognizer_createStream(JNIEnv * /*env*/,

SHERPA_ONNX_EXTERN_C
JNIEXPORT void JNICALL Java_com_k2fsa_sherpa_onnx_OfflineRecognizer_decode(
JNIEnv * /*env*/, jobject /*obj*/, jlong ptr, jlong streamPtr) {
auto recognizer = reinterpret_cast<sherpa_onnx::OfflineRecognizer *>(ptr);
auto stream = reinterpret_cast<sherpa_onnx::OfflineStream *>(streamPtr);

recognizer->DecodeStream(stream);
JNIEnv *env, jobject /*obj*/, jlong ptr, jlong streamPtr) {
try {
auto recognizer = reinterpret_cast<sherpa_onnx::OfflineRecognizer *>(ptr);
auto stream = reinterpret_cast<sherpa_onnx::OfflineStream *>(streamPtr);
recognizer->DecodeStream(stream);
} catch (const std::exception& e) {
jclass exClass = env->FindClass("java/lang/RuntimeException");
if (exClass != nullptr) {
env->ThrowNew(exClass, e.what());
}
} catch (...) {
jclass exClass = env->FindClass("java/lang/RuntimeException");
if (exClass != nullptr) {
env->ThrowNew(exClass, "Native exception: caught unknown exception");
}
}
}

SHERPA_ONNX_EXTERN_C
Expand Down
46 changes: 35 additions & 11 deletions sherpa-onnx/jni/voice-activity-detector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,26 @@ JNIEXPORT void JNICALL Java_com_k2fsa_sherpa_onnx_Vad_delete(JNIEnv * /*env*/,
SHERPA_ONNX_EXTERN_C
JNIEXPORT void JNICALL Java_com_k2fsa_sherpa_onnx_Vad_acceptWaveform(
JNIEnv *env, jobject /*obj*/, jlong ptr, jfloatArray samples) {
auto model = reinterpret_cast<sherpa_onnx::VoiceActivityDetector *>(ptr);

jfloat *p = env->GetFloatArrayElements(samples, nullptr);
jsize n = env->GetArrayLength(samples);

model->AcceptWaveform(p, n);

env->ReleaseFloatArrayElements(samples, p, JNI_ABORT);
try {
auto model = reinterpret_cast<sherpa_onnx::VoiceActivityDetector *>(ptr);

jfloat *p = env->GetFloatArrayElements(samples, nullptr);
jsize n = env->GetArrayLength(samples);

model->AcceptWaveform(p, n);

env->ReleaseFloatArrayElements(samples, p, JNI_ABORT);
} catch (const std::exception& e) {
jclass exClass = env->FindClass("java/lang/RuntimeException");
if (exClass != nullptr) {
env->ThrowNew(exClass, e.what());
}
} catch (...) {
jclass exClass = env->FindClass("java/lang/RuntimeException");
if (exClass != nullptr) {
env->ThrowNew(exClass, "Native exception: caught unknown exception");
}
}
}

SHERPA_ONNX_EXTERN_C
Expand Down Expand Up @@ -173,11 +185,23 @@ JNIEXPORT bool JNICALL Java_com_k2fsa_sherpa_onnx_Vad_isSpeechDetected(
}

SHERPA_ONNX_EXTERN_C
JNIEXPORT void JNICALL Java_com_k2fsa_sherpa_onnx_Vad_reset(JNIEnv * /*env*/,
JNIEXPORT void JNICALL Java_com_k2fsa_sherpa_onnx_Vad_reset(JNIEnv *env,
jobject /*obj*/,
jlong ptr) {
auto model = reinterpret_cast<sherpa_onnx::VoiceActivityDetector *>(ptr);
model->Reset();
try {
auto model = reinterpret_cast<sherpa_onnx::VoiceActivityDetector *>(ptr);
model->Reset();
} catch (const std::exception& e) {
jclass exClass = env->FindClass("java/lang/RuntimeException");
if (exClass != nullptr) {
env->ThrowNew(exClass, e.what());
}
} catch (...) {
jclass exClass = env->FindClass("java/lang/RuntimeException");
if (exClass != nullptr) {
env->ThrowNew(exClass, "Native exception: caught unknown exception");
}
}
}

SHERPA_ONNX_EXTERN_C
Expand Down