module.audio.vqf.php 6.0 KB

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