jukebox
All repositories: gitoria
3.3 KB
// 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// 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// 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.// The groups are found in one pass over the songs and kept in the shared cache until a scan changes something.import { songById, allSongs } from './db.hl'import { fold, SEP } from './util.hl'import { cache } from './state.hl'import { counts, jbCount } from './plays.hl'static maxGap = 2// a song's play count as the lists use it: the file's count plus the jukebox's ownstatic ownPlays = (id, filePlays) => {c = counts()[id]return filePlays + (c == null ? 0 : c)}// 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 apartstatic splitByLength = (group) => {sorted = []for (g of group) {let j = sorted.lengthsorted.push(g)while (j > 0 && sorted[j - 1].dur > g.dur) {sorted[j] = sorted[j - 1]j = j - 1}sorted[j] = g}clusters = []let cur = []let first = 0for (g of sorted) {if (cur.length > 0 && g.dur - first > maxGap) {clusters.push(cur)cur = []}if (cur.length == 0) { first = g.dur }cur.push(g)}if (cur.length > 0) { clusters.push(cur) }return clusters}// 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 copystatic dupMap = () => {if (cache.dups != null) { return cache.dups }groups = {}for (s of allSongs()) {k = fold(s.artist) + SEP + fold(s.title)let g = groups[k]if (g == null) { g = [] }g.push({ id = s.id dur = s.duration == null ? 0 : s.duration pc = ownPlays(s.id, s.playCount) })groups[k] = g}out = {}for (k in groups) {if (groups[k].length < 2) { continue }for (cluster of splitByLength(groups[k])) {if (cluster.length < 2) { continue }let first = cluster[0].idlet top = 0for (c of cluster) {if (c.id < first) { first = c.id }if (c.pc > top) { top = c.pc }}for (c of cluster) { out[c.id] = { first = first copies = cluster.length plays = top } }}}cache.dups = outreturn out}// the shown play count of a song and its number of copiesstatic showOf = (id, filePlays) => {d = dupMap()[id]if (d == null) { return { shown = true plays = ownPlays(id, filePlays) copies = 1 } }return { shown = d.first == id plays = d.plays copies = d.copies }}// the ids of every copy of a song, the song itself firststatic copiesOf = (id) => {d = dupMap()[id]if (d == null) { return [id] }out = [id]for (k in dupMap()) {e = dupMap()[k]if (e.first == d.first && k != id) { out.push(k) }}return out}// The API report of each copy, with its own play counts.static copyReport = (String id) => {s = songById(id)if (s == null) { return null }items = []for (copyId of copiesOf(s.id)) {c = songById(copyId)if (c != null) { items.push({ id = c.id path = c.path album = c.album duration = c.duration filePlays = c.playCount jukeboxPlays = jbCount(c.id) }) }}return { id = s.id items = items }}
Branches
Latest commits
- 6f0f7862|||PR Records 2026-11-24: LOG, STATUS, missions 020-023mre
- 0a2ae3daMerge remote-tracking branch 'origin/records-1123' into records-1124mre
- 9a48cfcaMerge branch 'vendor-theme' into mainJukebox
- 170d3735Merge branch 'code-order-2' into mainJukebox
- 3f1918a7|||PR Re-sync layouts theme 4099e64 with Segoe UI firstmre
- 2e7f6a43Vendor virtual-list 489afcf from components.hybrielmre
- 0ba85799Merge branch 'reset-classes' into mainJukebox
- 502eeed5jukebox#20: vendor virtual-list initial resetKey baseline fixmre
- 6d5ce287|||PR jukebox#9 Centralize songs table reads and writes in db.hlmre
- dcca21abjukebox#8 Move copies and queue logic into their topicsmre
- 3b76651cjukebox#20: update virtual-list README pin and resetKey optionsmre
- bc09e1e8|||PR jukebox#20: reset virtual lists through resetKey (partial validation)mre
- a3796dc5Records: live step waits for idlemre
- 3b158e16Records: merges of #9 and #11mre
- 27204167Merge main into records-1123mre
- 10f8a8c8Merge branch 'code-order' into mainJukebox
- 204fc828Merge branch 'day72' into mainJukebox
- 8e8d0e2b|||PR Records: PROCEDURES.md, missions 016-019, GO 2026-11-23mre
- 95f5faae|||PR jukebox#7 Drop needless let declarationsmre
- 33072bb5|||PR Record day72 gates and Chrome proofmre