PageRenderTime 74ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/ribcage-includes/getid3/module.audio.mpc.php

https://github.com/ericaustinlee/ribcage
PHP | 290 lines | 195 code | 57 blank | 38 comment | 27 complexity | 22c030a7d5ef25752bb5909f237ac2b1 MD5 | raw 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. /////////////////////////////////////////////////////////////////
  7. // See readme.txt for more details //
  8. /////////////////////////////////////////////////////////////////
  9. // //
  10. // module.audio.mpc.php //
  11. // module for analyzing Musepack/MPEG+ Audio files //
  12. // dependencies: NONE //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. class getid3_mpc
  16. {
  17. function getid3_mpc(&$fd, &$ThisFileInfo) {
  18. // http://www.uni-jena.de/~pfk/mpp/sv8/header.html
  19. $ThisFileInfo['mpc']['header'] = array();
  20. $thisfile_mpc_header = &$ThisFileInfo['mpc']['header'];
  21. $ThisFileInfo['fileformat'] = 'mpc';
  22. $ThisFileInfo['audio']['dataformat'] = 'mpc';
  23. $ThisFileInfo['audio']['bitrate_mode'] = 'vbr';
  24. $ThisFileInfo['audio']['channels'] = 2; // the format appears to be hardcoded for stereo only
  25. $ThisFileInfo['audio']['lossless'] = false;
  26. fseek($fd, $ThisFileInfo['avdataoffset'], SEEK_SET);
  27. $thisfile_mpc_header['size'] = 30;
  28. $MPCheaderData = fread($fd, $thisfile_mpc_header['size']);
  29. $offset = 0;
  30. if (substr($MPCheaderData, $offset, 3) == 'MP+') {
  31. // great, this is SV7+
  32. $thisfile_mpc_header['raw']['preamble'] = substr($MPCheaderData, $offset, 3); // should be 'MP+'
  33. $offset += 3;
  34. } elseif (preg_match('/^[\x00\x01\x10\x11\x40\x41\x50\x51\x80\x81\x90\x91\xC0\xC1\xD0\xD1][\x20-37][\x00\x20\x40\x60\x80\xA0\xC0\xE0]/s', substr($MPCheaderData, 0, 4))) {
  35. // this is SV4 - SV6, handle seperately
  36. // Most of this code adapted from Jurgen Faul's MPEGplus source code - thanks Jurgen! :)
  37. $HeaderDWORD[0] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, 0, 4));
  38. $HeaderDWORD[1] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, 4, 4));
  39. // DDDD DDDD CCCC CCCC BBBB BBBB AAAA AAAA
  40. // aaaa aaaa abcd dddd dddd deee eeff ffff
  41. //
  42. // a = bitrate = anything
  43. // b = IS = anything
  44. // c = MS = anything
  45. // d = streamversion = 0000000004 or 0000000005 or 0000000006
  46. // e = maxband = anything
  47. // f = blocksize = 000001 for SV5+, anything(?) for SV4
  48. $thisfile_mpc_header['target_bitrate'] = (($HeaderDWORD[0] & 0xFF800000) >> 23);
  49. $thisfile_mpc_header['intensity_stereo'] = (bool) (($HeaderDWORD[0] & 0x00400000) >> 22);
  50. $thisfile_mpc_header['mid-side_stereo'] = (bool) (($HeaderDWORD[0] & 0x00200000) >> 21);
  51. $thisfile_mpc_header['stream_major_version'] = ($HeaderDWORD[0] & 0x001FF800) >> 11;
  52. $thisfile_mpc_header['stream_minor_version'] = 0; // no sub-version numbers before SV7
  53. $thisfile_mpc_header['max_band'] = ($HeaderDWORD[0] & 0x000007C0) >> 6; // related to lowpass frequency, not sure how it translates exactly
  54. $thisfile_mpc_header['block_size'] = ($HeaderDWORD[0] & 0x0000003F);
  55. switch ($thisfile_mpc_header['stream_major_version']) {
  56. case 4:
  57. $thisfile_mpc_header['frame_count'] = ($HeaderDWORD[1] >> 16);
  58. break;
  59. case 5:
  60. case 6:
  61. $thisfile_mpc_header['frame_count'] = $HeaderDWORD[1];
  62. break;
  63. default:
  64. $ThisFileInfo['error'] = 'Expecting 4, 5 or 6 in version field, found '.$thisfile_mpc_header['stream_major_version'].' instead';
  65. unset($ThisFileInfo['mpc']);
  66. return false;
  67. break;
  68. }
  69. if (($thisfile_mpc_header['stream_major_version'] > 4) && ($thisfile_mpc_header['block_size'] != 1)) {
  70. $ThisFileInfo['warning'][] = 'Block size expected to be 1, actual value found: '.$thisfile_mpc_header['block_size'];
  71. }
  72. $thisfile_mpc_header['sample_rate'] = 44100; // AB: used by all files up to SV7
  73. $ThisFileInfo['audio']['sample_rate'] = $thisfile_mpc_header['sample_rate'];
  74. $thisfile_mpc_header['samples'] = $thisfile_mpc_header['frame_count'] * 1152 * $ThisFileInfo['audio']['channels'];
  75. if ($thisfile_mpc_header['target_bitrate'] == 0) {
  76. $ThisFileInfo['audio']['bitrate_mode'] = 'vbr';
  77. } else {
  78. $ThisFileInfo['audio']['bitrate_mode'] = 'cbr';
  79. }
  80. $ThisFileInfo['mpc']['bitrate'] = ($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']) * 8 * 44100 / $thisfile_mpc_header['frame_count'] / 1152;
  81. $ThisFileInfo['audio']['bitrate'] = $ThisFileInfo['mpc']['bitrate'];
  82. $ThisFileInfo['audio']['encoder'] = 'SV'.$thisfile_mpc_header['stream_major_version'];
  83. return true;
  84. } else {
  85. $ThisFileInfo['error'][] = 'Expecting "MP+" at offset '.$ThisFileInfo['avdataoffset'].', found "'.substr($MPCheaderData, $offset, 3).'"';
  86. unset($ThisFileInfo['fileformat']);
  87. unset($ThisFileInfo['mpc']);
  88. return false;
  89. }
  90. // Continue with SV7+ handling
  91. $StreamVersionByte = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 1));
  92. $offset += 1;
  93. $thisfile_mpc_header['stream_major_version'] = ($StreamVersionByte & 0x0F);
  94. $thisfile_mpc_header['stream_minor_version'] = ($StreamVersionByte & 0xF0) >> 4;
  95. $thisfile_mpc_header['frame_count'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 4));
  96. $offset += 4;
  97. switch ($thisfile_mpc_header['stream_major_version']) {
  98. case 7:
  99. //$ThisFileInfo['fileformat'] = 'SV7';
  100. break;
  101. default:
  102. $ThisFileInfo['error'][] = 'Only Musepack SV7 supported';
  103. return false;
  104. }
  105. $FlagsDWORD1 = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 4));
  106. $offset += 4;
  107. $thisfile_mpc_header['intensity_stereo'] = (bool) (($FlagsDWORD1 & 0x80000000) >> 31);
  108. $thisfile_mpc_header['mid_side_stereo'] = (bool) (($FlagsDWORD1 & 0x40000000) >> 30);
  109. $thisfile_mpc_header['max_subband'] = ($FlagsDWORD1 & 0x3F000000) >> 24;
  110. $thisfile_mpc_header['raw']['profile'] = ($FlagsDWORD1 & 0x00F00000) >> 20;
  111. $thisfile_mpc_header['begin_loud'] = (bool) (($FlagsDWORD1 & 0x00080000) >> 19);
  112. $thisfile_mpc_header['end_loud'] = (bool) (($FlagsDWORD1 & 0x00040000) >> 18);
  113. $thisfile_mpc_header['raw']['sample_rate'] = ($FlagsDWORD1 & 0x00030000) >> 16;
  114. $thisfile_mpc_header['max_level'] = ($FlagsDWORD1 & 0x0000FFFF);
  115. $thisfile_mpc_header['raw']['title_peak'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 2));
  116. $offset += 2;
  117. $thisfile_mpc_header['raw']['title_gain'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 2), true);
  118. $offset += 2;
  119. $thisfile_mpc_header['raw']['album_peak'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 2));
  120. $offset += 2;
  121. $thisfile_mpc_header['raw']['album_gain'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 2), true);
  122. $offset += 2;
  123. $FlagsDWORD2 = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 4));
  124. $offset += 4;
  125. $thisfile_mpc_header['true_gapless'] = (bool) (($FlagsDWORD2 & 0x80000000) >> 31);
  126. $thisfile_mpc_header['last_frame_length'] = ($FlagsDWORD2 & 0x7FF00000) >> 20;
  127. $thisfile_mpc_header['raw']['not_sure_what'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 3));
  128. $offset += 3;
  129. $thisfile_mpc_header['raw']['encoder_version'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 1));
  130. $offset += 1;
  131. $thisfile_mpc_header['profile'] = $this->MPCprofileNameLookup($thisfile_mpc_header['raw']['profile']);
  132. $thisfile_mpc_header['sample_rate'] = $this->MPCfrequencyLookup($thisfile_mpc_header['raw']['sample_rate']);
  133. if ($thisfile_mpc_header['sample_rate'] == 0) {
  134. $ThisFileInfo['error'][] = 'Corrupt MPC file: frequency == zero';
  135. return false;
  136. }
  137. $ThisFileInfo['audio']['sample_rate'] = $thisfile_mpc_header['sample_rate'];
  138. $thisfile_mpc_header['samples'] = ((($thisfile_mpc_header['frame_count'] - 1) * 1152) + $thisfile_mpc_header['last_frame_length']) * $ThisFileInfo['audio']['channels'];
  139. $ThisFileInfo['playtime_seconds'] = ($thisfile_mpc_header['samples'] / $ThisFileInfo['audio']['channels']) / $ThisFileInfo['audio']['sample_rate'];
  140. if ($ThisFileInfo['playtime_seconds'] == 0) {
  141. $ThisFileInfo['error'][] = 'Corrupt MPC file: playtime_seconds == zero';
  142. return false;
  143. }
  144. // add size of file header to avdataoffset - calc bitrate correctly + MD5 data
  145. $ThisFileInfo['avdataoffset'] += $thisfile_mpc_header['size'];
  146. $ThisFileInfo['audio']['bitrate'] = (($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']) * 8) / $ThisFileInfo['playtime_seconds'];
  147. $thisfile_mpc_header['title_peak'] = $thisfile_mpc_header['raw']['title_peak'];
  148. $thisfile_mpc_header['title_peak_db'] = $this->MPCpeakDBLookup($thisfile_mpc_header['title_peak']);
  149. if ($thisfile_mpc_header['raw']['title_gain'] < 0) {
  150. $thisfile_mpc_header['title_gain_db'] = (float) (32768 + $thisfile_mpc_header['raw']['title_gain']) / -100;
  151. } else {
  152. $thisfile_mpc_header['title_gain_db'] = (float) $thisfile_mpc_header['raw']['title_gain'] / 100;
  153. }
  154. $thisfile_mpc_header['album_peak'] = $thisfile_mpc_header['raw']['album_peak'];
  155. $thisfile_mpc_header['album_peak_db'] = $this->MPCpeakDBLookup($thisfile_mpc_header['album_peak']);
  156. if ($thisfile_mpc_header['raw']['album_gain'] < 0) {
  157. $thisfile_mpc_header['album_gain_db'] = (float) (32768 + $thisfile_mpc_header['raw']['album_gain']) / -100;
  158. } else {
  159. $thisfile_mpc_header['album_gain_db'] = (float) $thisfile_mpc_header['raw']['album_gain'] / 100;;
  160. }
  161. $thisfile_mpc_header['encoder_version'] = $this->MPCencoderVersionLookup($thisfile_mpc_header['raw']['encoder_version']);
  162. $ThisFileInfo['replay_gain']['track']['adjustment'] = $thisfile_mpc_header['title_gain_db'];
  163. $ThisFileInfo['replay_gain']['album']['adjustment'] = $thisfile_mpc_header['album_gain_db'];
  164. if ($thisfile_mpc_header['title_peak'] > 0) {
  165. $ThisFileInfo['replay_gain']['track']['peak'] = $thisfile_mpc_header['title_peak'];
  166. } elseif (round($thisfile_mpc_header['max_level'] * 1.18) > 0) {
  167. $ThisFileInfo['replay_gain']['track']['peak'] = getid3_lib::CastAsInt(round($thisfile_mpc_header['max_level'] * 1.18)); // why? I don't know - see mppdec.c
  168. }
  169. if ($thisfile_mpc_header['album_peak'] > 0) {
  170. $ThisFileInfo['replay_gain']['album']['peak'] = $thisfile_mpc_header['album_peak'];
  171. }
  172. //$ThisFileInfo['audio']['encoder'] = 'SV'.$thisfile_mpc_header['stream_major_version'].'.'.$thisfile_mpc_header['stream_minor_version'].', '.$thisfile_mpc_header['encoder_version'];
  173. $ThisFileInfo['audio']['encoder'] = $thisfile_mpc_header['encoder_version'];
  174. $ThisFileInfo['audio']['encoder_options'] = $thisfile_mpc_header['profile'];
  175. return true;
  176. }
  177. function MPCprofileNameLookup($profileid) {
  178. static $MPCprofileNameLookup = array(
  179. 0 => 'no profile',
  180. 1 => 'Experimental',
  181. 2 => 'unused',
  182. 3 => 'unused',
  183. 4 => 'unused',
  184. 5 => 'below Telephone (q = 0.0)',
  185. 6 => 'below Telephone (q = 1.0)',
  186. 7 => 'Telephone (q = 2.0)',
  187. 8 => 'Thumb (q = 3.0)',
  188. 9 => 'Radio (q = 4.0)',
  189. 10 => 'Standard (q = 5.0)',
  190. 11 => 'Extreme (q = 6.0)',
  191. 12 => 'Insane (q = 7.0)',
  192. 13 => 'BrainDead (q = 8.0)',
  193. 14 => 'above BrainDead (q = 9.0)',
  194. 15 => 'above BrainDead (q = 10.0)'
  195. );
  196. return (isset($MPCprofileNameLookup[$profileid]) ? $MPCprofileNameLookup[$profileid] : 'invalid');
  197. }
  198. function MPCfrequencyLookup($frequencyid) {
  199. static $MPCfrequencyLookup = array(
  200. 0 => 44100,
  201. 1 => 48000,
  202. 2 => 37800,
  203. 3 => 32000
  204. );
  205. return (isset($MPCfrequencyLookup[$frequencyid]) ? $MPCfrequencyLookup[$frequencyid] : 'invalid');
  206. }
  207. function MPCpeakDBLookup($intvalue) {
  208. if ($intvalue > 0) {
  209. return ((log10($intvalue) / log10(2)) - 15) * 6;
  210. }
  211. return false;
  212. }
  213. function MPCencoderVersionLookup($encoderversion) {
  214. //Encoder version * 100 (106 = 1.06)
  215. //EncoderVersion % 10 == 0 Release (1.0)
  216. //EncoderVersion % 2 == 0 Beta (1.06)
  217. //EncoderVersion % 2 == 1 Alpha (1.05a...z)
  218. if ($encoderversion == 0) {
  219. // very old version, not known exactly which
  220. return 'Buschmann v1.7.0-v1.7.9 or Klemm v0.90-v1.05';
  221. }
  222. if (($encoderversion % 10) == 0) {
  223. // release version
  224. return number_format($encoderversion / 100, 2);
  225. } elseif (($encoderversion % 2) == 0) {
  226. // beta version
  227. return number_format($encoderversion / 100, 2).' beta';
  228. }
  229. // alpha version
  230. return number_format($encoderversion / 100, 2).' alpha';
  231. }
  232. }
  233. ?>