diff --git a/examples/whisper.android.java/README_files/1.jpg b/examples/whisper.android.java/README_files/1.jpg new file mode 100644 index 00000000000..035cc105f3d Binary files /dev/null and b/examples/whisper.android.java/README_files/1.jpg differ diff --git a/examples/whisper.android.java/app/src/main/java/com/litongjava/whisper/android/java/MainActivity.java b/examples/whisper.android.java/app/src/main/java/com/litongjava/whisper/android/java/MainActivity.java index ecf89e6f993..097b0024e5f 100644 --- a/examples/whisper.android.java/app/src/main/java/com/litongjava/whisper/android/java/MainActivity.java +++ b/examples/whisper.android.java/app/src/main/java/com/litongjava/whisper/android/java/MainActivity.java @@ -9,24 +9,22 @@ import android.view.View; import android.widget.TextView; +import com.blankj.utilcode.util.ThreadUtils; import com.litongjava.android.view.inject.annotation.FindViewById; import com.litongjava.android.view.inject.annotation.FindViewByIdLayout; import com.litongjava.android.view.inject.annotation.OnClick; import com.litongjava.android.view.inject.utils.ViewInjectUtils; import com.litongjava.jfinal.aop.Aop; import com.litongjava.whisper.android.java.services.WhisperService; +import com.litongjava.whisper.android.java.task.LoadModelTask; +import com.litongjava.whisper.android.java.task.TranscriptionTask; import com.litongjava.whisper.android.java.utils.AssetUtils; -import com.litongjava.whisper.android.java.utils.WaveEncoder; -import com.whispercppdemo.whisper.WhisperContext; -import com.whispercppdemo.whisper.WhisperLib; +import com.whispercpp.java.whisper.WhisperLib; -import org.slf4j.ILoggerFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; -import java.io.IOException; -import java.util.concurrent.ExecutionException; @FindViewByIdLayout(R.layout.activity_main) @@ -51,13 +49,27 @@ protected void onCreate(Bundle savedInstanceState) { @OnClick(R.id.loadModelBtn) public void loadModelBtn_OnClick(View v) { Context context = getBaseContext(); - whisperService.loadModel(context, tv); + ThreadUtils.executeByIo(new LoadModelTask(context,tv)); + } @OnClick(R.id.transcriptSampleBtn) public void transcriptSampleBtn_OnClick(View v) { Context context = getBaseContext(); - whisperService.transcribeSample(context, tv); + + long start = System.currentTimeMillis(); + String sampleFilePath = "samples/jfk.wav"; + File filesDir = context.getFilesDir(); + File sampleFile = AssetUtils.copyFileIfNotExists(context, filesDir, sampleFilePath); + long end = System.currentTimeMillis(); + String msg = "copy file:" + (end - start) + "ms"; + outputMsg(tv, msg); + ThreadUtils.executeByIo(new TranscriptionTask(tv, sampleFile)); + } + + private void outputMsg(TextView tv, String msg) { + tv.append(msg + "\n"); + log.info(msg); } diff --git a/examples/whisper.android.java/app/src/main/java/com/litongjava/whisper/android/java/services/WhisperService.java b/examples/whisper.android.java/app/src/main/java/com/litongjava/whisper/android/java/services/WhisperService.java index e095d6a0325..5a3390d772e 100644 --- a/examples/whisper.android.java/app/src/main/java/com/litongjava/whisper/android/java/services/WhisperService.java +++ b/examples/whisper.android.java/app/src/main/java/com/litongjava/whisper/android/java/services/WhisperService.java @@ -3,15 +3,14 @@ import android.content.Context; import android.os.Build; import android.widget.TextView; +import android.widget.Toast; import androidx.annotation.RequiresApi; -import com.litongjava.jfinal.aop.AopManager; +import com.blankj.utilcode.util.ToastUtils; import com.litongjava.whisper.android.java.bean.WhisperSegment; import com.litongjava.whisper.android.java.single.LocalWhisper; -import com.litongjava.whisper.android.java.utils.AssetUtils; import com.litongjava.whisper.android.java.utils.WaveEncoder; -import com.whispercppdemo.whisper.WhisperContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,23 +36,17 @@ public void loadModel(Context context, TextView tv) { long end = System.currentTimeMillis(); msg = "model load successful:" + (end - start) + "ms"; outputMsg(tv, msg); + ToastUtils.showLong(msg); } - public void transcribeSample(Context context, TextView tv) { + @RequiresApi(api = Build.VERSION_CODES.O) + public void transcribeSample(TextView tv, File sampleFile) { String msg = ""; - long start = System.currentTimeMillis(); - String sampleFilePath = "samples/jfk.wav"; - File filesDir = context.getFilesDir(); - File sampleFile = AssetUtils.copyFileIfNotExists(context, filesDir, sampleFilePath); - long end = System.currentTimeMillis(); - msg = "copy file:" + (end - start) + "ms"; - outputMsg(tv, msg); - msg = "transcribe file from :" + sampleFile.getAbsolutePath(); outputMsg(tv, msg); - start = System.currentTimeMillis(); + Long start = System.currentTimeMillis(); float[] audioData = new float[0]; // 读取音频样本 try { audioData = WaveEncoder.decodeWaveFile(sampleFile); @@ -61,14 +54,13 @@ public void transcribeSample(Context context, TextView tv) { e.printStackTrace(); return; } - end = System.currentTimeMillis(); + long end = System.currentTimeMillis(); msg = "decode wave file:" + (end - start) + "ms"; outputMsg(tv, msg); start = System.currentTimeMillis(); List transcription = null; try { - //transcription = LocalWhisper.INSTANCE.transcribeData(audioData); transcription = LocalWhisper.INSTANCE.transcribeDataWithTime(audioData); } catch (ExecutionException e) { @@ -77,15 +69,28 @@ public void transcribeSample(Context context, TextView tv) { e.printStackTrace(); } end = System.currentTimeMillis(); - msg = "Transcript successful:" + (end - start) + "ms"; - outputMsg(tv, msg); + if(transcription!=null){ + ToastUtils.showLong(transcription.toString()); + + msg = "Transcript successful:" + (end - start) + "ms"; + outputMsg(tv, msg); + + msg = "Transcription:" + transcription.toString(); + outputMsg(tv, msg); + + }else{ + msg = "Transcript failed:" + (end - start) + "ms"; + outputMsg(tv, msg); + } - msg = "Transcription:" + transcription.toString(); - outputMsg(tv, msg); } private void outputMsg(TextView tv, String msg) { - tv.append(msg + "\n"); + if(tv!=null){ + tv.append(msg + "\n"); + } + log.info(msg); + } @RequiresApi(api = Build.VERSION_CODES.O) diff --git a/examples/whisper.android.java/app/src/main/java/com/litongjava/whisper/android/java/single/LocalWhisper.java b/examples/whisper.android.java/app/src/main/java/com/litongjava/whisper/android/java/single/LocalWhisper.java index 0f28837047d..3cbcb8f2489 100644 --- a/examples/whisper.android.java/app/src/main/java/com/litongjava/whisper/android/java/single/LocalWhisper.java +++ b/examples/whisper.android.java/app/src/main/java/com/litongjava/whisper/android/java/single/LocalWhisper.java @@ -8,12 +8,14 @@ import com.blankj.utilcode.util.Utils; import com.litongjava.whisper.android.java.bean.WhisperSegment; import com.litongjava.whisper.android.java.utils.AssetUtils; -import com.whispercppdemo.whisper.WhisperContext; +import com.whispercpp.java.whisper.WhisperContext; import java.io.File; import java.util.List; import java.util.concurrent.ExecutionException; + +@RequiresApi(api = Build.VERSION_CODES.O) public enum LocalWhisper { INSTANCE; diff --git a/examples/whisper.android.java/app/src/main/java/com/litongjava/whisper/android/java/task/LoadModelTask.java b/examples/whisper.android.java/app/src/main/java/com/litongjava/whisper/android/java/task/LoadModelTask.java new file mode 100644 index 00000000000..3ab2bc34032 --- /dev/null +++ b/examples/whisper.android.java/app/src/main/java/com/litongjava/whisper/android/java/task/LoadModelTask.java @@ -0,0 +1,42 @@ +package com.litongjava.whisper.android.java.task; + +import android.content.Context; +import android.os.Build; +import android.widget.TextView; + +import com.blankj.utilcode.util.ThreadUtils; +import com.litongjava.jfinal.aop.Aop; +import com.litongjava.whisper.android.java.services.WhisperService; + +import java.io.File; + +public class LoadModelTask extends ThreadUtils.Task { + private final Context context; + private final TextView tv; + public LoadModelTask(Context context,TextView tv) { + this.tv = tv; + this.context=context; + } + + @Override + public Object doInBackground() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + Aop.get(WhisperService.class).loadModel(context,tv); + }else{ + tv.append("not supported android devices"); + } + return null; + } + + @Override + public void onSuccess(Object result) { + } + + @Override + public void onCancel() { + } + + @Override + public void onFail(Throwable t) { + } +} \ No newline at end of file diff --git a/examples/whisper.android.java/app/src/main/java/com/litongjava/whisper/android/java/task/TranscriptionTask.java b/examples/whisper.android.java/app/src/main/java/com/litongjava/whisper/android/java/task/TranscriptionTask.java new file mode 100644 index 00000000000..7477f8eda18 --- /dev/null +++ b/examples/whisper.android.java/app/src/main/java/com/litongjava/whisper/android/java/task/TranscriptionTask.java @@ -0,0 +1,44 @@ +package com.litongjava.whisper.android.java.task; + +import android.content.Context; +import android.os.Build; +import android.widget.TextView; + +import com.blankj.utilcode.util.ThreadUtils; +import com.litongjava.jfinal.aop.Aop; +import com.litongjava.whisper.android.java.services.WhisperService; + +import java.io.File; + +public class TranscriptionTask extends ThreadUtils.Task { + private final TextView tv; + private final File sampleFile; + + public TranscriptionTask(TextView tv, File sampleFile) { + this.tv = tv; + this.sampleFile = sampleFile; + + } + + @Override + public Object doInBackground() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + Aop.get(WhisperService.class).transcribeSample(tv, sampleFile); + }else{ + tv.append("not supported android devices"); + } + return null; + } + + @Override + public void onSuccess(Object result) { + } + + @Override + public void onCancel() { + } + + @Override + public void onFail(Throwable t) { + } +} \ No newline at end of file diff --git a/examples/whisper.android.java/app/src/main/java/com/whispercppdemo/whisper/CpuInfo.java b/examples/whisper.android.java/app/src/main/java/com/whispercpp/java/whisper/CpuInfo.java similarity index 99% rename from examples/whisper.android.java/app/src/main/java/com/whispercppdemo/whisper/CpuInfo.java rename to examples/whisper.android.java/app/src/main/java/com/whispercpp/java/whisper/CpuInfo.java index b6ed7a49b3f..733ca354aee 100644 --- a/examples/whisper.android.java/app/src/main/java/com/whispercppdemo/whisper/CpuInfo.java +++ b/examples/whisper.android.java/app/src/main/java/com/whispercpp/java/whisper/CpuInfo.java @@ -1,4 +1,4 @@ -package com.whispercppdemo.whisper; +package com.whispercpp.java.whisper; import android.os.Build; import android.util.Log; diff --git a/examples/whisper.android.java/app/src/main/java/com/whispercppdemo/whisper/WhisperContext.java b/examples/whisper.android.java/app/src/main/java/com/whispercpp/java/whisper/WhisperContext.java similarity index 99% rename from examples/whisper.android.java/app/src/main/java/com/whispercppdemo/whisper/WhisperContext.java rename to examples/whisper.android.java/app/src/main/java/com/whispercpp/java/whisper/WhisperContext.java index 654c42866aa..0e52ec12105 100644 --- a/examples/whisper.android.java/app/src/main/java/com/whispercppdemo/whisper/WhisperContext.java +++ b/examples/whisper.android.java/app/src/main/java/com/whispercpp/java/whisper/WhisperContext.java @@ -1,4 +1,4 @@ -package com.whispercppdemo.whisper; +package com.whispercpp.java.whisper; import android.content.res.AssetManager; import android.os.Build; diff --git a/examples/whisper.android.java/app/src/main/java/com/whispercppdemo/whisper/WhisperCpuConfig.java b/examples/whisper.android.java/app/src/main/java/com/whispercpp/java/whisper/WhisperCpuConfig.java similarity index 87% rename from examples/whisper.android.java/app/src/main/java/com/whispercppdemo/whisper/WhisperCpuConfig.java rename to examples/whisper.android.java/app/src/main/java/com/whispercpp/java/whisper/WhisperCpuConfig.java index a1e69e3b9a2..8cd2b888d4a 100644 --- a/examples/whisper.android.java/app/src/main/java/com/whispercppdemo/whisper/WhisperCpuConfig.java +++ b/examples/whisper.android.java/app/src/main/java/com/whispercpp/java/whisper/WhisperCpuConfig.java @@ -1,4 +1,4 @@ -package com.whispercppdemo.whisper; +package com.whispercpp.java.whisper; import android.os.Build; diff --git a/examples/whisper.android.java/app/src/main/java/com/whispercppdemo/whisper/WhisperLib.java b/examples/whisper.android.java/app/src/main/java/com/whispercpp/java/whisper/WhisperLib.java similarity index 98% rename from examples/whisper.android.java/app/src/main/java/com/whispercppdemo/whisper/WhisperLib.java rename to examples/whisper.android.java/app/src/main/java/com/whispercpp/java/whisper/WhisperLib.java index bf99cc7ebb3..38dd47a38a2 100644 --- a/examples/whisper.android.java/app/src/main/java/com/whispercppdemo/whisper/WhisperLib.java +++ b/examples/whisper.android.java/app/src/main/java/com/whispercpp/java/whisper/WhisperLib.java @@ -1,4 +1,4 @@ -package com.whispercppdemo.whisper; +package com.whispercpp.java.whisper; import android.content.res.AssetManager; import android.os.Build; diff --git a/examples/whisper.android.java/app/src/main/java/com/whispercppdemo/whisper/WhisperUtils.java b/examples/whisper.android.java/app/src/main/java/com/whispercpp/java/whisper/WhisperUtils.java similarity index 95% rename from examples/whisper.android.java/app/src/main/java/com/whispercppdemo/whisper/WhisperUtils.java rename to examples/whisper.android.java/app/src/main/java/com/whispercpp/java/whisper/WhisperUtils.java index 5781db55c43..8e803b7ff40 100644 --- a/examples/whisper.android.java/app/src/main/java/com/whispercppdemo/whisper/WhisperUtils.java +++ b/examples/whisper.android.java/app/src/main/java/com/whispercpp/java/whisper/WhisperUtils.java @@ -1,4 +1,4 @@ -package com.whispercppdemo.whisper; +package com.whispercpp.java.whisper; import android.os.Build; import android.util.Log; diff --git a/examples/whisper.android.java/app/src/main/jni/whisper/jni.c b/examples/whisper.android.java/app/src/main/jni/whisper/jni.c index 5b75cd19f47..f8e7effedd0 100644 --- a/examples/whisper.android.java/app/src/main/jni/whisper/jni.c +++ b/examples/whisper.android.java/app/src/main/jni/whisper/jni.c @@ -67,7 +67,7 @@ void inputStreamClose(void * ctx) { } JNIEXPORT jlong JNICALL -Java_com_whispercppdemo_whisper_WhisperLib_initContextFromInputStream( +Java_com_whispercpp_java_whisper_WhisperLib_initContextFromInputStream( JNIEnv *env, jobject thiz, jobject input_stream) { UNUSED(thiz); @@ -131,7 +131,7 @@ static struct whisper_context *whisper_init_from_asset( } JNIEXPORT jlong JNICALL -Java_com_whispercppdemo_whisper_WhisperLib_initContextFromAsset( +Java_com_whispercpp_java_whisper_WhisperLib_initContextFromAsset( JNIEnv *env, jobject thiz, jobject assetManager, jstring asset_path_str) { UNUSED(thiz); struct whisper_context *context = NULL; @@ -142,7 +142,7 @@ Java_com_whispercppdemo_whisper_WhisperLib_initContextFromAsset( } JNIEXPORT jlong JNICALL -Java_com_whispercppdemo_whisper_WhisperLib_initContext( +Java_com_whispercpp_java_whisper_WhisperLib_initContext( JNIEnv *env, jobject thiz, jstring model_path_str) { UNUSED(thiz); struct whisper_context *context = NULL; @@ -153,7 +153,7 @@ Java_com_whispercppdemo_whisper_WhisperLib_initContext( } JNIEXPORT void JNICALL -Java_com_whispercppdemo_whisper_WhisperLib_freeContext( +Java_com_whispercpp_java_whisper_WhisperLib_freeContext( JNIEnv *env, jobject thiz, jlong context_ptr) { UNUSED(env); UNUSED(thiz); @@ -162,7 +162,7 @@ Java_com_whispercppdemo_whisper_WhisperLib_freeContext( } JNIEXPORT void JNICALL -Java_com_whispercppdemo_whisper_WhisperLib_fullTranscribe( +Java_com_whispercpp_java_whisper_WhisperLib_fullTranscribe( JNIEnv *env, jobject thiz, jlong context_ptr, jint num_threads, jfloatArray audio_data) { UNUSED(thiz); struct whisper_context *context = (struct whisper_context *) context_ptr; @@ -194,7 +194,7 @@ Java_com_whispercppdemo_whisper_WhisperLib_fullTranscribe( } JNIEXPORT jint JNICALL -Java_com_whispercppdemo_whisper_WhisperLib_getTextSegmentCount( +Java_com_whispercpp_java_whisper_WhisperLib_getTextSegmentCount( JNIEnv *env, jobject thiz, jlong context_ptr) { UNUSED(env); UNUSED(thiz); @@ -204,7 +204,7 @@ Java_com_whispercppdemo_whisper_WhisperLib_getTextSegmentCount( JNIEXPORT jstring JNICALL -Java_com_whispercppdemo_whisper_WhisperLib_getTextSegment( +Java_com_whispercpp_java_whisper_WhisperLib_getTextSegment( JNIEnv *env, jobject thiz, jlong context_ptr, jint index) { UNUSED(thiz); struct whisper_context *context = (struct whisper_context *) context_ptr; @@ -214,7 +214,7 @@ Java_com_whispercppdemo_whisper_WhisperLib_getTextSegment( } JNIEXPORT jlong JNICALL -Java_com_whispercppdemo_whisper_WhisperLib_getTextSegmentT0(JNIEnv *env, jobject thiz,jlong context_ptr, jint index) { +Java_com_whispercpp_java_whisper_WhisperLib_getTextSegmentT0(JNIEnv *env, jobject thiz,jlong context_ptr, jint index) { UNUSED(thiz); struct whisper_context *context = (struct whisper_context *) context_ptr; const int64_t t0 = whisper_full_get_segment_t0(context, index); @@ -222,7 +222,7 @@ Java_com_whispercppdemo_whisper_WhisperLib_getTextSegmentT0(JNIEnv *env, jobject } JNIEXPORT jlong JNICALL -Java_com_whispercppdemo_whisper_WhisperLib_getTextSegmentT1(JNIEnv *env, jobject thiz,jlong context_ptr, jint index) { +Java_com_whispercpp_java_whisper_WhisperLib_getTextSegmentT1(JNIEnv *env, jobject thiz,jlong context_ptr, jint index) { UNUSED(thiz); struct whisper_context *context = (struct whisper_context *) context_ptr; const int64_t t1 = whisper_full_get_segment_t1(context, index); @@ -230,7 +230,7 @@ Java_com_whispercppdemo_whisper_WhisperLib_getTextSegmentT1(JNIEnv *env, jobject } JNIEXPORT jstring JNICALL -Java_com_whispercppdemo_whisper_WhisperLib_getSystemInfo( +Java_com_whispercpp_java_whisper_WhisperLib_getSystemInfo( JNIEnv *env, jobject thiz ) { UNUSED(thiz); @@ -240,7 +240,7 @@ Java_com_whispercppdemo_whisper_WhisperLib_getSystemInfo( } JNIEXPORT jstring JNICALL -Java_com_whispercppdemo_whisper_WhisperLib_benchMemcpy(JNIEnv *env, jobject thiz, +Java_com_whispercpp_java_whisper_WhisperLib_benchMemcpy(JNIEnv *env, jobject thiz, jint n_threads) { UNUSED(thiz); const char *bench_ggml_memcpy = whisper_bench_memcpy_str(n_threads); @@ -248,7 +248,7 @@ Java_com_whispercppdemo_whisper_WhisperLib_benchMemcpy(JNIEnv *env, jobject thiz } JNIEXPORT jstring JNICALL -Java_com_whispercppdemo_whisper_WhisperLib_benchGgmlMulMat(JNIEnv *env, jobject thiz, +Java_com_whispercpp_java_whisper_WhisperLib_benchGgmlMulMat(JNIEnv *env, jobject thiz, jint n_threads) { UNUSED(thiz); const char *bench_ggml_mul_mat = whisper_bench_ggml_mul_mat_str(n_threads);