Skip to content

Commit

Permalink
feat: minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jhen0409 committed Nov 2, 2024
1 parent e17b3e9 commit bef528b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
15 changes: 8 additions & 7 deletions android/src/main/java/com/rnwhisper/WhisperContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public class WhisperContext {
private boolean isCapturing = false;
private boolean isStoppedByAction = false;
private boolean isTranscribing = false;
private boolean isTdrzEnable = false;
private Thread rootFullHandler = null;
private Thread fullHandler = null;
private ReadableMap options;

public WhisperContext(int id, ReactApplicationContext reactContext, long context) {
this.id = id;
Expand All @@ -74,6 +74,7 @@ private void rewind() {
isCapturing = false;
isStoppedByAction = false;
isTranscribing = false;
isTdrzEnable = false;
rootFullHandler = null;
fullHandler = null;
}
Expand Down Expand Up @@ -104,7 +105,7 @@ public int startRealtimeTranscribe(int jobId, ReadableMap options) {
rewind();

this.jobId = jobId;
this.options = options;

int realtimeAudioSec = options.hasKey("realtimeAudioSec") ? options.getInt("realtimeAudioSec") : 0;
final int audioSec = realtimeAudioSec > 0 ? realtimeAudioSec : DEFAULT_MAX_AUDIO_SEC;
int realtimeAudioSliceSec = options.hasKey("realtimeAudioSliceSec") ? options.getInt("realtimeAudioSliceSec") : 0;
Expand All @@ -114,6 +115,8 @@ public int startRealtimeTranscribe(int jobId, ReadableMap options) {
double realtimeAudioMinSec = options.hasKey("realtimeAudioMinSec") ? options.getDouble("realtimeAudioMinSec") : 0;
final double audioMinSec = realtimeAudioMinSec > 0.5 && realtimeAudioMinSec <= audioSliceSec ? realtimeAudioMinSec : 1;

this.isTdrzEnable = options.hasKey("tdrzEnable") && options.getBoolean("tdrzEnable");

createRealtimeTranscribeJob(jobId, context, options);

sliceNSamples = new ArrayList<Integer>();
Expand Down Expand Up @@ -334,8 +337,9 @@ public WritableMap transcribeInputStream(int jobId, InputStream inputStream, Rea
throw new Exception("Context is already in capturing or transcribing");
}
rewind();
this.options = options;
this.jobId = jobId;
this.isTdrzEnable = options.hasKey("tdrzEnable") && options.getBoolean("tdrzEnable");

isTranscribing = true;
float[] audioData = AudioUtils.decodeWaveFile(inputStream);

Expand Down Expand Up @@ -370,14 +374,11 @@ private WritableMap getTextSegments(int start, int count) {
WritableMap data = Arguments.createMap();
WritableArray segments = Arguments.createArray();

// Check if tdrzEnable is enabled
boolean tdrzEnable = options != null && options.hasKey("tdrzEnable") && options.getBoolean("tdrzEnable");

for (int i = 0; i < count; i++) {
String text = getTextSegment(context, i);

// If tdrzEnable is enabled and speaker turn is detected
if (tdrzEnable && getTextSegmentSpeakerTurnNext(context, i)) {
if (this.tdrzEnable && getTextSegmentSpeakerTurnNext(context, i)) {
text += " [SPEAKER_TURN]";
}

Expand Down
3 changes: 1 addition & 2 deletions ios/RNWhisperContext.mm
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#import "RNWhisperContext.h"
#include "whisper.h"
#import <Metal/Metal.h>
#include <vector>

Expand Down Expand Up @@ -543,7 +542,7 @@ - (NSMutableDictionary *)getTextSegments {
NSMutableString *mutable_ns_text = [NSMutableString stringWithUTF8String:text_cur];

// Simplified condition
if (self->recordState.options[@"tdrzEnable"] &&
if (self->recordState.options[@"tdrzEnable"] &&
[self->recordState.options[@"tdrzEnable"] boolValue] &&
whisper_full_get_segment_speaker_turn_next(self->ctx, i)) {
[mutable_ns_text appendString:@" [SPEAKER_TURN]"];
Expand Down

0 comments on commit bef528b

Please sign in to comment.