11package com .litongjava .whisper .cpp .android .java .demo ;
22
33import android .content .Context ;
4- import android .content .res .AssetManager ;
54import android .os .Build ;
65import android .os .Bundle ;
6+ import android .view .View ;
77import android .widget .TextView ;
88
99import androidx .annotation .RequiresApi ;
1010import androidx .appcompat .app .AppCompatActivity ;
1111
1212import com .litongjava .android .view .inject .annotation .FindViewById ;
1313import com .litongjava .android .view .inject .annotation .FindViewByIdLayout ;
14+ import com .litongjava .android .view .inject .annotation .OnClick ;
1415import 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
2020import org .slf4j .Logger ;
2121import 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 )
2925public 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+ }
0 commit comments