codezjx commited on
Commit
03fb680
·
unverified ·
1 Parent(s): 0a1cadb

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

Browse files
examples/whisper.android/app/src/main/java/com/whispercppdemo/ui/main/MainScreenViewModel.kt CHANGED
@@ -145,7 +145,7 @@ class MainScreenViewModel(private val application: Application) : ViewModel() {
145
  val start = System.currentTimeMillis()
146
  val text = whisperContext?.transcribeData(data)
147
  val elapsed = System.currentTimeMillis() - start
148
- printMessage("Done ($elapsed ms): $text\n")
149
  } catch (e: Exception) {
150
  Log.w(LOG_TAG, e)
151
  printMessage("${e.localizedMessage}\n")
 
145
  val start = System.currentTimeMillis()
146
  val text = whisperContext?.transcribeData(data)
147
  val elapsed = System.currentTimeMillis() - start
148
+ printMessage("Done ($elapsed ms): \n$text\n")
149
  } catch (e: Exception) {
150
  Log.w(LOG_TAG, e)
151
  printMessage("${e.localizedMessage}\n")
examples/whisper.android/lib/src/main/java/com/whispercpp/whisper/LibWhisper.kt CHANGED
@@ -16,7 +16,7 @@ class WhisperContext private constructor(private var ptr: Long) {
16
  Executors.newSingleThreadExecutor().asCoroutineDispatcher()
17
  )
18
 
19
- suspend fun transcribeData(data: FloatArray): String = withContext(scope.coroutineContext) {
20
  require(ptr != 0L)
21
  val numThreads = WhisperCpuConfig.preferredThreadCount
22
  Log.d(LOG_TAG, "Selecting $numThreads threads")
@@ -24,7 +24,13 @@ class WhisperContext private constructor(private var ptr: Long) {
24
  val textCount = WhisperLib.getTextSegmentCount(ptr)
25
  return@withContext buildString {
26
  for (i in 0 until textCount) {
27
- append(WhisperLib.getTextSegment(ptr, i))
 
 
 
 
 
 
28
  }
29
  }
30
  }
@@ -131,12 +137,29 @@ private class WhisperLib {
131
  external fun fullTranscribe(contextPtr: Long, numThreads: Int, audioData: FloatArray)
132
  external fun getTextSegmentCount(contextPtr: Long): Int
133
  external fun getTextSegment(contextPtr: Long, index: Int): String
 
 
134
  external fun getSystemInfo(): String
135
  external fun benchMemcpy(nthread: Int): String
136
  external fun benchGgmlMulMat(nthread: Int): String
137
  }
138
  }
139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  private fun isArmEabiV7a(): Boolean {
141
  return Build.SUPPORTED_ABIS[0].equals("armeabi-v7a")
142
  }
 
16
  Executors.newSingleThreadExecutor().asCoroutineDispatcher()
17
  )
18
 
19
+ suspend fun transcribeData(data: FloatArray, printTimestamp: Boolean = true): String = withContext(scope.coroutineContext) {
20
  require(ptr != 0L)
21
  val numThreads = WhisperCpuConfig.preferredThreadCount
22
  Log.d(LOG_TAG, "Selecting $numThreads threads")
 
24
  val textCount = WhisperLib.getTextSegmentCount(ptr)
25
  return@withContext buildString {
26
  for (i in 0 until textCount) {
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
+ }
34
  }
35
  }
36
  }
 
137
  external fun fullTranscribe(contextPtr: Long, numThreads: Int, audioData: FloatArray)
138
  external fun getTextSegmentCount(contextPtr: Long): Int
139
  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
142
  external fun getSystemInfo(): String
143
  external fun benchMemcpy(nthread: Int): String
144
  external fun benchGgmlMulMat(nthread: Int): String
145
  }
146
  }
147
 
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
+
163
  private fun isArmEabiV7a(): Boolean {
164
  return Build.SUPPORTED_ABIS[0].equals("armeabi-v7a")
165
  }
examples/whisper.android/lib/src/main/jni/whisper/jni.c CHANGED
@@ -212,6 +212,22 @@ Java_com_whispercpp_whisper_WhisperLib_00024Companion_getTextSegment(
212
  return string;
213
  }
214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
215
  JNIEXPORT jstring JNICALL
216
  Java_com_whispercpp_whisper_WhisperLib_00024Companion_getSystemInfo(
217
  JNIEnv *env, jobject thiz
 
212
  return string;
213
  }
214
 
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
+
231
  JNIEXPORT jstring JNICALL
232
  Java_com_whispercpp_whisper_WhisperLib_00024Companion_getSystemInfo(
233
  JNIEnv *env, jobject thiz