# risris511/simsongs architecture

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

- Interactive diagram: https://gitdiagram.com/risris511/simsongs
- Repository: https://github.com/risris511/simsongs

## Overview

#### Purpose and execution model

This repository is a command-line music similarity pipeline. It collects Deezer track previews, derives audio embeddings, builds a nearest-neighbor index, and queries that index with a local audio file.

Primary entry points:
- Dataset acquisition: `build_deezer_db.py`
- Index construction: `build_index.py`
- Similarity search: `query_script.py`

#### Deezer ingestion boundary

`build_deezer_db.py` is the external-data boundary. It calls Deezer services via HTTP, supporting chart, playlist, and genre sources, then obtains available preview audio for selected tracks.

It handles real-world API/media failures by skipping tracks without usable previews or failed downloads. Preview downloads are cached, making subsequent database builds less dependent on repeated network retrieval.

Paths:
- `build_deezer_db.py`
- `requirements.txt`

#### Audio feature extraction

`audio_embeddings.py` defines the shared representation layer: audio input is decoded and transformed into numerical embeddings suitable for comparison. The runtime audio stack is based on `librosa`, `soundfile`, and `numpy`.

This module is the consistency boundary between corpus construction and query: database previews and user-provided songs must be embedded with the same feature logic and dimensionality.

Paths:
- `audio_embeddings.py`
- `build_deezer_db.py`
- `query_script.py`

#### Persistent music database

The ingestion stage combines track metadata with generated embeddings into an `.npz` database artifact. This artifact is the handoff from remote acquisition/audio processing to local indexing, avoiding Deezer calls during index construction and querying.

The database can be written to a custom output path; downstream scripts implicitly depend on its schema, including aligned track records and embedding rows.

Paths:
- `build_deezer_db.py`
- `build_index.py`

#### Index construction

`build_index.py` loads the persisted embedding database and creates a local similarity-search index. The project uses `scikit-learn` and `joblib`, indicating an in-process Python nearest-neighbor model serialized for reuse.

This stage separates potentially expensive model fitting from interactive query execution. Its output must preserve or reconstruct the mapping from neighbor positions back to database track metadata.

Paths:
- `build_index.py`
- `audio_embeddings.py`
- `requirements.txt`

#### Query flow

`query_script.py` accepts a local song path, extracts its embedding through the shared audio layer, loads the prebuilt index, and returns the closest corpus tracks. It is fully local after artifacts have been built; no Deezer API call is required during search.

Control flow is:
1. Build preview-backed embedding database.
2. Build and persist a nearest-neighbor index.
3. Embed query audio.
4. Retrieve and present matching Deezer tracks.

Paths:
- `query_script.py`
- `audio_embeddings.py`
- `build_index.py`

#### Runtime and dependencies

The system is a Python CLI workflow, not a service or web application. Material dependencies are:
- Audio decoding/features: `librosa`, `soundfile`, `numpy`
- Local ML search and serialization: `scikit-learn`, `joblib`
- Remote Deezer API and preview retrieval: `requests`

Paths:
- `requirements.txt`
- `readme.md`

Main entry points: [`build_deezer_db.py`](https://github.com/risris511/simsongs/blob/main/build_deezer_db.py) (Database builder), [`query_script.py`](https://github.com/risris511/simsongs/blob/main/query_script.py) (Similarity query), [`audio_embeddings.py`](https://github.com/risris511/simsongs/blob/main/audio_embeddings.py) (Audio embeddings), [`requirements.txt`](https://github.com/risris511/simsongs/blob/main/requirements.txt) (Python dependencies)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ingest["Corpus acquisition"]
  node_deezer{{"Deezer services<br/>external API"}}
  node_build_db["Database builder<br/>Python CLI<br/>[build_deezer_db.py]"]
  node_preview_cache[("Preview cache<br/>cached audio")]
  node_previews["Usable previews<br/>audio corpus"]
end

subgraph group_local["Local similarity pipeline"]
  node_embeddings["Audio embeddings<br/>shared feature layer"]
  node_music_db[("Music embedding database<br/>NPZ artifact")]
  node_build_index["Index builder<br/>Python CLI<br/>[build_index.py]"]
  node_neighbor_index[("Nearest-neighbor index<br/>serialized local model")]
  node_query_cli["Similarity query<br/>Python CLI<br/>[query_script.py]"]
  node_local_audio["Local song file<br/>query input"]
  node_matches["Matching Deezer tracks<br/>CLI results"]
end

node_dependencies["Python dependencies<br/>runtime packages<br/>[requirements.txt]"]
node_readme["Workflow documentation<br/>[readme.md]"]

node_deezer -->|"metadata and previews"| node_build_db
node_build_db -->|"caches downloads"| node_preview_cache
node_preview_cache -->|"reuses audio"| node_previews
node_build_db -->|"downloads usable audio"| node_previews
node_previews -->|"decode and transform"| node_embeddings
node_embeddings -->|"corpus vectors"| node_music_db
node_build_db -->|"writes metadata"| node_music_db
node_music_db -->|"loads embeddings and records"| node_build_index
node_build_index -->|"fits and serializes"| node_neighbor_index
node_local_audio -->|"song path"| node_query_cli
node_query_cli -->|"embeds query audio"| node_embeddings
node_neighbor_index -->|"loads index"| node_query_cli
node_music_db -->|"resolves track metadata"| node_query_cli
node_query_cli -->|"presents nearest tracks"| node_matches
node_dependencies -.->|"HTTP and audio runtime"| node_build_db
node_dependencies -.->|"audio stack"| node_embeddings
node_dependencies -.->|"search and serialization"| node_build_index

click node_build_db "https://github.com/risris511/simsongs/blob/main/build_deezer_db.py"
click node_embeddings "https://github.com/risris511/simsongs/blob/main/audio_embeddings.py"
click node_build_index "https://github.com/risris511/simsongs/blob/main/build_index.py"
click node_query_cli "https://github.com/risris511/simsongs/blob/main/query_script.py"
click node_dependencies "https://github.com/risris511/simsongs/blob/main/requirements.txt"
click node_readme "https://github.com/risris511/simsongs/blob/main/readme.md"

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_deezer,node_build_db,node_preview_cache,node_previews toneBlue
class node_embeddings,node_music_db,node_build_index,node_neighbor_index,node_query_cli,node_local_audio,node_matches toneAmber
class node_dependencies,node_readme toneNeutral
```

## Components

### Corpus acquisition

- **Deezer services**: external API · Track metadata and preview audio source.
- [Database builder](https://github.com/risris511/simsongs/blob/main/build_deezer_db.py): Python CLI · `build_deezer_db.py`
- **Preview cache**: cached audio · Avoids repeated preview downloads.
- **Usable previews**: audio corpus

### Local similarity pipeline

- [Audio embeddings](https://github.com/risris511/simsongs/blob/main/audio_embeddings.py): shared feature layer · Keeps corpus and query vectors compatible. · `audio_embeddings.py`
- **Music embedding database**: NPZ artifact · Aligned track metadata and embedding rows.
- [Index builder](https://github.com/risris511/simsongs/blob/main/build_index.py): Python CLI · `build_index.py`
- **Nearest-neighbor index**: serialized local model · Preserves lookup from neighbors to tracks.
- [Similarity query](https://github.com/risris511/simsongs/blob/main/query_script.py): Python CLI · `query_script.py`
- **Local song file**: query input
- **Matching Deezer tracks**: CLI results

### Other components

- [Python dependencies](https://github.com/risris511/simsongs/blob/main/requirements.txt): runtime packages · Audio, HTTP, nearest-neighbor, and serialization libraries. · `requirements.txt`
- [Workflow documentation](https://github.com/risris511/simsongs/blob/main/readme.md): documentation · `readme.md`

## Connections

- Deezer services → Database builder: metadata and previews
- Database builder → Preview cache: caches downloads
- Preview cache → Usable previews: reuses audio
- Database builder → Usable previews: downloads usable audio
- Usable previews → Audio embeddings: decode and transform
- Audio embeddings → Music embedding database: corpus vectors
- Database builder → Music embedding database: writes metadata
- Music embedding database → Index builder: loads embeddings and records
- Index builder → Nearest-neighbor index: fits and serializes
- Local song file → Similarity query: song path
- Similarity query → Audio embeddings: embeds query audio
- Nearest-neighbor index → Similarity query: loads index
- Music embedding database → Similarity query: resolves track metadata
- Similarity query → Matching Deezer tracks: presents nearest tracks
- Python dependencies → Database builder: HTTP and audio runtime
- Python dependencies → Audio embeddings: audio stack
- Python dependencies → Index builder: search and serialization
