gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchmain10f8a8c8Merge branch 'code-order' into mainJukeboxmain/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

  • 10f8a8c8Merge branch 'code-order' into mainJukebox
  • 204fc828Merge branch 'day72' into mainJukebox
  • 95f5faae|||PR jukebox#7 Drop needless let declarationsmre
  • 33072bb5|||PR Record day72 gates and Chrome proofmre
  • 95f2cd73|||PR Use typed parent methods for screen playback and local server facesmre
  • 6422a020Sync media-player at 51c3818mre
  • b582ba1cVendor complete Hybriel day72 plugin setmre
  • 02dccbc9Merge branch 'records-1119' into mainJukebox
  • a39e1c23|||PR Records: missions 013-015, reconnect fix livemre
  • fc7db075Merge branch 'reconnect-retry' into mainJukebox
  • ac6f4c6fL2: plain declaration for a in pickedNextmre
  • 2f335086Review round 2: only an explicit queue pick drops pend (not the automatic advance); jb23 records every source changemre
  • 3eb6695fReview fixes: action generation, queue pick clears pend, Previous/Next retry the decided request, typed handlersmre
  • 7a53c0c2|||PR A tap lost to a dead socket plays after reconnect (mission 015)mre
  • 1f0cd77bMerge branch 'records-day41' into mainJukebox
  • 67be229f|||PR Records: day41 live (PR #4), mission 012, statusmre
  • 0bb027daconductor: caramboleyo is the user, not owner/creatorApollo
  • 04f5d9d5Merge branch 'day41' into mainJukebox
  • 8ed6da45|||PR README: media-player pin df9e7dfmre
  • d5d45fc3|||PR media-player df9e7df (repeat-one fix), sync pin and SOURCEmre