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
- 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
- 89cdabb1Vendor Hybriel day41 (57054d9e): plugin dirs, set .somre
- 1fecd6a7Merge branch 'records-day39' into mainJukebox
- d6b3e068|||PR Records: day39 live (PR #2), mission 011 PR step, statusmre