Skip to content

Commit

Permalink
fix: transcribeEnd event not only trig on final transcription (#59)
Browse files Browse the repository at this point in the history
* fix: transcribeEnd event not only trig on final transcription

* fix: android build

* fix: do not set isTranscribing immediately on stop
  • Loading branch information
jhen0409 authored Jun 13, 2023
1 parent e898615 commit d9092b1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 34 deletions.
26 changes: 14 additions & 12 deletions android/src/main/java/com/rnwhisper/WhisperContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,19 @@ private void fullTranscribeSamples(ReadableMap options, boolean skipCapturingChe
!isCapturing &&
nSamplesTranscribing == nSamplesOfIndex &&
sliceIndex == transcribeSliceIndex;
if (isStopped) {

if (
// If no more samples on current slice, move to next slice
nSamplesTranscribing == sliceNSamples.get(transcribeSliceIndex) &&
transcribeSliceIndex != sliceIndex
) {
transcribeSliceIndex++;
nSamplesTranscribing = 0;
}

boolean continueNeeded = !isCapturing && nSamplesTranscribing != nSamplesOfIndex;

if (isStopped && !continueNeeded) {
payload.putBoolean("isCapturing", false);
payload.putBoolean("isStoppedByAction", isStoppedByAction);
emitTranscribeEvent("@RNWhisper_onRealtimeTranscribeEnd", payload);
Expand All @@ -251,16 +263,7 @@ private void fullTranscribeSamples(ReadableMap options, boolean skipCapturingChe
emitTranscribeEvent("@RNWhisper_onRealtimeTranscribe", payload);
}

if (
// If no more samples on current slice, move to next slice
nSamplesTranscribing == sliceNSamples.get(transcribeSliceIndex) &&
transcribeSliceIndex != sliceIndex
) {
transcribeSliceIndex++;
nSamplesTranscribing = 0;
}

if (!isCapturing && nSamplesTranscribing != nSamplesOfIndex) {
if (continueNeeded) {
// If no more capturing, continue transcribing until all slices are transcribed
fullTranscribeSamples(options, true);
} else if (isStopped) {
Expand Down Expand Up @@ -368,7 +371,6 @@ public boolean isTranscribing() {
public void stopTranscribe(int jobId) {
abortTranscribe(jobId);
isCapturing = false;
isTranscribing = false;
isStoppedByAction = true;
}

Expand Down
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ PODS:
- RNFS (2.20.0):
- React-Core
- SocketRocket (0.6.0)
- whisper-rn (0.3.0-rc.2):
- whisper-rn (0.3.0-rc.3):
- React-Core
- Yoga (1.14.0)
- YogaKit (1.18.1):
Expand Down Expand Up @@ -626,7 +626,7 @@ SPEC CHECKSUMS:
ReactCommon: 85c98ab0a509e70bf5ee5d9715cf68dbf495b84c
RNFS: 4ac0f0ea233904cb798630b3c077808c06931688
SocketRocket: fccef3f9c5cedea1353a9ef6ada904fde10d6608
whisper-rn: a05d78c47352e885c5d2cf89f095156e39ab837a
whisper-rn: 7b95b2e1bca9b3ddd7e8ffbc28ec75cd84c54a94
Yoga: 065f0b74dba4832d6e328238de46eb72c5de9556
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a

Expand Down
39 changes: 19 additions & 20 deletions ios/RNWhisperContext.mm
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,26 @@ - (void)fullTranscribeSamples:(RNWhisperContextRecordState*) state {
}

nSamplesOfIndex = [[state->sliceNSamples objectAtIndex:state->transcribeSliceIndex] intValue];

bool isStopped = state->isStoppedByAction || (
!state->isCapturing &&
state->nSamplesTranscribing == nSamplesOfIndex &&
state->sliceIndex == state->transcribeSliceIndex
);

if (
state->isStoppedByAction ||
(
!state->isCapturing &&
state->nSamplesTranscribing == nSamplesOfIndex &&
state->sliceIndex == state->transcribeSliceIndex
)
// If no more samples on current slice, move to next slice
state->nSamplesTranscribing == nSamplesOfIndex &&
state->transcribeSliceIndex != state->sliceIndex
) {
state->transcribeSliceIndex++;
state->nSamplesTranscribing = 0;
}

bool continueNeeded = !state->isCapturing &&
state->nSamplesTranscribing != nSamplesOfIndex;

if (isStopped && !continueNeeded) {
NSLog(@"[RNWhisper] Transcribe end");
result[@"isStoppedByAction"] = @(state->isStoppedByAction);
result[@"isCapturing"] = @(false);
Expand All @@ -200,19 +212,7 @@ - (void)fullTranscribeSamples:(RNWhisperContextRecordState*) state {
state->transcribeHandler(state->jobId, @"transcribe", result);
}

if (
// If no more samples on current slice, move to next slice
state->nSamplesTranscribing == nSamplesOfIndex &&
state->transcribeSliceIndex != state->sliceIndex
) {
state->transcribeSliceIndex++;
state->nSamplesTranscribing = 0;
}

if (
!state->isCapturing &&
state->nSamplesTranscribing != nSamplesOfIndex
) {
if (continueNeeded) {
state->isTranscribing = true;
// Finish transcribing the rest of the samples
[self fullTranscribeSamples:state];
Expand Down Expand Up @@ -285,7 +285,6 @@ - (void)stopTranscribe:(int)jobId {
if (!self->recordState.isRealtime || !self->recordState.isCapturing) {
return;
}
self->recordState.isTranscribing = false;
self->recordState.isCapturing = false;
self->recordState.isStoppedByAction = true;
[self stopAudio];
Expand Down

0 comments on commit d9092b1

Please sign in to comment.