[SPIP] v3.2.1-->v3.2.3
[lhc/web/www.git] / www / plugins-dist / medias / lib / getid3 / module.audio.mpc.php
1 <?php
2
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 /////////////////////////////////////////////////////////////////
10 // //
11 // module.audio.mpc.php //
12 // module for analyzing Musepack/MPEG+ Audio files //
13 // dependencies: NONE //
14 // ///
15 /////////////////////////////////////////////////////////////////
16
17
18 class getid3_mpc extends getid3_handler
19 {
20 /**
21 * @return bool
22 */
23 public function Analyze() {
24 $info = &$this->getid3->info;
25
26 $info['mpc']['header'] = array();
27 $thisfile_mpc_header = &$info['mpc']['header'];
28
29 $info['fileformat'] = 'mpc';
30 $info['audio']['dataformat'] = 'mpc';
31 $info['audio']['bitrate_mode'] = 'vbr';
32 $info['audio']['channels'] = 2; // up to SV7 the format appears to have been hardcoded for stereo only
33 $info['audio']['lossless'] = false;
34
35 $this->fseek($info['avdataoffset']);
36 $MPCheaderData = $this->fread(4);
37 $info['mpc']['header']['preamble'] = substr($MPCheaderData, 0, 4); // should be 'MPCK' (SV8) or 'MP+' (SV7), otherwise possible stream data (SV4-SV6)
38 if (preg_match('#^MPCK#', $info['mpc']['header']['preamble'])) {
39
40 // this is SV8
41 return $this->ParseMPCsv8();
42
43 } elseif (preg_match('#^MP\+#', $info['mpc']['header']['preamble'])) {
44
45 // this is SV7
46 return $this->ParseMPCsv7();
47
48 } 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', $MPCheaderData)) {
49
50 // this is SV4 - SV6, handle seperately
51 return $this->ParseMPCsv6();
52
53 } else {
54
55 $this->error('Expecting "MP+" or "MPCK" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes(substr($MPCheaderData, 0, 4)).'"');
56 unset($info['fileformat']);
57 unset($info['mpc']);
58 return false;
59
60 }
61 }
62
63 /**
64 * @return bool
65 */
66 public function ParseMPCsv8() {
67 // this is SV8
68 // http://trac.musepack.net/trac/wiki/SV8Specification
69
70 $info = &$this->getid3->info;
71 $thisfile_mpc_header = &$info['mpc']['header'];
72
73 $keyNameSize = 2;
74 $maxHandledPacketLength = 9; // specs say: "n*8; 0 < n < 10"
75
76 $offset = $this->ftell();
77 while ($offset < $info['avdataend']) {
78 $thisPacket = array();
79 $thisPacket['offset'] = $offset;
80 $packet_offset = 0;
81
82 // Size is a variable-size field, could be 1-4 bytes (possibly more?)
83 // read enough data in and figure out the exact size later
84 $MPCheaderData = $this->fread($keyNameSize + $maxHandledPacketLength);
85 $packet_offset += $keyNameSize;
86 $thisPacket['key'] = substr($MPCheaderData, 0, $keyNameSize);
87 $thisPacket['key_name'] = $this->MPCsv8PacketName($thisPacket['key']);
88 if ($thisPacket['key'] == $thisPacket['key_name']) {
89 $this->error('Found unexpected key value "'.$thisPacket['key'].'" at offset '.$thisPacket['offset']);
90 return false;
91 }
92 $packetLength = 0;
93 $thisPacket['packet_size'] = $this->SV8variableLengthInteger(substr($MPCheaderData, $keyNameSize), $packetLength); // includes keyname and packet_size field
94 if ($thisPacket['packet_size'] === false) {
95 $this->error('Did not find expected packet length within '.$maxHandledPacketLength.' bytes at offset '.($thisPacket['offset'] + $keyNameSize));
96 return false;
97 }
98 $packet_offset += $packetLength;
99 $offset += $thisPacket['packet_size'];
100
101 switch ($thisPacket['key']) {
102 case 'SH': // Stream Header
103 $moreBytesToRead = $thisPacket['packet_size'] - $keyNameSize - $maxHandledPacketLength;
104 if ($moreBytesToRead > 0) {
105 $MPCheaderData .= $this->fread($moreBytesToRead);
106 }
107 $thisPacket['crc'] = getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 4));
108 $packet_offset += 4;
109 $thisPacket['stream_version'] = getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 1));
110 $packet_offset += 1;
111
112 $packetLength = 0;
113 $thisPacket['sample_count'] = $this->SV8variableLengthInteger(substr($MPCheaderData, $packet_offset, $maxHandledPacketLength), $packetLength);
114 $packet_offset += $packetLength;
115
116 $packetLength = 0;
117 $thisPacket['beginning_silence'] = $this->SV8variableLengthInteger(substr($MPCheaderData, $packet_offset, $maxHandledPacketLength), $packetLength);
118 $packet_offset += $packetLength;
119
120 $otherUsefulData = getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 2));
121 $packet_offset += 2;
122 $thisPacket['sample_frequency_raw'] = (($otherUsefulData & 0xE000) >> 13);
123 $thisPacket['max_bands_used'] = (($otherUsefulData & 0x1F00) >> 8);
124 $thisPacket['channels'] = (($otherUsefulData & 0x00F0) >> 4) + 1;
125 $thisPacket['ms_used'] = (bool) (($otherUsefulData & 0x0008) >> 3);
126 $thisPacket['audio_block_frames'] = (($otherUsefulData & 0x0007) >> 0);
127 $thisPacket['sample_frequency'] = $this->MPCfrequencyLookup($thisPacket['sample_frequency_raw']);
128
129 $thisfile_mpc_header['mid_side_stereo'] = $thisPacket['ms_used'];
130 $thisfile_mpc_header['sample_rate'] = $thisPacket['sample_frequency'];
131 $thisfile_mpc_header['samples'] = $thisPacket['sample_count'];
132 $thisfile_mpc_header['stream_version_major'] = $thisPacket['stream_version'];
133
134 $info['audio']['channels'] = $thisPacket['channels'];
135 $info['audio']['sample_rate'] = $thisPacket['sample_frequency'];
136 $info['playtime_seconds'] = $thisPacket['sample_count'] / $thisPacket['sample_frequency'];
137 $info['audio']['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];
138 break;
139
140 case 'RG': // Replay Gain
141 $moreBytesToRead = $thisPacket['packet_size'] - $keyNameSize - $maxHandledPacketLength;
142 if ($moreBytesToRead > 0) {
143 $MPCheaderData .= $this->fread($moreBytesToRead);
144 }
145 $thisPacket['replaygain_version'] = getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 1));
146 $packet_offset += 1;
147 $thisPacket['replaygain_title_gain'] = getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 2));
148 $packet_offset += 2;
149 $thisPacket['replaygain_title_peak'] = getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 2));
150 $packet_offset += 2;
151 $thisPacket['replaygain_album_gain'] = getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 2));
152 $packet_offset += 2;
153 $thisPacket['replaygain_album_peak'] = getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 2));
154 $packet_offset += 2;
155
156 if ($thisPacket['replaygain_title_gain']) { $info['replay_gain']['title']['gain'] = $thisPacket['replaygain_title_gain']; }
157 if ($thisPacket['replaygain_title_peak']) { $info['replay_gain']['title']['peak'] = $thisPacket['replaygain_title_peak']; }
158 if ($thisPacket['replaygain_album_gain']) { $info['replay_gain']['album']['gain'] = $thisPacket['replaygain_album_gain']; }
159 if ($thisPacket['replaygain_album_peak']) { $info['replay_gain']['album']['peak'] = $thisPacket['replaygain_album_peak']; }
160 break;
161
162 case 'EI': // Encoder Info
163 $moreBytesToRead = $thisPacket['packet_size'] - $keyNameSize - $maxHandledPacketLength;
164 if ($moreBytesToRead > 0) {
165 $MPCheaderData .= $this->fread($moreBytesToRead);
166 }
167 $profile_pns = getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 1));
168 $packet_offset += 1;
169 $quality_int = (($profile_pns & 0xF0) >> 4);
170 $quality_dec = (($profile_pns & 0x0E) >> 3);
171 $thisPacket['quality'] = (float) $quality_int + ($quality_dec / 8);
172 $thisPacket['pns_tool'] = (bool) (($profile_pns & 0x01) >> 0);
173 $thisPacket['version_major'] = getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 1));
174 $packet_offset += 1;
175 $thisPacket['version_minor'] = getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 1));
176 $packet_offset += 1;
177 $thisPacket['version_build'] = getid3_lib::BigEndian2Int(substr($MPCheaderData, $packet_offset, 1));
178 $packet_offset += 1;
179 $thisPacket['version'] = $thisPacket['version_major'].'.'.$thisPacket['version_minor'].'.'.$thisPacket['version_build'];
180
181 $info['audio']['encoder'] = 'MPC v'.$thisPacket['version'].' ('.(($thisPacket['version_minor'] % 2) ? 'unstable' : 'stable').')';
182 $thisfile_mpc_header['encoder_version'] = $info['audio']['encoder'];
183 //$thisfile_mpc_header['quality'] = (float) ($thisPacket['quality'] / 1.5875); // values can range from 0.000 to 15.875, mapped to qualities of 0.0 to 10.0
184 $thisfile_mpc_header['quality'] = (float) ($thisPacket['quality'] - 5); // values can range from 0.000 to 15.875, of which 0..4 are "reserved/experimental", and 5..15 are mapped to qualities of 0.0 to 10.0
185 break;
186
187 case 'SO': // Seek Table Offset
188 $packetLength = 0;
189 $thisPacket['seek_table_offset'] = $thisPacket['offset'] + $this->SV8variableLengthInteger(substr($MPCheaderData, $packet_offset, $maxHandledPacketLength), $packetLength);
190 $packet_offset += $packetLength;
191 break;
192
193 case 'ST': // Seek Table
194 case 'SE': // Stream End
195 case 'AP': // Audio Data
196 // nothing useful here, just skip this packet
197 $thisPacket = array();
198 break;
199
200 default:
201 $this->error('Found unhandled key type "'.$thisPacket['key'].'" at offset '.$thisPacket['offset']);
202 return false;
203 break;
204 }
205 if (!empty($thisPacket)) {
206 $info['mpc']['packets'][] = $thisPacket;
207 }
208 $this->fseek($offset);
209 }
210 $thisfile_mpc_header['size'] = $offset;
211 return true;
212 }
213
214 /**
215 * @return bool
216 */
217 public function ParseMPCsv7() {
218 // this is SV7
219 // http://www.uni-jena.de/~pfk/mpp/sv8/header.html
220
221 $info = &$this->getid3->info;
222 $thisfile_mpc_header = &$info['mpc']['header'];
223 $offset = 0;
224
225 $thisfile_mpc_header['size'] = 28;
226 $MPCheaderData = $info['mpc']['header']['preamble'];
227 $MPCheaderData .= $this->fread($thisfile_mpc_header['size'] - strlen($info['mpc']['header']['preamble']));
228 $offset = strlen('MP+');
229
230 $StreamVersionByte = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 1));
231 $offset += 1;
232 $thisfile_mpc_header['stream_version_major'] = ($StreamVersionByte & 0x0F) >> 0;
233 $thisfile_mpc_header['stream_version_minor'] = ($StreamVersionByte & 0xF0) >> 4; // should always be 0, subversions no longer exist in SV8
234 $thisfile_mpc_header['frame_count'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 4));
235 $offset += 4;
236
237 if ($thisfile_mpc_header['stream_version_major'] != 7) {
238 $this->error('Only Musepack SV7 supported (this file claims to be v'.$thisfile_mpc_header['stream_version_major'].')');
239 return false;
240 }
241
242 $FlagsDWORD1 = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 4));
243 $offset += 4;
244 $thisfile_mpc_header['intensity_stereo'] = (bool) (($FlagsDWORD1 & 0x80000000) >> 31);
245 $thisfile_mpc_header['mid_side_stereo'] = (bool) (($FlagsDWORD1 & 0x40000000) >> 30);
246 $thisfile_mpc_header['max_subband'] = ($FlagsDWORD1 & 0x3F000000) >> 24;
247 $thisfile_mpc_header['raw']['profile'] = ($FlagsDWORD1 & 0x00F00000) >> 20;
248 $thisfile_mpc_header['begin_loud'] = (bool) (($FlagsDWORD1 & 0x00080000) >> 19);
249 $thisfile_mpc_header['end_loud'] = (bool) (($FlagsDWORD1 & 0x00040000) >> 18);
250 $thisfile_mpc_header['raw']['sample_rate'] = ($FlagsDWORD1 & 0x00030000) >> 16;
251 $thisfile_mpc_header['max_level'] = ($FlagsDWORD1 & 0x0000FFFF);
252
253 $thisfile_mpc_header['raw']['title_peak'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 2));
254 $offset += 2;
255 $thisfile_mpc_header['raw']['title_gain'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 2), true);
256 $offset += 2;
257
258 $thisfile_mpc_header['raw']['album_peak'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 2));
259 $offset += 2;
260 $thisfile_mpc_header['raw']['album_gain'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 2), true);
261 $offset += 2;
262
263 $FlagsDWORD2 = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 4));
264 $offset += 4;
265 $thisfile_mpc_header['true_gapless'] = (bool) (($FlagsDWORD2 & 0x80000000) >> 31);
266 $thisfile_mpc_header['last_frame_length'] = ($FlagsDWORD2 & 0x7FF00000) >> 20;
267
268
269 $thisfile_mpc_header['raw']['not_sure_what'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 3));
270 $offset += 3;
271 $thisfile_mpc_header['raw']['encoder_version'] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, $offset, 1));
272 $offset += 1;
273
274 $thisfile_mpc_header['profile'] = $this->MPCprofileNameLookup($thisfile_mpc_header['raw']['profile']);
275 $thisfile_mpc_header['sample_rate'] = $this->MPCfrequencyLookup($thisfile_mpc_header['raw']['sample_rate']);
276 if ($thisfile_mpc_header['sample_rate'] == 0) {
277 $this->error('Corrupt MPC file: frequency == zero');
278 return false;
279 }
280 $info['audio']['sample_rate'] = $thisfile_mpc_header['sample_rate'];
281 $thisfile_mpc_header['samples'] = ((($thisfile_mpc_header['frame_count'] - 1) * 1152) + $thisfile_mpc_header['last_frame_length']) * $info['audio']['channels'];
282
283 $info['playtime_seconds'] = ($thisfile_mpc_header['samples'] / $info['audio']['channels']) / $info['audio']['sample_rate'];
284 if ($info['playtime_seconds'] == 0) {
285 $this->error('Corrupt MPC file: playtime_seconds == zero');
286 return false;
287 }
288
289 // add size of file header to avdataoffset - calc bitrate correctly + MD5 data
290 $info['avdataoffset'] += $thisfile_mpc_header['size'];
291
292 $info['audio']['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];
293
294 $thisfile_mpc_header['title_peak'] = $thisfile_mpc_header['raw']['title_peak'];
295 $thisfile_mpc_header['title_peak_db'] = $this->MPCpeakDBLookup($thisfile_mpc_header['title_peak']);
296 if ($thisfile_mpc_header['raw']['title_gain'] < 0) {
297 $thisfile_mpc_header['title_gain_db'] = (float) (32768 + $thisfile_mpc_header['raw']['title_gain']) / -100;
298 } else {
299 $thisfile_mpc_header['title_gain_db'] = (float) $thisfile_mpc_header['raw']['title_gain'] / 100;
300 }
301
302 $thisfile_mpc_header['album_peak'] = $thisfile_mpc_header['raw']['album_peak'];
303 $thisfile_mpc_header['album_peak_db'] = $this->MPCpeakDBLookup($thisfile_mpc_header['album_peak']);
304 if ($thisfile_mpc_header['raw']['album_gain'] < 0) {
305 $thisfile_mpc_header['album_gain_db'] = (float) (32768 + $thisfile_mpc_header['raw']['album_gain']) / -100;
306 } else {
307 $thisfile_mpc_header['album_gain_db'] = (float) $thisfile_mpc_header['raw']['album_gain'] / 100;;
308 }
309 $thisfile_mpc_header['encoder_version'] = $this->MPCencoderVersionLookup($thisfile_mpc_header['raw']['encoder_version']);
310
311 $info['replay_gain']['track']['adjustment'] = $thisfile_mpc_header['title_gain_db'];
312 $info['replay_gain']['album']['adjustment'] = $thisfile_mpc_header['album_gain_db'];
313
314 if ($thisfile_mpc_header['title_peak'] > 0) {
315 $info['replay_gain']['track']['peak'] = $thisfile_mpc_header['title_peak'];
316 } elseif (round($thisfile_mpc_header['max_level'] * 1.18) > 0) {
317 $info['replay_gain']['track']['peak'] = getid3_lib::CastAsInt(round($thisfile_mpc_header['max_level'] * 1.18)); // why? I don't know - see mppdec.c
318 }
319 if ($thisfile_mpc_header['album_peak'] > 0) {
320 $info['replay_gain']['album']['peak'] = $thisfile_mpc_header['album_peak'];
321 }
322
323 //$info['audio']['encoder'] = 'SV'.$thisfile_mpc_header['stream_version_major'].'.'.$thisfile_mpc_header['stream_version_minor'].', '.$thisfile_mpc_header['encoder_version'];
324 $info['audio']['encoder'] = $thisfile_mpc_header['encoder_version'];
325 $info['audio']['encoder_options'] = $thisfile_mpc_header['profile'];
326 $thisfile_mpc_header['quality'] = (float) ($thisfile_mpc_header['raw']['profile'] - 5); // values can range from 0 to 15, of which 0..4 are "reserved/experimental", and 5..15 are mapped to qualities of 0.0 to 10.0
327
328 return true;
329 }
330
331 /**
332 * @return bool
333 */
334 public function ParseMPCsv6() {
335 // this is SV4 - SV6
336
337 $info = &$this->getid3->info;
338 $thisfile_mpc_header = &$info['mpc']['header'];
339 $offset = 0;
340
341 $thisfile_mpc_header['size'] = 8;
342 $this->fseek($info['avdataoffset']);
343 $MPCheaderData = $this->fread($thisfile_mpc_header['size']);
344
345 // add size of file header to avdataoffset - calc bitrate correctly + MD5 data
346 $info['avdataoffset'] += $thisfile_mpc_header['size'];
347
348 // Most of this code adapted from Jurgen Faul's MPEGplus source code - thanks Jurgen! :)
349 $HeaderDWORD[0] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, 0, 4));
350 $HeaderDWORD[1] = getid3_lib::LittleEndian2Int(substr($MPCheaderData, 4, 4));
351
352
353 // DDDD DDDD CCCC CCCC BBBB BBBB AAAA AAAA
354 // aaaa aaaa abcd dddd dddd deee eeff ffff
355 //
356 // a = bitrate = anything
357 // b = IS = anything
358 // c = MS = anything
359 // d = streamversion = 0000000004 or 0000000005 or 0000000006
360 // e = maxband = anything
361 // f = blocksize = 000001 for SV5+, anything(?) for SV4
362
363 $thisfile_mpc_header['target_bitrate'] = (($HeaderDWORD[0] & 0xFF800000) >> 23);
364 $thisfile_mpc_header['intensity_stereo'] = (bool) (($HeaderDWORD[0] & 0x00400000) >> 22);
365 $thisfile_mpc_header['mid_side_stereo'] = (bool) (($HeaderDWORD[0] & 0x00200000) >> 21);
366 $thisfile_mpc_header['stream_version_major'] = ($HeaderDWORD[0] & 0x001FF800) >> 11;
367 $thisfile_mpc_header['stream_version_minor'] = 0; // no sub-version numbers before SV7
368 $thisfile_mpc_header['max_band'] = ($HeaderDWORD[0] & 0x000007C0) >> 6; // related to lowpass frequency, not sure how it translates exactly
369 $thisfile_mpc_header['block_size'] = ($HeaderDWORD[0] & 0x0000003F);
370
371 switch ($thisfile_mpc_header['stream_version_major']) {
372 case 4:
373 $thisfile_mpc_header['frame_count'] = ($HeaderDWORD[1] >> 16);
374 break;
375
376 case 5:
377 case 6:
378 $thisfile_mpc_header['frame_count'] = $HeaderDWORD[1];
379 break;
380
381 default:
382 $info['error'] = 'Expecting 4, 5 or 6 in version field, found '.$thisfile_mpc_header['stream_version_major'].' instead';
383 unset($info['mpc']);
384 return false;
385 break;
386 }
387
388 if (($thisfile_mpc_header['stream_version_major'] > 4) && ($thisfile_mpc_header['block_size'] != 1)) {
389 $this->warning('Block size expected to be 1, actual value found: '.$thisfile_mpc_header['block_size']);
390 }
391
392 $thisfile_mpc_header['sample_rate'] = 44100; // AB: used by all files up to SV7
393 $info['audio']['sample_rate'] = $thisfile_mpc_header['sample_rate'];
394 $thisfile_mpc_header['samples'] = $thisfile_mpc_header['frame_count'] * 1152 * $info['audio']['channels'];
395
396 if ($thisfile_mpc_header['target_bitrate'] == 0) {
397 $info['audio']['bitrate_mode'] = 'vbr';
398 } else {
399 $info['audio']['bitrate_mode'] = 'cbr';
400 }
401
402 $info['mpc']['bitrate'] = ($info['avdataend'] - $info['avdataoffset']) * 8 * 44100 / $thisfile_mpc_header['frame_count'] / 1152;
403 $info['audio']['bitrate'] = $info['mpc']['bitrate'];
404 $info['audio']['encoder'] = 'SV'.$thisfile_mpc_header['stream_version_major'];
405
406 return true;
407 }
408
409 /**
410 * @param int $profileid
411 *
412 * @return string
413 */
414 public function MPCprofileNameLookup($profileid) {
415 static $MPCprofileNameLookup = array(
416 0 => 'no profile',
417 1 => 'Experimental',
418 2 => 'unused',
419 3 => 'unused',
420 4 => 'unused',
421 5 => 'below Telephone (q = 0.0)',
422 6 => 'below Telephone (q = 1.0)',
423 7 => 'Telephone (q = 2.0)',
424 8 => 'Thumb (q = 3.0)',
425 9 => 'Radio (q = 4.0)',
426 10 => 'Standard (q = 5.0)',
427 11 => 'Extreme (q = 6.0)',
428 12 => 'Insane (q = 7.0)',
429 13 => 'BrainDead (q = 8.0)',
430 14 => 'above BrainDead (q = 9.0)',
431 15 => 'above BrainDead (q = 10.0)'
432 );
433 return (isset($MPCprofileNameLookup[$profileid]) ? $MPCprofileNameLookup[$profileid] : 'invalid');
434 }
435
436 /**
437 * @param int $frequencyid
438 *
439 * @return int|string
440 */
441 public function MPCfrequencyLookup($frequencyid) {
442 static $MPCfrequencyLookup = array(
443 0 => 44100,
444 1 => 48000,
445 2 => 37800,
446 3 => 32000
447 );
448 return (isset($MPCfrequencyLookup[$frequencyid]) ? $MPCfrequencyLookup[$frequencyid] : 'invalid');
449 }
450
451 /**
452 * @param int $intvalue
453 *
454 * @return float|false
455 */
456 public function MPCpeakDBLookup($intvalue) {
457 if ($intvalue > 0) {
458 return ((log10($intvalue) / log10(2)) - 15) * 6;
459 }
460 return false;
461 }
462
463 /**
464 * @param int $encoderversion
465 *
466 * @return string
467 */
468 public function MPCencoderVersionLookup($encoderversion) {
469 //Encoder version * 100 (106 = 1.06)
470 //EncoderVersion % 10 == 0 Release (1.0)
471 //EncoderVersion % 2 == 0 Beta (1.06)
472 //EncoderVersion % 2 == 1 Alpha (1.05a...z)
473
474 if ($encoderversion == 0) {
475 // very old version, not known exactly which
476 return 'Buschmann v1.7.0-v1.7.9 or Klemm v0.90-v1.05';
477 }
478
479 if (($encoderversion % 10) == 0) {
480
481 // release version
482 return number_format($encoderversion / 100, 2);
483
484 } elseif (($encoderversion % 2) == 0) {
485
486 // beta version
487 return number_format($encoderversion / 100, 2).' beta';
488
489 }
490
491 // alpha version
492 return number_format($encoderversion / 100, 2).' alpha';
493 }
494
495 /**
496 * @param string $data
497 * @param int $packetLength
498 * @param int $maxHandledPacketLength
499 *
500 * @return int|false
501 */
502 public function SV8variableLengthInteger($data, &$packetLength, $maxHandledPacketLength=9) {
503 $packet_size = 0;
504 for ($packetLength = 1; $packetLength <= $maxHandledPacketLength; $packetLength++) {
505 // variable-length size field:
506 // bits, big-endian
507 // 0xxx xxxx - value 0 to 2^7-1
508 // 1xxx xxxx 0xxx xxxx - value 0 to 2^14-1
509 // 1xxx xxxx 1xxx xxxx 0xxx xxxx - value 0 to 2^21-1
510 // 1xxx xxxx 1xxx xxxx 1xxx xxxx 0xxx xxxx - value 0 to 2^28-1
511 // ...
512 $thisbyte = ord(substr($data, ($packetLength - 1), 1));
513 // look through bytes until find a byte with MSB==0
514 $packet_size = ($packet_size << 7);
515 $packet_size = ($packet_size | ($thisbyte & 0x7F));
516 if (($thisbyte & 0x80) === 0) {
517 break;
518 }
519 if ($packetLength >= $maxHandledPacketLength) {
520 return false;
521 }
522 }
523 return $packet_size;
524 }
525
526 /**
527 * @param string $packetKey
528 *
529 * @return string
530 */
531 public function MPCsv8PacketName($packetKey) {
532 static $MPCsv8PacketName = array();
533 if (empty($MPCsv8PacketName)) {
534 $MPCsv8PacketName = array(
535 'AP' => 'Audio Packet',
536 'CT' => 'Chapter Tag',
537 'EI' => 'Encoder Info',
538 'RG' => 'Replay Gain',
539 'SE' => 'Stream End',
540 'SH' => 'Stream Header',
541 'SO' => 'Seek Table Offset',
542 'ST' => 'Seek Table',
543 );
544 }
545 return (isset($MPCsv8PacketName[$packetKey]) ? $MPCsv8PacketName[$packetKey] : $packetKey);
546 }
547 }