-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream_vad.py
More file actions
171 lines (145 loc) · 5.59 KB
/
Copy pathstream_vad.py
File metadata and controls
171 lines (145 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"""Silero voice-activity detection and a loss-resistant streaming audio gate.
The gate intentionally does not decide utterance boundaries. It ships pre-roll
when speech begins and trailing silence after speech ends, leaving Nemotron's
endpointer enough received audio to produce FINAL events. Only sustained idle
silence is withheld.
"""
from __future__ import annotations
from collections import deque
from dataclasses import dataclass
from pathlib import Path
from typing import Protocol
import numpy as np
SAMPLE_RATE = 16_000
WINDOW_SAMPLES = 512
CONTEXT_SAMPLES = 64
STATE_SHAPE = (2, 1, 128)
@dataclass(frozen=True)
class VadResult:
gate_open: bool
speech_started: bool = False
speech_ended: bool = False
probability: float = 0.0
class VoiceDetector(Protocol):
def process_pcm16(self, pcm: bytes) -> VadResult: ...
class SileroVad:
"""Silero VAD v5 ONNX runner using the model's required 64-sample context."""
def __init__(
self,
model_path: str | Path,
*,
enter: float = 0.5,
exit: float = 0.35,
hang_windows: int = 15,
) -> None:
try:
import onnxruntime as ort
except ImportError as exc:
raise RuntimeError(
"VAD is enabled but onnxruntime is not installed in mumble's venv"
) from exc
path = Path(model_path)
if not path.is_file():
raise RuntimeError(f"Silero VAD model not found: {path}")
self.session = ort.InferenceSession(
str(path), providers=["CPUExecutionProvider"]
)
self.enter = enter
self.exit = exit
self.hang_windows = hang_windows
self.state = np.zeros(STATE_SHAPE, dtype=np.float32)
self.context = np.zeros(CONTEXT_SAMPLES, dtype=np.float32)
self.pending = np.empty(0, dtype=np.float32)
self.in_speech = False
self.below_exit = 0
def _window_probability(self, window: np.ndarray) -> float:
model_input = np.concatenate((self.context, window))[None, :]
self.context = window[-CONTEXT_SAMPLES:].copy()
probability, self.state = self.session.run(
["output", "stateN"],
{
"input": model_input,
"state": self.state,
"sr": np.asarray(SAMPLE_RATE, dtype=np.int64),
},
)
return float(np.asarray(probability).reshape(-1)[0])
def process_pcm16(self, pcm: bytes) -> VadResult:
if len(pcm) % 2:
raise ValueError("PCM s16le audio must contain complete samples")
samples = np.frombuffer(pcm, dtype="<i2").astype(np.float32) / 32768.0
if self.pending.size:
samples = np.concatenate((self.pending, samples))
started = False
ended = False
latest_probability = 0.0
offset = 0
while samples.size - offset >= WINDOW_SAMPLES:
window = samples[offset : offset + WINDOW_SAMPLES]
offset += WINDOW_SAMPLES
latest_probability = self._window_probability(window)
if self.in_speech:
if latest_probability >= self.exit:
self.below_exit = 0
else:
self.below_exit += 1
if self.below_exit >= self.hang_windows:
self.in_speech = False
self.below_exit = 0
ended = True
elif latest_probability >= self.enter:
self.in_speech = True
self.below_exit = 0
started = True
self.pending = samples[offset:].copy()
return VadResult(self.in_speech, started, ended, latest_probability)
class StreamingVadGate:
"""Select capture chunks to send, with pre-roll, hangover, and fail-open."""
def __init__(
self,
detector: VoiceDetector,
*,
sample_rate: int = SAMPLE_RATE,
channels: int = 1,
pre_roll_ms: int = 1000,
trailing_ms: int = 3000,
) -> None:
self.detector = detector
self.bytes_per_ms = sample_rate * channels * 2 / 1000
self.pre_roll_bytes = int(self.bytes_per_ms * pre_roll_ms)
self.trailing_bytes = int(self.bytes_per_ms * trailing_ms)
self.pre_roll: deque[bytes] = deque()
self.pre_roll_size = 0
self.trailing_remaining = 0
self.failed = False
def _hold(self, chunk: bytes) -> None:
if self.pre_roll_bytes == 0:
return
self.pre_roll.append(chunk)
self.pre_roll_size += len(chunk)
while self.pre_roll and self.pre_roll_size > self.pre_roll_bytes:
self.pre_roll_size -= len(self.pre_roll.popleft())
def feed(self, chunk: bytes) -> list[bytes]:
"""Return zero or more chunks to ship for this captured audio chunk."""
if self.failed:
return [chunk]
try:
result = self.detector.process_pcm16(chunk)
except Exception:
# Optimization must never turn a VAD fault into lost dictation.
self.failed = True
held = list(self.pre_roll)
self.pre_roll.clear()
self.pre_roll_size = 0
return [*held, chunk]
if result.gate_open:
self.trailing_remaining = self.trailing_bytes
held = list(self.pre_roll)
self.pre_roll.clear()
self.pre_roll_size = 0
return [*held, chunk]
if self.trailing_remaining > 0:
self.trailing_remaining = max(0, self.trailing_remaining - len(chunk))
return [chunk]
self._hold(chunk)
return []