# huangjunsen0406/py-xiaozhi architecture

> Architecture diagram of the GitHub repository huangjunsen0406/py-xiaozhi, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/huangjunsen0406/py-xiaozhi
- Repository: https://github.com/huangjunsen0406/py-xiaozhi

## Overview

##### Project Type & Purpose
- py-xiaozhi is a Python-based AI voice client that provides voice interaction, visual multimodal experience, IoT device control, and online music playback.
- The README shows features like voice wake-up, continuous dialogue, and even both CLI and GUI display modes. Its modular design caters to both embedded devices (with CLI) and full-featured desktop systems (with GUI).

##### Analyzing the File Structure
- Top-Level Components: Notice directories such as “src”, “docs”, “scripts”, “libs”, “hooks”, and configuration files.
- “audio_codecs” and “audio_processing”: handling audio encoding/decoding, wake-word detection, and voice activity detection.
- “display”: provides both CLI and GUI display implementations.
- “iot”: contains modules for IoT device integration with a “thing_manager” to register/control devices like cameras, lamps, music players, and speakers.
- “protocols”: defines the communication protocols used (MQTT and WebSocket) for device communication and possibly audio stream transmission.
- “utils”: various utility functions for configuration management, logging, system info, and volume control.
- “application.py”: acts as the core entry point for the client’s business logic.
- Other files (e.g., main.py, build scripts, hooks) help with running and packaging the application.

##### Final Touches
- Avoid over-simplification – splitting the project into modules as detailed in the file tree (audio codecs, audio processing, iot things, protocols, display, etc.) will help in maintaining clarity.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Voice Input Layer
    subgraph "Voice Input Layer"
        VUI["User Voice Input"]:::input
    end

    %% Voice Processing Layer
    subgraph "Voice Processing Layer"
        AC["Audio Codecs"]:::voice
        VAD["VAD Detector"]:::voice
        WWD["Wake Word Detection"]:::voice
    end

    %% Application Core Layer
    subgraph "Application Core Layer"
        CA["Core Application (State:IDLE->CONNECTING->LISTENING->SPEAKING)"]:::core
    end

    %% Presentation Layer
    subgraph "Presentation Layer"
        CLI["CLI Display"]:::display
        GUI["GUI Display"]:::display
    end

    %% IoT Layer
    subgraph "IoT Layer"
        TH["Thing"]:::iot
        TM["Thing Manager"]:::iot
        CAM["Camera"]:::iot
        LMP["Lamp"]:::iot
        MP["Music Player"]:::iot
    end

    %% Communication Layer
    subgraph "Communication Layer"
        PI["Protocol Interface"]:::protocol
        MQTT["MQTT Protocol"]:::protocol
        WS["WebSocket Protocol"]:::protocol
    end

    %% Utility Services
    subgraph "Utility Services"
        CM["Config Manager"]:::utils
        LC["Logging Config"]:::utils
        SI["System Info"]:::utils
        VC["Volume Controller"]:::utils
    end

    %% External Hooks
    subgraph "External Hooks"
        OH["Opus Hook"]:::hooks
        VH["Vosk Hook"]:::hooks
        RH["Runtime Hook"]:::hooks
    end

    %% Connections
    VUI -->|"processes"| AC
    AC -->|"decodes"| VAD
    AC -->|"decodes"| WWD
    VAD -->|"triggers"| CA
    WWD -->|"triggers"| CA
    CA -->|"outputs"| CLI
    CA -->|"outputs"| GUI
    CA -->|"controls"| TM
    TM -->|"manages"| TH
    TM -->|"manages"| CAM
    TM -->|"manages"| LMP
    TM -->|"manages"| MP
    CA -->|"communicates"| PI
    PI -->|"via"| MQTT
    PI -->|"via"| WS
    CA -.->|"utilizes"| CM
    OH --- AC
    VH --- AC
    RH --- AC

    %% Click Events
    click CA "https://github.com/huangjunsen0406/py-xiaozhi/blob/main/src/application.py"
    click AC "https://github.com/huangjunsen0406/py-xiaozhi/blob/main/src/audio_codecs/audio_codec.py"
    click VAD "https://github.com/huangjunsen0406/py-xiaozhi/blob/main/src/audio_processing/vad_detector.py"
    click WWD "https://github.com/huangjunsen0406/py-xiaozhi/blob/main/src/audio_processing/wake_word_detect.py"
    click CLI "https://github.com/huangjunsen0406/py-xiaozhi/blob/main/src/display/cli_display.py"
    click GUI "https://github.com/huangjunsen0406/py-xiaozhi/blob/main/src/display/gui_display.py"
    click TH "https://github.com/huangjunsen0406/py-xiaozhi/blob/main/src/iot/thing.py"
    click TM "https://github.com/huangjunsen0406/py-xiaozhi/blob/main/src/iot/thing_manager.py"
    click CAM "https://github.com/huangjunsen0406/py-xiaozhi/blob/main/src/iot/things/CameraVL/Camera.py"
    click LMP "https://github.com/huangjunsen0406/py-xiaozhi/blob/main/src/iot/things/lamp.py"
    click MP "https://github.com/huangjunsen0406/py-xiaozhi/blob/main/src/iot/things/music_player.py"
    click PI "https://github.com/huangjunsen0406/py-xiaozhi/blob/main/src/protocols/protocol.py"
    click MQTT "https://github.com/huangjunsen0406/py-xiaozhi/blob/main/src/protocols/mqtt_protocol.py"
    click WS "https://github.com/huangjunsen0406/py-xiaozhi/blob/main/src/protocols/websocket_protocol.py"
    click CM "https://github.com/huangjunsen0406/py-xiaozhi/blob/main/src/utils/config_manager.py"
    click LC "https://github.com/huangjunsen0406/py-xiaozhi/blob/main/src/utils/logging_config.py"
    click SI "https://github.com/huangjunsen0406/py-xiaozhi/blob/main/src/utils/system_info.py"
    click VC "https://github.com/huangjunsen0406/py-xiaozhi/blob/main/src/utils/volume_controller.py"
    click OH "https://github.com/huangjunsen0406/py-xiaozhi/blob/main/hooks/hook-opuslib.py"
    click VH "https://github.com/huangjunsen0406/py-xiaozhi/blob/main/hooks/hook-vosk.py"
    click RH "https://github.com/huangjunsen0406/py-xiaozhi/blob/main/hooks/runtime_hook.py"

    %% Styles
    classDef input fill:#e0ffe0,stroke:#333,stroke-width:2px;
    classDef voice fill:#fff0b3,stroke:#333,stroke-width:2px;
    classDef core fill:#cce5ff,stroke:#333,stroke-width:2px;
    classDef display fill:#ffccf2,stroke:#333,stroke-width:2px;
    classDef iot fill:#d5f5e3,stroke:#333,stroke-width:2px;
    classDef protocol fill:#fff5cc,stroke:#333,stroke-width:2px;
    classDef utils fill:#e6e6fa,stroke:#333,stroke-width:2px;
    classDef hooks fill:#ffd6cc,stroke:#333,stroke-width:2px;
```
