jukebox
All repositories: gitoria
3.5 KB
// 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 }`// 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),// 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.// spec = { kind: 'favourites', minPlays } (every song played at least N times, file count + jukebox count)// | { kind: 'list', ctx } (the list a screen shows: songs with a minimum, an album, an artist's songs, a search)import { floor } from 'hl:math'import { orderList, songsPage, searchAll } from './browse.hl'import { cache } from './state.hl'static keepOrders = 6static roundStep = 7919// the ids of a pool, in list orderstatic poolFor = (spec) => {out = []if (spec.kind == 'favourites') {for (r of orderList('title')) {if (r.pc >= spec.minPlays) { out.push(r.id) }}return out}ctx = spec.ctxif (ctx.kind == 'songs') {min = ctx.minPlays == null ? 0 : ctx.minPlaysfor (r of orderList('title')) {if (r.pc >= min) { out.push(r.id) }}return out}let off = 0let total = 1while (off < total) {let page = nullif (ctx.kind == 'album') { page = songsPage('artist', off, 200, null, null, ctx.album) }else if (ctx.kind == 'artist-songs') { page = songsPage('artist', off, 200, null, ctx.artist, null) }else if (ctx.kind == 'search') { page = searchAll(ctx.q, off, 200).songs }else { return out }total = page.totalif (page.items.length == 0) { break }for (s of page.items) { out.push(s.id) }off = off + page.items.length}return out}// the pool in a random order made from the seed (Fisher-Yates with a Park-Miller generator: numbers stay far below 2^53).// `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).static shuffled = (spec, seed, first) => {ids = poolFor(spec)let x = seed % 2147483646 + 1let i = ids.length - 1while (i > 0) {x = x * 48271 % 2147483647j = floor(x / 2147483647 * (i + 1))t = ids[i]ids[i] = ids[j]ids[j] = ti = i - 1}if (first == null || first == '') { return ids }out = [first]for (id of ids) {if (id != first) { out.push(id) }}return out}static orderKey = (spec, seed, first) => { return seed + ':' + JSON.stringify(spec) + ':' + (first == null ? '' : first) }// the order of one round, from the memory or made nowstatic roundOrder = (spec, seed, first) => {k = orderKey(spec, seed, first)q = cache.queues[k]if (q != null) { return q }ids = shuffled(spec, seed, first)qs = cache.queuesqs[k] = idslet names = cache.queueNamesnames.push(k)if (names.length > keepOrders) {old = names[0]names = names.slice(1)qs[old] = null}cache.queues = qscache.queueNames = namesreturn ids}// the song id at a place of the endless queue (round after round), or null when the pool is emptystatic idAt = (ctx, index) => {firstRound = roundOrder(ctx.spec, ctx.seed, ctx.first)n = firstRound.lengthif (n == 0) { return null }round = floor(index / n)ids = round == 0 ? firstRound : roundOrder(ctx.spec, ctx.seed + round * roundStep, null)return ids[index - round * n]}// how many songs the pool holdsstatic poolSize = (ctx) => { return roundOrder(ctx.spec, ctx.seed, ctx.first).length }
Branches
Latest commits
- 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
- 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