gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit7d58b36e7d58b36ejukebox JB13 (jukebox#13): Next and Previous ask for the songs after / before the current song's id in the current order, not for place + 1; gate tests/jb13.mjs checks Next with the queue down to one song after the order changedmre7d58b36e/lib/catalog.hl

4.7 KB

  1. // lib/catalog.hl — THE DERIVED TABLES (albums, artists): built from the songs table after a scan that changed anything.
  2. // An album is one albumKey (album artist + album name without the CD marker; not the folder); a song with copies counts once; an artist is one artistKey. Both get a stable primary
  3. // key (a digest of the key), so an id from the API stays valid across rebuilds. All writes to those two tables are here.
  4. import { textDigest } from 'hl:fs'
  5. import { songsTable, albumsTable, artistsTable, countOfRows } from './db.hl'
  6. import { fold, safeKey, SEP } from './util.hl'
  7. import { albumKeyOf, sortKeys } from './albums.hl'
  8. import { showOf } from './dups.hl'
  9. import { dropOrderCache } from './state.hl'
  10. static variousName = 'Various Artists'
  11. // an album name that says nothing ("Unknown album", "Unbekannt", "Unbekannter Titel"): songs with it are never taken for one compilation
  12. static unknownAlbum = (name) => {
  13. f = fold(name)
  14. return f == '' || f.startsWith('unknown') || f.startsWith('unbekannt')
  15. }
  16. // A COMPILATION has no album artist tag, so each of its songs would be an album of its own. When three or more different artists share an album name in one folder
  17. // (and none of them has a real album artist), those songs get the album artist "Various Artists" and one album key. Their artist key stays their own, so an artist's page
  18. // still lists the songs. Idempotent; only songs whose keys change are written. Answers how many were changed.
  19. static fixCompilations = () => {
  20. groups = {}
  21. for (s of songsTable.iterate(null, null)) {
  22. if (unknownAlbum(s.album)) { continue }
  23. if (s.albumArtist != s.artist && s.albumArtist != variousName) { continue }
  24. k = fold(s.album) + SEP + s.folder
  25. let g = groups[k]
  26. if (g == null) { g = {} }
  27. g[fold(s.artist)] = true
  28. groups[k] = g
  29. }
  30. let changed = 0
  31. rows = []
  32. for (s of songsTable.iterate(null, null)) { rows.push(s) }
  33. for (s of rows) {
  34. if (unknownAlbum(s.album)) { continue }
  35. if (s.albumArtist != s.artist && s.albumArtist != variousName) { continue }
  36. g = groups[fold(s.album) + SEP + s.folder]
  37. let many = 0
  38. for (a in g) { many = many + 1 }
  39. want = many >= 3 ? variousName : s.artist
  40. if (want == s.albumArtist) { continue }
  41. keys = sortKeys(want, s.album, s.disc, s.track == null ? 0 : s.track, s.title)
  42. rec = {}
  43. for (f in s) {
  44. if (f != 'id') { rec[f] = s[f] }
  45. }
  46. rec.albumArtist = want
  47. rec.albumKey = albumKeyOf(want, s.album)
  48. rec.sortArtist = keys.sortArtist
  49. rec.sortAlbum = keys.sortAlbum
  50. songsTable.update(s.id, rec)
  51. changed = changed + 1
  52. }
  53. return changed
  54. }
  55. static idOf = (key) => { return textDigest(key).slice(0, 16) }
  56. // Rebuild both tables from scratch (one pass over the songs, then the old rows go and the new come). Answers
  57. // { albums, artists } counts.
  58. static rebuildCatalog = () => {
  59. fixCompilations()
  60. albums = {}
  61. artists = {}
  62. dropOrderCache()
  63. for (s of songsTable.iterate(null, null)) {
  64. w = showOf(s.id, s.playCount)
  65. if (!w.shown) { continue }
  66. let a = albums[s.albumKey]
  67. if (a == null) {
  68. a = { id = idOf(s.albumKey) key = s.albumKey name = s.album albumArtist = s.albumArtist artistKey = s.albumArtist == variousName ? safeKey(fold(variousName)) : s.artistKey folder = s.folder
  69. year = s.year songCount = 0 duration = 0 discs = 1 playCount = 0 coverId = null sortName = safeKey(fold(s.album) + '|' + s.artistKey) }
  70. }
  71. a.songCount = a.songCount + 1
  72. if (s.disc != null && s.disc > a.discs) { a.discs = s.disc }
  73. a.duration = a.duration + (s.duration == null ? 0 : s.duration)
  74. a.playCount = a.playCount + w.plays
  75. if (a.coverId == null && s.coverId != null) { a.coverId = s.coverId }
  76. if (a.year == null && s.year != null) { a.year = s.year }
  77. albums[s.albumKey] = a
  78. let r = artists[s.artistKey]
  79. if (r == null) {
  80. r = { id = idOf('artist:' + s.artistKey) key = s.artistKey name = s.albumArtist == variousName ? s.artist : s.albumArtist songCount = 0 albumCount = 0 playCount = 0 coverId = null
  81. sortName = s.artistKey seenAlbums = {} }
  82. }
  83. r.songCount = r.songCount + 1
  84. r.playCount = r.playCount + w.plays
  85. if (r.seenAlbums[s.albumKey] == null) {
  86. r.seenAlbums[s.albumKey] = true
  87. r.albumCount = r.albumCount + 1
  88. }
  89. if (r.coverId == null && s.coverId != null) { r.coverId = s.coverId }
  90. artists[s.artistKey] = r
  91. }
  92. // the old rows out
  93. for (old of albumsTable.find(null, null)) { albumsTable.delete(old.id) }
  94. for (old of artistsTable.find(null, null)) { artistsTable.delete(old.id) }
  95. let na = 0
  96. for (k in albums) {
  97. albumsTable.put(albums[k])
  98. na = na + 1
  99. }
  100. let nr = 0
  101. for (k in artists) {
  102. r = artists[k]
  103. artistsTable.put({ id = r.id key = r.key name = r.name songCount = r.songCount albumCount = r.albumCount playCount = r.playCount coverId = r.coverId sortName = r.sortName })
  104. nr = nr + 1
  105. }
  106. return { albums = na artists = nr }
  107. }

Branches

Latest commits

  • 7d58b36ejukebox JB13 (jukebox#13): Next and Previous ask for the songs after / before the current song's id in the current order, not for place + 1; gate tests/jb13.mjs checks Next with the queue down to one song after the order changedmre
  • 51358794jukebox JB14 (jukebox#14): the Songs screen opens sorted the way it was left (kept on the server in storage/ui.json), the sort control shows the sort the rows really have (selected options, autocomplete off); gate tests/jb14.mjsmre
  • cc68a063jukebox JB13 (jukebox#13): a tapped song plays the song that was tapped, found by its id in the list as it is now (not by its row number, which counted plays move); the queue goes on from its place there, the rows refresh on the tap; gate tests/jb13.mjs (songs by plays, favourites)mre
  • 88d3d5c1jukebox JB12b (jukebox#12): the scroll handle is big (2.75rem x 4rem, accent colour, grip) and drawn on every device, no media query; native bar hidden; gate tests/touch.mjs checks the drawn width with and without touchmre
  • b58e4495jukebox JB11 (jukebox#11): the next song starts in the ended event on the same audio element, so it also starts with the screen locked; gate tests/jb11.mjs (hidden tab, no frames)mre
  • 12a26233jukebox JB12 (jukebox#12): the long lists get a scroll handle a finger can use on touch screens (44 px tap area, slim look, drag through all rows), gate tests/touch.mjsmre
  • 0f2157a9jukebox JV1: onto Hybriel set-day27b: readBytes and stat for /stream and the mp3 tags (no dd, head or tail), startup fault exits 1, fs and mpackdb plugins re-vendored; songs with a comma in the title are indexed againmre
  • 63d0f2eejukebox ST1 (jukebox#2): styles of main, songs and search moved into their own Style blocks (styles.hl 81 to 8 rules, pixel-identical at 390 and 1280); artist and album wait for an hl:web fixmre
  • c5b8fde9jukebox JB3b: keyboard gap repro as a reportmre
  • 9daebb4bjukebox JB3b: shuffle, now-playing overlay, next and previous, play counting, media session, gates; keyboard left open (Hybriel gap)mre
  • 59c1373ejukebox JB3 (work in progress): albums without folder and with disc markers folded, compilations, duplicate songs, own play counter, server-side shuffle queuemre
  • 2a4cfc55jukebox: library screens (songs, artists, albums, artist, album, search, favourites), compact player bar, /stream with Range, thumbnails, virtual list vendored, ui gate and real-library checkmre
  • 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