module.audio.vqf.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.vqf.php //
  12. // module for analyzing VQF audio files //
  13. // dependencies: NONE //
  14. // ///
  15. /////////////////////////////////////////////////////////////////
  16. class getid3_vqf extends getid3_handler
  17. {
  18. public function Analyze() {
  19. $info = &$this->getid3->info;
  20. // based loosely on code from TTwinVQ by Jurgen Faul <jfaulØgmx*de>
  21. // http://jfaul.de/atl or http://j-faul.virtualave.net/atl/atl.html
  22. $info['fileformat'] = 'vqf';
  23. $info['audio']['dataformat'] = 'vqf';
  24. $info['audio']['bitrate_mode'] = 'cbr';
  25. $info['audio']['lossless'] = false;
  26. // shortcut
  27. $info['vqf']['raw'] = array();
  28. $thisfile_vqf = &$info['vqf'];
  29. $thisfile_vqf_raw = &$thisfile_vqf['raw'];
  30. $this->fseek($info['avdataoffset']);
  31. $VQFheaderData = $this->fread(16);
  32. $offset = 0;
  33. $thisfile_vqf_raw['header_tag'] = substr($VQFheaderData, $offset, 4);
  34. $magic = 'TWIN';
  35. if ($thisfile_vqf_raw['header_tag'] != $magic) {
  36. $info['error'][] = 'Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($thisfile_vqf_raw['header_tag']).'"';
  37. unset($info['vqf']);
  38. unset($info['fileformat']);
  39. return false;
  40. }
  41. $offset += 4;
  42. $thisfile_vqf_raw['version'] = substr($VQFheaderData, $offset, 8);
  43. $offset += 8;
  44. $thisfile_vqf_raw['size'] = getid3_lib::BigEndian2Int(substr($VQFheaderData, $offset, 4));
  45. $offset += 4;
  46. while ($this->ftell() < $info['avdataend']) {
  47. $ChunkBaseOffset = $this->ftell();
  48. $chunkoffset = 0;
  49. $ChunkData = $this->fread(8);
  50. $ChunkName = substr($ChunkData, $chunkoffset, 4);
  51. if ($ChunkName == 'DATA') {
  52. $info['avdataoffset'] = $ChunkBaseOffset;
  53. break;
  54. }
  55. $chunkoffset += 4;
  56. $ChunkSize = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
  57. $chunkoffset += 4;
  58. if ($ChunkSize > ($info['avdataend'] - $this->ftell())) {
  59. $info['error'][] = 'Invalid chunk size ('.$ChunkSize.') for chunk "'.$ChunkName.'" at offset '.$ChunkBaseOffset;
  60. break;
  61. }
  62. if ($ChunkSize > 0) {
  63. $ChunkData .= $this->fread($ChunkSize);
  64. }
  65. switch ($ChunkName) {
  66. case 'COMM':
  67. // shortcut
  68. $thisfile_vqf['COMM'] = array();
  69. $thisfile_vqf_COMM = &$thisfile_vqf['COMM'];
  70. $thisfile_vqf_COMM['channel_mode'] = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
  71. $chunkoffset += 4;
  72. $thisfile_vqf_COMM['bitrate'] = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
  73. $chunkoffset += 4;
  74. $thisfile_vqf_COMM['sample_rate'] = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
  75. $chunkoffset += 4;
  76. $thisfile_vqf_COMM['security_level'] = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
  77. $chunkoffset += 4;
  78. $info['audio']['channels'] = $thisfile_vqf_COMM['channel_mode'] + 1;
  79. $info['audio']['sample_rate'] = $this->VQFchannelFrequencyLookup($thisfile_vqf_COMM['sample_rate']);
  80. $info['audio']['bitrate'] = $thisfile_vqf_COMM['bitrate'] * 1000;
  81. $info['audio']['encoder_options'] = 'CBR' . ceil($info['audio']['bitrate']/1000);
  82. if ($info['audio']['bitrate'] == 0) {
  83. $info['error'][] = 'Corrupt VQF file: bitrate_audio == zero';
  84. return false;
  85. }
  86. break;
  87. case 'NAME':
  88. case 'AUTH':
  89. case '(c) ':
  90. case 'FILE':
  91. case 'COMT':
  92. case 'ALBM':
  93. $thisfile_vqf['comments'][$this->VQFcommentNiceNameLookup($ChunkName)][] = trim(substr($ChunkData, 8));
  94. break;
  95. case 'DSIZ':
  96. $thisfile_vqf['DSIZ'] = getid3_lib::BigEndian2Int(substr($ChunkData, 8, 4));
  97. break;
  98. default:
  99. $info['warning'][] = 'Unhandled chunk type "'.$ChunkName.'" at offset '.$ChunkBaseOffset;
  100. break;
  101. }
  102. }
  103. $info['playtime_seconds'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['audio']['bitrate'];
  104. if (isset($thisfile_vqf['DSIZ']) && (($thisfile_vqf['DSIZ'] != ($info['avdataend'] - $info['avdataoffset'] - strlen('DATA'))))) {
  105. switch ($thisfile_vqf['DSIZ']) {
  106. case 0:
  107. case 1:
  108. $info['warning'][] = 'Invalid DSIZ value "'.$thisfile_vqf['DSIZ'].'". This is known to happen with VQF files encoded by Ahead Nero, and seems to be its way of saying this is TwinVQF v'.($thisfile_vqf['DSIZ'] + 1).'.0';
  109. $info['audio']['encoder'] = 'Ahead Nero';
  110. break;
  111. default:
  112. $info['warning'][] = 'Probable corrupted file - should be '.$thisfile_vqf['DSIZ'].' bytes, actually '.($info['avdataend'] - $info['avdataoffset'] - strlen('DATA'));
  113. break;
  114. }
  115. }
  116. return true;
  117. }
  118. public function VQFchannelFrequencyLookup($frequencyid) {
  119. static $VQFchannelFrequencyLookup = array(
  120. 11 => 11025,
  121. 22 => 22050,
  122. 44 => 44100
  123. );
  124. return (isset($VQFchannelFrequencyLookup[$frequencyid]) ? $VQFchannelFrequencyLookup[$frequencyid] : $frequencyid * 1000);
  125. }
  126. public function VQFcommentNiceNameLookup($shortname) {
  127. static $VQFcommentNiceNameLookup = array(
  128. 'NAME' => 'title',
  129. 'AUTH' => 'artist',
  130. '(c) ' => 'copyright',
  131. 'FILE' => 'filename',
  132. 'COMT' => 'comment',
  133. 'ALBM' => 'album'
  134. );
  135. return (isset($VQFcommentNiceNameLookup[$shortname]) ? $VQFcommentNiceNameLookup[$shortname] : $shortname);
  136. }
  137. }