mirror of
https://github.com/ggerganov/whisper.cpp.git
synced 2024-12-18 20:27:53 +00:00
whisper.android : update example, add field to print timestamp (#2072)
This commit is contained in:
parent
b6bbce4ae9
commit
e93081f83f
@ -145,7 +145,7 @@ class MainScreenViewModel(private val application: Application) : ViewModel() {
|
||||
val start = System.currentTimeMillis()
|
||||
val text = whisperContext?.transcribeData(data)
|
||||
val elapsed = System.currentTimeMillis() - start
|
||||
printMessage("Done ($elapsed ms): $text\n")
|
||||
printMessage("Done ($elapsed ms): \n$text\n")
|
||||
} catch (e: Exception) {
|
||||
Log.w(LOG_TAG, e)
|
||||
printMessage("${e.localizedMessage}\n")
|
||||
|
@ -16,7 +16,7 @@ class WhisperContext private constructor(private var ptr: Long) {
|
||||
Executors.newSingleThreadExecutor().asCoroutineDispatcher()
|
||||
)
|
||||
|
||||
suspend fun transcribeData(data: FloatArray): String = withContext(scope.coroutineContext) {
|
||||
suspend fun transcribeData(data: FloatArray, printTimestamp: Boolean = true): String = withContext(scope.coroutineContext) {
|
||||
require(ptr != 0L)
|
||||
val numThreads = WhisperCpuConfig.preferredThreadCount
|
||||
Log.d(LOG_TAG, "Selecting $numThreads threads")
|
||||
@ -24,7 +24,13 @@ class WhisperContext private constructor(private var ptr: Long) {
|
||||
val textCount = WhisperLib.getTextSegmentCount(ptr)
|
||||
return@withContext buildString {
|
||||
for (i in 0 until textCount) {
|
||||
append(WhisperLib.getTextSegment(ptr, i))
|
||||
if (printTimestamp) {
|
||||
val textTimestamp = "[${toTimestamp(WhisperLib.getTextSegmentT0(ptr, i))} --> ${toTimestamp(WhisperLib.getTextSegmentT1(ptr, i))}]"
|
||||
val textSegment = WhisperLib.getTextSegment(ptr, i)
|
||||
append("$textTimestamp: $textSegment\n")
|
||||
} else {
|
||||
append(WhisperLib.getTextSegment(ptr, i))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -131,12 +137,29 @@ private class WhisperLib {
|
||||
external fun fullTranscribe(contextPtr: Long, numThreads: Int, audioData: FloatArray)
|
||||
external fun getTextSegmentCount(contextPtr: Long): Int
|
||||
external fun getTextSegment(contextPtr: Long, index: Int): String
|
||||
external fun getTextSegmentT0(contextPtr: Long, index: Int): Long
|
||||
external fun getTextSegmentT1(contextPtr: Long, index: Int): Long
|
||||
external fun getSystemInfo(): String
|
||||
external fun benchMemcpy(nthread: Int): String
|
||||
external fun benchGgmlMulMat(nthread: Int): String
|
||||
}
|
||||
}
|
||||
|
||||
// 500 -> 00:05.000
|
||||
// 6000 -> 01:00.000
|
||||
private fun toTimestamp(t: Long, comma: Boolean = false): String {
|
||||
var msec = t * 10
|
||||
val hr = msec / (1000 * 60 * 60)
|
||||
msec -= hr * (1000 * 60 * 60)
|
||||
val min = msec / (1000 * 60)
|
||||
msec -= min * (1000 * 60)
|
||||
val sec = msec / 1000
|
||||
msec -= sec * 1000
|
||||
|
||||
val delimiter = if (comma) "," else "."
|
||||
return String.format("%02d:%02d:%02d%s%03d", hr, min, sec, delimiter, msec)
|
||||
}
|
||||
|
||||
private fun isArmEabiV7a(): Boolean {
|
||||
return Build.SUPPORTED_ABIS[0].equals("armeabi-v7a")
|
||||
}
|
||||
|
@ -212,6 +212,22 @@ Java_com_whispercpp_whisper_WhisperLib_00024Companion_getTextSegment(
|
||||
return string;
|
||||
}
|
||||
|
||||
JNIEXPORT jlong JNICALL
|
||||
Java_com_whispercpp_whisper_WhisperLib_00024Companion_getTextSegmentT0(
|
||||
JNIEnv *env, jobject thiz, jlong context_ptr, jint index) {
|
||||
UNUSED(thiz);
|
||||
struct whisper_context *context = (struct whisper_context *) context_ptr;
|
||||
return whisper_full_get_segment_t0(context, index);
|
||||
}
|
||||
|
||||
JNIEXPORT jlong JNICALL
|
||||
Java_com_whispercpp_whisper_WhisperLib_00024Companion_getTextSegmentT1(
|
||||
JNIEnv *env, jobject thiz, jlong context_ptr, jint index) {
|
||||
UNUSED(thiz);
|
||||
struct whisper_context *context = (struct whisper_context *) context_ptr;
|
||||
return whisper_full_get_segment_t1(context, index);
|
||||
}
|
||||
|
||||
JNIEXPORT jstring JNICALL
|
||||
Java_com_whispercpp_whisper_WhisperLib_00024Companion_getSystemInfo(
|
||||
JNIEnv *env, jobject thiz
|
||||
|
Loading…
Reference in New Issue
Block a user