gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit0433e9a80433e9a8jukebox: records: gitoria remote added and pushedmre0433e9a8/lib/tags.hl

7.1 KB

  1. // lib/tags.hl — READ ONE FILE'S TAGS and make a song record of them. mp3: our own ID3 reader (lib/id3.hl) on the
  2. // first bytes of the file (hl:fs `readBytes(path, offset, length)`, hybriel#233) and the ID3v1 block
  3. // (the last 128 bytes, found with `stat`); the duration from the first audio frame (lib/mpeg.hl). Every other format: one ffprobe process.
  4. // The library is opened for reading only, by those programs. The normal form (genre names, "n/m" numbers, the year,
  5. // "Unknown artist") is `songFrom`. The cover picture of an mp3 comes back beside the record, not inside it.
  6. import { run } from 'hl:proc'
  7. import { readBytes, stat } from 'hl:fs'
  8. import { readId3v2, readId3v1, tagLength } from './id3.hl'
  9. import { mp3Seconds } from './mpeg.hl'
  10. import { genreName, genreNames } from './id3-helpers.hl'
  11. import { leadingInt, baseName, folderOf, extOf, fold, padNumber, SEP, safeKey } from './util.hl'
  12. import { splitDisc, albumKeyOf, discFor, sortKeys } from './albums.hl'
  13. static headBytes = (path, n) => {
  14. return readBytes(path, 0, n)
  15. }
  16. static tailBytes = (path, n) => {
  17. st = stat(path)
  18. if (st == null || st.size < n) { return null }
  19. return readBytes(path, st.size - n, n)
  20. }
  21. static first = (t, name) => {
  22. v = t[name]
  23. return v == null ? '' : v[0]
  24. }
  25. // a year out of "2002", "2002-05-01", "(c) 1999": the first 4 digits that make a year, else null
  26. static yearOf = (s) => {
  27. if (s == null) { return null }
  28. let i = 0
  29. while (i + 4 <= s.length) {
  30. n = leadingInt(s.slice(i, i + 4))
  31. if (n != null && s.slice(i, i + 4).length == 4 && n >= 1000 && n <= 2999 && '' + n == s.slice(i, i + 4)) { return n }
  32. i = i + 1
  33. }
  34. return null
  35. }
  36. // "11/21" → [11, 21]; "3" → [3, null]; '' → [null, null]
  37. static pairOf = (s) => {
  38. if (s == null || s == '') { return [null, null] }
  39. k = s.indexOf('/')
  40. if (k < 0) { return [leadingInt(s.trim()), null] }
  41. return [leadingInt(s.slice(0, k).trim()), leadingInt(s.slice(k + 1).trim())]
  42. }
  43. static starsOf = (rating) => {
  44. if (rating <= 0) { return 0 }
  45. if (rating < 32) { return 1 }
  46. if (rating < 96) { return 2 }
  47. if (rating < 160) { return 3 }
  48. if (rating < 224) { return 4 }
  49. return 5
  50. }
  51. // THE RAW READ of an mp3: what the tags say, before normalising. { title, artist, albumArtist, album, track, disc, year,
  52. // genre, duration, popm = [{owner, rating, count}], pcnt, cover = { mime, data } | null } — or null when unreadable.
  53. static readMp3 = (path, size) => {
  54. let b = headBytes(path, 131072)
  55. if (b == null) { return null }
  56. tl = tagLength(b)
  57. let need = tl + 4096
  58. if (need > 8388608) { need = 8388608 }
  59. if (need > b.length && size > b.length) {
  60. more = headBytes(path, need)
  61. if (more != null) { b = more }
  62. }
  63. t = readId3v2(b)
  64. text = t == null ? {} : t.text
  65. let title = first(text, 'TIT2')
  66. let artist = first(text, 'TPE1')
  67. let album = first(text, 'TALB')
  68. let year = first(text, 'TDRC')
  69. if (year == '') { year = first(text, 'TYER') }
  70. if (year == '') { year = first(text, 'TORY') }
  71. if (year == '') { year = first(text, 'TDOR') }
  72. let genre = first(text, 'TCON')
  73. let track = first(text, 'TRCK')
  74. if (t == null || (title == '' && artist == '' && album == '')) {
  75. tail = tailBytes(path, 128)
  76. v1 = tail == null ? null : readId3v1(tail)
  77. if (v1 != null) {
  78. if (title == '') { title = v1.title }
  79. if (artist == '') { artist = v1.artist }
  80. if (album == '') { album = v1.album }
  81. if (year == '') { year = v1.year }
  82. if (genre == '' && v1.genre < genreNames.length) { genre = genreNames[v1.genre] }
  83. if (track == '' && v1.track != null) { track = '' + v1.track }
  84. }
  85. }
  86. let duration = null
  87. tlen = leadingInt(first(text, 'TLEN'))
  88. audioStart = tl
  89. secs = mp3Seconds(b, audioStart, size - audioStart)
  90. if (secs != null) { duration = secs }
  91. else if (tlen != null) { duration = tlen / 1000 }
  92. return {
  93. title = title artist = artist albumArtist = first(text, 'TPE2') album = album track = track
  94. disc = first(text, 'TPOS') year = year genre = genre duration = duration
  95. popm = t == null ? [] : t.popm pcnt = t == null ? null : t.pcnt cover = t == null ? null : t.cover
  96. }
  97. }
  98. // THE RAW READ of anything else, with ffprobe (one process): the format's tags and duration
  99. static readProbe = (path) => {
  100. r = run(['ffprobe', '-v', 'error', '-print_format', 'json', '-show_entries', 'format=duration:format_tags:stream_tags', path], { timeout = 60 })
  101. if (r == null || r.exit != 0) { return null }
  102. let text = ''
  103. for (l of r.lines) { text = text + l + ' ' }
  104. if (!text.includes('"format"')) { return null }
  105. j = JSON.parse(text)
  106. if (j == null || j.format == null) { return null }
  107. tags = {}
  108. // the tags of the format first; an ogg or flac file keeps them on its audio stream
  109. if (j.streams != null && j.streams.length != null) {
  110. for (st of j.streams) {
  111. if (st.tags != null) {
  112. for (k in st.tags) { tags[k.toLowerCase()] = st.tags[k] }
  113. }
  114. }
  115. }
  116. if (j.format.tags != null) {
  117. for (k in j.format.tags) { tags[k.toLowerCase()] = j.format.tags[k] }
  118. }
  119. pick = (a, b2) => {
  120. let v = tags[a]
  121. if (v == null && b2 != null) { v = tags[b2] }
  122. return v == null ? '' : ('' + v).trim()
  123. }
  124. dur = j.format.duration == null ? null : toNumber(j.format.duration)
  125. return {
  126. title = pick('title', null) artist = pick('artist', null) albumArtist = pick('album_artist', 'albumartist') album = pick('album', null)
  127. track = pick('track', 'tracknumber') disc = pick('disc', 'discnumber') year = pick('date', 'year') genre = pick('genre', null)
  128. duration = dur popm = [] pcnt = null cover = null
  129. }
  130. }
  131. static videoExt = { 'mp4' = true 'm4v' = true 'mkv' = true 'webm' = true 'mov' = true 'avi' = true }
  132. // THE SONG RECORD from a raw read. `meta` = { path, size, changeTime }.
  133. static songFrom = (raw, meta, coverId) => {
  134. path = meta.path
  135. folder = folderOf(path)
  136. ext = extOf(path)
  137. let artist = raw.artist
  138. let albumArtist = raw.albumArtist
  139. if (artist == '') { artist = albumArtist }
  140. if (artist == '') { artist = 'Unknown artist' }
  141. let album = raw.album
  142. if (album == '') { album = 'Unknown album' }
  143. let title = raw.title
  144. if (title == '') { title = baseName(path) }
  145. tn = pairOf(raw.track)
  146. dn = pairOf(raw.disc)
  147. let play = raw.pcnt == null ? 0 : raw.pcnt
  148. let rating = 0
  149. for (p of raw.popm) {
  150. if (p.count > play) { play = p.count }
  151. if (p.rating > rating) { rating = p.rating }
  152. }
  153. groupArtist = albumArtist != '' ? albumArtist : artist
  154. parts = splitDisc(album)
  155. album = parts.name
  156. albumKey = albumKeyOf(groupArtist, album)
  157. disc = discFor(dn[0], parts.disc)
  158. track = tn[0] == null ? 0 : tn[0]
  159. keys = sortKeys(groupArtist, album, disc, track, title)
  160. return {
  161. path = path folder = folder title = title artist = artist albumArtist = groupArtist album = album
  162. track = track trackTotal = tn[1] disc = disc discTotal = dn[1] year = yearOf(raw.year)
  163. genre = genreName(raw.genre) duration = raw.duration format = ext kind = videoExt[ext] == true ? 'video' : 'audio'
  164. size = meta.size changeTime = meta.changeTime playCount = play popm = raw.popm rating = rating
  165. stars = starsOf(rating) coverId = coverId
  166. albumKey = albumKey artistKey = safeKey(fold(groupArtist))
  167. search = fold(artist + ' ' + title + ' ' + album + ' ' + raw.genre)
  168. sortArtist = keys.sortArtist
  169. sortAlbum = keys.sortAlbum
  170. sortTitle = safeKey(fold(title) + SEP + fold(groupArtist) + SEP + fold(album))
  171. }
  172. }

Branches

Latest commits

  • 0433e9a8jukebox: records: gitoria remote added and pushedmre
  • 1722d707jukebox: records: repo creation handed to Gitoriamre
  • b42e7dfbjukebox: conductor records (CONDUCTOR.md, STATUS.md), old STATUS archivedmre
  • 1b0d0bc1jukebox: a startup error prints its message before exit 1 (hybriel#247)mre
  • b19c478bjukebox JB18 (jukebox#17): onto Hybriel set-day31 (b287cd82, contains the SIGPIPE fix 6d2ca454): plugins/core and plugins/web re-vendored, bin/ from the set; a client that closes /stream while it is written no longer kills the server (90 aborted streams survived, the old bin died with exit 141 on the first)mre
  • 73867a2ejukebox#2: Style blocks in the language's nested form (rules named after the View's own elements, pseudo-classes nested, merged duplicates); the same CSS rules, the pages look the samemre
  • 06c548d6jukebox JB15: report, log linemre
  • 980859cbjukebox JB15 (jukebox#15): the now-playing overlay on a short screen (tablet or phone on its side) gave the queue 12 to 100 px because the big cover took the room; the cover now shrinks with the screen height and goes away under 41rem, the queue keeps 3+ rows; gate tests/jb15.mjs at 8 sizesmre
  • 9efc40ffjukebox#12 #14: Grok session 1793246574527-181160mre
  • 7d58b36ejukebox JB13 (jukebox#13): Next and Previous ask for the songs after / before the current song's id in the current order, not for place + 1; gate tests/jb13.mjs checks Next with the queue down to one song after the order changedmre
  • 51358794jukebox JB14 (jukebox#14): the Songs screen opens sorted the way it was left (kept on the server in storage/ui.json), the sort control shows the sort the rows really have (selected options, autocomplete off); gate tests/jb14.mjsmre
  • cc68a063jukebox JB13 (jukebox#13): a tapped song plays the song that was tapped, found by its id in the list as it is now (not by its row number, which counted plays move); the queue goes on from its place there, the rows refresh on the tap; gate tests/jb13.mjs (songs by plays, favourites)mre
  • 88d3d5c1jukebox JB12b (jukebox#12): the scroll handle is big (2.75rem x 4rem, accent colour, grip) and drawn on every device, no media query; native bar hidden; gate tests/touch.mjs checks the drawn width with and without touchmre
  • b58e4495jukebox JB11 (jukebox#11): the next song starts in the ended event on the same audio element, so it also starts with the screen locked; gate tests/jb11.mjs (hidden tab, no frames)mre
  • 12a26233jukebox JB12 (jukebox#12): the long lists get a scroll handle a finger can use on touch screens (44 px tap area, slim look, drag through all rows), gate tests/touch.mjsmre
  • 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