PageRenderTime 59ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/Vendor/getid3/module.audio.mp3.php

https://bitbucket.org/nova-atlantis/simple-server-media-player
PHP | 2012 lines | 1432 code | 283 blank | 297 comment | 398 complexity | eb87257b2f0507f4ac5591d4226dfc86 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  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.mp3.php //
  12. // module for analyzing MP3 files //
  13. // dependencies: NONE //
  14. // ///
  15. /////////////////////////////////////////////////////////////////
  16. // number of frames to scan to determine if MPEG-audio sequence is valid
  17. // Lower this number to 5-20 for faster scanning
  18. // Increase this number to 50+ for most accurate detection of valid VBR/CBR
  19. // mpeg-audio streams
  20. define('GETID3_MP3_VALID_CHECK_FRAMES', 35);
  21. class getid3_mp3 extends getid3_handler
  22. {
  23. public $allow_bruteforce = false; // forces getID3() to scan the file byte-by-byte and log all the valid audio frame headers - extremely slow, unrecommended, but may provide data from otherwise-unusuable files
  24. public function Analyze() {
  25. $info = &$this->getid3->info;
  26. $initialOffset = $info['avdataoffset'];
  27. if (!$this->getOnlyMPEGaudioInfo($info['avdataoffset'])) {
  28. if ($this->allow_bruteforce) {
  29. $info['error'][] = 'Rescanning file in BruteForce mode';
  30. $this->getOnlyMPEGaudioInfoBruteForce($this->getid3->fp, $info);
  31. }
  32. }
  33. if (isset($info['mpeg']['audio']['bitrate_mode'])) {
  34. $info['audio']['bitrate_mode'] = strtolower($info['mpeg']['audio']['bitrate_mode']);
  35. }
  36. if (((isset($info['id3v2']['headerlength']) && ($info['avdataoffset'] > $info['id3v2']['headerlength'])) || (!isset($info['id3v2']) && ($info['avdataoffset'] > 0) && ($info['avdataoffset'] != $initialOffset)))) {
  37. $synchoffsetwarning = 'Unknown data before synch ';
  38. if (isset($info['id3v2']['headerlength'])) {
  39. $synchoffsetwarning .= '(ID3v2 header ends at '.$info['id3v2']['headerlength'].', then '.($info['avdataoffset'] - $info['id3v2']['headerlength']).' bytes garbage, ';
  40. } elseif ($initialOffset > 0) {
  41. $synchoffsetwarning .= '(should be at '.$initialOffset.', ';
  42. } else {
  43. $synchoffsetwarning .= '(should be at beginning of file, ';
  44. }
  45. $synchoffsetwarning .= 'synch detected at '.$info['avdataoffset'].')';
  46. if (isset($info['audio']['bitrate_mode']) && ($info['audio']['bitrate_mode'] == 'cbr')) {
  47. if (!empty($info['id3v2']['headerlength']) && (($info['avdataoffset'] - $info['id3v2']['headerlength']) == $info['mpeg']['audio']['framelength'])) {
  48. $synchoffsetwarning .= '. This is a known problem with some versions of LAME (3.90-3.92) DLL in CBR mode.';
  49. $info['audio']['codec'] = 'LAME';
  50. $CurrentDataLAMEversionString = 'LAME3.';
  51. } elseif (empty($info['id3v2']['headerlength']) && ($info['avdataoffset'] == $info['mpeg']['audio']['framelength'])) {
  52. $synchoffsetwarning .= '. This is a known problem with some versions of LAME (3.90 - 3.92) DLL in CBR mode.';
  53. $info['audio']['codec'] = 'LAME';
  54. $CurrentDataLAMEversionString = 'LAME3.';
  55. }
  56. }
  57. $info['warning'][] = $synchoffsetwarning;
  58. }
  59. if (isset($info['mpeg']['audio']['LAME'])) {
  60. $info['audio']['codec'] = 'LAME';
  61. if (!empty($info['mpeg']['audio']['LAME']['long_version'])) {
  62. $info['audio']['encoder'] = rtrim($info['mpeg']['audio']['LAME']['long_version'], "\x00");
  63. } elseif (!empty($info['mpeg']['audio']['LAME']['short_version'])) {
  64. $info['audio']['encoder'] = rtrim($info['mpeg']['audio']['LAME']['short_version'], "\x00");
  65. }
  66. }
  67. $CurrentDataLAMEversionString = (!empty($CurrentDataLAMEversionString) ? $CurrentDataLAMEversionString : (isset($info['audio']['encoder']) ? $info['audio']['encoder'] : ''));
  68. if (!empty($CurrentDataLAMEversionString) && (substr($CurrentDataLAMEversionString, 0, 6) == 'LAME3.') && !preg_match('[0-9\)]', substr($CurrentDataLAMEversionString, -1))) {
  69. // a version number of LAME that does not end with a number like "LAME3.92"
  70. // or with a closing parenthesis like "LAME3.88 (alpha)"
  71. // or a version of LAME with the LAMEtag-not-filled-in-DLL-mode bug (3.90-3.92)
  72. // not sure what the actual last frame length will be, but will be less than or equal to 1441
  73. $PossiblyLongerLAMEversion_FrameLength = 1441;
  74. // Not sure what version of LAME this is - look in padding of last frame for longer version string
  75. $PossibleLAMEversionStringOffset = $info['avdataend'] - $PossiblyLongerLAMEversion_FrameLength;
  76. $this->fseek($PossibleLAMEversionStringOffset);
  77. $PossiblyLongerLAMEversion_Data = $this->fread($PossiblyLongerLAMEversion_FrameLength);
  78. switch (substr($CurrentDataLAMEversionString, -1)) {
  79. case 'a':
  80. case 'b':
  81. // "LAME3.94a" will have a longer version string of "LAME3.94 (alpha)" for example
  82. // need to trim off "a" to match longer string
  83. $CurrentDataLAMEversionString = substr($CurrentDataLAMEversionString, 0, -1);
  84. break;
  85. }
  86. if (($PossiblyLongerLAMEversion_String = strstr($PossiblyLongerLAMEversion_Data, $CurrentDataLAMEversionString)) !== false) {
  87. if (substr($PossiblyLongerLAMEversion_String, 0, strlen($CurrentDataLAMEversionString)) == $CurrentDataLAMEversionString) {
  88. $PossiblyLongerLAMEversion_NewString = substr($PossiblyLongerLAMEversion_String, 0, strspn($PossiblyLongerLAMEversion_String, 'LAME0123456789., (abcdefghijklmnopqrstuvwxyzJFSOND)')); //"LAME3.90.3" "LAME3.87 (beta 1, Sep 27 2000)" "LAME3.88 (beta)"
  89. if (empty($info['audio']['encoder']) || (strlen($PossiblyLongerLAMEversion_NewString) > strlen($info['audio']['encoder']))) {
  90. $info['audio']['encoder'] = $PossiblyLongerLAMEversion_NewString;
  91. }
  92. }
  93. }
  94. }
  95. if (!empty($info['audio']['encoder'])) {
  96. $info['audio']['encoder'] = rtrim($info['audio']['encoder'], "\x00 ");
  97. }
  98. switch (isset($info['mpeg']['audio']['layer']) ? $info['mpeg']['audio']['layer'] : '') {
  99. case 1:
  100. case 2:
  101. $info['audio']['dataformat'] = 'mp'.$info['mpeg']['audio']['layer'];
  102. break;
  103. }
  104. if (isset($info['fileformat']) && ($info['fileformat'] == 'mp3')) {
  105. switch ($info['audio']['dataformat']) {
  106. case 'mp1':
  107. case 'mp2':
  108. case 'mp3':
  109. $info['fileformat'] = $info['audio']['dataformat'];
  110. break;
  111. default:
  112. $info['warning'][] = 'Expecting [audio][dataformat] to be mp1/mp2/mp3 when fileformat == mp3, [audio][dataformat] actually "'.$info['audio']['dataformat'].'"';
  113. break;
  114. }
  115. }
  116. if (empty($info['fileformat'])) {
  117. unset($info['fileformat']);
  118. unset($info['audio']['bitrate_mode']);
  119. unset($info['avdataoffset']);
  120. unset($info['avdataend']);
  121. return false;
  122. }
  123. $info['mime_type'] = 'audio/mpeg';
  124. $info['audio']['lossless'] = false;
  125. // Calculate playtime
  126. if (!isset($info['playtime_seconds']) && isset($info['audio']['bitrate']) && ($info['audio']['bitrate'] > 0)) {
  127. $info['playtime_seconds'] = ($info['avdataend'] - $info['avdataoffset']) * 8 / $info['audio']['bitrate'];
  128. }
  129. $info['audio']['encoder_options'] = $this->GuessEncoderOptions();
  130. return true;
  131. }
  132. public function GuessEncoderOptions() {
  133. // shortcuts
  134. $info = &$this->getid3->info;
  135. if (!empty($info['mpeg']['audio'])) {
  136. $thisfile_mpeg_audio = &$info['mpeg']['audio'];
  137. if (!empty($thisfile_mpeg_audio['LAME'])) {
  138. $thisfile_mpeg_audio_lame = &$thisfile_mpeg_audio['LAME'];
  139. }
  140. }
  141. $encoder_options = '';
  142. static $NamedPresetBitrates = array(16, 24, 40, 56, 112, 128, 160, 192, 256);
  143. if (isset($thisfile_mpeg_audio['VBR_method']) && ($thisfile_mpeg_audio['VBR_method'] == 'Fraunhofer') && !empty($thisfile_mpeg_audio['VBR_quality'])) {
  144. $encoder_options = 'VBR q'.$thisfile_mpeg_audio['VBR_quality'];
  145. } elseif (!empty($thisfile_mpeg_audio_lame['preset_used']) && (!in_array($thisfile_mpeg_audio_lame['preset_used_id'], $NamedPresetBitrates))) {
  146. $encoder_options = $thisfile_mpeg_audio_lame['preset_used'];
  147. } elseif (!empty($thisfile_mpeg_audio_lame['vbr_quality'])) {
  148. static $KnownEncoderValues = array();
  149. if (empty($KnownEncoderValues)) {
  150. //$KnownEncoderValues[abrbitrate_minbitrate][vbr_quality][raw_vbr_method][raw_noise_shaping][raw_stereo_mode][ath_type][lowpass_frequency] = 'preset name';
  151. $KnownEncoderValues[0xFF][58][1][1][3][2][20500] = '--alt-preset insane'; // 3.90, 3.90.1, 3.92
  152. $KnownEncoderValues[0xFF][58][1][1][3][2][20600] = '--alt-preset insane'; // 3.90.2, 3.90.3, 3.91
  153. $KnownEncoderValues[0xFF][57][1][1][3][4][20500] = '--alt-preset insane'; // 3.94, 3.95
  154. $KnownEncoderValues['**'][78][3][2][3][2][19500] = '--alt-preset extreme'; // 3.90, 3.90.1, 3.92
  155. $KnownEncoderValues['**'][78][3][2][3][2][19600] = '--alt-preset extreme'; // 3.90.2, 3.91
  156. $KnownEncoderValues['**'][78][3][1][3][2][19600] = '--alt-preset extreme'; // 3.90.3
  157. $KnownEncoderValues['**'][78][4][2][3][2][19500] = '--alt-preset fast extreme'; // 3.90, 3.90.1, 3.92
  158. $KnownEncoderValues['**'][78][4][2][3][2][19600] = '--alt-preset fast extreme'; // 3.90.2, 3.90.3, 3.91
  159. $KnownEncoderValues['**'][78][3][2][3][4][19000] = '--alt-preset standard'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92
  160. $KnownEncoderValues['**'][78][3][1][3][4][19000] = '--alt-preset standard'; // 3.90.3
  161. $KnownEncoderValues['**'][78][4][2][3][4][19000] = '--alt-preset fast standard'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92
  162. $KnownEncoderValues['**'][78][4][1][3][4][19000] = '--alt-preset fast standard'; // 3.90.3
  163. $KnownEncoderValues['**'][88][4][1][3][3][19500] = '--r3mix'; // 3.90, 3.90.1, 3.92
  164. $KnownEncoderValues['**'][88][4][1][3][3][19600] = '--r3mix'; // 3.90.2, 3.90.3, 3.91
  165. $KnownEncoderValues['**'][67][4][1][3][4][18000] = '--r3mix'; // 3.94, 3.95
  166. $KnownEncoderValues['**'][68][3][2][3][4][18000] = '--alt-preset medium'; // 3.90.3
  167. $KnownEncoderValues['**'][68][4][2][3][4][18000] = '--alt-preset fast medium'; // 3.90.3
  168. $KnownEncoderValues[0xFF][99][1][1][1][2][0] = '--preset studio'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92
  169. $KnownEncoderValues[0xFF][58][2][1][3][2][20600] = '--preset studio'; // 3.90.3, 3.93.1
  170. $KnownEncoderValues[0xFF][58][2][1][3][2][20500] = '--preset studio'; // 3.93
  171. $KnownEncoderValues[0xFF][57][2][1][3][4][20500] = '--preset studio'; // 3.94, 3.95
  172. $KnownEncoderValues[0xC0][88][1][1][1][2][0] = '--preset cd'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92
  173. $KnownEncoderValues[0xC0][58][2][2][3][2][19600] = '--preset cd'; // 3.90.3, 3.93.1
  174. $KnownEncoderValues[0xC0][58][2][2][3][2][19500] = '--preset cd'; // 3.93
  175. $KnownEncoderValues[0xC0][57][2][1][3][4][19500] = '--preset cd'; // 3.94, 3.95
  176. $KnownEncoderValues[0xA0][78][1][1][3][2][18000] = '--preset hifi'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92
  177. $KnownEncoderValues[0xA0][58][2][2][3][2][18000] = '--preset hifi'; // 3.90.3, 3.93, 3.93.1
  178. $KnownEncoderValues[0xA0][57][2][1][3][4][18000] = '--preset hifi'; // 3.94, 3.95
  179. $KnownEncoderValues[0x80][67][1][1][3][2][18000] = '--preset tape'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92
  180. $KnownEncoderValues[0x80][67][1][1][3][2][15000] = '--preset radio'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92
  181. $KnownEncoderValues[0x70][67][1][1][3][2][15000] = '--preset fm'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92
  182. $KnownEncoderValues[0x70][58][2][2][3][2][16000] = '--preset tape/radio/fm'; // 3.90.3, 3.93, 3.93.1
  183. $KnownEncoderValues[0x70][57][2][1][3][4][16000] = '--preset tape/radio/fm'; // 3.94, 3.95
  184. $KnownEncoderValues[0x38][58][2][2][0][2][10000] = '--preset voice'; // 3.90.3, 3.93, 3.93.1
  185. $KnownEncoderValues[0x38][57][2][1][0][4][15000] = '--preset voice'; // 3.94, 3.95
  186. $KnownEncoderValues[0x38][57][2][1][0][4][16000] = '--preset voice'; // 3.94a14
  187. $KnownEncoderValues[0x28][65][1][1][0][2][7500] = '--preset mw-us'; // 3.90, 3.90.1, 3.92
  188. $KnownEncoderValues[0x28][65][1][1][0][2][7600] = '--preset mw-us'; // 3.90.2, 3.91
  189. $KnownEncoderValues[0x28][58][2][2][0][2][7000] = '--preset mw-us'; // 3.90.3, 3.93, 3.93.1
  190. $KnownEncoderValues[0x28][57][2][1][0][4][10500] = '--preset mw-us'; // 3.94, 3.95
  191. $KnownEncoderValues[0x28][57][2][1][0][4][11200] = '--preset mw-us'; // 3.94a14
  192. $KnownEncoderValues[0x28][57][2][1][0][4][8800] = '--preset mw-us'; // 3.94a15
  193. $KnownEncoderValues[0x18][58][2][2][0][2][4000] = '--preset phon+/lw/mw-eu/sw'; // 3.90.3, 3.93.1
  194. $KnownEncoderValues[0x18][58][2][2][0][2][3900] = '--preset phon+/lw/mw-eu/sw'; // 3.93
  195. $KnownEncoderValues[0x18][57][2][1][0][4][5900] = '--preset phon+/lw/mw-eu/sw'; // 3.94, 3.95
  196. $KnownEncoderValues[0x18][57][2][1][0][4][6200] = '--preset phon+/lw/mw-eu/sw'; // 3.94a14
  197. $KnownEncoderValues[0x18][57][2][1][0][4][3200] = '--preset phon+/lw/mw-eu/sw'; // 3.94a15
  198. $KnownEncoderValues[0x10][58][2][2][0][2][3800] = '--preset phone'; // 3.90.3, 3.93.1
  199. $KnownEncoderValues[0x10][58][2][2][0][2][3700] = '--preset phone'; // 3.93
  200. $KnownEncoderValues[0x10][57][2][1][0][4][5600] = '--preset phone'; // 3.94, 3.95
  201. }
  202. if (isset($KnownEncoderValues[$thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate']][$thisfile_mpeg_audio_lame['vbr_quality']][$thisfile_mpeg_audio_lame['raw']['vbr_method']][$thisfile_mpeg_audio_lame['raw']['noise_shaping']][$thisfile_mpeg_audio_lame['raw']['stereo_mode']][$thisfile_mpeg_audio_lame['ath_type']][$thisfile_mpeg_audio_lame['lowpass_frequency']])) {
  203. $encoder_options = $KnownEncoderValues[$thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate']][$thisfile_mpeg_audio_lame['vbr_quality']][$thisfile_mpeg_audio_lame['raw']['vbr_method']][$thisfile_mpeg_audio_lame['raw']['noise_shaping']][$thisfile_mpeg_audio_lame['raw']['stereo_mode']][$thisfile_mpeg_audio_lame['ath_type']][$thisfile_mpeg_audio_lame['lowpass_frequency']];
  204. } elseif (isset($KnownEncoderValues['**'][$thisfile_mpeg_audio_lame['vbr_quality']][$thisfile_mpeg_audio_lame['raw']['vbr_method']][$thisfile_mpeg_audio_lame['raw']['noise_shaping']][$thisfile_mpeg_audio_lame['raw']['stereo_mode']][$thisfile_mpeg_audio_lame['ath_type']][$thisfile_mpeg_audio_lame['lowpass_frequency']])) {
  205. $encoder_options = $KnownEncoderValues['**'][$thisfile_mpeg_audio_lame['vbr_quality']][$thisfile_mpeg_audio_lame['raw']['vbr_method']][$thisfile_mpeg_audio_lame['raw']['noise_shaping']][$thisfile_mpeg_audio_lame['raw']['stereo_mode']][$thisfile_mpeg_audio_lame['ath_type']][$thisfile_mpeg_audio_lame['lowpass_frequency']];
  206. } elseif ($info['audio']['bitrate_mode'] == 'vbr') {
  207. // http://gabriel.mp3-tech.org/mp3infotag.html
  208. // int Quality = (100 - 10 * gfp->VBR_q - gfp->quality)h
  209. $LAME_V_value = 10 - ceil($thisfile_mpeg_audio_lame['vbr_quality'] / 10);
  210. $LAME_q_value = 100 - $thisfile_mpeg_audio_lame['vbr_quality'] - ($LAME_V_value * 10);
  211. $encoder_options = '-V'.$LAME_V_value.' -q'.$LAME_q_value;
  212. } elseif ($info['audio']['bitrate_mode'] == 'cbr') {
  213. $encoder_options = strtoupper($info['audio']['bitrate_mode']).ceil($info['audio']['bitrate'] / 1000);
  214. } else {
  215. $encoder_options = strtoupper($info['audio']['bitrate_mode']);
  216. }
  217. } elseif (!empty($thisfile_mpeg_audio_lame['bitrate_abr'])) {
  218. $encoder_options = 'ABR'.$thisfile_mpeg_audio_lame['bitrate_abr'];
  219. } elseif (!empty($info['audio']['bitrate'])) {
  220. if ($info['audio']['bitrate_mode'] == 'cbr') {
  221. $encoder_options = strtoupper($info['audio']['bitrate_mode']).ceil($info['audio']['bitrate'] / 1000);
  222. } else {
  223. $encoder_options = strtoupper($info['audio']['bitrate_mode']);
  224. }
  225. }
  226. if (!empty($thisfile_mpeg_audio_lame['bitrate_min'])) {
  227. $encoder_options .= ' -b'.$thisfile_mpeg_audio_lame['bitrate_min'];
  228. }
  229. if (!empty($thisfile_mpeg_audio_lame['encoding_flags']['nogap_prev']) || !empty($thisfile_mpeg_audio_lame['encoding_flags']['nogap_next'])) {
  230. $encoder_options .= ' --nogap';
  231. }
  232. if (!empty($thisfile_mpeg_audio_lame['lowpass_frequency'])) {
  233. $ExplodedOptions = explode(' ', $encoder_options, 4);
  234. if ($ExplodedOptions[0] == '--r3mix') {
  235. $ExplodedOptions[1] = 'r3mix';
  236. }
  237. switch ($ExplodedOptions[0]) {
  238. case '--preset':
  239. case '--alt-preset':
  240. case '--r3mix':
  241. if ($ExplodedOptions[1] == 'fast') {
  242. $ExplodedOptions[1] .= ' '.$ExplodedOptions[2];
  243. }
  244. switch ($ExplodedOptions[1]) {
  245. case 'portable':
  246. case 'medium':
  247. case 'standard':
  248. case 'extreme':
  249. case 'insane':
  250. case 'fast portable':
  251. case 'fast medium':
  252. case 'fast standard':
  253. case 'fast extreme':
  254. case 'fast insane':
  255. case 'r3mix':
  256. static $ExpectedLowpass = array(
  257. 'insane|20500' => 20500,
  258. 'insane|20600' => 20600, // 3.90.2, 3.90.3, 3.91
  259. 'medium|18000' => 18000,
  260. 'fast medium|18000' => 18000,
  261. 'extreme|19500' => 19500, // 3.90, 3.90.1, 3.92, 3.95
  262. 'extreme|19600' => 19600, // 3.90.2, 3.90.3, 3.91, 3.93.1
  263. 'fast extreme|19500' => 19500, // 3.90, 3.90.1, 3.92, 3.95
  264. 'fast extreme|19600' => 19600, // 3.90.2, 3.90.3, 3.91, 3.93.1
  265. 'standard|19000' => 19000,
  266. 'fast standard|19000' => 19000,
  267. 'r3mix|19500' => 19500, // 3.90, 3.90.1, 3.92
  268. 'r3mix|19600' => 19600, // 3.90.2, 3.90.3, 3.91
  269. 'r3mix|18000' => 18000, // 3.94, 3.95
  270. );
  271. if (!isset($ExpectedLowpass[$ExplodedOptions[1].'|'.$thisfile_mpeg_audio_lame['lowpass_frequency']]) && ($thisfile_mpeg_audio_lame['lowpass_frequency'] < 22050) && (round($thisfile_mpeg_audio_lame['lowpass_frequency'] / 1000) < round($thisfile_mpeg_audio['sample_rate'] / 2000))) {
  272. $encoder_options .= ' --lowpass '.$thisfile_mpeg_audio_lame['lowpass_frequency'];
  273. }
  274. break;
  275. default:
  276. break;
  277. }
  278. break;
  279. }
  280. }
  281. if (isset($thisfile_mpeg_audio_lame['raw']['source_sample_freq'])) {
  282. if (($thisfile_mpeg_audio['sample_rate'] == 44100) && ($thisfile_mpeg_audio_lame['raw']['source_sample_freq'] != 1)) {
  283. $encoder_options .= ' --resample 44100';
  284. } elseif (($thisfile_mpeg_audio['sample_rate'] == 48000) && ($thisfile_mpeg_audio_lame['raw']['source_sample_freq'] != 2)) {
  285. $encoder_options .= ' --resample 48000';
  286. } elseif ($thisfile_mpeg_audio['sample_rate'] < 44100) {
  287. switch ($thisfile_mpeg_audio_lame['raw']['source_sample_freq']) {
  288. case 0: // <= 32000
  289. // may or may not be same as source frequency - ignore
  290. break;
  291. case 1: // 44100
  292. case 2: // 48000
  293. case 3: // 48000+
  294. $ExplodedOptions = explode(' ', $encoder_options, 4);
  295. switch ($ExplodedOptions[0]) {
  296. case '--preset':
  297. case '--alt-preset':
  298. switch ($ExplodedOptions[1]) {
  299. case 'fast':
  300. case 'portable':
  301. case 'medium':
  302. case 'standard':
  303. case 'extreme':
  304. case 'insane':
  305. $encoder_options .= ' --resample '.$thisfile_mpeg_audio['sample_rate'];
  306. break;
  307. default:
  308. static $ExpectedResampledRate = array(
  309. 'phon+/lw/mw-eu/sw|16000' => 16000,
  310. 'mw-us|24000' => 24000, // 3.95
  311. 'mw-us|32000' => 32000, // 3.93
  312. 'mw-us|16000' => 16000, // 3.92
  313. 'phone|16000' => 16000,
  314. 'phone|11025' => 11025, // 3.94a15
  315. 'radio|32000' => 32000, // 3.94a15
  316. 'fm/radio|32000' => 32000, // 3.92
  317. 'fm|32000' => 32000, // 3.90
  318. 'voice|32000' => 32000);
  319. if (!isset($ExpectedResampledRate[$ExplodedOptions[1].'|'.$thisfile_mpeg_audio['sample_rate']])) {
  320. $encoder_options .= ' --resample '.$thisfile_mpeg_audio['sample_rate'];
  321. }
  322. break;
  323. }
  324. break;
  325. case '--r3mix':
  326. default:
  327. $encoder_options .= ' --resample '.$thisfile_mpeg_audio['sample_rate'];
  328. break;
  329. }
  330. break;
  331. }
  332. }
  333. }
  334. if (empty($encoder_options) && !empty($info['audio']['bitrate']) && !empty($info['audio']['bitrate_mode'])) {
  335. //$encoder_options = strtoupper($info['audio']['bitrate_mode']).ceil($info['audio']['bitrate'] / 1000);
  336. $encoder_options = strtoupper($info['audio']['bitrate_mode']);
  337. }
  338. return $encoder_options;
  339. }
  340. public function decodeMPEGaudioHeader($offset, &$info, $recursivesearch=true, $ScanAsCBR=false, $FastMPEGheaderScan=false) {
  341. static $MPEGaudioVersionLookup;
  342. static $MPEGaudioLayerLookup;
  343. static $MPEGaudioBitrateLookup;
  344. static $MPEGaudioFrequencyLookup;
  345. static $MPEGaudioChannelModeLookup;
  346. static $MPEGaudioModeExtensionLookup;
  347. static $MPEGaudioEmphasisLookup;
  348. if (empty($MPEGaudioVersionLookup)) {
  349. $MPEGaudioVersionLookup = self::MPEGaudioVersionArray();
  350. $MPEGaudioLayerLookup = self::MPEGaudioLayerArray();
  351. $MPEGaudioBitrateLookup = self::MPEGaudioBitrateArray();
  352. $MPEGaudioFrequencyLookup = self::MPEGaudioFrequencyArray();
  353. $MPEGaudioChannelModeLookup = self::MPEGaudioChannelModeArray();
  354. $MPEGaudioModeExtensionLookup = self::MPEGaudioModeExtensionArray();
  355. $MPEGaudioEmphasisLookup = self::MPEGaudioEmphasisArray();
  356. }
  357. if ($this->fseek($offset) != 0) {
  358. $info['error'][] = 'decodeMPEGaudioHeader() failed to seek to next offset at '.$offset;
  359. return false;
  360. }
  361. //$headerstring = $this->fread(1441); // worst-case max length = 32kHz @ 320kbps layer 3 = 1441 bytes/frame
  362. $headerstring = $this->fread(226); // LAME header at offset 36 + 190 bytes of Xing/LAME data
  363. // MP3 audio frame structure:
  364. // $aa $aa $aa $aa [$bb $bb] $cc...
  365. // where $aa..$aa is the four-byte mpeg-audio header (below)
  366. // $bb $bb is the optional 2-byte CRC
  367. // and $cc... is the audio data
  368. $head4 = substr($headerstring, 0, 4);
  369. static $MPEGaudioHeaderDecodeCache = array();
  370. if (isset($MPEGaudioHeaderDecodeCache[$head4])) {
  371. $MPEGheaderRawArray = $MPEGaudioHeaderDecodeCache[$head4];
  372. } else {
  373. $MPEGheaderRawArray = self::MPEGaudioHeaderDecode($head4);
  374. $MPEGaudioHeaderDecodeCache[$head4] = $MPEGheaderRawArray;
  375. }
  376. static $MPEGaudioHeaderValidCache = array();
  377. if (!isset($MPEGaudioHeaderValidCache[$head4])) { // Not in cache
  378. //$MPEGaudioHeaderValidCache[$head4] = self::MPEGaudioHeaderValid($MPEGheaderRawArray, false, true); // allow badly-formatted freeformat (from LAME 3.90 - 3.93.1)
  379. $MPEGaudioHeaderValidCache[$head4] = self::MPEGaudioHeaderValid($MPEGheaderRawArray, false, false);
  380. }
  381. // shortcut
  382. if (!isset($info['mpeg']['audio'])) {
  383. $info['mpeg']['audio'] = array();
  384. }
  385. $thisfile_mpeg_audio = &$info['mpeg']['audio'];
  386. if ($MPEGaudioHeaderValidCache[$head4]) {
  387. $thisfile_mpeg_audio['raw'] = $MPEGheaderRawArray;
  388. } else {
  389. $info['error'][] = 'Invalid MPEG audio header ('.getid3_lib::PrintHexBytes($head4).') at offset '.$offset;
  390. return false;
  391. }
  392. if (!$FastMPEGheaderScan) {
  393. $thisfile_mpeg_audio['version'] = $MPEGaudioVersionLookup[$thisfile_mpeg_audio['raw']['version']];
  394. $thisfile_mpeg_audio['layer'] = $MPEGaudioLayerLookup[$thisfile_mpeg_audio['raw']['layer']];
  395. $thisfile_mpeg_audio['channelmode'] = $MPEGaudioChannelModeLookup[$thisfile_mpeg_audio['raw']['channelmode']];
  396. $thisfile_mpeg_audio['channels'] = (($thisfile_mpeg_audio['channelmode'] == 'mono') ? 1 : 2);
  397. $thisfile_mpeg_audio['sample_rate'] = $MPEGaudioFrequencyLookup[$thisfile_mpeg_audio['version']][$thisfile_mpeg_audio['raw']['sample_rate']];
  398. $thisfile_mpeg_audio['protection'] = !$thisfile_mpeg_audio['raw']['protection'];
  399. $thisfile_mpeg_audio['private'] = (bool) $thisfile_mpeg_audio['raw']['private'];
  400. $thisfile_mpeg_audio['modeextension'] = $MPEGaudioModeExtensionLookup[$thisfile_mpeg_audio['layer']][$thisfile_mpeg_audio['raw']['modeextension']];
  401. $thisfile_mpeg_audio['copyright'] = (bool) $thisfile_mpeg_audio['raw']['copyright'];
  402. $thisfile_mpeg_audio['original'] = (bool) $thisfile_mpeg_audio['raw']['original'];
  403. $thisfile_mpeg_audio['emphasis'] = $MPEGaudioEmphasisLookup[$thisfile_mpeg_audio['raw']['emphasis']];
  404. $info['audio']['channels'] = $thisfile_mpeg_audio['channels'];
  405. $info['audio']['sample_rate'] = $thisfile_mpeg_audio['sample_rate'];
  406. if ($thisfile_mpeg_audio['protection']) {
  407. $thisfile_mpeg_audio['crc'] = getid3_lib::BigEndian2Int(substr($headerstring, 4, 2));
  408. }
  409. }
  410. if ($thisfile_mpeg_audio['raw']['bitrate'] == 15) {
  411. // http://www.hydrogenaudio.org/?act=ST&f=16&t=9682&st=0
  412. $info['warning'][] = 'Invalid bitrate index (15), this is a known bug in free-format MP3s encoded by LAME v3.90 - 3.93.1';
  413. $thisfile_mpeg_audio['raw']['bitrate'] = 0;
  414. }
  415. $thisfile_mpeg_audio['padding'] = (bool) $thisfile_mpeg_audio['raw']['padding'];
  416. $thisfile_mpeg_audio['bitrate'] = $MPEGaudioBitrateLookup[$thisfile_mpeg_audio['version']][$thisfile_mpeg_audio['layer']][$thisfile_mpeg_audio['raw']['bitrate']];
  417. if (($thisfile_mpeg_audio['bitrate'] == 'free') && ($offset == $info['avdataoffset'])) {
  418. // only skip multiple frame check if free-format bitstream found at beginning of file
  419. // otherwise is quite possibly simply corrupted data
  420. $recursivesearch = false;
  421. }
  422. // For Layer 2 there are some combinations of bitrate and mode which are not allowed.
  423. if (!$FastMPEGheaderScan && ($thisfile_mpeg_audio['layer'] == '2')) {
  424. $info['audio']['dataformat'] = 'mp2';
  425. switch ($thisfile_mpeg_audio['channelmode']) {
  426. case 'mono':
  427. if (($thisfile_mpeg_audio['bitrate'] == 'free') || ($thisfile_mpeg_audio['bitrate'] <= 192000)) {
  428. // these are ok
  429. } else {
  430. $info['error'][] = $thisfile_mpeg_audio['bitrate'].'kbps not allowed in Layer 2, '.$thisfile_mpeg_audio['channelmode'].'.';
  431. return false;
  432. }
  433. break;
  434. case 'stereo':
  435. case 'joint stereo':
  436. case 'dual channel':
  437. if (($thisfile_mpeg_audio['bitrate'] == 'free') || ($thisfile_mpeg_audio['bitrate'] == 64000) || ($thisfile_mpeg_audio['bitrate'] >= 96000)) {
  438. // these are ok
  439. } else {
  440. $info['error'][] = intval(round($thisfile_mpeg_audio['bitrate'] / 1000)).'kbps not allowed in Layer 2, '.$thisfile_mpeg_audio['channelmode'].'.';
  441. return false;
  442. }
  443. break;
  444. }
  445. }
  446. if ($info['audio']['sample_rate'] > 0) {
  447. $thisfile_mpeg_audio['framelength'] = self::MPEGaudioFrameLength($thisfile_mpeg_audio['bitrate'], $thisfile_mpeg_audio['version'], $thisfile_mpeg_audio['layer'], (int) $thisfile_mpeg_audio['padding'], $info['audio']['sample_rate']);
  448. }
  449. $nextframetestoffset = $offset + 1;
  450. if ($thisfile_mpeg_audio['bitrate'] != 'free') {
  451. $info['audio']['bitrate'] = $thisfile_mpeg_audio['bitrate'];
  452. if (isset($thisfile_mpeg_audio['framelength'])) {
  453. $nextframetestoffset = $offset + $thisfile_mpeg_audio['framelength'];
  454. } else {
  455. $info['error'][] = 'Frame at offset('.$offset.') is has an invalid frame length.';
  456. return false;
  457. }
  458. }
  459. $ExpectedNumberOfAudioBytes = 0;
  460. ////////////////////////////////////////////////////////////////////////////////////
  461. // Variable-bitrate headers
  462. if (substr($headerstring, 4 + 32, 4) == 'VBRI') {
  463. // Fraunhofer VBR header is hardcoded 'VBRI' at offset 0x24 (36)
  464. // specs taken from http://minnie.tuhs.org/pipermail/mp3encoder/2001-January/001800.html
  465. $thisfile_mpeg_audio['bitrate_mode'] = 'vbr';
  466. $thisfile_mpeg_audio['VBR_method'] = 'Fraunhofer';
  467. $info['audio']['codec'] = 'Fraunhofer';
  468. $SideInfoData = substr($headerstring, 4 + 2, 32);
  469. $FraunhoferVBROffset = 36;
  470. $thisfile_mpeg_audio['VBR_encoder_version'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 4, 2)); // VbriVersion
  471. $thisfile_mpeg_audio['VBR_encoder_delay'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 6, 2)); // VbriDelay
  472. $thisfile_mpeg_audio['VBR_quality'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 8, 2)); // VbriQuality
  473. $thisfile_mpeg_audio['VBR_bytes'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 10, 4)); // VbriStreamBytes
  474. $thisfile_mpeg_audio['VBR_frames'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 14, 4)); // VbriStreamFrames
  475. $thisfile_mpeg_audio['VBR_seek_offsets'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 18, 2)); // VbriTableSize
  476. $thisfile_mpeg_audio['VBR_seek_scale'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 20, 2)); // VbriTableScale
  477. $thisfile_mpeg_audio['VBR_entry_bytes'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 22, 2)); // VbriEntryBytes
  478. $thisfile_mpeg_audio['VBR_entry_frames'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 24, 2)); // VbriEntryFrames
  479. $ExpectedNumberOfAudioBytes = $thisfile_mpeg_audio['VBR_bytes'];
  480. $previousbyteoffset = $offset;
  481. for ($i = 0; $i < $thisfile_mpeg_audio['VBR_seek_offsets']; $i++) {
  482. $Fraunhofer_OffsetN = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset, $thisfile_mpeg_audio['VBR_entry_bytes']));
  483. $FraunhoferVBROffset += $thisfile_mpeg_audio['VBR_entry_bytes'];
  484. $thisfile_mpeg_audio['VBR_offsets_relative'][$i] = ($Fraunhofer_OffsetN * $thisfile_mpeg_audio['VBR_seek_scale']);
  485. $thisfile_mpeg_audio['VBR_offsets_absolute'][$i] = ($Fraunhofer_OffsetN * $thisfile_mpeg_audio['VBR_seek_scale']) + $previousbyteoffset;
  486. $previousbyteoffset += $Fraunhofer_OffsetN;
  487. }
  488. } else {
  489. // Xing VBR header is hardcoded 'Xing' at a offset 0x0D (13), 0x15 (21) or 0x24 (36)
  490. // depending on MPEG layer and number of channels
  491. $VBRidOffset = self::XingVBRidOffset($thisfile_mpeg_audio['version'], $thisfile_mpeg_audio['channelmode']);
  492. $SideInfoData = substr($headerstring, 4 + 2, $VBRidOffset - 4);
  493. if ((substr($headerstring, $VBRidOffset, strlen('Xing')) == 'Xing') || (substr($headerstring, $VBRidOffset, strlen('Info')) == 'Info')) {
  494. // 'Xing' is traditional Xing VBR frame
  495. // 'Info' is LAME-encoded CBR (This was done to avoid CBR files to be recognized as traditional Xing VBR files by some decoders.)
  496. // 'Info' *can* legally be used to specify a VBR file as well, however.
  497. // http://www.multiweb.cz/twoinches/MP3inside.htm
  498. //00..03 = "Xing" or "Info"
  499. //04..07 = Flags:
  500. // 0x01 Frames Flag set if value for number of frames in file is stored
  501. // 0x02 Bytes Flag set if value for filesize in bytes is stored
  502. // 0x04 TOC Flag set if values for TOC are stored
  503. // 0x08 VBR Scale Flag set if values for VBR scale is stored
  504. //08..11 Frames: Number of frames in file (including the first Xing/Info one)
  505. //12..15 Bytes: File length in Bytes
  506. //16..115 TOC (Table of Contents):
  507. // Contains of 100 indexes (one Byte length) for easier lookup in file. Approximately solves problem with moving inside file.
  508. // Each Byte has a value according this formula:
  509. // (TOC[i] / 256) * fileLenInBytes
  510. // So if song lasts eg. 240 sec. and you want to jump to 60. sec. (and file is 5 000 000 Bytes length) you can use:
  511. // TOC[(60/240)*100] = TOC[25]
  512. // and corresponding Byte in file is then approximately at:
  513. // (TOC[25]/256) * 5000000
  514. //116..119 VBR Scale
  515. // should be safe to leave this at 'vbr' and let it be overriden to 'cbr' if a CBR preset/mode is used by LAME
  516. // if (substr($headerstring, $VBRidOffset, strlen('Info')) == 'Xing') {
  517. $thisfile_mpeg_audio['bitrate_mode'] = 'vbr';
  518. $thisfile_mpeg_audio['VBR_method'] = 'Xing';
  519. // } else {
  520. // $ScanAsCBR = true;
  521. // $thisfile_mpeg_audio['bitrate_mode'] = 'cbr';
  522. // }
  523. $thisfile_mpeg_audio['xing_flags_raw'] = getid3_lib::BigEndian2Int(substr($headerstring, $VBRidOffset + 4, 4));
  524. $thisfile_mpeg_audio['xing_flags']['frames'] = (bool) ($thisfile_mpeg_audio['xing_flags_raw'] & 0x00000001);
  525. $thisfile_mpeg_audio['xing_flags']['bytes'] = (bool) ($thisfile_mpeg_audio['xing_flags_raw'] & 0x00000002);
  526. $thisfile_mpeg_audio['xing_flags']['toc'] = (bool) ($thisfile_mpeg_audio['xing_flags_raw'] & 0x00000004);
  527. $thisfile_mpeg_audio['xing_flags']['vbr_scale'] = (bool) ($thisfile_mpeg_audio['xing_flags_raw'] & 0x00000008);
  528. if ($thisfile_mpeg_audio['xing_flags']['frames']) {
  529. $thisfile_mpeg_audio['VBR_frames'] = getid3_lib::BigEndian2Int(substr($headerstring, $VBRidOffset + 8, 4));
  530. //$thisfile_mpeg_audio['VBR_frames']--; // don't count header Xing/Info frame
  531. }
  532. if ($thisfile_mpeg_audio['xing_flags']['bytes']) {
  533. $thisfile_mpeg_audio['VBR_bytes'] = getid3_lib::BigEndian2Int(substr($headerstring, $VBRidOffset + 12, 4));
  534. }
  535. //if (($thisfile_mpeg_audio['bitrate'] == 'free') && !empty($thisfile_mpeg_audio['VBR_frames']) && !empty($thisfile_mpeg_audio['VBR_bytes'])) {
  536. if (!empty($thisfile_mpeg_audio['VBR_frames']) && !empty($thisfile_mpeg_audio['VBR_bytes'])) {
  537. $framelengthfloat = $thisfile_mpeg_audio['VBR_bytes'] / $thisfile_mpeg_audio['VBR_frames'];
  538. if ($thisfile_mpeg_audio['layer'] == '1') {
  539. // BitRate = (((FrameLengthInBytes / 4) - Padding) * SampleRate) / 12
  540. //$info['audio']['bitrate'] = ((($framelengthfloat / 4) - intval($thisfile_mpeg_audio['padding'])) * $thisfile_mpeg_audio['sample_rate']) / 12;
  541. $info['audio']['bitrate'] = ($framelengthfloat / 4) * $thisfile_mpeg_audio['sample_rate'] * (2 / $info['audio']['channels']) / 12;
  542. } else {
  543. // Bitrate = ((FrameLengthInBytes - Padding) * SampleRate) / 144
  544. //$info['audio']['bitrate'] = (($framelengthfloat - intval($thisfile_mpeg_audio['padding'])) * $thisfile_mpeg_audio['sample_rate']) / 144;
  545. $info['audio']['bitrate'] = $framelengthfloat * $thisfile_mpeg_audio['sample_rate'] * (2 / $info['audio']['channels']) / 144;
  546. }
  547. $thisfile_mpeg_audio['framelength'] = floor($framelengthfloat);
  548. }
  549. if ($thisfile_mpeg_audio['xing_flags']['toc']) {
  550. $LAMEtocData = substr($headerstring, $VBRidOffset + 16, 100);
  551. for ($i = 0; $i < 100; $i++) {
  552. $thisfile_mpeg_audio['toc'][$i] = ord($LAMEtocData{$i});
  553. }
  554. }
  555. if ($thisfile_mpeg_audio['xing_flags']['vbr_scale']) {
  556. $thisfile_mpeg_audio['VBR_scale'] = getid3_lib::BigEndian2Int(substr($headerstring, $VBRidOffset + 116, 4));
  557. }
  558. // http://gabriel.mp3-tech.org/mp3infotag.html
  559. if (substr($headerstring, $VBRidOffset + 120, 4) == 'LAME') {
  560. // shortcut
  561. $thisfile_mpeg_audio['LAME'] = array();
  562. $thisfile_mpeg_audio_lame = &$thisfile_mpeg_audio['LAME'];
  563. $thisfile_mpeg_audio_lame['long_version'] = substr($headerstring, $VBRidOffset + 120, 20);
  564. $thisfile_mpeg_audio_lame['short_version'] = substr($thisfile_mpeg_audio_lame['long_version'], 0, 9);
  565. if ($thisfile_mpeg_audio_lame['short_version'] >= 'LAME3.90') {
  566. // extra 11 chars are not part of version string when LAMEtag present
  567. unset($thisfile_mpeg_audio_lame['long_version']);
  568. // It the LAME tag was only introduced in LAME v3.90
  569. // http://www.hydrogenaudio.org/?act=ST&f=15&t=9933
  570. // Offsets of various bytes in http://gabriel.mp3-tech.org/mp3infotag.html
  571. // are assuming a 'Xing' identifier offset of 0x24, which is the case for
  572. // MPEG-1 non-mono, but not for other combinations
  573. $LAMEtagOffsetContant = $VBRidOffset - 0x24;
  574. // shortcuts
  575. $thisfile_mpeg_audio_lame['RGAD'] = array('track'=>array(), 'album'=>array());
  576. $thisfile_mpeg_audio_lame_RGAD = &$thisfile_mpeg_audio_lame['RGAD'];
  577. $thisfile_mpeg_audio_lame_RGAD_track = &$thisfile_mpeg_audio_lame_RGAD['track'];
  578. $thisfile_mpeg_audio_lame_RGAD_album = &$thisfile_mpeg_audio_lame_RGAD['album'];
  579. $thisfile_mpeg_audio_lame['raw'] = array();
  580. $thisfile_mpeg_audio_lame_raw = &$thisfile_mpeg_audio_lame['raw'];
  581. // byte $9B VBR Quality
  582. // This field is there to indicate a quality level, although the scale was not precised in the original Xing specifications.
  583. // Actually overwrites original Xing bytes
  584. unset($thisfile_mpeg_audio['VBR_scale']);
  585. $thisfile_mpeg_audio_lame['vbr_quality'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0x9B, 1));
  586. // bytes $9C-$A4 Encoder short VersionString
  587. $thisfile_mpeg_audio_lame['short_version'] = substr($headerstring, $LAMEtagOffsetContant + 0x9C, 9);
  588. // byte $A5 Info Tag revision + VBR method
  589. $LAMEtagRevisionVBRmethod = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xA5, 1));
  590. $thisfile_mpeg_audio_lame['tag_revision'] = ($LAMEtagRevisionVBRmethod & 0xF0) >> 4;
  591. $thisfile_mpeg_audio_lame_raw['vbr_method'] = $LAMEtagRevisionVBRmethod & 0x0F;
  592. $thisfile_mpeg_audio_lame['vbr_method'] = self::LAMEvbrMethodLookup($thisfile_mpeg_audio_lame_raw['vbr_method']);
  593. $thisfile_mpeg_audio['bitrate_mode'] = substr($thisfile_mpeg_audio_lame['vbr_method'], 0, 3); // usually either 'cbr' or 'vbr', but truncates 'vbr-old / vbr-rh' to 'vbr'
  594. // byte $A6 Lowpass filter value
  595. $thisfile_mpeg_audio_lame['lowpass_frequency'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xA6, 1)) * 100;
  596. // bytes $A7-$AE Replay Gain
  597. // http://privatewww.essex.ac.uk/~djmrob/replaygain/rg_data_format.html
  598. // bytes $A7-$AA : 32 bit floating point "Peak signal amplitude"
  599. if ($thisfile_mpeg_audio_lame['short_version'] >= 'LAME3.94b') {
  600. // LAME 3.94a16 and later - 9.23 fixed point
  601. // ie 0x0059E2EE / (2^23) = 5890798 / 8388608 = 0.7022378444671630859375
  602. $thisfile_mpeg_audio_lame_RGAD['peak_amplitude'] = (float) ((getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xA7, 4))) / 8388608);
  603. } else {
  604. // LAME 3.94a15 and earlier - 32-bit floating point
  605. // Actually 3.94a16 will fall in here too and be WRONG, but is hard to detect 3.94a16 vs 3.94a15
  606. $thisfile_mpeg_audio_lame_RGAD['peak_amplitude'] = getid3_lib::LittleEndian2Float(substr($headerstring, $LAMEtagOffsetContant + 0xA7, 4));
  607. }
  608. if ($thisfile_mpeg_audio_lame_RGAD['peak_amplitude'] == 0) {
  609. unset($thisfile_mpeg_audio_lame_RGAD['peak_amplitude']);
  610. } else {
  611. $thisfile_mpeg_audio_lame_RGAD['peak_db'] = getid3_lib::RGADamplitude2dB($thisfile_mpeg_audio_lame_RGAD['peak_amplitude']);
  612. }
  613. $thisfile_mpeg_audio_lame_raw['RGAD_track'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xAB, 2));
  614. $thisfile_mpeg_audio_lame_raw['RGAD_album'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xAD, 2));
  615. if ($thisfile_mpeg_audio_lame_raw['RGAD_track'] != 0) {
  616. $thisfile_mpeg_audio_lame_RGAD_track['raw']['name'] = ($thisfile_mpeg_audio_lame_raw['RGAD_track'] & 0xE000) >> 13;
  617. $thisfile_mpeg_audio_lame_RGAD_track['raw']['originator'] = ($thisfile_mpeg_audio_lame_raw['RGAD_track'] & 0x1C00) >> 10;
  618. $thisfile_mpeg_audio_lame_RGAD_track['raw']['sign_bit'] = ($thisfile_mpeg_audio_lame_raw['RGAD_track'] & 0x0200) >> 9;
  619. $thisfile_mpeg_audio_lame_RGAD_track['raw']['gain_adjust'] = $thisfile_mpeg_audio_lame_raw['RGAD_track'] & 0x01FF;
  620. $thisfile_mpeg_audio_lame_RGAD_track['name'] = getid3_lib::RGADnameLookup($thisfile_mpeg_audio_lame_RGAD_track['raw']['name']);
  621. $thisfile_mpeg_audio_lame_RGAD_track['originator'] = getid3_lib::RGADoriginatorLookup($thisfile_mpeg_audio_lame_RGAD_track['raw']['originator']);
  622. $thisfile_mpeg_audio_lame_RGAD_track['gain_db'] = getid3_lib::RGADadjustmentLookup($thisfile_mpeg_audio_lame_RGAD_track['raw']['gain_adjust'], $thisfile_mpeg_audio_lame_RGAD_track['raw']['sign_bit']);
  623. if (!empty($thisfile_mpeg_audio_lame_RGAD['peak_amplitude'])) {
  624. $info['replay_gain']['track']['peak'] = $thisfile_mpeg_audio_lame_RGAD['peak_amplitude'];
  625. }
  626. $info['replay_gain']['track']['originator'] = $thisfile_mpeg_audio_lame_RGAD_track['originator'];
  627. $info['replay_gain']['track']['adjustment'] = $thisfile_mpeg_audio_lame_RGAD_track['gain_db'];
  628. } else {
  629. unset($thisfile_mpeg_audio_lame_RGAD['track']);
  630. }
  631. if ($thisfile_mpeg_audio_lame_raw['RGAD_album'] != 0) {
  632. $thisfile_mpeg_audio_lame_RGAD_album['raw']['name'] = ($thisfile_mpeg_audio_lame_raw['RGAD_album'] & 0xE000) >> 13;
  633. $thisfile_mpeg_audio_lame_RGAD_album['raw']['originator'] = ($thisfile_mpeg_audio_lame_raw['RGAD_album'] & 0x1C00) >> 10;
  634. $thisfile_mpeg_audio_lame_RGAD_album['raw']['sign_bit'] = ($thisfile_mpeg_audio_lame_raw['RGAD_album'] & 0x0200) >> 9;
  635. $thisfile_mpeg_audio_lame_RGAD_album['raw']['gain_adjust'] = $thisfile_mpeg_audio_lame_raw['RGAD_album'] & 0x01FF;
  636. $thisfile_mpeg_audio_lame_RGAD_album['name'] = getid3_lib::RGADnameLookup($thisfile_mpeg_audio_lame_RGAD_album['raw']['name']);
  637. $thisfile_mpeg_audio_lame_RGAD_album['originator'] = getid3_lib::RGADoriginatorLookup($thisfile_mpeg_audio_lame_RGAD_album['raw']['originator']);
  638. $thisfile_mpeg_audio_lame_RGAD_album['gain_db'] = getid3_lib::RGADadjustmentLookup($thisfile_mpeg_audio_lame_RGAD_album['raw']['gain_adjust'], $thisfile_mpeg_audio_lame_RGAD_album['raw']['sign_bit']);
  639. if (!empty($thisfile_mpeg_audio_lame_RGAD['peak_amplitude'])) {
  640. $info['replay_gain']['album']['peak'] = $thisfile_mpeg_audio_lame_RGAD['peak_amplitude'];
  641. }
  642. $info['replay_gain']['album']['originator'] = $thisfile_mpeg_audio_lame_RGAD_album['originator'];
  643. $info['replay_gain']['album']['adjustment'] = $thisfile_mpeg_audio_lame_RGAD_album['gain_db'];
  644. } else {
  645. unset($thisfile_mpeg_audio_lame_RGAD['album']);
  646. }
  647. if (empty($thisfile_mpeg_audio_lame_RGAD)) {
  648. unset($thisfile_mpeg_audio_lame['RGAD']);
  649. }
  650. // byte $AF Encoding flags + ATH Type
  651. $EncodingFlagsATHtype = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xAF, 1));
  652. $thisfile_mpeg_audio_lame['encoding_flags']['nspsytune'] = (bool) ($EncodingFlagsATHtype & 0x10);
  653. $thisfile_mpeg_audio_lame['encoding_flags']['nssafejoint'] = (bool) ($EncodingFlagsATHtype & 0x20);
  654. $thisfile_mpeg_audio_lame['encoding_flags']['nogap_next'] = (bool) ($EncodingFlagsATHtype & 0x40);
  655. $thisfile_mpeg_audio_lame['encoding_flags']['nogap_prev'] = (bool) ($EncodingFlagsATHtype & 0x80);
  656. $thisfile_mpeg_audio_lame['ath_type'] = $EncodingFlagsATHtype & 0x0F;
  657. // byte $B0 if ABR {specified bitrate} else {minimal bitrate}
  658. $thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB0, 1));
  659. if ($thisfile_mpeg_audio_lame_raw['vbr_method'] == 2) { // Average BitRate (ABR)
  660. $thisfile_mpeg_audio_lame['bitrate_abr'] = $thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate'];
  661. } elseif ($thisfile_mpeg_audio_lame_raw['vbr_method'] == 1) { // Constant BitRate (CBR)
  662. // ignore
  663. } elseif ($thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate'] > 0) { // Variable BitRate (VBR) - minimum bitrate
  664. $thisfile_mpeg_audio_lame['bitrate_min'] = $thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate'];
  665. }
  666. // bytes $B1-$B3 Encoder delays
  667. $EncoderDelays = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB1, 3));
  668. $thisfile_mpeg_audio_lame['encoder_delay'] = ($EncoderDelays & 0xFFF000) >> 12;
  669. $thisfile_mpeg_audio_lame['end_padding'] = $EncoderDelays & 0x000FFF;
  670. // byte $B4 Misc
  671. $MiscByte = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB4, 1));
  672. $thisfile_mpeg_audio_lame_raw['noise_shaping'] = ($MiscByte & 0x03);
  673. $thisfile_mpeg_audio_lame_raw['stereo_mode'] = ($MiscByte & 0x1C) >> 2;
  674. $thisfile_mpeg_audio_lame_raw['not_optimal_quality'] = ($MiscByte & 0x20) >> 5;
  675. $thisfile_mpeg_audio_lame_raw['source_sample_freq'] = ($MiscByte & 0xC0) >> 6;
  676. $thisfile_mpeg_audio_lame['noise_shaping'] = $thisfile_mpeg_audio_lame_raw['noise_shaping'];
  677. $thisfile_mpeg_audio_lame['stereo_mode'] = self::LAMEmiscStereoModeLookup($thisfile_mpeg_audio_lame_raw['stereo_mode']);
  678. $thisfile_mpeg_audio_lame['not_optimal_quality'] = (bool) $thisfile_mpeg_audio_lame_raw['not_optimal_quality'];
  679. $thisfile_mpeg_audio_lame['source_sample_freq'] = self::LAMEmiscSourceSampleFrequencyLookup($thisfile_mpeg_audio_lame_raw['source_sample_freq']);
  680. // byte $B5 MP3 Gain
  681. $thisfile_mpeg_audio_lame_raw['mp3_gain'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB5, 1), false, true);
  682. $thisfile_mpeg_audio_lame['mp3_gain_db'] = (getid3_lib::RGADamplitude2dB(2) / 4) * $thisfile_mpeg_audio_lame_raw['mp3_gain'];
  683. $thisfile_mpeg_audio_lame['mp3_gain_factor'] = pow(2, ($thisfile_mpeg_audio_lame['mp3_gain_db'] / 6));
  684. // bytes $B6-$B7 Preset and surround info
  685. $PresetSurroundBytes = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB6, 2));
  686. // Reserved = ($PresetSurroundBytes & 0xC000);
  687. $thisfile_mpeg_audio_lame_raw['surround_info'] = ($PresetSurroundBytes & 0x3800);
  688. $thisfile_mpeg_audio_lame['surround_info'] = self::LAMEsurroundInfoLookup($thisfile_mpeg_audio_lame_raw['surround_info']);
  689. $thisfile_mpeg_audio_lame['preset_used_id'] = ($PresetSurroundBytes & 0x07FF);
  690. $thisfile_mpeg_audio_lame['preset_used'] = self::LAMEpresetUsedLookup($thisfile_mpeg_audio_lame);
  691. if (!empty($thisfile_mpeg_audio_lame['preset_used_id']) && empty($thisfile_mpeg_audio_lame['preset_used'])) {
  692. $info['warning'][] = 'Unknown LAME preset used ('.$thisfile_mpeg_audio_lame['preset_used_id'].') - please report to info@getid3.org';
  693. }
  694. if (($thisfile_mpeg_audio_lame['short_version'] == 'LAME3.90.') && !empty($thisfile_mpeg_audio_lame['preset_used_id'])) {
  695. // this may change if 3.90.4 ever comes out
  696. $thisfile_mpeg_audio_lame['short_version'] = 'LAME3.90.3';
  697. }
  698. // bytes $B8-$BB MusicLength
  699. $thisfile_mpeg_audio_lame['audio_bytes'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB8, 4));
  700. $ExpectedNumberOfAudioBytes = (($thisfile_mpeg_audio_lame['audio_bytes'] > 0) ? $thisfile_mpeg_audio_lame['audio_bytes'] : $thisfile_mpeg_audio['VBR_bytes']);
  701. // bytes $BC-$BD MusicCRC
  702. $thisfile_mpeg_audio_lame['music_crc'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xBC, 2));
  703. // bytes $BE-$BF CRC-16 of Info Tag
  704. $thisfile_mpeg_audio_lame['lame_tag_crc'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xBE, 2));
  705. // LAME CBR
  706. if ($thisfile_mpeg_audio_lame_raw['vbr_method'] == 1) {
  707. $thisfile_mpeg_audio['bitrate_mode'] = 'cbr';
  708. $thisfile_mpeg_audio['bitrate'] = self::ClosestStandardMP3Bitrate($thisfile_mpeg_audio['bitrate']);
  709. $info['audio']['bitrate'] = $thisfile_mpeg_audio['bitrate'];
  710. //if (empty($thisfile_mpeg_audio['bitrate']) || (!empty($thisfile_mpeg_audio_lame['bitrate_min']) && ($thisfile_mpeg_audio_lame['bitrate_min'] != 255))) {
  711. // $thisfile_mpeg_audio['bitrate'] = $thisfile_mpeg_audio_lame['bitrate_min'];
  712. //}
  713. }
  714. }
  715. }
  716. } else {
  717. // not Fraunhofer or Xing VBR methods, most likely CBR (but could be VBR with no header)
  718. $thisfile_mpeg_audio['bitrate_mode'] = 'cbr';
  719. if ($recursivesearch) {
  720. $thisfile_mpeg_audio['bitrate_mode'] = 'vbr';
  721. if ($this->RecursiveFrameScanning($offset, $nextframetestoffset, true)) {
  722. $recursivesearch = false;
  723. $thisfile_mpeg_audio['bitrate_mode'] = 'cbr';
  724. }
  725. if ($thisfile_mpeg_audio['bitrate_mode'] == 'vbr') {
  726. $info['warning'][] = 'VBR file with no VBR header. Bitrate values calculated from actual frame bitrates.';
  727. }
  728. }
  729. }
  730. }
  731. if (($ExpectedNumberOfAudioBytes > 0) && ($ExpectedNumberOfAudioBytes != ($info['avdataend'] - $info['avdataoffset']))) {
  732. if ($ExpectedNumberOfAudioBytes > ($info['avdataend'] - $info['avdataoffset'])) {
  733. if ($this->isDependencyFor('matroska') || $this->isDependencyFor('riff')) {
  734. // ignore, audio data is broken into chunks so will always be data "missing"
  735. }
  736. elseif (($ExpectedNumberOfAudioBytes - ($info['avdataend'] - $info['avdataoffset'])) == 1) {
  737. $this->warning('Last byte of data truncated (this is a known bug in Meracl ID3 Tag Writer before v1.3.5)');
  738. }
  739. else {
  740. $this->warning('Probable truncated file: expecting '.$ExpectedNumberOfAudioBytes.' bytes of audio data, only found '.($info['avdataend'] - $info['avdataoffset']).' (short by '.($ExpectedNumberOfAudioBytes - ($info['avdataend'] - $info['avdataoffset'])).' bytes)');
  741. }
  742. } else {
  743. if ((($info['avda…

Large files files are truncated, but you can click here to view the full file