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 '' }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
- 95f5faae|||PR jukebox#7 Drop needless let declarationsmre
- 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