gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit227c9b96227c9b96Records: jb19 livemre227c9b96/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

  • 227c9b96Records: jb19 livemre
  • 9f365376README: the bar's controls (jb19)mre
  • d18851fdjb19: pressed switches look different (whole selector), queue column cannot widen past the panel, gates follow the new bar (jb3, jb11, jb15)mre
  • 82b54c0ejb19: bar Shuffle · Previous · Play · Next · Repeat, no 10 s buttons; Shuffle switch (shuffle from the playing song, back to the list), Repeat off/all/one, both remembered in ui.json; Shuffle my favourites an ordinary button; gate tests/jb19.mjsmre
  • ba354710jb19: icons pinned to components.hybriel 46fefaa (shuffle, repeat, repeatOne, prevTrack, nextTrack)mre
  • d8e8d059jb19 step 2: media-player from components.hybriel c96709c (skip, repeat options; the end of a song only calls onPlay), sync-components.sh and SOURCE point to it, jb11 counts media loads and metadata writes (from jb21 d3a026e)mre
  • 7b048c1bRecords: jb19 done, L2 live stepmre
  • 665185aeMission 009: jb19 livemre
  • 47b2d3beRecords: P1 resumedmre
  • 8e326e94Records: resetKey blockedmre
  • f72dd7edRecords: icons comingmre
  • b264e5deRecords: P1 icons stopmre
  • 514d7cbeRecords: mission 008 startedmre
  • 61c40127Mission 008: #19 + #21mre
  • 6dcaeb46Records: resetKey planmre
  • c5dbf2ecRecords: R2 resultmre
  • 956069b8Records: R1 result, mission 007mre
  • 3c8a2e12Mission 007: #20 sort scrollmre
  • deb8cef5Records: mission 006 startedmre
  • 4f0ad42fMission 006: jukebox#20mre