gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit33072bb533072bb5|||PR Record day72 gates and Chrome proofmre33072bb5/lib/id3.hl

6.7 KB

  1. // lib/id3.hl — THE ID3v2 READER (versions 2.2, 2.3, 2.4): the tag at the start of an mp3, as plain values.
  2. // Text frames, POPM (every owner), PCNT, the cover picture (APIC), unsynchronisation (whole tag or per frame),
  3. // the extended header, frame flags (grouping, data length), and the ID3v1 block at the end of a file.
  4. // The input is a Bytes that begins with the tag; nothing is written anywhere. The noise (text encodings, genre
  5. // names, synchsafe numbers) is lib/id3-helpers.hl.
  6. import { decodeText, decodeLatin1, terminatorAt, terminatorWidth, textValues, synchsafe, bigEndian, undoUnsync } from './id3-helpers.hl'
  7. // the text frames we keep, by their ID3v2.3/2.4 names; v2.2 names are mapped onto them
  8. static wantedText = {
  9. 'TIT2' = true 'TPE1' = true 'TPE2' = true 'TALB' = true 'TRCK' = true 'TPOS' = true 'TYER' = true
  10. 'TDRC' = true 'TORY' = true 'TDOR' = true 'TCON' = true 'TLEN' = true 'TCOM' = true
  11. }
  12. static v22Names = {
  13. 'TT2' = 'TIT2' 'TP1' = 'TPE1' 'TP2' = 'TPE2' 'TAL' = 'TALB' 'TRK' = 'TRCK' 'TPA' = 'TPOS' 'TYE' = 'TYER'
  14. 'TCO' = 'TCON' 'TLE' = 'TLEN' 'TCM' = 'TCOM' 'POP' = 'POPM' 'CNT' = 'PCNT' 'PIC' = 'APIC'
  15. }
  16. // How long the whole tag is, header included (10 + size, plus the 10-byte footer when the footer flag is set), from
  17. // the first 10 bytes of a file; 0 when the file does not start with an ID3v2 tag.
  18. static tagLength = (b) => {
  19. if (b.length < 10) { return 0 }
  20. if (b[0] != 73 || b[1] != 68 || b[2] != 51) { return 0 }
  21. if (b[3] == 255 || b[6] >= 128 || b[7] >= 128 || b[8] >= 128 || b[9] >= 128) { return 0 }
  22. return 10 + synchsafe(b, 6) + ((b[3] == 4 && (b[5] & 16) != 0) ? 10 : 0)
  23. }
  24. static idOf = (b, i, n) => { return decodeLatin1(b, i, i + n) }
  25. static isFrameId = (b, i, n) => {
  26. let k = 0
  27. while (k < n) {
  28. c = b[i + k]
  29. if (!((c >= 65 && c <= 90) || (c >= 48 && c <= 57))) { return false }
  30. k = k + 1
  31. }
  32. return true
  33. }
  34. // POPM: owner (Latin-1, zero terminated), rating (1 byte), counter (big endian, any length, may be missing)
  35. static popmOf = (b, lo, hi) => {
  36. end = terminatorAt(0, b, lo, hi)
  37. if (end + 1 >= hi) { return null }
  38. return { owner = decodeLatin1(b, lo, end) rating = b[end + 1] count = bigEndian(b, end + 2, hi) }
  39. }
  40. // APIC / PIC: encoding, mime (v2.2: 3 letter format), picture type, description (encoded, terminated), then the picture
  41. static picOf = (b, lo, hi, isV22) => {
  42. if (hi - lo < 6) { return null }
  43. enc = b[lo]
  44. let at = lo + 1
  45. let mime = ''
  46. if (isV22) {
  47. fmt = decodeLatin1(b, at, at + 3).toLowerCase()
  48. mime = fmt == 'png' ? 'image/png' : 'image/jpeg'
  49. at = at + 3
  50. } else {
  51. e = terminatorAt(0, b, at, hi)
  52. mime = decodeLatin1(b, at, e).toLowerCase()
  53. at = e + 1
  54. }
  55. if (at >= hi) { return null }
  56. kind = b[at]
  57. at = at + 1
  58. d = terminatorAt(enc, b, at, hi)
  59. at = d + terminatorWidth(enc)
  60. if (at >= hi) { return null }
  61. // the picture may carry a wrong or missing mime: the first bytes decide
  62. if (b[at] == 255 && b[at + 1] == 216) { mime = 'image/jpeg' }
  63. else if (b[at] == 137 && b[at + 1] == 80) { mime = 'image/png' }
  64. else if (b[at] == 71 && b[at + 1] == 73) { mime = 'image/gif' }
  65. else if (mime == '' || mime == 'image/jpg') { mime = 'image/jpeg' }
  66. return { mime = mime kind = kind data = b.slice(at, hi) }
  67. }
  68. // THE READER. `b` starts with the tag (and may hold more). Answers null when there is no ID3v2 tag, else
  69. // { version, size, text = { TIT2 = [values] ... }, popm = [{ owner, rating, count }], pcnt = Number | null,
  70. // cover = { mime, kind, data } | null }
  71. // A frame that cannot be read is skipped; a damaged tag ends the walk but keeps what was read before it.
  72. static readId3v2 = (b) => {
  73. total = tagLength(b)
  74. if (total == 0) { return null }
  75. v = b[3]
  76. if (v < 2 || v > 4) { return null }
  77. flags = b[5]
  78. let end = total < b.length ? total : b.length
  79. if (v == 4 && (flags & 16) != 0) { end = end - 10 }
  80. let body = b
  81. let at = 10
  82. // version 2.2 and 2.3 unsynchronise the whole tag (2.4 does it per frame, or for all frames with the same flag)
  83. if ((flags & 128) != 0 && v < 4) {
  84. body = undoUnsync(b, 10, end)
  85. at = 0
  86. end = body.length
  87. }
  88. if ((flags & 64) != 0 && v == 3 && at + 4 <= end) {
  89. at = at + 4 + bigEndian(body, at, at + 4)
  90. } else if ((flags & 64) != 0 && v == 4 && at + 4 <= end) {
  91. at = at + synchsafe(body, at)
  92. }
  93. idLen = v == 2 ? 3 : 4
  94. hl = v == 2 ? 6 : 10
  95. text = {}
  96. popm = []
  97. let pcnt = null
  98. let cover = null
  99. let coverRank = 0
  100. while (at + hl <= end) {
  101. if (body[at] == 0) { break }
  102. if (!isFrameId(body, at, idLen)) { break }
  103. let id = idOf(body, at, idLen)
  104. let size = 0
  105. if (v == 2) { size = bigEndian(body, at + 3, at + 6) }
  106. else if (v == 3) { size = bigEndian(body, at + 4, at + 8) }
  107. else { size = synchsafe(body, at + 4) }
  108. let lo = at + hl
  109. let hi = lo + size
  110. at = hi
  111. if (hi > end) { hi = end }
  112. if (lo >= hi) { continue }
  113. let frameBytes = body
  114. let skip = false
  115. if (v == 3) {
  116. f = body[lo - 10 + 9]
  117. if ((f & 128) != 0 || (f & 64) != 0) { skip = true }
  118. if ((f & 32) != 0) { lo = lo + 1 }
  119. } else if (v == 4) {
  120. f = body[lo - 10 + 9]
  121. if ((f & 12) != 0) { skip = true }
  122. if ((f & 64) != 0) { lo = lo + 1 }
  123. if ((f & 1) != 0) { lo = lo + 4 }
  124. if (!skip && ((f & 2) != 0 || (flags & 128) != 0)) {
  125. frameBytes = undoUnsync(body, lo, hi)
  126. lo = 0
  127. hi = frameBytes.length
  128. }
  129. }
  130. if (skip || lo >= hi) { continue }
  131. let name = id
  132. if (v == 2) {
  133. m = v22Names[id]
  134. if (m != null) { name = m }
  135. }
  136. if (name == 'POPM') {
  137. p = popmOf(frameBytes, lo, hi)
  138. if (p != null) { popm.push(p) }
  139. } else if (name == 'PCNT') {
  140. pcnt = bigEndian(frameBytes, lo, hi)
  141. } else if (name == 'APIC') {
  142. pic = picOf(frameBytes, lo, hi, v == 2)
  143. if (pic != null) {
  144. rank = pic.kind == 3 ? 2 : 1
  145. if (rank > coverRank) {
  146. cover = pic
  147. coverRank = rank
  148. }
  149. }
  150. } else if (wantedText[name] == true) {
  151. vals = textValues(frameBytes, lo, hi)
  152. if (text[name] == null && vals.length > 0) { text[name] = vals }
  153. }
  154. }
  155. return { version = v size = total text = text popm = popm pcnt = pcnt cover = cover }
  156. }
  157. // THE ID3v1 BLOCK: the last 128 bytes of a file ("TAG", title 30, artist 30, album 30, year 4, comment 30, genre 1;
  158. // a zero byte before the last byte of the comment makes the next byte the track number). null when it is not there.
  159. static readId3v1 = (b) => {
  160. if (b.length < 128) { return null }
  161. o = b.length - 128
  162. if (b[o] != 84 || b[o + 1] != 65 || b[o + 2] != 71) { return null }
  163. field = (a, n) => {
  164. let e = a + n
  165. while (e > a && (b[e - 1] == 0 || b[e - 1] == 32)) { e = e - 1 }
  166. let z = a
  167. while (z < e && b[z] != 0) { z = z + 1 }
  168. return decodeText(0, b, a, z).trim()
  169. }
  170. let track = null
  171. if (b[o + 125] == 0 && b[o + 126] != 0) { track = b[o + 126] }
  172. return { title = field(o + 3, 30) artist = field(o + 33, 30) album = field(o + 63, 30) year = field(o + 93, 4) track = track genre = b[o + 127] }
  173. }

Branches

Latest commits

  • 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
  • d6b3e068|||PR Records: day39 live (PR #2), mission 011 PR step, statusmre