1
1
package com .litongjava .whisper .cpp .android .java .demo ;
2
2
3
3
import android .content .Context ;
4
- import android .content .res .AssetManager ;
5
4
import android .os .Build ;
6
5
import android .os .Bundle ;
6
+ import android .view .View ;
7
7
import android .widget .TextView ;
8
8
9
9
import androidx .annotation .RequiresApi ;
10
10
import androidx .appcompat .app .AppCompatActivity ;
11
11
12
12
import com .litongjava .android .view .inject .annotation .FindViewById ;
13
13
import com .litongjava .android .view .inject .annotation .FindViewByIdLayout ;
14
+ import com .litongjava .android .view .inject .annotation .OnClick ;
14
15
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 ;
19
19
20
20
import org .slf4j .Logger ;
21
21
import org .slf4j .LoggerFactory ;
22
22
23
- import java .io .File ;
24
- import java .io .IOException ;
25
- import java .util .Arrays ;
26
- import java .util .concurrent .ExecutionException ;
27
23
28
24
@ FindViewByIdLayout (R .layout .activity_main )
29
25
public class MainActivity extends AppCompatActivity {
30
- private Logger log = LoggerFactory .getLogger (this .getClass ());
31
- private WhisperContext whisperContext ;
32
26
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 );
35
32
36
33
@ RequiresApi (api = Build .VERSION_CODES .O )
37
34
@ Override
38
35
protected void onCreate (Bundle savedInstanceState ) {
39
-
40
36
super .onCreate (savedInstanceState );
41
- /// setContentView(R.layout.activity_main);
37
+ //setContentView(R.layout.activity_main);
42
38
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 ();
74
60
}
75
61
76
62
@ 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 " );
80
66
}
81
67
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 ("" );
101
71
}
102
72
103
73
@ RequiresApi (api = Build .VERSION_CODES .O )
104
74
@ Override
105
75
protected void onDestroy () {
106
76
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 ();
117
78
}
118
- }
79
+ }
0 commit comments