gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchmain10f8a8c8Merge branch 'code-order' into mainJukeboxmain/tests/make-fixtures.mjs

6.6 KB

  1. // tests/make-fixtures.mjs — builds the tiny test mp3s of tests/fixtures: ffmpeg makes one second of audio, the ID3 tags are
  2. // built by hand here, byte by byte, so every case of the reader is covered (v2.2, v2.3, v2.4, extended header,
  3. // unsynchronisation, several POPM owners, PCNT, APIC, ID3v1 only, no tags). Run: node tests/make-fixtures.mjs
  4. import { execFileSync } from 'node:child_process'
  5. import { mkdirSync, writeFileSync, readFileSync, rmSync } from 'node:fs'
  6. import { dirname, join } from 'node:path'
  7. import { fileURLToPath } from 'node:url'
  8. const here = dirname(fileURLToPath(import.meta.url))
  9. const out = join(here, 'fixtures')
  10. rmSync(out, { recursive: true, force: true })
  11. mkdirSync(join(out, 'withcover'), { recursive: true })
  12. const tmp = join(out, '.tmp')
  13. mkdirSync(tmp, { recursive: true })
  14. const ff = (...a) => execFileSync('ffmpeg', ['-v', 'error', '-y', ...a])
  15. ff('-f', 'lavfi', '-i', 'sine=frequency=440:duration=1', '-c:a', 'libmp3lame', '-b:a', '64k', '-write_xing', '0', '-id3v2_version', '0', '-write_id3v1', '0', join(tmp, 'audio.mp3'))
  16. ff('-f', 'lavfi', '-i', 'color=c=red:s=16x16:d=1', '-frames:v', '1', join(tmp, 'cover.png'))
  17. ff('-f', 'lavfi', '-i', 'color=c=blue:s=16x16:d=1', '-frames:v', '1', join(tmp, 'folder.jpg'))
  18. const audio = readFileSync(join(tmp, 'audio.mp3'))
  19. const png = readFileSync(join(tmp, 'cover.png'))
  20. const folderJpg = readFileSync(join(tmp, 'folder.jpg'))
  21. const u32 = (n) => Buffer.from([(n >>> 24) & 255, (n >>> 16) & 255, (n >>> 8) & 255, n & 255])
  22. const synch = (n) => Buffer.from([(n >>> 21) & 127, (n >>> 14) & 127, (n >>> 7) & 127, n & 127])
  23. const latin = (s) => Buffer.from(s, 'latin1')
  24. const utf16 = (s) => Buffer.concat([Buffer.from([0xff, 0xfe]), Buffer.from(s, 'utf16le')])
  25. const unsync = (b) => { const o = []; for (let i = 0; i < b.length; i++) { o.push(b[i]); if (b[i] === 0xff && (i + 1 === b.length || b[i + 1] === 0 || (b[i + 1] & 0xe0) === 0xe0)) o.push(0) } return Buffer.from(o) }
  26. // a frame of version 2 (3-letter id, 3-byte size), 3 or 4
  27. const frame = (v, id, body, flags = 0) => {
  28. if (v === 2) return Buffer.concat([latin(id), Buffer.from([(body.length >> 16) & 255, (body.length >> 8) & 255, body.length & 255]), body])
  29. const size = v === 4 ? synch(body.length) : u32(body.length)
  30. return Buffer.concat([latin(id), size, Buffer.from([0, flags]), body])
  31. }
  32. const text = (v, id, s, enc = 0) => {
  33. const data = enc === 0 ? latin(s) : enc === 1 ? utf16(s) : Buffer.from(s, 'utf8')
  34. return frame(v, id, Buffer.concat([Buffer.from([enc]), data]))
  35. }
  36. const popm = (v, id, owner, rating, count) => frame(v, id, Buffer.concat([latin(owner), Buffer.from([0, rating]), count === null ? Buffer.alloc(0) : u32(count)]))
  37. const tag = (v, frames, { flags = 0, ext = null, unsyncAll = false, pad = 40 } = {}) => {
  38. let body = Buffer.concat([...(ext ? [ext] : []), ...frames])
  39. if (unsyncAll) body = unsync(body)
  40. body = Buffer.concat([body, Buffer.alloc(pad)])
  41. return Buffer.concat([latin('ID3'), Buffer.from([v, 0, flags]), synch(body.length), body])
  42. }
  43. const put = (path, ...parts) => writeFileSync(join(out, path), Buffer.concat(parts))
  44. // 1. v2.3, Latin-1 and UTF-16 texts, genre "(13)", track "3/12", disc "1/2", two POPM owners (7 and 12) and a PCNT of 9
  45. put('v23-two-owners.mp3', tag(3, [
  46. text(3, 'TIT2', 'Über Song', 1), text(3, 'TPE1', 'Die Ärzte'), text(3, 'TALB', 'Album Eins CD 1'), text(3, 'TRCK', '3/12'),
  47. text(3, 'TPOS', '1/2'), text(3, 'TYER', '1999'), text(3, 'TCON', '(13)'),
  48. popm(3, 'POPM', 'Banshee', 192, 7), popm(3, 'POPM', 'Guayadeque', 128, 12), frame(3, 'PCNT', u32(9)),
  49. ]), audio)
  50. // 2. v2.4, UTF-8, two artists in one frame (zero separated), TDRC with a full date, album artist, a front cover (PNG), Windows Media Player rating
  51. put('v24-cover.mp3', tag(4, [
  52. text(4, 'TIT2', 'Größe Ängste', 3), frame(4, 'TPE1', Buffer.concat([Buffer.from([3]), Buffer.from('Erste Band', 'utf8'), Buffer.from([0]), Buffer.from('Zweite Band', 'utf8')])),
  53. text(4, 'TPE2', 'Sammler', 3), text(4, 'TALB', 'Vierer', 3), text(4, 'TDRC', '2005-06-01', 3), text(4, 'TRCK', '11/21', 3), text(4, 'TCON', '49', 0),
  54. frame(4, 'APIC', Buffer.concat([Buffer.from([0]), latin('image/png'), Buffer.from([0, 3]), latin('front'), Buffer.from([0]), png])),
  55. popm(4, 'POPM', 'Windows Media Player 9 Series', 255, 4),
  56. ]), audio)
  57. // 3. v2.4 with an extended header (flag 0x40, synchsafe size 6, 1 flag byte count, no flags)
  58. put('v24-extheader.mp3', tag(4, [text(4, 'TIT2', 'Mit Erweiterung', 3), text(4, 'TPE1', 'Extender', 3), popm(4, 'POPM', 'Banshee', 0, 3)],
  59. { flags: 0x40, ext: Buffer.concat([synch(6), Buffer.from([1, 0])]) }), audio)
  60. // 4. v2.3 with unsynchronisation on the whole tag: a POPM counter with 0xFF bytes (255) and a UTF-16 title (byte order mark FF FE)
  61. put('v23-unsync.mp3', tag(3, [
  62. text(3, 'TIT2', 'Unsynced', 1), text(3, 'TPE1', 'Sync Band', 1), text(3, 'TALB', 'Sync Album'),
  63. popm(3, 'POPM', 'Banshee', 255, 255), popm(3, 'POPM', 'Guayadeque', 255, 0x00ff00ff),
  64. ], { flags: 0x80, unsyncAll: true }), audio)
  65. // 5. v2.2 frames (TT2 TP1 TAL TRK TYE TCO POP)
  66. put('v22.mp3', tag(2, [
  67. text(2, 'TT2', 'Alt'), text(2, 'TP1', 'Old Timer'), text(2, 'TAL', 'Retro'), text(2, 'TRK', '5/9'), text(2, 'TYE', '1994'), text(2, 'TCO', '(17)'),
  68. popm(2, 'POP', 'old', 64, 21),
  69. ]), audio)
  70. // 6. PCNT only (no POPM)
  71. put('pcnt-only.mp3', tag(3, [text(3, 'TIT2', 'Nur Zähler'), text(3, 'TPE1', 'Counter'), frame(3, 'PCNT', u32(5))]), audio)
  72. // 7. ID3v1 only: title, artist, album, year, comment with a track number, genre 17 (Rock)
  73. const v1 = Buffer.alloc(128); latin('TAG').copy(v1, 0)
  74. latin('Alt Lied').copy(v1, 3); latin('Alte Band').copy(v1, 33); latin('Altes Album').copy(v1, 63); latin('1987').copy(v1, 93); v1[125] = 0; v1[126] = 4; v1[127] = 17
  75. put('v1-only.mp3', audio, v1)
  76. // 8. no tags at all
  77. put('notags.mp3', audio)
  78. // 9. no embedded cover, but a folder.jpg next to it
  79. put('withcover/song-without-picture.mp3', tag(3, [text(3, 'TIT2', 'Ohne Bild'), text(3, 'TPE1', 'Folder Band'), text(3, 'TALB', 'Folder Album')]), audio)
  80. writeFileSync(join(out, 'withcover', 'folder.jpg'), folderJpg)
  81. // 9b. commas in title, artist and album (mpackdb loses an indexed value with a comma after a reopen: the sort keys and album keys must not hold one)
  82. put('comma.mp3', tag(3, [text(3, 'TIT2', 'Eins, zwei'), text(3, 'TPE1', 'Band, The'), text(3, 'TALB', 'Album, mit Komma'), popm(3, 'POPM', 'Banshee', 128, 2)]), audio)
  83. // 10. an ogg file (read with ffprobe) with tags
  84. ff('-f', 'lavfi', '-i', 'sine=frequency=330:duration=1', '-c:a', 'libvorbis', '-metadata', 'title=Vogel', '-metadata', 'artist=Ogg Band', '-metadata', 'album=Ogg Album',
  85. '-metadata', 'date=2011', '-metadata', 'track=2/8', '-metadata', 'genre=Rock', join(out, 'tagged.ogg'))
  86. rmSync(tmp, { recursive: true, force: true })
  87. console.log('fixtures written to ' + out)

Branches

Latest commits

  • 10f8a8c8Merge branch 'code-order' into mainJukebox
  • 204fc828Merge branch 'day72' into mainJukebox
  • 95f5faae|||PR jukebox#7 Drop needless let declarationsmre
  • 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