19
19
import java .util .Random ;
20
20
import java .io .File ;
21
21
import java .io .FileInputStream ;
22
+ import java .io .InputStream ;
22
23
import java .io .PushbackInputStream ;
23
24
24
25
public class RNWhisper implements LifecycleEventListener {
@@ -119,44 +120,16 @@ protected void onPostExecute(Integer id) {
119
120
tasks .put (task , "initContext" );
120
121
}
121
122
122
- public void transcribeFile (double id , double jobId , String filePath , ReadableMap options , Promise promise ) {
123
- final WhisperContext context = contexts .get ((int ) id );
124
- if (context == null ) {
125
- promise .reject ("Context not found" );
126
- return ;
127
- }
128
- if (context .isCapturing ()) {
129
- promise .reject ("The context is in realtime transcribe mode" );
130
- return ;
131
- }
132
- if (context .isTranscribing ()) {
133
- promise .reject ("Context is already transcribing" );
134
- return ;
135
- }
123
+ private AsyncTask transcribe (WhisperContext context , double jobId , final float [] audioData , final ReadableMap options , Promise promise ) {
136
124
AsyncTask task = new AsyncTask <Void , Void , WritableMap >() {
137
125
private Exception exception ;
138
126
139
127
@ Override
140
128
protected WritableMap doInBackground (Void ... voids ) {
141
129
try {
142
- String waveFilePath = filePath ;
143
-
144
- if (filePath .startsWith ("http://" ) || filePath .startsWith ("https://" )) {
145
- waveFilePath = downloader .downloadFile (filePath );
146
- }
147
-
148
- int resId = getResourceIdentifier (waveFilePath );
149
- if (resId > 0 ) {
150
- return context .transcribeInputStream (
151
- (int ) jobId ,
152
- reactContext .getResources ().openRawResource (resId ),
153
- options
154
- );
155
- }
156
-
157
- return context .transcribeInputStream (
130
+ return context .transcribe (
158
131
(int ) jobId ,
159
- new FileInputStream ( new File ( waveFilePath )) ,
132
+ audioData ,
160
133
options
161
134
);
162
135
} catch (Exception e ) {
@@ -175,7 +148,66 @@ protected void onPostExecute(WritableMap data) {
175
148
tasks .remove (this );
176
149
}
177
150
}.executeOnExecutor (AsyncTask .THREAD_POOL_EXECUTOR );
178
- tasks .put (task , "transcribeFile-" + id );
151
+ return task ;
152
+ }
153
+
154
+ public void transcribeFile (double id , double jobId , String filePathOrBase64 , ReadableMap options , Promise promise ) {
155
+ final WhisperContext context = contexts .get ((int ) id );
156
+ if (context == null ) {
157
+ promise .reject ("Context not found" );
158
+ return ;
159
+ }
160
+ if (context .isCapturing ()) {
161
+ promise .reject ("The context is in realtime transcribe mode" );
162
+ return ;
163
+ }
164
+ if (context .isTranscribing ()) {
165
+ promise .reject ("Context is already transcribing" );
166
+ return ;
167
+ }
168
+
169
+ String waveFilePath = filePathOrBase64 ;
170
+ try {
171
+ if (filePathOrBase64 .startsWith ("http://" ) || filePathOrBase64 .startsWith ("https://" )) {
172
+ waveFilePath = downloader .downloadFile (filePathOrBase64 );
173
+ }
174
+
175
+ float [] audioData ;
176
+ int resId = getResourceIdentifier (waveFilePath );
177
+ if (resId > 0 ) {
178
+ audioData = AudioUtils .decodeWaveFile (reactContext .getResources ().openRawResource (resId ));
179
+ } else if (filePathOrBase64 .startsWith ("data:audio/wav;base64," )) {
180
+ audioData = AudioUtils .decodeWaveData (filePathOrBase64 );
181
+ } else {
182
+ audioData = AudioUtils .decodeWaveFile (new FileInputStream (new File (waveFilePath )));
183
+ }
184
+
185
+ AsyncTask task = transcribe (context , jobId , audioData , options , promise );
186
+ tasks .put (task , "transcribeFile-" + id );
187
+ } catch (Exception e ) {
188
+ promise .reject (e );
189
+ }
190
+ }
191
+
192
+ public void transcribeData (double id , double jobId , String dataBase64 , ReadableMap options , Promise promise ) {
193
+ final WhisperContext context = contexts .get ((int ) id );
194
+ if (context == null ) {
195
+ promise .reject ("Context not found" );
196
+ return ;
197
+ }
198
+ if (context .isCapturing ()) {
199
+ promise .reject ("The context is in realtime transcribe mode" );
200
+ return ;
201
+ }
202
+ if (context .isTranscribing ()) {
203
+ promise .reject ("Context is already transcribing" );
204
+ return ;
205
+ }
206
+
207
+ float [] audioData = AudioUtils .decodePcmData (dataBase64 );
208
+ AsyncTask task = transcribe (context , jobId , audioData , options , promise );
209
+
210
+ tasks .put (task , "transcribeData-" + id );
179
211
}
180
212
181
213
public void startRealtimeTranscribe (double id , double jobId , ReadableMap options , Promise promise ) {
@@ -211,7 +243,7 @@ protected Void doInBackground(Void... voids) {
211
243
context .stopTranscribe ((int ) jobId );
212
244
AsyncTask completionTask = null ;
213
245
for (AsyncTask task : tasks .keySet ()) {
214
- if (tasks .get (task ).equals ("transcribeFile-" + id )) {
246
+ if (tasks .get (task ).equals ("transcribeFile-" + id ) || tasks . get ( task ). equals ( "transcribeData-" + id ) ) {
215
247
task .get ();
216
248
break ;
217
249
}
@@ -259,7 +291,7 @@ protected Void doInBackground(Void... voids) {
259
291
context .stopCurrentTranscribe ();
260
292
AsyncTask completionTask = null ;
261
293
for (AsyncTask task : tasks .keySet ()) {
262
- if (tasks .get (task ).equals ("transcribeFile-" + contextId )) {
294
+ if (tasks .get (task ).equals ("transcribeFile-" + contextId ) || tasks . get ( task ). equals ( "transcribeData-" + contextId ) ) {
263
295
task .get ();
264
296
break ;
265
297
}
0 commit comments