xiaozhi-esp32-server/main/digital-human/wakeword_runtime/runtime/app.py
hrz 98c314a485 唤醒词启动失败时把麦克风采集流关掉
模型文件缺失导致启动中断时,麦克风的采集流会一直占着设备。这里让停止流程不管有没有启动成功都把插件停掉,采集流能正常关闭。
2026-10-27 10:22:07 +08:00

71 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import logging
from typing import Protocol
from ..config import RuntimeConfig
from ..plugins import AudioPlugin, PluginManager, WakeWordPlugin
logger = logging.getLogger(__name__)
class EventPublisher(Protocol):
def publish_service_ready(self) -> None:
...
def publish_service_stopping(self) -> None:
...
def publish_detected(self, wake_word: str) -> None:
...
class TestRuntimeApplication:
def __init__(self, config: RuntimeConfig, event_publisher: EventPublisher) -> None:
self.config = config
self.event_bridge = event_publisher
self.plugins = PluginManager()
self.audio_source = None
self._is_setup = False
self._is_running = False
def setup(self) -> None:
if self._is_setup:
return
if self.config.wakeword_enabled:
self.plugins.register(
AudioPlugin(),
WakeWordPlugin(),
)
self.plugins.setup_all(self)
self._is_setup = True
def start(self) -> None:
if self._is_running:
return
self.setup()
self.plugins.start_all()
if self.config.wakeword_enabled:
self.event_bridge.publish_service_ready()
self._is_running = True
logger.info("test runtime application started")
def stop(self) -> None:
was_running = self._is_running
self._is_running = False
if was_running:
self.event_bridge.publish_service_stopping()
logger.info("test runtime application stopped")
# 启动中途失败时 _is_running 还是 False,但插件可能已经开过采集流,
# 这里不提前返回,保证采集流一定被关掉
self.plugins.stop_all()
def shutdown(self) -> None:
self.stop()
if self._is_setup:
self.plugins.shutdown_all()
def handle_wake_word_detected(self, wake_word: str, full_text: str) -> None:
_ = full_text
logger.info("wake word detected: %s", wake_word)
self.event_bridge.publish_detected(wake_word)