jukebox
All repositories: gitoria
7.5 KB
// lib/id3-helpers.hl — the noise of the ID3 reader (lib/id3.hl): bytes to text in every ID3 encoding, the synchsafe// numbers, undoing unsynchronisation, the genre names. Statics only. Everything works on a Bytes and byte offsets.// a code point as a one-character text; a surrogate or a value past U+10FFFF becomes U+FFFDstatic charOf = (cp) => {if (cp < 0 || cp > 1114111 || (cp >= 55296 && cp <= 57343)) { return String.fromCharCode(65533) }return String.fromCharCode(cp)}// Latin-1: every byte is the character of the same numberstatic decodeLatin1 = (b, lo, to) => {let out = ''let i = lowhile (i < to) {out = out + String.fromCharCode(b[i])i = i + 1}return out}// UTF-8, strict: null when the bytes are not valid UTF-8 (so the caller can fall back to Latin-1)static decodeUtf8Strict = (b, lo, to) => {let out = ''let i = lowhile (i < to) {c = b[i]if (c < 128) {out = out + String.fromCharCode(c)i = i + 1} else {let need = 0let cp = 0if (c >= 194 && c <= 223) {need = 1cp = c - 192}else if (c >= 224 && c <= 239) {need = 2cp = c - 224}else if (c >= 240 && c <= 244) {need = 3cp = c - 240}else { return null }if (i + need > to - 1) { return null }let k = 1while (k <= need) {d = b[i + k]if (d < 128 || d > 191) { return null }cp = cp * 64 + (d - 128)k = k + 1}out = out + charOf(cp)i = i + need + 1}}return out}// encoding 0 says Latin-1, but many taggers wrote UTF-8 into it: valid UTF-8 with high bytes is taken as UTF-8static decodeLatin1OrUtf8 = (b, lo, to) => {let high = falselet i = lowhile (i < to) {if (b[i] >= 128) {high = truebreak}i = i + 1}if (!high) { return decodeLatin1(b, lo, to) }u = decodeUtf8Strict(b, lo, to)return u == null ? decodeLatin1(b, lo, to) : u}// UTF-16 with a byte order mark (or `bigDefault` when there is none); surrogate pairs are joinedstatic decodeUtf16 = (b, lo, to, bigDefault) => {let big = bigDefaultlet i = loif (to - lo >= 2) {if (b[lo] == 255 && b[lo + 1] == 254) {big = falsei = lo + 2}else if (b[lo] == 254 && b[lo + 1] == 255) {big = truei = lo + 2}}let out = ''while (i + 1 < to) {let u = big ? b[i] * 256 + b[i + 1] : b[i + 1] * 256 + b[i]i = i + 2if (u >= 55296 && u <= 56319 && i + 1 < to) {let w = big ? b[i] * 256 + b[i + 1] : b[i + 1] * 256 + b[i]if (w >= 56320 && w <= 57343) {u = 65536 + (u - 55296) * 1024 + (w - 56320)i = i + 2}}out = out + charOf(u)}return out}// text of an ID3 text encoding byte (0 Latin-1, 1 UTF-16 with BOM, 2 UTF-16BE, 3 UTF-8) over [lo, to)static decodeText = (enc, b, lo, to) => {if (to <= lo) { return '' }if (enc == 1) { return decodeUtf16(b, lo, to, false) }if (enc == 2) { return decodeUtf16(b, lo, to, true) }if (enc == 3) {u = decodeUtf8Strict(b, lo, to)return u == null ? decodeLatin1(b, lo, to) : u}return decodeLatin1OrUtf8(b, lo, to)}// where a terminated text ends: the index of its terminator (one zero byte, or two aligned zero bytes in UTF-16), or `to`static terminatorAt = (enc, b, lo, to) => {let i = loif (enc == 1 || enc == 2) {while (i + 1 < to) {if (b[i] == 0 && b[i + 1] == 0) { return i }i = i + 2}return to}while (i < to) {if (b[i] == 0) { return i }i = i + 1}return to}// the width of the terminator of an encodingstatic terminatorWidth = (enc) => { return (enc == 1 || enc == 2) ? 2 : 1 }// all the values of a text frame body (a list: ID3v2.4 separates several values with a zero); empty ones are droppedstatic textValues = (b, lo, to) => {out = []if (to <= lo) { return out }enc = b[lo]let start = lo + 1w = terminatorWidth(enc)while (start < to) {end = terminatorAt(enc, b, start, to)t = decodeText(enc, b, start, end).trim()if (t != '') { out.push(t) }start = end + w}return out}// ---- numbers ----------------------------------------------------------------------------------------------static synchsafe = (b, i) => { return b[i] * 2097152 + b[i + 1] * 16384 + b[i + 2] * 128 + b[i + 3] }static bigEndian = (b, lo, to) => {let n = 0let i = lowhile (i < to) {n = n * 256 + b[i]i = i + 1}return n}// UNSYNCHRONISATION undone: every FF 00 becomes FF. Answers a new Bytes.static undoUnsync = (b, lo, to) => {out = toBytes(0)let i = lolet runStart = lowhile (i < to) {if (b[i] == 255 && i + 1 < to && b[i + 1] == 0) {out.append(b.slice(runStart, i + 1))i = i + 2runStart = i} else {i = i + 1}}if (runStart < to) { out.append(b.slice(runStart, to)) }return out}// ---- genres: the ID3v1 numbers, with the Winamp extensions ---------------------------------------------------static genreNames = ['Blues', 'Classic Rock', 'Country', 'Dance', 'Disco', 'Funk', 'Grunge', 'Hip-Hop', 'Jazz', 'Metal','New Age', 'Oldies', 'Other', 'Pop', 'R&B', 'Rap', 'Reggae', 'Rock', 'Techno', 'Industrial','Alternative', 'Ska', 'Death Metal', 'Pranks', 'Soundtrack', 'Euro-Techno', 'Ambient', 'Trip-Hop', 'Vocal', 'Jazz+Funk','Fusion', 'Trance', 'Classical', 'Instrumental', 'Acid', 'House', 'Game', 'Sound Clip', 'Gospel', 'Noise','AlternRock', 'Bass', 'Soul', 'Punk', 'Space', 'Meditative', 'Instrumental Pop', 'Instrumental Rock', 'Ethnic', 'Gothic','Darkwave', 'Techno-Industrial', 'Electronic', 'Pop-Folk', 'Eurodance', 'Dream', 'Southern Rock', 'Comedy', 'Cult', 'Gangsta','Top 40', 'Christian Rap', 'Pop/Funk', 'Jungle', 'Native American', 'Cabaret', 'New Wave', 'Psychedelic', 'Rave', 'Showtunes','Trailer', 'Lo-Fi', 'Tribal', 'Acid Punk', 'Acid Jazz', 'Polka', 'Retro', 'Musical', 'Rock & Roll', 'Hard Rock','Folk', 'Folk-Rock', 'National Folk', 'Swing', 'Fast Fusion', 'Bebop', 'Latin', 'Revival', 'Celtic', 'Bluegrass','Avantgarde', 'Gothic Rock', 'Progressive Rock', 'Psychedelic Rock', 'Symphonic Rock', 'Slow Rock', 'Big Band', 'Chorus', 'Easy Listening', 'Acoustic','Humour', 'Speech', 'Chanson', 'Opera', 'Chamber Music', 'Sonata', 'Symphony', 'Booty Bass', 'Primus', 'Porn Groove','Satire', 'Slow Jam', 'Club', 'Tango', 'Samba', 'Folklore', 'Ballad', 'Power Ballad', 'Rhythmic Soul', 'Freestyle','Duet', 'Punk Rock', 'Drum Solo', 'A Cappella', 'Euro-House', 'Dance Hall', 'Goa', 'Drum & Bass', 'Club-House', 'Hardcore','Terror', 'Indie', 'BritPop', 'Negerpunk', 'Polsk Punk', 'Beat', 'Christian Gangsta Rap', 'Heavy Metal', 'Black Metal', 'Crossover','Contemporary Christian', 'Christian Rock', 'Merengue', 'Salsa', 'Thrash Metal', 'Anime', 'Jpop', 'Synthpop']// a genre as the tag holds it — "(13)", "13", "(13)Pop", "(17)(13)", "(RX)", "Rock" — as a name; '' stays ''static genreName = (raw) => {if (raw == null) { return '' }let g = raw.trim()if (g == '') { return '' }if (g == '(RX)' || g == 'RX') { return 'Remix' }if (g == '(CR)' || g == 'CR') { return 'Cover' }// "(13)" or "(13)Pop": the number between the brackets, the text after them wins when there is oneif (g.startsWith('(')) {close = g.indexOf(')')if (close > 1) {rest = g.slice(close + 1).trim()if (rest != '' && !rest.startsWith('(')) { return rest }num = g.slice(1, close)n = digitsValue(num)if (n != null && n < genreNames.length) { return genreNames[n] }return num}}n = digitsValue(g)if (n != null) { return n < genreNames.length ? genreNames[n] : g }return g}// a text of digits only as a Number, else nullstatic digitsValue = (s) => {if (s == null || s.length == 0) { return null }let n = 0let i = 0while (i < s.length) {c = s.charCodeAt(i)if (c < 48 || c > 57) { return null }n = n * 10 + (c - 48)i = i + 1}return n}
Branches
Latest commits
- 6dcaeb46Records: resetKey planmre
- c5dbf2ecRecords: R2 resultmre
- 956069b8Records: R1 result, mission 007mre
- 3c8a2e12Mission 007: #20 sort scrollmre
- deb8cef5Records: mission 006 startedmre
- 4f0ad42fMission 006: jukebox#20mre
- 800cae06Records: #21 planmre
- 64bc7823Records: #21 causemre
- 76639655Records: B1 questionmre
- d7906b36Records: mission 005 startedmre
- d3df773cMission 005: doc linemre
- 6a5d840aMission 005: jukebox#22mre
- 13e5e748Records: mission 004 startedmre
- a8098271Mission 004: jukebox#21mre
- df8f5205Records: jukebox#19-21, #11 closedmre
- b7a4e692Records: hybriel#298mre
- 9bcbb017Records: day38b waits for day39 fixmre
- f80504e1Records: day38b repro sentmre
- 8048258bRecords: day38b jb3 regression, L1 Chromes stoppedmre
- 68aace7fRecords: mission 003 day38bmre