ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
module.audio.mpc.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.mpc.php //
11// module for analyzing Musepack/MPEG+ Audio files //
12// dependencies: NONE //
13// ///
15
16
18{
19
20 function getid3_mpc(&$fd, &$ThisFileInfo) {
21 // http://www.uni-jena.de/~pfk/mpp/sv8/header.html
22
23 $ThisFileInfo['mpc']['header'] = array();
24 $thisfile_mpc_header = &$ThisFileInfo['mpc']['header'];
25
26 $ThisFileInfo['fileformat'] = 'mpc';
27 $ThisFileInfo['audio']['dataformat'] = 'mpc';
28 $ThisFileInfo['audio']['bitrate_mode'] = 'vbr';
29 $ThisFileInfo['audio']['channels'] = 2; // the format appears to be hardcoded for stereo only
30 $ThisFileInfo['audio']['lossless'] = false;
31
32 fseek($fd, $ThisFileInfo['avdataoffset'], SEEK_SET);
33
34 $thisfile_mpc_header['size'] = 28;
35 $MPCheaderData = fread($fd, $thisfile_mpc_header['size']);
36 $offset = 0;
37
38 if (substr($MPCheaderData, $offset, 3) == 'MP+') {
39
40 // great, this is SV7+
41 $thisfile_mpc_header['raw']['preamble'] = substr($MPCheaderData, $offset, 3); // should be 'MP+'
42 $offset += 3;
43
44 } elseif (preg_match('/^[\x00\x01\x10\x11\x40\x41\x50\x51\x80\x81\x90\x91\xC0\xC1\xD0\xD1][\x20-37][\x00\x20\x40\x60\x80\xA0\xC0\xE0]/s', substr($MPCheaderData, 0, 4))) {
45
46 // this is SV4 - SV6, handle seperately
47 $thisfile_mpc_header['size'] = 8;
48
49 // add size of file header to avdataoffset - calc bitrate correctly + MD5 data
50 $ThisFileInfo['avdataoffset'] += $thisfile_mpc_header['size'];
51
52 // Most of this code adapted from Jurgen Faul's MPEGplus source code - thanks Jurgen! :)
53 $HeaderDWORD[0] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, 0, 4));
54 $HeaderDWORD[1] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, 4, 4));
55
56
57 // DDDD DDDD CCCC CCCC BBBB BBBB AAAA AAAA
58 // aaaa aaaa abcd dddd dddd deee eeff ffff
59 //
60 // a = bitrate = anything
61 // b = IS = anything
62 // c = MS = anything
63 // d = streamversion = 0000000004 or 0000000005 or 0000000006
64 // e = maxband = anything
65 // f = blocksize = 000001 for SV5+, anything(?) for SV4
66
67 $thisfile_mpc_header['target_bitrate'] = (($HeaderDWORD[0] & 0xFF800000) >> 23);
68 $thisfile_mpc_header['intensity_stereo'] = (bool) (($HeaderDWORD[0] & 0x00400000) >> 22);
69 $thisfile_mpc_header['mid-side_stereo'] = (bool) (($HeaderDWORD[0] & 0x00200000) >> 21);
70 $thisfile_mpc_header['stream_major_version'] = ($HeaderDWORD[0] & 0x001FF800) >> 11;
71 $thisfile_mpc_header['stream_minor_version'] = 0; // no sub-version numbers before SV7
72 $thisfile_mpc_header['max_band'] = ($HeaderDWORD[0] & 0x000007C0) >> 6; // related to lowpass frequency, not sure how it translates exactly
73 $thisfile_mpc_header['block_size'] = ($HeaderDWORD[0] & 0x0000003F);
74
75 switch ($thisfile_mpc_header['stream_major_version']) {
76 case 4:
77 $thisfile_mpc_header['frame_count'] = ($HeaderDWORD[1] >> 16);
78 break;
79
80 case 5:
81 case 6:
82 $thisfile_mpc_header['frame_count'] = $HeaderDWORD[1];
83 break;
84
85 default:
86 $ThisFileInfo['error'] = 'Expecting 4, 5 or 6 in version field, found '.$thisfile_mpc_header['stream_major_version'].' instead';
87 unset($ThisFileInfo['mpc']);
88 return false;
89 break;
90 }
91
92 if (($thisfile_mpc_header['stream_major_version'] > 4) && ($thisfile_mpc_header['block_size'] != 1)) {
93 $ThisFileInfo['warning'][] = 'Block size expected to be 1, actual value found: '.$thisfile_mpc_header['block_size'];
94 }
95
96 $thisfile_mpc_header['sample_rate'] = 44100; // AB: used by all files up to SV7
97 $ThisFileInfo['audio']['sample_rate'] = $thisfile_mpc_header['sample_rate'];
98 $thisfile_mpc_header['samples'] = $thisfile_mpc_header['frame_count'] * 1152 * $ThisFileInfo['audio']['channels'];
99
100 if ($thisfile_mpc_header['target_bitrate'] == 0) {
101 $ThisFileInfo['audio']['bitrate_mode'] = 'vbr';
102 } else {
103 $ThisFileInfo['audio']['bitrate_mode'] = 'cbr';
104 }
105
106 $ThisFileInfo['mpc']['bitrate'] = ($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']) * 8 * 44100 / $thisfile_mpc_header['frame_count'] / 1152;
107 $ThisFileInfo['audio']['bitrate'] = $ThisFileInfo['mpc']['bitrate'];
108 $ThisFileInfo['audio']['encoder'] = 'SV'.$thisfile_mpc_header['stream_major_version'];
109
110 return true;
111
112 } else {
113
114 $ThisFileInfo['error'][] = 'Expecting "MP+" at offset '.$ThisFileInfo['avdataoffset'].', found "'.substr($MPCheaderData, $offset, 3).'"';
115 unset($ThisFileInfo['fileformat']);
116 unset($ThisFileInfo['mpc']);
117 return false;
118
119 }
120
121 // Continue with SV7+ handling
122 $StreamVersionByte = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 1));
123 $offset += 1;
124 $thisfile_mpc_header['stream_major_version'] = ($StreamVersionByte & 0x0F);
125 $thisfile_mpc_header['stream_minor_version'] = ($StreamVersionByte & 0xF0) >> 4;
126 $thisfile_mpc_header['frame_count'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 4));
127 $offset += 4;
128
129 switch ($thisfile_mpc_header['stream_major_version']) {
130 case 7:
131 //$ThisFileInfo['fileformat'] = 'SV7';
132 break;
133
134 default:
135 $ThisFileInfo['error'][] = 'Only Musepack SV7 supported';
136 return false;
137 }
138
139 $FlagsDWORD1 = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 4));
140 $offset += 4;
141 $thisfile_mpc_header['intensity_stereo'] = (bool) (($FlagsDWORD1 & 0x80000000) >> 31);
142 $thisfile_mpc_header['mid_side_stereo'] = (bool) (($FlagsDWORD1 & 0x40000000) >> 30);
143 $thisfile_mpc_header['max_subband'] = ($FlagsDWORD1 & 0x3F000000) >> 24;
144 $thisfile_mpc_header['raw']['profile'] = ($FlagsDWORD1 & 0x00F00000) >> 20;
145 $thisfile_mpc_header['begin_loud'] = (bool) (($FlagsDWORD1 & 0x00080000) >> 19);
146 $thisfile_mpc_header['end_loud'] = (bool) (($FlagsDWORD1 & 0x00040000) >> 18);
147 $thisfile_mpc_header['raw']['sample_rate'] = ($FlagsDWORD1 & 0x00030000) >> 16;
148 $thisfile_mpc_header['max_level'] = ($FlagsDWORD1 & 0x0000FFFF);
149
150 $thisfile_mpc_header['raw']['title_peak'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 2));
151 $offset += 2;
152 $thisfile_mpc_header['raw']['title_gain'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 2), true);
153 $offset += 2;
154
155 $thisfile_mpc_header['raw']['album_peak'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 2));
156 $offset += 2;
157 $thisfile_mpc_header['raw']['album_gain'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 2), true);
158 $offset += 2;
159
160 $FlagsDWORD2 = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 4));
161 $offset += 4;
162 $thisfile_mpc_header['true_gapless'] = (bool) (($FlagsDWORD2 & 0x80000000) >> 31);
163 $thisfile_mpc_header['last_frame_length'] = ($FlagsDWORD2 & 0x7FF00000) >> 20;
164
165
166 $thisfile_mpc_header['raw']['not_sure_what'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 3));
167 $offset += 3;
168 $thisfile_mpc_header['raw']['encoder_version'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 1));
169 $offset += 1;
170
171 $thisfile_mpc_header['profile'] = $this->MPCprofileNameLookup($thisfile_mpc_header['raw']['profile']);
172 $thisfile_mpc_header['sample_rate'] = $this->MPCfrequencyLookup($thisfile_mpc_header['raw']['sample_rate']);
173 if ($thisfile_mpc_header['sample_rate'] == 0) {
174 $ThisFileInfo['error'][] = 'Corrupt MPC file: frequency == zero';
175 return false;
176 }
177 $ThisFileInfo['audio']['sample_rate'] = $thisfile_mpc_header['sample_rate'];
178 $thisfile_mpc_header['samples'] = ((($thisfile_mpc_header['frame_count'] - 1) * 1152) + $thisfile_mpc_header['last_frame_length']) * $ThisFileInfo['audio']['channels'];
179
180 $ThisFileInfo['playtime_seconds'] = ($thisfile_mpc_header['samples'] / $ThisFileInfo['audio']['channels']) / $ThisFileInfo['audio']['sample_rate'];
181 if ($ThisFileInfo['playtime_seconds'] == 0) {
182 $ThisFileInfo['error'][] = 'Corrupt MPC file: playtime_seconds == zero';
183 return false;
184 }
185
186 // add size of file header to avdataoffset - calc bitrate correctly + MD5 data
187 $ThisFileInfo['avdataoffset'] += $thisfile_mpc_header['size'];
188
189 $ThisFileInfo['audio']['bitrate'] = (($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']) * 8) / $ThisFileInfo['playtime_seconds'];
190
191 $thisfile_mpc_header['title_peak'] = $thisfile_mpc_header['raw']['title_peak'];
192 $thisfile_mpc_header['title_peak_db'] = $this->MPCpeakDBLookup($thisfile_mpc_header['title_peak']);
193 if ($thisfile_mpc_header['raw']['title_gain'] < 0) {
194 $thisfile_mpc_header['title_gain_db'] = (float) (32768 + $thisfile_mpc_header['raw']['title_gain']) / -100;
195 } else {
196 $thisfile_mpc_header['title_gain_db'] = (float) $thisfile_mpc_header['raw']['title_gain'] / 100;
197 }
198
199 $thisfile_mpc_header['album_peak'] = $thisfile_mpc_header['raw']['album_peak'];
200 $thisfile_mpc_header['album_peak_db'] = $this->MPCpeakDBLookup($thisfile_mpc_header['album_peak']);
201 if ($thisfile_mpc_header['raw']['album_gain'] < 0) {
202 $thisfile_mpc_header['album_gain_db'] = (float) (32768 + $thisfile_mpc_header['raw']['album_gain']) / -100;
203 } else {
204 $thisfile_mpc_header['album_gain_db'] = (float) $thisfile_mpc_header['raw']['album_gain'] / 100;;
205 }
206 $thisfile_mpc_header['encoder_version'] = $this->MPCencoderVersionLookup($thisfile_mpc_header['raw']['encoder_version']);
207
208 $ThisFileInfo['replay_gain']['track']['adjustment'] = $thisfile_mpc_header['title_gain_db'];
209 $ThisFileInfo['replay_gain']['album']['adjustment'] = $thisfile_mpc_header['album_gain_db'];
210
211 if ($thisfile_mpc_header['title_peak'] > 0) {
212 $ThisFileInfo['replay_gain']['track']['peak'] = $thisfile_mpc_header['title_peak'];
213 } elseif (round($thisfile_mpc_header['max_level'] * 1.18) > 0) {
214 $ThisFileInfo['replay_gain']['track']['peak'] = getid3_lib::CastAsInt(round($thisfile_mpc_header['max_level'] * 1.18)); // why? I don't know - see mppdec.c
215 }
216 if ($thisfile_mpc_header['album_peak'] > 0) {
217 $ThisFileInfo['replay_gain']['album']['peak'] = $thisfile_mpc_header['album_peak'];
218 }
219
220 //$ThisFileInfo['audio']['encoder'] = 'SV'.$thisfile_mpc_header['stream_major_version'].'.'.$thisfile_mpc_header['stream_minor_version'].', '.$thisfile_mpc_header['encoder_version'];
221 $ThisFileInfo['audio']['encoder'] = $thisfile_mpc_header['encoder_version'];
222 $ThisFileInfo['audio']['encoder_options'] = $thisfile_mpc_header['profile'];
223
224 return true;
225 }
226
227 function MPCprofileNameLookup($profileid) {
228 static $MPCprofileNameLookup = array(
229 0 => 'no profile',
230 1 => 'Experimental',
231 2 => 'unused',
232 3 => 'unused',
233 4 => 'unused',
234 5 => 'below Telephone (q = 0.0)',
235 6 => 'below Telephone (q = 1.0)',
236 7 => 'Telephone (q = 2.0)',
237 8 => 'Thumb (q = 3.0)',
238 9 => 'Radio (q = 4.0)',
239 10 => 'Standard (q = 5.0)',
240 11 => 'Extreme (q = 6.0)',
241 12 => 'Insane (q = 7.0)',
242 13 => 'BrainDead (q = 8.0)',
243 14 => 'above BrainDead (q = 9.0)',
244 15 => 'above BrainDead (q = 10.0)'
245 );
246 return (isset($MPCprofileNameLookup[$profileid]) ? $MPCprofileNameLookup[$profileid] : 'invalid');
247 }
248
249 function MPCfrequencyLookup($frequencyid) {
250 static $MPCfrequencyLookup = array(
251 0 => 44100,
252 1 => 48000,
253 2 => 37800,
254 3 => 32000
255 );
256 return (isset($MPCfrequencyLookup[$frequencyid]) ? $MPCfrequencyLookup[$frequencyid] : 'invalid');
257 }
258
259 function MPCpeakDBLookup($intvalue) {
260 if ($intvalue > 0) {
261 return ((log10($intvalue) / log10(2)) - 15) * 6;
262 }
263 return false;
264 }
265
266 function MPCencoderVersionLookup($encoderversion) {
267 //Encoder version * 100 (106 = 1.06)
268 //EncoderVersion % 10 == 0 Release (1.0)
269 //EncoderVersion % 2 == 0 Beta (1.06)
270 //EncoderVersion % 2 == 1 Alpha (1.05a...z)
271
272 if ($encoderversion == 0) {
273 // very old version, not known exactly which
274 return 'Buschmann v1.7.0-v1.7.9 or Klemm v0.90-v1.05';
275 }
276
277 if (($encoderversion % 10) == 0) {
278
279 // release version
280 return number_format($encoderversion / 100, 2);
281
282 } elseif (($encoderversion % 2) == 0) {
283
284 // beta version
285 return number_format($encoderversion / 100, 2).' beta';
286
287 }
288
289 // alpha version
290 return number_format($encoderversion / 100, 2).' alpha';
291 }
292
293}
294
295
296?>
fseek($bytes, $whence=SEEK_SET)
Definition: getid3.php:1697
fread($bytes)
Definition: getid3.php:1685
LittleEndian2Int($byteword, $signed=false)
Definition: getid3.lib.php:266
CastAsInt($floatnum)
Definition: getid3.lib.php:60
getID3() by James Heinrich info@getid3.org //
MPCpeakDBLookup($intvalue)
MPCencoderVersionLookup($encoderversion)
MPCprofileNameLookup($profileid)
getid3_mpc(&$fd, &$ThisFileInfo)
MPCfrequencyLookup($frequencyid)