gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit0bb027da0bb027daconductor: caramboleyo is the user, not owner/creatorApollo0bb027da/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

  • 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
  • d6b3e068|||PR Records: day39 live (PR #2), mission 011 PR step, statusmre
  • 5fa19e89Merge branch 'day39' into mainJukebox
  • cfe95440|||PR day39 re-vendor: Hybriel 51e921d7 (bin 6b481aa7), ids kebab-case since hybriel#256: quoted state rule and tests use jb-shuffle / jb-repeatmre
  • f3614c9bRe-vendor to Hybriel day39 (51e921d7, bin 6b481aa7)mre
  • 9f5c6532Merge branch 'status-22-pr' into mainJukebox
  • 5838d6c7|||PR STATUS: #22 pending decision, PR-only rulemre
  • 29d2029cRecords: PR-only rule (Kunibert)mre
  • adacb0abRecords: neutral ticket rule, #22 decision recordedmre
  • b2d6f121Records: #308 hold liftedmre
  • 7ab67533Records: day39 on hold (hybriel#308)mre
  • 22f21739Records: note rebase before mergemre
  • f7368ea3Records: mission 011 day39, waits for lisbethmre
  • 9515d2cdRecords: jb20 livemre
  • 019f6601ui gate: comment names the open bug components#27 (workaround, not fixed)mre