Digipom commited on
Commit
eb271b2
·
unverified ·
1 Parent(s): f31830b

whisper.android : add support for loading directly from asset in C (#415)

Browse files
examples/whisper.android/app/src/main/java/com/whispercppdemo/ui/main/MainScreenViewModel.kt CHANGED
@@ -73,8 +73,7 @@ class MainScreenViewModel(private val application: Application) : ViewModel() {
73
  printMessage("Loading model...\n")
74
  val models = application.assets.list("models/")
75
  if (models != null) {
76
- val inputstream = application.assets.open("models/" + models[0])
77
- whisperContext = WhisperContext.createContextFromInputStream(inputstream)
78
  printMessage("Loaded model ${models[0]}.\n")
79
  }
80
 
 
73
  printMessage("Loading model...\n")
74
  val models = application.assets.list("models/")
75
  if (models != null) {
76
+ whisperContext = WhisperContext.createContextFromAsset(application.assets, "models/" + models[0])
 
77
  printMessage("Loaded model ${models[0]}.\n")
78
  }
79
 
examples/whisper.android/app/src/main/java/com/whispercppdemo/whisper/LibWhisper.kt CHANGED
@@ -1,5 +1,6 @@
1
  package com.whispercppdemo.whisper
2
 
 
3
  import android.os.Build
4
  import android.util.Log
5
  import kotlinx.coroutines.*
@@ -56,6 +57,15 @@ class WhisperContext private constructor(private var ptr: Long) {
56
  }
57
  return WhisperContext(ptr)
58
  }
 
 
 
 
 
 
 
 
 
59
  }
60
  }
61
 
@@ -87,6 +97,7 @@ private class WhisperLib {
87
 
88
  // JNI methods
89
  external fun initContextFromInputStream(inputStream: InputStream): Long
 
90
  external fun initContext(modelPath: String): Long
91
  external fun freeContext(contextPtr: Long)
92
  external fun fullTranscribe(contextPtr: Long, audioData: FloatArray)
 
1
  package com.whispercppdemo.whisper
2
 
3
+ import android.content.res.AssetManager
4
  import android.os.Build
5
  import android.util.Log
6
  import kotlinx.coroutines.*
 
57
  }
58
  return WhisperContext(ptr)
59
  }
60
+
61
+ fun createContextFromAsset(assetManager: AssetManager, assetPath: String): WhisperContext {
62
+ val ptr = WhisperLib.initContextFromAsset(assetManager, assetPath)
63
+
64
+ if (ptr == 0L) {
65
+ throw java.lang.RuntimeException("Couldn't create context from asset $assetPath")
66
+ }
67
+ return WhisperContext(ptr)
68
+ }
69
  }
70
  }
71
 
 
97
 
98
  // JNI methods
99
  external fun initContextFromInputStream(inputStream: InputStream): Long
100
+ external fun initContextFromAsset(assetManager: AssetManager, assetPath: String): Long
101
  external fun initContext(modelPath: String): Long
102
  external fun freeContext(contextPtr: Long)
103
  external fun fullTranscribe(contextPtr: Long, audioData: FloatArray)
examples/whisper.android/app/src/main/jni/whisper/Whisper.mk CHANGED
@@ -1,5 +1,5 @@
1
  WHISPER_LIB_DIR := $(LOCAL_PATH)/../../../../../../../
2
- LOCAL_LDLIBS := -llog
3
 
4
  # Make the final output library smaller by only keeping the symbols referenced from the app.
5
  ifneq ($(APP_OPTIM),debug)
 
1
  WHISPER_LIB_DIR := $(LOCAL_PATH)/../../../../../../../
2
+ LOCAL_LDLIBS := -landroid -llog
3
 
4
  # Make the final output library smaller by only keeping the symbols referenced from the app.
5
  ifneq ($(APP_OPTIM),debug)
examples/whisper.android/app/src/main/jni/whisper/jni.c CHANGED
@@ -1,4 +1,6 @@
1
  #include <jni.h>
 
 
2
  #include <android/log.h>
3
  #include <stdlib.h>
4
  #include <sys/sysinfo.h>
@@ -9,6 +11,7 @@
9
  #define TAG "JNI"
10
 
11
  #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__)
 
12
 
13
  static inline int min(int a, int b) {
14
  return (a < b) ? a : b;
@@ -91,6 +94,52 @@ Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_initContextFromInputSt
91
  return (jlong) context;
92
  }
93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  JNIEXPORT jlong JNICALL
95
  Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_initContext(
96
  JNIEnv *env, jobject thiz, jstring model_path_str) {
 
1
  #include <jni.h>
2
+ #include <android/asset_manager.h>
3
+ #include <android/asset_manager_jni.h>
4
  #include <android/log.h>
5
  #include <stdlib.h>
6
  #include <sys/sysinfo.h>
 
11
  #define TAG "JNI"
12
 
13
  #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__)
14
+ #define LOGW(...) __android_log_print(ANDROID_LOG_WARN, TAG, __VA_ARGS__)
15
 
16
  static inline int min(int a, int b) {
17
  return (a < b) ? a : b;
 
94
  return (jlong) context;
95
  }
96
 
97
+ static size_t asset_read(void *ctx, void *output, size_t read_size) {
98
+ return AAsset_read((AAsset *) ctx, output, read_size);
99
+ }
100
+
101
+ static bool asset_is_eof(void *ctx) {
102
+ return AAsset_getRemainingLength64((AAsset *) ctx) <= 0;
103
+ }
104
+
105
+ static void asset_close(void *ctx) {
106
+ AAsset_close((AAsset *) ctx);
107
+ }
108
+
109
+ static struct whisper_context *whisper_init_from_asset(
110
+ JNIEnv *env,
111
+ jobject assetManager,
112
+ const char *asset_path
113
+ ) {
114
+ LOGI("Loading model from asset '%s'\n", asset_path);
115
+ AAssetManager *asset_manager = AAssetManager_fromJava(env, assetManager);
116
+ AAsset *asset = AAssetManager_open(asset_manager, asset_path, AASSET_MODE_STREAMING);
117
+ if (!asset) {
118
+ LOGW("Failed to open '%s'\n", asset_path);
119
+ return NULL;
120
+ }
121
+
122
+ whisper_model_loader loader = {
123
+ .context = asset,
124
+ .read = &asset_read,
125
+ .eof = &asset_is_eof,
126
+ .close = &asset_close
127
+ };
128
+
129
+ return whisper_init(&loader);
130
+ }
131
+
132
+ JNIEXPORT jlong JNICALL
133
+ Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_initContextFromAsset(
134
+ JNIEnv *env, jobject thiz, jobject assetManager, jstring asset_path_str) {
135
+ UNUSED(thiz);
136
+ struct whisper_context *context = NULL;
137
+ const char *asset_path_chars = (*env)->GetStringUTFChars(env, asset_path_str, NULL);
138
+ context = whisper_init_from_asset(env, assetManager, asset_path_chars);
139
+ (*env)->ReleaseStringUTFChars(env, asset_path_str, asset_path_chars);
140
+ return (jlong) context;
141
+ }
142
+
143
  JNIEXPORT jlong JNICALL
144
  Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_initContext(
145
  JNIEnv *env, jobject thiz, jstring model_path_str) {