1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
<?php
/* vim:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab: */
// +----------------------------------------------------------------------+
// | PHP version 5 |
// +----------------------------------------------------------------------+
// | Copyright (c) 2002-2006 James Heinrich, Allan Hansen |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2 of the GPL license, |
// | that is bundled with this package in the file license.txt and is |
// | available through the world-wide-web at the following url: |
// | http://www.gnu.org/copyleft/gpl.html |
// +----------------------------------------------------------------------+
// | getID3() - http://getid3.sourceforge.net or http://www.getid3.org |
// +----------------------------------------------------------------------+
// | Authors: James Heinrich <info�getid3*org> |
// | Allan Hansen <ah�artemis*dk> |
// +----------------------------------------------------------------------+
// | module.audio.monkey.php |
// | Module for analyzing Monkey's Audio files |
// | dependencies: NONE |
// +----------------------------------------------------------------------+
//
// $Id: module.audio.monkey.php,v 1.2 2006/11/02 10:48:01 ah Exp $
class getid3_monkey extends getid3_handler
{
public function Analyze() {
$getid3 = $this->getid3;
// based loosely on code from TMonkey by Jurgen Faul <jfaul�gmx*de>
// http://jfaul.de/atl or http://j-faul.virtualave.net/atl/atl.html
$getid3->info['fileformat'] = 'mac';
$getid3->info['audio']['dataformat'] = 'mac';
$getid3->info['audio']['bitrate_mode'] = 'vbr';
$getid3->info['audio']['lossless'] = true;
$getid3->info['monkeys_audio']['raw'] = array ();
$info_monkeys_audio = &$getid3->info['monkeys_audio'];
$info_monkeys_audio_raw = &$info_monkeys_audio['raw'];
// Read file header
fseek($getid3->fp, $getid3->info['avdataoffset'], SEEK_SET);
$mac_header_data = fread($getid3->fp, 74);
$info_monkeys_audio_raw['magic'] = 'MAC '; // Magic bytes
// Read MAC version
$info_monkeys_audio_raw['nVersion'] = getid3_lib::LittleEndian2Int(substr($mac_header_data, 4, 2)); // appears to be uint32 in 3.98+
// Parse MAC Header < v3980
if ($info_monkeys_audio_raw['nVersion'] < 3980) {
getid3_lib::ReadSequence("LittleEndian2Int", $info_monkeys_audio_raw, $mac_header_data, 6,
array (
'nCompressionLevel' => 2,
'nFormatFlags' => 2,
'nChannels' => 2,
'nSampleRate' => 4,
'nHeaderDataBytes' => 4,
'nWAVTerminatingBytes' => 4,
'nTotalFrames' => 4,
'nFinalFrameSamples' => 4,
'nPeakLevel' => 4,
'IGNORE-1' => 2,
'nSeekElements' => 2
)
);
}
// Parse MAC Header >= v3980
else {
getid3_lib::ReadSequence("LittleEndian2Int", $info_monkeys_audio_raw, $mac_header_data, 8,
array (
// APE_DESCRIPTOR
'nDescriptorBytes' => 4,
'nHeaderBytes' => 4,
'nSeekTableBytes' => 4,
'nHeaderDataBytes' => 4,
'nAPEFrameDataBytes' => 4,
'nAPEFrameDataBytesHigh'=> 4,
'nTerminatingDataBytes' => 4,
// MD5 - string
'cFileMD5' => -16,
// APE_HEADER
'nCompressionLevel' => 2,
'nFormatFlags' => 2,
'nBlocksPerFrame' => 4,
'nFinalFrameBlocks' => 4,
'nTotalFrames' => 4,
'nBitsPerSample' => 2,
'nChannels' => 2,
'nSampleRate' => 4
)
);
}
// Process data
$info_monkeys_audio['flags']['8-bit'] = (bool)($info_monkeys_audio_raw['nFormatFlags'] & 0x0001);
$info_monkeys_audio['flags']['crc-32'] = (bool)($info_monkeys_audio_raw['nFormatFlags'] & 0x0002);
$info_monkeys_audio['flags']['peak_level'] = (bool)($info_monkeys_audio_raw['nFormatFlags'] & 0x0004);
$info_monkeys_audio['flags']['24-bit'] = (bool)($info_monkeys_audio_raw['nFormatFlags'] & 0x0008);
$info_monkeys_audio['flags']['seek_elements'] = (bool)($info_monkeys_audio_raw['nFormatFlags'] & 0x0010);
$info_monkeys_audio['flags']['no_wav_header'] = (bool)($info_monkeys_audio_raw['nFormatFlags'] & 0x0020);
$info_monkeys_audio['version'] = $info_monkeys_audio_raw['nVersion'] / 1000;
$info_monkeys_audio['compression'] = getid3_monkey::MonkeyCompressionLevelNameLookup($info_monkeys_audio_raw['nCompressionLevel']);
$info_monkeys_audio['bits_per_sample'] = ($info_monkeys_audio['flags']['24-bit'] ? 24 : ($info_monkeys_audio['flags']['8-bit'] ? 8 : 16));
$info_monkeys_audio['channels'] = $info_monkeys_audio_raw['nChannels'];
$getid3->info['audio']['channels'] = $info_monkeys_audio['channels'];
$info_monkeys_audio['sample_rate'] = $info_monkeys_audio_raw['nSampleRate'];
$getid3->info['audio']['sample_rate'] = $info_monkeys_audio['sample_rate'];
if ($info_monkeys_audio['flags']['peak_level']) {
$info_monkeys_audio['peak_level'] = $info_monkeys_audio_raw['nPeakLevel'];
$info_monkeys_audio['peak_ratio'] = $info_monkeys_audio['peak_level'] / pow(2, $info_monkeys_audio['bits_per_sample'] - 1);
}
// MAC >= v3980
if ($info_monkeys_audio_raw['nVersion'] >= 3980) {
$info_monkeys_audio['samples'] = (($info_monkeys_audio_raw['nTotalFrames'] - 1) * $info_monkeys_audio_raw['nBlocksPerFrame']) + $info_monkeys_audio_raw['nFinalFrameBlocks'];
}
// MAC < v3980
else {
$info_monkeys_audio['samples_per_frame'] = getid3_monkey::MonkeySamplesPerFrame($info_monkeys_audio_raw['nVersion'], $info_monkeys_audio_raw['nCompressionLevel']);
$info_monkeys_audio['samples'] = (($info_monkeys_audio_raw['nTotalFrames'] - 1) * $info_monkeys_audio['samples_per_frame']) + $info_monkeys_audio_raw['nFinalFrameSamples'];
}
$info_monkeys_audio['playtime'] = $info_monkeys_audio['samples'] / $info_monkeys_audio['sample_rate'];
$getid3->info['playtime_seconds'] = $info_monkeys_audio['playtime'];
$info_monkeys_audio['compressed_size'] = $getid3->info['avdataend'] - $getid3->info['avdataoffset'];
$info_monkeys_audio['uncompressed_size'] = $info_monkeys_audio['samples'] * $info_monkeys_audio['channels'] * ($info_monkeys_audio['bits_per_sample'] / 8);
$info_monkeys_audio['compression_ratio'] = $info_monkeys_audio['compressed_size'] / ($info_monkeys_audio['uncompressed_size'] + $info_monkeys_audio_raw['nHeaderDataBytes']);
$info_monkeys_audio['bitrate'] = (($info_monkeys_audio['samples'] * $info_monkeys_audio['channels'] * $info_monkeys_audio['bits_per_sample']) / $info_monkeys_audio['playtime']) * $info_monkeys_audio['compression_ratio'];
$getid3->info['audio']['bitrate'] = $info_monkeys_audio['bitrate'];
$getid3->info['audio']['bits_per_sample'] = $info_monkeys_audio['bits_per_sample'];
$getid3->info['audio']['encoder'] = 'MAC v'.number_format($info_monkeys_audio['version'], 2);
$getid3->info['audio']['encoder_options'] = ucfirst($info_monkeys_audio['compression']).' compression';
// MAC >= v3980 - get avdataoffsets from MAC header
if ($info_monkeys_audio_raw['nVersion'] >= 3980) {
$getid3->info['avdataoffset'] += $info_monkeys_audio_raw['nDescriptorBytes'] + $info_monkeys_audio_raw['nHeaderBytes'] + $info_monkeys_audio_raw['nSeekTableBytes'] + $info_monkeys_audio_raw['nHeaderDataBytes'];
$getid3->info['avdataend'] -= $info_monkeys_audio_raw['nTerminatingDataBytes'];
}
// MAC < v3980 Add size of MAC header to avdataoffset
else {
$getid3->info['avdataoffset'] += 8;
}
// Convert md5sum to 32 byte string
if (@$info_monkeys_audio_raw['cFileMD5']) {
if ($info_monkeys_audio_raw['cFileMD5'] !== str_repeat("\x00", 16)) {
$getid3->info['md5_data_source'] = '';
$md5 = $info_monkeys_audio_raw['cFileMD5'];
for ($i = 0; $i < strlen($md5); $i++) {
$getid3->info['md5_data_source'] .= str_pad(dechex(ord($md5{$i})), 2, '00', STR_PAD_LEFT);
}
if (!preg_match('/^[0-9a-f]{32}$/', $getid3->info['md5_data_source'])) {
unset($getid3->info['md5_data_source']);
}
}
}
return true;
}
public static function MonkeyCompressionLevelNameLookup($compression_level) {
static $lookup = array (
0 => 'unknown',
1000 => 'fast',
2000 => 'normal',
3000 => 'high',
4000 => 'extra-high',
5000 => 'insane'
);
return (isset($lookup[$compression_level]) ? $lookup[$compression_level] : 'invalid');
}
public static function MonkeySamplesPerFrame($version_id, $compression_level) {
if ($version_id >= 3950) {
return 73728 * 4;
}
if (($version_id >= 3900) || (($version_id >= 3800) && ($compression_level == 4000))) {
return 73728;
}
return 9216;
}
}
?>
|