module.audio.bonk.php 8.9 KB

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