id3-generator.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * Helper functions for creating ID3 metadata.
  3. */
  4. 'use strict';
  5. var stringToInts, stringToCString, id3Tag, id3Frame;
  6. stringToInts = function(string) {
  7. var result = [], i;
  8. for (i = 0; i < string.length; i++) {
  9. result[i] = string.charCodeAt(i);
  10. }
  11. return result;
  12. };
  13. stringToCString = function(string) {
  14. return stringToInts(string).concat([0x00]);
  15. };
  16. id3Tag = function() {
  17. var
  18. frames = Array.prototype.concat.apply([], Array.prototype.slice.call(arguments)),
  19. result = stringToInts('ID3').concat([
  20. 0x03, 0x00, // version 3.0 of ID3v2 (aka ID3v.2.3.0)
  21. 0x40, // flags. include an extended header
  22. 0x00, 0x00, 0x00, 0x00, // size. set later
  23. // extended header
  24. 0x00, 0x00, 0x00, 0x06, // extended header size. no CRC
  25. 0x00, 0x00, // extended flags
  26. 0x00, 0x00, 0x00, 0x02 // size of padding
  27. ], frames),
  28. size;
  29. // size is stored as a sequence of four 7-bit integers with the
  30. // high bit of each byte set to zero
  31. size = result.length - 10;
  32. result[6] = (size >>> 21) & 0x7f;
  33. result[7] = (size >>> 14) & 0x7f;
  34. result[8] = (size >>> 7) & 0x7f;
  35. result[9] = size & 0x7f;
  36. return result;
  37. };
  38. id3Frame = function(type) {
  39. var result = stringToInts(type).concat([
  40. 0x00, 0x00, 0x00, 0x00, // size
  41. 0xe0, 0x00 // flags. tag/file alter preservation, read-only
  42. ]),
  43. size = result.length - 10;
  44. // append the fields of the ID3 frame
  45. result = result.concat.apply(result, Array.prototype.slice.call(arguments, 1));
  46. // set the size
  47. size = result.length - 10;
  48. result[4] = (size >>> 21) & 0x7f;
  49. result[5] = (size >>> 14) & 0x7f;
  50. result[6] = (size >>> 7) & 0x7f;
  51. result[7] = size & 0x7f;
  52. return result;
  53. };
  54. module.exports = {
  55. stringToInts: stringToInts,
  56. stringToCString: stringToCString,
  57. id3Tag: id3Tag,
  58. id3Frame: id3Frame
  59. };