gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit0f2157a90f2157a9jukebox 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 againmre0f2157a9/lib/rows.hl

6.7 KB

  1. // lib/rows.hl — WHAT THE SCREENS SHOW: the database's songs, artists and albums turned into the rows of the virtual list
  2. // (`{ id, title, sub, aside, img }`) and into the items of the player's queue. The one place that decides how a row reads.
  3. // A screen asks `rowsFor(ctx, offset, limit)` with its context: which list it shows. Pure reading, no writes.
  4. import { songsPage, artistsPage, albumsPage, searchAll } from './browse.hl'
  5. import { artistsTable, albumsTable, songsTable } from './db.hl'
  6. import { idAt } from './queue.hl'
  7. // 3,966 → "3,966"
  8. static thousands = (n) => {
  9. s = '' + n
  10. let out = ''
  11. let i = 0
  12. while (i < s.length) {
  13. if (i > 0 && (s.length - i) % 3 == 0) { out = out + ',' }
  14. out = out + s[i]
  15. i = i + 1
  16. }
  17. return out
  18. }
  19. // 221.4 seconds → "3:41"; 3725 seconds → "1:02:05"; unknown → ""
  20. static clock = (seconds) => {
  21. if (seconds == null || seconds <= 0) { return '' }
  22. whole = seconds - seconds % 1
  23. h = (whole - whole % 3600) / 3600
  24. m = ((whole - h * 3600) - (whole - h * 3600) % 60) / 60
  25. s = whole - h * 3600 - m * 60
  26. ss = (s < 10 ? '0' : '') + s
  27. if (h > 0) { return h + ':' + (m < 10 ? '0' : '') + m + ':' + ss }
  28. return m + ':' + ss
  29. }
  30. // the small picture of a cover (96 px, made once by lib/covers.hl) and the large one
  31. static thumbUrl = (coverId) => { return coverId == null ? '' : '/api/thumb/' + coverId }
  32. static coverUrl = (coverId) => { return coverId == null ? '' : '/api/cover/' + coverId }
  33. // the player shows title and artist only beside a cover: a song without one gets this empty picture (the cover box is the surface colour)
  34. static noPicture = 'data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%2F%3E'
  35. static songRow = (s, sort) => {
  36. return { id = s.id title = s.title sub = s.artist + ' · ' + s.album + (s.copies > 1 ? ' · ' + s.copies + ' copies' : '') aside = sort == 'plays' ? s.playCount + '×' : clock(s.duration) img = thumbUrl(s.coverId) }
  37. }
  38. // inside one album the track number leads and the artist is left out
  39. static albumSongRow = (s, multi) => {
  40. return { id = s.id title = (multi && s.disc != null ? s.disc + '-' : '') + (s.track != null && s.track > 0 ? s.track + '. ' : '') + s.title sub = s.artist == s.albumArtist ? '' : s.artist aside = clock(s.duration) img = '' }
  41. }
  42. static artistRow = (a) => {
  43. return { id = a.id title = a.name sub = thousands(a.albumCount) + (a.albumCount == 1 ? ' album · ' : ' albums · ') + thousands(a.songCount) + (a.songCount == 1 ? ' song' : ' songs') aside = '' img = thumbUrl(a.coverId) hasImg = a.coverId != null noImg = a.coverId == null }
  44. }
  45. static albumRow = (a) => {
  46. return { id = a.id title = a.name sub = a.albumArtist + (a.year == null ? '' : ' · ' + a.year) aside = thousands(a.songCount) img = thumbUrl(a.coverId) hasImg = a.coverId != null noImg = a.coverId == null }
  47. }
  48. // kind of row: 'song', 'albumSong', 'artist', 'album'
  49. static rowsOf = (page, kind, sort) => {
  50. out = []
  51. for (s of page.items) {
  52. if (kind == 'artist') { out.push(artistRow(s)) }
  53. else if (kind == 'album') { out.push(albumRow(s)) }
  54. else if (kind == 'albumSong') { out.push(albumSongRow(s, sort == 'multi')) }
  55. else { out.push(songRow(s, sort)) }
  56. }
  57. return { total = page.total offset = page.offset rows = out }
  58. }
  59. // the queue item of the player for one song: its stream, its title, its length
  60. static queueItem = (s, index) => {
  61. return { src = '/stream/' + s.id title = s.title dur = clock(s.duration) artist = s.artist album = s.album cover = s.coverId == null ? noPicture : thumbUrl(s.coverId) art = s.coverId == null ? noPicture : coverUrl(s.coverId) idx = index id = s.id }
  62. }
  63. // ctx.kind: songs (sort, minPlays) | artists | albums | artist-songs (artist, sort) | artist-albums (artist) | album (album) | search (q)
  64. static pageOfSongs = (ctx, offset, limit) => {
  65. let sort = ctx.sort == null ? 'artist' : ctx.sort
  66. if (ctx.kind == 'album') { return songsPage('artist', offset, limit, null, null, ctx.album) }
  67. if (ctx.kind == 'artist-songs') { return songsPage(sort, offset, limit, null, ctx.artist, null) }
  68. if (ctx.kind == 'search') { return searchAll(ctx.q, offset, limit).songs }
  69. return songsPage(sort, offset, limit, ctx.minPlays, null, null)
  70. }
  71. static rowsFor = (ctx, offset, limit) => {
  72. if (ctx.kind == 'artists') { return rowsOf(artistsPage(offset, limit), 'artist', null) }
  73. if (ctx.kind == 'albums') { return rowsOf(albumsPage(offset, limit, null), 'album', null) }
  74. if (ctx.kind == 'artist-albums') { return rowsOf(albumsPage(offset, limit, ctx.artist), 'album', null) }
  75. page = pageOfSongs(ctx, offset, limit)
  76. if (ctx.kind == 'album') {
  77. a = albumsTable.fetch(ctx.album)
  78. return rowsOf(page, 'albumSong', a != null && a.discs > 1 ? 'multi' : '')
  79. }
  80. return rowsOf(page, 'song', ctx.sort)
  81. }
  82. // the next `count` songs of the list a song was picked from, starting at its place: the player's queue
  83. static queueFor = (ctx, index, count) => {
  84. if (ctx.kind == 'shuffle') { return shuffleQueue(ctx, index, count) }
  85. page = pageOfSongs(ctx, index, count)
  86. out = []
  87. let i = index
  88. for (s of page.items) {
  89. out.push(queueItem(s, i))
  90. i = i + 1
  91. }
  92. return out
  93. }
  94. // the next songs of a shuffle: the server holds the order, the browser gets `count` items from `index` on
  95. static shuffleQueue = (ctx, index, count) => {
  96. out = []
  97. let i = index
  98. while (out.length < count) {
  99. id = idAt(ctx, i)
  100. if (id == null) { break }
  101. s = songsTable.fetch(id)
  102. if (s != null) { out.push(queueItem(s, i)) }
  103. i = i + 1
  104. }
  105. return out
  106. }
  107. // everything the search page shows next to the songs: the artists and the albums that match (at most 10 each)
  108. static searchGroups = (q) => {
  109. r = searchAll(q, 0, 100)
  110. artists = []
  111. for (a of r.artists) { artists.push(artistRow(a)) }
  112. albums = []
  113. for (a of r.albums) { albums.push(albumRow(a)) }
  114. return { artists = artists albums = albums songs = rowsOf(r.songs, 'song', null) }
  115. }
  116. // the head of an artist's or an album's screen: its name, its numbers and its picture
  117. static artistInfo = (id) => {
  118. a = artistsTable.fetch(id)
  119. if (a == null) { return { missing = true name = '' line = '' img = '' } }
  120. return { missing = false name = a.name line = thousands(a.albumCount) + (a.albumCount == 1 ? ' album · ' : ' albums · ') + thousands(a.songCount) + (a.songCount == 1 ? ' song' : ' songs') img = thumbUrl(a.coverId) songs = a.songCount albums = a.albumCount }
  121. }
  122. static albumInfo = (id) => {
  123. a = albumsTable.fetch(id)
  124. if (a == null) { return { missing = true name = '' artist = '' line = '' img = '' } }
  125. len = clock(a.duration)
  126. return { missing = false name = a.name artist = a.albumArtist line = (a.year == null ? '' : a.year + ' · ') + (a.discs > 1 ? a.discs + ' discs · ' : '') + thousands(a.songCount) + (a.songCount == 1 ? ' song' : ' songs') + (len == '' ? '' : ' · ' + len) img = coverUrl(a.coverId) songs = a.songCount }
  127. }

Branches

Latest commits

  • 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
  • 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