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
- f7368ea3Records: mission 011 day39, waits for lisbethmre
- 9515d2cdRecords: jb20 livemre
- 019f6601ui gate: comment names the open bug components#27 (workaround, not fixed)mre
- 277a2722README: provenance linemre
- 4169ca6aREADME: sync pinmre
- a01f3aa8ui gate: back to the top before the sort checks (the screen reopens where it was left)mre
- f7936c20Lists keep their scroll position across a reload (jukebox#20): per-list offsets in storage/ui.json, gate jb20mre
- d67906devirtual-list and icons from components.hybriel e5b98ec (centerId, scrollTop, onScroll)mre
- df7920ceRecords: R3 startedmre
- 0549f038Mission 010: jb20 livemre
- 273494e2STATUS rewrittenmre
- 227c9b96Records: jb19 livemre
- 9f365376README: the bar's controls (jb19)mre
- d18851fdjb19: pressed switches look different (whole selector), queue column cannot widen past the panel, gates follow the new bar (jb3, jb11, jb15)mre
- 82b54c0ejb19: bar Shuffle · Previous · Play · Next · Repeat, no 10 s buttons; Shuffle switch (shuffle from the playing song, back to the list), Repeat off/all/one, both remembered in ui.json; Shuffle my favourites an ordinary button; gate tests/jb19.mjsmre
- ba354710jb19: icons pinned to components.hybriel 46fefaa (shuffle, repeat, repeatOne, prevTrack, nextTrack)mre
- d8e8d059jb19 step 2: media-player from components.hybriel c96709c (skip, repeat options; the end of a song only calls onPlay), sync-components.sh and SOURCE point to it, jb11 counts media loads and metadata writes (from jb21 d3a026e)mre
- 7b048c1bRecords: jb19 done, L2 live stepmre
- 665185aeMission 009: jb19 livemre
- 47b2d3beRecords: P1 resumedmre