# m1lucken/audio-reactive-led-strip architecture

> Architecture diagram of the GitHub repository m1lucken/audio-reactive-led-strip, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/m1lucken/audio-reactive-led-strip
- Repository: https://github.com/m1lucken/audio-reactive-led-strip

## Overview

##### Project Type and Purpose
- This is a full-stack IoT/music visualization application.
- Purpose: Capture real-time audio, process it, generate LED pixel data, and drive a WS2812 LED strip via either an ESP8266 microcontroller over WiFi or directly from a Raspberry Pi.

##### Main Components
A. Audio Input Layer
– Microphone or virtual audio source on the host computer or USB audio device on Raspberry Pi.
– Python module: `microphone.py`
B. Processing & Visualization Layer (Host)
1. DSP Pipeline
- `dsp.py` – FFT, filtering, melbank generation (`melbank.py`), gamma correction table (`gamma_table.npy`)
2. Visualization Engine
- `visualization.py` – 1D mapping of frequency bins to pixel intensities
3. GUI (optional on Pi)
- `gui.py` – PyQtGraph to display real-time plot
4. Configuration
- `config.py` – settings: `N_PIXELS`, `MIC_RATE`, `UDP_IP`, device type
C. Network Transmission Layer
- `led.py` – packetizes pixel data into UDP frames, sends to ESP8266’s IP on the local WiFi network
- Uses standard UDP socket library
D. Microcontroller Firmware Layer
- Arduino sketch: `ws2812_controller_esp8266.ino` (also ESP32 variant)
- WebSocketServer library or raw UDP listener on ESP8266
- I²S peripheral driver to bit-bang/stream data into WS2812 strip
- Network stack (ESP8266 WiFi) configured via SSID/password/gateway/IP
E. Hardware Layer
1. Host machine or Raspberry Pi
2. WiFi router (SSID, gateway)
3. ESP8266 module (NodeMCU v3 / Feather HUZZAH)
4. WS2812B LED strip
5. Power supply + level shifter

##### Relationships and Data Flow
- Audio input → DSP → Visualization → (if on computer) GUI
- Visualization output → UDP → ESP8266
- ESP8266 → I²S → WS2812 LED strip
- On Raspberry Pi mode: Visualization can bypass UDP and drive LEDs via `rpi_ws281x` library directly.

##### Architectural Patterns and Design Principles
- Pipeline pattern for the audio-to-pixels transformation
- Clear separation of concerns:
– Capture (microphone.py)
– Process (dsp.py, melbank.py)
– Transport (led.py)
– Actuate (Arduino firmware)
- Configuration-driven behaviour (config.py) to switch between “computer+ESP8266” and “standalone Pi” modes
- Use of asynchronous or buffered I/O to maintain real-time constraints

##### Relevant Technologies and Frameworks
- Python 2.7/3.x, NumPy, SciPy, PyAudio, PyQtGraph
- UDP sockets (Python stdlib)
- Arduino IDE with ESP8266 addon, WebSocketServer library, esp8266_ws2812_i2s driver
- Raspberry Pi: `rpi_ws281x` library (C/Python bindings)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Audio Input Layer
    subgraph "Audio Input Layer"
        MIC["microphone.py"]:::host
        click MIC "https://github.com/m1lucken/audio-reactive-led-strip/blob/master/python/microphone.py"
    end

    %% Processing & Visualization Layer
    subgraph "Processing & Visualization Layer"
        DSP["dsp.py"]:::host
        MEL["melbank.py"]:::host
        GAMMA["gamma_table.npy"]:::host
        VIS["visualization.py"]:::host
        GUI["gui.py"]:::host
        CFG["config.py"]:::host

        click DSP "https://github.com/m1lucken/audio-reactive-led-strip/blob/master/python/dsp.py"
        click MEL "https://github.com/m1lucken/audio-reactive-led-strip/blob/master/python/melbank.py"
        click GAMMA "https://github.com/m1lucken/audio-reactive-led-strip/blob/master/python/gamma_table.npy"
        click VIS "https://github.com/m1lucken/audio-reactive-led-strip/blob/master/python/visualization.py"
        click GUI "https://github.com/m1lucken/audio-reactive-led-strip/blob/master/python/gui.py"
        click CFG "https://github.com/m1lucken/audio-reactive-led-strip/blob/master/python/config.py"
    end

    MIC -->|"audio samples"| DSP
    DSP -->|"mel filters"| MEL
    DSP -->|"gamma table"| GAMMA
    DSP -->|"FFT bins"| VIS
    VIS -->|"pixel intensities"| GUI
    CFG --> DSP
    CFG --> VIS

    %% Network Transmission Layer
    subgraph "Network Transmission Layer"
        LEDPY["led.py"]:::host
        click LEDPY "https://github.com/m1lucken/audio-reactive-led-strip/blob/master/python/led.py"
    end

    VIS -->|"UDP frames"| LEDPY

    %% Hardware Network
    subgraph "Network Infrastructure"
        ROUTER["WiFi Router"]:::external
    end

    LEDPY -->|"UDP over WiFi"| ROUTER

    %% Microcontroller Firmware Layer
    subgraph "Microcontroller Firmware Layer"
        ESP8266FW["ws2812_controller_esp8266.ino"]:::firmware
        ESP32FW["ws2812_controller_esp32.ino"]:::firmware

        click ESP8266FW "https://github.com/m1lucken/audio-reactive-led-strip/blob/master/arduino/ws2812_controller_esp8266/ws2812_controller_esp8266.ino"
        click ESP32FW "https://github.com/m1lucken/audio-reactive-led-strip/blob/master/arduino/ws2812_controller_esp32/ws2812_controller_esp32.ino"
    end

    ROUTER -->|"UDP frames"| ESP8266FW
    ROUTER -->|"UDP frames"| ESP32FW

    %% Hardware Layer
    subgraph "Hardware Layer"
        ESP8266_MOD["ESP8266 Module"]:::hardware
        ESP32_MOD["ESP32 Module"]:::hardware
        LEDSTRIP["WS2812 LED Strip"]:::hardware
        PI["Raspberry Pi"]:::hardware
    end

    ESP8266FW -->|"I²S stream"| ESP8266_MOD
    ESP32FW -->|"I²S stream"| ESP32_MOD
    ESP8266_MOD -->|"drives"| LEDSTRIP
    ESP32_MOD -->|"drives"| LEDSTRIP

    %% Alternate Raspberry Pi Path
    VIS -->|"pixel data"| RPI_LIB["rpi_ws281x"]:::host
    RPI_LIB -->|"GPIO signal"| LEDSTRIP

    %% Legend and Styles
    classDef host fill:#D0E8FF,stroke:#004080,color:#000;
    classDef firmware fill:#D0FFD0,stroke:#008000,color:#000;
    classDef hardware fill:#FFE0B2,stroke:#FF8000,color:#000;
    classDef external fill:#D3D3D3,stroke:#808080,color:#000;
```
