PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/getid3/module.audio.monkey.php

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