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