jukebox
All repositories: gitoria
7.1 KB
// 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// first bytes of the file (hl:fs `readBytes(path, offset, length)`, hybriel#233) and the ID3v1 block// (the last 128 bytes, found with `stat`); the duration from the first audio frame (lib/mpeg.hl). Every other format: one ffprobe process.// The library is opened for reading only, by those programs. The normal form (genre names, "n/m" numbers, the year,// "Unknown artist") is `songFrom`. The cover picture of an mp3 comes back beside the record, not inside it.import { run } from 'hl:proc'import { readBytes, stat } from 'hl:fs'import { readId3v2, readId3v1, tagLength } from './id3.hl'import { mp3Seconds } from './mpeg.hl'import { genreName, genreNames } from './id3-helpers.hl'import { leadingInt, baseName, folderOf, extOf, fold, padNumber, SEP, safeKey } from './util.hl'import { splitDisc, albumKeyOf, discFor, sortKeys } from './albums.hl'static headBytes = (path, n) => {return readBytes(path, 0, n)}static tailBytes = (path, n) => {st = stat(path)if (st == null || st.size < n) { return null }return readBytes(path, st.size - n, n)}static first = (t, name) => {v = t[name]return v == null ? '' : v[0]}// a year out of "2002", "2002-05-01", "(c) 1999": the first 4 digits that make a year, else nullstatic yearOf = (s) => {if (s == null) { return null }let i = 0while (i + 4 <= s.length) {n = leadingInt(s.slice(i, i + 4))if (n != null && s.slice(i, i + 4).length == 4 && n >= 1000 && n <= 2999 && '' + n == s.slice(i, i + 4)) { return n }i = i + 1}return null}// "11/21" → [11, 21]; "3" → [3, null]; '' → [null, null]static pairOf = (s) => {if (s == null || s == '') { return [null, null] }k = s.indexOf('/')if (k < 0) { return [leadingInt(s.trim()), null] }return [leadingInt(s.slice(0, k).trim()), leadingInt(s.slice(k + 1).trim())]}static starsOf = (rating) => {if (rating <= 0) { return 0 }if (rating < 32) { return 1 }if (rating < 96) { return 2 }if (rating < 160) { return 3 }if (rating < 224) { return 4 }return 5}// THE RAW READ of an mp3: what the tags say, before normalising. { title, artist, albumArtist, album, track, disc, year,// genre, duration, popm = [{owner, rating, count}], pcnt, cover = { mime, data } | null } — or null when unreadable.static readMp3 = (path, size) => {let b = headBytes(path, 131072)if (b == null) { return null }tl = tagLength(b)let need = tl + 4096if (need > 8388608) { need = 8388608 }if (need > b.length && size > b.length) {more = headBytes(path, need)if (more != null) { b = more }}t = readId3v2(b)text = t == null ? {} : t.textlet title = first(text, 'TIT2')let artist = first(text, 'TPE1')let album = first(text, 'TALB')let year = first(text, 'TDRC')if (year == '') { year = first(text, 'TYER') }if (year == '') { year = first(text, 'TORY') }if (year == '') { year = first(text, 'TDOR') }let genre = first(text, 'TCON')let track = first(text, 'TRCK')if (t == null || (title == '' && artist == '' && album == '')) {tail = tailBytes(path, 128)v1 = tail == null ? null : readId3v1(tail)if (v1 != null) {if (title == '') { title = v1.title }if (artist == '') { artist = v1.artist }if (album == '') { album = v1.album }if (year == '') { year = v1.year }if (genre == '' && v1.genre < genreNames.length) { genre = genreNames[v1.genre] }if (track == '' && v1.track != null) { track = '' + v1.track }}}let duration = nulltlen = leadingInt(first(text, 'TLEN'))audioStart = tlsecs = mp3Seconds(b, audioStart, size - audioStart)if (secs != null) { duration = secs }else if (tlen != null) { duration = tlen / 1000 }return {title = title artist = artist albumArtist = first(text, 'TPE2') album = album track = trackdisc = first(text, 'TPOS') year = year genre = genre duration = durationpopm = t == null ? [] : t.popm pcnt = t == null ? null : t.pcnt cover = t == null ? null : t.cover}}// THE RAW READ of anything else, with ffprobe (one process): the format's tags and durationstatic readProbe = (path) => {r = run(['ffprobe', '-v', 'error', '-print_format', 'json', '-show_entries', 'format=duration:format_tags:stream_tags', path], { timeout = 60 })if (r == null || r.exit != 0) { return null }let text = ''for (l of r.lines) { text = text + l + ' ' }if (!text.includes('"format"')) { return null }j = JSON.parse(text)if (j == null || j.format == null) { return null }tags = {}// the tags of the format first; an ogg or flac file keeps them on its audio streamif (j.streams != null && j.streams.length != null) {for (st of j.streams) {if (st.tags != null) {for (k in st.tags) { tags[k.toLowerCase()] = st.tags[k] }}}}if (j.format.tags != null) {for (k in j.format.tags) { tags[k.toLowerCase()] = j.format.tags[k] }}pick = (a, b2) => {let v = tags[a]if (v == null && b2 != null) { v = tags[b2] }return v == null ? '' : ('' + v).trim()}dur = j.format.duration == null ? null : toNumber(j.format.duration)return {title = pick('title', null) artist = pick('artist', null) albumArtist = pick('album_artist', 'albumartist') album = pick('album', null)track = pick('track', 'tracknumber') disc = pick('disc', 'discnumber') year = pick('date', 'year') genre = pick('genre', null)duration = dur popm = [] pcnt = null cover = null}}static videoExt = { 'mp4' = true 'm4v' = true 'mkv' = true 'webm' = true 'mov' = true 'avi' = true }// THE SONG RECORD from a raw read. `meta` = { path, size, changeTime }.static songFrom = (raw, meta, coverId) => {path = meta.pathfolder = folderOf(path)ext = extOf(path)let artist = raw.artistlet albumArtist = raw.albumArtistif (artist == '') { artist = albumArtist }if (artist == '') { artist = 'Unknown artist' }let album = raw.albumif (album == '') { album = 'Unknown album' }let title = raw.titleif (title == '') { title = baseName(path) }tn = pairOf(raw.track)dn = pairOf(raw.disc)let play = raw.pcnt == null ? 0 : raw.pcntlet rating = 0for (p of raw.popm) {if (p.count > play) { play = p.count }if (p.rating > rating) { rating = p.rating }}groupArtist = albumArtist != '' ? albumArtist : artistparts = splitDisc(album)album = parts.namealbumKey = albumKeyOf(groupArtist, album)disc = discFor(dn[0], parts.disc)track = tn[0] == null ? 0 : tn[0]keys = sortKeys(groupArtist, album, disc, track, title)return {path = path folder = folder title = title artist = artist albumArtist = groupArtist album = albumtrack = track trackTotal = tn[1] disc = disc discTotal = dn[1] year = yearOf(raw.year)genre = genreName(raw.genre) duration = raw.duration format = ext kind = videoExt[ext] == true ? 'video' : 'audio'size = meta.size changeTime = meta.changeTime playCount = play popm = raw.popm rating = ratingstars = starsOf(rating) coverId = coverIdalbumKey = albumKey artistKey = safeKey(fold(groupArtist))search = fold(artist + ' ' + title + ' ' + album + ' ' + raw.genre)sortArtist = keys.sortArtistsortAlbum = keys.sortAlbumsortTitle = safeKey(fold(title) + SEP + fold(groupArtist) + SEP + fold(album))}}
Branches
Latest commits
- a4bce6d1Records: ticket push subscriptionmre
- 14abde3bjukebox: records: Atlas's records moved to worldapi-conductor/archivemre
- 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