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