module.audio.bonk.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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.la.php //
  11. // module for analyzing BONK audio files //
  12. // dependencies: module.tag.id3v2.php (optional) //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. class getid3_bonk extends getid3_handler
  16. {
  17. function Analyze() {
  18. $info = &$this->getid3->info;
  19. // shortcut
  20. $info['bonk'] = array();
  21. $thisfile_bonk = &$info['bonk'];
  22. $thisfile_bonk['dataoffset'] = $info['avdataoffset'];
  23. $thisfile_bonk['dataend'] = $info['avdataend'];
  24. if (!getid3_lib::intValueSupported($thisfile_bonk['dataend'])) {
  25. $info['warning'][] = 'Unable to parse BONK file from end (v0.6+ preferred method) because PHP filesystem functions only support up to '.round(PHP_INT_MAX / 1073741824).'GB';
  26. } else {
  27. // scan-from-end method, for v0.6 and higher
  28. fseek($this->getid3->fp, $thisfile_bonk['dataend'] - 8, SEEK_SET);
  29. $PossibleBonkTag = fread($this->getid3->fp, 8);
  30. while ($this->BonkIsValidTagName(substr($PossibleBonkTag, 4, 4), true)) {
  31. $BonkTagSize = getid3_lib::LittleEndian2Int(substr($PossibleBonkTag, 0, 4));
  32. fseek($this->getid3->fp, 0 - $BonkTagSize, SEEK_CUR);
  33. $BonkTagOffset = ftell($this->getid3->fp);
  34. $TagHeaderTest = fread($this->getid3->fp, 5);
  35. if (($TagHeaderTest{0} != "\x00") || (substr($PossibleBonkTag, 4, 4) != strtolower(substr($PossibleBonkTag, 4, 4)))) {
  36. $info['error'][] = 'Expecting "'.getid3_lib::PrintHexBytes("\x00".strtoupper(substr($PossibleBonkTag, 4, 4))).'" at offset '.$BonkTagOffset.', found "'.getid3_lib::PrintHexBytes($TagHeaderTest).'"';
  37. return false;
  38. }
  39. $BonkTagName = substr($TagHeaderTest, 1, 4);
  40. $thisfile_bonk[$BonkTagName]['size'] = $BonkTagSize;
  41. $thisfile_bonk[$BonkTagName]['offset'] = $BonkTagOffset;
  42. $this->HandleBonkTags($BonkTagName);
  43. $NextTagEndOffset = $BonkTagOffset - 8;
  44. if ($NextTagEndOffset < $thisfile_bonk['dataoffset']) {
  45. if (empty($info['audio']['encoder'])) {
  46. $info['audio']['encoder'] = 'Extended BONK v0.9+';
  47. }
  48. return true;
  49. }
  50. fseek($this->getid3->fp, $NextTagEndOffset, SEEK_SET);
  51. $PossibleBonkTag = fread($this->getid3->fp, 8);
  52. }
  53. }
  54. // seek-from-beginning method for v0.4 and v0.5
  55. if (empty($thisfile_bonk['BONK'])) {
  56. fseek($this->getid3->fp, $thisfile_bonk['dataoffset'], SEEK_SET);
  57. do {
  58. $TagHeaderTest = fread($this->getid3->fp, 5);
  59. switch ($TagHeaderTest) {
  60. case "\x00".'BONK':
  61. if (empty($info['audio']['encoder'])) {
  62. $info['audio']['encoder'] = 'BONK v0.4';
  63. }
  64. break;
  65. case "\x00".'INFO':
  66. $info['audio']['encoder'] = 'Extended BONK v0.5';
  67. break;
  68. default:
  69. break 2;
  70. }
  71. $BonkTagName = substr($TagHeaderTest, 1, 4);
  72. $thisfile_bonk[$BonkTagName]['size'] = $thisfile_bonk['dataend'] - $thisfile_bonk['dataoffset'];
  73. $thisfile_bonk[$BonkTagName]['offset'] = $thisfile_bonk['dataoffset'];
  74. $this->HandleBonkTags($BonkTagName);
  75. } while (true);
  76. }
  77. // parse META block for v0.6 - v0.8
  78. if (empty($thisfile_bonk['INFO']) && isset($thisfile_bonk['META']['tags']['info'])) {
  79. fseek($this->getid3->fp, $thisfile_bonk['META']['tags']['info'], SEEK_SET);
  80. $TagHeaderTest = fread($this->getid3->fp, 5);
  81. if ($TagHeaderTest == "\x00".'INFO') {
  82. $info['audio']['encoder'] = 'Extended BONK v0.6 - v0.8';
  83. $BonkTagName = substr($TagHeaderTest, 1, 4);
  84. $thisfile_bonk[$BonkTagName]['size'] = $thisfile_bonk['dataend'] - $thisfile_bonk['dataoffset'];
  85. $thisfile_bonk[$BonkTagName]['offset'] = $thisfile_bonk['dataoffset'];
  86. $this->HandleBonkTags($BonkTagName);
  87. }
  88. }
  89. if (empty($info['audio']['encoder'])) {
  90. $info['audio']['encoder'] = 'Extended BONK v0.9+';
  91. }
  92. if (empty($thisfile_bonk['BONK'])) {
  93. unset($info['bonk']);
  94. }
  95. return true;
  96. }
  97. function HandleBonkTags($BonkTagName) {
  98. $info = &$this->getid3->info;
  99. switch ($BonkTagName) {
  100. case 'BONK':
  101. // shortcut
  102. $thisfile_bonk_BONK = &$info['bonk']['BONK'];
  103. $BonkData = "\x00".'BONK'.fread($this->getid3->fp, 17);
  104. $thisfile_bonk_BONK['version'] = getid3_lib::LittleEndian2Int(substr($BonkData, 5, 1));
  105. $thisfile_bonk_BONK['number_samples'] = getid3_lib::LittleEndian2Int(substr($BonkData, 6, 4));
  106. $thisfile_bonk_BONK['sample_rate'] = getid3_lib::LittleEndian2Int(substr($BonkData, 10, 4));
  107. $thisfile_bonk_BONK['channels'] = getid3_lib::LittleEndian2Int(substr($BonkData, 14, 1));
  108. $thisfile_bonk_BONK['lossless'] = (bool) getid3_lib::LittleEndian2Int(substr($BonkData, 15, 1));
  109. $thisfile_bonk_BONK['joint_stereo'] = (bool) getid3_lib::LittleEndian2Int(substr($BonkData, 16, 1));
  110. $thisfile_bonk_BONK['number_taps'] = getid3_lib::LittleEndian2Int(substr($BonkData, 17, 2));
  111. $thisfile_bonk_BONK['downsampling_ratio'] = getid3_lib::LittleEndian2Int(substr($BonkData, 19, 1));
  112. $thisfile_bonk_BONK['samples_per_packet'] = getid3_lib::LittleEndian2Int(substr($BonkData, 20, 2));
  113. $info['avdataoffset'] = $thisfile_bonk_BONK['offset'] + 5 + 17;
  114. $info['avdataend'] = $thisfile_bonk_BONK['offset'] + $thisfile_bonk_BONK['size'];
  115. $info['fileformat'] = 'bonk';
  116. $info['audio']['dataformat'] = 'bonk';
  117. $info['audio']['bitrate_mode'] = 'vbr'; // assumed
  118. $info['audio']['channels'] = $thisfile_bonk_BONK['channels'];
  119. $info['audio']['sample_rate'] = $thisfile_bonk_BONK['sample_rate'];
  120. $info['audio']['channelmode'] = ($thisfile_bonk_BONK['joint_stereo'] ? 'joint stereo' : 'stereo');
  121. $info['audio']['lossless'] = $thisfile_bonk_BONK['lossless'];
  122. $info['audio']['codec'] = 'bonk';
  123. $info['playtime_seconds'] = $thisfile_bonk_BONK['number_samples'] / ($thisfile_bonk_BONK['sample_rate'] * $thisfile_bonk_BONK['channels']);
  124. if ($info['playtime_seconds'] > 0) {
  125. $info['audio']['bitrate'] = (($info['bonk']['dataend'] - $info['bonk']['dataoffset']) * 8) / $info['playtime_seconds'];
  126. }
  127. break;
  128. case 'INFO':
  129. // shortcut
  130. $thisfile_bonk_INFO = &$info['bonk']['INFO'];
  131. $thisfile_bonk_INFO['version'] = getid3_lib::LittleEndian2Int(fread($this->getid3->fp, 1));
  132. $thisfile_bonk_INFO['entries_count'] = 0;
  133. $NextInfoDataPair = fread($this->getid3->fp, 5);
  134. if (!$this->BonkIsValidTagName(substr($NextInfoDataPair, 1, 4))) {
  135. while (!feof($this->getid3->fp)) {
  136. //$CurrentSeekInfo['offset'] = getid3_lib::LittleEndian2Int(substr($NextInfoDataPair, 0, 4));
  137. //$CurrentSeekInfo['nextbit'] = getid3_lib::LittleEndian2Int(substr($NextInfoDataPair, 4, 1));
  138. //$thisfile_bonk_INFO[] = $CurrentSeekInfo;
  139. $NextInfoDataPair = fread($this->getid3->fp, 5);
  140. if ($this->BonkIsValidTagName(substr($NextInfoDataPair, 1, 4))) {
  141. fseek($this->getid3->fp, -5, SEEK_CUR);
  142. break;
  143. }
  144. $thisfile_bonk_INFO['entries_count']++;
  145. }
  146. }
  147. break;
  148. case 'META':
  149. $BonkData = "\x00".'META'.fread($this->getid3->fp, $info['bonk']['META']['size'] - 5);
  150. $info['bonk']['META']['version'] = getid3_lib::LittleEndian2Int(substr($BonkData, 5, 1));
  151. $MetaTagEntries = floor(((strlen($BonkData) - 8) - 6) / 8); // BonkData - xxxxmeta - ØMETA
  152. $offset = 6;
  153. for ($i = 0; $i < $MetaTagEntries; $i++) {
  154. $MetaEntryTagName = substr($BonkData, $offset, 4);
  155. $offset += 4;
  156. $MetaEntryTagOffset = getid3_lib::LittleEndian2Int(substr($BonkData, $offset, 4));
  157. $offset += 4;
  158. $info['bonk']['META']['tags'][$MetaEntryTagName] = $MetaEntryTagOffset;
  159. }
  160. break;
  161. case ' ID3':
  162. $info['audio']['encoder'] = 'Extended BONK v0.9+';
  163. // ID3v2 checking is optional
  164. if (class_exists('getid3_id3v2')) {
  165. $getid3_temp = new getID3();
  166. $getid3_temp->openfile($this->getid3->filename);
  167. $getid3_id3v2 = new getid3_id3v2($getid3_temp);
  168. $getid3_id3v2->StartingOffset = $info['bonk'][' ID3']['offset'] + 2;
  169. $info['bonk'][' ID3']['valid'] = $getid3_id3v2->Analyze();
  170. if ($info['bonk'][' ID3']['valid']) {
  171. $info['id3v2'] = $getid3_temp->info['id3v2'];
  172. }
  173. unset($getid3_temp, $getid3_id3v2);
  174. }
  175. break;
  176. default:
  177. $info['warning'][] = 'Unexpected Bonk tag "'.$BonkTagName.'" at offset '.$info['bonk'][$BonkTagName]['offset'];
  178. break;
  179. }
  180. }
  181. static function BonkIsValidTagName($PossibleBonkTag, $ignorecase=false) {
  182. static $BonkIsValidTagName = array('BONK', 'INFO', ' ID3', 'META');
  183. foreach ($BonkIsValidTagName as $validtagname) {
  184. if ($validtagname == $PossibleBonkTag) {
  185. return true;
  186. } elseif ($ignorecase && (strtolower($validtagname) == strtolower($PossibleBonkTag))) {
  187. return true;
  188. }
  189. }
  190. return false;
  191. }
  192. }
  193. ?>