Skip to content

Commit

Permalink
release to v1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
litongjava committed Oct 22, 2023
1 parent 98cbb1d commit 42814f3
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 1,749 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.litongjava.whisper.cpp.android.java.bean;

/**
* Created by [email protected] on 10/21/2023_7:48 AM
*/
public class WhisperSegment {
private long start, end;
private String sentence;

public WhisperSegment() {
}

public WhisperSegment(long start, long end, String sentence) {
this.start = start;
this.end = end;
this.sentence = sentence;
}

public long getStart() {
return start;
}

public long getEnd() {
return end;
}

public String getSentence() {
return sentence;
}

public void setStart(long start) {
this.start = start;
}

public void setEnd(long end) {
this.end = end;
}

public void setSentence(String sentence) {
this.sentence = sentence;
}

@Override
public String toString() {
return "["+start+" --> "+end+"]:"+sentence;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.litongjava.whisper.cpp.android.java.demo.app;

import android.app.Application;

import com.blankj.utilcode.util.Utils;

public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
Utils.init(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,101 +6,89 @@

import androidx.annotation.RequiresApi;

import com.litongjava.whisper.cpp.android.java.bean.WhisperSegment;
import com.litongjava.whisper.cpp.android.java.media.WaveEncoder;
import com.litongjava.whisper.cpp.android.java.single.LocalWhisper;
import com.litongjava.whisper.cpp.android.java.utils.AssetUtils;
import com.whispercppdemo.whisper.WhisperContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutionException;

public class WhisperService {
private Logger log = LoggerFactory.getLogger(this.getClass());
private WhisperContext whisperContext;

private final Object lock = new Object();

@RequiresApi(api = Build.VERSION_CODES.O)
public void loadModel(Context context, TextView tv) {
String modelFilePath = LocalWhisper.modelFilePath;
String msg = "load model from :" + modelFilePath + "\n";
outputMsg(tv, msg);

File filesDir = context.getFilesDir();
String modelFilePath = "models/ggml-tiny.bin";
File modelFile = AssetUtils.copyFileIfNotExists(context, filesDir, modelFilePath);
modelFilePath = modelFile.getAbsolutePath();
long start = System.currentTimeMillis();
LocalWhisper.INSTANCE.init();
long end = System.currentTimeMillis();
msg = "model load successful:" + (end - start) + "ms";
outputMsg(tv, msg);

String msg = "load model from :" + modelFilePath + "\n";
log.info(msg);
tv.append(msg);
if (whisperContext == null) {
long start = System.currentTimeMillis();
whisperContext = WhisperContext.createContextFromFile(modelFilePath);
// AopManager.me().addSingletonObject(whisperContext);
long end = System.currentTimeMillis();
msg = "model load successful:" + (end - start) + "ms\n";
log.info(msg);
tv.append(msg);
} else {
msg = "model loaded\n";
log.info(msg);
tv.append(msg);
}
}

public void transcribeSample(Context context, TextView tv) {
String msg = "";
long start = System.currentTimeMillis();
String sampleFilePath = "samples/jfk.wav";
File filesDir = context.getFilesDir();
File sampleFile = AssetUtils.copyFileIfNotExists(context, filesDir, sampleFilePath);
log.info("transcribe file from :{}", sampleFile.getAbsolutePath());
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();
float[] audioData = new float[0]; // 读取音频样本
try {
audioData = WaveEncoder.decodeWaveFile(sampleFile);
} catch (IOException e) {
e.printStackTrace();
return;
}
end = System.currentTimeMillis();
msg = "decode wave file:" + (end - start) + "ms";
outputMsg(tv, msg);

String transcription = null; // 转录音频数据

String msg = "";
start = System.currentTimeMillis();
List<WhisperSegment> transcription = null;
try {
if (whisperContext == null) {
msg = "please load model or wait model loaded";
log.info(msg);

} else {
long start = System.currentTimeMillis();
transcription = whisperContext.transcribeData(audioData);
long end = System.currentTimeMillis();
msg = "Transcript successful:" + (end - start) + "ms";
log.info(msg);
tv.append(msg + "\n");

msg = "Transcription:" + transcription;
log.info(msg);
tv.append(msg + "\n");
}


//transcription = LocalWhisper.INSTANCE.transcribeData(audioData);
transcription = LocalWhisper.INSTANCE.transcribeDataWithTime(audioData);
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
end = System.currentTimeMillis();
msg = "Transcript successful:" + (end - start) + "ms";
outputMsg(tv, msg);

msg = "Transcription:" + transcription.toString();
outputMsg(tv, msg);
}

private void outputMsg(TextView tv, String msg) {
tv.append(msg + "\n");
log.info(msg);
}

@RequiresApi(api = Build.VERSION_CODES.O)
public void release() {
if (whisperContext != null) {
try {
whisperContext.release();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
whisperContext = null;
}
//noting to do
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.litongjava.whisper.cpp.android.java.single;

import android.app.Application;
import android.os.Build;

import androidx.annotation.RequiresApi;

import com.blankj.utilcode.util.Utils;
import com.litongjava.whisper.cpp.android.java.bean.WhisperSegment;
import com.litongjava.whisper.cpp.android.java.utils.AssetUtils;
import com.whispercppdemo.whisper.WhisperContext;

import java.io.File;
import java.util.List;
import java.util.concurrent.ExecutionException;

//import com.litongjava.whisper.android.java.bean.WhisperSegment;
//import com.litongjava.whisper.android.java.utils.AssetUtils;

public enum LocalWhisper {
INSTANCE;

public static final String modelFilePath = "models/ggml-tiny.bin";
private WhisperContext whisperContext;

@RequiresApi(api = Build.VERSION_CODES.O)
LocalWhisper() {
Application context = Utils.getApp();
File filesDir = context.getFilesDir();
File modelFile = AssetUtils.copyFileIfNotExists(context, filesDir, modelFilePath);
String realModelFilePath = modelFile.getAbsolutePath();
whisperContext = WhisperContext.createContextFromFile(realModelFilePath);
}

public synchronized String transcribeData(float[] data) throws ExecutionException, InterruptedException {
return whisperContext.transcribeData(data);
}

public List<WhisperSegment> transcribeDataWithTime(float[] audioData) throws ExecutionException, InterruptedException {
return whisperContext.transcribeDataWithTime(audioData);
}

public void init() {
//noting to do.but init
}


}
48 changes: 44 additions & 4 deletions app/src/main/java/com/whispercppdemo/whisper/WhisperContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@

import androidx.annotation.RequiresApi;

import com.litongjava.whisper.cpp.android.java.bean.WhisperSegment;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
Expand All @@ -33,17 +37,53 @@ public String call() throws Exception {
}
int numThreads = WhisperCpuConfig.getPreferredThreadCount();
Log.d(LOG_TAG, "Selecting " + numThreads + " threads");
WhisperLib.fullTranscribe(ptr, numThreads, data);
int textCount = WhisperLib.getTextSegmentCount(ptr);

StringBuilder result = new StringBuilder();
for (int i = 0; i < textCount; i++) {
result.append(WhisperLib.getTextSegment(ptr, i));
synchronized (this) {

WhisperLib.fullTranscribe(ptr, numThreads, data);
int textCount = WhisperLib.getTextSegmentCount(ptr);
for (int i = 0; i < textCount; i++) {
String sentence = WhisperLib.getTextSegment(ptr, i);
result.append(sentence);
}
}
return result.toString();
}
}).get();
}

public List<WhisperSegment> transcribeDataWithTime(float[] data) throws ExecutionException, InterruptedException {
return executorService.submit(new Callable<List<WhisperSegment>>() {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public List<WhisperSegment> call() throws Exception {
if (ptr == 0L) {
throw new IllegalStateException();
}
int numThreads = WhisperCpuConfig.getPreferredThreadCount();
Log.d(LOG_TAG, "Selecting " + numThreads + " threads");

List<WhisperSegment> segments = new ArrayList<>();
synchronized (this) {
// StringBuilder result = new StringBuilder();
WhisperLib.fullTranscribe(ptr, numThreads, data);
int textCount = WhisperLib.getTextSegmentCount(ptr);
for (int i = 0; i < textCount; i++) {
long start = WhisperLib.getTextSegmentT0(ptr, i);
String sentence = WhisperLib.getTextSegment(ptr, i);
long end = WhisperLib.getTextSegmentT1(ptr, i);
// result.append();
segments.add(new WhisperSegment(start, end, sentence));

}
// return result.toString();
}
return segments;
}
}).get();
}

@RequiresApi(api = Build.VERSION_CODES.O)
public String benchMemory(int nthreads) throws ExecutionException, InterruptedException {
return executorService.submit(() -> WhisperLib.benchMemcpy(nthreads)).get();
Expand Down
24 changes: 8 additions & 16 deletions app/src/main/java/com/whispercppdemo/whisper/WhisperLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,45 @@

import androidx.annotation.RequiresApi;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;

@RequiresApi(api = Build.VERSION_CODES.O)
public class WhisperLib {
private static final String LOG_TAG = "LibWhisper";
private static Logger log = LoggerFactory.getLogger(WhisperLib.class);

static {

Log.d(LOG_TAG, "Primary ABI: " + Build.SUPPORTED_ABIS[0]);
log.info("Primary ABI: " + Build.SUPPORTED_ABIS[0]);
boolean loadVfpv4 = false;
boolean loadV8fp16 = false;
if (Utils.isArmEabiV7a()) {
String cpuInfo = Utils.cpuInfo();
if (WhisperUtils.isArmEabiV7a()) {
String cpuInfo = WhisperUtils.cpuInfo();
if (cpuInfo != null) {
Log.d(LOG_TAG, "CPU info: " + cpuInfo);
log.info("CPU info: " + cpuInfo);
if (cpuInfo.contains("vfpv4")) {
Log.d(LOG_TAG, "CPU supports vfpv4");
log.info("CPU supports vfpv4");
loadVfpv4 = true;
}
}
} else if (Utils.isArmEabiV8a()) {
String cpuInfo = Utils.cpuInfo();
} else if (WhisperUtils.isArmEabiV8a()) {
String cpuInfo = WhisperUtils.cpuInfo();
if (cpuInfo != null) {
Log.d(LOG_TAG, "CPU info: " + cpuInfo);
log.info("CPU info: " + cpuInfo);
if (cpuInfo.contains("fphp")) {
Log.d(LOG_TAG, "CPU supports fp16 arithmetic");
log.info("CPU supports fp16 arithmetic");
loadV8fp16 = true;
}
}
}

if (loadVfpv4) {
Log.d(LOG_TAG, "Loading libwhisper_vfpv4.so");
log.info("Loading libwhisper_vfpv4.so");
System.loadLibrary("whisper_vfpv4");
} else if (loadV8fp16) {
Log.d(LOG_TAG, "Loading libwhisper_v8fp16_va.so");
log.info("Loading libwhisper_v8fp16_va.so");
System.loadLibrary("whisper_v8fp16_va");
} else {
Log.d(LOG_TAG, "Loading libwhisper.so");
log.info("Loading libwhisper.so");
System.loadLibrary("whisper");
}
}
Expand All @@ -75,6 +63,10 @@ public class WhisperLib {

public static native String getTextSegment(long contextPtr, int index);

public static native long getTextSegmentT0(long contextPtr, int index);

public static native long getTextSegmentT1(long contextPtr, int index);

public static native String getSystemInfo();

public static native String benchMemcpy(int nthread);
Expand Down
Loading

0 comments on commit 42814f3

Please sign in to comment.