@@ -3,6 +3,7 @@ import { createHash, randomUUID } from "node:crypto";
33import { createReadStream , mkdirSync , renameSync , rmSync , statSync } from "node:fs" ;
44import { homedir } from "node:os" ;
55import { join } from "node:path" ;
6+ import { createInterface } from "node:readline" ;
67import { fileURLToPath } from "node:url" ;
78import { downloadToFile } from "../cloud/download.js" ;
89import { stoppedByCancelSignal } from "../utils/renderCancellation.js" ;
@@ -20,10 +21,12 @@ import {
2021 mergeWindowsToWords ,
2122 SHERPA_ERROR_PREFIX ,
2223 SHERPA_RESULT_PREFIX ,
24+ SHERPA_WINDOW_PREFIX ,
2325 PARAKEET_MODEL_LABEL ,
2426 writeParakeetTranscript ,
2527 type SherpaWindow ,
2628} from "./parakeet.js" ;
29+ import { emitWords } from "./progress.js" ;
2730import {
2831 getPreparedWavDurationSeconds ,
2932 prepareWav ,
@@ -394,13 +397,14 @@ function decode(
394397 wavPath : string ,
395398 runtimePath : string ,
396399 signal : AbortSignal ,
400+ onWindow ?: ( window : SherpaWindow , through : number ) => void ,
397401) : Promise < SherpaWindow [ ] > {
398402 const sourceMode = import . meta. url . endsWith ( ".ts" ) ;
399403 const worker = new URL ( sourceMode ? "./sherpaWorker.ts" : "./sherpaWorker.js" , import . meta. url ) ;
400404 const args = [ ...( sourceMode ? [ "--import" , "tsx" ] : [ ] ) , fileURLToPath ( worker ) ] ;
401405 const input = { wavPath, runtimePath, config : recognizerConfig ( ) } ;
402406 return new Promise ( ( resolve , reject ) => {
403- execFile (
407+ const child = execFile (
404408 process . execPath ,
405409 args ,
406410 {
@@ -419,6 +423,18 @@ function decode(
419423 else reject ( new Error ( `Parakeet decoder ${ failureReason ( err , stderr ) } ` ) ) ;
420424 } ,
421425 ) ;
426+ if ( onWindow && child . stdout ) {
427+ createInterface ( { input : child . stdout } ) . on ( "line" , ( l ) => {
428+ if ( ! l . startsWith ( SHERPA_WINDOW_PREFIX ) ) return ;
429+ let streamed : { window : SherpaWindow ; through : number } ;
430+ try {
431+ streamed = JSON . parse ( l . slice ( SHERPA_WINDOW_PREFIX . length ) ) ;
432+ } catch {
433+ return ;
434+ }
435+ onWindow ( streamed . window , streamed . through ) ;
436+ } ) ;
437+ }
422438 } ) ;
423439}
424440
@@ -455,6 +471,7 @@ export async function transcribeWithSherpa(
455471 phase : "transcription" ,
456472 model : PARAKEET_MODEL_LABEL ,
457473 status : "started" ,
474+ durationSeconds : getPreparedWavDurationSeconds ( wavPath ) ,
458475 } ) ;
459476 if ( options . signal . aborted ) throw new DecodeCancelled ( "Transcription cancelled" ) ;
460477 const selected = await selectRuntime (
@@ -468,7 +485,15 @@ export async function transcribeWithSherpa(
468485 `Parakeet runtime does not load (${ selected . error } ). Run hyperframes models install parakeet to repair it, or use --engine whisper.` ,
469486 ) ;
470487 }
471- const windows = await decode ( wavPath , selected . path , options . signal ) ;
488+ const onEvent = options . onEvent ;
489+ const windows = await decode (
490+ wavPath ,
491+ selected . path ,
492+ options . signal ,
493+ onEvent &&
494+ ( ( window , through ) =>
495+ emitWords ( onEvent , PARAKEET_MODEL_LABEL , mergeWindowsToWords ( [ window ] ) , through ) ) ,
496+ ) ;
472497 const result = writeParakeetTranscript ( dir , mergeWindowsToWords ( windows ) ) ;
473498 options . onEvent ?.( {
474499 type : "progress" ,
0 commit comments