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
- d56e5054Sync theme and shared tokens from layouts 17e0f03mre
- b1362547Re-vendor to Hybriel set day35mre
- 89e10f08Records: mission 002 rollback wordingmre
- 9485dee7Records: mission 002 brief (live step)mre
- 9ba68fe7Records: S1b reviewedmre
- 193dfff1Records: S1 result, S1b startedmre
- 028ed776Records: mission 001 continuation S1bmre
- 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