Skip to content

Commit e93081f

Browse files
authored
whisper.android : update example, add field to print timestamp (ggml-org#2072)
1 parent b6bbce4 commit e93081f

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

examples/whisper.android/app/src/main/java/com/whispercppdemo/ui/main/MainScreenViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class MainScreenViewModel(private val application: Application) : ViewModel() {
145145
val start = System.currentTimeMillis()
146146
val text = whisperContext?.transcribeData(data)
147147
val elapsed = System.currentTimeMillis() - start
148-
printMessage("Done ($elapsed ms): $text\n")
148+
printMessage("Done ($elapsed ms): \n$text\n")
149149
} catch (e: Exception) {
150150
Log.w(LOG_TAG, e)
151151
printMessage("${e.localizedMessage}\n")

examples/whisper.android/lib/src/main/java/com/whispercpp/whisper/LibWhisper.kt

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,21 @@ class WhisperContext private constructor(private var ptr: Long) {
1616
Executors.newSingleThreadExecutor().asCoroutineDispatcher()
1717
)
1818

19-
suspend fun transcribeData(data: FloatArray): String = withContext(scope.coroutineContext) {
19+
suspend fun transcribeData(data: FloatArray, printTimestamp: Boolean = true): String = withContext(scope.coroutineContext) {
2020
require(ptr != 0L)
2121
val numThreads = WhisperCpuConfig.preferredThreadCount
2222
Log.d(LOG_TAG, "Selecting $numThreads threads")
2323
WhisperLib.fullTranscribe(ptr, numThreads, data)
2424
val textCount = WhisperLib.getTextSegmentCount(ptr)
2525
return@withContext buildString {
2626
for (i in 0 until textCount) {
27-
append(WhisperLib.getTextSegment(ptr, i))
27+
if (printTimestamp) {
28+
val textTimestamp = "[${toTimestamp(WhisperLib.getTextSegmentT0(ptr, i))} --> ${toTimestamp(WhisperLib.getTextSegmentT1(ptr, i))}]"
29+
val textSegment = WhisperLib.getTextSegment(ptr, i)
30+
append("$textTimestamp: $textSegment\n")
31+
} else {
32+
append(WhisperLib.getTextSegment(ptr, i))
33+
}
2834
}
2935
}
3036
}
@@ -131,12 +137,29 @@ private class WhisperLib {
131137
external fun fullTranscribe(contextPtr: Long, numThreads: Int, audioData: FloatArray)
132138
external fun getTextSegmentCount(contextPtr: Long): Int
133139
external fun getTextSegment(contextPtr: Long, index: Int): String
140+
external fun getTextSegmentT0(contextPtr: Long, index: Int): Long
141+
external fun getTextSegmentT1(contextPtr: Long, index: Int): Long
134142
external fun getSystemInfo(): String
135143
external fun benchMemcpy(nthread: Int): String
136144
external fun benchGgmlMulMat(nthread: Int): String
137145
}
138146
}
139147

148+
// 500 -> 00:05.000
149+
// 6000 -> 01:00.000
150+
private fun toTimestamp(t: Long, comma: Boolean = false): String {
151+
var msec = t * 10
152+
val hr = msec / (1000 * 60 * 60)
153+
msec -= hr * (1000 * 60 * 60)
154+
val min = msec / (1000 * 60)
155+
msec -= min * (1000 * 60)
156+
val sec = msec / 1000
157+
msec -= sec * 1000
158+
159+
val delimiter = if (comma) "," else "."
160+
return String.format("%02d:%02d:%02d%s%03d", hr, min, sec, delimiter, msec)
161+
}
162+
140163
private fun isArmEabiV7a(): Boolean {
141164
return Build.SUPPORTED_ABIS[0].equals("armeabi-v7a")
142165
}

examples/whisper.android/lib/src/main/jni/whisper/jni.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,22 @@ Java_com_whispercpp_whisper_WhisperLib_00024Companion_getTextSegment(
212212
return string;
213213
}
214214

215+
JNIEXPORT jlong JNICALL
216+
Java_com_whispercpp_whisper_WhisperLib_00024Companion_getTextSegmentT0(
217+
JNIEnv *env, jobject thiz, jlong context_ptr, jint index) {
218+
UNUSED(thiz);
219+
struct whisper_context *context = (struct whisper_context *) context_ptr;
220+
return whisper_full_get_segment_t0(context, index);
221+
}
222+
223+
JNIEXPORT jlong JNICALL
224+
Java_com_whispercpp_whisper_WhisperLib_00024Companion_getTextSegmentT1(
225+
JNIEnv *env, jobject thiz, jlong context_ptr, jint index) {
226+
UNUSED(thiz);
227+
struct whisper_context *context = (struct whisper_context *) context_ptr;
228+
return whisper_full_get_segment_t1(context, index);
229+
}
230+
215231
JNIEXPORT jstring JNICALL
216232
Java_com_whispercpp_whisper_WhisperLib_00024Companion_getSystemInfo(
217233
JNIEnv *env, jobject thiz

0 commit comments

Comments
 (0)