mirror of
https://github.com/elisspace/core.git
synced 2026-09-29 05:49:53 +00:00
Use webrcvad to detect silence in pipelines (#90610)
* Add webrtcvad requirement * Use webrcvad for voice command segmenting * Add vad test
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
"""Tests for webrtcvad voice command segmenter."""
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.components.voice_assistant.vad import VoiceCommandSegmenter
|
||||
|
||||
_ONE_SECOND = 16000 * 2 # 16Khz 16-bit
|
||||
|
||||
|
||||
def test_silence() -> None:
|
||||
"""Test that 3 seconds of silence does not trigger a voice command."""
|
||||
segmenter = VoiceCommandSegmenter()
|
||||
|
||||
# True return value indicates voice command has not finished
|
||||
assert segmenter.process(bytes(_ONE_SECOND * 3))
|
||||
|
||||
|
||||
def test_speech() -> None:
|
||||
"""Test that silence + speech + silence triggers a voice command."""
|
||||
|
||||
def is_speech(self, chunk, sample_rate):
|
||||
"""Anything non-zero is speech."""
|
||||
return sum(chunk) > 0
|
||||
|
||||
with patch(
|
||||
"webrtcvad.Vad.is_speech",
|
||||
new=is_speech,
|
||||
):
|
||||
segmenter = VoiceCommandSegmenter()
|
||||
|
||||
# silence
|
||||
assert segmenter.process(bytes(_ONE_SECOND))
|
||||
|
||||
# "speech"
|
||||
assert segmenter.process(bytes([255] * _ONE_SECOND))
|
||||
|
||||
# silence
|
||||
# False return value indicates voice command is finished
|
||||
assert not segmenter.process(bytes(_ONE_SECOND))
|
||||
@@ -75,7 +75,7 @@ class MockSTT:
|
||||
hass: HomeAssistant,
|
||||
config: ConfigType,
|
||||
discovery_info: DiscoveryInfoType | None = None,
|
||||
) -> tts.Provider:
|
||||
) -> stt.Provider:
|
||||
"""Set up a mock speech component."""
|
||||
return MockSttProvider(hass, _TRANSCRIPT)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user