import { AudioFeatures } from "./audio-features.js"; import { VAD } from "./vad.js"; export * from "./models.js"; export { AudioFeatures } from "./audio-features.js"; export { VAD } from "./vad.js"; /** Options for {@link configureOrt}. */ export interface ConfigureOrtOptions { /** Base URL/path for the ONNX Runtime Web wasm binaries. */ wasmPaths?: string; /** Number of wasm threads. Use 1 to avoid requiring COOP/COEP headers. */ numThreads?: number; /** Enable SIMD wasm. */ simd?: boolean; } /** * Configure the ONNX Runtime Web environment. Call once before * {@link OpenWakeWord.create} to point at self-hosted wasm or tweak threading. */ export function configureOrt(opts?: ConfigureOrtOptions): void; /** A custom wake word model supplied by URL. */ export interface CustomWakewordModel { name: string; url: string; inputFrames?: number; classMapping?: Record; } /** Wake word model reference: a pre-trained name, or a custom model by URL. */ export type WakewordModelRef = string | CustomWakewordModel; /** Payload passed to {@link OpenWakeWordOptions.onDetection}. */ export interface DetectionEvent { /** The wake word label that was detected. */ label: string; /** Detection score in 0..1. */ score: number; } /** * Payload passed to {@link OpenWakeWordOptions.onUtterance}. * Contains the audio recorded from the moment of wake word detection * until VAD reports speech has ended. */ export interface UtteranceEvent { /** The wake word label that triggered this capture. */ label: string; /** 16 kHz 16-bit PCM audio of the utterance (wake word + command). */ audio: Int16Array; } /** Options for {@link OpenWakeWord.create}. */ export interface OpenWakeWordOptions { /** Base URL/path for model files. Default `"./models/"`. */ baseUrl?: string; /** * Wake word models to load. Strings are looked up in the pre-trained registry * (e.g. `"hey_jarvis"`); objects load a custom model by URL. Defaults to all * pre-trained models. */ wakewordModels?: WakewordModelRef[]; /** Override the melspectrogram model URL. */ melspectrogramUrl?: string; /** Override the embedding model URL. */ embeddingUrl?: string; /** * Override the Silero VAD model URL. Only needed when {@link onUtterance} is * set and `baseUrl` does not point at a directory containing `silero_vad.onnx`. */ vadUrl?: string; /** ONNX Runtime execution providers. Default `["wasm"]`. */ executionProviders?: string[]; /** Options forwarded to {@link configureOrt}. */ ort?: ConfigureOrtOptions; /** * Score threshold for triggering {@link onDetection} and for starting utterance * capture. Default `0.5`. Also stored as `oww.threshold` so it can be changed * at runtime. */ threshold?: number; /** * Called from within {@link OpenWakeWord.predict} whenever a label's score * meets or exceeds {@link threshold}. May be called multiple times per * `predict()` invocation if several labels fire simultaneously. */ onDetection?: (event: DetectionEvent) => void; /** * When provided, the library loads `silero_vad.onnx` and enables utterance * capture. After a wake word is detected, audio is buffered until VAD reports * speech has ended, then this callback is fired with the full utterance. * * Feed the returned `audio` to a speech-to-text model to get the user's command. */ onUtterance?: (event: UtteranceEvent) => void; /** * VAD score below which a predict() frame counts as silence. * Default `0.5`. Only relevant when {@link onUtterance} is set. */ vadStopThreshold?: number; /** * Number of consecutive silent predict() frames (~80 ms each) required before * the utterance is considered complete. Default `6` (~480 ms of silence). * Only relevant when {@link onUtterance} is set. */ vadStopFrames?: number; /** * Hard cap on capture duration in seconds. `onUtterance` is fired even if VAD * has not gone silent. Default `10`. Only relevant when {@link onUtterance} is set. */ maxCaptureDuration?: number; } /** * Native browser port of `openwakeword.Model`. Runs the full melspectrogram -> * embedding -> wake word pipeline client-side using ONNX Runtime Web. */ export class OpenWakeWord { features: AudioFeatures; /** Detection score threshold. Can be updated at runtime. Default `0.5`. */ threshold: number; /** Callback fired on detection. Can be replaced at runtime. */ onDetection: ((event: DetectionEvent) => void) | null; /** Callback fired when a post-wakeword utterance ends. Can be replaced at runtime. */ onUtterance: ((event: UtteranceEvent) => void) | null; /** VAD score threshold for silence detection. Can be updated at runtime. */ vadStopThreshold: number; /** Consecutive silent frames required before utterance fires. Can be updated at runtime. */ vadStopFrames: number; /** Hard cap on capture duration in seconds. Can be updated at runtime. */ maxCaptureDuration: number; /** Create and initialise a model. */ static create(opts?: OpenWakeWordOptions): Promise; /** Names of the loaded wake word models. */ readonly modelNames: string[]; /** Reset all streaming/prediction state, including any in-progress capture. */ reset(): Promise; /** * Predict wake word scores for a frame of 16-bit PCM @ 16 kHz audio (ideally * multiples of 1280 samples / 80 ms). * @returns a `{ label: score }` map, score in 0..1. */ predict(x: Int16Array): Promise>; } export default OpenWakeWord;