Skip to content

Commit

Permalink
feat(android): migrate from deprecated API in jni.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
jhen0409 committed Dec 5, 2023
1 parent ff19b49 commit 43a4ae2
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions android/src/main/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ static void input_stream_close(void *ctx) {

static struct whisper_context *whisper_init_from_input_stream(
JNIEnv *env,
jobject input_stream // PushbackInputStream
jobject input_stream, // PushbackInputStream
struct whisper_context_params cparams
) {
input_stream_context *context = new input_stream_context;
context->env = env;
Expand All @@ -109,7 +110,7 @@ static struct whisper_context *whisper_init_from_input_stream(
.eof = &input_stream_is_eof,
.close = &input_stream_close
};
return whisper_init(&loader);
return whisper_init_with_params(&loader, cparams);
}

// Load model from asset
Expand All @@ -128,7 +129,8 @@ static void asset_close(void *ctx) {
static struct whisper_context *whisper_init_from_asset(
JNIEnv *env,
jobject assetManager,
const char *asset_path
const char *asset_path,
struct whisper_context_params cparams
) {
LOGI("Loading model from asset '%s'\n", asset_path);
AAssetManager *asset_manager = AAssetManager_fromJava(env, assetManager);
Expand All @@ -143,7 +145,7 @@ static struct whisper_context *whisper_init_from_asset(
.eof = &asset_is_eof,
.close = &asset_close
};
return whisper_init(&loader);
return whisper_init_with_params(&loader, cparams);
}

extern "C" {
Expand All @@ -152,9 +154,10 @@ JNIEXPORT jlong JNICALL
Java_com_rnwhisper_WhisperContext_initContext(
JNIEnv *env, jobject thiz, jstring model_path_str) {
UNUSED(thiz);
struct whisper_context_params cparams;
struct whisper_context *context = nullptr;
const char *model_path_chars = env->GetStringUTFChars(model_path_str, nullptr);
context = whisper_init_from_file(model_path_chars);
context = whisper_init_from_file_with_params(model_path_chars, cparams);
env->ReleaseStringUTFChars(model_path_str, model_path_chars);
return reinterpret_cast<jlong>(context);
}
Expand All @@ -167,9 +170,10 @@ Java_com_rnwhisper_WhisperContext_initContextWithAsset(
jstring model_path_str
) {
UNUSED(thiz);
struct whisper_context_params cparams;
struct whisper_context *context = nullptr;
const char *model_path_chars = env->GetStringUTFChars(model_path_str, nullptr);
context = whisper_init_from_asset(env, asset_manager, model_path_chars);
context = whisper_init_from_asset(env, asset_manager, model_path_chars, cparams);
env->ReleaseStringUTFChars(model_path_str, model_path_chars);
return reinterpret_cast<jlong>(context);
}
Expand All @@ -181,8 +185,9 @@ Java_com_rnwhisper_WhisperContext_initContextWithInputStream(
jobject input_stream
) {
UNUSED(thiz);
struct whisper_context_params cparams;
struct whisper_context *context = nullptr;
context = whisper_init_from_input_stream(env, input_stream);
context = whisper_init_from_input_stream(env, input_stream, cparams);
return reinterpret_cast<jlong>(context);
}

Expand Down

0 comments on commit 43a4ae2

Please sign in to comment.