whisper.rn 0.3.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.ts CHANGED
@@ -24,9 +24,26 @@ if (Platform.OS === 'android') {
24
24
 
25
25
  export type { TranscribeOptions, TranscribeResult }
26
26
 
27
+
28
+ const EVENT_ON_TRANSCRIBE_PROGRESS = '@RNWhisper_onTranscribeProgress'
29
+
27
30
  const EVENT_ON_REALTIME_TRANSCRIBE = '@RNWhisper_onRealtimeTranscribe'
28
31
  const EVENT_ON_REALTIME_TRANSCRIBE_END = '@RNWhisper_onRealtimeTranscribeEnd'
29
32
 
33
+ export type TranscribeFileOptions = TranscribeOptions & {
34
+ /**
35
+ * Progress callback, the progress is between 0 and 100
36
+ */
37
+ onProgress?: (progress: number) => void
38
+ }
39
+
40
+ export type TranscribeProgressNativeEvent = {
41
+ contextId: number
42
+ jobId: number
43
+ progress: number
44
+ }
45
+
46
+ // NOTE: codegen missing TSIntersectionType support so we dont put it into the native spec
30
47
  export type TranscribeRealtimeOptions = TranscribeOptions & {
31
48
  /**
32
49
  * Realtime record max duration in seconds.
@@ -91,7 +108,7 @@ export class WhisperContext {
91
108
  /** Transcribe audio file */
92
109
  transcribe(
93
110
  filePath: string | number,
94
- options: TranscribeOptions = {},
111
+ options: TranscribeFileOptions = {},
95
112
  ): {
96
113
  /** Stop the transcribe */
97
114
  stop: () => void
@@ -113,9 +130,46 @@ export class WhisperContext {
113
130
  }
114
131
  if (path.startsWith('file://')) path = path.slice(7)
115
132
  const jobId: number = Math.floor(Math.random() * 10000)
133
+
134
+ const { onProgress, ...rest } = options
135
+ let progressListener: any
136
+ let lastProgress: number = 0
137
+ if (onProgress) {
138
+ progressListener = EventEmitter.addListener(
139
+ EVENT_ON_TRANSCRIBE_PROGRESS,
140
+ (evt: TranscribeProgressNativeEvent) => {
141
+ const { contextId, progress } = evt
142
+ if (contextId !== this.id || evt.jobId !== jobId) return
143
+ lastProgress = progress
144
+ onProgress(progress)
145
+ },
146
+ )
147
+ }
148
+ const removeProgressListener = () => {
149
+ if (progressListener) {
150
+ progressListener.remove()
151
+ progressListener = null
152
+ }
153
+ }
116
154
  return {
117
- stop: () => RNWhisper.abortTranscribe(this.id, jobId),
118
- promise: RNWhisper.transcribeFile(this.id, jobId, path, options),
155
+ stop: async () => {
156
+ await RNWhisper.abortTranscribe(this.id, jobId)
157
+ removeProgressListener()
158
+ },
159
+ promise: RNWhisper.transcribeFile(this.id, jobId, path, {
160
+ ...rest,
161
+ onProgress: !!onProgress
162
+ }).then((result) => {
163
+ removeProgressListener()
164
+ if (!result.isAborted && lastProgress !== 100) {
165
+ // Handle the case that the last progress event is not triggered
166
+ onProgress?.(100)
167
+ }
168
+ return result
169
+ }).catch((e) => {
170
+ removeProgressListener()
171
+ throw e
172
+ }),
119
173
  }
120
174
  }
121
175