@fciannella/nvidia-asr-client
Version:
Minimal cross-platform wrapper around NVIDIA/Riva streaming ASR WebSocket API with optional client-side silence detection.
74 lines (73 loc) • 2.78 kB
TypeScript
declare class EventEmitter {
private events;
on(event: string, listener: (...args: any[]) => void): this;
emit(event: string, ...args: any[]): boolean;
removeListener(event: string, listener: (...args: any[]) => void): this;
removeAllListeners(event?: string): this;
}
export interface NvidiaAsrOptions {
websocketUrl?: string;
languageCode?: string;
/** Seconds of inactivity (no server messages) before we auto-finalize/close. Disable with `undefined` */
silenceTimeout?: number;
/** When `true` (default) the client calls `end()` – closing the WebSocket – after the silence timer fires. Set to `false` to keep the connection open while still emitting `final` and `silence` events. */
closeOnSilence?: boolean;
/** Encoding of incoming frames passed to write(). Default 'f32' */
inputFormat?: 'f32' | 'pcm_s16' | 'g711_ulaw';
/** Sample-rate of incoming frames. */
inputSampleRate?: number;
/** Rate that will be sent to Riva (must match model). Default 16000 */
targetSampleRate?: number;
}
export interface PartialTranscriptEvent {
text: string;
serverFinal: boolean;
}
export interface FinalTranscriptEvent {
text: string;
}
export type NvidiaAsrEvent = 'partial' | 'final' | 'silence' | 'error';
/**
* Minimal wrapper around NVIDIA/Riva streaming ASR WebSocket.
* Works in browsers with native WebSocket API.
*/
export declare class NvidiaAsrClient extends EventEmitter {
private ws;
private lastActivity;
private transcript;
private silenceTimer?;
private _dbg;
readonly opts: {
websocketUrl: string;
languageCode: string;
silenceTimeout?: number;
closeOnSilence: boolean;
inputFormat: 'f32' | 'pcm_s16' | 'g711_ulaw';
inputSampleRate: number;
targetSampleRate: number;
};
constructor(opts?: NvidiaAsrOptions);
/** Opens WebSocket and sends configuration packet. */
connect(): Promise<void>;
/**
* Send a chunk of PCM float32 samples. Provide sampleRate if not 16 kHz.
*/
write(chunk: Int16Array | Float32Array | ArrayBuffer, sampleRate?: number): void;
/**
* Signal end-of-audio but keep the socket open so Riva can flush any
* remaining hypotheses. The connection will be closed automatically
* by the silence timer or you can call `end()` manually afterwards.
*/
finish(): void;
/**
* Flushes EOS marker **and** closes the WebSocket immediately. In most
* cases you should prefer `finish()` + wait for a `final` or `silence`
* event so you don't lose the last transcript.
*/
end(): void;
private handleMessage;
private setupSilenceWatcher;
private clearSilenceWatcher;
private cleanup;
}
export {};