Skip to content

Commit

Permalink
fix: abort transcribe file
Browse files Browse the repository at this point in the history
  • Loading branch information
jhen0409 committed Aug 8, 2023
1 parent 6b8e594 commit 82ff81b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 21 deletions.
7 changes: 7 additions & 0 deletions cpp/rn-whisper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ void rn_whisper_abort_transcribe(int job_id) {
}
}

bool rn_whisper_transcribe_is_aborted(int job_id) {
if (abort_map.find(job_id) != abort_map.end()) {
return abort_map[job_id];
}
return false;
}

void rn_whisper_abort_all_transcribe() {
for (auto it = abort_map.begin(); it != abort_map.end(); ++it) {
it->second = true;
Expand Down
1 change: 1 addition & 0 deletions cpp/rn-whisper.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extern "C" {
bool* rn_whisper_assign_abort_map(int job_id);
void rn_whisper_remove_abort_map(int job_id);
void rn_whisper_abort_transcribe(int job_id);
bool rn_whisper_transcribe_is_aborted(int job_id);
void rn_whisper_abort_all_transcribe();

#ifdef __cplusplus
Expand Down
53 changes: 34 additions & 19 deletions ios/RNWhisper.mm
Original file line number Diff line number Diff line change
Expand Up @@ -122,27 +122,34 @@ - (NSArray *)supportedEvents {
reject(@"whisper_error", @"Invalid file", nil);
return;
}
int code = [context transcribeFile:jobId
audioData:waveFile
audioDataCount:count
options:options
onProgress: ^(int progress) {
[self sendEventWithName:@"@RNWhisper_onTranscribeProgress"
body:@{
@"contextId": [NSNumber numberWithInt:contextId],
@"jobId": [NSNumber numberWithInt:jobId],
@"progress": [NSNumber numberWithInt:progress]
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
int code = [context transcribeFile:jobId
audioData:waveFile
audioDataCount:count
options:options
onProgress: ^(int progress) {
if (rn_whisper_transcribe_is_aborted(jobId)) {
return;
}
];
dispatch_async(dispatch_get_main_queue(), ^{
[self sendEventWithName:@"@RNWhisper_onTranscribeProgress"
body:@{
@"contextId": [NSNumber numberWithInt:contextId],
@"jobId": [NSNumber numberWithInt:jobId],
@"progress": [NSNumber numberWithInt:progress]
}
];
});
}
];
if (code != 0) {
free(waveFile);
reject(@"whisper_cpp_error", [NSString stringWithFormat:@"Failed to transcribe the file. Code: %d", code], nil);
return;
}
];
if (code != 0) {
free(waveFile);
reject(@"whisper_cpp_error", [NSString stringWithFormat:@"Failed to transcribe the file. Code: %d", code], nil);
return;
}
free(waveFile);
resolve([context getTextSegments]);
resolve([context getTextSegments]);
});
}

RCT_REMAP_METHOD(startRealtimeTranscribe,
Expand Down Expand Up @@ -190,12 +197,20 @@ - (NSArray *)supportedEvents {
}
reject(@"whisper_error", [NSString stringWithFormat:@"Failed to start realtime transcribe. Status: %d", status], nil);
}

RCT_REMAP_METHOD(abortTranscribe,
withContextId:(int)contextId
withJobId:(int)jobId)
withJobId:(int)jobId
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
{
RNWhisperContext *context = contexts[[NSNumber numberWithInt:contextId]];
if (context == nil) {
reject(@"whisper_error", @"Context not found", nil);
return;
}
[context stopTranscribe:jobId];
resolve(nil);
}

RCT_REMAP_METHOD(releaseContext,
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ export class WhisperContext {
)
}
return {
stop: () => {
stop: async () => {
await RNWhisper.abortTranscribe(this.id, jobId)
progressListener?.remove()
return RNWhisper.abortTranscribe(this.id, jobId)
},
promise: RNWhisper.transcribeFile(this.id, jobId, path, {
...rest,
Expand Down

0 comments on commit 82ff81b

Please sign in to comment.