# jayesh-cmd/daydreamin architecture

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

- Interactive diagram: https://gitdiagram.com/jayesh-cmd/daydreamin
- Repository: https://github.com/jayesh-cmd/daydreamin

## Overview

1. **Two-process architecture**
   - The repo is split into a SwiftUI iOS client and a Python FastAPI backend.
   - The iPhone app does not stream directly from third-party services; it talks to the backend over HTTP, and the backend resolves metadata, streaming, lyrics, and recommendations.

2. **iOS app boundary**
   - `Bitsongs/BitsongApp.swift` is the app entry point.
   - `Bitsongs/Views/MusicPlayerView.swift` is the main screen, assembled from reusable UI components under `Views/Components/`.
   - The UI is mostly presentation-only; state and playback behavior are centralized in the view model.

3. **Client state and playback orchestration**
   - `ViewModels/MusicPlayerViewModel.swift` is the core client-side controller.
   - It manages current song state, queue/up-next data, search results, playback state, and coordination with the network layer.
   - It is the bridge between SwiftUI views and the backend API.

4. **Client networking layer**
   - `Services/NetworkService.swift` is the only explicit server-facing client module.
   - It holds the backend base URL and performs API calls to the FastAPI server for search, chart data, play/stream resolution, lyrics, recommendations, and caching.
   - This is the main architectural boundary between local UI and remote data/services.

5. **Playback and system integrations on iOS**
   - The app uses `AVPlayer` for actual audio playback.
   - It also integrates with `MPNowPlayingInfoCenter` and `MPRemoteCommandCenter` to support lock-screen metadata and remote controls.
   - Haptics are handled locally through `Utilities/HapticManager.swift`.

6. **UI composition and visual theming**
   - `AlbumArtView`, `PlaybackControlsView`, `SearchBarView`, `UpNextView`, and `DynamicBackgroundView` split the player into clear subviews.
   - `Utilities/ColorExtractor.swift` suggests the artwork drives dynamic theming/background colors.
   - Asset catalogs contain album art variants and app icons; these are presentation assets, not core data.

7. **Backend responsibilities**
   - `Server/app.py` is the backend entry point and exposes the mobile API.
   - It acts as an orchestration service: search, trending/chart retrieval, stream URL generation/proxying, lyrics lookup, recommendation generation, and cache management.
   - The backend is the only component that talks to external music/content sources.

8. **External service dependencies**
   - Search and metadata are sourced from the iTunes API.
   - Audio streaming is resolved via `yt-dlp`, with `ffmpeg` required for processing/compatibility.
   - Lyrics come from LRCLIB.
   - These integrations matter architecturally because the server converts unstable third-party sources into a simpler app-facing API.

9. **Recommendation subsystem**
   - `Server/recommendation/` is a self-contained engine with behavior-based and content-based strategies.
   - `behavior.py`, `content.py`, `engine.py`, and `storage.py` indicate separation between scoring logic, strategy composition, and persistence.
   - `Server/data/songs.json` and `Server/data/tally_counter.json` are the primary data stores for recommendation inputs and transition history.

10. **Caching and persistence**
   - The backend maintains an audio cache separate from recommendation data, with cached files stored under `song_cache/`.
   - The README describes cache eviction when size exceeds 600 MB, implying the server owns lifecycle management for streamed media artifacts.
   - This keeps repeated plays fast without pushing cache complexity into the iOS client.

11. **API shape and data flow**
   - Typical flow: user searches in the app → iOS calls `/api/mobile/search` or `/api/mobile/chart` → user selects a song → iOS calls `/api/mobile/play` → backend returns a stream URL and may record transition data → AVPlayer plays audio.
   - Recommendations and “Up Next” are separate follow-up calls driven by the selected/current song.
   - Lyrics are also a separate lookup, so the UI can present them independently of playback.

12. **Validation and test boundary**
   - `Server/tests/test_recommendation.py` focuses on recommendation correctness, indicating this logic is treated as an isolated backend subsystem.
   - `requirements.txt` and the README setup show the backend is intended to run as a local Python service alongside the Xcode app during development.

Main entry points: [`Bitsongs/Views/MusicPlayerView.swift`](https://github.com/jayesh-cmd/daydreamin/blob/main/Bitsongs/Views/MusicPlayerView.swift) (Player Screen), [`Bitsongs/ViewModels/MusicPlayerViewModel.swift`](https://github.com/jayesh-cmd/daydreamin/blob/main/Bitsongs/ViewModels/MusicPlayerViewModel.swift) (Player VM), [`Server/app.py`](https://github.com/jayesh-cmd/daydreamin/blob/main/Server/app.py) (API Server), [`Server/recommendation/engine.py`](https://github.com/jayesh-cmd/daydreamin/blob/main/Server/recommendation/engine.py) (Reco Engine)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ios["iOS Client"]
  node_app(("App Entry<br/>SwiftUI app<br/>[BitsongApp.swift]"))
  node_player_view["Player Screen<br/>SwiftUI view"]
  node_music_vm{{"Player VM<br/>view model"}}
  node_network["API Client<br/>network service"]
  node_song_model["Song Model<br/>data model<br/>[Song.swift]"]
  node_album_art["Album Art<br/>UI component<br/>[AlbumArtView.swift]"]
  node_controls["Playback Controls<br/>UI component"]
  node_search_bar["Search Bar<br/>UI component"]
  node_up_next["Up Next<br/>UI component<br/>[UpNextView.swift]"]
  node_background["Dynamic BG<br/>UI component"]
  node_haptics["Haptics"]
  node_colors["Color Extractor"]
  node_tone["Tone Generator"]
end

subgraph group_backend["Backend Service"]
  node_server(("API Server<br/>FastAPI app<br/>[app.py]"))
  node_reco_engine{{"Reco Engine<br/>recommendation subsystem<br/>[engine.py]"}}
  node_reco_behavior["Behavior Scoring<br/>recommendation strategy<br/>[behavior.py]"]
  node_reco_content["Content Scoring<br/>recommendation strategy<br/>[content.py]"]
  node_reco_storage[("Reco Storage<br/>persistence<br/>[storage.py]")]
  node_songs_data["Songs Data<br/>json store<br/>[songs.json]"]
  node_tally_data["Tally Data<br/>json store<br/>[tally_counter.json]"]
  node_tests["Reco Tests<br/>backend tests"]
end

subgraph group_external["External Sources"]
  node_itunes(("iTunes API<br/>external metadata source"))
  node_yt_dlp(("yt-dlp<br/>stream resolver"))
  node_ffmpeg(("ffmpeg<br/>media processor"))
  node_lrclib(("LRCLIB<br/>lyrics source"))
  node_avplayer(("AVPlayer<br/>iOS audio runtime"))
  node_nowplaying(("Now Playing<br/>system integration"))
end

node_app -->|"launches"| node_player_view
node_player_view -->|"binds"| node_music_vm
node_player_view -->|"composes"| node_album_art
node_player_view -->|"composes"| node_controls
node_player_view -->|"composes"| node_search_bar
node_player_view -->|"composes"| node_up_next
node_player_view -->|"styles"| node_background
node_music_vm -->|"calls"| node_network
node_music_vm -->|"uses"| node_song_model
node_music_vm -->|"triggers"| node_haptics
node_music_vm -->|"uses"| node_tone
node_album_art -->|"feeds"| node_colors
node_colors -->|"themes"| node_background
node_network -->|"http api"| node_server
node_server -->|"queries"| node_itunes
node_server -->|"resolves"| node_yt_dlp
node_server -->|"processes"| node_ffmpeg
node_server -->|"fetches"| node_lrclib
node_server -->|"delegates"| node_reco_engine
node_reco_engine -->|"scores"| node_reco_behavior
node_reco_engine -->|"scores"| node_reco_content
node_reco_engine -->|"persists"| node_reco_storage
node_reco_storage -->|"reads"| node_songs_data
node_reco_storage -->|"updates"| node_tally_data
node_server -.->|"validated by"| node_tests
node_music_vm -->|"controls"| node_avplayer
node_music_vm -->|"publishes"| node_nowplaying

click node_app "https://github.com/jayesh-cmd/daydreamin/blob/main/Bitsongs/BitsongApp.swift"
click node_player_view "https://github.com/jayesh-cmd/daydreamin/blob/main/Bitsongs/Views/MusicPlayerView.swift"
click node_music_vm "https://github.com/jayesh-cmd/daydreamin/blob/main/Bitsongs/ViewModels/MusicPlayerViewModel.swift"
click node_network "https://github.com/jayesh-cmd/daydreamin/blob/main/Bitsongs/Services/NetworkService.swift"
click node_song_model "https://github.com/jayesh-cmd/daydreamin/blob/main/Bitsongs/Models/Song.swift"
click node_album_art "https://github.com/jayesh-cmd/daydreamin/blob/main/Bitsongs/Views/Components/AlbumArtView.swift"
click node_controls "https://github.com/jayesh-cmd/daydreamin/blob/main/Bitsongs/Views/Components/PlaybackControlsView.swift"
click node_search_bar "https://github.com/jayesh-cmd/daydreamin/blob/main/Bitsongs/Views/Components/SearchBarView.swift"
click node_up_next "https://github.com/jayesh-cmd/daydreamin/blob/main/Bitsongs/Views/Components/UpNextView.swift"
click node_background "https://github.com/jayesh-cmd/daydreamin/blob/main/Bitsongs/Views/Components/DynamicBackgroundView.swift"
click node_haptics "https://github.com/jayesh-cmd/daydreamin/blob/main/Bitsongs/Utilities/HapticManager.swift"
click node_colors "https://github.com/jayesh-cmd/daydreamin/blob/main/Bitsongs/Utilities/ColorExtractor.swift"
click node_tone "https://github.com/jayesh-cmd/daydreamin/blob/main/Bitsongs/Utilities/ToneGenerator.swift"
click node_server "https://github.com/jayesh-cmd/daydreamin/blob/main/Server/app.py"
click node_reco_engine "https://github.com/jayesh-cmd/daydreamin/blob/main/Server/recommendation/engine.py"
click node_reco_behavior "https://github.com/jayesh-cmd/daydreamin/blob/main/Server/recommendation/behavior.py"
click node_reco_content "https://github.com/jayesh-cmd/daydreamin/blob/main/Server/recommendation/content.py"
click node_reco_storage "https://github.com/jayesh-cmd/daydreamin/blob/main/Server/recommendation/storage.py"
click node_songs_data "https://github.com/jayesh-cmd/daydreamin/blob/main/Server/data/songs.json"
click node_tally_data "https://github.com/jayesh-cmd/daydreamin/blob/main/Server/data/tally_counter.json"
click node_tests "https://github.com/jayesh-cmd/daydreamin/blob/main/Server/tests/test_recommendation.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_app,node_player_view,node_music_vm,node_network,node_song_model,node_album_art,node_controls,node_search_bar,node_up_next,node_background,node_haptics,node_colors,node_tone toneBlue
class node_server,node_reco_engine,node_reco_behavior,node_reco_content,node_reco_storage,node_songs_data,node_tally_data,node_tests toneAmber
class node_itunes,node_yt_dlp,node_ffmpeg,node_lrclib,node_avplayer,node_nowplaying toneMint
```

## Components

### iOS Client

SwiftUI app, local playback, and UI composition.

- [App Entry](https://github.com/jayesh-cmd/daydreamin/blob/main/Bitsongs/BitsongApp.swift): SwiftUI app · Launches the iOS application and wires the main scene. · `Bitsongs/BitsongApp.swift`
- [Player Screen](https://github.com/jayesh-cmd/daydreamin/blob/main/Bitsongs/Views/MusicPlayerView.swift): SwiftUI view · Main music player screen assembled from reusable components. · `Bitsongs/Views/MusicPlayerView.swift`
- [Player VM](https://github.com/jayesh-cmd/daydreamin/blob/main/Bitsongs/ViewModels/MusicPlayerViewModel.swift): view model · Central client controller for playback state, queue, search, and backend coordination. · `Bitsongs/ViewModels/MusicPlayerViewModel.swift`
- [API Client](https://github.com/jayesh-cmd/daydreamin/blob/main/Bitsongs/Services/NetworkService.swift): network service · Single iOS boundary for backend calls: search, charts, play, lyrics, recommendations, and cache-aware requests. · `Bitsongs/Services/NetworkService.swift`
- [Song Model](https://github.com/jayesh-cmd/daydreamin/blob/main/Bitsongs/Models/Song.swift): data model · Shared song representation used by the client UI and view model. · `Bitsongs/Models/Song.swift`
- [Album Art](https://github.com/jayesh-cmd/daydreamin/blob/main/Bitsongs/Views/Components/AlbumArtView.swift): UI component · Displays artwork and feeds visual identity into the player UI. · `Bitsongs/Views/Components/AlbumArtView.swift`
- [Playback Controls](https://github.com/jayesh-cmd/daydreamin/blob/main/Bitsongs/Views/Components/PlaybackControlsView.swift): UI component · Play/pause and transport controls for local playback. · `Bitsongs/Views/Components/PlaybackControlsView.swift`
- [Search Bar](https://github.com/jayesh-cmd/daydreamin/blob/main/Bitsongs/Views/Components/SearchBarView.swift): UI component · Song search input embedded in the player experience. · `Bitsongs/Views/Components/SearchBarView.swift`
- [Up Next](https://github.com/jayesh-cmd/daydreamin/blob/main/Bitsongs/Views/Components/UpNextView.swift): UI component · Shows queued and recommended follow-up tracks. · `Bitsongs/Views/Components/UpNextView.swift`
- [Dynamic BG](https://github.com/jayesh-cmd/daydreamin/blob/main/Bitsongs/Views/Components/DynamicBackgroundView.swift): UI component · Background styling that adapts to album art and theme colors. · `Bitsongs/Views/Components/DynamicBackgroundView.swift`
- [Haptics](https://github.com/jayesh-cmd/daydreamin/blob/main/Bitsongs/Utilities/HapticManager.swift): utility · Local tactile feedback for player interactions. · `Bitsongs/Utilities/HapticManager.swift`
- [Color Extractor](https://github.com/jayesh-cmd/daydreamin/blob/main/Bitsongs/Utilities/ColorExtractor.swift): utility · Derives theme colors from artwork for dynamic presentation. · `Bitsongs/Utilities/ColorExtractor.swift`
- [Tone Generator](https://github.com/jayesh-cmd/daydreamin/blob/main/Bitsongs/Utilities/ToneGenerator.swift): utility · Local audio/tone utility used by the app. · `Bitsongs/Utilities/ToneGenerator.swift`

### Backend Service

FastAPI API, orchestration, and recommendation engine.

- [API Server](https://github.com/jayesh-cmd/daydreamin/blob/main/Server/app.py): FastAPI app · Backend entry point that exposes the mobile API and orchestrates external lookups. · `Server/app.py`
- [Reco Engine](https://github.com/jayesh-cmd/daydreamin/blob/main/Server/recommendation/engine.py): recommendation subsystem · Combines strategy logic and persistence for behavior- and content-based recommendations. · `Server/recommendation/engine.py`
- [Behavior Scoring](https://github.com/jayesh-cmd/daydreamin/blob/main/Server/recommendation/behavior.py): recommendation strategy · Scores recommendations from transition and listening behavior. · `Server/recommendation/behavior.py`
- [Content Scoring](https://github.com/jayesh-cmd/daydreamin/blob/main/Server/recommendation/content.py): recommendation strategy · Scores recommendations from song metadata and content similarity. · `Server/recommendation/content.py`
- [Reco Storage](https://github.com/jayesh-cmd/daydreamin/blob/main/Server/recommendation/storage.py): persistence · Loads and updates recommendation state and counters. · `Server/recommendation/storage.py`
- [Songs Data](https://github.com/jayesh-cmd/daydreamin/blob/main/Server/data/songs.json): json store · Primary recommendation input dataset. · `Server/data/songs.json`
- [Tally Data](https://github.com/jayesh-cmd/daydreamin/blob/main/Server/data/tally_counter.json): json store · Transition history and counter store for recommendation learning. · `Server/data/tally_counter.json`
- [Reco Tests](https://github.com/jayesh-cmd/daydreamin/blob/main/Server/tests/test_recommendation.py): backend tests · Validates recommendation behavior as an isolated subsystem. · `Server/tests/test_recommendation.py`

### External Sources

Third-party services and system frameworks the repo depends on.

- **iTunes API**: external metadata source · Provides search and metadata lookup for songs.
- **yt-dlp**: stream resolver · Resolves audio streams and media URLs for playback.
- **ffmpeg**: media processor · Used for stream processing and compatibility on the server.
- **LRCLIB**: lyrics source · Provides lyrics lookup for the backend API.
- **AVPlayer**: iOS audio runtime · Handles actual audio playback on the device.
- **Now Playing**: system integration · Lock-screen metadata and media controls integration.

## Connections

- App Entry → Player Screen: launches · App entry presents the main player screen.
- Player Screen → Player VM: binds · The view is driven by centralized playback state.
- Player Screen → Album Art: composes · Main screen includes artwork presentation.
- Player Screen → Playback Controls: composes · Main screen includes playback controls.
- Player Screen → Search Bar: composes · Main screen includes search input.
- Player Screen → Up Next: composes · Main screen includes queue and follow-up tracks.
- Player Screen → Dynamic BG: styles · Main screen uses dynamic background treatment.
- Player VM → API Client: calls · View model delegates search, playback resolution, lyrics, and recommendations.
- Player VM → Song Model: uses · Tracks selected and queued songs through shared model objects.
- Player VM → Haptics: triggers · Playback interactions generate local haptic feedback.
- Player VM → Tone Generator: uses · May use local tone utilities for audio-related behavior.
- Album Art → Color Extractor: feeds · Artwork can drive extracted theme colors.
- Color Extractor → Dynamic BG: themes · Extracted colors inform the dynamic player background.
- API Client → API Server: http api · Client talks to the backend over HTTP.
- API Server → iTunes API: queries · Backend sources search and metadata from iTunes.
- API Server → yt-dlp: resolves · Backend resolves playable streams through yt-dlp.
- API Server → ffmpeg: processes · Backend uses ffmpeg for stream compatibility and processing.
- API Server → LRCLIB: fetches · Backend retrieves lyrics from LRCLIB.
- API Server → Reco Engine: delegates · Backend hands recommendation logic to the engine.
- Reco Engine → Behavior Scoring: scores · Behavior-based strategy contributes to ranking.
- Reco Engine → Content Scoring: scores · Content-based strategy contributes to ranking.
- Reco Engine → Reco Storage: persists · Engine reads and writes recommendation state.
- Reco Storage → Songs Data: reads · Uses song catalog data as recommendation input.
- Reco Storage → Tally Data: updates · Maintains transition counters and history.
- API Server → Reco Tests: validated by · Recommendation subsystem is covered by backend tests.
- Player VM → AVPlayer: controls · View model orchestrates local audio playback.
- Player VM → Now Playing: publishes · Updates system playback metadata and remote controls.
