Files

124 lines
4.8 KiB
Python

import unittest
from pathlib import Path
import numpy as np
from vad import Config, Silero, VadSession, WINDOW
class Fake:
def new_state(self):
return np.zeros(1)
def infer(self, pcm, state):
state = state.copy() + 1
return (0.9 if pcm[0] else 0.0), state
class VadTests(unittest.TestCase):
def test_chunks_and_frames_equal(self):
data = np.concatenate([np.zeros(2048, dtype='<i2'), np.ones(4096, dtype='<i2'), np.zeros(8192, dtype='<i2')]).tobytes()
outputs = []
for size in (640, 1, 137, 65536):
session = VadSession(Fake())
events = []
for i in range(0, len(data), size):
events += session.feed(data[i:i + size])
events += session.finish()
outputs.append(events)
self.assertLess(len(session.pending), 1024)
self.assertTrue(all(x == outputs[0] for x in outputs))
self.assertEqual([e['type'] for e in outputs[0]], ['speech.start', 'speech.end', 'session.ended'])
def test_isolation(self):
model = Fake()
a, b = VadSession(model), VadSession(model)
a.feed(np.ones(1536, dtype='<i2').tobytes())
self.assertTrue(a.active)
self.assertFalse(b.active)
self.assertEqual(b.state[0], 0)
b.feed(bytes(4096))
self.assertTrue(a.active)
self.assertFalse(b.active)
def test_silero_silence_and_state(self):
model = Silero(Path('models/silero_vad.onnx'))
a, b = VadSession(model), VadSession(model)
self.assertEqual(a.feed(bytes(32000)), [])
self.assertTrue(np.all(b.state[0] == 0))
b.feed(bytes(32000))
np.testing.assert_array_equal(a.state[0], b.state[0])
a.reset()
self.assertTrue(np.all(a.state[0] == 0))
def test_reset_end(self):
a = VadSession(Fake())
a.feed(np.ones(1536, dtype='<i2').tobytes())
events = a.reset()
self.assertEqual(events[0]['reason'], 'reset')
self.assertEqual(a.samples, 0)
self.assertEqual(a.epoch, 1)
a.feed(np.ones(1536, dtype='<i2').tobytes())
self.assertEqual(a.finish()[0]['reason'], 'input_end')
with self.assertRaises(ValueError):
a.feed(bytes(2))
a.reset()
self.assertEqual(a.feed(bytes(2)), [])
def test_limits(self):
a = VadSession(Fake(), Config(max_speech_seconds=1, max_session_seconds=2))
events = a.feed(np.ones(20000, dtype='<i2').tobytes())
self.assertIn('max_duration', [e.get('reason') for e in events])
with self.assertRaises(ValueError):
a.feed(bytes(30000))
with self.assertRaises(ValueError):
VadSession(Fake()).feed(bytes(65537))
a = VadSession(Fake())
a.feed(b'\x01')
with self.assertRaises(ValueError):
a.finish()
for value in (199, 601):
with self.assertRaises(ValueError):
Config(silence_ms=value)
def test_debounce_silence_timestamps(self):
a = VadSession(Fake())
self.assertEqual(a.feed(np.ones(1024, dtype='<i2').tobytes()), [])
start = a.feed(np.ones(512, dtype='<i2').tobytes())[0]
self.assertEqual(start['sample'], 0)
self.assertEqual(start['detected_at_sample'], 1536)
ends = a.feed(bytes(1024 * 10))
self.assertEqual(ends[0]['sample'], 1536)
self.assertEqual(ends[0]['detected_at_sample'], 6656)
def test_low_threshold_ends_silence(self):
a = VadSession(Fake(), Config(threshold=0.1))
a.feed(np.ones(1536, dtype='<i2').tobytes())
events = a.feed(bytes(1024 * 10))
self.assertEqual(events[0]['reason'], 'silence')
self.assertFalse(a.active)
def test_tail_timestamps_and_audio(self):
for size in (513, 1025, 15999):
a = VadSession(Fake(), Config(max_speech_seconds=1, start_windows=2, include_preroll_audio=True))
a.feed(np.ones(size, dtype='<i2').tobytes())
events = a.finish()
for event in events:
self.assertLessEqual(event['sample'], size)
self.assertLessEqual(event['time_ms'], size / 16)
self.assertLessEqual(event['detected_at_sample'], size)
if 'audio_until_sample' in event:
self.assertLessEqual(event['audio_until_sample'], size)
import base64
self.assertEqual(len(base64.b64decode(event['preroll_pcm16_base64'])), (event['audio_until_sample'] - event['preroll_start_sample']) * 2)
self.assertEqual(events[-1]['sample'], size)
def test_preroll_audio_opt_in(self):
a = VadSession(Fake())
event = a.feed(np.ones(1536, dtype='<i2').tobytes())[0]
self.assertNotIn('preroll_pcm16_base64', event)
if __name__ == '__main__':
unittest.main()