ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
module.audio.monkey.php
Go to the documentation of this file.
1 <?php
4 // available at http://getid3.sourceforge.net //
5 // or http://www.getid3.org //
6 // also https://github.com/JamesHeinrich/getID3 //
8 // See readme.txt for more details //
10 // //
11 // module.audio.monkey.php //
12 // module for analyzing Monkey's Audio files //
13 // dependencies: NONE //
14 // ///
16 
17 
19 {
20 
21  public function Analyze() {
22  $info = &$this->getid3->info;
23 
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
26 
27  $info['fileformat'] = 'mac';
28  $info['audio']['dataformat'] = 'mac';
29  $info['audio']['bitrate_mode'] = 'vbr';
30  $info['audio']['lossless'] = true;
31 
32  $info['monkeys_audio']['raw'] = array();
33  $thisfile_monkeysaudio = &$info['monkeys_audio'];
34  $thisfile_monkeysaudio_raw = &$thisfile_monkeysaudio['raw'];
35 
36  $this->fseek($info['avdataoffset']);
37  $MACheaderData = $this->fread(74);
38 
39  $thisfile_monkeysaudio_raw['magic'] = substr($MACheaderData, 0, 4);
40  $magic = 'MAC ';
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']);
44  return false;
45  }
46  $thisfile_monkeysaudio_raw['nVersion'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, 4, 2)); // appears to be uint32 in 3.98+
47 
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));
59  $offset = 8;
60  } else {
61  $offset = 8;
62  // APE_DESCRIPTOR
63  $thisfile_monkeysaudio_raw['nDescriptorBytes'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4));
64  $offset += 4;
65  $thisfile_monkeysaudio_raw['nHeaderBytes'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4));
66  $offset += 4;
67  $thisfile_monkeysaudio_raw['nSeekTableBytes'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4));
68  $offset += 4;
69  $thisfile_monkeysaudio_raw['nHeaderDataBytes'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4));
70  $offset += 4;
71  $thisfile_monkeysaudio_raw['nAPEFrameDataBytes'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4));
72  $offset += 4;
73  $thisfile_monkeysaudio_raw['nAPEFrameDataBytesHigh'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4));
74  $offset += 4;
75  $thisfile_monkeysaudio_raw['nTerminatingDataBytes'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4));
76  $offset += 4;
77  $thisfile_monkeysaudio_raw['cFileMD5'] = substr($MACheaderData, $offset, 16);
78  $offset += 16;
79 
80  // APE_HEADER
81  $thisfile_monkeysaudio_raw['nCompressionLevel'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 2));
82  $offset += 2;
83  $thisfile_monkeysaudio_raw['nFormatFlags'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 2));
84  $offset += 2;
85  $thisfile_monkeysaudio_raw['nBlocksPerFrame'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4));
86  $offset += 4;
87  $thisfile_monkeysaudio_raw['nFinalFrameBlocks'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4));
88  $offset += 4;
89  $thisfile_monkeysaudio_raw['nTotalFrames'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4));
90  $offset += 4;
91  $thisfile_monkeysaudio_raw['nBitsPerSample'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 2));
92  $offset += 2;
93  $thisfile_monkeysaudio_raw['nChannels'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 2));
94  $offset += 2;
95  $thisfile_monkeysaudio_raw['nSampleRate'] = getid3_lib::LittleEndian2Int(substr($MACheaderData, $offset, 4));
96  $offset += 4;
97  }
98 
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']);
109  }
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');
116  return false;
117  }
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);
122  }
123  if ($thisfile_monkeysaudio_raw['nVersion'] >= 3980) {
124  $thisfile_monkeysaudio['samples'] = (($thisfile_monkeysaudio_raw['nTotalFrames'] - 1) * $thisfile_monkeysaudio_raw['nBlocksPerFrame']) + $thisfile_monkeysaudio_raw['nFinalFrameBlocks'];
125  } else {
126  $thisfile_monkeysaudio['samples'] = (($thisfile_monkeysaudio_raw['nTotalFrames'] - 1) * $thisfile_monkeysaudio['samples_per_frame']) + $thisfile_monkeysaudio_raw['nFinalFrameSamples'];
127  }
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');
131  return false;
132  }
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');
138  return false;
139  }
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'];
143 
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'];
150 
151  $info['avdataend'] -= $thisfile_monkeysaudio_raw['nTerminatingDataBytes'];
152  } else {
153  $info['avdataoffset'] += $offset;
154  }
155 
156  if ($thisfile_monkeysaudio_raw['nVersion'] >= 3980) {
157  if ($thisfile_monkeysaudio_raw['cFileMD5'] === str_repeat("\x00", 16)) {
158  //$this->warning('cFileMD5 is null');
159  } else {
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);
164  }
165  if (!preg_match('/^[0-9a-f]{32}$/', $info['md5_data_source'])) {
166  unset($info['md5_data_source']);
167  }
168  }
169  }
170 
171 
172 
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';
176 
177  return true;
178  }
179 
180  public function MonkeyCompressionLevelNameLookup($compressionlevel) {
181  static $MonkeyCompressionLevelNameLookup = array(
182  0 => 'unknown',
183  1000 => 'fast',
184  2000 => 'normal',
185  3000 => 'high',
186  4000 => 'extra-high',
187  5000 => 'insane'
188  );
189  return (isset($MonkeyCompressionLevelNameLookup[$compressionlevel]) ? $MonkeyCompressionLevelNameLookup[$compressionlevel] : 'invalid');
190  }
191 
192  public function MonkeySamplesPerFrame($versionid, $compressionlevel) {
193  if ($versionid >= 3950) {
194  return 73728 * 4;
195  } elseif ($versionid >= 3900) {
196  return 73728;
197  } elseif (($versionid >= 3800) && ($compressionlevel == 4000)) {
198  return 73728;
199  } else {
200  return 9216;
201  }
202  }
203 
204 }
getID3() by James Heinrich info@getid3.org //
error($text)
Definition: getid3.php:1752
MonkeyCompressionLevelNameLookup($compressionlevel)
static LittleEndian2Int($byteword, $signed=false)
Definition: getid3.lib.php:292
static PrintHexBytes($string, $hex=true, $spaces=true, $htmlencoding='UTF-8')
Definition: getid3.lib.php:18
fread($bytes)
Definition: getid3.php:1683
Create styles array
The data for the language used.
$i
Definition: disco.tpl.php:19
fseek($bytes, $whence=SEEK_SET)
Definition: getid3.php:1711
$info
Definition: index.php:5
MonkeySamplesPerFrame($versionid, $compressionlevel)