gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit8ed6da458ed6da45|||PR README: media-player pin df9e7dfmre8ed6da45/lib/queue.hl

3.5 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 }

Branches

Latest commits

  • 8ed6da45|||PR README: media-player pin df9e7dfmre
  • d5d45fc3|||PR media-player df9e7df (repeat-one fix), sync pin and SOURCEmre
  • 89cdabb1Vendor Hybriel day41 (57054d9e): plugin dirs, set .somre
  • 1fecd6a7Merge branch 'records-day39' into mainJukebox
  • d6b3e068|||PR Records: day39 live (PR #2), mission 011 PR step, statusmre
  • 5fa19e89Merge branch 'day39' into mainJukebox
  • cfe95440|||PR day39 re-vendor: Hybriel 51e921d7 (bin 6b481aa7), ids kebab-case since hybriel#256: quoted state rule and tests use jb-shuffle / jb-repeatmre
  • f3614c9bRe-vendor to Hybriel day39 (51e921d7, bin 6b481aa7)mre
  • 9f5c6532Merge branch 'status-22-pr' into mainJukebox
  • 5838d6c7|||PR STATUS: #22 pending decision, PR-only rulemre
  • 29d2029cRecords: PR-only rule (Kunibert)mre
  • adacb0abRecords: neutral ticket rule, #22 decision recordedmre
  • b2d6f121Records: #308 hold liftedmre
  • 7ab67533Records: day39 on hold (hybriel#308)mre
  • 22f21739Records: note rebase before mergemre
  • f7368ea3Records: mission 011 day39, waits for lisbethmre
  • 9515d2cdRecords: jb20 livemre
  • 019f6601ui gate: comment names the open bug components#27 (workaround, not fixed)mre
  • 277a2722README: provenance linemre
  • 4169ca6aREADME: sync pinmre