gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commita4bce6d1a4bce6d1Records: ticket push subscriptionmrea4bce6d1/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

  • 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
  • 51358794jukebox JB14 (jukebox#14): the Songs screen opens sorted the way it was left (kept on the server in storage/ui.json), the sort control shows the sort the rows really have (selected options, autocomplete off); gate tests/jb14.mjsmre
  • cc68a063jukebox JB13 (jukebox#13): a tapped song plays the song that was tapped, found by its id in the list as it is now (not by its row number, which counted plays move); the queue goes on from its place there, the rows refresh on the tap; gate tests/jb13.mjs (songs by plays, favourites)mre
  • 88d3d5c1jukebox JB12b (jukebox#12): the scroll handle is big (2.75rem x 4rem, accent colour, grip) and drawn on every device, no media query; native bar hidden; gate tests/touch.mjs checks the drawn width with and without touchmre
  • b58e4495jukebox JB11 (jukebox#11): the next song starts in the ended event on the same audio element, so it also starts with the screen locked; gate tests/jb11.mjs (hidden tab, no frames)mre
  • 12a26233jukebox JB12 (jukebox#12): the long lists get a scroll handle a finger can use on touch screens (44 px tap area, slim look, drag through all rows), gate tests/touch.mjsmre
  • 0f2157a9jukebox JV1: onto Hybriel set-day27b: readBytes and stat for /stream and the mp3 tags (no dd, head or tail), startup fault exits 1, fs and mpackdb plugins re-vendored; songs with a comma in the title are indexed againmre
  • 63d0f2eejukebox ST1 (jukebox#2): styles of main, songs and search moved into their own Style blocks (styles.hl 81 to 8 rules, pixel-identical at 390 and 1280); artist and album wait for an hl:web fixmre
  • c5b8fde9jukebox JB3b: keyboard gap repro as a reportmre