module.audio.mpc.php 23 KB

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