module.audio.tta.php 5.3 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. // also https://github.com/JamesHeinrich/getID3 //
  7. /////////////////////////////////////////////////////////////////
  8. // See readme.txt for more details //
  9. /////////////////////////////////////////////////////////////////
  10. // //
  11. // module.audio.tta.php //
  12. // module for analyzing TTA Audio files //
  13. // dependencies: NONE //
  14. // ///
  15. /////////////////////////////////////////////////////////////////
  16. class getid3_tta extends getid3_handler
  17. {
  18. public function Analyze() {
  19. $info = &$this->getid3->info;
  20. $info['fileformat'] = 'tta';
  21. $info['audio']['dataformat'] = 'tta';
  22. $info['audio']['lossless'] = true;
  23. $info['audio']['bitrate_mode'] = 'vbr';
  24. $this->fseek($info['avdataoffset']);
  25. $ttaheader = $this->fread(26);
  26. $info['tta']['magic'] = substr($ttaheader, 0, 3);
  27. $magic = 'TTA';
  28. if ($info['tta']['magic'] != $magic) {
  29. $info['error'][] = 'Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($info['tta']['magic']).'"';
  30. unset($info['fileformat']);
  31. unset($info['audio']);
  32. unset($info['tta']);
  33. return false;
  34. }
  35. switch ($ttaheader{3}) {
  36. case "\x01": // TTA v1.x
  37. case "\x02": // TTA v1.x
  38. case "\x03": // TTA v1.x
  39. // "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."
  40. $info['tta']['major_version'] = 1;
  41. $info['avdataoffset'] += 16;
  42. $info['tta']['compression_level'] = ord($ttaheader{3});
  43. $info['tta']['channels'] = getid3_lib::LittleEndian2Int(substr($ttaheader, 4, 2));
  44. $info['tta']['bits_per_sample'] = getid3_lib::LittleEndian2Int(substr($ttaheader, 6, 2));
  45. $info['tta']['sample_rate'] = getid3_lib::LittleEndian2Int(substr($ttaheader, 8, 4));
  46. $info['tta']['samples_per_channel'] = getid3_lib::LittleEndian2Int(substr($ttaheader, 12, 4));
  47. $info['audio']['encoder_options'] = '-e'.$info['tta']['compression_level'];
  48. $info['playtime_seconds'] = $info['tta']['samples_per_channel'] / $info['tta']['sample_rate'];
  49. break;
  50. case '2': // TTA v2.x
  51. // "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."
  52. $info['tta']['major_version'] = 2;
  53. $info['avdataoffset'] += 20;
  54. $info['tta']['compression_level'] = getid3_lib::LittleEndian2Int(substr($ttaheader, 4, 2));
  55. $info['tta']['audio_format'] = getid3_lib::LittleEndian2Int(substr($ttaheader, 6, 2));
  56. $info['tta']['channels'] = getid3_lib::LittleEndian2Int(substr($ttaheader, 8, 2));
  57. $info['tta']['bits_per_sample'] = getid3_lib::LittleEndian2Int(substr($ttaheader, 10, 2));
  58. $info['tta']['sample_rate'] = getid3_lib::LittleEndian2Int(substr($ttaheader, 12, 4));
  59. $info['tta']['data_length'] = getid3_lib::LittleEndian2Int(substr($ttaheader, 16, 4));
  60. $info['audio']['encoder_options'] = '-e'.$info['tta']['compression_level'];
  61. $info['playtime_seconds'] = $info['tta']['data_length'] / $info['tta']['sample_rate'];
  62. break;
  63. case '1': // TTA v3.x
  64. // "This is a first stable release of the TTA format. It will be supported by the encoders v3 or higher."
  65. $info['tta']['major_version'] = 3;
  66. $info['avdataoffset'] += 26;
  67. $info['tta']['audio_format'] = getid3_lib::LittleEndian2Int(substr($ttaheader, 4, 2)); // getid3_riff::wFormatTagLookup()
  68. $info['tta']['channels'] = getid3_lib::LittleEndian2Int(substr($ttaheader, 6, 2));
  69. $info['tta']['bits_per_sample'] = getid3_lib::LittleEndian2Int(substr($ttaheader, 8, 2));
  70. $info['tta']['sample_rate'] = getid3_lib::LittleEndian2Int(substr($ttaheader, 10, 4));
  71. $info['tta']['data_length'] = getid3_lib::LittleEndian2Int(substr($ttaheader, 14, 4));
  72. $info['tta']['crc32_footer'] = substr($ttaheader, 18, 4);
  73. $info['tta']['seek_point'] = getid3_lib::LittleEndian2Int(substr($ttaheader, 22, 4));
  74. $info['playtime_seconds'] = $info['tta']['data_length'] / $info['tta']['sample_rate'];
  75. break;
  76. default:
  77. $info['error'][] = 'This version of getID3() ['.$this->getid3->version().'] 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};
  78. return false;
  79. break;
  80. }
  81. $info['audio']['encoder'] = 'TTA v'.$info['tta']['major_version'];
  82. $info['audio']['bits_per_sample'] = $info['tta']['bits_per_sample'];
  83. $info['audio']['sample_rate'] = $info['tta']['sample_rate'];
  84. $info['audio']['channels'] = $info['tta']['channels'];
  85. $info['audio']['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];
  86. return true;
  87. }
  88. }