Skip to content

Commit

Permalink
Optimize code logic
Browse files Browse the repository at this point in the history
  • Loading branch information
litongjava committed Oct 24, 2023
1 parent 006a88d commit 644c2d5
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 46 deletions.
Binary file added examples/whisper.android.java/README_files/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,38 +36,31 @@ 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);
} catch (IOException e) {
e.printStackTrace();
return;
}
end = System.currentTimeMillis();
long end = System.currentTimeMillis();
msg = "decode wave file:" + (end - start) + "ms";
outputMsg(tv, msg);

start = System.currentTimeMillis();
List<WhisperSegment> transcription = null;
try {

//transcription = LocalWhisper.INSTANCE.transcribeData(audioData);
transcription = LocalWhisper.INSTANCE.transcribeDataWithTime(audioData);
} catch (ExecutionException e) {
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Object> {
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) {
}
}
Original file line number Diff line number Diff line change
@@ -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<Object> {
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) {
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.whispercppdemo.whisper;
package com.whispercpp.java.whisper;

import android.os.Build;
import android.util.Log;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.whispercppdemo.whisper;
package com.whispercpp.java.whisper;

import android.content.res.AssetManager;
import android.os.Build;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.whispercppdemo.whisper;
package com.whispercpp.java.whisper;

import android.os.Build;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.whispercppdemo.whisper;
package com.whispercpp.java.whisper;

import android.content.res.AssetManager;
import android.os.Build;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.whispercppdemo.whisper;
package com.whispercpp.java.whisper;

import android.os.Build;
import android.util.Log;
Expand Down
24 changes: 12 additions & 12 deletions examples/whisper.android.java/app/src/main/jni/whisper/jni.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -214,23 +214,23 @@ 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);
return (jlong)t0;
}

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);
return (jlong)t1;
}

JNIEXPORT jstring JNICALL
Java_com_whispercppdemo_whisper_WhisperLib_getSystemInfo(
Java_com_whispercpp_java_whisper_WhisperLib_getSystemInfo(
JNIEnv *env, jobject thiz
) {
UNUSED(thiz);
Expand All @@ -240,15 +240,15 @@ 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);
jstring string = (*env)->NewStringUTF(env, bench_ggml_memcpy);
}

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);
Expand Down

0 comments on commit 644c2d5

Please sign in to comment.