gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit1838d7e81838d7e8Records: S1 startedmre1838d7e8/lib/dups.hl

2.9 KB

  1. // lib/dups.hl — THE SAME SONG IN TWO PLACES. A library holds copies (the same album in two folders, a single and the album). Copies are songs with the same artist and the
  2. // same title whose lengths differ by at most 2 seconds. Lists show a group once: its first copy (the lowest id) is the one that is shown, the others are "copies". For display and for
  3. // Favourites the group takes the highest play count of its copies (file count + the jukebox's own count of each copy); every file keeps its own count.
  4. // The groups are found in one pass over the songs and kept in the shared cache until a scan changes something.
  5. import { songsTable } from './db.hl'
  6. import { fold, SEP } from './util.hl'
  7. import { cache } from './state.hl'
  8. import { counts } from './plays.hl'
  9. static maxGap = 2
  10. // a song's play count as the lists use it: the file's count plus the jukebox's own
  11. static ownPlays = (id, filePlays) => {
  12. c = counts()[id]
  13. return filePlays + (c == null ? 0 : c)
  14. }
  15. // copies of one group of songs with the same artist and title: lists of { id, dur, pc }, split where the lengths are more than 2 s apart
  16. static splitByLength = (group) => {
  17. sorted = []
  18. for (g of group) {
  19. let j = sorted.length
  20. sorted.push(g)
  21. while (j > 0 && sorted[j - 1].dur > g.dur) {
  22. sorted[j] = sorted[j - 1]
  23. j = j - 1
  24. }
  25. sorted[j] = g
  26. }
  27. clusters = []
  28. let cur = []
  29. let first = 0
  30. for (g of sorted) {
  31. if (cur.length > 0 && g.dur - first > maxGap) {
  32. clusters.push(cur)
  33. cur = []
  34. }
  35. if (cur.length == 0) { first = g.dur }
  36. cur.push(g)
  37. }
  38. if (cur.length > 0) { clusters.push(cur) }
  39. return clusters
  40. }
  41. // the map song id → { first: id of the shown copy, copies: how many, plays: the highest play count of the group } for every song that has a copy
  42. static dupMap = () => {
  43. if (cache.dups != null) { return cache.dups }
  44. groups = {}
  45. for (s of songsTable.iterate(null, null)) {
  46. k = fold(s.artist) + SEP + fold(s.title)
  47. let g = groups[k]
  48. if (g == null) { g = [] }
  49. g.push({ id = s.id dur = s.duration == null ? 0 : s.duration pc = ownPlays(s.id, s.playCount) })
  50. groups[k] = g
  51. }
  52. out = {}
  53. for (k in groups) {
  54. if (groups[k].length < 2) { continue }
  55. for (cluster of splitByLength(groups[k])) {
  56. if (cluster.length < 2) { continue }
  57. let first = cluster[0].id
  58. let top = 0
  59. for (c of cluster) {
  60. if (c.id < first) { first = c.id }
  61. if (c.pc > top) { top = c.pc }
  62. }
  63. for (c of cluster) { out[c.id] = { first = first copies = cluster.length plays = top } }
  64. }
  65. }
  66. cache.dups = out
  67. return out
  68. }
  69. // the shown play count of a song and its number of copies
  70. static showOf = (id, filePlays) => {
  71. d = dupMap()[id]
  72. if (d == null) { return { shown = true plays = ownPlays(id, filePlays) copies = 1 } }
  73. return { shown = d.first == id plays = d.plays copies = d.copies }
  74. }
  75. // the ids of every copy of a song, the song itself first
  76. static copiesOf = (id) => {
  77. d = dupMap()[id]
  78. if (d == null) { return [id] }
  79. out = [id]
  80. for (k in dupMap()) {
  81. e = dupMap()[k]
  82. if (e.first == d.first && k != id) { out.push(k) }
  83. }
  84. return out
  85. }

Branches

Latest commits

  • 1838d7e8Records: S1 startedmre
  • a43fca3fRecords: mission 001 decided pointsmre
  • 41773828Records: statusmre
  • a82c0805Records: mission 001 brief (Style form)mre
  • a4bce6d1Records: ticket push subscriptionmre
  • 14abde3bjukebox: records: Atlas's records moved to worldapi-conductor/archivemre
  • 0433e9a8jukebox: records: gitoria remote added and pushedmre
  • 1722d707jukebox: records: repo creation handed to Gitoriamre
  • b42e7dfbjukebox: conductor records (CONDUCTOR.md, STATUS.md), old STATUS archivedmre
  • 1b0d0bc1jukebox: a startup error prints its message before exit 1 (hybriel#247)mre
  • b19c478bjukebox JB18 (jukebox#17): onto Hybriel set-day31 (b287cd82, contains the SIGPIPE fix 6d2ca454): plugins/core and plugins/web re-vendored, bin/ from the set; a client that closes /stream while it is written no longer kills the server (90 aborted streams survived, the old bin died with exit 141 on the first)mre
  • 73867a2ejukebox#2: Style blocks in the language's nested form (rules named after the View's own elements, pseudo-classes nested, merged duplicates); the same CSS rules, the pages look the samemre
  • 06c548d6jukebox JB15: report, log linemre
  • 980859cbjukebox JB15 (jukebox#15): the now-playing overlay on a short screen (tablet or phone on its side) gave the queue 12 to 100 px because the big cover took the room; the cover now shrinks with the screen height and goes away under 41rem, the queue keeps 3+ rows; gate tests/jb15.mjs at 8 sizesmre
  • 9efc40ffjukebox#12 #14: Grok session 1793246574527-181160mre
  • 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