gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit956069b8956069b8Records: R1 result, mission 007mre956069b8/lib/id3-helpers.hl

7.5 KB

  1. // lib/id3-helpers.hl — the noise of the ID3 reader (lib/id3.hl): bytes to text in every ID3 encoding, the synchsafe
  2. // numbers, undoing unsynchronisation, the genre names. Statics only. Everything works on a Bytes and byte offsets.
  3. // a code point as a one-character text; a surrogate or a value past U+10FFFF becomes U+FFFD
  4. static charOf = (cp) => {
  5. if (cp < 0 || cp > 1114111 || (cp >= 55296 && cp <= 57343)) { return String.fromCharCode(65533) }
  6. return String.fromCharCode(cp)
  7. }
  8. // Latin-1: every byte is the character of the same number
  9. static decodeLatin1 = (b, lo, to) => {
  10. let out = ''
  11. let i = lo
  12. while (i < to) {
  13. out = out + String.fromCharCode(b[i])
  14. i = i + 1
  15. }
  16. return out
  17. }
  18. // UTF-8, strict: null when the bytes are not valid UTF-8 (so the caller can fall back to Latin-1)
  19. static decodeUtf8Strict = (b, lo, to) => {
  20. let out = ''
  21. let i = lo
  22. while (i < to) {
  23. c = b[i]
  24. if (c < 128) {
  25. out = out + String.fromCharCode(c)
  26. i = i + 1
  27. } else {
  28. let need = 0
  29. let cp = 0
  30. if (c >= 194 && c <= 223) {
  31. need = 1
  32. cp = c - 192
  33. }
  34. else if (c >= 224 && c <= 239) {
  35. need = 2
  36. cp = c - 224
  37. }
  38. else if (c >= 240 && c <= 244) {
  39. need = 3
  40. cp = c - 240
  41. }
  42. else { return null }
  43. if (i + need > to - 1) { return null }
  44. let k = 1
  45. while (k <= need) {
  46. d = b[i + k]
  47. if (d < 128 || d > 191) { return null }
  48. cp = cp * 64 + (d - 128)
  49. k = k + 1
  50. }
  51. out = out + charOf(cp)
  52. i = i + need + 1
  53. }
  54. }
  55. return out
  56. }
  57. // encoding 0 says Latin-1, but many taggers wrote UTF-8 into it: valid UTF-8 with high bytes is taken as UTF-8
  58. static decodeLatin1OrUtf8 = (b, lo, to) => {
  59. let high = false
  60. let i = lo
  61. while (i < to) {
  62. if (b[i] >= 128) {
  63. high = true
  64. break
  65. }
  66. i = i + 1
  67. }
  68. if (!high) { return decodeLatin1(b, lo, to) }
  69. u = decodeUtf8Strict(b, lo, to)
  70. return u == null ? decodeLatin1(b, lo, to) : u
  71. }
  72. // UTF-16 with a byte order mark (or `bigDefault` when there is none); surrogate pairs are joined
  73. static decodeUtf16 = (b, lo, to, bigDefault) => {
  74. let big = bigDefault
  75. let i = lo
  76. if (to - lo >= 2) {
  77. if (b[lo] == 255 && b[lo + 1] == 254) {
  78. big = false
  79. i = lo + 2
  80. }
  81. else if (b[lo] == 254 && b[lo + 1] == 255) {
  82. big = true
  83. i = lo + 2
  84. }
  85. }
  86. let out = ''
  87. while (i + 1 < to) {
  88. let u = big ? b[i] * 256 + b[i + 1] : b[i + 1] * 256 + b[i]
  89. i = i + 2
  90. if (u >= 55296 && u <= 56319 && i + 1 < to) {
  91. let w = big ? b[i] * 256 + b[i + 1] : b[i + 1] * 256 + b[i]
  92. if (w >= 56320 && w <= 57343) {
  93. u = 65536 + (u - 55296) * 1024 + (w - 56320)
  94. i = i + 2
  95. }
  96. }
  97. out = out + charOf(u)
  98. }
  99. return out
  100. }
  101. // text of an ID3 text encoding byte (0 Latin-1, 1 UTF-16 with BOM, 2 UTF-16BE, 3 UTF-8) over [lo, to)
  102. static decodeText = (enc, b, lo, to) => {
  103. if (to <= lo) { return '' }
  104. if (enc == 1) { return decodeUtf16(b, lo, to, false) }
  105. if (enc == 2) { return decodeUtf16(b, lo, to, true) }
  106. if (enc == 3) {
  107. u = decodeUtf8Strict(b, lo, to)
  108. return u == null ? decodeLatin1(b, lo, to) : u
  109. }
  110. return decodeLatin1OrUtf8(b, lo, to)
  111. }
  112. // where a terminated text ends: the index of its terminator (one zero byte, or two aligned zero bytes in UTF-16), or `to`
  113. static terminatorAt = (enc, b, lo, to) => {
  114. let i = lo
  115. if (enc == 1 || enc == 2) {
  116. while (i + 1 < to) {
  117. if (b[i] == 0 && b[i + 1] == 0) { return i }
  118. i = i + 2
  119. }
  120. return to
  121. }
  122. while (i < to) {
  123. if (b[i] == 0) { return i }
  124. i = i + 1
  125. }
  126. return to
  127. }
  128. // the width of the terminator of an encoding
  129. static terminatorWidth = (enc) => { return (enc == 1 || enc == 2) ? 2 : 1 }
  130. // all the values of a text frame body (a list: ID3v2.4 separates several values with a zero); empty ones are dropped
  131. static textValues = (b, lo, to) => {
  132. out = []
  133. if (to <= lo) { return out }
  134. enc = b[lo]
  135. let start = lo + 1
  136. w = terminatorWidth(enc)
  137. while (start < to) {
  138. end = terminatorAt(enc, b, start, to)
  139. t = decodeText(enc, b, start, end).trim()
  140. if (t != '') { out.push(t) }
  141. start = end + w
  142. }
  143. return out
  144. }
  145. // ---- numbers ----------------------------------------------------------------------------------------------
  146. static synchsafe = (b, i) => { return b[i] * 2097152 + b[i + 1] * 16384 + b[i + 2] * 128 + b[i + 3] }
  147. static bigEndian = (b, lo, to) => {
  148. let n = 0
  149. let i = lo
  150. while (i < to) {
  151. n = n * 256 + b[i]
  152. i = i + 1
  153. }
  154. return n
  155. }
  156. // UNSYNCHRONISATION undone: every FF 00 becomes FF. Answers a new Bytes.
  157. static undoUnsync = (b, lo, to) => {
  158. out = toBytes(0)
  159. let i = lo
  160. let runStart = lo
  161. while (i < to) {
  162. if (b[i] == 255 && i + 1 < to && b[i + 1] == 0) {
  163. out.append(b.slice(runStart, i + 1))
  164. i = i + 2
  165. runStart = i
  166. } else {
  167. i = i + 1
  168. }
  169. }
  170. if (runStart < to) { out.append(b.slice(runStart, to)) }
  171. return out
  172. }
  173. // ---- genres: the ID3v1 numbers, with the Winamp extensions ---------------------------------------------------
  174. static genreNames = [
  175. 'Blues', 'Classic Rock', 'Country', 'Dance', 'Disco', 'Funk', 'Grunge', 'Hip-Hop', 'Jazz', 'Metal',
  176. 'New Age', 'Oldies', 'Other', 'Pop', 'R&B', 'Rap', 'Reggae', 'Rock', 'Techno', 'Industrial',
  177. 'Alternative', 'Ska', 'Death Metal', 'Pranks', 'Soundtrack', 'Euro-Techno', 'Ambient', 'Trip-Hop', 'Vocal', 'Jazz+Funk',
  178. 'Fusion', 'Trance', 'Classical', 'Instrumental', 'Acid', 'House', 'Game', 'Sound Clip', 'Gospel', 'Noise',
  179. 'AlternRock', 'Bass', 'Soul', 'Punk', 'Space', 'Meditative', 'Instrumental Pop', 'Instrumental Rock', 'Ethnic', 'Gothic',
  180. 'Darkwave', 'Techno-Industrial', 'Electronic', 'Pop-Folk', 'Eurodance', 'Dream', 'Southern Rock', 'Comedy', 'Cult', 'Gangsta',
  181. 'Top 40', 'Christian Rap', 'Pop/Funk', 'Jungle', 'Native American', 'Cabaret', 'New Wave', 'Psychedelic', 'Rave', 'Showtunes',
  182. 'Trailer', 'Lo-Fi', 'Tribal', 'Acid Punk', 'Acid Jazz', 'Polka', 'Retro', 'Musical', 'Rock & Roll', 'Hard Rock',
  183. 'Folk', 'Folk-Rock', 'National Folk', 'Swing', 'Fast Fusion', 'Bebop', 'Latin', 'Revival', 'Celtic', 'Bluegrass',
  184. 'Avantgarde', 'Gothic Rock', 'Progressive Rock', 'Psychedelic Rock', 'Symphonic Rock', 'Slow Rock', 'Big Band', 'Chorus', 'Easy Listening', 'Acoustic',
  185. 'Humour', 'Speech', 'Chanson', 'Opera', 'Chamber Music', 'Sonata', 'Symphony', 'Booty Bass', 'Primus', 'Porn Groove',
  186. 'Satire', 'Slow Jam', 'Club', 'Tango', 'Samba', 'Folklore', 'Ballad', 'Power Ballad', 'Rhythmic Soul', 'Freestyle',
  187. 'Duet', 'Punk Rock', 'Drum Solo', 'A Cappella', 'Euro-House', 'Dance Hall', 'Goa', 'Drum & Bass', 'Club-House', 'Hardcore',
  188. 'Terror', 'Indie', 'BritPop', 'Negerpunk', 'Polsk Punk', 'Beat', 'Christian Gangsta Rap', 'Heavy Metal', 'Black Metal', 'Crossover',
  189. 'Contemporary Christian', 'Christian Rock', 'Merengue', 'Salsa', 'Thrash Metal', 'Anime', 'Jpop', 'Synthpop'
  190. ]
  191. // a genre as the tag holds it — "(13)", "13", "(13)Pop", "(17)(13)", "(RX)", "Rock" — as a name; '' stays ''
  192. static genreName = (raw) => {
  193. if (raw == null) { return '' }
  194. let g = raw.trim()
  195. if (g == '') { return '' }
  196. if (g == '(RX)' || g == 'RX') { return 'Remix' }
  197. if (g == '(CR)' || g == 'CR') { return 'Cover' }
  198. // "(13)" or "(13)Pop": the number between the brackets, the text after them wins when there is one
  199. if (g.startsWith('(')) {
  200. close = g.indexOf(')')
  201. if (close > 1) {
  202. rest = g.slice(close + 1).trim()
  203. if (rest != '' && !rest.startsWith('(')) { return rest }
  204. num = g.slice(1, close)
  205. n = digitsValue(num)
  206. if (n != null && n < genreNames.length) { return genreNames[n] }
  207. return num
  208. }
  209. }
  210. n = digitsValue(g)
  211. if (n != null) { return n < genreNames.length ? genreNames[n] : g }
  212. return g
  213. }
  214. // a text of digits only as a Number, else null
  215. static digitsValue = (s) => {
  216. if (s == null || s.length == 0) { return null }
  217. let n = 0
  218. let i = 0
  219. while (i < s.length) {
  220. c = s.charCodeAt(i)
  221. if (c < 48 || c > 57) { return null }
  222. n = n * 10 + (c - 48)
  223. i = i + 1
  224. }
  225. return n
  226. }

Branches

Latest commits

  • 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
  • d105d8a9Records: mpackdb integrity check cleanmre
  • 6e980c53Records: mpackdb rule refinementmre