gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit2a4cfc552a4cfc55jukebox: library screens (songs, artists, albums, artist, album, search, favourites), compact player bar, /stream with Range, thumbnails, virtual list vendored, ui gate and real-library checkmre2a4cfc55/lib/browse.hl

8.8 KB

  1. // lib/browse.hl — READING THE DATABASE FAST: pages of songs in the three orders, artists, albums, search, shuffle, stats.
  2. // The order lists (song ids with their play count, sorted by title / artist / album) are made once from the sorted
  3. // indexes of the songs table and kept in memory until a scan changes something; a page is a slice of such a list and
  4. // one lookup per row. Nothing here returns more than `maxLimit` rows. No writes (lib/library.hl and lib/catalog.hl write).
  5. import { random } from 'hl:math'
  6. import { songsTable, albumsTable, artistsTable, coversTable } from './db.hl'
  7. import { floor } from 'hl:math'
  8. import { fold, safeKey } from './util.hl'
  9. import OrderCache from './order-cache.hl'
  10. static maxLimit = 200
  11. static defaultLimit = 50
  12. static cache = new OrderCache()
  13. static dropOrderCache = () => {
  14. cache.lists = {}
  15. cache.stats = null
  16. }
  17. static sortField = (sort) => {
  18. if (sort == 'title') { return 'sortTitle' }
  19. if (sort == 'album') { return 'sortAlbum' }
  20. if (sort == 'plays') { return 'sortTitle' }
  21. return 'sortArtist'
  22. }
  23. // the lean rows { id, pc } of all songs in one order
  24. static orderList = (sort) => {
  25. field = sortField(sort)
  26. list = cache.lists[field]
  27. if (list != null) { return list }
  28. let out = []
  29. for (r of songsTable.range(field, null, null)) { out.push({ id = r.id pc = r.playCount }) }
  30. // the most played first, by a counting sort over the play counts (the index of playCount sorts the numbers as text: 9 before 12)
  31. if (sort == 'plays') {
  32. let top = 0
  33. for (r of out) { if (r.pc > top) { top = r.pc } }
  34. buckets = []
  35. let b = 0
  36. while (b <= top) {
  37. buckets.push([])
  38. b = b + 1
  39. }
  40. for (r of out) { buckets[r.pc].push(r) }
  41. let sorted = []
  42. let k = top
  43. while (k >= 0) {
  44. for (r of buckets[k]) { sorted.push(r) }
  45. k = k - 1
  46. }
  47. out = sorted
  48. }
  49. lists = cache.lists
  50. lists[field] = out
  51. cache.lists = lists
  52. return out
  53. }
  54. // ---- what the API shows of a song / album / artist --------------------------------------------------------------
  55. static publicSong = (s) => {
  56. return {
  57. id = s.id title = s.title artist = s.artist albumArtist = s.albumArtist album = s.album track = s.track trackTotal = s.trackTotal
  58. disc = s.disc year = s.year genre = s.genre duration = s.duration format = s.format kind = s.kind size = s.size
  59. playCount = s.playCount rating = s.rating stars = s.stars popm = s.popm coverId = s.coverId path = s.path addedAt = s.addedAt
  60. }
  61. }
  62. static publicAlbum = (a) => {
  63. return { id = a.id name = a.name albumArtist = a.albumArtist year = a.year songCount = a.songCount duration = a.duration playCount = a.playCount coverId = a.coverId folder = a.folder }
  64. }
  65. static publicArtist = (a) => {
  66. return { id = a.id name = a.name songCount = a.songCount albumCount = a.albumCount playCount = a.playCount coverId = a.coverId }
  67. }
  68. static clampLimit = (limit) => {
  69. if (limit == null || limit < 1) { return defaultLimit }
  70. return limit > maxLimit ? maxLimit : limit
  71. }
  72. static clampOffset = (offset) => { return offset == null || offset < 0 ? 0 : offset }
  73. // rows ordered by one string field (insertion sort; small lists only: one album, one artist)
  74. static orderedBy = (rows, field) => {
  75. out = []
  76. for (r of rows) {
  77. let j = out.length
  78. out.push(r)
  79. while (j > 0 && out[j - 1][field] > r[field]) {
  80. out[j] = out[j - 1]
  81. j = j - 1
  82. }
  83. out[j] = r
  84. }
  85. return out
  86. }
  87. static page = (total, offset, limit, items) => { return { total = total offset = offset limit = limit items = items } }
  88. // ---- songs ------------------------------------------------------------------------------------------------------
  89. // params: sort (title | artist | album | plays), offset, limit, minPlays, artist (an artist id), album (an album id)
  90. static songsPage = (sort, offset, limit, minPlays, artistId, albumId) => {
  91. off = clampOffset(offset)
  92. lim = clampLimit(limit)
  93. if (albumId != null) {
  94. a = albumsTable.fetch(albumId)
  95. if (a == null) { return page(0, off, lim, []) }
  96. return slicePage(orderedBy(songsTable.find('albumKey', a.key), 'sortAlbum'), off, lim, minPlays)
  97. }
  98. if (artistId != null) {
  99. a = artistsTable.fetch(artistId)
  100. if (a == null) { return page(0, off, lim, []) }
  101. return slicePage(orderedBy(songsTable.find('artistKey', a.key), 'sortArtist'), off, lim, minPlays)
  102. }
  103. list = orderList(sort)
  104. min = minPlays == null ? 0 : minPlays
  105. items = []
  106. let total = 0
  107. for (r of list) {
  108. if (r.pc < min) { continue }
  109. if (total >= off && items.length < lim) {
  110. s = songsTable.fetch(r.id)
  111. if (s != null) { items.push(publicSong(s)) }
  112. }
  113. total = total + 1
  114. }
  115. return page(total, off, lim, items)
  116. }
  117. // a page out of full rows already in order (an album, an artist)
  118. static slicePage = (rows, off, lim, minPlays) => {
  119. min = minPlays == null ? 0 : minPlays
  120. items = []
  121. let total = 0
  122. for (s of rows) {
  123. if (s.playCount < min) { continue }
  124. if (total >= off && items.length < lim) { items.push(publicSong(s)) }
  125. total = total + 1
  126. }
  127. return page(total, off, lim, items)
  128. }
  129. // `count` random songs (no repeats) among those played at least `minPlays` times
  130. static shuffle = (minPlays, count) => {
  131. lim = clampLimit(count)
  132. min = minPlays == null ? 0 : minPlays
  133. pool = []
  134. for (r of orderList('title')) {
  135. if (r.pc >= min) { pool.push(r.id) }
  136. }
  137. items = []
  138. let left = pool.length
  139. while (items.length < lim && left > 0) {
  140. k = floor(random() * left)
  141. id = pool[k]
  142. pool[k] = pool[left - 1]
  143. left = left - 1
  144. s = songsTable.fetch(id)
  145. if (s != null) { items.push(publicSong(s)) }
  146. }
  147. return page(pool.length, 0, lim, items)
  148. }
  149. // ---- artists and albums -----------------------------------------------------------------------------------------
  150. static artistsPage = (offset, limit) => {
  151. off = clampOffset(offset)
  152. lim = clampLimit(limit)
  153. rows = artistsTable.range('sortName', null, null)
  154. items = []
  155. let i = off
  156. while (i < rows.length && items.length < lim) {
  157. items.push(publicArtist(rows[i]))
  158. i = i + 1
  159. }
  160. return page(rows.length, off, lim, items)
  161. }
  162. static albumsPage = (offset, limit, artistId) => {
  163. off = clampOffset(offset)
  164. lim = clampLimit(limit)
  165. let rows = []
  166. if (artistId != null) {
  167. a = artistsTable.fetch(artistId)
  168. if (a != null) { rows = orderedBy(albumsTable.find('artistKey', a.key), 'sortName') }
  169. } else {
  170. rows = albumsTable.range('sortName', null, null)
  171. }
  172. items = []
  173. let i = off
  174. while (i < rows.length && items.length < lim) {
  175. items.push(publicAlbum(rows[i]))
  176. i = i + 1
  177. }
  178. return page(rows.length, off, lim, items)
  179. }
  180. // ---- search -----------------------------------------------------------------------------------------------------
  181. // every word of q must be in the song's folded artist / title / album / genre text
  182. static searchAll = (q, offset, limit) => {
  183. off = clampOffset(offset)
  184. lim = clampLimit(limit)
  185. terms = []
  186. let words = []
  187. for (t of fold(q).split(' ')) {
  188. if (t != '') { words.push(t) }
  189. }
  190. for (t of words) { terms.push(t) }
  191. // artists and albums are matched on their index keys, where a comma is character 2
  192. keyTerms = []
  193. for (t of words) { keyTerms.push(safeKey(t)) }
  194. if (terms.length == 0) { return { q = q songs = page(0, off, lim, []) artists = [] albums = [] } }
  195. items = []
  196. let total = 0
  197. for (s of songsTable.iterate(null, null)) {
  198. let ok = true
  199. for (t of terms) {
  200. if (!s.search.includes(t)) {
  201. ok = false
  202. break
  203. }
  204. }
  205. if (!ok) { continue }
  206. if (total >= off && items.length < lim) { items.push(publicSong(s)) }
  207. total = total + 1
  208. }
  209. artists = []
  210. for (a of artistsTable.range('sortName', null, null)) {
  211. if (artists.length >= 10) { break }
  212. let ok = true
  213. for (t of keyTerms) {
  214. if (!a.sortName.includes(t)) { ok = false }
  215. }
  216. if (ok) { artists.push(publicArtist(a)) }
  217. }
  218. albums = []
  219. for (a of albumsTable.range('sortName', null, null)) {
  220. if (albums.length >= 10) { break }
  221. let ok = true
  222. for (t of keyTerms) {
  223. if (!a.sortName.includes(t)) { ok = false }
  224. }
  225. if (ok) { albums.push(publicAlbum(a)) }
  226. }
  227. return { q = q songs = page(total, off, lim, items) artists = artists albums = albums }
  228. }
  229. // ---- stats ------------------------------------------------------------------------------------------------------
  230. static libraryStats = () => {
  231. if (cache.stats != null) { return cache.stats }
  232. let songs = 0
  233. let seconds = 0
  234. let bytes = 0
  235. let played1 = 0
  236. let played5 = 0
  237. let withCover = 0
  238. let videos = 0
  239. for (s of songsTable.iterate(null, null)) {
  240. songs = songs + 1
  241. seconds = seconds + (s.duration == null ? 0 : s.duration)
  242. bytes = bytes + (s.size == null ? 0 : s.size)
  243. if (s.playCount >= 1) { played1 = played1 + 1 }
  244. if (s.playCount >= 5) { played5 = played5 + 1 }
  245. if (s.coverId != null) { withCover = withCover + 1 }
  246. if (s.kind == 'video') { videos = videos + 1 }
  247. }
  248. st = { songs = songs artists = artistsTable.count() albums = albumsTable.count() covers = coversTable.count() songsWithCover = withCover
  249. videos = videos playedAtLeast1 = played1 playedAtLeast5 = played5 totalSeconds = seconds totalBytes = bytes }
  250. cache.stats = st
  251. return st
  252. }

Branches

Latest commits

  • 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