Skip to content

Open-source speech recognition toolkit for training, inference, streaming ASR, VAD, punctuation, speaker diarization pipelines, and OpenAI-compatible/MCP serving.

fix(onnx): return [] and reset state at the end of short VAD streams

Tanishqf90306551674
diff --git a/runtime/python/onnxruntime/funasr_onnx/utils/e2e_vad.py b/runtime/python/onnxruntime/funasr_onnx/utils/e2e_vad.py
index 41e357ee7..a991cb3ba 100644
--- a/runtime/python/onnxruntime/funasr_onnx/utils/e2e_vad.py
+++ b/runtime/python/onnxruntime/funasr_onnx/utils/e2e_vad.py
@@ -452,6 +452,10 @@ class E2EVadModel:
def GetFrameState(self, t: int) -> FrameState:
frame_state = FrameState.kFrameStateInvalid
+ # A final call can flush frames that lack a full 25 ms window (streams under 45 ms) and
+ # so a decibel value; score them as silence, as FsmnVADStreaming.GetFrameState does.
+ if t >= len(self.decibel):
+ return FrameState.kFrameStateSil
cur_decibel = self.decibel[t]
cur_snr = cur_decibel - self.noise_average_decibel
# for each frame, calc log posterior probability of each state
diff --git a/runtime/python/onnxruntime/funasr_onnx/utils/frontend.py b/runtime/python/onnxruntime/funasr_onnx/utils/frontend.py
index 54f9deb71..f150ccbf8 100644
--- a/runtime/python/onnxruntime/funasr_onnx/utils/frontend.py
+++ b/runtime/python/onnxruntime/funasr_onnx/utils/frontend.py
@@ -361,9 +361,11 @@ class WavFrontendOnline(WavFrontend):
self.lfr_splice_cache[i] = np.concatenate(
(self.lfr_splice_cache[i], feats[i]), axis=0
)
- return np.empty(0, dtype=np.float32), feats_lengths
+ # No LFR frame yet; a final call still resets the caches below.
+ feats = np.empty(0, dtype=np.float32)
else:
- if is_final:
+ # A stream without a full 25 ms window has no fbank frame to flush.
+ if is_final and self.lfr_splice_cache:
self.waveforms = (
waveforms if self.reserve_waveforms is None else self.reserve_waveforms
)
diff --git a/tests/test_funasr_onnx_vad_online_short_chunks.py b/tests/test_funasr_onnx_vad_online_short_chunks.py
index 5602e1fe3..99b9131c2 100644
--- a/tests/test_funasr_onnx_vad_online_short_chunks.py
+++ b/tests/test_funasr_onnx_vad_online_short_chunks.py
@@ -55,11 +55,12 @@ def make_audio():
return audio
-def stream(chunk_sizes):
+def stream(chunk_sizes, param_dict=None):
# Feed the audio in chunks, cycling through chunk_sizes; the last chunk is final.
model = make_model()
audio = make_audio()
- param_dict = {"in_cache": []}
+ if param_dict is None:
+ param_dict = {"in_cache": []}
results = []
offset = 0
for size in itertools.cycle(chunk_sizes):
@@ -73,6 +74,16 @@ def stream(chunk_sizes):
return results
+def short_stream(sizes, param_dict):
+ # Send silent chunks of these sizes as a whole stream; the last call is final.
+ model = make_model()
+ results = []
+ for i, size in enumerate(sizes):
+ param_dict["is_final"] = i == len(sizes) - 1
+ results.append(model(audio_in=np.zeros(size, dtype=np.float32), param_dict=param_dict))
+ return results
+
+
class FsmnVadOnlineShortChunkTest(unittest.TestCase):
def test_demo_chunk_size(self):
self.assertEqual(stream([1600]), EXPECTED)
@@ -90,6 +101,27 @@ class FsmnVadOnlineShortChunkTest(unittest.TestCase):
with self.subTest(chunk_sizes=chunk_sizes):
self.assertEqual(stream(chunk_sizes), EXPECTED)
+ def test_short_first_and_final_call(self):
+ # Under 400 samples (25 ms) the stream has no fbank frame to flush.
+ for size in (0, 160, 320, 399):
+ with self.subTest(size=size):
+ self.assertEqual(short_stream([size], {"in_cache": []}), [[]])
+
+ def test_empty_final_call_after_partial_chunk(self):
+ # 400-719 samples give one or two fbank frames but no LFR frame. The empty final call
+ # flushes them with less than one 25 ms window of waveform, so they have no decibel value.
+ for size in (400, 480, 560, 719):
+ with self.subTest(size=size):
+ self.assertEqual(short_stream([size, 0], {"in_cache": []}), [[], []])
+
+ def test_param_dict_reuse_after_short_stream(self):
+ # The final call of a short stream resets the frontend and scorer for the next stream.
+ for sizes in ([320], [480, 0], [480]):
+ with self.subTest(sizes=sizes):
+ param_dict = {"in_cache": []}
+ self.assertEqual(short_stream(sizes, param_dict), [[]] * len(sizes))
+ self.assertEqual(stream([1600], param_dict), EXPECTED)
+
if __name__ == "__main__":
unittest.main()