Update models.py

Added threading event for audio loop optimization.
This commit is contained in:
imonlinux
2025-11-19 18:47:01 -08:00
committed by GitHub
parent e63bda7e76
commit cf82a3158d
+14 -1
View File
@@ -3,6 +3,7 @@
import asyncio import asyncio
import json import json
import logging import logging
import threading
from dataclasses import asdict, dataclass, field from dataclasses import asdict, dataclass, field
from enum import Enum from enum import Enum
from pathlib import Path from pathlib import Path
@@ -13,7 +14,7 @@ from .event_bus import EventBus
if TYPE_CHECKING: if TYPE_CHECKING:
from pymicro_wakeword import MicroWakeWord from pymicro_wakeword import MicroWakeWord
from pyopen_wakeword import OpenWakeWord from pyopen_wakeword import OpenWakeWord
from .entity import ESPHomeEntity, MediaPlayerEntity from .entity import ESPHomeEntity
from .mpv_player import MpvMediaPlayer from .mpv_player import MpvMediaPlayer
from .satellite import VoiceSatelliteProtocol from .satellite import VoiceSatelliteProtocol
@@ -92,7 +93,19 @@ class ServerState:
refractory_seconds: float = 2.0 refractory_seconds: float = 2.0
mic_muted: bool = False mic_muted: bool = False
shutdown: bool = False shutdown: bool = False
# Threading event to pause the audio thread efficiently when muted
# set() = Mic is ON (Audio processing running)
# clear() = Mic is OFF (Audio processing paused)
mic_muted_event: threading.Event = field(default_factory=threading.Event)
def __post_init__(self):
"""Ensure the threading event matches the boolean state on init."""
if not self.mic_muted:
self.mic_muted_event.set()
else:
self.mic_muted_event.clear()
def save_preferences(self) -> None: def save_preferences(self) -> None:
"""Save preferences as JSON.""" """Save preferences as JSON."""
_LOGGER.debug("Saving preferences: %s", self.preferences_path) _LOGGER.debug("Saving preferences: %s", self.preferences_path)