# kaillora/moodtomusic architecture

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

- Interactive diagram: https://gitdiagram.com/kaillora/moodtomusic
- Repository: https://github.com/kaillora/moodtomusic

## Overview

MoodToMusic turns webcam frames into emotion feedback and mood-based song recommendations. In the web workflow, the browser sends frames over a WebSocket; the backend processes detections, aggregates emotions, maps the prevalent mood to VAD, and selects a song. The frontend presents live emotion and recommendation state. A separate `emotion.py` path provides standalone webcam recognition. Backend excerpts establish frame decoding and recommendation stages; some runtime calls and frontend wiring are not shown, so those links are marked unverified.

Main entry points: [`backend/api/websocket.py`](https://github.com/kaillora/moodtomusic/blob/main/backend/api/websocket.py) (WebSocket handler), [`backend/app.py`](https://github.com/kaillora/moodtomusic/blob/main/backend/app.py) (FastAPI app), [`backend/services/music_recommender.py`](https://github.com/kaillora/moodtomusic/blob/main/backend/services/music_recommender.py) (Song recommender), [`frontend/lib/store/index.ts`](https://github.com/kaillora/moodtomusic/blob/main/frontend/lib/store/index.ts) (UI state)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_web["Web experience"]
  node_page["App screen<br/>[page.tsx]"]
  node_capture["Video capture<br/>[VideoCapture.tsx]"]
  node_hook["WebSocket hook<br/>[useWebSocket.ts]"]
  node_clientws["WebSocket client<br/>[websocket.ts]"]
  node_store["UI state<br/>[index.ts]"]
  node_emotionui["Emotion views<br/>[EmotionMeter.tsx]"]
  node_history["Emotion history<br/>[EmotionHistory.tsx]"]
  node_recommendationui["Recommendation card"]
  node_settings["Settings control"]
end

subgraph group_api["Backend runtime"]
  node_app["FastAPI app<br/>[app.py]"]
  node_routes["REST routes<br/>[routes.py]"]
  node_ws["WebSocket handler<br/>[websocket.py]"]
  node_frames["Frame decoder<br/>[frame_processor.py]"]
  node_config["Backend settings<br/>[config.py]"]
end

subgraph group_domain["Mood and recommendation"]
  node_detector["Emotion detector"]
  node_logger["Emotion aggregation<br/>[emotion_logger.py]"]
  node_mapper["Emotion-to-VAD mapping<br/>[mood_mapper.py]"]
  node_recommender["Song recommender"]
  node_dataset[("Song dataset")]
  node_emotionmodel["CNN weights"]
  node_musicmodels["Music model assets"]
end

subgraph group_standalone["Standalone mode"]
  node_cli["Standalone webcam app<br/>[emotion.py]"]
end

node_user(("Listener"))

node_user -.->|"uses"| node_page
node_capture -.->|"sends frames"| node_hook
node_hook -->|"uses"| node_clientws
node_hook -->|"updates state"| node_store
node_store -.->|"feeds display"| node_emotionui
node_store -.->|"feeds display"| node_history
node_store -.->|"feeds display"| node_recommendationui
node_settings -.->|"configures period"| node_hook
node_clientws -.->|"exchanges messages"| node_ws
node_app -->|"registers routes"| node_routes
node_app -->|"dispatches socket"| node_ws
node_app -->|"loads model"| node_detector
node_app -->|"initializes"| node_recommender
node_ws -->|"decodes frames"| node_frames
node_ws -->|"uses"| node_logger
node_ws -->|"uses"| node_mapper
node_ws -.->|"processes detections"| node_detector
node_ws -.->|"requests songs"| node_recommender
node_recommender -->|"loads songs"| node_dataset
node_recommender -->|"loads models"| node_musicmodels
node_app -->|"reads settings"| node_config
node_detector -.->|"loads weights"| node_emotionmodel
node_cli -->|"records emotion"| node_logger
node_cli -->|"requests songs"| node_recommender

click node_page "https://github.com/kaillora/moodtomusic/blob/main/frontend/app/page.tsx"
click node_capture "https://github.com/kaillora/moodtomusic/blob/main/frontend/components/video/VideoCapture.tsx"
click node_hook "https://github.com/kaillora/moodtomusic/blob/main/frontend/lib/hooks/useWebSocket.ts"
click node_clientws "https://github.com/kaillora/moodtomusic/blob/main/frontend/lib/services/websocket.ts"
click node_store "https://github.com/kaillora/moodtomusic/blob/main/frontend/lib/store/index.ts"
click node_emotionui "https://github.com/kaillora/moodtomusic/blob/main/frontend/components/emotion/EmotionMeter.tsx"
click node_history "https://github.com/kaillora/moodtomusic/blob/main/frontend/components/emotion/EmotionHistory.tsx"
click node_recommendationui "https://github.com/kaillora/moodtomusic/blob/main/frontend/components/music/RecommendationCard.tsx"
click node_settings "https://github.com/kaillora/moodtomusic/blob/main/frontend/components/SettingsControl.tsx"
click node_app "https://github.com/kaillora/moodtomusic/blob/main/backend/app.py"
click node_routes "https://github.com/kaillora/moodtomusic/blob/main/backend/api/routes.py"
click node_ws "https://github.com/kaillora/moodtomusic/blob/main/backend/api/websocket.py"
click node_frames "https://github.com/kaillora/moodtomusic/blob/main/backend/utils/frame_processor.py"
click node_detector "https://github.com/kaillora/moodtomusic/blob/main/backend/services/emotion_detector.py"
click node_logger "https://github.com/kaillora/moodtomusic/blob/main/backend/services/emotion_logger.py"
click node_mapper "https://github.com/kaillora/moodtomusic/blob/main/backend/services/mood_mapper.py"
click node_recommender "https://github.com/kaillora/moodtomusic/blob/main/backend/services/music_recommender.py"
click node_dataset "https://github.com/kaillora/moodtomusic/blob/main/data/processed_songs.csv"
click node_emotionmodel "https://github.com/kaillora/moodtomusic/blob/main/models/emotion_detection/custom_model_weighted.h5"
click node_musicmodels "https://github.com/kaillora/moodtomusic/tree/main/models/music_recommendation"
click node_config "https://github.com/kaillora/moodtomusic/blob/main/backend/config.py"
click node_cli "https://github.com/kaillora/moodtomusic/blob/main/emotion.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_page,node_capture,node_hook,node_clientws,node_store,node_emotionui,node_history,node_recommendationui,node_settings toneBlue
class node_app,node_routes,node_ws,node_frames,node_config toneAmber
class node_detector,node_logger,node_mapper,node_recommender,node_dataset,node_emotionmodel,node_musicmodels toneMint
class node_cli toneRose
class node_user toneIndigo
```

## Components

### Web experience

- [App screen](https://github.com/kaillora/moodtomusic/blob/main/frontend/app/page.tsx): `frontend/app/page.tsx`
- [Video capture](https://github.com/kaillora/moodtomusic/blob/main/frontend/components/video/VideoCapture.tsx): `frontend/components/video/VideoCapture.tsx`
- [WebSocket hook](https://github.com/kaillora/moodtomusic/blob/main/frontend/lib/hooks/useWebSocket.ts): `frontend/lib/hooks/useWebSocket.ts`
- [WebSocket client](https://github.com/kaillora/moodtomusic/blob/main/frontend/lib/services/websocket.ts): `frontend/lib/services/websocket.ts`
- [UI state](https://github.com/kaillora/moodtomusic/blob/main/frontend/lib/store/index.ts): `frontend/lib/store/index.ts`
- [Emotion views](https://github.com/kaillora/moodtomusic/blob/main/frontend/components/emotion/EmotionMeter.tsx): `frontend/components/emotion/EmotionMeter.tsx`
- [Emotion history](https://github.com/kaillora/moodtomusic/blob/main/frontend/components/emotion/EmotionHistory.tsx): `frontend/components/emotion/EmotionHistory.tsx`
- [Recommendation card](https://github.com/kaillora/moodtomusic/blob/main/frontend/components/music/RecommendationCard.tsx): `frontend/components/music/RecommendationCard.tsx`
- [Settings control](https://github.com/kaillora/moodtomusic/blob/main/frontend/components/SettingsControl.tsx): `frontend/components/SettingsControl.tsx`

### Backend runtime

- [FastAPI app](https://github.com/kaillora/moodtomusic/blob/main/backend/app.py): `backend/app.py`
- [REST routes](https://github.com/kaillora/moodtomusic/blob/main/backend/api/routes.py): `backend/api/routes.py`
- [WebSocket handler](https://github.com/kaillora/moodtomusic/blob/main/backend/api/websocket.py): `backend/api/websocket.py`
- [Frame decoder](https://github.com/kaillora/moodtomusic/blob/main/backend/utils/frame_processor.py): `backend/utils/frame_processor.py`
- [Backend settings](https://github.com/kaillora/moodtomusic/blob/main/backend/config.py): `backend/config.py`

### Mood and recommendation

- [Emotion detector](https://github.com/kaillora/moodtomusic/blob/main/backend/services/emotion_detector.py): `backend/services/emotion_detector.py`
- [Emotion aggregation](https://github.com/kaillora/moodtomusic/blob/main/backend/services/emotion_logger.py): `backend/services/emotion_logger.py`
- [Emotion-to-VAD mapping](https://github.com/kaillora/moodtomusic/blob/main/backend/services/mood_mapper.py): `backend/services/mood_mapper.py`
- [Song recommender](https://github.com/kaillora/moodtomusic/blob/main/backend/services/music_recommender.py): `backend/services/music_recommender.py`
- [Song dataset](https://github.com/kaillora/moodtomusic/blob/main/data/processed_songs.csv): `data/processed_songs.csv`
- [CNN weights](https://github.com/kaillora/moodtomusic/blob/main/models/emotion_detection/custom_model_weighted.h5): `models/emotion_detection/custom_model_weighted.h5`
- [Music model assets](https://github.com/kaillora/moodtomusic/tree/main/models/music_recommendation): `models/music_recommendation`

### Standalone mode

- [Standalone webcam app](https://github.com/kaillora/moodtomusic/blob/main/emotion.py): `emotion.py`

### Other components

- **Listener**

## Connections

- Listener → App screen: uses
- Video capture → WebSocket hook: sends frames
- WebSocket hook → WebSocket client: uses (evidence: `frontend/lib/hooks/useWebSocket.ts`)
- WebSocket hook → UI state: updates state (evidence: `frontend/lib/hooks/useWebSocket.ts`)
- UI state → Emotion views: feeds display
- UI state → Emotion history: feeds display
- UI state → Recommendation card: feeds display
- Settings control → WebSocket hook: configures period
- WebSocket client → WebSocket handler: exchanges messages
- FastAPI app → REST routes: registers routes (evidence: `backend/app.py`)
- FastAPI app → WebSocket handler: dispatches socket (evidence: `backend/app.py`)
- FastAPI app → Emotion detector: loads model (evidence: `backend/app.py`)
- FastAPI app → Song recommender: initializes (evidence: `backend/app.py`)
- WebSocket handler → Frame decoder: decodes frames (evidence: `backend/api/websocket.py`)
- WebSocket handler → Emotion aggregation: uses (evidence: `backend/api/websocket.py`)
- WebSocket handler → Emotion-to-VAD mapping: uses (evidence: `backend/api/websocket.py`)
- WebSocket handler → Emotion detector: processes detections
- WebSocket handler → Song recommender: requests songs
- Song recommender → Song dataset: loads songs (evidence: `backend/services/music_recommender.py`)
- Song recommender → Music model assets: loads models (evidence: `backend/services/music_recommender.py`)
- FastAPI app → Backend settings: reads settings (evidence: `backend/app.py`)
- Emotion detector → CNN weights: loads weights
- Standalone webcam app → Emotion aggregation: records emotion (evidence: `emotion.py`)
- Standalone webcam app → Song recommender: requests songs (evidence: `emotion.py`)
