gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit93a4421293a44212jukebox: report, numbers of the full scan, notes on the index key rulemre93a44212/README.md

11.2 KB

  1. # jukebox — a network media player for the music on loreana
  2. caramboleyo's idea (ticket jukebox#1): a player that runs directly on loreana, opens a web interface and plays the audio there. It watches
  3. library folders, scans them, keeps every tag in an mpackdb for fast browsing, shows the album cover, orders by artist, album and song,
  4. searches, plays playlists and shuffles, and can pick only the songs that were played often. **This first part is the back end only:
  5. the scanner, the database and a small JSON API. There is no player screen yet** (the start page only points at the API).
  6. ## Read only
  7. The music library is never changed. The scanner lists folders and reads files (`listDir`, `changeTime` of hl:fs, and the programs `head`,
  8. `tail` and `ffprobe` on a file). Everything the app writes lives under `storage/` of this project: the database (`storage/mpackdb/`), the cover
  9. cache (`storage/covers/`) and the settings (`storage/settings.json`). The gate checks that the files of a library copy have the same bytes, sizes
  10. and times after a scan; the full scans of the real library were followed by `find -cnewer` and found nothing changed (reports/JB1-jukebox-scanner.md).
  11. ## Run it
  12. ```
  13. ./bin/hybriel project.hl # the server, port 8700 (JUKEBOX_PORT), storage in ./storage (JUKEBOX_STORAGE)
  14. JUKEBOX_STORAGE=/abs/path ./bin/hybriel tools/scan.hl [/library/path ...] # one scan from the command line, prints the numbers
  15. node tests/make-fixtures.mjs # rebuilds tests/fixtures with ffmpeg
  16. node tests/gate.mjs # the gate (fixtures scan + the API over HTTP, port 8701)
  17. ```
  18. `bin/` is not in git (the vendored binary is described below). The first start compiles the app (cold 10–40 s).
  19. Environment: `JUKEBOX_PORT`, `JUKEBOX_STORAGE`, `JUKEBOX_WATCH=0` (no dev watcher), `JUKEBOX_AUTOSCAN=0` (no timed scans), `HL_HOST`.
  20. ## Settings
  21. `storage/settings.json`: `{ "libraryPaths": ["/media/SLOW1TB1/Musik"], "rescanMinutes": 30 }` (these are the defaults). Read and changed through `GET/POST /api/settings`.
  22. ## Scanning
  23. - The server starts a scan about 2 seconds after it started, then every `rescanMinutes` after the last scan ended, and whenever `POST /api/rescan` asks.
  24. No inotify: a 75 GB library on a slow disk is cheaper to compare by change time than to watch.
  25. - **The scan runs in a child process** (`tools/scan.hl`, under `nice -n 19 ionice -c3`). A scan inside the server stopped it from answering for the whole scan
  26. (measured); the child lets the server answer all the time, and the server shows the child's progress in `/api/stats`.
  27. - A file is skipped when its path, size and `changeTime` equal the database record. A rescan of an unchanged library reads no tag.
  28. New and changed files are read again, files that vanished are removed, but only under a library path that could be listed (an unmounted disk removes nothing).
  29. - Files taken: mp3, m4a, mp4, m4v, ogg, oga, opus, flac, aac, wav, ape, wma, mkv, webm.
  30. - **mp3**: our own ID3 reader (`lib/id3.hl`). ID3v2.2, 2.3 and 2.4; unsynchronisation (whole tag, or per frame in 2.4); extended header; frame flags
  31. (grouping, data length; compressed or encrypted frames are skipped); text frames in Latin-1 (UTF-8 inside Latin-1 is recognised), UTF-16, UTF-16BE, UTF-8;
  32. POPM of every owner; PCNT; APIC (the front cover wins); ID3v1 at the end of the file when the ID3v2 tag has no title, artist and album. The duration comes from
  33. the Xing/Info/VBRI header or the first frame (`lib/mpeg.hl`). Hybriel's hl:fs has no ranged read yet (hybriel#233), so the first bytes of a file are read with
  34. `head -c` (and the last 128 with `tail -c`) through hl:proc, one process per file.
  35. - **Everything else** (ogg, m4a, flac, ...): one `ffprobe` process per file (format and stream tags, duration). Pictures inside those files are not extracted; the folder cover is used.
  36. - Normalising: genre numbers and "(13)" forms become names (the ID3v1 list with the Winamp extensions); track and disc "11/21" become number and total; the year comes from
  37. TDRC, TYER, TORY or TDOR; texts are trimmed; a missing artist is "Unknown artist", a missing album "Unknown album", a missing title is the file name.
  38. - **Play count** = the highest of all POPM counters and the PCNT counter in the file (Banshee and Guayadeque both hold the same plays, so they are not added); each owner's
  39. rating and count stay in `popm`. **Rating** = the highest POPM rating (0–255), `stars` maps it to 0–5 (1–31 → 1, 32–95 → 2, 96–159 → 3, 160–223 → 4, 224–255 → 5, so Banshee's 64/128/192/255 are 2/3/4/5).
  40. - **Covers**: the APIC picture, else `folder.jpg|jpeg|png`, `cover.*`, `front.*`, `AlbumArt*.jpg` in the file's folder. The picture is copied to `storage/covers/<first 32 hex of the SHA-256 of its content>.<ext>`,
  41. one file however many songs use it. A folder cover is looked at only when a file of that folder is read, so a cover added later shows when a song of the folder changes (or after deleting `storage/`).
  42. ## Database (mpackdb)
  43. Tables in `storage/mpackdb/`, all opened in `lib/db.hl` (the one file that knows mpackdb):
  44. | table | key | indexes | what |
  45. |---|---|---|---|
  46. | songs | `@id` | `!path`, folder, albumKey, artistKey, sortArtist, sortAlbum, sortTitle, playCount | one record per file |
  47. | albums | `id` (digest of albumKey) | `!key`, artistKey, sortName | derived: name, albumArtist, year, songCount, duration, playCount, coverId, folder |
  48. | artists | `id` (digest of artistKey) | `!key`, sortName | derived: name, songCount, albumCount, playCount, coverId |
  49. | covers | `hash` | – | ext, mime, size of a cached cover |
  50. | scans | `@id` | at | one record per finished scan: seconds, seen, added, updated, removed, unreadable |
  51. **An index key may hold no comma** (an mpackdb bug: such a key is lost when the table is reopened, repro in the report), so `albumKey`, `artistKey` and the sort keys have commas turned into character 2 (`safeKey`), and `path` and `folder` are not indexed.
  52. A song record: `path folder title artist albumArtist album track trackTotal disc discTotal year genre duration format kind size changeTime playCount popm[{owner,rating,count}] rating stars coverId addedAt`
  53. and the browse fields `albumKey` (= album artist or artist, folded, + album name folded + folder), `artistKey`, `search` (lower case, accents folded: artist, title, album, genre) and the sort keys
  54. `sortArtist` (artist, album, disc, track, title), `sortAlbum`, `sortTitle` (folded, parts separated by character 1 so that "alt" sorts before "alt lied").
  55. Albums and artists are rebuilt from the songs after a scan that changed something; their ids are stable. The song ids are mpackdb's own and stay when a file is re-read.
  56. Browsing is fast because the sort orders come from the indexes: the server keeps, per order, the list of song ids with their play count in memory (built from the sorted index once, dropped when a scan changed songs),
  57. and a page is a slice of that list plus one lookup per row.
  58. ### mpackdb version
  59. The brief asked for mpackdb 1.0.10, which Nemesis was to release; **no 1.0.10 release was found** (no tag in the hybriel repository, no newer plugin copy in any app). The plugin `plugins/mpackdb` is the newest one on loreana, the copy of
  60. gitoria's vendor (hybriel master c1b1010f, on-disk format of the JS mpackdb 1.0.7). All use of it is in `lib/db.hl` (tables) and the calls `put fetch find range iterate update delete count` in the topic files, so a switch is a copy of the plugin folder.
  61. ## The API (JSON, nothing is sent whole: default 50 rows, at most 200, `offset` for the next page)
  62. | request | answer |
  63. |---|---|
  64. | `GET /api/stats` | `library` (songs, artists, albums, covers, songsWithCover, videos, playedAtLeast1, playedAtLeast5, totalSeconds, totalBytes), `scan` (running, phase, seen, added, updated, removed, unreadable, lastSeconds, lastError, queued), `lastScan`, `settings` |
  65. | `GET /api/songs?sort=title\|artist\|album&offset&limit` | `{ total, offset, limit, items }`; optional `minPlays=N` (only songs played at least N times), `artist=<artist id>`, `album=<album id>`; default sort artist |
  66. | `GET /api/artists?offset&limit` | page of artists by name |
  67. | `GET /api/albums?offset&limit[&artist=<id>]` | page of albums by name |
  68. | `GET /api/search?q=&offset&limit` | `{ q, songs: page, artists: [≤10], albums: [≤10] }`; every word must match (case and accents ignored) |
  69. | `GET /api/shuffle?minPlays=5&count=50` | `count` random songs (no repeats) among the songs with at least `minPlays` plays; `total` = how many qualify |
  70. | `GET /api/cover/<coverId>` | the picture, cached for a year (the id is the content hash); 404 when unknown |
  71. | `POST /api/rescan` | 202, `{ started }`; the scan starts within a second |
  72. | `GET /api/settings`, `POST /api/settings` | `{ libraryPaths, rescanMinutes }` (absolute paths, at least one) |
  73. Song items: `id title artist albumArtist album track trackTotal disc year genre duration format kind size playCount rating stars popm coverId path addedAt`.
  74. There is no login yet and no audio route: the UI mission adds streaming with Range requests (hl:fs ranged read, hybriel#233) and the playlists.
  75. ## Files
  76. | file | what |
  77. |---|---|
  78. | `project.hl` | the map: routes, config, the one-second timer |
  79. | `lib/id3.hl`, `lib/id3-helpers.hl` | the ID3v2/ID3v1 reader; text encodings, unsynchronisation, genre names |
  80. | `lib/mpeg.hl` | mp3 duration |
  81. | `lib/tags.hl` | read one file (mp3 or ffprobe) and make the song record |
  82. | `lib/library.hl`, `lib/scan-state.hl` | the scanner and what it reports; `lib/schedule.hl` when it runs (child process) |
  83. | `lib/covers.hl` | the cover cache |
  84. | `lib/db.hl`, `lib/catalog.hl` | the tables; the derived albums and artists |
  85. | `lib/browse.hl`, `lib/order-cache.hl` | pages, search, shuffle, stats |
  86. | `lib/api.hl`, `lib/api-helpers.hl`, `lib/jsoncheck.hl` | the JSON routes (jsoncheck is notes' copy: JSON.parse cannot be caught) |
  87. | `lib/settings.hl`, `lib/util.hl` | settings file, storage folders; text folding, number helpers |
  88. | `tools/scan.hl` | one scan from the command line (also the child process of the server) |
  89. | `tests/make-fixtures.mjs`, `tests/fixtures/`, `tests/gate.mjs` | the test mp3s built byte by byte, and the gate |
  90. | `theme/`, `shared/`, `components/` | the WorldAPI theme (verbatim from gitoria), tokens, the start page |
  91. ## Hybriel notes
  92. - Vendor: `bin/` and `plugins/` are copied from gitoria.worldapi.org (Hybriel master c1b1010f, binary sha256 `df362699…`, day26 or later) plus `plugins/math` from the hybriel repository. `bin/` is not in git.
  93. Day26: no `list.upsert` and no one-argument `list.remove` anywhere in this code (checked with grep); the only `delete` calls are mpackdb's `delete(key)`.
  94. - Found while building (workarounds in the code, no JavaScript): `from` is a reserved word; no semicolons; a `.hl` file is one class (so `scan-state.hl` and `order-cache.hl` are class files); `Bytes.text()` on invalid UTF-8
  95. cannot be caught (so the UTF-8 decoder is our own); `String.fromCharCode` takes one argument and refuses surrogates; a scan run inside a timer handler of the server kept it from answering any request until the scan was over (so the scan is a child process).
  96. - hl:fs has no ranged read and no `stat` (hybriel#233, mission 483 is written, not built): `listDir` gives the size and type of every entry, `changeTime(path)` the change time. The start of an mp3 is read with `head -c`.

Branches

Latest commits

  • 93a44212jukebox: report, numbers of the full scan, notes on the index key rulemre
  • bf5167dajukebox: index keys without commas (mpackdb loses them after a reopen), comma fixture and gate checksmre
  • 28a82fa2jukebox: README, STATUS, LOG, DECISIONSmre
  • d13987dfjukebox: library scanner, ID3 reader, mpackdb database and JSON API, with fixtures and a gatemre