mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-26 17:13:54 +08:00
update:test迁移重命名为digital-human
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
from .audio import AudioPlugin
|
||||
from .base import Plugin
|
||||
from .manager import PluginManager
|
||||
from .wake_word import WakeWordPlugin
|
||||
@@ -0,0 +1,37 @@
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from ..core import MicrophoneListener
|
||||
from .base import Plugin
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class AudioPlugin(Plugin):
|
||||
name = "audio"
|
||||
priority = 10
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.app = None
|
||||
self.source: MicrophoneListener | None = None
|
||||
|
||||
def setup(self, app: Any) -> None:
|
||||
self.app = app
|
||||
self.source = MicrophoneListener(app.config)
|
||||
self.app.audio_source = self.source
|
||||
|
||||
def start(self) -> None:
|
||||
if self.source is None:
|
||||
logger.warning("audio source not initialized")
|
||||
return
|
||||
self.source.start()
|
||||
|
||||
def stop(self) -> None:
|
||||
if self.source is not None:
|
||||
self.source.stop()
|
||||
|
||||
def shutdown(self) -> None:
|
||||
if self.app is not None:
|
||||
self.app.audio_source = None
|
||||
self.source = None
|
||||
self.app = None
|
||||
@@ -0,0 +1,18 @@
|
||||
from typing import Any
|
||||
|
||||
|
||||
class Plugin:
|
||||
name: str = "plugin"
|
||||
priority: int = 50
|
||||
|
||||
def setup(self, app: Any) -> None:
|
||||
return None
|
||||
|
||||
def start(self) -> None:
|
||||
return None
|
||||
|
||||
def stop(self) -> None:
|
||||
return None
|
||||
|
||||
def shutdown(self) -> None:
|
||||
return None
|
||||
@@ -0,0 +1,37 @@
|
||||
from typing import Any
|
||||
|
||||
from .base import Plugin
|
||||
|
||||
|
||||
class PluginManager:
|
||||
def __init__(self) -> None:
|
||||
self._plugins: list[Plugin] = []
|
||||
self._by_name: dict[str, Plugin] = {}
|
||||
|
||||
def register(self, *plugins: Plugin) -> None:
|
||||
for plugin in sorted(plugins, key=lambda item: getattr(item, "priority", 50)):
|
||||
if plugin in self._plugins:
|
||||
continue
|
||||
self._plugins.append(plugin)
|
||||
name = getattr(plugin, "name", "")
|
||||
if isinstance(name, str) and name:
|
||||
self._by_name[name] = plugin
|
||||
|
||||
def get_plugin(self, name: str) -> Plugin | None:
|
||||
return self._by_name.get(name)
|
||||
|
||||
def setup_all(self, app: Any) -> None:
|
||||
for plugin in list(self._plugins):
|
||||
plugin.setup(app)
|
||||
|
||||
def start_all(self) -> None:
|
||||
for plugin in list(self._plugins):
|
||||
plugin.start()
|
||||
|
||||
def stop_all(self) -> None:
|
||||
for plugin in reversed(self._plugins):
|
||||
plugin.stop()
|
||||
|
||||
def shutdown_all(self) -> None:
|
||||
for plugin in reversed(self._plugins):
|
||||
plugin.shutdown()
|
||||
@@ -0,0 +1,53 @@
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from ..core import WakewordDetector
|
||||
from .base import Plugin
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class WakeWordPlugin(Plugin):
|
||||
name = "wake_word"
|
||||
priority = 30
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.app = None
|
||||
self.detector: WakewordDetector | None = None
|
||||
|
||||
def setup(self, app: Any) -> None:
|
||||
self.app = app
|
||||
self.detector = WakewordDetector(app.config)
|
||||
if not self.detector.enabled:
|
||||
self.detector = None
|
||||
return
|
||||
self.detector.on_detected(self._on_detected)
|
||||
self.detector.on_error = self._on_error
|
||||
|
||||
def start(self) -> None:
|
||||
if self.detector is None:
|
||||
return
|
||||
|
||||
audio_plugin = self.app.plugins.get_plugin("audio") if self.app else None
|
||||
audio_source = getattr(audio_plugin, "source", None)
|
||||
if audio_source is None:
|
||||
logger.warning("audio source unavailable, wakeword plugin not started")
|
||||
return
|
||||
|
||||
self.detector.start(audio_source)
|
||||
|
||||
def stop(self) -> None:
|
||||
if self.detector is not None:
|
||||
self.detector.stop()
|
||||
|
||||
def shutdown(self) -> None:
|
||||
self.detector = None
|
||||
self.app = None
|
||||
|
||||
def _on_detected(self, wake_word: str, full_text: str) -> None:
|
||||
if self.app is None:
|
||||
return
|
||||
self.app.handle_wake_word_detected(wake_word, full_text)
|
||||
|
||||
def _on_error(self, error: Exception) -> None:
|
||||
logger.error("wakeword detection error: %s", error)
|
||||
Reference in New Issue
Block a user