module.audio.mpc.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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.mpc.php //
  12. // module for analyzing Musepack/MPEG+ Audio files //
  13. // dependencies: NONE //
  14. // ///
  15. /////////////////////////////////////////////////////////////////
  16. class getid3_mpc extends getid3_handler
  17. {
  18. public function Analyze() {
  19. $info = &$this->getid3->info;
  20. $info['mpc']['header'] = array();
  21. $thisfile_mpc_header = &$info['mpc']['header'];
  22. $info['fileformat'] = 'mpc';
  23. $info['audio']['dataformat'] = 'mpc';
  24. $info['audio']['bitrate_mode'] = 'vbr';
  25. $info['audio']['channels'] = 2; // up to SV7 the format appears to have been hardcoded for stereo only
  26. $info['audio']['lossless'] = false;
  27. $this->fseek($info['avdataoffset']);
  28. $MPCheaderData = $this->fread(4);
  29. $info['mpc']['header']['preamble'] = substr($MPCheaderData, 0, 4); // should be 'MPCK' (SV8) or 'MP+' (SV7), otherwise possible stream data (SV4-SV6)
  30. if (preg_match('#^MPCK#', $info['mpc']['header']['preamble'])) {
  31. // this is SV8
  32. return $this->ParseMPCsv8();
  33. } elseif (preg_match('#^MP\+#', $info['mpc']['header']['preamble'])) {
  34. // this is SV7
  35. return $this->ParseMPCsv7();
  36. } 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', $MPCheaderData)) {
  37. // this is SV4 - SV6, handle seperately
  38. return $this->ParseMPCsv6();
  39. } else {
  40. $info['error'][] = 'Expecting "MP+" or "MPCK" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes(substr($MPCheaderData, 0, 4)).'"';
  41. unset($info['fileformat']);
  42. unset($info['mpc']);
  43. return false;
  44. }
  45. return false;
  46. }
  47. public function ParseMPCsv8() {
  48. // this is SV8
  49. // http://trac.musepack.net/trac/wiki/SV8Specification
  50. $info = &$this->getid3->info;
  51. $thisfile_mpc_header = &$info['mpc']['header'];
  52. $keyNameSize = 2;
  53. $maxHandledPacketLength = 9; // specs say: "n*8; 0 < n < 10"
  54. $offset = $this->ftell();
  55. while ($offset < $info['avdataend']) {
  56. $thisPacket = array();
  57. $thisPacket['offset'] = $offset;
  58. $packet_offset = 0;
  59. // Size is a variable-size field, could be 1-4 bytes (possibly more?)
  60. // read enough data in and figure out the exact size later
  61. $MPCheaderData = $this->fread($keyNameSize + $maxHandledPacketLength);
  62. $packet_offset += $keyNameSize;
  63. $thisPacket['key'] = substr($MPCheaderData, 0, $keyNameSize);
  64. $thisPacket['key_name'] = $this->MPCsv8PacketName($thisPacket['key']);
  65. if ($thisPacket['key'] == $thisPacket['key_name']) {
  66. $info['error'][] = 'Found unexpected key value "'.$thisPacket['key'].'" at offset '.$thisPacket['offset'];
  67. return false;
  68. }
  69. $packetLength = 0;
  70. $thisPacket['packet_size'] = $this->SV8variableLengthInteger(substr($MPCheaderData, $keyNameSize), $packetLength); // includes keyname and packet_size field
  71. if ($thisPacket['packet_size'] === false) {
  72. $info['error'][] = 'Did not find expected packet length within '.$maxHandledPacketLength.' bytes at offset '.($thisPacket['offset'] + $keyNameSize);
  73. return false;
  74. }
  75. $packet_offset += $packetLength;
  76. $offset += $thisPacket['packet_size'];
  77. switch ($thisPacket['key']) {
  78. case 'SH': // Stream Header
  79. $moreBytesToRead = $thisPacket['packet_size'] - $keyNameSize - $maxHandledPacketLength;
  80. if ($moreBytesToRead > 0) {
  81. $MPCheaderData .= $this->fread($moreBytesToRead);
  82. }
  83. $thisPacket['crc'] = getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 4));
  84. $packet_offset += 4;
  85. $thisPacket['stream_version'] = getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 1));
  86. $packet_offset += 1;
  87. $packetLength = 0;
  88. $thisPacket['sample_count'] = $this->SV8variableLengthInteger(substr($MPCheaderData, $packet_offset, $maxHandledPacketLength), $packetLength);
  89. $packet_offset += $packetLength;
  90. $packetLength = 0;
  91. $thisPacket['beginning_silence'] = $this->SV8variableLengthInteger(substr($MPCheaderData, $packet_offset, $maxHandledPacketLength), $packetLength);
  92. $packet_offset += $packetLength;
  93. $otherUsefulData = getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 2));
  94. $packet_offset += 2;
  95. $thisPacket['sample_frequency_raw'] = (($otherUsefulData & 0xE000) >> 13);
  96. $thisPacket['max_bands_used'] = (($otherUsefulData & 0x1F00) >> 8);
  97. $thisPacket['channels'] = (($otherUsefulData & 0x00F0) >> 4) + 1;
  98. $thisPacket['ms_used'] = (bool) (($otherUsefulData & 0x0008) >> 3);
  99. $thisPacket['audio_block_frames'] = (($otherUsefulData & 0x0007) >> 0);
  100. $thisPacket['sample_frequency'] = $this->MPCfrequencyLookup($thisPacket['sample_frequency_raw']);
  101. $thisfile_mpc_header['mid_side_stereo'] = $thisPacket['ms_used'];
  102. $thisfile_mpc_header['sample_rate'] = $thisPacket['sample_frequency'];
  103. $thisfile_mpc_header['samples'] = $thisPacket['sample_count'];
  104. $thisfile_mpc_header['stream_version_major'] = $thisPacket['stream_version'];
  105. $info['audio']['channels'] = $thisPacket['channels'];
  106. $info['audio']['sample_rate'] = $thisPacket['sample_frequency'];
  107. $info['playtime_seconds'] = $thisPacket['sample_count'] / $thisPacket['sample_frequency'];
  108. $info['audio']['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];
  109. break;
  110. case 'RG': // Replay Gain
  111. $moreBytesToRead = $thisPacket['packet_size'] - $keyNameSize - $maxHandledPacketLength;
  112. if ($moreBytesToRead > 0) {
  113. $MPCheaderData .= $this->fread($moreBytesToRead);
  114. }
  115. $thisPacket['replaygain_version'] = getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 1));
  116. $packet_offset += 1;
  117. $thisPacket['replaygain_title_gain'] = getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 2));
  118. $packet_offset += 2;
  119. $thisPacket['replaygain_title_peak'] = getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 2));
  120. $packet_offset += 2;
  121. $thisPacket['replaygain_album_gain'] = getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 2));
  122. $packet_offset += 2;
  123. $thisPacket['replaygain_album_peak'] = getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 2));
  124. $packet_offset += 2;
  125. if ($thisPacket['replaygain_title_gain']) { $info['replay_gain']['title']['gain'] = $thisPacket['replaygain_title_gain']; }
  126. if ($thisPacket['replaygain_title_peak']) { $info['replay_gain']['title']['peak'] = $thisPacket['replaygain_title_peak']; }
  127. if ($thisPacket['replaygain_album_gain']) { $info['replay_gain']['album']['gain'] = $thisPacket['replaygain_album_gain']; }
  128. if ($thisPacket['replaygain_album_peak']) { $info['replay_gain']['album']['peak'] = $thisPacket['replaygain_album_peak']; }
  129. break;
  130. case 'EI': // Encoder Info
  131. $moreBytesToRead = $thisPacket['packet_size'] - $keyNameSize - $maxHandledPacketLength;
  132. if ($moreBytesToRead > 0) {
  133. $MPCheaderData .= $this->fread($moreBytesToRead);
  134. }
  135. $profile_pns = getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 1));
  136. $packet_offset += 1;
  137. $quality_int = (($profile_pns & 0xF0) >> 4);
  138. $quality_dec = (($profile_pns & 0x0E) >> 3);
  139. $thisPacket['quality'] = (float) $quality_int + ($quality_dec / 8);
  140. $thisPacket['pns_tool'] = (bool) (($profile_pns & 0x01) >> 0);
  141. $thisPacket['version_major'] = getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 1));
  142. $packet_offset += 1;
  143. $thisPacket['version_minor'] = getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 1));
  144. $packet_offset += 1;
  145. $thisPacket['version_build'] = getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 1));
  146. $packet_offset += 1;
  147. $thisPacket['version'] = $thisPacket['version_major'].'.'.$thisPacket['version_minor'].'.'.$thisPacket['version_build'];
  148. $info['audio']['encoder'] = 'MPC v'.$thisPacket['version'].' ('.(($thisPacket['version_minor'] % 2) ? 'unstable' : 'stable').')';
  149. $thisfile_mpc_header['encoder_version'] = $info['audio']['encoder'];
  150. //$thisfile_mpc_header['quality'] = (float) ($thisPacket['quality'] / 1.5875); // values can range from 0.000 to 15.875, mapped to qualities of 0.0 to 10.0
  151. $thisfile_mpc_header['quality'] = (float) ($thisPacket['quality'] - 5); // values can range from 0.000 to 15.875, of which 0..4 are "reserved/experimental", and 5..15 are mapped to qualities of 0.0 to 10.0
  152. break;
  153. case 'SO': // Seek Table Offset
  154. $packetLength = 0;
  155. $thisPacket['seek_table_offset'] = $thisPacket['offset'] + $this->SV8variableLengthInteger(substr($MPCheaderData, $packet_offset, $maxHandledPacketLength), $packetLength);
  156. $packet_offset += $packetLength;
  157. break;
  158. case 'ST': // Seek Table
  159. case 'SE': // Stream End
  160. case 'AP': // Audio Data
  161. // nothing useful here, just skip this packet
  162. $thisPacket = array();
  163. break;
  164. default:
  165. $info['error'][] = 'Found unhandled key type "'.$thisPacket['key'].'" at offset '.$thisPacket['offset'];
  166. return false;
  167. break;
  168. }
  169. if (!empty($thisPacket)) {
  170. $info['mpc']['packets'][] = $thisPacket;
  171. }
  172. $this->fseek($offset);
  173. }
  174. $thisfile_mpc_header['size'] = $offset;
  175. return true;
  176. }
  177. public function ParseMPCsv7() {
  178. // this is SV7
  179. // http://www.uni-jena.de/~pfk/mpp/sv8/header.html
  180. $info = &$this->getid3->info;
  181. $thisfile_mpc_header = &$info['mpc']['header'];
  182. $offset = 0;
  183. $thisfile_mpc_header['size'] = 28;
  184. $MPCheaderData = $info['mpc']['header']['preamble'];
  185. $MPCheaderData .= $this->fread($thisfile_mpc_header['size'] - strlen($info['mpc']['header']['preamble']));
  186. $offset = strlen('MP+');
  187. $StreamVersionByte = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 1));
  188. $offset += 1;
  189. $thisfile_mpc_header['stream_version_major'] = ($StreamVersionByte & 0x0F) >> 0;
  190. $thisfile_mpc_header['stream_version_minor'] = ($StreamVersionByte & 0xF0) >> 4; // should always be 0, subversions no longer exist in SV8
  191. $thisfile_mpc_header['frame_count'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 4));
  192. $offset += 4;
  193. if ($thisfile_mpc_header['stream_version_major'] != 7) {
  194. $info['error'][] = 'Only Musepack SV7 supported (this file claims to be v'.$thisfile_mpc_header['stream_version_major'].')';
  195. return false;
  196. }
  197. $FlagsDWORD1 = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 4));
  198. $offset += 4;
  199. $thisfile_mpc_header['intensity_stereo'] = (bool) (($FlagsDWORD1 & 0x80000000) >> 31);
  200. $thisfile_mpc_header['mid_side_stereo'] = (bool) (($FlagsDWORD1 & 0x40000000) >> 30);
  201. $thisfile_mpc_header['max_subband'] = ($FlagsDWORD1 & 0x3F000000) >> 24;
  202. $thisfile_mpc_header['raw']['profile'] = ($FlagsDWORD1 & 0x00F00000) >> 20;
  203. $thisfile_mpc_header['begin_loud'] = (bool) (($FlagsDWORD1 & 0x00080000) >> 19);
  204. $thisfile_mpc_header['end_loud'] = (bool) (($FlagsDWORD1 & 0x00040000) >> 18);
  205. $thisfile_mpc_header['raw']['sample_rate'] = ($FlagsDWORD1 & 0x00030000) >> 16;
  206. $thisfile_mpc_header['max_level'] = ($FlagsDWORD1 & 0x0000FFFF);
  207. $thisfile_mpc_header['raw']['title_peak'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 2));
  208. $offset += 2;
  209. $thisfile_mpc_header['raw']['title_gain'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 2), true);
  210. $offset += 2;
  211. $thisfile_mpc_header['raw']['album_peak'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 2));
  212. $offset += 2;
  213. $thisfile_mpc_header['raw']['album_gain'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 2), true);
  214. $offset += 2;
  215. $FlagsDWORD2 = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 4));
  216. $offset += 4;
  217. $thisfile_mpc_header['true_gapless'] = (bool) (($FlagsDWORD2 & 0x80000000) >> 31);
  218. $thisfile_mpc_header['last_frame_length'] = ($FlagsDWORD2 & 0x7FF00000) >> 20;
  219. $thisfile_mpc_header['raw']['not_sure_what'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 3));
  220. $offset += 3;
  221. $thisfile_mpc_header['raw']['encoder_version'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 1));
  222. $offset += 1;
  223. $thisfile_mpc_header['profile'] = $this->MPCprofileNameLookup($thisfile_mpc_header['raw']['profile']);
  224. $thisfile_mpc_header['sample_rate'] = $this->MPCfrequencyLookup($thisfile_mpc_header['raw']['sample_rate']);
  225. if ($thisfile_mpc_header['sample_rate'] == 0) {
  226. $info['error'][] = 'Corrupt MPC file: frequency == zero';
  227. return false;
  228. }
  229. $info['audio']['sample_rate'] = $thisfile_mpc_header['sample_rate'];
  230. $thisfile_mpc_header['samples'] = ((($thisfile_mpc_header['frame_count'] - 1) * 1152) + $thisfile_mpc_header['last_frame_length']) * $info['audio']['channels'];
  231. $info['playtime_seconds'] = ($thisfile_mpc_header['samples'] / $info['audio']['channels']) / $info['audio']['sample_rate'];
  232. if ($info['playtime_seconds'] == 0) {
  233. $info['error'][] = 'Corrupt MPC file: playtime_seconds == zero';
  234. return false;
  235. }
  236. // add size of file header to avdataoffset - calc bitrate correctly + MD5 data
  237. $info['avdataoffset'] += $thisfile_mpc_header['size'];
  238. $info['audio']['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];
  239. $thisfile_mpc_header['title_peak'] = $thisfile_mpc_header['raw']['title_peak'];
  240. $thisfile_mpc_header['title_peak_db'] = $this->MPCpeakDBLookup($thisfile_mpc_header['title_peak']);
  241. if ($thisfile_mpc_header['raw']['title_gain'] < 0) {
  242. $thisfile_mpc_header['title_gain_db'] = (float) (32768 + $thisfile_mpc_header['raw']['title_gain']) / -100;
  243. } else {
  244. $thisfile_mpc_header['title_gain_db'] = (float) $thisfile_mpc_header['raw']['title_gain'] / 100;
  245. }
  246. $thisfile_mpc_header['album_peak'] = $thisfile_mpc_header['raw']['album_peak'];
  247. $thisfile_mpc_header['album_peak_db'] = $this->MPCpeakDBLookup($thisfile_mpc_header['album_peak']);
  248. if ($thisfile_mpc_header['raw']['album_gain'] < 0) {
  249. $thisfile_mpc_header['album_gain_db'] = (float) (32768 + $thisfile_mpc_header['raw']['album_gain']) / -100;
  250. } else {
  251. $thisfile_mpc_header['album_gain_db'] = (float) $thisfile_mpc_header['raw']['album_gain'] / 100;;
  252. }
  253. $thisfile_mpc_header['encoder_version'] = $this->MPCencoderVersionLookup($thisfile_mpc_header['raw']['encoder_version']);
  254. $info['replay_gain']['track']['adjustment'] = $thisfile_mpc_header['title_gain_db'];
  255. $info['replay_gain']['album']['adjustment'] = $thisfile_mpc_header['album_gain_db'];
  256. if ($thisfile_mpc_header['title_peak'] > 0) {
  257. $info['replay_gain']['track']['peak'] = $thisfile_mpc_header['title_peak'];
  258. } elseif (round($thisfile_mpc_header['max_level'] * 1.18) > 0) {
  259. $info['replay_gain']['track']['peak'] = getid3_lib::CastAsInt(round($thisfile_mpc_header['max_level'] * 1.18)); // why? I don't know - see mppdec.c
  260. }
  261. if ($thisfile_mpc_header['album_peak'] > 0) {
  262. $info['replay_gain']['album']['peak'] = $thisfile_mpc_header['album_peak'];
  263. }
  264. //$info['audio']['encoder'] = 'SV'.$thisfile_mpc_header['stream_version_major'].'.'.$thisfile_mpc_header['stream_version_minor'].', '.$thisfile_mpc_header['encoder_version'];
  265. $info['audio']['encoder'] = $thisfile_mpc_header['encoder_version'];
  266. $info['audio']['encoder_options'] = $thisfile_mpc_header['profile'];
  267. $thisfile_mpc_header['quality'] = (float) ($thisfile_mpc_header['raw']['profile'] - 5); // values can range from 0 to 15, of which 0..4 are "reserved/experimental", and 5..15 are mapped to qualities of 0.0 to 10.0
  268. return true;
  269. }
  270. public function ParseMPCsv6() {
  271. // this is SV4 - SV6
  272. $info = &$this->getid3->info;
  273. $thisfile_mpc_header = &$info['mpc']['header'];
  274. $offset = 0;
  275. $thisfile_mpc_header['size'] = 8;
  276. $this->fseek($info['avdataoffset']);
  277. $MPCheaderData = $this->fread($thisfile_mpc_header['size']);
  278. // add size of file header to avdataoffset - calc bitrate correctly + MD5 data
  279. $info['avdataoffset'] += $thisfile_mpc_header['size'];
  280. // Most of this code adapted from Jurgen Faul's MPEGplus source code - thanks Jurgen! :)
  281. $HeaderDWORD[0] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, 0, 4));
  282. $HeaderDWORD[1] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, 4, 4));
  283. // DDDD DDDD CCCC CCCC BBBB BBBB AAAA AAAA
  284. // aaaa aaaa abcd dddd dddd deee eeff ffff
  285. //
  286. // a = bitrate = anything
  287. // b = IS = anything
  288. // c = MS = anything
  289. // d = streamversion = 0000000004 or 0000000005 or 0000000006
  290. // e = maxband = anything
  291. // f = blocksize = 000001 for SV5+, anything(?) for SV4
  292. $thisfile_mpc_header['target_bitrate'] = (($HeaderDWORD[0] & 0xFF800000) >> 23);
  293. $thisfile_mpc_header['intensity_stereo'] = (bool) (($HeaderDWORD[0] & 0x00400000) >> 22);
  294. $thisfile_mpc_header['mid_side_stereo'] = (bool) (($HeaderDWORD[0] & 0x00200000) >> 21);
  295. $thisfile_mpc_header['stream_version_major'] = ($HeaderDWORD[0] & 0x001FF800) >> 11;
  296. $thisfile_mpc_header['stream_version_minor'] = 0; // no sub-version numbers before SV7
  297. $thisfile_mpc_header['max_band'] = ($HeaderDWORD[0] & 0x000007C0) >> 6; // related to lowpass frequency, not sure how it translates exactly
  298. $thisfile_mpc_header['block_size'] = ($HeaderDWORD[0] & 0x0000003F);
  299. switch ($thisfile_mpc_header['stream_version_major']) {
  300. case 4:
  301. $thisfile_mpc_header['frame_count'] = ($HeaderDWORD[1] >> 16);
  302. break;
  303. case 5:
  304. case 6:
  305. $thisfile_mpc_header['frame_count'] = $HeaderDWORD[1];
  306. break;
  307. default:
  308. $info['error'] = 'Expecting 4, 5 or 6 in version field, found '.$thisfile_mpc_header['stream_version_major'].' instead';
  309. unset($info['mpc']);
  310. return false;
  311. break;
  312. }
  313. if (($thisfile_mpc_header['stream_version_major'] > 4) && ($thisfile_mpc_header['block_size'] != 1)) {
  314. $info['warning'][] = 'Block size expected to be 1, actual value found: '.$thisfile_mpc_header['block_size'];
  315. }
  316. $thisfile_mpc_header['sample_rate'] = 44100; // AB: used by all files up to SV7
  317. $info['audio']['sample_rate'] = $thisfile_mpc_header['sample_rate'];
  318. $thisfile_mpc_header['samples'] = $thisfile_mpc_header['frame_count'] * 1152 * $info['audio']['channels'];
  319. if ($thisfile_mpc_header['target_bitrate'] == 0) {
  320. $info['audio']['bitrate_mode'] = 'vbr';
  321. } else {
  322. $info['audio']['bitrate_mode'] = 'cbr';
  323. }
  324. $info['mpc']['bitrate'] = ($info['avdataend'] - $info['avdataoffset']) * 8 * 44100 / $thisfile_mpc_header['frame_count'] / 1152;
  325. $info['audio']['bitrate'] = $info['mpc']['bitrate'];
  326. $info['audio']['encoder'] = 'SV'.$thisfile_mpc_header['stream_version_major'];
  327. return true;
  328. }
  329. public function MPCprofileNameLookup($profileid) {
  330. static $MPCprofileNameLookup = array(
  331. 0 => 'no profile',
  332. 1 => 'Experimental',
  333. 2 => 'unused',
  334. 3 => 'unused',
  335. 4 => 'unused',
  336. 5 => 'below Telephone (q = 0.0)',
  337. 6 => 'below Telephone (q = 1.0)',
  338. 7 => 'Telephone (q = 2.0)',
  339. 8 => 'Thumb (q = 3.0)',
  340. 9 => 'Radio (q = 4.0)',
  341. 10 => 'Standard (q = 5.0)',
  342. 11 => 'Extreme (q = 6.0)',
  343. 12 => 'Insane (q = 7.0)',
  344. 13 => 'BrainDead (q = 8.0)',
  345. 14 => 'above BrainDead (q = 9.0)',
  346. 15 => 'above BrainDead (q = 10.0)'
  347. );
  348. return (isset($MPCprofileNameLookup[$profileid]) ? $MPCprofileNameLookup[$profileid] : 'invalid');
  349. }
  350. public function MPCfrequencyLookup($frequencyid) {
  351. static $MPCfrequencyLookup = array(
  352. 0 => 44100,
  353. 1 => 48000,
  354. 2 => 37800,
  355. 3 => 32000
  356. );
  357. return (isset($MPCfrequencyLookup[$frequencyid]) ? $MPCfrequencyLookup[$frequencyid] : 'invalid');
  358. }
  359. public function MPCpeakDBLookup($intvalue) {
  360. if ($intvalue > 0) {
  361. return ((log10($intvalue) / log10(2)) - 15) * 6;
  362. }
  363. return false;
  364. }
  365. public function MPCencoderVersionLookup($encoderversion) {
  366. //Encoder version * 100 (106 = 1.06)
  367. //EncoderVersion % 10 == 0 Release (1.0)
  368. //EncoderVersion % 2 == 0 Beta (1.06)
  369. //EncoderVersion % 2 == 1 Alpha (1.05a...z)
  370. if ($encoderversion == 0) {
  371. // very old version, not known exactly which
  372. return 'Buschmann v1.7.0-v1.7.9 or Klemm v0.90-v1.05';
  373. }
  374. if (($encoderversion % 10) == 0) {
  375. // release version
  376. return number_format($encoderversion / 100, 2);
  377. } elseif (($encoderversion % 2) == 0) {
  378. // beta version
  379. return number_format($encoderversion / 100, 2).' beta';
  380. }
  381. // alpha version
  382. return number_format($encoderversion / 100, 2).' alpha';
  383. }
  384. public function SV8variableLengthInteger($data, &$packetLength, $maxHandledPacketLength=9) {
  385. $packet_size = 0;
  386. for ($packetLength = 1; $packetLength <= $maxHandledPacketLength; $packetLength++) {
  387. // variable-length size field:
  388. // bits, big-endian
  389. // 0xxx xxxx - value 0 to 2^7-1
  390. // 1xxx xxxx 0xxx xxxx - value 0 to 2^14-1
  391. // 1xxx xxxx 1xxx xxxx 0xxx xxxx - value 0 to 2^21-1
  392. // 1xxx xxxx 1xxx xxxx 1xxx xxxx 0xxx xxxx - value 0 to 2^28-1
  393. // ...
  394. $thisbyte = ord(substr($data, ($packetLength - 1), 1));
  395. // look through bytes until find a byte with MSB==0
  396. $packet_size = ($packet_size << 7);
  397. $packet_size = ($packet_size | ($thisbyte & 0x7F));
  398. if (($thisbyte & 0x80) === 0) {
  399. break;
  400. }
  401. if ($packetLength >= $maxHandledPacketLength) {
  402. return false;
  403. }
  404. }
  405. return $packet_size;
  406. }
  407. public function MPCsv8PacketName($packetKey) {
  408. static $MPCsv8PacketName = array();
  409. if (empty($MPCsv8PacketName)) {
  410. $MPCsv8PacketName = array(
  411. 'AP' => 'Audio Packet',
  412. 'CT' => 'Chapter Tag',
  413. 'EI' => 'Encoder Info',
  414. 'RG' => 'Replay Gain',
  415. 'SE' => 'Stream End',
  416. 'SH' => 'Stream Header',
  417. 'SO' => 'Seek Table Offset',
  418. 'ST' => 'Seek Table',
  419. );
  420. }
  421. return (isset($MPCsv8PacketName[$packetKey]) ? $MPCsv8PacketName[$packetKey] : $packetKey);
  422. }
  423. }