module.audio.bonk.php 9.1 KB

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