Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

whisper.android: update example, add field to print timestamp #2072

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@ 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")
WhisperLib.fullTranscribe(ptr, numThreads, data)
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))
}
}
}
}
Expand Down Expand Up @@ -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")
}
Expand Down
16 changes: 16 additions & 0 deletions examples/whisper.android/lib/src/main/jni/whisper/jni.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down