gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchmain10f8a8c8Merge branch 'code-order' into mainJukeboxmain/lib/rows.hl

9.0 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, poolSize } 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. 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. // where a song is in the list as it is NOW: the list is searched around the place the screen remembers (counted plays move songs a few places), then all of it.
  83. // Answers the place, or -1 when the song is no longer in the list.
  84. static placeOf = (ctx, index, id) => {
  85. let lo = index - 200
  86. if (lo < 0) { lo = 0 }
  87. let page = pageOfSongs(ctx, lo, 400)
  88. let i = 0
  89. while (i < page.items.length) {
  90. if (page.items[i].id == id) { return lo + i }
  91. i = i + 1
  92. }
  93. let off = 0
  94. let total = 1
  95. while (off < total) {
  96. page = pageOfSongs(ctx, off, 500)
  97. total = page.total
  98. if (page.items.length == 0) { break }
  99. let j = 0
  100. while (j < page.items.length) {
  101. if (page.items[j].id == id) { return off + j }
  102. j = j + 1
  103. }
  104. off = off + page.items.length
  105. }
  106. return -1
  107. }
  108. // the next `count` songs of the list a song was picked from, starting at its place: the player's queue.
  109. // A song is named by its id: `index` is only where the screen saw it, and plays counted since then may have moved it, so the song is looked up in the list as it is now;
  110. // a song that is no longer in the list still plays first, and the list goes on from the place it was seen at.
  111. // `step` moves the start from the id's place: 0 = the song itself, 1 = the song after it (Next), -1 = the song before it (Previous).
  112. // `again` (Repeat all): true = the queue never ends (a list starts again from its first song, a shuffle goes on with a new round); false = it ends with the list
  113. // (a shuffle after one round); null (callers that do not ask) = as before: a list ends, a shuffle goes on.
  114. static queueFor = (ctx, index, count, id, step, again) => {
  115. if (ctx.kind == 'shuffle') { return shuffleQueue(ctx, index, count, again) }
  116. let start = index
  117. out = []
  118. let by = step == null ? 0 : step
  119. if (id != null && id != '') {
  120. at = placeOf(ctx, index, id)
  121. if (at >= 0) { start = at + by }
  122. else if (by == 0) {
  123. gone = songsTable.fetch(id)
  124. if (gone != null) { out.push(queueItem(gone, index)) start = index + 1 }
  125. }
  126. else { start = index + by }
  127. }
  128. if (start < 0) { start = 0 }
  129. page = pageOfSongs(ctx, start, count)
  130. let i = start
  131. for (s of page.items) {
  132. out.push(queueItem(s, i))
  133. i = i + 1
  134. }
  135. if (again == true && out.length < count && out.length > 0) {
  136. for (w of wrapQueue(ctx, out[0].id, count - out.length)) { out.push(w) }
  137. }
  138. return out
  139. }
  140. // Repeat all: the end of the list is followed by its first songs, up to (not including) the song the queue starts with, so no song stands twice in one queue
  141. static wrapQueue = (ctx, startId, count) => {
  142. more = []
  143. page = pageOfSongs(ctx, 0, count)
  144. let i = 0
  145. for (s of page.items) {
  146. if (s.id == startId) { break }
  147. more.push(queueItem(s, i))
  148. i = i + 1
  149. }
  150. return more
  151. }
  152. // the next songs of a shuffle: the server holds the order, the browser gets `count` items from `index` on
  153. static shuffleQueue = (ctx, index, count, again) => {
  154. out = []
  155. let i = index
  156. let last = again == false ? poolSize(ctx) : -1
  157. while (out.length < count) {
  158. if (last >= 0 && i >= last) { break }
  159. id = idAt(ctx, i)
  160. if (id == null) { break }
  161. s = songsTable.fetch(id)
  162. if (s != null) { out.push(queueItem(s, i)) }
  163. i = i + 1
  164. }
  165. return out
  166. }
  167. // everything the search page shows next to the songs: the artists and the albums that match (at most 10 each)
  168. static searchGroups = (q) => {
  169. r = searchAll(q, 0, 100)
  170. artists = []
  171. for (a of r.artists) { artists.push(artistRow(a)) }
  172. albums = []
  173. for (a of r.albums) { albums.push(albumRow(a)) }
  174. return { artists = artists albums = albums songs = rowsOf(r.songs, 'song', null) }
  175. }
  176. // the head of an artist's or an album's screen: its name, its numbers and its picture
  177. static artistInfo = (id) => {
  178. a = artistsTable.fetch(id)
  179. if (a == null) { return { missing = true name = '' line = '' img = '' } }
  180. 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 }
  181. }
  182. static albumInfo = (id) => {
  183. a = albumsTable.fetch(id)
  184. if (a == null) { return { missing = true name = '' artist = '' line = '' img = '' } }
  185. len = clock(a.duration)
  186. 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 }
  187. }

Branches

Latest commits

  • 10f8a8c8Merge branch 'code-order' into mainJukebox
  • 204fc828Merge branch 'day72' into mainJukebox
  • 95f5faae|||PR jukebox#7 Drop needless let declarationsmre
  • 33072bb5|||PR Record day72 gates and Chrome proofmre
  • 95f2cd73|||PR Use typed parent methods for screen playback and local server facesmre
  • 6422a020Sync media-player at 51c3818mre
  • 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