afa2eaf27e584759f55db2aa1832ae4c5a1fd515
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 /////////////////////////////////////////////////////////////////
11 // module.audio.monkey.php //
12 // module for analyzing Monkey's Audio files //
13 // dependencies: NONE //
15 /////////////////////////////////////////////////////////////////
18 class getid3_monkey
extends getid3_handler
21 public function Analyze() {
22 $info = &$this->getid3
->info
;
24 // based loosely on code from TMonkey by Jurgen Faul <jfaulØgmx*de>
25 // 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;
32 $info['monkeys_audio']['raw'] = array();
33 $thisfile_monkeysaudio = &$info['monkeys_audio'];
34 $thisfile_monkeysaudio_raw = &$thisfile_monkeysaudio['raw'];
36 $this->fseek($info['avdataoffset']);
37 $MACheaderData = $this->fread(74);
39 $thisfile_monkeysaudio_raw['magic'] = substr($MACheaderData, 0, 4);
41 if ($thisfile_monkeysaudio_raw['magic'] != $magic) {
42 $this->error('Expecting "'.getid3_lib
::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib
::PrintHexBytes($thisfile_monkeysaudio_raw['magic']).'"');
43 unset($info['fileformat']);
46 $thisfile_monkeysaudio_raw['nVersion'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, 4, 2)); // appears to be uint32 in 3.98+
48 if ($thisfile_monkeysaudio_raw['nVersion'] < 3980) {
49 $thisfile_monkeysaudio_raw['nCompressionLevel'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, 6, 2));
50 $thisfile_monkeysaudio_raw['nFormatFlags'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, 8, 2));
51 $thisfile_monkeysaudio_raw['nChannels'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, 10, 2));
52 $thisfile_monkeysaudio_raw['nSampleRate'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, 12, 4));
53 $thisfile_monkeysaudio_raw['nHeaderDataBytes'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, 16, 4));
54 $thisfile_monkeysaudio_raw['nWAVTerminatingBytes'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, 20, 4));
55 $thisfile_monkeysaudio_raw['nTotalFrames'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, 24, 4));
56 $thisfile_monkeysaudio_raw['nFinalFrameSamples'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, 28, 4));
57 $thisfile_monkeysaudio_raw['nPeakLevel'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, 32, 4));
58 $thisfile_monkeysaudio_raw['nSeekElements'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, 38, 2));
63 $thisfile_monkeysaudio_raw['nDescriptorBytes'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, $offset, 4));
65 $thisfile_monkeysaudio_raw['nHeaderBytes'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, $offset, 4));
67 $thisfile_monkeysaudio_raw['nSeekTableBytes'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, $offset, 4));
69 $thisfile_monkeysaudio_raw['nHeaderDataBytes'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, $offset, 4));
71 $thisfile_monkeysaudio_raw['nAPEFrameDataBytes'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, $offset, 4));
73 $thisfile_monkeysaudio_raw['nAPEFrameDataBytesHigh'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, $offset, 4));
75 $thisfile_monkeysaudio_raw['nTerminatingDataBytes'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, $offset, 4));
77 $thisfile_monkeysaudio_raw['cFileMD5'] = substr($MACheaderData, $offset, 16);
81 $thisfile_monkeysaudio_raw['nCompressionLevel'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, $offset, 2));
83 $thisfile_monkeysaudio_raw['nFormatFlags'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, $offset, 2));
85 $thisfile_monkeysaudio_raw['nBlocksPerFrame'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, $offset, 4));
87 $thisfile_monkeysaudio_raw['nFinalFrameBlocks'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, $offset, 4));
89 $thisfile_monkeysaudio_raw['nTotalFrames'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, $offset, 4));
91 $thisfile_monkeysaudio_raw['nBitsPerSample'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, $offset, 2));
93 $thisfile_monkeysaudio_raw['nChannels'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, $offset, 2));
95 $thisfile_monkeysaudio_raw['nSampleRate'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, $offset, 4));
99 $thisfile_monkeysaudio['flags']['8-bit'] = (bool) ($thisfile_monkeysaudio_raw['nFormatFlags'] & 0x0001);
100 $thisfile_monkeysaudio['flags']['crc-32'] = (bool) ($thisfile_monkeysaudio_raw['nFormatFlags'] & 0x0002);
101 $thisfile_monkeysaudio['flags']['peak_level'] = (bool) ($thisfile_monkeysaudio_raw['nFormatFlags'] & 0x0004);
102 $thisfile_monkeysaudio['flags']['24-bit'] = (bool) ($thisfile_monkeysaudio_raw['nFormatFlags'] & 0x0008);
103 $thisfile_monkeysaudio['flags']['seek_elements'] = (bool) ($thisfile_monkeysaudio_raw['nFormatFlags'] & 0x0010);
104 $thisfile_monkeysaudio['flags']['no_wav_header'] = (bool) ($thisfile_monkeysaudio_raw['nFormatFlags'] & 0x0020);
105 $thisfile_monkeysaudio['version'] = $thisfile_monkeysaudio_raw['nVersion'] / 1000;
106 $thisfile_monkeysaudio['compression'] = $this->MonkeyCompressionLevelNameLookup($thisfile_monkeysaudio_raw['nCompressionLevel']);
107 if ($thisfile_monkeysaudio_raw['nVersion'] < 3980) {
108 $thisfile_monkeysaudio['samples_per_frame'] = $this->MonkeySamplesPerFrame($thisfile_monkeysaudio_raw['nVersion'], $thisfile_monkeysaudio_raw['nCompressionLevel']);
110 $thisfile_monkeysaudio['bits_per_sample'] = ($thisfile_monkeysaudio['flags']['24-bit'] ?
24 : ($thisfile_monkeysaudio['flags']['8-bit'] ?
8 : 16));
111 $thisfile_monkeysaudio['channels'] = $thisfile_monkeysaudio_raw['nChannels'];
112 $info['audio']['channels'] = $thisfile_monkeysaudio['channels'];
113 $thisfile_monkeysaudio['sample_rate'] = $thisfile_monkeysaudio_raw['nSampleRate'];
114 if ($thisfile_monkeysaudio['sample_rate'] == 0) {
115 $this->error('Corrupt MAC file: frequency == zero');
118 $info['audio']['sample_rate'] = $thisfile_monkeysaudio['sample_rate'];
119 if ($thisfile_monkeysaudio['flags']['peak_level']) {
120 $thisfile_monkeysaudio['peak_level'] = $thisfile_monkeysaudio_raw['nPeakLevel'];
121 $thisfile_monkeysaudio['peak_ratio'] = $thisfile_monkeysaudio['peak_level'] / pow(2, $thisfile_monkeysaudio['bits_per_sample'] - 1);
123 if ($thisfile_monkeysaudio_raw['nVersion'] >= 3980) {
124 $thisfile_monkeysaudio['samples'] = (($thisfile_monkeysaudio_raw['nTotalFrames'] - 1) * $thisfile_monkeysaudio_raw['nBlocksPerFrame']) +
$thisfile_monkeysaudio_raw['nFinalFrameBlocks'];
126 $thisfile_monkeysaudio['samples'] = (($thisfile_monkeysaudio_raw['nTotalFrames'] - 1) * $thisfile_monkeysaudio['samples_per_frame']) +
$thisfile_monkeysaudio_raw['nFinalFrameSamples'];
128 $thisfile_monkeysaudio['playtime'] = $thisfile_monkeysaudio['samples'] / $thisfile_monkeysaudio['sample_rate'];
129 if ($thisfile_monkeysaudio['playtime'] == 0) {
130 $this->error('Corrupt MAC file: playtime == zero');
133 $info['playtime_seconds'] = $thisfile_monkeysaudio['playtime'];
134 $thisfile_monkeysaudio['compressed_size'] = $info['avdataend'] - $info['avdataoffset'];
135 $thisfile_monkeysaudio['uncompressed_size'] = $thisfile_monkeysaudio['samples'] * $thisfile_monkeysaudio['channels'] * ($thisfile_monkeysaudio['bits_per_sample'] / 8);
136 if ($thisfile_monkeysaudio['uncompressed_size'] == 0) {
137 $this->error('Corrupt MAC file: uncompressed_size == zero');
140 $thisfile_monkeysaudio['compression_ratio'] = $thisfile_monkeysaudio['compressed_size'] / ($thisfile_monkeysaudio['uncompressed_size'] +
$thisfile_monkeysaudio_raw['nHeaderDataBytes']);
141 $thisfile_monkeysaudio['bitrate'] = (($thisfile_monkeysaudio['samples'] * $thisfile_monkeysaudio['channels'] * $thisfile_monkeysaudio['bits_per_sample']) / $thisfile_monkeysaudio['playtime']) * $thisfile_monkeysaudio['compression_ratio'];
142 $info['audio']['bitrate'] = $thisfile_monkeysaudio['bitrate'];
144 // add size of MAC header to avdataoffset
145 if ($thisfile_monkeysaudio_raw['nVersion'] >= 3980) {
146 $info['avdataoffset'] +
= $thisfile_monkeysaudio_raw['nDescriptorBytes'];
147 $info['avdataoffset'] +
= $thisfile_monkeysaudio_raw['nHeaderBytes'];
148 $info['avdataoffset'] +
= $thisfile_monkeysaudio_raw['nSeekTableBytes'];
149 $info['avdataoffset'] +
= $thisfile_monkeysaudio_raw['nHeaderDataBytes'];
151 $info['avdataend'] -= $thisfile_monkeysaudio_raw['nTerminatingDataBytes'];
153 $info['avdataoffset'] +
= $offset;
156 if ($thisfile_monkeysaudio_raw['nVersion'] >= 3980) {
157 if ($thisfile_monkeysaudio_raw['cFileMD5'] === str_repeat("\x00", 16)) {
158 //$this->warning('cFileMD5 is null');
160 $info['md5_data_source'] = '';
161 $md5 = $thisfile_monkeysaudio_raw['cFileMD5'];
162 for ($i = 0; $i < strlen($md5); $i++
) {
163 $info['md5_data_source'] .= str_pad(dechex(ord($md5{$i})), 2, '00', STR_PAD_LEFT
);
165 if (!preg_match('/^[0-9a-f]{32}$/', $info['md5_data_source'])) {
166 unset($info['md5_data_source']);
173 $info['audio']['bits_per_sample'] = $thisfile_monkeysaudio['bits_per_sample'];
174 $info['audio']['encoder'] = 'MAC v'.number_format($thisfile_monkeysaudio['version'], 2);
175 $info['audio']['encoder_options'] = ucfirst($thisfile_monkeysaudio['compression']).' compression';
180 public function MonkeyCompressionLevelNameLookup($compressionlevel) {
181 static $MonkeyCompressionLevelNameLookup = array(
186 4000 => 'extra-high',
189 return (isset($MonkeyCompressionLevelNameLookup[$compressionlevel]) ?
$MonkeyCompressionLevelNameLookup[$compressionlevel] : 'invalid');
192 public function MonkeySamplesPerFrame($versionid, $compressionlevel) {
193 if ($versionid >= 3950) {
195 } elseif ($versionid >= 3900) {
197 } elseif (($versionid >= 3800) && ($compressionlevel == 4000)) {