view e2gallerypro/e2upload/Backend/Assets/getid3/module.audio.wavpack.php @ 3:3f6b44aa6b35 judyates

[svn r4] added ability to buy stuff, from a Prints page, but it doesn't work well with the css, and it also has not been fitted into the perl make system.
author rlm
date Mon, 22 Feb 2010 08:02:39 -0500
parents
children
line wrap: on
line source
1 <?php
2 // +----------------------------------------------------------------------+
3 // | PHP version 5 |
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) 2002-2006 James Heinrich, Allan Hansen |
6 // +----------------------------------------------------------------------+
7 // | This source file is subject to version 2 of the GPL license, |
8 // | that is bundled with this package in the file license.txt and is |
9 // | available through the world-wide-web at the following url: |
10 // | http://www.gnu.org/copyleft/gpl.html |
11 // +----------------------------------------------------------------------+
12 // | getID3() - http://getid3.sourceforge.net or http://www.getid3.org |
13 // +----------------------------------------------------------------------+
14 // | Authors: James Heinrich <infoØgetid3*org> |
15 // | Allan Hansen <ahØartemis*dk> |
16 // +----------------------------------------------------------------------+
17 // | module.audio.wavpack.php |
18 // | module for analyzing WavPack v4.0+ Audio files |
19 // | dependencies: audio-video.riff |
20 // +----------------------------------------------------------------------+
21 //
22 // $Id: module.audio.wavpack.php,v 1.2 2006/11/02 10:48:02 ah Exp $
25 class getid3_wavpack extends getid3_handler
26 {
28 public function Analyze() {
30 $getid3 = $this->getid3;
32 $getid3->include_module('audio-video.riff');
34 $getid3->info['wavpack'] = array ();
35 $info_wavpack = &$getid3->info['wavpack'];
37 fseek($getid3->fp, $getid3->info['avdataoffset'], SEEK_SET);
39 while (true) {
41 $wavpack_header = fread($getid3->fp, 32);
43 if (ftell($getid3->fp) >= $getid3->info['avdataend']) {
44 break;
45 } elseif (feof($getid3->fp)) {
46 break;
47 } elseif (
48 (@$info_wavpack_blockheader['total_samples'] > 0) &&
49 (@$info_wavpack_blockheader['block_samples'] > 0) &&
50 (!isset($info_wavpack['riff_trailer_size']) || ($info_wavpack['riff_trailer_size'] <= 0)) &&
51 ((@$info_wavpack['config_flags']['md5_checksum'] === false) || !empty($getid3->info['md5_data_source']))) {
52 break;
53 }
55 $block_header_offset = ftell($getid3->fp) - 32;
56 $block_header_magic = substr($wavpack_header, 0, 4);
57 $block_header_size = getid3_lib::LittleEndian2Int(substr($wavpack_header, 4, 4));
59 if ($block_header_magic != 'wvpk') {
60 throw new getid3_exception('Expecting "wvpk" at offset '.$block_header_offset.', found "'.$block_header_magic.'"');
61 }
63 if ((@$info_wavpack_blockheader['block_samples'] <= 0) || (@$info_wavpack_blockheader['total_samples'] <= 0)) {
65 // Also, it is possible that the first block might not have
66 // any samples (block_samples == 0) and in this case you should skip blocks
67 // until you find one with samples because the other information (like
68 // total_samples) are not guaranteed to be correct until (block_samples > 0)
70 // Finally, I have defined a format for files in which the length is not known
71 // (for example when raw files are created using pipes). In these cases
72 // total_samples will be -1 and you must seek to the final block to determine
73 // the total number of samples.
76 $getid3->info['audio']['dataformat'] = 'wavpack';
77 $getid3->info['fileformat'] = 'wavpack';
78 $getid3->info['audio']['lossless'] = true;
79 $getid3->info['audio']['bitrate_mode'] = 'vbr';
81 $info_wavpack['blockheader']['offset'] = $block_header_offset;
82 $info_wavpack['blockheader']['magic'] = $block_header_magic;
83 $info_wavpack['blockheader']['size'] = $block_header_size;
84 $info_wavpack_blockheader = &$info_wavpack['blockheader'];
86 if ($info_wavpack_blockheader['size'] >= 0x100000) {
87 throw new getid3_exception('Expecting WavPack block size less than "0x100000", found "'.$info_wavpack_blockheader['size'].'" at offset '.$info_wavpack_blockheader['offset']);
88 }
90 $info_wavpack_blockheader['minor_version'] = ord($wavpack_header{8});
91 $info_wavpack_blockheader['major_version'] = ord($wavpack_header{9});
93 if (($info_wavpack_blockheader['major_version'] != 4) ||
94 (($info_wavpack_blockheader['minor_version'] < 4) &&
95 ($info_wavpack_blockheader['minor_version'] > 16))) {
96 throw new getid3_exception('Expecting WavPack version between "4.2" and "4.16", found version "'.$info_wavpack_blockheader['major_version'].'.'.$info_wavpack_blockheader['minor_version'].'" at offset '.$info_wavpack_blockheader['offset']);
97 }
99 $info_wavpack_blockheader['track_number'] = ord($wavpack_header{10}); // unused
100 $info_wavpack_blockheader['index_number'] = ord($wavpack_header{11}); // unused
102 getid3_lib::ReadSequence('LittleEndian2Int', $info_wavpack_blockheader, $wavpack_header, 12,
103 array (
104 'total_samples' => 4,
105 'block_index' => 4,
106 'block_samples' => 4,
107 'flags_raw' => 4,
108 'crc' => 4
109 )
110 );
113 $info_wavpack_blockheader['flags']['bytes_per_sample'] = 1 + ($info_wavpack_blockheader['flags_raw'] & 0x00000003);
114 $info_wavpack_blockheader['flags']['mono'] = (bool) ($info_wavpack_blockheader['flags_raw'] & 0x00000004);
115 $info_wavpack_blockheader['flags']['hybrid'] = (bool) ($info_wavpack_blockheader['flags_raw'] & 0x00000008);
116 $info_wavpack_blockheader['flags']['joint_stereo'] = (bool) ($info_wavpack_blockheader['flags_raw'] & 0x00000010);
117 $info_wavpack_blockheader['flags']['cross_decorrelation'] = (bool) ($info_wavpack_blockheader['flags_raw'] & 0x00000020);
118 $info_wavpack_blockheader['flags']['hybrid_noiseshape'] = (bool) ($info_wavpack_blockheader['flags_raw'] & 0x00000040);
119 $info_wavpack_blockheader['flags']['ieee_32bit_float'] = (bool) ($info_wavpack_blockheader['flags_raw'] & 0x00000080);
120 $info_wavpack_blockheader['flags']['int_32bit'] = (bool) ($info_wavpack_blockheader['flags_raw'] & 0x00000100);
121 $info_wavpack_blockheader['flags']['hybrid_bitrate_noise'] = (bool) ($info_wavpack_blockheader['flags_raw'] & 0x00000200);
122 $info_wavpack_blockheader['flags']['hybrid_balance_noise'] = (bool) ($info_wavpack_blockheader['flags_raw'] & 0x00000400);
123 $info_wavpack_blockheader['flags']['multichannel_initial'] = (bool) ($info_wavpack_blockheader['flags_raw'] & 0x00000800);
124 $info_wavpack_blockheader['flags']['multichannel_final'] = (bool) ($info_wavpack_blockheader['flags_raw'] & 0x00001000);
126 $getid3->info['audio']['lossless'] = !$info_wavpack_blockheader['flags']['hybrid'];
127 }
130 while (!feof($getid3->fp) && (ftell($getid3->fp) < ($block_header_offset + $block_header_size + 8))) {
132 $metablock = array('offset'=>ftell($getid3->fp));
133 $metablockheader = fread($getid3->fp, 2);
134 if (feof($getid3->fp)) {
135 break;
136 }
137 $metablock['id'] = ord($metablockheader{0});
138 $metablock['function_id'] = ($metablock['id'] & 0x3F);
139 $metablock['function_name'] = $this->WavPackMetablockNameLookup($metablock['function_id']);
141 // The 0x20 bit in the id of the meta subblocks (which is defined as
142 // ID_OPTIONAL_DATA) is a permanent part of the id. The idea is that
143 // if a decoder encounters an id that it does not know about, it uses
144 // that "ID_OPTIONAL_DATA" flag to determine what to do. If it is set
145 // then the decoder simply ignores the metadata, but if it is zero
146 // then the decoder should quit because it means that an understanding
147 // of the metadata is required to correctly decode the audio.
149 $metablock['non_decoder'] = (bool) ($metablock['id'] & 0x20);
150 $metablock['padded_data'] = (bool) ($metablock['id'] & 0x40);
151 $metablock['large_block'] = (bool) ($metablock['id'] & 0x80);
152 if ($metablock['large_block']) {
153 $metablockheader .= fread($getid3->fp, 2);
154 }
155 $metablock['size'] = getid3_lib::LittleEndian2Int(substr($metablockheader, 1)) * 2; // size is stored in words
156 $metablock['data'] = null;
158 if ($metablock['size'] > 0) {
160 switch ($metablock['function_id']) {
162 case 0x21: // ID_RIFF_HEADER
163 case 0x22: // ID_RIFF_TRAILER
164 case 0x23: // ID_REPLAY_GAIN
165 case 0x24: // ID_CUESHEET
166 case 0x25: // ID_CONFIG_BLOCK
167 case 0x26: // ID_MD5_CHECKSUM
168 $metablock['data'] = fread($getid3->fp, $metablock['size']);
170 if ($metablock['padded_data']) {
171 // padded to the nearest even byte
172 $metablock['size']--;
173 $metablock['data'] = substr($metablock['data'], 0, -1);
174 }
175 break;
178 case 0x00: // ID_DUMMY
179 case 0x01: // ID_ENCODER_INFO
180 case 0x02: // ID_DECORR_TERMS
181 case 0x03: // ID_DECORR_WEIGHTS
182 case 0x04: // ID_DECORR_SAMPLES
183 case 0x05: // ID_ENTROPY_VARS
184 case 0x06: // ID_HYBRID_PROFILE
185 case 0x07: // ID_SHAPING_WEIGHTS
186 case 0x08: // ID_FLOAT_INFO
187 case 0x09: // ID_INT32_INFO
188 case 0x0A: // ID_WV_BITSTREAM
189 case 0x0B: // ID_WVC_BITSTREAM
190 case 0x0C: // ID_WVX_BITSTREAM
191 case 0x0D: // ID_CHANNEL_INFO
192 fseek($getid3->fp, $metablock['offset'] + ($metablock['large_block'] ? 4 : 2) + $metablock['size'], SEEK_SET);
193 break;
196 default:
197 $getid3->warning('Unexpected metablock type "0x'.str_pad(dechex($metablock['function_id']), 2, '0', STR_PAD_LEFT).'" at offset '.$metablock['offset']);
198 fseek($getid3->fp, $metablock['offset'] + ($metablock['large_block'] ? 4 : 2) + $metablock['size'], SEEK_SET);
199 break;
200 }
203 switch ($metablock['function_id']) {
205 case 0x21: // ID_RIFF_HEADER
207 $original_wav_filesize = getid3_lib::LittleEndian2Int(substr($metablock['data'], 4, 4));
209 // Clone getid3
210 $clone = clone $getid3;
212 // Analyze clone by string
213 $riff = new getid3_riff($clone);
214 $riff->AnalyzeString($metablock['data']);
216 // Import from clone and destroy
217 $metablock['riff'] = $clone->info['riff'];
218 $getid3->warnings($clone->warnings());
219 unset($clone);
221 // Save RIFF header - we may need it later for RIFF footer parsing
222 $this->riff_header = $metablock['data'];
224 $metablock['riff']['original_filesize'] = $original_wav_filesize;
225 $info_wavpack['riff_trailer_size'] = $original_wav_filesize - $metablock['riff']['WAVE']['data'][0]['size'] - $metablock['riff']['header_size'];
227 $getid3->info['audio']['sample_rate'] = $metablock['riff']['raw']['fmt ']['nSamplesPerSec'];
228 $getid3->info['playtime_seconds'] = $info_wavpack_blockheader['total_samples'] / $getid3->info['audio']['sample_rate'];
230 // Safe RIFF header in case there's a RIFF footer later
231 $metablock_riff_header = $metablock['data'];
232 break;
235 case 0x22: // ID_RIFF_TRAILER
237 $metablock_riff_footer = $metablock_riff_header.$metablock['data'];
239 $start_offset = $metablock['offset'] + ($metablock['large_block'] ? 4 : 2);
241 $ftell_old = ftell($getid3->fp);
243 // Clone getid3
244 $clone = clone $getid3;
246 // Call public method that really should be private
247 $riff = new getid3_riff($clone);
248 $metablock['riff'] = $riff->ParseRIFF($start_offset, $start_offset + $metablock['size']);
249 unset($clone);
251 fseek($getid3->fp, $ftell_old, SEEK_SET);
253 if (!empty($metablock['riff']['INFO'])) {
254 getid3_riff::RIFFCommentsParse($metablock['riff']['INFO'], $metablock['comments']);
255 $getid3->info['tags']['riff'] = $metablock['comments'];
256 }
257 break;
260 case 0x23: // ID_REPLAY_GAIN
261 $getid3->warning('WavPack "Replay Gain" contents not yet handled by getID3() in metablock at offset '.$metablock['offset']);
262 break;
265 case 0x24: // ID_CUESHEET
266 $getid3->warning('WavPack "Cuesheet" contents not yet handled by getID3() in metablock at offset '.$metablock['offset']);
267 break;
270 case 0x25: // ID_CONFIG_BLOCK
271 $metablock['flags_raw'] = getid3_lib::LittleEndian2Int(substr($metablock['data'], 0, 3));
273 $metablock['flags']['adobe_mode'] = (bool) ($metablock['flags_raw'] & 0x000001); // "adobe" mode for 32-bit floats
274 $metablock['flags']['fast_flag'] = (bool) ($metablock['flags_raw'] & 0x000002); // fast mode
275 $metablock['flags']['very_fast_flag'] = (bool) ($metablock['flags_raw'] & 0x000004); // double fast
276 $metablock['flags']['high_flag'] = (bool) ($metablock['flags_raw'] & 0x000008); // high quality mode
277 $metablock['flags']['very_high_flag'] = (bool) ($metablock['flags_raw'] & 0x000010); // double high (not used yet)
278 $metablock['flags']['bitrate_kbps'] = (bool) ($metablock['flags_raw'] & 0x000020); // bitrate is kbps, not bits / sample
279 $metablock['flags']['auto_shaping'] = (bool) ($metablock['flags_raw'] & 0x000040); // automatic noise shaping
280 $metablock['flags']['shape_override'] = (bool) ($metablock['flags_raw'] & 0x000080); // shaping mode specified
281 $metablock['flags']['joint_override'] = (bool) ($metablock['flags_raw'] & 0x000100); // joint-stereo mode specified
282 $metablock['flags']['copy_time'] = (bool) ($metablock['flags_raw'] & 0x000200); // copy file-time from source
283 $metablock['flags']['create_exe'] = (bool) ($metablock['flags_raw'] & 0x000400); // create executable
284 $metablock['flags']['create_wvc'] = (bool) ($metablock['flags_raw'] & 0x000800); // create correction file
285 $metablock['flags']['optimize_wvc'] = (bool) ($metablock['flags_raw'] & 0x001000); // maximize bybrid compression
286 $metablock['flags']['quality_mode'] = (bool) ($metablock['flags_raw'] & 0x002000); // psychoacoustic quality mode
287 $metablock['flags']['raw_flag'] = (bool) ($metablock['flags_raw'] & 0x004000); // raw mode (not implemented yet)
288 $metablock['flags']['calc_noise'] = (bool) ($metablock['flags_raw'] & 0x008000); // calc noise in hybrid mode
289 $metablock['flags']['lossy_mode'] = (bool) ($metablock['flags_raw'] & 0x010000); // obsolete (for information)
290 $metablock['flags']['extra_mode'] = (bool) ($metablock['flags_raw'] & 0x020000); // extra processing mode
291 $metablock['flags']['skip_wvx'] = (bool) ($metablock['flags_raw'] & 0x040000); // no wvx stream w/ floats & big ints
292 $metablock['flags']['md5_checksum'] = (bool) ($metablock['flags_raw'] & 0x080000); // compute & store MD5 signature
293 $metablock['flags']['quiet_mode'] = (bool) ($metablock['flags_raw'] & 0x100000); // don't report progress %
295 $info_wavpack['config_flags'] = $metablock['flags'];
297 $getid3->info['audio']['encoder_options'] = trim(
298 ($info_wavpack_blockheader['flags']['hybrid'] ? ' -b???' : '') .
299 ($metablock['flags']['adobe_mode'] ? ' -a' : '') .
300 ($metablock['flags']['optimize_wvc'] ? ' -cc' : '') .
301 ($metablock['flags']['create_exe'] ? ' -e' : '') .
302 ($metablock['flags']['fast_flag'] ? ' -f' : '') .
303 ($metablock['flags']['joint_override'] ? ' -j?' : '') .
304 ($metablock['flags']['high_flag'] ? ' -h' : '') .
305 ($metablock['flags']['md5_checksum'] ? ' -m' : '') .
306 ($metablock['flags']['calc_noise'] ? ' -n' : '') .
307 ($metablock['flags']['shape_override'] ? ' -s?' : '') .
308 ($metablock['flags']['extra_mode'] ? ' -x?' : '')
309 );
310 if (!$getid3->info['audio']['encoder_options']) {
311 unset($getid3->info['audio']['encoder_options']);
312 }
313 break;
316 case 0x26: // ID_MD5_CHECKSUM
317 if (strlen($metablock['data']) == 16) {
318 $getid3->info['md5_data_source'] = strtolower(getid3_lib::PrintHexBytes($metablock['data'], true, false, false));
319 } else {
320 $getid3->warning('Expecting 16 bytes of WavPack "MD5 Checksum" in metablock at offset '.$metablock['offset'].', but found '.strlen($metablock['data']).' bytes');
321 }
322 break;
325 case 0x00: // ID_DUMMY
326 case 0x01: // ID_ENCODER_INFO
327 case 0x02: // ID_DECORR_TERMS
328 case 0x03: // ID_DECORR_WEIGHTS
329 case 0x04: // ID_DECORR_SAMPLES
330 case 0x05: // ID_ENTROPY_VARS
331 case 0x06: // ID_HYBRID_PROFILE
332 case 0x07: // ID_SHAPING_WEIGHTS
333 case 0x08: // ID_FLOAT_INFO
334 case 0x09: // ID_INT32_INFO
335 case 0x0A: // ID_WV_BITSTREAM
336 case 0x0B: // ID_WVC_BITSTREAM
337 case 0x0C: // ID_WVX_BITSTREAM
338 case 0x0D: // ID_CHANNEL_INFO
339 unset($metablock);
340 break;
341 }
343 }
345 if (!empty($metablock)) {
346 $info_wavpack['metablocks'][] = $metablock;
347 }
349 }
351 }
353 $getid3->info['audio']['encoder'] = 'WavPack v'.$info_wavpack_blockheader['major_version'].'.'.str_pad($info_wavpack_blockheader['minor_version'], 2, '0', STR_PAD_LEFT);
354 $getid3->info['audio']['bits_per_sample'] = $info_wavpack_blockheader['flags']['bytes_per_sample'] * 8;
355 $getid3->info['audio']['channels'] = ($info_wavpack_blockheader['flags']['mono'] ? 1 : 2);
357 if (@$getid3->info['playtime_seconds']) {
358 $getid3->info['audio']['bitrate'] = (($getid3->info['avdataend'] - $getid3->info['avdataoffset']) * 8) / $getid3->info['playtime_seconds'];
359 } else {
360 $getid3->info['audio']['dataformat'] = 'wvc';
361 }
363 return true;
364 }
368 public static function WavPackMetablockNameLookup($id) {
370 static $lookup = array(
371 0x00 => 'Dummy',
372 0x01 => 'Encoder Info',
373 0x02 => 'Decorrelation Terms',
374 0x03 => 'Decorrelation Weights',
375 0x04 => 'Decorrelation Samples',
376 0x05 => 'Entropy Variables',
377 0x06 => 'Hybrid Profile',
378 0x07 => 'Shaping Weights',
379 0x08 => 'Float Info',
380 0x09 => 'Int32 Info',
381 0x0A => 'WV Bitstream',
382 0x0B => 'WVC Bitstream',
383 0x0C => 'WVX Bitstream',
384 0x0D => 'Channel Info',
385 0x21 => 'RIFF header',
386 0x22 => 'RIFF trailer',
387 0x23 => 'Replay Gain',
388 0x24 => 'Cuesheet',
389 0x25 => 'Config Block',
390 0x26 => 'MD5 Checksum',
391 );
393 return (@$lookup[$id]);
394 }
396 }
399 ?>