Skip to content

About

Audio-similarity recommendations for Navidrome with low resource usage. Instant Mix and weekly mood playlist ranked on what tracks sound like.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Latest commit

 

History

27 Commits

Folders and files

Repository files navigation

navidrome-sonic

Audio-similarity recommendations for Navidrome: Instant Mix in the web UI, getSimilarSongs2, and the OpenSubsonic getSonicSimilarTracks / findSonicPath calls, ranked by what the tracks sound like, and six mood playlists per user, refreshed every second day.

Features

  • Similar tracks and Instant Mix from the audio itself, not from tags or play history.
  • Mood playlists: "Happy Mix", "Sad Mix", "Relaxed Mix", "Aggressive Mix", "Party Mix" and "Dance Mix" for every user, 50 tracks each, drawn from the user's libraries and replaced every second day.
  • Independent of file paths: the plugin needs no filesystem access, and Navidrome and the sidecar may mount the library at different paths (see Track lookup).
  • Each file's audio is analysed once. A moved, renamed or retagged file keeps its embedding and mood scores, and the same file in several libraries is analysed once and offered once.
  • Results per user: each user gets tracks from the libraries they can open, shared libraries included (see Several libraries and users).
  • Runs on one CPU thread next to Navidrome, without a database server or GPU.

How it works

Two parts, built into one image:

  • a Go sidecar that walks the music library once an hour, computes a Discogs-EffNet embedding and Essentia's six mood scores for every file with new audio, and answers nearest-neighbour and mood queries over HTTP; patch selection and ranking follow LMS. Embeddings live in SQLite under /data, each with a hash of the audio stream, which paths and tags don't affect. Runs on one thread with onnxruntime and ffmpeg: in sidecar/, sonic.go holds the scan and the HTTP API, analysis.go the front end and models, index.go the ranking. decode.c reads the audio through ffmpeg's libraries, decoding only around the analysed windows where that gives the same samples.
  • plugin/, a Navidrome plugin (sonic.ndp) that implements the SonicSimilarity capability and the GetSimilarSongsByTrack metadata agent by asking the sidecar, and writes the mood playlists: five minutes after it loads and at 04:00 on every odd day of the month, it asks the sidecar for random pages of the tracks that score at least 0.8 for a mood, up to ten pages of 100, until 50 of them lie in the user's libraries, and replaces the playlist's songs with those. Only playlists the plugin created are touched.

On start the container copies sonic.ndp into /plugins, which Navidrome mounts as its plugin folder.

Track lookup

Navidrome and the sidecar know tracks by different keys. Navidrome's track IDs are random, assigned when a library is first scanned, so the sidecar cannot derive them; the sidecar knows files.

For a seed track the plugin asks Navidrome's matcher for the track by ID. The answer carries the file size, duration and title, and the plugin sends those to the sidecar, which looks the track up by size, takes the closest duration among files of equal size and then an exact title. Answers go back as tags (title, artists, album, MBIDs, duration), which Navidrome matches to its own tracks.

The manifest requests matcher for that lookup, library because Navidrome requires it alongside matcher, http for the sidecar, and users, subsonicapi and scheduler for the mood playlists. It does not request library.filesystem: the lookup works without it, and with it Navidrome would also give the plugin track paths and read access to every library file.

Compose

services:
  navidrome:
    image: deluan/navidrome:0.64.2
    environment:
      ND_PLUGINS_ENABLED: "true"
      ND_PLUGINS_FOLDER: /plugins
      # First agent: answers Instant Mix; the others take over for tracks it
      # has no answer for.
      ND_AGENTS: sonic,deezer,lastfm,listenbrainz
    volumes:
      - music:/music:ro
      - navidrome_data:/data
      - navidrome_plugins:/plugins:ro
    networks: [default, sonic]
    depends_on:
      navidrome-sonic:
        condition: service_healthy

  navidrome-sonic:
    # Replace vX.Y.Z with the latest tag from
    # https://github.com/thilo/navidrome-sonic/releases
    build: https://github.com/thilo/navidrome-sonic.git#vX.Y.Z
    init: true
    # Analysis runs on one thread; the limits leave room above the peaks
    # under Benchmarks.
    cpus: 1
    mem_limit: 128m
    pids_limit: 32
    # Must be able to read the music files.
    user: "1000:1000"
    volumes:
      - music:/music:ro
      - navidrome_sonic_data:/data
      - navidrome_plugins:/plugins
    networks: [sonic]
    healthcheck:
      test: ["CMD", "wget", "-q", "-T", "5", "-O", "/dev/null", "http://127.0.0.1:8000/health"]
      interval: 30s

networks:
  # The sidecar's API has no authentication.
  sonic:
    internal: true

volumes:
  music:
  navidrome_data:
  navidrome_sonic_data:
  navidrome_plugins:

The plugin reaches the sidecar at http://navidrome-sonic:8000, so the service keeps that name, on a private Docker network.

Several libraries and users

Mount every Navidrome library somewhere below the sidecar's /music; it analyses whatever it finds there as one collection.

Results are per user. The sidecar answers from all libraries, and Navidrome matches the answer within the libraries the requesting user can open: a track in someone else's library drops out, a track in a shared library stays. For Instant Mix the plugin asks for three times the requested count, so dropped tracks leave room for shared ones; getSonicSimilarTracks and findSonicPath return at most the requested count before that filtering. For a mood playlist the plugin knows the user and asks for further random pages until 50 tracks remain, so a user who can open few of the tracks still gets a full playlist.

Enabling the plugin

Navidrome loads a new plugin disabled. Enable it once after the first start, and again whenever sonic.ndp changes, which Navidrome detects by its hash. The plugin looks up the seed track in the libraries it is granted and writes playlists for the users it is granted, so grant it all of both:

docker compose exec navidrome navidrome plugin edit sonic --all-libraries --all-users
docker compose exec navidrome navidrome plugin enable sonic
docker compose restart navidrome

The package is built reproducibly, so rebuilding the image without plugin changes keeps it enabled.

Until a track is analysed, Instant Mix for it falls back to the agents after sonic in ND_AGENTS, and it is missing from the mood playlists.

Test

cd sidecar && go test

It needs ffmpeg and sonic-decode in sidecar/; the Docker build runs it too.

Benchmarks

On a 2-core x86 server (AVX-512, no VNNI) shared with other services.

The sidecar (0.4.0), library of about 7,000 tracks:

Memory peak, building the index at startup 76 MiB
Memory, idle 44 MiB
Processes and threads 13
Analysing the whole library about 25 tracks a minute (measured on 0.3.0, which uses about the same CPU per track)

Embedding models, model CPU per track on up to 20 patches (MAEST: 8), one thread, decoding excluded:

Model Used by CPU per track Memory peak
Discogs-EffNet navidrome-sonic 0.3.0 and later 0.30 s, plus 0.14 s mel –
MusicNN LMS, navidrome-sonic 0.1–0.2 1.2 s 112 MiB
MAEST 30 s maestro 69 s 811 MiB

EffNet's mood heads run on the stored embedding, so moods cost no second pass.

License

GPL-3.0 (see LICENSE); the plugin links Navidrome's GPL-3.0 PDK. The models in models/ are Essentia's, licensed CC BY-NC-SA 4.0, which rules out commercial use; see NOTICE.

About

Audio-similarity recommendations for Navidrome with low resource usage. Instant Mix and weekly mood playlist ranked on what tracks sound like.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages