module.audio.vqf.php 5.9 KB

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