module.audio.tta.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at http://getid3.sourceforge.net //
  5. // or http://www.getid3.org //
  6. /////////////////////////////////////////////////////////////////
  7. // See readme.txt for more details //
  8. /////////////////////////////////////////////////////////////////
  9. // //
  10. // module.audio.tta.php //
  11. // module for analyzing TTA Audio files //
  12. // dependencies: NONE //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. class getid3_tta
  16. {
  17. function getid3_tta(&$fd, &$ThisFileInfo) {
  18. $ThisFileInfo['fileformat'] = 'tta';
  19. $ThisFileInfo['audio']['dataformat'] = 'tta';
  20. $ThisFileInfo['audio']['lossless'] = true;
  21. $ThisFileInfo['audio']['bitrate_mode'] = 'vbr';
  22. fseek($fd, $ThisFileInfo['avdataoffset'], SEEK_SET);
  23. $ttaheader = fread($fd, 26);
  24. $ThisFileInfo['tta']['magic'] = substr($ttaheader, 0, 3);
  25. if ($ThisFileInfo['tta']['magic'] != 'TTA') {
  26. $ThisFileInfo['error'][] = 'Expecting "TTA" at offset '.$ThisFileInfo['avdataoffset'].', found "'.$ThisFileInfo['tta']['magic'].'"';
  27. unset($ThisFileInfo['fileformat']);
  28. unset($ThisFileInfo['audio']);
  29. unset($ThisFileInfo['tta']);
  30. return false;
  31. }
  32. switch ($ttaheader{3}) {
  33. case "\x01": // TTA v1.x
  34. case "\x02": // TTA v1.x
  35. case "\x03": // TTA v1.x
  36. // "It was the demo-version of the TTA encoder. There is no released format with such header. TTA encoder v1 is not supported about a year."
  37. $ThisFileInfo['tta']['major_version'] = 1;
  38. $ThisFileInfo['avdataoffset'] += 16;
  39. $ThisFileInfo['tta']['compression_level'] = ord($ttaheader{3});
  40. $ThisFileInfo['tta']['channels'] = getid3_lib::LittleEndian2Int(substr($ttaheader, 4, 2));
  41. $ThisFileInfo['tta']['bits_per_sample'] = getid3_lib::LittleEndian2Int(substr($ttaheader, 6, 2));
  42. $ThisFileInfo['tta']['sample_rate'] = getid3_lib::LittleEndian2Int(substr($ttaheader, 8, 4));
  43. $ThisFileInfo['tta']['samples_per_channel'] = getid3_lib::LittleEndian2Int(substr($ttaheader, 12, 4));
  44. $ThisFileInfo['audio']['encoder_options'] = '-e'.$ThisFileInfo['tta']['compression_level'];
  45. $ThisFileInfo['playtime_seconds'] = $ThisFileInfo['tta']['samples_per_channel'] / $ThisFileInfo['tta']['sample_rate'];
  46. break;
  47. case '2': // TTA v2.x
  48. // "I have hurried to release the TTA 2.0 encoder. Format documentation is removed from our site. This format still in development. Please wait the TTA2 format, encoder v4."
  49. $ThisFileInfo['tta']['major_version'] = 2;
  50. $ThisFileInfo['avdataoffset'] += 20;
  51. $ThisFileInfo['tta']['compression_level'] = getid3_lib::LittleEndian2Int(substr($ttaheader, 4, 2));
  52. $ThisFileInfo['tta']['audio_format'] = getid3_lib::LittleEndian2Int(substr($ttaheader, 6, 2));
  53. $ThisFileInfo['tta']['channels'] = getid3_lib::LittleEndian2Int(substr($ttaheader, 8, 2));
  54. $ThisFileInfo['tta']['bits_per_sample'] = getid3_lib::LittleEndian2Int(substr($ttaheader, 10, 2));
  55. $ThisFileInfo['tta']['sample_rate'] = getid3_lib::LittleEndian2Int(substr($ttaheader, 12, 4));
  56. $ThisFileInfo['tta']['data_length'] = getid3_lib::LittleEndian2Int(substr($ttaheader, 16, 4));
  57. $ThisFileInfo['audio']['encoder_options'] = '-e'.$ThisFileInfo['tta']['compression_level'];
  58. $ThisFileInfo['playtime_seconds'] = $ThisFileInfo['tta']['data_length'] / $ThisFileInfo['tta']['sample_rate'];
  59. break;
  60. case '1': // TTA v3.x
  61. // "This is a first stable release of the TTA format. It will be supported by the encoders v3 or higher."
  62. $ThisFileInfo['tta']['major_version'] = 3;
  63. $ThisFileInfo['avdataoffset'] += 26;
  64. $ThisFileInfo['tta']['audio_format'] = getid3_lib::LittleEndian2Int(substr($ttaheader, 4, 2)); // getid3_riff::RIFFwFormatTagLookup()
  65. $ThisFileInfo['tta']['channels'] = getid3_lib::LittleEndian2Int(substr($ttaheader, 6, 2));
  66. $ThisFileInfo['tta']['bits_per_sample'] = getid3_lib::LittleEndian2Int(substr($ttaheader, 8, 2));
  67. $ThisFileInfo['tta']['sample_rate'] = getid3_lib::LittleEndian2Int(substr($ttaheader, 10, 4));
  68. $ThisFileInfo['tta']['data_length'] = getid3_lib::LittleEndian2Int(substr($ttaheader, 14, 4));
  69. $ThisFileInfo['tta']['crc32_footer'] = substr($ttaheader, 18, 4);
  70. $ThisFileInfo['tta']['seek_point'] = getid3_lib::LittleEndian2Int(substr($ttaheader, 22, 4));
  71. $ThisFileInfo['playtime_seconds'] = $ThisFileInfo['tta']['data_length'] / $ThisFileInfo['tta']['sample_rate'];
  72. break;
  73. default:
  74. $ThisFileInfo['error'][] = 'This version of getID3() only knows how to handle TTA v1 and v2 - it may not work correctly with this file which appears to be TTA v'.$ttaheader{3};
  75. return false;
  76. break;
  77. }
  78. $ThisFileInfo['audio']['encoder'] = 'TTA v'.$ThisFileInfo['tta']['major_version'];
  79. $ThisFileInfo['audio']['bits_per_sample'] = $ThisFileInfo['tta']['bits_per_sample'];
  80. $ThisFileInfo['audio']['sample_rate'] = $ThisFileInfo['tta']['sample_rate'];
  81. $ThisFileInfo['audio']['channels'] = $ThisFileInfo['tta']['channels'];
  82. $ThisFileInfo['audio']['bitrate'] = (($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']) * 8) / $ThisFileInfo['playtime_seconds'];
  83. return true;
  84. }
  85. }
  86. ?>