jukebox
All repositories: gitoria
6.7 KB
// lib/id3.hl — THE ID3v2 READER (versions 2.2, 2.3, 2.4): the tag at the start of an mp3, as plain values.// Text frames, POPM (every owner), PCNT, the cover picture (APIC), unsynchronisation (whole tag or per frame),// the extended header, frame flags (grouping, data length), and the ID3v1 block at the end of a file.// The input is a Bytes that begins with the tag; nothing is written anywhere. The noise (text encodings, genre// names, synchsafe numbers) is lib/id3-helpers.hl.import { decodeText, decodeLatin1, terminatorAt, terminatorWidth, textValues, synchsafe, bigEndian, undoUnsync } from './id3-helpers.hl'// the text frames we keep, by their ID3v2.3/2.4 names; v2.2 names are mapped onto themstatic wantedText = {'TIT2' = true 'TPE1' = true 'TPE2' = true 'TALB' = true 'TRCK' = true 'TPOS' = true 'TYER' = true'TDRC' = true 'TORY' = true 'TDOR' = true 'TCON' = true 'TLEN' = true 'TCOM' = true}static v22Names = {'TT2' = 'TIT2' 'TP1' = 'TPE1' 'TP2' = 'TPE2' 'TAL' = 'TALB' 'TRK' = 'TRCK' 'TPA' = 'TPOS' 'TYE' = 'TYER''TCO' = 'TCON' 'TLE' = 'TLEN' 'TCM' = 'TCOM' 'POP' = 'POPM' 'CNT' = 'PCNT' 'PIC' = 'APIC'}// How long the whole tag is, header included (10 + size, plus the 10-byte footer when the footer flag is set), from// the first 10 bytes of a file; 0 when the file does not start with an ID3v2 tag.static tagLength = (b) => {if (b.length < 10) { return 0 }if (b[0] != 73 || b[1] != 68 || b[2] != 51) { return 0 }if (b[3] == 255 || b[6] >= 128 || b[7] >= 128 || b[8] >= 128 || b[9] >= 128) { return 0 }return 10 + synchsafe(b, 6) + ((b[3] == 4 && (b[5] & 16) != 0) ? 10 : 0)}static idOf = (b, i, n) => { return decodeLatin1(b, i, i + n) }static isFrameId = (b, i, n) => {let k = 0while (k < n) {c = b[i + k]if (!((c >= 65 && c <= 90) || (c >= 48 && c <= 57))) { return false }k = k + 1}return true}// POPM: owner (Latin-1, zero terminated), rating (1 byte), counter (big endian, any length, may be missing)static popmOf = (b, lo, hi) => {end = terminatorAt(0, b, lo, hi)if (end + 1 >= hi) { return null }return { owner = decodeLatin1(b, lo, end) rating = b[end + 1] count = bigEndian(b, end + 2, hi) }}// APIC / PIC: encoding, mime (v2.2: 3 letter format), picture type, description (encoded, terminated), then the picturestatic picOf = (b, lo, hi, isV22) => {if (hi - lo < 6) { return null }enc = b[lo]let at = lo + 1let mime = ''if (isV22) {fmt = decodeLatin1(b, at, at + 3).toLowerCase()mime = fmt == 'png' ? 'image/png' : 'image/jpeg'at = at + 3} else {e = terminatorAt(0, b, at, hi)mime = decodeLatin1(b, at, e).toLowerCase()at = e + 1}if (at >= hi) { return null }kind = b[at]at = at + 1d = terminatorAt(enc, b, at, hi)at = d + terminatorWidth(enc)if (at >= hi) { return null }// the picture may carry a wrong or missing mime: the first bytes decideif (b[at] == 255 && b[at + 1] == 216) { mime = 'image/jpeg' }else if (b[at] == 137 && b[at + 1] == 80) { mime = 'image/png' }else if (b[at] == 71 && b[at + 1] == 73) { mime = 'image/gif' }else if (mime == '' || mime == 'image/jpg') { mime = 'image/jpeg' }return { mime = mime kind = kind data = b.slice(at, hi) }}// THE READER. `b` starts with the tag (and may hold more). Answers null when there is no ID3v2 tag, else// { version, size, text = { TIT2 = [values] ... }, popm = [{ owner, rating, count }], pcnt = Number | null,// cover = { mime, kind, data } | null }// A frame that cannot be read is skipped; a damaged tag ends the walk but keeps what was read before it.static readId3v2 = (b) => {total = tagLength(b)if (total == 0) { return null }v = b[3]if (v < 2 || v > 4) { return null }flags = b[5]let end = total < b.length ? total : b.lengthif (v == 4 && (flags & 16) != 0) { end = end - 10 }let body = blet at = 10// version 2.2 and 2.3 unsynchronise the whole tag (2.4 does it per frame, or for all frames with the same flag)if ((flags & 128) != 0 && v < 4) {body = undoUnsync(b, 10, end)at = 0end = body.length}if ((flags & 64) != 0 && v == 3 && at + 4 <= end) {at = at + 4 + bigEndian(body, at, at + 4)} else if ((flags & 64) != 0 && v == 4 && at + 4 <= end) {at = at + synchsafe(body, at)}idLen = v == 2 ? 3 : 4hl = v == 2 ? 6 : 10text = {}popm = []let pcnt = nulllet cover = nulllet coverRank = 0while (at + hl <= end) {if (body[at] == 0) { break }if (!isFrameId(body, at, idLen)) { break }let id = idOf(body, at, idLen)let size = 0if (v == 2) { size = bigEndian(body, at + 3, at + 6) }else if (v == 3) { size = bigEndian(body, at + 4, at + 8) }else { size = synchsafe(body, at + 4) }let lo = at + hllet hi = lo + sizeat = hiif (hi > end) { hi = end }if (lo >= hi) { continue }let frameBytes = bodylet skip = falseif (v == 3) {f = body[lo - 10 + 9]if ((f & 128) != 0 || (f & 64) != 0) { skip = true }if ((f & 32) != 0) { lo = lo + 1 }} else if (v == 4) {f = body[lo - 10 + 9]if ((f & 12) != 0) { skip = true }if ((f & 64) != 0) { lo = lo + 1 }if ((f & 1) != 0) { lo = lo + 4 }if (!skip && ((f & 2) != 0 || (flags & 128) != 0)) {frameBytes = undoUnsync(body, lo, hi)lo = 0hi = frameBytes.length}}if (skip || lo >= hi) { continue }let name = idif (v == 2) {m = v22Names[id]if (m != null) { name = m }}if (name == 'POPM') {p = popmOf(frameBytes, lo, hi)if (p != null) { popm.push(p) }} else if (name == 'PCNT') {pcnt = bigEndian(frameBytes, lo, hi)} else if (name == 'APIC') {pic = picOf(frameBytes, lo, hi, v == 2)if (pic != null) {rank = pic.kind == 3 ? 2 : 1if (rank > coverRank) {cover = piccoverRank = rank}}} else if (wantedText[name] == true) {vals = textValues(frameBytes, lo, hi)if (text[name] == null && vals.length > 0) { text[name] = vals }}}return { version = v size = total text = text popm = popm pcnt = pcnt cover = cover }}// THE ID3v1 BLOCK: the last 128 bytes of a file ("TAG", title 30, artist 30, album 30, year 4, comment 30, genre 1;// a zero byte before the last byte of the comment makes the next byte the track number). null when it is not there.static readId3v1 = (b) => {if (b.length < 128) { return null }o = b.length - 128if (b[o] != 84 || b[o + 1] != 65 || b[o + 2] != 71) { return null }field = (a, n) => {let e = a + nwhile (e > a && (b[e - 1] == 0 || b[e - 1] == 32)) { e = e - 1 }let z = awhile (z < e && b[z] != 0) { z = z + 1 }return decodeText(0, b, a, z).trim()}let track = nullif (b[o + 125] == 0 && b[o + 126] != 0) { track = b[o + 126] }return { title = field(o + 3, 30) artist = field(o + 33, 30) album = field(o + 63, 30) year = field(o + 93, 4) track = track genre = b[o + 127] }}
Branches
Latest commits
- 1838d7e8Records: S1 startedmre
- a43fca3fRecords: mission 001 decided pointsmre
- 41773828Records: statusmre
- a82c0805Records: mission 001 brief (Style form)mre
- 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