Skip to content

Commit

Permalink
feat(ios, ts): add onNewSegments prop
Browse files Browse the repository at this point in the history
  • Loading branch information
jhen0409 committed Sep 30, 2023
1 parent bec4c19 commit 5432799
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
3 changes: 3 additions & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ export default function App() {
onProgress: (cur) => {
log(`Transcribing progress: ${cur}%`)
},
// onNewSegments: (segments) => {
// console.log('New segments:', segments)
// },
})
setStopTranscribe({ stop })
const { result, segments } = await promise
Expand Down
9 changes: 8 additions & 1 deletion ios/RNWhisper.mm
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ - (NSDictionary *)constantsToExport
- (NSArray *)supportedEvents {
return@[
@"@RNWhisper_onTranscribeProgress",
@"@RNWhisper_onTranscribeNewSegments",
@"@RNWhisper_onRealtimeTranscribe",
@"@RNWhisper_onRealtimeTranscribeEnd",
];
Expand Down Expand Up @@ -149,7 +150,13 @@ - (NSArray *)supportedEvents {
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
// TODO
[self sendEventWithName:@"@RNWhisper_onTranscribeNewSegments"
body:@{
@"contextId": [NSNumber numberWithInt:contextId],
@"jobId": [NSNumber numberWithInt:jobId],
@"result": result
}
];
});
}
onEnd: ^(int code) {
Expand Down
2 changes: 1 addition & 1 deletion src/NativeRNWhisper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export interface Spec extends TurboModule {
contextId: number,
jobId: number,
path: string,
options: {}, // TranscribeOptions & { onProgress?: boolean }
options: {}, // TranscribeOptions & { onProgress?: boolean, onNewSegments?: boolean }
): Promise<TranscribeResult>;
startRealtimeTranscribe(
contextId: number,
Expand Down
46 changes: 44 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,34 @@ export type { TranscribeOptions, TranscribeResult }


const EVENT_ON_TRANSCRIBE_PROGRESS = '@RNWhisper_onTranscribeProgress'
const EVENT_ON_TRANSCRIBE_NEW_SEGMENTS = '@RNWhisper_onTranscribeNewSegments'

const EVENT_ON_REALTIME_TRANSCRIBE = '@RNWhisper_onRealtimeTranscribe'
const EVENT_ON_REALTIME_TRANSCRIBE_END = '@RNWhisper_onRealtimeTranscribeEnd'

export type TranscribeNewSegmentsResult = {
nNew: number
totalNNew: number
result: string
segments: TranscribeResult['segments']
}

export type TranscribeNewSegmentsNativeEvent = {
contextId: number
jobId: number
result: TranscribeNewSegmentsResult
}

// Fn -> Boolean in TranscribeFileNativeOptions
export type TranscribeFileOptions = TranscribeOptions & {
/**
* Progress callback, the progress is between 0 and 100
*/
onProgress?: (progress: number) => void
/**
* Callback when new segments are transcribed
*/
onNewSegments?: (result: TranscribeNewSegmentsResult) => void
}

export type TranscribeProgressNativeEvent = {
Expand Down Expand Up @@ -155,7 +173,8 @@ export class WhisperContext {
if (path.startsWith('file://')) path = path.slice(7)
const jobId: number = Math.floor(Math.random() * 10000)

const { onProgress, ...rest } = options
const { onProgress, onNewSegments, ...rest } = options

let progressListener: any
let lastProgress: number = 0
if (onProgress) {
Expand All @@ -175,23 +194,46 @@ export class WhisperContext {
progressListener = null
}
}

let newSegmentsListener: any
if (onNewSegments) {
newSegmentsListener = EventEmitter.addListener(
EVENT_ON_TRANSCRIBE_NEW_SEGMENTS,
(evt: TranscribeNewSegmentsNativeEvent) => {
const { contextId, result } = evt
if (contextId !== this.id || evt.jobId !== jobId) return
onNewSegments(result)
},
)
}
const removeNewSegmenetsListener = () => {
if (newSegmentsListener) {
newSegmentsListener.remove()
newSegmentsListener = null
}
}

return {
stop: async () => {
await RNWhisper.abortTranscribe(this.id, jobId)
removeProgressListener()
removeNewSegmenetsListener()
},
promise: RNWhisper.transcribeFile(this.id, jobId, path, {
...rest,
onProgress: !!onProgress
onProgress: !!onProgress,
onNewSegments: !!onNewSegments
}).then((result) => {
removeProgressListener()
removeNewSegmenetsListener()
if (!result.isAborted && lastProgress !== 100) {
// Handle the case that the last progress event is not triggered
onProgress?.(100)
}
return result
}).catch((e) => {
removeProgressListener()
removeNewSegmenetsListener()
throw e
}),
}
Expand Down

0 comments on commit 5432799

Please sign in to comment.