gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit9485dee79485dee7Records: mission 002 brief (live step)mre9485dee7/lib/queue.hl

3.1 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. static shuffled = (spec, seed) => {
  45. ids = poolFor(spec)
  46. let x = seed % 2147483646 + 1
  47. let i = ids.length - 1
  48. while (i > 0) {
  49. x = x * 48271 % 2147483647
  50. j = floor(x / 2147483647 * (i + 1))
  51. t = ids[i]
  52. ids[i] = ids[j]
  53. ids[j] = t
  54. i = i - 1
  55. }
  56. return ids
  57. }
  58. static orderKey = (spec, seed) => { return seed + ':' + JSON.stringify(spec) }
  59. // the order of one round, from the memory or made now
  60. static roundOrder = (spec, seed) => {
  61. k = orderKey(spec, seed)
  62. q = cache.queues[k]
  63. if (q != null) { return q }
  64. ids = shuffled(spec, seed)
  65. qs = cache.queues
  66. qs[k] = ids
  67. let names = cache.queueNames
  68. names.push(k)
  69. if (names.length > keepOrders) {
  70. old = names[0]
  71. names = names.slice(1)
  72. qs[old] = null
  73. }
  74. cache.queues = qs
  75. cache.queueNames = names
  76. return ids
  77. }
  78. // the song id at a place of the endless queue (round after round), or null when the pool is empty
  79. static idAt = (ctx, index) => {
  80. first = roundOrder(ctx.spec, ctx.seed)
  81. n = first.length
  82. if (n == 0) { return null }
  83. round = floor(index / n)
  84. ids = round == 0 ? first : roundOrder(ctx.spec, ctx.seed + round * roundStep)
  85. return ids[index - round * n]
  86. }
  87. // how many songs the pool holds
  88. static poolSize = (ctx) => { return roundOrder(ctx.spec, ctx.seed).length }

Branches

Latest commits

  • 9485dee7Records: mission 002 brief (live step)mre
  • 9ba68fe7Records: S1b reviewedmre
  • 193dfff1Records: S1 result, S1b startedmre
  • 028ed776Records: mission 001 continuation S1bmre
  • 1838d7e8Records: S1 startedmre
  • a43fca3fRecords: mission 001 decided pointsmre
  • 41773828Records: statusmre
  • a82c0805Records: mission 001 brief (Style form)mre
  • a4bce6d1Records: ticket push subscriptionmre
  • 14abde3bjukebox: records: Atlas's records moved to worldapi-conductor/archivemre
  • 0433e9a8jukebox: records: gitoria remote added and pushedmre
  • 1722d707jukebox: records: repo creation handed to Gitoriamre
  • b42e7dfbjukebox: conductor records (CONDUCTOR.md, STATUS.md), old STATUS archivedmre
  • 1b0d0bc1jukebox: a startup error prints its message before exit 1 (hybriel#247)mre
  • b19c478bjukebox JB18 (jukebox#17): onto Hybriel set-day31 (b287cd82, contains the SIGPIPE fix 6d2ca454): plugins/core and plugins/web re-vendored, bin/ from the set; a client that closes /stream while it is written no longer kills the server (90 aborted streams survived, the old bin died with exit 141 on the first)mre
  • 73867a2ejukebox#2: Style blocks in the language's nested form (rules named after the View's own elements, pseudo-classes nested, merged duplicates); the same CSS rules, the pages look the samemre
  • 06c548d6jukebox JB15: report, log linemre
  • 980859cbjukebox JB15 (jukebox#15): the now-playing overlay on a short screen (tablet or phone on its side) gave the queue 12 to 100 px because the big cover took the room; the cover now shrinks with the screen height and goes away under 41rem, the queue keeps 3+ rows; gate tests/jb15.mjs at 8 sizesmre
  • 9efc40ffjukebox#12 #14: Grok session 1793246574527-181160mre
  • 7d58b36ejukebox JB13 (jukebox#13): Next and Previous ask for the songs after / before the current song's id in the current order, not for place + 1; gate tests/jb13.mjs checks Next with the queue down to one song after the order changedmre