tobrun commited on
Commit
9f860c0
·
unverified ·
1 Parent(s): f0f722d

android : decouple example into a library and app module (#1445)

Browse files
examples/whisper.android/app/build.gradle CHANGED
@@ -18,9 +18,7 @@ android {
18
  vectorDrawables {
19
  useSupportLibrary true
20
  }
21
- ndk {
22
- abiFilters 'arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64'
23
- }
24
  }
25
 
26
  buildTypes {
@@ -43,20 +41,10 @@ android {
43
  composeOptions {
44
  kotlinCompilerExtensionVersion '1.5.0'
45
  }
46
- ndkVersion "25.2.9519653"
47
- externalNativeBuild {
48
- cmake {
49
- path = file("src/main/jni/whisper/CMakeLists.txt")
50
- }
51
- }
52
- packagingOptions {
53
- resources {
54
- excludes += '/META-INF/{AL2.0,LGPL2.1}'
55
- }
56
- }
57
  }
58
 
59
  dependencies {
 
60
  implementation 'androidx.activity:activity-compose:1.7.2'
61
  implementation 'androidx.compose.material:material-icons-core:1.5.0'
62
  implementation 'androidx.compose.material3:material3:1.1.1'
 
18
  vectorDrawables {
19
  useSupportLibrary true
20
  }
21
+
 
 
22
  }
23
 
24
  buildTypes {
 
41
  composeOptions {
42
  kotlinCompilerExtensionVersion '1.5.0'
43
  }
 
 
 
 
 
 
 
 
 
 
 
44
  }
45
 
46
  dependencies {
47
+ implementation project(':lib')
48
  implementation 'androidx.activity:activity-compose:1.7.2'
49
  implementation 'androidx.compose.material:material-icons-core:1.5.0'
50
  implementation 'androidx.compose.material3:material3:1.1.1'
examples/whisper.android/app/src/main/java/com/whispercppdemo/ui/main/MainScreenViewModel.kt CHANGED
@@ -15,7 +15,7 @@ import androidx.lifecycle.viewmodel.initializer
15
  import androidx.lifecycle.viewmodel.viewModelFactory
16
  import com.whispercppdemo.media.decodeWaveFile
17
  import com.whispercppdemo.recorder.Recorder
18
- import com.whispercppdemo.whisper.WhisperContext
19
  import kotlinx.coroutines.Dispatchers
20
  import kotlinx.coroutines.launch
21
  import kotlinx.coroutines.runBlocking
@@ -35,7 +35,7 @@ class MainScreenViewModel(private val application: Application) : ViewModel() {
35
  private val modelsPath = File(application.filesDir, "models")
36
  private val samplesPath = File(application.filesDir, "samples")
37
  private var recorder: Recorder = Recorder()
38
- private var whisperContext: WhisperContext? = null
39
  private var mediaPlayer: MediaPlayer? = null
40
  private var recordedFile: File? = null
41
 
@@ -47,7 +47,7 @@ class MainScreenViewModel(private val application: Application) : ViewModel() {
47
  }
48
 
49
  private suspend fun printSystemInfo() {
50
- printMessage(String.format("System Info: %s\n", WhisperContext.getSystemInfo()))
51
  }
52
 
53
  private suspend fun loadData() {
@@ -78,7 +78,7 @@ class MainScreenViewModel(private val application: Application) : ViewModel() {
78
  printMessage("Loading model...\n")
79
  val models = application.assets.list("models/")
80
  if (models != null) {
81
- whisperContext = WhisperContext.createContextFromAsset(application.assets, "models/" + models[0])
82
  printMessage("Loaded model ${models[0]}.\n")
83
  }
84
 
 
15
  import androidx.lifecycle.viewmodel.viewModelFactory
16
  import com.whispercppdemo.media.decodeWaveFile
17
  import com.whispercppdemo.recorder.Recorder
18
+ import com.whispercpp.whisper.WhisperContext
19
  import kotlinx.coroutines.Dispatchers
20
  import kotlinx.coroutines.launch
21
  import kotlinx.coroutines.runBlocking
 
35
  private val modelsPath = File(application.filesDir, "models")
36
  private val samplesPath = File(application.filesDir, "samples")
37
  private var recorder: Recorder = Recorder()
38
+ private var whisperContext: com.whispercpp.whisper.WhisperContext? = null
39
  private var mediaPlayer: MediaPlayer? = null
40
  private var recordedFile: File? = null
41
 
 
47
  }
48
 
49
  private suspend fun printSystemInfo() {
50
+ printMessage(String.format("System Info: %s\n", com.whispercpp.whisper.WhisperContext.getSystemInfo()))
51
  }
52
 
53
  private suspend fun loadData() {
 
78
  printMessage("Loading model...\n")
79
  val models = application.assets.list("models/")
80
  if (models != null) {
81
+ whisperContext = com.whispercpp.whisper.WhisperContext.createContextFromAsset(application.assets, "models/" + models[0])
82
  printMessage("Loaded model ${models[0]}.\n")
83
  }
84
 
examples/whisper.android/lib/.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ /build
examples/whisper.android/lib/build.gradle ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ plugins {
2
+ id 'com.android.library'
3
+ id 'org.jetbrains.kotlin.android'
4
+ }
5
+
6
+ android {
7
+ namespace 'com.whispercpp'
8
+ compileSdk 34
9
+
10
+ defaultConfig {
11
+ minSdk 26
12
+ targetSdk 34
13
+ versionCode 1
14
+ versionName "1.0"
15
+
16
+ ndk {
17
+ abiFilters 'arm64-v8a', 'armeabi-v7a', 'x86', 'x86_64'
18
+ }
19
+ }
20
+
21
+ buildTypes {
22
+ release {
23
+ minifyEnabled false
24
+ }
25
+ }
26
+ compileOptions {
27
+ sourceCompatibility JavaVersion.VERSION_1_8
28
+ targetCompatibility JavaVersion.VERSION_1_8
29
+ }
30
+ kotlinOptions {
31
+ jvmTarget = '1.8'
32
+ }
33
+
34
+ ndkVersion "25.2.9519653"
35
+ externalNativeBuild {
36
+ cmake {
37
+ path = file("src/main/jni/whisper/CMakeLists.txt")
38
+ }
39
+ }
40
+ packagingOptions {
41
+ resources {
42
+ excludes += '/META-INF/{AL2.0,LGPL2.1}'
43
+ }
44
+ }
45
+ }
46
+
47
+ dependencies {
48
+ implementation 'androidx.core:core-ktx:1.9.0'
49
+ implementation 'androidx.appcompat:appcompat:1.6.1'
50
+ implementation 'com.google.android.material:material:1.8.0'
51
+ }
examples/whisper.android/lib/src/main/AndroidManifest.xml ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android">
3
+
4
+ </manifest>
examples/whisper.android/{app/src/main/java/com/whispercppdemo → lib/src/main/java/com/whispercpp}/whisper/LibWhisper.kt RENAMED
@@ -1,4 +1,4 @@
1
- package com.whispercppdemo.whisper
2
 
3
  import android.content.res.AssetManager
4
  import android.os.Build
 
1
+ package com.whispercpp.whisper
2
 
3
  import android.content.res.AssetManager
4
  import android.os.Build
examples/whisper.android/{app/src/main/java/com/whispercppdemo → lib/src/main/java/com/whispercpp}/whisper/WhisperCpuConfig.kt RENAMED
@@ -1,4 +1,4 @@
1
- package com.whispercppdemo.whisper
2
 
3
  import android.util.Log
4
  import java.io.BufferedReader
 
1
+ package com.whispercpp.whisper
2
 
3
  import android.util.Log
4
  import java.io.BufferedReader
examples/whisper.android/{app → lib}/src/main/jni/whisper/CMakeLists.txt RENAMED
File without changes
examples/whisper.android/{app → lib}/src/main/jni/whisper/jni.c RENAMED
@@ -131,7 +131,7 @@ static struct whisper_context *whisper_init_from_asset(
131
  }
132
 
133
  JNIEXPORT jlong JNICALL
134
- Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_initContextFromAsset(
135
  JNIEnv *env, jobject thiz, jobject assetManager, jstring asset_path_str) {
136
  UNUSED(thiz);
137
  struct whisper_context *context = NULL;
@@ -142,7 +142,7 @@ Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_initContextFromAsset(
142
  }
143
 
144
  JNIEXPORT jlong JNICALL
145
- Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_initContext(
146
  JNIEnv *env, jobject thiz, jstring model_path_str) {
147
  UNUSED(thiz);
148
  struct whisper_context *context = NULL;
@@ -153,7 +153,7 @@ Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_initContext(
153
  }
154
 
155
  JNIEXPORT void JNICALL
156
- Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_freeContext(
157
  JNIEnv *env, jobject thiz, jlong context_ptr) {
158
  UNUSED(env);
159
  UNUSED(thiz);
@@ -162,7 +162,7 @@ Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_freeContext(
162
  }
163
 
164
  JNIEXPORT void JNICALL
165
- Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_fullTranscribe(
166
  JNIEnv *env, jobject thiz, jlong context_ptr, jint num_threads, jfloatArray audio_data) {
167
  UNUSED(thiz);
168
  struct whisper_context *context = (struct whisper_context *) context_ptr;
@@ -194,7 +194,7 @@ Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_fullTranscribe(
194
  }
195
 
196
  JNIEXPORT jint JNICALL
197
- Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_getTextSegmentCount(
198
  JNIEnv *env, jobject thiz, jlong context_ptr) {
199
  UNUSED(env);
200
  UNUSED(thiz);
@@ -203,7 +203,7 @@ Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_getTextSegmentCount(
203
  }
204
 
205
  JNIEXPORT jstring JNICALL
206
- Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_getTextSegment(
207
  JNIEnv *env, jobject thiz, jlong context_ptr, jint index) {
208
  UNUSED(thiz);
209
  struct whisper_context *context = (struct whisper_context *) context_ptr;
@@ -213,7 +213,7 @@ Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_getTextSegment(
213
  }
214
 
215
  JNIEXPORT jstring JNICALL
216
- Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_getSystemInfo(
217
  JNIEnv *env, jobject thiz
218
  ) {
219
  UNUSED(thiz);
@@ -223,7 +223,7 @@ Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_getSystemInfo(
223
  }
224
 
225
  JNIEXPORT jstring JNICALL
226
- Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_benchMemcpy(JNIEnv *env, jobject thiz,
227
  jint n_threads) {
228
  UNUSED(thiz);
229
  const char *bench_ggml_memcpy = whisper_bench_memcpy_str(n_threads);
@@ -231,7 +231,7 @@ Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_benchMemcpy(JNIEnv *en
231
  }
232
 
233
  JNIEXPORT jstring JNICALL
234
- Java_com_whispercppdemo_whisper_WhisperLib_00024Companion_benchGgmlMulMat(JNIEnv *env, jobject thiz,
235
  jint n_threads) {
236
  UNUSED(thiz);
237
  const char *bench_ggml_mul_mat = whisper_bench_ggml_mul_mat_str(n_threads);
 
131
  }
132
 
133
  JNIEXPORT jlong JNICALL
134
+ Java_com_whispercpp_whisper_WhisperLib_00024Companion_initContextFromAsset(
135
  JNIEnv *env, jobject thiz, jobject assetManager, jstring asset_path_str) {
136
  UNUSED(thiz);
137
  struct whisper_context *context = NULL;
 
142
  }
143
 
144
  JNIEXPORT jlong JNICALL
145
+ Java_com_whispercpp_whisper_WhisperLib_00024Companion_initContext(
146
  JNIEnv *env, jobject thiz, jstring model_path_str) {
147
  UNUSED(thiz);
148
  struct whisper_context *context = NULL;
 
153
  }
154
 
155
  JNIEXPORT void JNICALL
156
+ Java_com_whispercpp_whisper_WhisperLib_00024Companion_freeContext(
157
  JNIEnv *env, jobject thiz, jlong context_ptr) {
158
  UNUSED(env);
159
  UNUSED(thiz);
 
162
  }
163
 
164
  JNIEXPORT void JNICALL
165
+ Java_com_whispercpp_whisper_WhisperLib_00024Companion_fullTranscribe(
166
  JNIEnv *env, jobject thiz, jlong context_ptr, jint num_threads, jfloatArray audio_data) {
167
  UNUSED(thiz);
168
  struct whisper_context *context = (struct whisper_context *) context_ptr;
 
194
  }
195
 
196
  JNIEXPORT jint JNICALL
197
+ Java_com_whispercpp_whisper_WhisperLib_00024Companion_getTextSegmentCount(
198
  JNIEnv *env, jobject thiz, jlong context_ptr) {
199
  UNUSED(env);
200
  UNUSED(thiz);
 
203
  }
204
 
205
  JNIEXPORT jstring JNICALL
206
+ Java_com_whispercpp_whisper_WhisperLib_00024Companion_getTextSegment(
207
  JNIEnv *env, jobject thiz, jlong context_ptr, jint index) {
208
  UNUSED(thiz);
209
  struct whisper_context *context = (struct whisper_context *) context_ptr;
 
213
  }
214
 
215
  JNIEXPORT jstring JNICALL
216
+ Java_com_whispercpp_whisper_WhisperLib_00024Companion_getSystemInfo(
217
  JNIEnv *env, jobject thiz
218
  ) {
219
  UNUSED(thiz);
 
223
  }
224
 
225
  JNIEXPORT jstring JNICALL
226
+ Java_com_whispercpp_whisper_WhisperLib_00024Companion_benchMemcpy(JNIEnv *env, jobject thiz,
227
  jint n_threads) {
228
  UNUSED(thiz);
229
  const char *bench_ggml_memcpy = whisper_bench_memcpy_str(n_threads);
 
231
  }
232
 
233
  JNIEXPORT jstring JNICALL
234
+ Java_com_whispercpp_whisper_WhisperLib_00024Companion_benchGgmlMulMat(JNIEnv *env, jobject thiz,
235
  jint n_threads) {
236
  UNUSED(thiz);
237
  const char *bench_ggml_mul_mat = whisper_bench_ggml_mul_mat_str(n_threads);
examples/whisper.android/settings.gradle CHANGED
@@ -14,3 +14,4 @@ dependencyResolutionManagement {
14
  }
15
  rootProject.name = "WhisperCppDemo"
16
  include ':app'
 
 
14
  }
15
  rootProject.name = "WhisperCppDemo"
16
  include ':app'
17
+ include ':lib'