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
- 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
- 980859cbjukebox JB15 (jukebox#15): the now-playing overlay on a short screen (tablet or phone on its side) gave the queue 12 to 100 px because the big cover took the room; the cover now shrinks with the screen height and goes away under 41rem, the queue keeps 3+ rows; gate tests/jb15.mjs at 8 sizesmre
- 9efc40ffjukebox#12 #14: Grok session 1793246574527-181160mre
- 7d58b36ejukebox JB13 (jukebox#13): Next and Previous ask for the songs after / before the current song's id in the current order, not for place + 1; gate tests/jb13.mjs checks Next with the queue down to one song after the order changedmre
- 51358794jukebox JB14 (jukebox#14): the Songs screen opens sorted the way it was left (kept on the server in storage/ui.json), the sort control shows the sort the rows really have (selected options, autocomplete off); gate tests/jb14.mjsmre
- cc68a063jukebox JB13 (jukebox#13): a tapped song plays the song that was tapped, found by its id in the list as it is now (not by its row number, which counted plays move); the queue goes on from its place there, the rows refresh on the tap; gate tests/jb13.mjs (songs by plays, favourites)mre
- 88d3d5c1jukebox JB12b (jukebox#12): the scroll handle is big (2.75rem x 4rem, accent colour, grip) and drawn on every device, no media query; native bar hidden; gate tests/touch.mjs checks the drawn width with and without touchmre
- b58e4495jukebox JB11 (jukebox#11): the next song starts in the ended event on the same audio element, so it also starts with the screen locked; gate tests/jb11.mjs (hidden tab, no frames)mre
- 12a26233jukebox JB12 (jukebox#12): the long lists get a scroll handle a finger can use on touch screens (44 px tap area, slim look, drag through all rows), gate tests/touch.mjsmre
- 0f2157a9jukebox JV1: onto Hybriel set-day27b: readBytes and stat for /stream and the mp3 tags (no dd, head or tail), startup fault exits 1, fs and mpackdb plugins re-vendored; songs with a comma in the title are indexed againmre
- 63d0f2eejukebox ST1 (jukebox#2): styles of main, songs and search moved into their own Style blocks (styles.hl 81 to 8 rules, pixel-identical at 390 and 1280); artist and album wait for an hl:web fixmre
- c5b8fde9jukebox JB3b: keyboard gap repro as a reportmre