gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitf56cd7c7f56cd7c7Sync icons from 8176eb1 and verify mixed component sourcesmref56cd7c7/lib/mpeg.hl

2.4 KB

  1. // lib/mpeg.hl — the duration of an mp3 from its first audio frame: the Xing/Info or VBRI header when there is one
  2. // (variable bit rate), else the bit rate of the first frame and the length of the audio (constant bit rate).
  3. // Input: a Bytes that holds the first audio frame, where to start looking, and how many bytes of audio the file has.
  4. static bitrates = {
  5. '1-1' = [0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448]
  6. '1-2' = [0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384]
  7. '1-3' = [0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320]
  8. '2-1' = [0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256]
  9. '2-2' = [0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160]
  10. '2-3' = [0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160]
  11. }
  12. static sampleRates = { '3' = [44100, 48000, 32000] '2' = [22050, 24000, 16000] '0' = [11025, 12000, 8000] }
  13. static hasText = (b, at, t) => {
  14. let k = 0
  15. while (k < t.length) {
  16. if (at + k >= b.length || b[at + k] != t.charCodeAt(k)) { return false }
  17. k = k + 1
  18. }
  19. return true
  20. }
  21. // seconds (whole), or null when no valid frame header is found within 4096 bytes from `start`
  22. static mp3Seconds = (b, start, audioBytes) => {
  23. let i = start
  24. let stop = start + 4096
  25. if (stop > b.length - 4) { stop = b.length - 4 }
  26. while (i < stop) {
  27. if (b[i] == 255 && (b[i + 1] & 224) == 224) {
  28. vbits = (b[i + 1] >> 3) & 3
  29. lbits = (b[i + 1] >> 1) & 3
  30. bidx = (b[i + 2] >> 4) & 15
  31. sidx = (b[i + 2] >> 2) & 3
  32. if (vbits != 1 && lbits != 0 && bidx != 0 && bidx != 15 && sidx != 3) {
  33. layer = 4 - lbits
  34. mpeg1 = vbits == 3
  35. table = bitrates[(mpeg1 ? '1' : '2') + '-' + layer]
  36. kbps = table[bidx]
  37. rate = sampleRates['' + vbits][sidx]
  38. let spf = 1152
  39. if (layer == 1) { spf = 384 }
  40. else if (layer == 3 && !mpeg1) { spf = 576 }
  41. mono = ((b[i + 3] >> 6) & 3) == 3
  42. side = mpeg1 ? (mono ? 17 : 32) : (mono ? 9 : 17)
  43. x = i + 4 + side
  44. if (hasText(b, x, 'Xing') || hasText(b, x, 'Info')) {
  45. if ((b[x + 7] & 1) != 0) {
  46. frames = b[x + 8] * 16777216 + b[x + 9] * 65536 + b[x + 10] * 256 + b[x + 11]
  47. return frames * spf / rate
  48. }
  49. }
  50. if (hasText(b, i + 36, 'VBRI')) {
  51. frames = b[i + 36 + 14] * 16777216 + b[i + 36 + 15] * 65536 + b[i + 36 + 16] * 256 + b[i + 36 + 17]
  52. return frames * spf / rate
  53. }
  54. return audioBytes * 8 / (kbps * 1000)
  55. }
  56. }
  57. i = i + 1
  58. }
  59. return null
  60. }

Branches

Latest commits

  • f56cd7c7Sync icons from 8176eb1 and verify mixed component sourcesmre
  • d56e5054Sync theme and shared tokens from layouts 17e0f03mre
  • b1362547Re-vendor to Hybriel set day35mre
  • 89e10f08Records: mission 002 rollback wordingmre
  • 9485dee7Records: mission 002 brief (live step)mre
  • 9ba68fe7Records: S1b reviewedmre
  • 193dfff1Records: S1 result, S1b startedmre
  • 028ed776Records: mission 001 continuation S1bmre
  • 1838d7e8Records: S1 startedmre
  • a43fca3fRecords: mission 001 decided pointsmre
  • 41773828Records: statusmre
  • a82c0805Records: mission 001 brief (Style form)mre
  • 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