module.audio.mpc.php 23 KB

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