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