gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchrecords-1124bfa86b08c|||PR Records 2026-11-24: LOG, STATUS, missions 020-023mrerecords-1124b/lib/queue.hl

4.0 KB

  1. // lib/queue.hl — THE SHUFFLED QUEUE, kept on the server. A shuffle never sends its songs to the browser: the browser holds a small context `{ kind: 'shuffle', seed, spec }`
  2. // and asks for the next items by index. The server makes the order from the seed (the same seed gives the same order, so a restarted server or a second tab finds the same queue),
  3. // every song of the pool comes once before any comes again (a new round gets a new order from seed + round), and the last orders are kept in memory.
  4. // spec = { kind: 'favourites', minPlays } (every song played at least N times, file count + jukebox count)
  5. // | { kind: 'list', ctx } (the list a screen shows: songs with a minimum, an album, an artist's songs, a search)
  6. import { floor } from 'hl:math'
  7. import { orderList, songsPage, searchAll } from './browse.hl'
  8. import { cache } from './state.hl'
  9. static keepOrders = 6
  10. static roundStep = 7919
  11. // the ids of a pool, in list order
  12. static poolFor = (spec) => {
  13. out = []
  14. if (spec.kind == 'favourites') {
  15. for (r of orderList('title')) {
  16. if (r.pc >= spec.minPlays) { out.push(r.id) }
  17. }
  18. return out
  19. }
  20. ctx = spec.ctx
  21. if (ctx.kind == 'songs') {
  22. min = ctx.minPlays == null ? 0 : ctx.minPlays
  23. for (r of orderList('title')) {
  24. if (r.pc >= min) { out.push(r.id) }
  25. }
  26. return out
  27. }
  28. let off = 0
  29. let total = 1
  30. while (off < total) {
  31. let page = null
  32. if (ctx.kind == 'album') { page = songsPage('artist', off, 200, null, null, ctx.album) }
  33. else if (ctx.kind == 'artist-songs') { page = songsPage('artist', off, 200, null, ctx.artist, null) }
  34. else if (ctx.kind == 'search') { page = searchAll(ctx.q, off, 200).songs }
  35. else { return out }
  36. total = page.total
  37. if (page.items.length == 0) { break }
  38. for (s of page.items) { out.push(s.id) }
  39. off = off + page.items.length
  40. }
  41. return out
  42. }
  43. // the pool in a random order made from the seed (Fisher-Yates with a Park-Miller generator: numbers stay far below 2^53).
  44. // `first` (a song id) goes to the front of the order: a shuffle turned on while that song plays keeps it playing (it is added when the pool lacks it).
  45. static shuffled = (spec, seed, first) => {
  46. ids = poolFor(spec)
  47. let x = seed % 2147483646 + 1
  48. let i = ids.length - 1
  49. while (i > 0) {
  50. x = x * 48271 % 2147483647
  51. j = floor(x / 2147483647 * (i + 1))
  52. t = ids[i]
  53. ids[i] = ids[j]
  54. ids[j] = t
  55. i = i - 1
  56. }
  57. if (first == null || first == '') { return ids }
  58. out = [first]
  59. for (id of ids) {
  60. if (id != first) { out.push(id) }
  61. }
  62. return out
  63. }
  64. static orderKey = (spec, seed, first) => { return seed + ':' + JSON.stringify(spec) + ':' + (first == null ? '' : first) }
  65. // the order of one round, from the memory or made now
  66. static roundOrder = (spec, seed, first) => {
  67. k = orderKey(spec, seed, first)
  68. q = cache.queues[k]
  69. if (q != null) { return q }
  70. ids = shuffled(spec, seed, first)
  71. qs = cache.queues
  72. qs[k] = ids
  73. let names = cache.queueNames
  74. names.push(k)
  75. if (names.length > keepOrders) {
  76. old = names[0]
  77. names = names.slice(1)
  78. qs[old] = null
  79. }
  80. cache.queues = qs
  81. cache.queueNames = names
  82. return ids
  83. }
  84. // the song id at a place of the endless queue (round after round), or null when the pool is empty
  85. static idAt = (ctx, index) => {
  86. firstRound = roundOrder(ctx.spec, ctx.seed, ctx.first)
  87. n = firstRound.length
  88. if (n == 0) { return null }
  89. round = floor(index / n)
  90. ids = round == 0 ? firstRound : roundOrder(ctx.spec, ctx.seed + round * roundStep, null)
  91. return ids[index - round * n]
  92. }
  93. // how many songs the pool holds
  94. static poolSize = (ctx) => { return roundOrder(ctx.spec, ctx.seed, ctx.first).length }
  95. // The requested slice of a favourites shuffle, using the same context as the player.
  96. static queueIds = (Number seed, Number minPlays, Number offset, Number count) => {
  97. ctx = { kind = 'shuffle' seed = seed spec = { kind = 'favourites' minPlays = minPlays == null ? 5 : minPlays } }
  98. start = offset == null ? 0 : offset
  99. limit = count == null ? 50 : count
  100. ids = []
  101. let i = start
  102. while (i < start + limit) {
  103. ids.push(idAt(ctx, i))
  104. i = i + 1
  105. }
  106. return { pool = poolSize(ctx) start = start ids = ids }
  107. }

Branches

Latest commits

  • fa86b08c|||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