[唤醒灵敏度与端点恢复]:完成唤醒提示顺序和Hybrid VAD优化,包含阈值默认值、缓冲时序和测试覆盖

This commit is contained in:
mkbk
2026-06-17 21:00:28 +08:00
parent 44f708a3dc
commit 95e4996434
13 changed files with 145 additions and 29 deletions
+47
View File
@@ -157,6 +157,53 @@ class SherpaOnnxVadProvider:
self._model.reset()
class HybridVadProvider:
"""Combine local model VAD with energy fallback for live microphone variance."""
def __init__(self, primary: Any, fallback: Any) -> None:
self.primary = primary
self.fallback = fallback
self.loaded = False
self._speech_ms = 0
self._silence_ms = 0
def load(self) -> None:
self.primary.load()
self.fallback.load()
self.loaded = True
def analyze(self, frame: AudioFrame) -> VadResult:
if not self.loaded:
raise ProviderError(
ErrorCode.VAD_MODEL_LOAD_FAILED,
"hybrid VAD provider is not loaded",
False,
"hybrid-vad",
"vad",
)
primary = self.primary.analyze(frame)
fallback = self.fallback.analyze(frame)
is_speech = primary.is_speech or fallback.is_speech
frame_ms = int(frame.metadata.get("duration_ms", 20))
if is_speech:
self._speech_ms += frame_ms
self._silence_ms = 0
else:
self._silence_ms += frame_ms
return VadResult(
is_speech=is_speech,
confidence=max(primary.confidence, fallback.confidence),
speech_ms=self._speech_ms,
silence_ms=self._silence_ms,
)
def reset(self) -> None:
self._speech_ms = 0
self._silence_ms = 0
self.primary.reset()
self.fallback.reset()
@dataclass(slots=True)
class VadRecorder:
provider: Any