module.audio.monkey.php 11 KB

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