module.audio.monkey.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.monkey.php //
  11. // module for analyzing Monkey's 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_monkey extends getid3_handler
  19. {
  20. /**
  21. * @return bool
  22. */
  23. public function Analyze() {
  24. $info = &$this->getid3->info;
  25. // based loosely on code from TMonkey by Jurgen Faul <jfaulØgmx*de>
  26. // http://jfaul.de/atl or http://j-faul.virtualave.net/atl/atl.html
  27. $info['fileformat'] = 'mac';
  28. $info['audio']['dataformat'] = 'mac';
  29. $info['audio']['bitrate_mode'] = 'vbr';
  30. $info['audio']['lossless'] = true;
  31. $info['monkeys_audio']['raw'] = array();
  32. $thisfile_monkeysaudio = &$info['monkeys_audio'];
  33. $thisfile_monkeysaudio_raw = &$thisfile_monkeysaudio['raw'];
  34. $this->fseek($info['avdataoffset']);
  35. $MACheaderData = $this->fread(74);
  36. $thisfile_monkeysaudio_raw['magic'] = substr($MACheaderData, 0, 4);
  37. $magic = 'MAC ';
  38. if ($thisfile_monkeysaudio_raw['magic'] != $magic) {
  39. $this->error('Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($thisfile_monkeysaudio_raw['magic']).'"');
  40. unset($info['fileformat']);
  41. return false;
  42. }
  43. $thisfile_monkeysaudio_raw['nVersion'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, 4, 2)); // appears to be uint32 in 3.98+
  44. if ($thisfile_monkeysaudio_raw['nVersion'] < 3980) {
  45. $thisfile_monkeysaudio_raw['nCompressionLevel'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, 6, 2));
  46. $thisfile_monkeysaudio_raw['nFormatFlags'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, 8, 2));
  47. $thisfile_monkeysaudio_raw['nChannels'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, 10, 2));
  48. $thisfile_monkeysaudio_raw['nSampleRate'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, 12, 4));
  49. $thisfile_monkeysaudio_raw['nHeaderDataBytes'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, 16, 4));
  50. $thisfile_monkeysaudio_raw['nWAVTerminatingBytes'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, 20, 4));
  51. $thisfile_monkeysaudio_raw['nTotalFrames'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, 24, 4));
  52. $thisfile_monkeysaudio_raw['nFinalFrameSamples'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, 28, 4));
  53. $thisfile_monkeysaudio_raw['nPeakLevel'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, 32, 4));
  54. $thisfile_monkeysaudio_raw['nSeekElements'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, 38, 2));
  55. $offset = 8;
  56. } else {
  57. $offset = 8;
  58. // APE_DESCRIPTOR
  59. $thisfile_monkeysaudio_raw['nDescriptorBytes'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4));
  60. $offset += 4;
  61. $thisfile_monkeysaudio_raw['nHeaderBytes'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4));
  62. $offset += 4;
  63. $thisfile_monkeysaudio_raw['nSeekTableBytes'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4));
  64. $offset += 4;
  65. $thisfile_monkeysaudio_raw['nHeaderDataBytes'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4));
  66. $offset += 4;
  67. $thisfile_monkeysaudio_raw['nAPEFrameDataBytes'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4));
  68. $offset += 4;
  69. $thisfile_monkeysaudio_raw['nAPEFrameDataBytesHigh'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4));
  70. $offset += 4;
  71. $thisfile_monkeysaudio_raw['nTerminatingDataBytes'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4));
  72. $offset += 4;
  73. $thisfile_monkeysaudio_raw['cFileMD5'] = substr($MACheaderData, $offset, 16);
  74. $offset += 16;
  75. // APE_HEADER
  76. $thisfile_monkeysaudio_raw['nCompressionLevel'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 2));
  77. $offset += 2;
  78. $thisfile_monkeysaudio_raw['nFormatFlags'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 2));
  79. $offset += 2;
  80. $thisfile_monkeysaudio_raw['nBlocksPerFrame'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4));
  81. $offset += 4;
  82. $thisfile_monkeysaudio_raw['nFinalFrameBlocks'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4));
  83. $offset += 4;
  84. $thisfile_monkeysaudio_raw['nTotalFrames'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4));
  85. $offset += 4;
  86. $thisfile_monkeysaudio_raw['nBitsPerSample'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 2));
  87. $offset += 2;
  88. $thisfile_monkeysaudio_raw['nChannels'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 2));
  89. $offset += 2;
  90. $thisfile_monkeysaudio_raw['nSampleRate'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4));
  91. $offset += 4;
  92. }
  93. $thisfile_monkeysaudio['flags']['8-bit'] = (bool) ($thisfile_monkeysaudio_raw['nFormatFlags'] & 0x0001);
  94. $thisfile_monkeysaudio['flags']['crc-32'] = (bool) ($thisfile_monkeysaudio_raw['nFormatFlags'] & 0x0002);
  95. $thisfile_monkeysaudio['flags']['peak_level'] = (bool) ($thisfile_monkeysaudio_raw['nFormatFlags'] & 0x0004);
  96. $thisfile_monkeysaudio['flags']['24-bit'] = (bool) ($thisfile_monkeysaudio_raw['nFormatFlags'] & 0x0008);
  97. $thisfile_monkeysaudio['flags']['seek_elements'] = (bool) ($thisfile_monkeysaudio_raw['nFormatFlags'] & 0x0010);
  98. $thisfile_monkeysaudio['flags']['no_wav_header'] = (bool) ($thisfile_monkeysaudio_raw['nFormatFlags'] & 0x0020);
  99. $thisfile_monkeysaudio['version'] = $thisfile_monkeysaudio_raw['nVersion'] / 1000;
  100. $thisfile_monkeysaudio['compression'] = $this->MonkeyCompressionLevelNameLookup($thisfile_monkeysaudio_raw['nCompressionLevel']);
  101. if ($thisfile_monkeysaudio_raw['nVersion'] < 3980) {
  102. $thisfile_monkeysaudio['samples_per_frame'] = $this->MonkeySamplesPerFrame($thisfile_monkeysaudio_raw['nVersion'], $thisfile_monkeysaudio_raw['nCompressionLevel']);
  103. }
  104. $thisfile_monkeysaudio['bits_per_sample'] = ($thisfile_monkeysaudio['flags']['24-bit'] ? 24 : ($thisfile_monkeysaudio['flags']['8-bit'] ? 8 : 16));
  105. $thisfile_monkeysaudio['channels'] = $thisfile_monkeysaudio_raw['nChannels'];
  106. $info['audio']['channels'] = $thisfile_monkeysaudio['channels'];
  107. $thisfile_monkeysaudio['sample_rate'] = $thisfile_monkeysaudio_raw['nSampleRate'];
  108. if ($thisfile_monkeysaudio['sample_rate'] == 0) {
  109. $this->error('Corrupt MAC file: frequency == zero');
  110. return false;
  111. }
  112. $info['audio']['sample_rate'] = $thisfile_monkeysaudio['sample_rate'];
  113. if ($thisfile_monkeysaudio['flags']['peak_level']) {
  114. $thisfile_monkeysaudio['peak_level'] = $thisfile_monkeysaudio_raw['nPeakLevel'];
  115. $thisfile_monkeysaudio['peak_ratio'] = $thisfile_monkeysaudio['peak_level'] / pow(2, $thisfile_monkeysaudio['bits_per_sample'] - 1);
  116. }
  117. if ($thisfile_monkeysaudio_raw['nVersion'] >= 3980) {
  118. $thisfile_monkeysaudio['samples'] = (($thisfile_monkeysaudio_raw['nTotalFrames'] - 1) * $thisfile_monkeysaudio_raw['nBlocksPerFrame']) + $thisfile_monkeysaudio_raw['nFinalFrameBlocks'];
  119. } else {
  120. $thisfile_monkeysaudio['samples'] = (($thisfile_monkeysaudio_raw['nTotalFrames'] - 1) * $thisfile_monkeysaudio['samples_per_frame']) + $thisfile_monkeysaudio_raw['nFinalFrameSamples'];
  121. }
  122. $thisfile_monkeysaudio['playtime'] = $thisfile_monkeysaudio['samples'] / $thisfile_monkeysaudio['sample_rate'];
  123. if ($thisfile_monkeysaudio['playtime'] == 0) {
  124. $this->error('Corrupt MAC file: playtime == zero');
  125. return false;
  126. }
  127. $info['playtime_seconds'] = $thisfile_monkeysaudio['playtime'];
  128. $thisfile_monkeysaudio['compressed_size'] = $info['avdataend'] - $info['avdataoffset'];
  129. $thisfile_monkeysaudio['uncompressed_size'] = $thisfile_monkeysaudio['samples'] * $thisfile_monkeysaudio['channels'] * ($thisfile_monkeysaudio['bits_per_sample'] / 8);
  130. if ($thisfile_monkeysaudio['uncompressed_size'] == 0) {
  131. $this->error('Corrupt MAC file: uncompressed_size == zero');
  132. return false;
  133. }
  134. $thisfile_monkeysaudio['compression_ratio'] = $thisfile_monkeysaudio['compressed_size'] / ($thisfile_monkeysaudio['uncompressed_size'] + $thisfile_monkeysaudio_raw['nHeaderDataBytes']);
  135. $thisfile_monkeysaudio['bitrate'] = (($thisfile_monkeysaudio['samples'] * $thisfile_monkeysaudio['channels'] * $thisfile_monkeysaudio['bits_per_sample']) / $thisfile_monkeysaudio['playtime']) * $thisfile_monkeysaudio['compression_ratio'];
  136. $info['audio']['bitrate'] = $thisfile_monkeysaudio['bitrate'];
  137. // add size of MAC header to avdataoffset
  138. if ($thisfile_monkeysaudio_raw['nVersion'] >= 3980) {
  139. $info['avdataoffset'] += $thisfile_monkeysaudio_raw['nDescriptorBytes'];
  140. $info['avdataoffset'] += $thisfile_monkeysaudio_raw['nHeaderBytes'];
  141. $info['avdataoffset'] += $thisfile_monkeysaudio_raw['nSeekTableBytes'];
  142. $info['avdataoffset'] += $thisfile_monkeysaudio_raw['nHeaderDataBytes'];
  143. $info['avdataend'] -= $thisfile_monkeysaudio_raw['nTerminatingDataBytes'];
  144. } else {
  145. $info['avdataoffset'] += $offset;
  146. }
  147. if ($thisfile_monkeysaudio_raw['nVersion'] >= 3980) {
  148. if ($thisfile_monkeysaudio_raw['cFileMD5'] === str_repeat("\x00", 16)) {
  149. //$this->warning('cFileMD5 is null');
  150. } else {
  151. $info['md5_data_source'] = '';
  152. $md5 = $thisfile_monkeysaudio_raw['cFileMD5'];
  153. for ($i = 0; $i < strlen($md5); $i++) {
  154. $info['md5_data_source'] .= str_pad(dechex(ord($md5[$i])), 2, '00', STR_PAD_LEFT);
  155. }
  156. if (!preg_match('/^[0-9a-f]{32}$/', $info['md5_data_source'])) {
  157. unset($info['md5_data_source']);
  158. }
  159. }
  160. }
  161. $info['audio']['bits_per_sample'] = $thisfile_monkeysaudio['bits_per_sample'];
  162. $info['audio']['encoder'] = 'MAC v'.number_format($thisfile_monkeysaudio['version'], 2);
  163. $info['audio']['encoder_options'] = ucfirst($thisfile_monkeysaudio['compression']).' compression';
  164. return true;
  165. }
  166. /**
  167. * @param int $compressionlevel
  168. *
  169. * @return string
  170. */
  171. public function MonkeyCompressionLevelNameLookup($compressionlevel) {
  172. static $MonkeyCompressionLevelNameLookup = array(
  173. 0 => 'unknown',
  174. 1000 => 'fast',
  175. 2000 => 'normal',
  176. 3000 => 'high',
  177. 4000 => 'extra-high',
  178. 5000 => 'insane'
  179. );
  180. return (isset($MonkeyCompressionLevelNameLookup[$compressionlevel]) ? $MonkeyCompressionLevelNameLookup[$compressionlevel] : 'invalid');
  181. }
  182. /**
  183. * @param int $versionid
  184. * @param int $compressionlevel
  185. *
  186. * @return int
  187. */
  188. public function MonkeySamplesPerFrame($versionid, $compressionlevel) {
  189. if ($versionid >= 3950) {
  190. return 73728 * 4;
  191. } elseif ($versionid >= 3900) {
  192. return 73728;
  193. } elseif (($versionid >= 3800) && ($compressionlevel == 4000)) {
  194. return 73728;
  195. } else {
  196. return 9216;
  197. }
  198. }
  199. }