gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit95f2cd7395f2cd73|||PR Use typed parent methods for screen playback and local server facesmre95f2cd73/README.md

17.8 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.
  5. **Done so far:** the scanner, the database and the JSON API (mission JB1), and **the library screens with playing in the browser (mission JB2)**:
  6. Songs, Artists, Albums, an artist, an album, Search, a Favourites filter and a player bar at the bottom. Shuffle, the now-playing overlay and counting new plays came with JB3 (see below). Not yet: playlists, keyboard keys (a Hybriel gap), a login (home network only; the server binds 127.0.0.1 unless `HL_HOST=0.0.0.0` is set).
  7. ## The screens (JB2)
  8. Phone first (390 px), then desktop (1280 px), on the WorldAPI layouts theme (dark): the layouts header with the main menu (the phone's sidebar behind the toggle).
  9. - **Songs** (`/`): all 13,449 songs in one list. Sort by artist, title, album or most played; the **Favourites** button keeps the songs played at least N times
  10. (N is a field next to it, default 5) and the line under the toolbar says how many songs that is ("4,572 songs").
  11. - **Artists** (`/artists`) and **Albums** (`/albums`): by name, with a small cover. **An artist** (`/artists/<id>`) has a switch between their albums and their songs;
  12. **an album** (`/albums/<id>`) shows the cover large on top and the songs in track order.
  13. - **Search** (`/search`): one field, results while you type (the search waits 250 ms after the last key): artists and albums (at most 10 each) above, the songs below.
  14. - **Playing:** a tap on a song plays it in the media player at the bottom (a compact bar: seek line, small cover, title and artist, Shuffle, previous, play/pause, next, Repeat (JB19, no 10 s buttons); the
  15. times on desktop). The list the song was tapped in is the queue: the shell asks the server for the next 40 songs of that list and refills before they run out; when a song
  16. ends the next one starts. The bar belongs to the shell, so the music goes on while you open other screens.
  17. - **Long lists:** the lists are the **virtual list** (`components/virtual-list`, a new shared component, README there): fixed row height 56 px, only the rows near the viewport
  18. exist (about 35), the rows come from the server 100 at a time as you scroll; the whole 13,449 rows scroll without lag. Thumbnails are 96 px pictures made once by ffmpeg
  19. (`/api/thumb/<cover id>`) and loaded lazily.
  20. - **Audio route** `GET /stream/<song id>` (`lib/stream.hl`): HTTP Range (206 with Content-Range, 416 past the end, one answer at most 1 MiB, so `bytes=0-` gets the first
  21. megabyte and the browser asks for the next as it plays), the content type from the format, the bytes read with hl:fs `readBytes(path, offset, length)` and `stat(path)`
  22. (hybriel#233, since Hybriel set-day27b). The id is looked up in the songs table; the path comes from the database, never from the URL.
  23. A request without a Range header is answered like `bytes=0-`.
  24. ## Read only
  25. The music library is never changed. The scanner lists folders and reads files (`listDir`, `changeTime` of hl:fs, and the programs `head`,
  26. `tail` and `ffprobe` on a file). Everything the app writes lives under `storage/` of this project: the database (`storage/mpackdb/`), the cover
  27. cache (`storage/covers/`) and the settings (`storage/settings.json`). The gate checks that the files of a library copy have the same bytes, sizes
  28. 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).
  29. ## Run it
  30. ```
  31. ./bin/hybriel project.hl # the server, port 8700 (JUKEBOX_PORT), storage in ./storage (JUKEBOX_STORAGE)
  32. JUKEBOX_STORAGE=/abs/path ./bin/hybriel tools/scan.hl [/library/path ...] # one scan from the command line, prints the numbers
  33. node tests/make-fixtures.mjs # rebuilds tests/fixtures with ffmpeg
  34. node tests/gate.mjs # the scanner and API gate (fixtures scan + the API over HTTP, port 8701)
  35. node tests/ui.mjs # the screens' gate: a generated database of 13,450 songs, a real headless Chrome, 390 and 1280 px (server 8710, Chrome 8700-8709)
  36. node tests/real.mjs # one check on a COPY of the real database (see the file's head): scroll, search, play 5 s
  37. tools/sync-components.sh [46fefaa] # sync pinned icons, player (51c3818) and virtual list (e5b98ec); --check verifies all three (components/SOURCE)
  38. JUKEBOX_STORAGE=/abs/storage ./bin/hybriel tools/make-test-db.hl song.mp3 cover.jpg # the generated test database
  39. ```
  40. `bin/` is not in git (the vendored binary is described below). The first start compiles the app (cold 10–40 s).
  41. Environment: `JUKEBOX_PORT`, `JUKEBOX_STORAGE`, `JUKEBOX_WATCH=0` (no dev watcher), `JUKEBOX_AUTOSCAN=0` (no timed scans), `HL_HOST`.
  42. ## Settings
  43. `storage/settings.json`: `{ "libraryPaths": ["/media/SLOW1TB1/Musik"], "rescanMinutes": 30 }` (these are the defaults). Read and changed through `GET/POST /api/settings`.
  44. ## Scanning
  45. - 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.
  46. No inotify: a 75 GB library on a slow disk is cheaper to compare by change time than to watch.
  47. - **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
  48. (measured); the child lets the server answer all the time, and the server shows the child's progress in `/api/stats`.
  49. - A file is skipped when its path, size and `changeTime` equal the database record. A rescan of an unchanged library reads no tag.
  50. 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).
  51. - Files taken: mp3, m4a, mp4, m4v, ogg, oga, opus, flac, aac, wav, ape, wma, mkv, webm.
  52. - **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
  53. (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;
  54. 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
  55. the Xing/Info/VBRI header or the first frame (`lib/mpeg.hl`). The first bytes of a file are read with hl:fs `readBytes(path, 0, n)`
  56. and the last 128 with `stat` and `readBytes` (hybriel#233, set-day27b); no process per file any more.
  57. - **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.
  58. - 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
  59. 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.
  60. - **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
  61. 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).
  62. - **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>`,
  63. 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/`).
  64. ## Database (mpackdb)
  65. Tables in `storage/mpackdb/`, all opened in `lib/db.hl` (the one file that knows mpackdb):
  66. | table | key | indexes | what |
  67. |---|---|---|---|
  68. | songs | `@id` | `!path`, folder, albumKey, artistKey, sortArtist, sortAlbum, sortTitle, playCount | one record per file |
  69. | albums | `id` (digest of albumKey) | `!key`, artistKey, sortName | derived: name, albumArtist, year, songCount, duration, playCount, coverId, folder |
  70. | artists | `id` (digest of artistKey) | `!key`, sortName | derived: name, songCount, albumCount, playCount, coverId |
  71. | covers | `hash` | – | ext, mime, size of a cached cover |
  72. | scans | `@id` | at | one record per finished scan: seconds, seen, added, updated, removed, unreadable |
  73. **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.
  74. 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`
  75. 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
  76. `sortArtist` (artist, album, disc, track, title), `sortAlbum`, `sortTitle` (folded, parts separated by character 1 so that "alt" sorts before "alt lied").
  77. 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.
  78. 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),
  79. and a page is a slice of that list plus one lookup per row.
  80. ### mpackdb version
  81. 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
  82. 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.
  83. ## The API (JSON, nothing is sent whole: default 50 rows, at most 200, `offset` for the next page)
  84. | request | answer |
  85. |---|---|
  86. | `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` |
  87. | `GET /api/songs?sort=title\|artist\|album\|plays&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 |
  88. | `GET /api/artists?offset&limit` | page of artists by name |
  89. | `GET /api/albums?offset&limit[&artist=<id>]` | page of albums by name |
  90. | `GET /api/search?q=&offset&limit` | `{ q, songs: page, artists: [≤10], albums: [≤10] }`; every word must match (case and accents ignored) |
  91. | `GET /api/shuffle?minPlays=5&count=50` | `count` random songs (no repeats) among the songs with at least `minPlays` plays; `total` = how many qualify |
  92. | `GET /api/cover/<coverId>` | the picture, cached for a year (the id is the content hash); 404 when unknown |
  93. | `GET /api/thumb/<coverId>` | the same as a 96 px square jpeg (made once with ffmpeg into `storage/covers/thumbs/`) |
  94. | `GET /stream/<songId>` | the audio with HTTP Range (see above) |
  95. | `POST /api/rescan` | 202, `{ started }`; the scan starts within a second |
  96. | `GET /api/settings`, `POST /api/settings` | `{ libraryPaths, rescanMinutes }` (absolute paths, at least one) |
  97. Song items: `id title artist albumArtist album track trackTotal disc year genre duration format kind size playCount rating stars popm coverId path addedAt`.
  98. There is no login yet. `sort=plays` is the most played first (a counting sort over the play counts: mpackdb's playCount index orders the numbers as text, 9 before 12).
  99. ## Files
  100. | file | what |
  101. |---|---|
  102. | `project.hl` | the map: routes, config, the one-second timer |
  103. | `lib/id3.hl`, `lib/id3-helpers.hl` | the ID3v2/ID3v1 reader; text encodings, unsynchronisation, genre names |
  104. | `lib/mpeg.hl` | mp3 duration |
  105. | `lib/tags.hl` | read one file (mp3 or ffprobe) and make the song record |
  106. | `lib/library.hl`, `lib/scan-state.hl` | the scanner and what it reports; `lib/schedule.hl` when it runs (child process) |
  107. | `lib/covers.hl` | the cover cache |
  108. | `lib/db.hl`, `lib/catalog.hl` | the tables; the derived albums and artists |
  109. | `lib/browse.hl`, `lib/order-cache.hl` | pages, search, shuffle, stats |
  110. | `lib/api.hl`, `lib/api-helpers.hl`, `lib/jsoncheck.hl` | the JSON routes (jsoncheck is notes' copy: JSON.parse cannot be caught) |
  111. | `lib/settings.hl`, `lib/util.hl` | settings file, storage folders; text folding, number helpers |
  112. | `tools/scan.hl` | one scan from the command line (also the child process of the server) |
  113. | `tests/make-fixtures.mjs`, `tests/fixtures/`, `tests/gate.mjs` | the test mp3s built byte by byte, and the gate |
  114. | `lib/rows.hl` | what a list row, an artist or album head and a queue item show (the one place that decides how a row reads) |
  115. | `lib/stream.hl` | the audio route with Range |
  116. | `components/main.hl` | the shell: header, menu, player bar, the server faces `jbRows` and `jbQueue`, `jbPlay` |
  117. | `components/songs.hl artists.hl albums.hl artist.hl album.hl search.hl` | the screens |
  118. | `components/styles.hl` | our CSS (theme inherited; the screen, the toolbar, the album head, the compact player bar) |
  119. | `components/media-player/`, `virtual-list/`, `icons/`, `SOURCE` | vendored from components.hybriel by `tools/sync-components.sh` (byte-verbatim, never edited here) |
  120. | `theme/`, `shared/` | the WorldAPI theme (verbatim from layouts.worldapi.org) and the tokens |
  121. | `tests/ui.mjs`, `tests/real.mjs`, `tests/cdp.mjs`, `tests/ports.mjs` | the screens' gate, the real-library check, the browser driver (copied from components.hybriel) |
  122. | `tools/make-test-db.hl` | the generated 13,450-song test database |
  123. ## Hybriel notes
  124. - Vendor: Hybriel set-day72 (source 00c9dbb84d7f90a2f1cd124d7e512eda6c066747), binary SHA-256 `69f9421ab58e4b7811bb1b0481947a918021b1142d96375d383427937ffc50fe`: `bin/` is the set's `bin/` and `lib/` (not in git); the complete plugin dirs core, crypto, data, fetch, fs, http, http1, math, mpackdb, proc, time, web are copies from that commit, with the set's `.so` files. Set SHA256SUMS verified.
  125. 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)`.
  126. - 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
  127. 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).
  128. - Since set-day27b (binary a9712e61, Hybriel f5ff97bf) hl:fs has `readBytes(path, offset, length)` and `stat(path)` (hybriel#233); mpackdb rebuilds string indexes with commas once on first open (hybriel#234; `safeKey` stays, it does no harm); `on Error` exits 1 at startup (`e.phase`).
  129. ## Shuffle, the overlay and counting plays (JB3)
  130. - **Albums** are artist + album name; "CD 1", "(Cd2)", "Disc 2" become a disc number, the folder does not matter. **Copies**: the same artist and title with lengths within 2 s are shown once ("2 copies"), with the highest play count.
  131. - **Shuffle**: "Shuffle my favourites" (Songs screen; the songs played at least N times, N is the Favourites field, default 5) and "Shuffle" on Songs, an album, an artist and Search. The server holds the seeded order (`lib/queue.hl`), the browser gets 40 songs at a time; every song comes once before any repeats.
  132. - **Bar and overlay**: previous / play / next in the bar; a tap on the bar opens the overlay (phone: full screen, desktop: a 24 rem panel at the right): large cover, title, artist, album, previous, back 10 s, play, forward 10 s, next, times, mute, speed, and the song that plays plus the 20 after it. Close with the button or a swipe down. Previous restarts the song after 3 s, else goes back one.
  133. - **Counting**: a play counts after half of the song or 4 minutes of real listening (seconds the audio played, a one second timer in the shell), once per play, through the face `jbPlayed` (same function as `POST /api/played/<id>`). The count lives in the jukebox database (table plays); the file is never written. Favourites use file count + jukebox count.
  134. - **Lock screen**: the browser's media session shows title, artist, album, cover and has play, pause, previous and next. The keyboard is not done: a Hybriel gap (`reports/JB3b-keyboard-gap.md`).
  135. - Gates: `node tests/jb3.mjs` (server 8712, takes about 2 minutes: it waits for a real play to count).
  136. Theme and shared tokens are unchanged git-source copies from layouts.worldapi.org `17e0f03ecd09b4427fab17276f646e0094260fd5`.
  137. Icons are unchanged copies from components.hybriel `46fefaa`; virtual-list is an unchanged copy from components.hybriel `e5b98ec` (with `centerId`, `scrollTop`, `onScroll`), and media-player is `51c3818` (skip and repeat options, repeat-one fix, detached-event and autoplay mount fixes; the end of a song only calls onPlay). `tools/sync-components.sh --check` verifies each against its recorded source commit.

Branches

Latest commits

  • 95f2cd73|||PR Use typed parent methods for screen playback and local server facesmre
  • 6422a020Sync media-player at 51c3818mre
  • b582ba1cVendor complete Hybriel day72 plugin setmre
  • 02dccbc9Merge branch 'records-1119' into mainJukebox
  • a39e1c23|||PR Records: missions 013-015, reconnect fix livemre
  • fc7db075Merge branch 'reconnect-retry' into mainJukebox
  • ac6f4c6fL2: plain declaration for a in pickedNextmre
  • 2f335086Review round 2: only an explicit queue pick drops pend (not the automatic advance); jb23 records every source changemre
  • 3eb6695fReview fixes: action generation, queue pick clears pend, Previous/Next retry the decided request, typed handlersmre
  • 7a53c0c2|||PR A tap lost to a dead socket plays after reconnect (mission 015)mre
  • 1f0cd77bMerge branch 'records-day41' into mainJukebox
  • 67be229f|||PR Records: day41 live (PR #4), mission 012, statusmre
  • 0bb027daconductor: caramboleyo is the user, not owner/creatorApollo
  • 04f5d9d5Merge branch 'day41' into mainJukebox
  • 8ed6da45|||PR README: media-player pin df9e7dfmre
  • d5d45fc3|||PR media-player df9e7df (repeat-one fix), sync pin and SOURCEmre
  • 89cdabb1Vendor Hybriel day41 (57054d9e): plugin dirs, set .somre
  • 1fecd6a7Merge branch 'records-day39' into mainJukebox
  • d6b3e068|||PR Records: day39 live (PR #2), mission 011 PR step, statusmre
  • 5fa19e89Merge branch 'day39' into mainJukebox