"""Streaming Silero VAD (the v6 model bundled with faster-whisper), one state per speaker.""" from __future__ import annotations import os import numpy as np from .segmenter import WINDOW _CONTEXT = 64 class SileroVad: """Owns the ONNX session; ``stream()`` gives each speaker its own recurrent state.""" def __init__(self, path: str | None = None): import onnxruntime if path is None: from faster_whisper.utils import get_assets_path path = os.path.join(get_assets_path(), "silero_vad_v6.onnx") opts = onnxruntime.SessionOptions() opts.inter_op_num_threads = 1 opts.intra_op_num_threads = 1 opts.enable_cpu_mem_arena = False opts.log_severity_level = 4 self.session = onnxruntime.InferenceSession(path, providers=["CPUExecutionProvider"], sess_options=opts) def stream(self) -> "SileroStream": return SileroStream(self.session) class SileroStream: def __init__(self, session): self.session = session self.h = np.zeros((1, 1, 128), dtype=np.float32) self.c = np.zeros((1, 1, 128), dtype=np.float32) self.context = np.zeros(_CONTEXT, dtype=np.float32) def prob(self, window: np.ndarray) -> float: assert len(window) == WINDOW x = np.concatenate([self.context, window.astype(np.float32, copy=False)])[None, :] out, self.h, self.c = self.session.run(None, {"input": x, "h": self.h, "c": self.c}) self.context = x[0, -_CONTEXT:] return float(np.asarray(out).reshape(-1)[0])