beaca8c1b5e58bec8922fd36d397b7c34405a751
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 /////////////////////////////////////////////////////////////////
11 // module.audio.monkey.php //
12 // module for analyzing Monkey's Audio files //
13 // dependencies: NONE //
15 /////////////////////////////////////////////////////////////////
17 if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
21 class getid3_monkey
extends getid3_handler
26 public function Analyze() {
27 $info = &$this->getid3
->info
;
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
32 $info['fileformat'] = 'mac';
33 $info['audio']['dataformat'] = 'mac';
34 $info['audio']['bitrate_mode'] = 'vbr';
35 $info['audio']['lossless'] = true;
37 $info['monkeys_audio']['raw'] = array();
38 $thisfile_monkeysaudio = &$info['monkeys_audio'];
39 $thisfile_monkeysaudio_raw = &$thisfile_monkeysaudio['raw'];
41 $this->fseek($info['avdataoffset']);
42 $MACheaderData = $this->fread(74);
44 $thisfile_monkeysaudio_raw['magic'] = substr($MACheaderData, 0, 4);
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']);
51 $thisfile_monkeysaudio_raw['nVersion'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, 4, 2)); // appears to be uint32 in 3.98+
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));
68 $thisfile_monkeysaudio_raw['nDescriptorBytes'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, $offset, 4));
70 $thisfile_monkeysaudio_raw['nHeaderBytes'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, $offset, 4));
72 $thisfile_monkeysaudio_raw['nSeekTableBytes'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, $offset, 4));
74 $thisfile_monkeysaudio_raw['nHeaderDataBytes'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, $offset, 4));
76 $thisfile_monkeysaudio_raw['nAPEFrameDataBytes'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, $offset, 4));
78 $thisfile_monkeysaudio_raw['nAPEFrameDataBytesHigh'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, $offset, 4));
80 $thisfile_monkeysaudio_raw['nTerminatingDataBytes'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, $offset, 4));
82 $thisfile_monkeysaudio_raw['cFileMD5'] = substr($MACheaderData, $offset, 16);
86 $thisfile_monkeysaudio_raw['nCompressionLevel'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, $offset, 2));
88 $thisfile_monkeysaudio_raw['nFormatFlags'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, $offset, 2));
90 $thisfile_monkeysaudio_raw['nBlocksPerFrame'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, $offset, 4));
92 $thisfile_monkeysaudio_raw['nFinalFrameBlocks'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, $offset, 4));
94 $thisfile_monkeysaudio_raw['nTotalFrames'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, $offset, 4));
96 $thisfile_monkeysaudio_raw['nBitsPerSample'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, $offset, 2));
98 $thisfile_monkeysaudio_raw['nChannels'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, $offset, 2));
100 $thisfile_monkeysaudio_raw['nSampleRate'] = getid3_lib
::LittleEndian2Int(substr($MACheaderData, $offset, 4));
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']);
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');
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);
128 if ($thisfile_monkeysaudio_raw['nVersion'] >= 3980) {
129 $thisfile_monkeysaudio['samples'] = (($thisfile_monkeysaudio_raw['nTotalFrames'] - 1) * $thisfile_monkeysaudio_raw['nBlocksPerFrame']) +
$thisfile_monkeysaudio_raw['nFinalFrameBlocks'];
131 $thisfile_monkeysaudio['samples'] = (($thisfile_monkeysaudio_raw['nTotalFrames'] - 1) * $thisfile_monkeysaudio['samples_per_frame']) +
$thisfile_monkeysaudio_raw['nFinalFrameSamples'];
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');
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');
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'];
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'];
156 $info['avdataend'] -= $thisfile_monkeysaudio_raw['nTerminatingDataBytes'];
158 $info['avdataoffset'] +
= $offset;
161 if ($thisfile_monkeysaudio_raw['nVersion'] >= 3980) {
162 if ($thisfile_monkeysaudio_raw['cFileMD5'] === str_repeat("\x00", 16)) {
163 //$this->warning('cFileMD5 is null');
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
);
170 if (!preg_match('/^[0-9a-f]{32}$/', $info['md5_data_source'])) {
171 unset($info['md5_data_source']);
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';
186 * @param int $compressionlevel
190 public function MonkeyCompressionLevelNameLookup($compressionlevel) {
191 static $MonkeyCompressionLevelNameLookup = array(
196 4000 => 'extra-high',
199 return (isset($MonkeyCompressionLevelNameLookup[$compressionlevel]) ?
$MonkeyCompressionLevelNameLookup[$compressionlevel] : 'invalid');
203 * @param int $versionid
204 * @param int $compressionlevel
208 public function MonkeySamplesPerFrame($versionid, $compressionlevel) {
209 if ($versionid >= 3950) {
211 } elseif ($versionid >= 3900) {
213 } elseif (($versionid >= 3800) && ($compressionlevel == 4000)) {