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/README.md +9 -2
- package/android/src/main/java/com/rnwhisper/WhisperContext.java +37 -6
- package/android/src/main/jni/whisper/jni.cpp +22 -1
- package/cpp/rn-whisper.cpp +7 -0
- package/cpp/rn-whisper.h +1 -0
- package/cpp/whisper.cpp +114 -102
- package/cpp/whisper.h +6 -0
- package/ios/RNWhisper.mm +46 -15
- package/ios/RNWhisperContext.h +4 -2
- package/ios/RNWhisperContext.mm +51 -13
- package/jest/mock.js +1 -0
- package/lib/commonjs/NativeRNWhisper.js.map +1 -1
- package/lib/commonjs/index.js +42 -2
- package/lib/commonjs/index.js.map +1 -1
- package/lib/module/NativeRNWhisper.js.map +1 -1
- package/lib/module/index.js +42 -2
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/NativeRNWhisper.d.ts +3 -0
- package/lib/typescript/NativeRNWhisper.d.ts.map +1 -1
- package/lib/typescript/index.d.ts +12 -1
- package/lib/typescript/index.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/NativeRNWhisper.ts +3 -0
- package/src/index.ts +57 -3
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:
|
|
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: () =>
|
|
118
|
-
|
|
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
|
|