Skip to content

Commit 6ef8552

Browse files
author
litongjava
committed
add examples for Java
1 parent 365e466 commit 6ef8552

File tree

7 files changed

+190
-86
lines changed

7 files changed

+190
-86
lines changed

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@ dependencies {
4747

4848
//litongjava
4949
implementation 'com.litongjava:android-view-inject:1.0'
50+
implementation 'com.litongjava:jfinal-aop:1.0.1'
5051
implementation 'com.litongjava:litongjava-android-utils:1.0.0'
5152
}
Lines changed: 40 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,79 @@
11
package com.litongjava.whisper.cpp.android.java.demo;
22

33
import android.content.Context;
4-
import android.content.res.AssetManager;
54
import android.os.Build;
65
import android.os.Bundle;
6+
import android.view.View;
77
import android.widget.TextView;
88

99
import androidx.annotation.RequiresApi;
1010
import androidx.appcompat.app.AppCompatActivity;
1111

1212
import com.litongjava.android.view.inject.annotation.FindViewById;
1313
import com.litongjava.android.view.inject.annotation.FindViewByIdLayout;
14+
import com.litongjava.android.view.inject.annotation.OnClick;
1415
import com.litongjava.android.view.inject.utils.ViewInjectUtils;
15-
import com.litongjava.whisper.cpp.android.java.demo.media.WaveEncoder;
16-
import com.litongjava.whisper.cpp.android.java.demo.utils.AssetUtils;
17-
import com.whispercppdemo.whisper.WhisperContext;
18-
import com.whispercppdemo.whisper.WhisperCpuConfig;
16+
import com.litongjava.jfinal.aop.Aop;
17+
import com.litongjava.whisper.cpp.android.java.services.WhisperService;
18+
import com.whispercppdemo.whisper.WhisperLib;
1919

2020
import org.slf4j.Logger;
2121
import org.slf4j.LoggerFactory;
2222

23-
import java.io.File;
24-
import java.io.IOException;
25-
import java.util.Arrays;
26-
import java.util.concurrent.ExecutionException;
2723

2824
@FindViewByIdLayout(R.layout.activity_main)
2925
public class MainActivity extends AppCompatActivity {
30-
private Logger log = LoggerFactory.getLogger(this.getClass());
31-
private WhisperContext whisperContext;
3226

33-
@FindViewById(R.id.textHelloWorld)
34-
private TextView textHelloWorld;
27+
@FindViewById(R.id.sample_text)
28+
private TextView tv;
29+
30+
Logger log = LoggerFactory.getLogger(this.getClass());
31+
private WhisperService whisperService = Aop.get(WhisperService.class);
3532

3633
@RequiresApi(api = Build.VERSION_CODES.O)
3734
@Override
3835
protected void onCreate(Bundle savedInstanceState) {
39-
4036
super.onCreate(savedInstanceState);
41-
///setContentView(R.layout.activity_main);
37+
//setContentView(R.layout.activity_main);
4238
ViewInjectUtils.injectActivity(this, this);
43-
int preferredThreadCount = WhisperCpuConfig.getPreferredThreadCount();
44-
log.info("preferredThreadCount:{}", preferredThreadCount);
45-
46-
Context baseContext = getBaseContext();
47-
File filesDir = baseContext.getFilesDir();
48-
log.info("filesDir:{}", filesDir);
49-
AssetManager assets = baseContext.getAssets();
50-
try {
51-
String[] models = assets.list("models");
52-
log.info("models:{}", Arrays.toString(models));
53-
} catch (IOException e) {
54-
e.printStackTrace();
55-
}
56-
57-
try {
58-
String[] samples = assets.list("samples");
59-
log.info("samples:{}", Arrays.toString(samples));
60-
} catch (IOException e) {
61-
e.printStackTrace();
62-
}
63-
String modelFilePath = "models/ggml-tiny.bin";
64-
File modelFile = AssetUtils.copyFileIfNotExists(baseContext, filesDir, modelFilePath);
65-
modelFilePath = modelFile.getAbsolutePath();
66-
// 加载模型
67-
loadModel(modelFilePath);
68-
69-
70-
String sampleFilePath = "samples/jfk.wav";
71-
File sampleFile = AssetUtils.copyFileIfNotExists(baseContext, filesDir, sampleFilePath);
72-
// 识别样本
73-
transcribeSample(sampleFile);
39+
showSystemInfo();
40+
}
41+
42+
@RequiresApi(api = Build.VERSION_CODES.O)
43+
@OnClick(R.id.loadModelBtn)
44+
public void loadModelBtn_OnClick(View v) {
45+
Context context = getBaseContext();
46+
whisperService.loadModel(context, tv);
47+
}
48+
49+
@OnClick(R.id.transcriptSampleBtn)
50+
public void transcriptSampleBtn_OnClick(View v) {
51+
Context context = getBaseContext();
52+
whisperService.transcribeSample(context, tv);
53+
}
54+
55+
56+
@RequiresApi(api = Build.VERSION_CODES.O)
57+
@OnClick(R.id.systemInfoBtn)
58+
public void systemInfoBtn_OnClick(View v) {
59+
showSystemInfo();
7460
}
7561

7662
@RequiresApi(api = Build.VERSION_CODES.O)
77-
private void loadModel(String modelPath) {
78-
log.info("load model from :{}", modelPath);
79-
whisperContext = WhisperContext.createContextFromFile(modelPath);
63+
public void showSystemInfo() {
64+
String systemInfo = WhisperLib.getSystemInfo();
65+
tv.append(systemInfo + "\n");
8066
}
8167

82-
private void transcribeSample(File sampleFile) {
83-
log.info("transcribe file from :{}", sampleFile.getAbsolutePath());
84-
float[] audioData = new float[0]; // 读取音频样本
85-
try {
86-
audioData = WaveEncoder.decodeWaveFile(sampleFile);
87-
} catch (IOException e) {
88-
e.printStackTrace();
89-
}
90-
91-
String transcription = null; // 转录音频数据
92-
try {
93-
transcription = whisperContext.transcribeData(audioData);
94-
} catch (ExecutionException e) {
95-
e.printStackTrace();
96-
} catch (InterruptedException e) {
97-
e.printStackTrace();
98-
}
99-
log.info("Transcription: {}", transcription); // 打印转录结果
100-
textHelloWorld.setText(transcription);
68+
@OnClick(R.id.clearBtn)
69+
public void clearBtn_OnClick(View v) {
70+
tv.setText("");
10171
}
10272

10373
@RequiresApi(api = Build.VERSION_CODES.O)
10474
@Override
10575
protected void onDestroy() {
10676
super.onDestroy();
107-
if (whisperContext != null) {
108-
try {
109-
whisperContext.release();
110-
} catch (ExecutionException e) {
111-
e.printStackTrace();
112-
} catch (InterruptedException e) {
113-
e.printStackTrace();
114-
}
115-
whisperContext = null;
116-
}
77+
whisperService.release();
11778
}
118-
}
79+
}

app/src/main/java/com/litongjava/whisper/cpp/android/java/demo/media/WaveEncoder.java renamed to app/src/main/java/com/litongjava/whisper/cpp/android/java/media/WaveEncoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.litongjava.whisper.cpp.android.java.demo.media;
1+
package com.litongjava.whisper.cpp.android.java.media;
22

33
import java.io.ByteArrayOutputStream;
44
import java.io.File;

app/src/main/java/com/litongjava/whisper/cpp/android/java/demo/recoder/Recorder.java renamed to app/src/main/java/com/litongjava/whisper/cpp/android/java/recoder/Recorder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
package com.litongjava.whisper.cpp.android.java.demo.recoder;
1+
package com.litongjava.whisper.cpp.android.java.recoder;
22

33
import android.annotation.SuppressLint;
44
import android.media.AudioFormat;
55
import android.media.AudioRecord;
66
import android.media.MediaRecorder;
77

8-
import com.litongjava.whisper.cpp.android.java.demo.media.WaveEncoder;
8+
import com.litongjava.whisper.cpp.android.java.media.WaveEncoder;
99

1010
import java.io.File;
1111
import java.util.ArrayList;
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.litongjava.whisper.cpp.android.java.services;
2+
3+
import android.content.Context;
4+
import android.os.Build;
5+
import android.widget.TextView;
6+
7+
import androidx.annotation.RequiresApi;
8+
9+
import com.litongjava.whisper.cpp.android.java.media.WaveEncoder;
10+
import com.litongjava.whisper.cpp.android.java.utils.AssetUtils;
11+
import com.whispercppdemo.whisper.WhisperContext;
12+
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
15+
16+
import java.io.File;
17+
import java.io.IOException;
18+
import java.util.concurrent.ExecutionException;
19+
20+
public class WhisperService {
21+
private Logger log = LoggerFactory.getLogger(this.getClass());
22+
private WhisperContext whisperContext;
23+
24+
@RequiresApi(api = Build.VERSION_CODES.O)
25+
public void loadModel(Context context, TextView tv) {
26+
27+
File filesDir = context.getFilesDir();
28+
String modelFilePath = "models/ggml-tiny.bin";
29+
File modelFile = AssetUtils.copyFileIfNotExists(context, filesDir, modelFilePath);
30+
modelFilePath = modelFile.getAbsolutePath();
31+
32+
String msg = "load model from :" + modelFilePath + "\n";
33+
log.info(msg);
34+
tv.append(msg);
35+
if (whisperContext == null) {
36+
long start = System.currentTimeMillis();
37+
whisperContext = WhisperContext.createContextFromFile(modelFilePath);
38+
// AopManager.me().addSingletonObject(whisperContext);
39+
long end = System.currentTimeMillis();
40+
msg = "model load successful:" + (end - start) + "ms\n";
41+
log.info(msg);
42+
tv.append(msg);
43+
} else {
44+
msg = "model loaded\n";
45+
log.info(msg);
46+
tv.append(msg);
47+
}
48+
}
49+
50+
public void transcribeSample(Context context, TextView tv) {
51+
String sampleFilePath = "samples/jfk.wav";
52+
File filesDir = context.getFilesDir();
53+
File sampleFile = AssetUtils.copyFileIfNotExists(context, filesDir, sampleFilePath);
54+
log.info("transcribe file from :{}", sampleFile.getAbsolutePath());
55+
float[] audioData = new float[0]; // 读取音频样本
56+
try {
57+
audioData = WaveEncoder.decodeWaveFile(sampleFile);
58+
} catch (IOException e) {
59+
e.printStackTrace();
60+
}
61+
62+
String transcription = null; // 转录音频数据
63+
64+
String msg = "";
65+
try {
66+
if (whisperContext == null) {
67+
msg = "please load model or wait model loaded";
68+
log.info(msg);
69+
70+
} else {
71+
long start = System.currentTimeMillis();
72+
transcription = whisperContext.transcribeData(audioData);
73+
long end = System.currentTimeMillis();
74+
msg = "Transcript successful:" + (end - start) + "ms";
75+
log.info(msg);
76+
tv.append(msg + "\n");
77+
78+
msg = "Transcription:" + transcription;
79+
log.info(msg);
80+
tv.append(msg + "\n");
81+
}
82+
83+
84+
} catch (ExecutionException e) {
85+
e.printStackTrace();
86+
} catch (InterruptedException e) {
87+
e.printStackTrace();
88+
}
89+
90+
91+
}
92+
93+
@RequiresApi(api = Build.VERSION_CODES.O)
94+
public void release() {
95+
if (whisperContext != null) {
96+
try {
97+
whisperContext.release();
98+
} catch (ExecutionException e) {
99+
e.printStackTrace();
100+
} catch (InterruptedException e) {
101+
e.printStackTrace();
102+
}
103+
whisperContext = null;
104+
}
105+
}
106+
}

app/src/main/java/com/litongjava/whisper/cpp/android/java/demo/utils/AssetUtils.java renamed to app/src/main/java/com/litongjava/whisper/cpp/android/java/utils/AssetUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.litongjava.whisper.cpp.android.java.demo.utils;
1+
package com.litongjava.whisper.cpp.android.java.utils;
22

33
import android.content.Context;
44

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,49 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
xmlns:tools="http://schemas.android.com/tools"
55
android:layout_width="match_parent"
66
android:layout_height="match_parent"
7+
android:orientation="vertical"
78
tools:context=".MainActivity">
89

10+
<LinearLayout
11+
android:layout_width="match_parent"
12+
android:layout_height="wrap_content">
13+
14+
<Button
15+
android:id="@+id/systemInfoBtn"
16+
android:layout_width="wrap_content"
17+
android:layout_height="wrap_content"
18+
android:text="System Info" />
19+
20+
<Button
21+
android:id="@+id/loadModelBtn"
22+
android:layout_width="wrap_content"
23+
android:layout_height="wrap_content"
24+
android:text="Load model" />
25+
26+
</LinearLayout>
27+
28+
<LinearLayout
29+
android:layout_width="wrap_content"
30+
android:layout_height="wrap_content">
31+
32+
<Button
33+
android:id="@+id/transcriptSampleBtn"
34+
android:layout_width="wrap_content"
35+
android:layout_height="wrap_content"
36+
android:text="Transcribe sample" />
37+
38+
<Button
39+
android:id="@+id/clearBtn"
40+
android:layout_width="wrap_content"
41+
android:layout_height="wrap_content"
42+
android:text="Clear" />
43+
</LinearLayout>
44+
945
<TextView
10-
android:id="@+id/textHelloWorld"
46+
android:id="@+id/sample_text"
1147
android:layout_width="wrap_content"
1248
android:layout_height="wrap_content"
1349
android:text="Hello World!"
@@ -16,4 +52,4 @@
1652
app:layout_constraintRight_toRightOf="parent"
1753
app:layout_constraintTop_toTopOf="parent" />
1854

19-
</androidx.constraintlayout.widget.ConstraintLayout>
55+
</LinearLayout>

0 commit comments

Comments
 (0)