# perminder-klair/subwave architecture

> Architecture diagram of the GitHub repository perminder-klair/subwave, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/perminder-klair/subwave
- Repository: https://github.com/perminder-klair/subwave

## Overview

#### Purpose and Entry Points
SUB/WAVE is a self-hosted personal internet-radio platform: one shared Icecast broadcast, automated from a Navidrome music library by an AI DJ. Operators deploy through Docker Compose, use the `subwave` CLI, or administer via the web UI. Primary entry points are `docker-compose.yml`, `bin/subwave`, `cli/src/cli.ts`, `controller/src/server.ts`, `web/app/page.tsx`, and `app/src/app/_layout.tsx`.

#### Deployment and Runtime Topology
Production Compose orchestrates Caddy, web, controller, broadcast, Icecast, and the default acoustic analyzer; optional profiles add heavy TTS or GPU analysis. Caddy is the public routing/TLS boundary, while BYO mode exposes web, controller, and Icecast independently. Docker assets and service entrypoints are in `docker-compose.yml`, `docker/Caddyfile`, `docker/Dockerfile.controller`, and `docker/broadcast-entrypoint.sh`. State is intended to persist outside application containers.

#### Broadcast Pipeline
Liquidsoap owns real-time audio assembly: it receives controller-directed tracks/speech, applies crossfade and voice ducking, and emits shared mounts through Icecast. The controller’s queue/session/scheduler coordinate what is on air and when; Liquidsoap is deliberately separated as the timing-sensitive mixer. Core paths: `liquidsoap/radio.liq`, `controller/src/broadcast/queue.ts`, `controller/src/broadcast/session.ts`, `controller/src/broadcast/liquidsoap-control.ts`, and `controller/src/broadcast/scheduler.ts`.

#### Controller: Station Domain and API
The Node.js controller is the system of record for configuration, broadcast state, station operations, and HTTP APIs. `controller/src/server.ts` composes routes, middleware, and startup; route modules separate public listener endpoints from authenticated operator/admin surfaces. Setup and secrets are explicit first-run boundaries. Relevant paths: `controller/src/server.ts`, `controller/src/routes/public.ts`, `controller/src/routes/settings.ts`, `controller/src/routes/onboarding.ts`, `controller/src/setup/firstRun.ts`, and `controller/src/middleware/auth.ts`.

#### AI DJ and Speech Generation
At track boundaries, the DJ agent combines active show/persona context, station state, requests, and skills to choose music and generate spoken segments. The LLM abstraction supports runtime provider selection, strategies, retries/failover, tool calls, budgets, and telemetry. Generated text is handed to pluggable local, cloud, remote, or heavy-sidecar TTS, with fallback behavior. Paths: `controller/src/broadcast/dj-agent.ts`, `controller/src/llm/agent.ts`, `controller/src/llm/internal/provider/registry.ts`, `controller/src/audio/tts.ts`, and `controller/src/audio/tts-fallback.ts`.

#### Music Library, Curation, and Analysis
Navidrome/Subsonic is the external music-library authority. The controller imports and tags library metadata, optionally delegates acoustic analysis to the Python analyzer service, then uses pools, recency, shows, requests, moods, embeddings, and mix constraints to select tracks. External enrichment/scrobbling integrations include Last.fm, MusicBrainz, ListenBrainz, and optional search providers. Paths: `controller/src/music/subsonic.ts`, `controller/src/music/library.ts`, `controller/src/music/picker.ts`, `controller/src/music/analyze-library.ts`, `docker/analyzer/server.py`, and `controller/src/broadcast/scrobble.ts`.

#### Client and Operator Surfaces
The Next.js app provides public listening, onboarding, documentation, and authenticated administration; it consumes controller APIs and Icecast stream metadata. The native Expo app is a separate listener client with background playback, casting, and multi-station support. External agents enter through the standalone MCP server, which forwards station-control/request actions to the controller API. Paths: `web/app/listen/page.tsx`, `web/components/admin/AdminShell.tsx`, `app/src/audio/player.ts`, `app/src/player/PlayerScreen.tsx`, and `mcp-subwave/src/index.ts`.

Main entry points: [`controller/src/server.ts`](https://github.com/perminder-klair/subwave/blob/develop/controller/src/server.ts) (Controller API), [`docker-compose.yml`](https://github.com/perminder-klair/subwave/blob/develop/docker-compose.yml) (Docker Compose), [`docker/Caddyfile`](https://github.com/perminder-klair/subwave/blob/develop/docker/Caddyfile) (Caddy), [`controller/src/broadcast/dj-agent.ts`](https://github.com/perminder-klair/subwave/blob/develop/controller/src/broadcast/dj-agent.ts) (AI DJ)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_deploy["Deployment"]
  node_compose{{"Docker Compose<br/>orchestrator<br/>[docker-compose.yml]"}}
  node_caddy["Caddy<br/>public gateway"]
end

subgraph group_controller["Controller"]
  node_controller_api["Controller API<br/>Node HTTP server<br/>[server.ts]"]
  node_auth_setup["Auth &amp; first run<br/>access boundary<br/>[firstRun.ts]"]
  node_station_state[("Station domain<br/>state &amp; operations<br/>[settings.ts]")]
  node_dj["AI DJ<br/>show agent<br/>[dj-agent.ts]"]
  node_llm["LLM runtime<br/>provider abstraction<br/>[agent.ts]"]
  node_tts["TTS pipeline<br/>speech synthesis<br/>[tts-fallback.ts]"]
  node_library["Library &amp; curation<br/>music selection<br/>[picker.ts]"]
end

subgraph group_broadcast["Broadcast"]
  node_scheduler["Broadcast scheduler<br/>airtime coordinator<br/>[scheduler.ts]"]
  node_queue["Broadcast queue<br/>playback queue<br/>[queue.ts]"]
  node_liquidsoap["Liquidsoap<br/>real-time mixer<br/>[radio.liq]"]
  node_icecast["Icecast<br/>stream server"]
end

subgraph group_clients["Clients &amp; Operators"]
  node_cli["subwave CLI<br/>operator CLI<br/>[cli.ts]"]
  node_web["Web app<br/>Next.js client<br/>[page.tsx]"]
  node_native["Native listener<br/>Expo client<br/>[PlayerScreen.tsx]"]
  node_mcp["MCP server<br/>agent gateway<br/>[index.ts]"]
end

subgraph group_external["External Services"]
  node_navidrome[("Navidrome / Subsonic<br/>music authority")]
  node_analyzer["Acoustic analyzer<br/>Python analysis service<br/>[server.py]"]
  node_ai_providers["LLM &amp; TTS providers<br/>AI providers"]
  node_persistent_state[("Persistent state<br/>external storage")]
end

node_compose -->|"starts"| node_caddy
node_compose -->|"starts"| node_controller_api
node_compose -->|"starts broadcast"| node_liquidsoap
node_compose -->|"starts"| node_icecast
node_compose -.->|"default analysis"| node_analyzer
node_caddy -->|"routes public web"| node_web
node_caddy -->|"routes API"| node_controller_api
node_caddy -->|"routes streams"| node_icecast
node_cli -->|"administers"| node_controller_api
node_web -->|"uses APIs"| node_controller_api
node_web -->|"plays stream"| node_icecast
node_native -->|"reads station APIs"| node_controller_api
node_native -->|"plays stream"| node_icecast
node_mcp -->|"forwards actions"| node_controller_api
node_controller_api -->|"enforces setup and auth"| node_auth_setup
node_controller_api -->|"manages"| node_station_state
node_controller_api -->|"operates"| node_scheduler
node_station_state -->|"persists"| node_persistent_state
node_scheduler -->|"triggers boundaries"| node_dj
node_scheduler -->|"schedules"| node_queue
node_dj -->|"generates with"| node_llm
node_dj -->|"requests selections"| node_library
node_dj -->|"hands off speech"| node_tts
node_llm -->|"selects provider"| node_ai_providers
node_tts -->|"synthesizes via"| node_ai_providers
node_library -->|"imports and streams music"| node_navidrome
node_library -.->|"delegates analysis"| node_analyzer
node_queue -->|"directs media and speech"| node_liquidsoap
node_liquidsoap -->|"publishes mounts"| node_icecast

click node_compose "https://github.com/perminder-klair/subwave/blob/develop/docker-compose.yml"
click node_caddy "https://github.com/perminder-klair/subwave/blob/develop/docker/Caddyfile"
click node_cli "https://github.com/perminder-klair/subwave/blob/develop/cli/src/cli.ts"
click node_web "https://github.com/perminder-klair/subwave/blob/develop/web/app/page.tsx"
click node_native "https://github.com/perminder-klair/subwave/blob/develop/app/src/player/PlayerScreen.tsx"
click node_mcp "https://github.com/perminder-klair/subwave/blob/develop/mcp-subwave/src/index.ts"
click node_controller_api "https://github.com/perminder-klair/subwave/blob/develop/controller/src/server.ts"
click node_auth_setup "https://github.com/perminder-klair/subwave/blob/develop/controller/src/setup/firstRun.ts"
click node_station_state "https://github.com/perminder-klair/subwave/blob/develop/controller/src/routes/settings.ts"
click node_scheduler "https://github.com/perminder-klair/subwave/blob/develop/controller/src/broadcast/scheduler.ts"
click node_queue "https://github.com/perminder-klair/subwave/blob/develop/controller/src/broadcast/queue.ts"
click node_dj "https://github.com/perminder-klair/subwave/blob/develop/controller/src/broadcast/dj-agent.ts"
click node_llm "https://github.com/perminder-klair/subwave/blob/develop/controller/src/llm/agent.ts"
click node_tts "https://github.com/perminder-klair/subwave/blob/develop/controller/src/audio/tts-fallback.ts"
click node_library "https://github.com/perminder-klair/subwave/blob/develop/controller/src/music/picker.ts"
click node_liquidsoap "https://github.com/perminder-klair/subwave/blob/develop/liquidsoap/radio.liq"
click node_analyzer "https://github.com/perminder-klair/subwave/blob/develop/docker/analyzer/server.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_compose,node_caddy toneBlue
class node_controller_api,node_auth_setup,node_station_state,node_dj,node_llm,node_tts,node_library toneAmber
class node_scheduler,node_queue,node_liquidsoap,node_icecast toneMint
class node_cli,node_web,node_native,node_mcp toneRose
class node_navidrome,node_analyzer,node_ai_providers,node_persistent_state toneIndigo
```

## Components

### Deployment

- [Docker Compose](https://github.com/perminder-klair/subwave/blob/develop/docker-compose.yml): orchestrator · Starts the production services and optional profiles. · `docker-compose.yml`
- [Caddy](https://github.com/perminder-klair/subwave/blob/develop/docker/Caddyfile): public gateway · TLS and public routing boundary. · `docker/Caddyfile`

### Controller

- [Controller API](https://github.com/perminder-klair/subwave/blob/develop/controller/src/server.ts): Node HTTP server · Composes APIs, middleware, and station startup. · `controller/src/server.ts`
- [Auth & first run](https://github.com/perminder-klair/subwave/blob/develop/controller/src/setup/firstRun.ts): access boundary · Protects operator APIs and initializes secrets. · `controller/src/setup/firstRun.ts`
- [Station domain](https://github.com/perminder-klair/subwave/blob/develop/controller/src/routes/settings.ts): state & operations · System of record for configuration and broadcast state. · `controller/src/routes/settings.ts`
- [AI DJ](https://github.com/perminder-klair/subwave/blob/develop/controller/src/broadcast/dj-agent.ts): show agent · Chooses music and writes track-boundary speech. · `controller/src/broadcast/dj-agent.ts`
- [LLM runtime](https://github.com/perminder-klair/subwave/blob/develop/controller/src/llm/agent.ts): provider abstraction · Selects providers and handles tools, budgets, retries, and failover. · `controller/src/llm/agent.ts`
- [TTS pipeline](https://github.com/perminder-klair/subwave/blob/develop/controller/src/audio/tts-fallback.ts): speech synthesis · Uses pluggable synthesis with fallback behavior. · `controller/src/audio/tts-fallback.ts`
- [Library & curation](https://github.com/perminder-klair/subwave/blob/develop/controller/src/music/picker.ts): music selection · Imports metadata and selects tracks using pools and mix constraints. · `controller/src/music/picker.ts`

### Broadcast

- [Broadcast scheduler](https://github.com/perminder-klair/subwave/blob/develop/controller/src/broadcast/scheduler.ts): airtime coordinator · Coordinates sessions and upcoming playback. · `controller/src/broadcast/scheduler.ts`
- [Broadcast queue](https://github.com/perminder-klair/subwave/blob/develop/controller/src/broadcast/queue.ts): playback queue · Sends selected tracks and speech to the mixer. · `controller/src/broadcast/queue.ts`
- [Liquidsoap](https://github.com/perminder-klair/subwave/blob/develop/liquidsoap/radio.liq): real-time mixer · Crossfades, ducks voice, and assembles the live program. · `liquidsoap/radio.liq`
- **Icecast**: stream server · Publishes shared listener mounts.

### Clients & Operators

- [subwave CLI](https://github.com/perminder-klair/subwave/blob/develop/cli/src/cli.ts): operator CLI · `cli/src/cli.ts`
- [Web app](https://github.com/perminder-klair/subwave/blob/develop/web/app/page.tsx): Next.js client · Public listening, setup, docs, and administration. · `web/app/page.tsx`
- [Native listener](https://github.com/perminder-klair/subwave/blob/develop/app/src/player/PlayerScreen.tsx): Expo client · Playback, casting, and multi-station listening. · `app/src/player/PlayerScreen.tsx`
- [MCP server](https://github.com/perminder-klair/subwave/blob/develop/mcp-subwave/src/index.ts): agent gateway · Exposes station actions to external agents. · `mcp-subwave/src/index.ts`

### External Services

- **Navidrome / Subsonic**: music authority · External music-library source.
- [Acoustic analyzer](https://github.com/perminder-klair/subwave/blob/develop/docker/analyzer/server.py): Python analysis service · Optional service for library acoustic features. · `docker/analyzer/server.py`
- **LLM & TTS providers**: AI providers · Local, cloud, remote, or heavy-sidecar inference.
- **Persistent state**: external storage · State persists outside application containers.

## Connections

- Docker Compose → Caddy: starts
- Docker Compose → Controller API: starts
- Docker Compose → Liquidsoap: starts broadcast
- Docker Compose → Icecast: starts
- Docker Compose → Acoustic analyzer: default analysis
- Caddy → Web app: routes public web
- Caddy → Controller API: routes API
- Caddy → Icecast: routes streams
- subwave CLI → Controller API: administers
- Web app → Controller API: uses APIs
- Web app → Icecast: plays stream
- Native listener → Controller API: reads station APIs
- Native listener → Icecast: plays stream
- MCP server → Controller API: forwards actions
- Controller API → Auth & first run: enforces setup and auth
- Controller API → Station domain: manages
- Controller API → Broadcast scheduler: operates
- Station domain → Persistent state: persists
- Broadcast scheduler → AI DJ: triggers boundaries
- Broadcast scheduler → Broadcast queue: schedules
- AI DJ → LLM runtime: generates with
- AI DJ → Library & curation: requests selections
- AI DJ → TTS pipeline: hands off speech
- LLM runtime → LLM & TTS providers: selects provider
- TTS pipeline → LLM & TTS providers: synthesizes via
- Library & curation → Navidrome / Subsonic: imports and streams music
- Library & curation → Acoustic analyzer: delegates analysis
- Broadcast queue → Liquidsoap: directs media and speech
- Liquidsoap → Icecast: publishes mounts
