jukebox
All repositories: gitoria
10.8 KB
// components/main.hl — THE SHELL: the layouts header with the main menu (the phone's sidebar behind the toggle), the slot every screen renders// into, and the PLAYER BAR at the bottom. The shell stays when a screen is replaced by a click on a link (hl:web navigates in the page), so the song keeps playing// while you browse. A screen starts a song with `emit jbPlay(ctx, index)`: the context says which list the song was tapped in (`{ kind, sort, minPlays, ... }`,// lib/rows.hl), the index which row. A screen starts a shuffle with `emit jbShuffle(spec)` (lib/queue.hl: favourites or the list the screen shows); the server then holds the// shuffled order and the browser asks for 40 songs at a time. The queue of the media player always starts at the song that plays: the bar shows it, the overlay lists the 20 after it.// THE NOW-PLAYING OVERLAY is the same bar and the same media player, drawn bigger by CSS: a tap on the bar opens it (full screen on the phone, a side panel from 60rem), the// close button, the Escape key or a swipe down closes it. THE COUNT: a song counts as played after half of its length or 4 minutes of listening (whichever comes first); the shell counts// the seconds the audio really plays (a one second timer), asks the server once (`jbPlayed`) and the jukebox database adds 1 to its own counter. The files are never written.// KEYBOARD: NOT HERE. Every key handler of the shell (document listener, body, the app div) made the audio element load its source again on each key press (the song// restarted): Hybriel gap, repro in .scratch/KEYBOARD-GAP.md. The media player's own keys (Space, arrows on its audio element) stay as they are.// LOCK SCREEN: the browser's media session shows title, artist, album and cover and gives play, pause, previous and next to the lock screen and the headphone keys.import AppHeader from '../theme/shell/app-header.hl'import MainMenu from '../theme/shell/main-menu.hl'import Sidebar from '../theme/shell/app-sidebar.hl'import MediaPlayer from './media-player/media-player.hl'import Icon from './icons/icons.hl'import { now } from 'hl:time'import { rowsFor, queueFor } from '../lib/rows.hl'import { countPlay } from '../lib/plays.hl'slot = nullmenu = [ { label = 'Songs' href = '/' current = false } { label = 'Artists' href = '/artists' } { label = 'Albums' href = '/albums' } { label = 'Search' href = '/search' } ]// the player: what plays now, and the songs that follow in the list it came from (the first item of the queue is the song that plays)String nowSrc = ''String nowTitle = ''String nowArtist = ''String nowAlbum = ''String nowCover = ''String nowArt = ''String nowId = ''String autoPlay = 'false'Hybrid nowQueue = []Hybrid nowCtx = {}Number nowIdx = 0String overlay = 'false'String barClass = 'jb-app'String coverNow = overlay == 'true' ? nowArt : nowCoverNumber heard = 0String countedId = ''Number swipeY = -1String ticking = 'false'View {body {AppHeader { brand = "Jukebox"MainMenu { items = menu }}Sidebar { desktop = "false" MainMenu { items = menu } }div { class = barClassslotjbTick { on animationiteration(e) { emit jbMount(e) } }div { class = "jb-bar" on click(e) { emit jbBarClick(e) } on pointerdown(e) { emit jbDown(e) } on pointerup(e) { emit jbUp(e) } on pointercancel(e) { emit jbCancel(e) }button { type = "button" class = "jb-close" title = "Close" 'aria-label' = "Close" Icon { name = "close" size = "1.5em" } on click(e) { emit jbClose(e) } }MediaPlayer { queueLabel = "Next up" src = nowSrc kind = "audio" title = nowTitle artist = nowArtist cover = coverNow queue = nowQueue onPlay = pickedNext autoPlay = autoPlay }p { class = "jb-album" nowAlbum }button { type = "button" class = "jb-prev" title = "Previous song" 'aria-label' = "Previous song" Icon { name = "prevTrack" size = "1.5em" } on click(e) { emit jbPrevClick(e) } }button { type = "button" class = "jb-next" title = "Next song" 'aria-label' = "Next song" Icon { name = "nextTrack" size = "1.5em" } on click(e) { emit jbNextClick(e) } }}}}}// ---- the server's part -------------------------------------------------------------------------------------------------------// rows of a list for a screen: `{ total, offset, rows }`on server jbRows(ctx, offset, limit) {return rowsFor(ctx, offset, limit)}// the songs of a list from `index` on, as queue itemson server jbQueue(ctx, index, count) {return queueFor(ctx, index, count)}// a new shuffle of a pool (`{ kind: 'favourites', minPlays }` or `{ kind: 'list', ctx }`): its context, with a seed from the clockon server jbShuffleCtx(spec) {return { kind = 'shuffle' seed = now() % 2147483000 spec = spec }}// the song was listened to long enough: the jukebox database adds 1 to its own counter (the same call as POST /api/played/<id>)on server jbPlayed(id) {return countPlay(id, now())}// ---- playing -----------------------------------------------------------------------------------------------------------------setBar = () => {barClass = 'jb-app' + (nowId != '' ? ' playing' : '') + (overlay == 'true' ? ' open' : '')}// the audio element of the media playeraudioEl = () => { return document.querySelector('media-player audio') }// show what plays (a queue item) and start itshowNow = (item) => {nowSrc = item.srcnowTitle = item.titlenowArtist = item.artistnowAlbum = item.albumnowCover = item.covernowArt = item.artnowId = item.idnowIdx = item.idxheard = 0countedId = ''autoPlay = 'true'setBar()let ms = window.navigator.mediaSessionif (ms != null) {ms.metadata = window.Reflect.construct(window.MediaMetadata, [ { title = item.title artist = item.artist album = item.album artwork = [ { src = item.art } ] } ])}}// a queue that starts at the song that playsfromPlace = (at) => {out = []let i = atwhile (i < nowQueue.length) {out.push(nowQueue[i])i = i + 1}return out}// the button or row that started a song gives the focus back (the player's own buttons keep it), so Space keeps meaning play / pauseon jbUnfocus() {f = document.activeElementif (f != null && f.blur != null && f.closest('media-player') == null) { f.blur() }return null}// a song was tapped in a screen: its list becomes the queueon jbPlay(ctx, index) {emit jbUnfocus()items = emit server jbQueue(ctx, index, 40)if (items == null || items.length == 0) { return null }nowCtx = ctxnowQueue = itemsshowNow(items[0])return null}// a shuffle was asked for: a new random order of the pool, played from its starton jbShuffle(spec) {emit jbUnfocus()ctx = emit server jbShuffleCtx(spec)emit jbPlay(ctx, 0)return null}// the place of a song in the queue, -1 when it is not thereplaceOf = (src) => {let at = -1let i = 0while (i < nowQueue.length) {if (nowQueue[i].src == src) { at = i }i = i + 1}return at}// the player picked a queue item (a tap on it, or the end of the song before): it becomes the first of the queue; the queue is refilled when fewer than 25 songs followpickedNext = (src) => {at = placeOf(src)if (at < 0) { return }item = nowQueue[at]nowQueue = fromPlace(at)showNow(item)if (nowQueue.length < 25) {more = emit server jbQueue(nowCtx, item.idx, 40)if (more != null && more.length > 0) { nowQueue = more }}}// next song: the second of the queue, or a request when the list has run out of items hereon jbNext() {if (nowId == '') { return null }if (nowQueue.length > 1) { pickedNext(nowQueue[1].src) return null }more = emit server jbQueue(nowCtx, nowIdx + 1, 40)if (more != null && more.length > 0) {nowQueue = moreshowNow(more[0])}return null}// previous song: after 3 seconds into a song the first press starts it again, then the song before it in the liston jbPrev() {if (nowId == '') { return null }let a = audioEl()if (a != null && a.currentTime > 3) { a.currentTime = 0 return null }if (nowIdx < 1) { if (a != null) { a.currentTime = 0 } return null }items = emit server jbQueue(nowCtx, nowIdx - 1, 40)if (items != null && items.length > 0) {nowQueue = itemsshowNow(items[0])}return null}// the buttons give the focus back, so Space keeps meaning play / pauseon jbNextClick(e) { e.target.closest('button').blur() emit jbNext() return null }on jbPrevClick(e) { e.target.closest('button').blur() emit jbPrev() return null }toggleNow = () => {let a = audioEl()if (a == null || nowId == '') { return }if (a.paused) { a.play() } else { a.pause() }}jumpBy = (seconds) => {let a = audioEl()if (a == null || nowId == '') { return }let t = a.currentTime + secondsif (t < 0) { t = 0 }if (a.duration > 0 && t > a.duration) { t = a.duration }a.currentTime = t}// ---- the overlay ---------------------------------------------------------------------------------------------------------------on jbOpen() {if (nowId == '') { return null }overlay = 'true'setBar()return null}on jbClose(e) {if (e.target != null && e.target.closest != null && e.target.closest('button') != null) { e.target.closest('button').blur() }overlay = 'false'setBar()return null}// a tap on the bar (not on a button or the seek line) opens the overlayon jbBarClick(e) {if (overlay == 'true') { return null }if (e.target.closest('button, mp-seek') != null) { return null }emit jbOpen()return null}// a swipe down over the cover or the title closes it (not over the buttons, the seek line or the queue, which scroll)on jbDown(e) {if (overlay != 'true' || e.target.closest('button, mp-seek, ol') != null) { swipeY = -1 return null }swipeY = e.clientYreturn null}on jbUp(e) {if (swipeY >= 0 && e.clientY - swipeY > 80) { overlay = 'false' setBar() }swipeY = -1return null}on jbCancel(e) { swipeY = -1 return null }// ---- counting, keys and the lock screen -----------------------------------------------------------------------------------// every second: the seconds the audio really played; at half the song or 4 minutes the server counts the play, onceon jbTick() {let a = audioEl()if (a == null || nowId == '' || a.paused || a.ended) { return null }heard = heard + 1let need = 240if (a.duration > 0 && a.duration / 2 < 240) { need = a.duration / 2 }if (heard >= need && countedId != nowId) {countedId = nowIdemit server jbPlayed(nowId)}return null}// the mount tick (the media player's trick): once the page is live the timer, the keys and the lock screen's buttons are set upon jbMount(e) {e.target.style.animationName = 'none'if (ticking == 'true') { return null }ticking = 'true'window.setInterval(() => { emit jbTick() }, 1000)let ms = window.navigator.mediaSessionif (ms != null) {ms.setActionHandler('play', () => { toggleNow() })ms.setActionHandler('pause', () => { toggleNow() })ms.setActionHandler('previoustrack', () => { emit jbPrev() })ms.setActionHandler('nexttrack', () => { emit jbNext() })}return null}
Branches
Latest commits
- 9daebb4bjukebox JB3b: shuffle, now-playing overlay, next and previous, play counting, media session, gates; keyboard left open (Hybriel gap)mre
- 59c1373ejukebox JB3 (work in progress): albums without folder and with disc markers folded, compilations, duplicate songs, own play counter, server-side shuffle queuemre
- 2a4cfc55jukebox: library screens (songs, artists, albums, artist, album, search, favourites), compact player bar, /stream with Range, thumbnails, virtual list vendored, ui gate and real-library checkmre
- 93a44212jukebox: report, numbers of the full scan, notes on the index key rulemre
- bf5167dajukebox: index keys without commas (mpackdb loses them after a reopen), comma fixture and gate checksmre
- 28a82fa2jukebox: README, STATUS, LOG, DECISIONSmre
- d13987dfjukebox: library scanner, ID3 reader, mpackdb database and JSON API, with fixtures and a gatemre