# danztee/shazam-build architecture

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

- Interactive diagram: https://gitdiagram.com/danztee/shazam-build
- Repository: https://github.com/danztee/shazam-build

## Overview

1. **Top-level shape**
   This repo is a two-part system: a Go backend that performs audio ingestion, fingerprinting, and matching; and a React frontend that records microphone audio and displays match results. The backend owns all recognition logic and persistence. The frontend is just a thin capture/display client.

2. **Backend entrypoints**
   The Go service starts from `cmd/main.go` and `cmd/api.go`. These files are the composition root for the API server: they load configuration, wire services, connect to PostgreSQL, and register HTTP routes. The presence of `chi` in the README suggests the backend is organized as a small REST API rather than a large framework app.

3. **Core recognition pipeline**
   `internal/audio` is the heart of the system. It contains the DSP stages needed to recreate Shazam-style fingerprints:
   - WAV decoding / normalization
   - FFT-based spectrogram generation
   - peak detection in the spectrogram
   - combinatorial fingerprint generation
   This subsystem is pure processing logic and is the main computational boundary in the repo.

4. **Ingestion flow**
   Song ingestion starts in `internal/songs/handler.go`, flows through `internal/songs/service.go`, and then fans out to Spotify and download/audio processing. The API accepts a Spotify URL, fetches metadata through `internal/spotify`, downloads or normalizes the source audio through `internal/download`, then runs it through `internal/audio` to extract fingerprints before storing everything in PostgreSQL.

5. **Matching flow**
   Recognition likely uses the same audio pipeline in reverse: recorded audio is submitted from the frontend to the backend, converted into fingerprints, and compared against stored fingerprints in the database. Matching then uses time-offset coherency / diagonal alignment logic to score candidate songs. The match result is returned to the frontend as a selected song or “no match.”

6. **Persistence layer**
   `internal/database` contains the database boundary. The generated SQLC code in `internal/database/queries` shows that persistence is strongly typed and query-driven rather than ORM-based. The schema is managed by Goose migrations under `migrations/`, with a `songs` table for metadata and a `fingerprints` table for the high-cardinality hash index that powers matching.

7. **External integrations**
   The only external service explicitly integrated is Spotify. `internal/spotify` handles talking to Spotify APIs for metadata and source lookup during song ingestion. FFmpeg is another important external dependency, but it acts as a local processing tool for audio normalization rather than a remote service.

8. **API surface**
   `internal/songs/handler.go` and `internal/spotify/handler.go` indicate the backend exposes resource-oriented HTTP endpoints. The README documents at least `POST /api/v1/songs` for ingestion. The API is therefore split by domain: song management/matching, and Spotify-related ingestion support.

9. **Frontend architecture**
   The `frontend/` app is a Vite + React + TypeScript client. Its main functional pieces are:
   - `AudioRecorder.tsx` for browser microphone capture via the MediaRecorder API
   - `MatchResult.tsx` for rendering recognition output
   - `App.tsx` as the UI coordinator
   The frontend does not implement recognition itself; it packages audio and sends it to the backend.

10. **Deployment and local infrastructure**
   `docker-compose.yml`, `.air.toml`, and the Makefile indicate a developer workflow built around local services and hot reloading. PostgreSQL is expected to run in Docker, while the Go backend likely uses Air for live reload. The frontend is a separate Node/pnpm project with its own build pipeline, so the system is split into independently runnable services.

11. **Data boundaries**
   The main data boundary is between raw audio and fingerprints. Audio enters as either Spotify-sourced tracks during ingestion or microphone recordings during recognition. Everything downstream of that boundary is represented as spectrogram peaks, hashes, and time offsets, which are the values persisted and queried for matching.

12. **What to draw in an architecture diagram**
   The cleanest diagram is:
   frontend microphone capture → HTTP request to Go API → audio normalization / FFT / peak / fingerprint pipeline → PostgreSQL fingerprint lookup → match scoring → JSON response back to frontend.
   In parallel, a song-ingestion path should show: Spotify URL → Spotify API/download → audio pipeline → database insert of song metadata plus fingerprints.

Main entry points: [`internal/songs/service.go`](https://github.com/danztee/shazam-build/blob/master/internal/songs/service.go) (Songs service), [`internal/audio/service.go`](https://github.com/danztee/shazam-build/blob/master/internal/audio/service.go) (Audio pipeline), [`internal/songs/handler.go`](https://github.com/danztee/shazam-build/blob/master/internal/songs/handler.go) (Songs API), [`cmd/api.go`](https://github.com/danztee/shazam-build/blob/master/cmd/api.go) (API setup)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_backend["Backend"]
  node_cmd_main["Main<br/>go entrypoint<br/>[main.go]"]
  node_cmd_api["API setup<br/>go composition root<br/>[api.go]"]
  node_songs_handler["Songs API<br/>http handler<br/>[handler.go]"]
  node_songs_service["Songs service<br/>domain service<br/>[service.go]"]
  node_spotify_handler["Spotify API<br/>http handler<br/>[handler.go]"]
  node_spotify_service["Spotify service<br/>integration service<br/>[service.go]"]
  node_download_service["Downloader<br/>audio fetcher<br/>[service.go]"]
  node_audio_wav["WAV decode<br/>dsp stage<br/>[wav.go]"]
  node_audio_spectrogram["Spectrogram<br/>dsp stage<br/>[spectrogram.go]"]
  node_audio_peaks["Peak detect<br/>dsp stage<br/>[peaks.go]"]
  node_audio_fingerprint["Fingerprints<br/>dsp stage<br/>[fingerprint.go]"]
  node_audio_service["Audio pipeline<br/>processing service<br/>[service.go]"]
end

subgraph group_frontend["Frontend"]
  node_frontend_app["App<br/>react shell<br/>[App.tsx]"]
  node_audio_recorder["Recorder<br/>browser capture<br/>[AudioRecorder.tsx]"]
  node_match_result["Result view<br/>ui component<br/>[MatchResult.tsx]"]
end

subgraph group_storage["Persistence"]
  node_db_queries["SQLC queries<br/>db access layer<br/>[db.go]"]
  node_db_schema[("Schema<br/>database migrations")]
end

node_cmd_main -->|"starts"| node_cmd_api
node_cmd_api -->|"registers"| node_songs_handler
node_cmd_api -->|"registers"| node_spotify_handler
node_songs_handler -->|"calls"| node_songs_service
node_songs_service -->|"uses"| node_spotify_service
node_songs_service -->|"uses"| node_download_service
node_songs_service -->|"uses"| node_audio_service
node_songs_service -->|"stores/reads"| node_db_queries
node_spotify_handler -->|"calls"| node_spotify_service
node_audio_service -->|"decodes"| node_audio_wav
node_audio_service -->|"builds"| node_audio_spectrogram
node_audio_service -->|"detects"| node_audio_peaks
node_audio_service -->|"hashes"| node_audio_fingerprint
node_db_queries -->|"depends on"| node_db_schema
node_frontend_app -->|"composes"| node_audio_recorder
node_frontend_app -->|"composes"| node_match_result
node_audio_recorder -->|"POST audio"| node_songs_handler
node_songs_handler -->|"returns"| node_match_result

click node_cmd_main "https://github.com/danztee/shazam-build/blob/master/cmd/main.go"
click node_cmd_api "https://github.com/danztee/shazam-build/blob/master/cmd/api.go"
click node_songs_handler "https://github.com/danztee/shazam-build/blob/master/internal/songs/handler.go"
click node_songs_service "https://github.com/danztee/shazam-build/blob/master/internal/songs/service.go"
click node_spotify_handler "https://github.com/danztee/shazam-build/blob/master/internal/spotify/handler.go"
click node_spotify_service "https://github.com/danztee/shazam-build/blob/master/internal/spotify/service.go"
click node_download_service "https://github.com/danztee/shazam-build/blob/master/internal/download/service.go"
click node_audio_wav "https://github.com/danztee/shazam-build/blob/master/internal/audio/wav.go"
click node_audio_spectrogram "https://github.com/danztee/shazam-build/blob/master/internal/audio/spectrogram.go"
click node_audio_peaks "https://github.com/danztee/shazam-build/blob/master/internal/audio/peaks.go"
click node_audio_fingerprint "https://github.com/danztee/shazam-build/blob/master/internal/audio/fingerprint.go"
click node_audio_service "https://github.com/danztee/shazam-build/blob/master/internal/audio/service.go"
click node_db_queries "https://github.com/danztee/shazam-build/blob/master/internal/database/queries/db.go"
click node_db_schema "https://github.com/danztee/shazam-build/blob/master/migrations/20251221032923_create_schema.sql"
click node_frontend_app "https://github.com/danztee/shazam-build/blob/master/frontend/src/App.tsx"
click node_audio_recorder "https://github.com/danztee/shazam-build/blob/master/frontend/src/components/AudioRecorder.tsx"
click node_match_result "https://github.com/danztee/shazam-build/blob/master/frontend/src/components/MatchResult.tsx"

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_cmd_main,node_cmd_api,node_songs_handler,node_songs_service,node_spotify_handler,node_spotify_service,node_download_service,node_audio_wav,node_audio_spectrogram,node_audio_peaks,node_audio_fingerprint,node_audio_service toneBlue
class node_frontend_app,node_audio_recorder,node_match_result toneAmber
class node_db_queries,node_db_schema toneMint
```

## Components

### Backend

Go API, ingestion, recognition, and integration logic

- [Main](https://github.com/danztee/shazam-build/blob/master/cmd/main.go): go entrypoint · Bootstraps the service process · `cmd/main.go`
- [API setup](https://github.com/danztee/shazam-build/blob/master/cmd/api.go): go composition root · Loads config, wiring, and HTTP routes · `cmd/api.go`
- [Songs API](https://github.com/danztee/shazam-build/blob/master/internal/songs/handler.go): http handler · Song ingestion and recognition endpoints · `internal/songs/handler.go`
- [Songs service](https://github.com/danztee/shazam-build/blob/master/internal/songs/service.go): domain service · Coordinates ingestion, matching, and persistence · `internal/songs/service.go`
- [Spotify API](https://github.com/danztee/shazam-build/blob/master/internal/spotify/handler.go): http handler · Support endpoints for Spotify-backed ingestion · `internal/spotify/handler.go`
- [Spotify service](https://github.com/danztee/shazam-build/blob/master/internal/spotify/service.go): integration service · Fetches Spotify metadata and source info · `internal/spotify/service.go`
- [Downloader](https://github.com/danztee/shazam-build/blob/master/internal/download/service.go): audio fetcher · Retrieves and prepares source audio for processing · `internal/download/service.go`
- [WAV decode](https://github.com/danztee/shazam-build/blob/master/internal/audio/wav.go): dsp stage · Loads and normalizes audio samples · `internal/audio/wav.go`
- [Spectrogram](https://github.com/danztee/shazam-build/blob/master/internal/audio/spectrogram.go): dsp stage · Builds frequency-time representations · `internal/audio/spectrogram.go`
- [Peak detect](https://github.com/danztee/shazam-build/blob/master/internal/audio/peaks.go): dsp stage · Finds salient spectral peaks · `internal/audio/peaks.go`
- [Fingerprints](https://github.com/danztee/shazam-build/blob/master/internal/audio/fingerprint.go): dsp stage · Generates combinatorial audio hashes · `internal/audio/fingerprint.go`
- [Audio pipeline](https://github.com/danztee/shazam-build/blob/master/internal/audio/service.go): processing service · Coordinates the full fingerprinting flow · `internal/audio/service.go`

### Frontend

React client for recording and displaying matches

- [App](https://github.com/danztee/shazam-build/blob/master/frontend/src/App.tsx): react shell · Coordinates recording and results UI · `frontend/src/App.tsx`
- [Recorder](https://github.com/danztee/shazam-build/blob/master/frontend/src/components/AudioRecorder.tsx): browser capture · Captures microphone audio in the browser · `frontend/src/components/AudioRecorder.tsx`
- [Result view](https://github.com/danztee/shazam-build/blob/master/frontend/src/components/MatchResult.tsx): ui component · Renders recognition outcomes · `frontend/src/components/MatchResult.tsx`

### Persistence

Database schema and query layer

- [SQLC queries](https://github.com/danztee/shazam-build/blob/master/internal/database/queries/db.go): db access layer · Typed persistence for songs and fingerprints · `internal/database/queries/db.go`
- [Schema](https://github.com/danztee/shazam-build/blob/master/migrations/20251221032923_create_schema.sql): database migrations · Creates songs and fingerprint tables · `migrations/20251221032923_create_schema.sql`

## Connections

- Main → API setup: starts · Process entry delegates to API wiring
- API setup → Songs API: registers · Binds song routes and handlers
- API setup → Spotify API: registers · Binds Spotify support routes
- Songs API → Songs service: calls · HTTP requests are handled by domain logic
- Songs service → Spotify service: uses · Looks up Spotify metadata during ingestion
- Songs service → Downloader: uses · Fetches or prepares source audio
- Songs service → Audio pipeline: uses · Extracts fingerprints from audio
- Songs service → SQLC queries: stores/reads · Persists songs and fingerprint rows
- Spotify API → Spotify service: calls · HTTP support endpoints delegate to integration logic
- Audio pipeline → WAV decode: decodes · Begins with waveform normalization
- Audio pipeline → Spectrogram: builds · Transforms samples into spectrograms
- Audio pipeline → Peak detect: detects · Finds spectral peaks
- Audio pipeline → Fingerprints: hashes · Produces matchable fingerprints
- SQLC queries → Schema: depends on · Queries operate over the migrated schema
- App → Recorder: composes · App hosts capture UI
- App → Result view: composes · App renders match output
- Recorder → Songs API: POST audio · Recorded audio is sent to the backend API
- Songs API → Result view: returns · Match response is surfaced to the UI
