module.audio.monkey.php 11 KB

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