| 1 |
"""Voice activity detection.""" |
| 2 |
|
| 3 |
from dataclasses import dataclass |
| 4 |
from typing import Optional |
| 5 |
|
| 6 |
from .event import Event, Eventable |
| 7 |
|
| 8 |
DOMAIN = "vad" |
| 9 |
_STARTED_TYPE = "voice-started" |
| 10 |
_STOPPED_TYPE = "voice-stopped" |
| 11 |
|
| 12 |
|
| 13 |
@dataclass |
| 14 |
class VoiceStarted(Eventable): |
| 15 |
"""User has started speaking.""" |
| 16 |
|
| 17 |
timestamp: Optional[int] = None |
| 18 |
"""Milliseconds""" |
| 19 |
|
| 20 |
@staticmethod |
| 21 |
def is_type(event_type: str) -> bool: |
| 22 |
return event_type == _STARTED_TYPE |
| 23 |
|
| 24 |
def event(self) -> Event: |
| 25 |
return Event( |
| 26 |
type=_STARTED_TYPE, |
| 27 |
data={"timestamp": self.timestamp}, |
| 28 |
) |
| 29 |
|
| 30 |
@staticmethod |
| 31 |
def from_event(event: Event) -> "VoiceStarted": |
| 32 |
return VoiceStarted(timestamp=event.data.get("timestamp")) |
| 33 |
|
| 34 |
|
| 35 |
@dataclass |
| 36 |
class VoiceStopped(Eventable): |
| 37 |
"""User has stopped speaking.""" |
| 38 |
|
| 39 |
timestamp: Optional[int] = None |
| 40 |
"""Milliseconds""" |
| 41 |
|
| 42 |
@staticmethod |
| 43 |
def is_type(event_type: str) -> bool: |
| 44 |
return event_type == _STOPPED_TYPE |
| 45 |
|
| 46 |
def event(self) -> Event: |
| 47 |
return Event( |
| 48 |
type=_STOPPED_TYPE, |
| 49 |
data={"timestamp": self.timestamp}, |
| 50 |
) |
| 51 |
|
| 52 |
@staticmethod |
| 53 |
def from_event(event: Event) -> "VoiceStopped": |
| 54 |
return VoiceStopped(timestamp=event.data.get("timestamp")) |