ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
getid3_wavpack Class Reference

getID3() by James Heinrich info@.nosp@m.geti.nosp@m.d3.or.nosp@m.g // More...

+ Inheritance diagram for getid3_wavpack:
+ Collaboration diagram for getid3_wavpack:

Public Member Functions

 Analyze ()
 
 WavPackMetablockNameLookup (&$id)
 
- Public Member Functions inherited from getid3_handler
 __construct (getID3 $getid3, $call_module=null)
 
 Analyze ()
 
 AnalyzeString ($string)
 
 setStringMode ($string)
 
 saveAttachment ($name, $offset, $length, $image_mime=null)
 

Additional Inherited Members

- Protected Member Functions inherited from getid3_handler
 ftell ()
 
 fread ($bytes)
 
 fseek ($bytes, $whence=SEEK_SET)
 
 feof ()
 
 isDependencyFor ($module)
 
 error ($text)
 
 warning ($text)
 
 notice ($text)
 
- Protected Attributes inherited from getid3_handler
 $getid3
 
 $data_string_flag = false
 
 $data_string = ''
 
 $data_string_position = 0
 
 $data_string_length = 0
 

Detailed Description

getID3() by James Heinrich info@.nosp@m.geti.nosp@m.d3.or.nosp@m.g //

Definition at line 18 of file module.audio.wavpack.php.

Member Function Documentation

◆ Analyze()

getid3_wavpack::Analyze ( )

Definition at line 21 of file module.audio.wavpack.php.

References $info, getid3_handler\error(), getid3_handler\feof(), getid3_handler\fread(), getid3_handler\fseek(), getid3_handler\ftell(), getid3_lib\IncludeDependency(), getid3_lib\LittleEndian2Int(), getid3_riff\parseComments(), getid3_lib\PrintHexBytes(), getid3_handler\warning(), and WavPackMetablockNameLookup().

21  {
22  $info = &$this->getid3->info;
23 
24  $this->fseek($info['avdataoffset']);
25 
26  while (true) {
27 
28  $wavpackheader = $this->fread(32);
29 
30  if ($this->ftell() >= $info['avdataend']) {
31  break;
32  } elseif (feof($this->getid3->fp)) {
33  break;
34  } elseif (
35  isset($info['wavpack']['blockheader']['total_samples']) &&
36  isset($info['wavpack']['blockheader']['block_samples']) &&
37  ($info['wavpack']['blockheader']['total_samples'] > 0) &&
38  ($info['wavpack']['blockheader']['block_samples'] > 0) &&
39  (!isset($info['wavpack']['riff_trailer_size']) || ($info['wavpack']['riff_trailer_size'] <= 0)) &&
40  ((isset($info['wavpack']['config_flags']['md5_checksum']) && ($info['wavpack']['config_flags']['md5_checksum'] === false)) || !empty($info['md5_data_source']))) {
41  break;
42  }
43 
44  $blockheader_offset = $this->ftell() - 32;
45  $blockheader_magic = substr($wavpackheader, 0, 4);
46  $blockheader_size = getid3_lib::LittleEndian2Int(substr($wavpackheader, 4, 4));
47 
48  $magic = 'wvpk';
49  if ($blockheader_magic != $magic) {
50  $this->error('Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$blockheader_offset.', found "'.getid3_lib::PrintHexBytes($blockheader_magic).'"');
51  switch (isset($info['audio']['dataformat']) ? $info['audio']['dataformat'] : '') {
52  case 'wavpack':
53  case 'wvc':
54  break;
55  default:
56  unset($info['fileformat']);
57  unset($info['audio']);
58  unset($info['wavpack']);
59  break;
60  }
61  return false;
62  }
63 
64  if (empty($info['wavpack']['blockheader']['block_samples']) ||
65  empty($info['wavpack']['blockheader']['total_samples']) ||
66  ($info['wavpack']['blockheader']['block_samples'] <= 0) ||
67  ($info['wavpack']['blockheader']['total_samples'] <= 0)) {
68  // Also, it is possible that the first block might not have
69  // any samples (block_samples == 0) and in this case you should skip blocks
70  // until you find one with samples because the other information (like
71  // total_samples) are not guaranteed to be correct until (block_samples > 0)
72 
73  // Finally, I have defined a format for files in which the length is not known
74  // (for example when raw files are created using pipes). In these cases
75  // total_samples will be -1 and you must seek to the final block to determine
76  // the total number of samples.
77 
78 
79  $info['audio']['dataformat'] = 'wavpack';
80  $info['fileformat'] = 'wavpack';
81  $info['audio']['lossless'] = true;
82  $info['audio']['bitrate_mode'] = 'vbr';
83 
84  $info['wavpack']['blockheader']['offset'] = $blockheader_offset;
85  $info['wavpack']['blockheader']['magic'] = $blockheader_magic;
86  $info['wavpack']['blockheader']['size'] = $blockheader_size;
87 
88  if ($info['wavpack']['blockheader']['size'] >= 0x100000) {
89  $this->error('Expecting WavPack block size less than "0x100000", found "'.$info['wavpack']['blockheader']['size'].'" at offset '.$info['wavpack']['blockheader']['offset']);
90  switch (isset($info['audio']['dataformat']) ? $info['audio']['dataformat'] : '') {
91  case 'wavpack':
92  case 'wvc':
93  break;
94  default:
95  unset($info['fileformat']);
96  unset($info['audio']);
97  unset($info['wavpack']);
98  break;
99  }
100  return false;
101  }
102 
103  $info['wavpack']['blockheader']['minor_version'] = ord($wavpackheader{8});
104  $info['wavpack']['blockheader']['major_version'] = ord($wavpackheader{9});
105 
106  if (($info['wavpack']['blockheader']['major_version'] != 4) ||
107  (($info['wavpack']['blockheader']['minor_version'] < 4) &&
108  ($info['wavpack']['blockheader']['minor_version'] > 16))) {
109  $this->error('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']);
110  switch (isset($info['audio']['dataformat']) ? $info['audio']['dataformat'] : '') {
111  case 'wavpack':
112  case 'wvc':
113  break;
114  default:
115  unset($info['fileformat']);
116  unset($info['audio']);
117  unset($info['wavpack']);
118  break;
119  }
120  return false;
121  }
122 
123  $info['wavpack']['blockheader']['track_number'] = ord($wavpackheader{10}); // unused
124  $info['wavpack']['blockheader']['index_number'] = ord($wavpackheader{11}); // unused
125  $info['wavpack']['blockheader']['total_samples'] = getid3_lib::LittleEndian2Int(substr($wavpackheader, 12, 4));
126  $info['wavpack']['blockheader']['block_index'] = getid3_lib::LittleEndian2Int(substr($wavpackheader, 16, 4));
127  $info['wavpack']['blockheader']['block_samples'] = getid3_lib::LittleEndian2Int(substr($wavpackheader, 20, 4));
128  $info['wavpack']['blockheader']['flags_raw'] = getid3_lib::LittleEndian2Int(substr($wavpackheader, 24, 4));
129  $info['wavpack']['blockheader']['crc'] = getid3_lib::LittleEndian2Int(substr($wavpackheader, 28, 4));
130 
131  $info['wavpack']['blockheader']['flags']['bytes_per_sample'] = 1 + ($info['wavpack']['blockheader']['flags_raw'] & 0x00000003);
132  $info['wavpack']['blockheader']['flags']['mono'] = (bool) ($info['wavpack']['blockheader']['flags_raw'] & 0x00000004);
133  $info['wavpack']['blockheader']['flags']['hybrid'] = (bool) ($info['wavpack']['blockheader']['flags_raw'] & 0x00000008);
134  $info['wavpack']['blockheader']['flags']['joint_stereo'] = (bool) ($info['wavpack']['blockheader']['flags_raw'] & 0x00000010);
135  $info['wavpack']['blockheader']['flags']['cross_decorrelation'] = (bool) ($info['wavpack']['blockheader']['flags_raw'] & 0x00000020);
136  $info['wavpack']['blockheader']['flags']['hybrid_noiseshape'] = (bool) ($info['wavpack']['blockheader']['flags_raw'] & 0x00000040);
137  $info['wavpack']['blockheader']['flags']['ieee_32bit_float'] = (bool) ($info['wavpack']['blockheader']['flags_raw'] & 0x00000080);
138  $info['wavpack']['blockheader']['flags']['int_32bit'] = (bool) ($info['wavpack']['blockheader']['flags_raw'] & 0x00000100);
139  $info['wavpack']['blockheader']['flags']['hybrid_bitrate_noise'] = (bool) ($info['wavpack']['blockheader']['flags_raw'] & 0x00000200);
140  $info['wavpack']['blockheader']['flags']['hybrid_balance_noise'] = (bool) ($info['wavpack']['blockheader']['flags_raw'] & 0x00000400);
141  $info['wavpack']['blockheader']['flags']['multichannel_initial'] = (bool) ($info['wavpack']['blockheader']['flags_raw'] & 0x00000800);
142  $info['wavpack']['blockheader']['flags']['multichannel_final'] = (bool) ($info['wavpack']['blockheader']['flags_raw'] & 0x00001000);
143 
144  $info['audio']['lossless'] = !$info['wavpack']['blockheader']['flags']['hybrid'];
145  }
146 
147  while (!feof($this->getid3->fp) && ($this->ftell() < ($blockheader_offset + $blockheader_size + 8))) {
148 
149  $metablock = array('offset'=>$this->ftell());
150  $metablockheader = $this->fread(2);
151  if (feof($this->getid3->fp)) {
152  break;
153  }
154  $metablock['id'] = ord($metablockheader{0});
155  $metablock['function_id'] = ($metablock['id'] & 0x3F);
156  $metablock['function_name'] = $this->WavPackMetablockNameLookup($metablock['function_id']);
157 
158  // The 0x20 bit in the id of the meta subblocks (which is defined as
159  // ID_OPTIONAL_DATA) is a permanent part of the id. The idea is that
160  // if a decoder encounters an id that it does not know about, it uses
161  // that "ID_OPTIONAL_DATA" flag to determine what to do. If it is set
162  // then the decoder simply ignores the metadata, but if it is zero
163  // then the decoder should quit because it means that an understanding
164  // of the metadata is required to correctly decode the audio.
165  $metablock['non_decoder'] = (bool) ($metablock['id'] & 0x20);
166 
167  $metablock['padded_data'] = (bool) ($metablock['id'] & 0x40);
168  $metablock['large_block'] = (bool) ($metablock['id'] & 0x80);
169  if ($metablock['large_block']) {
170  $metablockheader .= $this->fread(2);
171  }
172  $metablock['size'] = getid3_lib::LittleEndian2Int(substr($metablockheader, 1)) * 2; // size is stored in words
173  $metablock['data'] = null;
174 
175  if ($metablock['size'] > 0) {
176 
177  switch ($metablock['function_id']) {
178  case 0x21: // ID_RIFF_HEADER
179  case 0x22: // ID_RIFF_TRAILER
180  case 0x23: // ID_REPLAY_GAIN
181  case 0x24: // ID_CUESHEET
182  case 0x25: // ID_CONFIG_BLOCK
183  case 0x26: // ID_MD5_CHECKSUM
184  $metablock['data'] = $this->fread($metablock['size']);
185 
186  if ($metablock['padded_data']) {
187  // padded to the nearest even byte
188  $metablock['size']--;
189  $metablock['data'] = substr($metablock['data'], 0, -1);
190  }
191  break;
192 
193  case 0x00: // ID_DUMMY
194  case 0x01: // ID_ENCODER_INFO
195  case 0x02: // ID_DECORR_TERMS
196  case 0x03: // ID_DECORR_WEIGHTS
197  case 0x04: // ID_DECORR_SAMPLES
198  case 0x05: // ID_ENTROPY_VARS
199  case 0x06: // ID_HYBRID_PROFILE
200  case 0x07: // ID_SHAPING_WEIGHTS
201  case 0x08: // ID_FLOAT_INFO
202  case 0x09: // ID_INT32_INFO
203  case 0x0A: // ID_WV_BITSTREAM
204  case 0x0B: // ID_WVC_BITSTREAM
205  case 0x0C: // ID_WVX_BITSTREAM
206  case 0x0D: // ID_CHANNEL_INFO
207  $this->fseek($metablock['offset'] + ($metablock['large_block'] ? 4 : 2) + $metablock['size']);
208  break;
209 
210  default:
211  $this->warning('Unexpected metablock type "0x'.str_pad(dechex($metablock['function_id']), 2, '0', STR_PAD_LEFT).'" at offset '.$metablock['offset']);
212  $this->fseek($metablock['offset'] + ($metablock['large_block'] ? 4 : 2) + $metablock['size']);
213  break;
214  }
215 
216  switch ($metablock['function_id']) {
217  case 0x21: // ID_RIFF_HEADER
218  getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio-video.riff.php', __FILE__, true);
219  $original_wav_filesize = getid3_lib::LittleEndian2Int(substr($metablock['data'], 4, 4));
220 
221  $getid3_temp = new getID3();
222  $getid3_temp->openfile($this->getid3->filename);
223  $getid3_riff = new getid3_riff($getid3_temp);
224  $getid3_riff->ParseRIFFdata($metablock['data']);
225  $metablock['riff'] = $getid3_temp->info['riff'];
226  $info['audio']['sample_rate'] = $getid3_temp->info['riff']['raw']['fmt ']['nSamplesPerSec'];
227  unset($getid3_riff, $getid3_temp);
228 
229  $metablock['riff']['original_filesize'] = $original_wav_filesize;
230  $info['wavpack']['riff_trailer_size'] = $original_wav_filesize - $metablock['riff']['WAVE']['data'][0]['size'] - $metablock['riff']['header_size'];
231  $info['playtime_seconds'] = $info['wavpack']['blockheader']['total_samples'] / $info['audio']['sample_rate'];
232 
233  // Safe RIFF header in case there's a RIFF footer later
234  $metablockRIFFheader = $metablock['data'];
235  break;
236 
237 
238  case 0x22: // ID_RIFF_TRAILER
239  $metablockRIFFfooter = $metablockRIFFheader.$metablock['data'];
240  getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio-video.riff.php', __FILE__, true);
241 
242  $startoffset = $metablock['offset'] + ($metablock['large_block'] ? 4 : 2);
243  $getid3_temp = new getID3();
244  $getid3_temp->openfile($this->getid3->filename);
245  $getid3_temp->info['avdataend'] = $info['avdataend'];
246  //$getid3_temp->info['fileformat'] = 'riff';
247  $getid3_riff = new getid3_riff($getid3_temp);
248  $metablock['riff'] = $getid3_riff->ParseRIFF($startoffset, $startoffset + $metablock['size']);
249 
250  if (!empty($metablock['riff']['INFO'])) {
251  getid3_riff::parseComments($metablock['riff']['INFO'], $metablock['comments']);
252  $info['tags']['riff'] = $metablock['comments'];
253  }
254  unset($getid3_temp, $getid3_riff);
255  break;
256 
257 
258  case 0x23: // ID_REPLAY_GAIN
259  $this->warning('WavPack "Replay Gain" contents not yet handled by getID3() in metablock at offset '.$metablock['offset']);
260  break;
261 
262 
263  case 0x24: // ID_CUESHEET
264  $this->warning('WavPack "Cuesheet" contents not yet handled by getID3() in metablock at offset '.$metablock['offset']);
265  break;
266 
267 
268  case 0x25: // ID_CONFIG_BLOCK
269  $metablock['flags_raw'] = getid3_lib::LittleEndian2Int(substr($metablock['data'], 0, 3));
270 
271  $metablock['flags']['adobe_mode'] = (bool) ($metablock['flags_raw'] & 0x000001); // "adobe" mode for 32-bit floats
272  $metablock['flags']['fast_flag'] = (bool) ($metablock['flags_raw'] & 0x000002); // fast mode
273  $metablock['flags']['very_fast_flag'] = (bool) ($metablock['flags_raw'] & 0x000004); // double fast
274  $metablock['flags']['high_flag'] = (bool) ($metablock['flags_raw'] & 0x000008); // high quality mode
275  $metablock['flags']['very_high_flag'] = (bool) ($metablock['flags_raw'] & 0x000010); // double high (not used yet)
276  $metablock['flags']['bitrate_kbps'] = (bool) ($metablock['flags_raw'] & 0x000020); // bitrate is kbps, not bits / sample
277  $metablock['flags']['auto_shaping'] = (bool) ($metablock['flags_raw'] & 0x000040); // automatic noise shaping
278  $metablock['flags']['shape_override'] = (bool) ($metablock['flags_raw'] & 0x000080); // shaping mode specified
279  $metablock['flags']['joint_override'] = (bool) ($metablock['flags_raw'] & 0x000100); // joint-stereo mode specified
280  $metablock['flags']['copy_time'] = (bool) ($metablock['flags_raw'] & 0x000200); // copy file-time from source
281  $metablock['flags']['create_exe'] = (bool) ($metablock['flags_raw'] & 0x000400); // create executable
282  $metablock['flags']['create_wvc'] = (bool) ($metablock['flags_raw'] & 0x000800); // create correction file
283  $metablock['flags']['optimize_wvc'] = (bool) ($metablock['flags_raw'] & 0x001000); // maximize bybrid compression
284  $metablock['flags']['quality_mode'] = (bool) ($metablock['flags_raw'] & 0x002000); // psychoacoustic quality mode
285  $metablock['flags']['raw_flag'] = (bool) ($metablock['flags_raw'] & 0x004000); // raw mode (not implemented yet)
286  $metablock['flags']['calc_noise'] = (bool) ($metablock['flags_raw'] & 0x008000); // calc noise in hybrid mode
287  $metablock['flags']['lossy_mode'] = (bool) ($metablock['flags_raw'] & 0x010000); // obsolete (for information)
288  $metablock['flags']['extra_mode'] = (bool) ($metablock['flags_raw'] & 0x020000); // extra processing mode
289  $metablock['flags']['skip_wvx'] = (bool) ($metablock['flags_raw'] & 0x040000); // no wvx stream w/ floats & big ints
290  $metablock['flags']['md5_checksum'] = (bool) ($metablock['flags_raw'] & 0x080000); // compute & store MD5 signature
291  $metablock['flags']['quiet_mode'] = (bool) ($metablock['flags_raw'] & 0x100000); // don't report progress %
292 
293  $info['wavpack']['config_flags'] = $metablock['flags'];
294 
295 
296  $info['audio']['encoder_options'] = '';
297  if ($info['wavpack']['blockheader']['flags']['hybrid']) {
298  $info['audio']['encoder_options'] .= ' -b???';
299  }
300  $info['audio']['encoder_options'] .= ($metablock['flags']['adobe_mode'] ? ' -a' : '');
301  $info['audio']['encoder_options'] .= ($metablock['flags']['optimize_wvc'] ? ' -cc' : '');
302  $info['audio']['encoder_options'] .= ($metablock['flags']['create_exe'] ? ' -e' : '');
303  $info['audio']['encoder_options'] .= ($metablock['flags']['fast_flag'] ? ' -f' : '');
304  $info['audio']['encoder_options'] .= ($metablock['flags']['joint_override'] ? ' -j?' : '');
305  $info['audio']['encoder_options'] .= ($metablock['flags']['high_flag'] ? ' -h' : '');
306  $info['audio']['encoder_options'] .= ($metablock['flags']['md5_checksum'] ? ' -m' : '');
307  $info['audio']['encoder_options'] .= ($metablock['flags']['calc_noise'] ? ' -n' : '');
308  $info['audio']['encoder_options'] .= ($metablock['flags']['shape_override'] ? ' -s?' : '');
309  $info['audio']['encoder_options'] .= ($metablock['flags']['extra_mode'] ? ' -x?' : '');
310  if (!empty($info['audio']['encoder_options'])) {
311  $info['audio']['encoder_options'] = trim($info['audio']['encoder_options']);
312  } elseif (isset($info['audio']['encoder_options'])) {
313  unset($info['audio']['encoder_options']);
314  }
315  break;
316 
317 
318  case 0x26: // ID_MD5_CHECKSUM
319  if (strlen($metablock['data']) == 16) {
320  $info['md5_data_source'] = strtolower(getid3_lib::PrintHexBytes($metablock['data'], true, false, false));
321  } else {
322  $this->warning('Expecting 16 bytes of WavPack "MD5 Checksum" in metablock at offset '.$metablock['offset'].', but found '.strlen($metablock['data']).' bytes');
323  }
324  break;
325 
326 
327  case 0x00: // ID_DUMMY
328  case 0x01: // ID_ENCODER_INFO
329  case 0x02: // ID_DECORR_TERMS
330  case 0x03: // ID_DECORR_WEIGHTS
331  case 0x04: // ID_DECORR_SAMPLES
332  case 0x05: // ID_ENTROPY_VARS
333  case 0x06: // ID_HYBRID_PROFILE
334  case 0x07: // ID_SHAPING_WEIGHTS
335  case 0x08: // ID_FLOAT_INFO
336  case 0x09: // ID_INT32_INFO
337  case 0x0A: // ID_WV_BITSTREAM
338  case 0x0B: // ID_WVC_BITSTREAM
339  case 0x0C: // ID_WVX_BITSTREAM
340  case 0x0D: // ID_CHANNEL_INFO
341  unset($metablock);
342  break;
343  }
344 
345  }
346  if (!empty($metablock)) {
347  $info['wavpack']['metablocks'][] = $metablock;
348  }
349 
350  }
351 
352  }
353 
354  $info['audio']['encoder'] = 'WavPack v'.$info['wavpack']['blockheader']['major_version'].'.'.str_pad($info['wavpack']['blockheader']['minor_version'], 2, '0', STR_PAD_LEFT);
355  $info['audio']['bits_per_sample'] = $info['wavpack']['blockheader']['flags']['bytes_per_sample'] * 8;
356  $info['audio']['channels'] = ($info['wavpack']['blockheader']['flags']['mono'] ? 1 : 2);
357 
358  if (!empty($info['playtime_seconds'])) {
359 
360  $info['audio']['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];
361 
362  } else {
363 
364  $info['audio']['dataformat'] = 'wvc';
365 
366  }
367 
368  return true;
369  }
error($text)
Definition: getid3.php:1752
warning($text)
Definition: getid3.php:1758
static LittleEndian2Int($byteword, $signed=false)
Definition: getid3.lib.php:292
static IncludeDependency($filename, $sourcefile, $DieOnFailure=false)
static PrintHexBytes($string, $hex=true, $spaces=true, $htmlencoding='UTF-8')
Definition: getid3.lib.php:18
fread($bytes)
Definition: getid3.php:1683
static parseComments(&$RIFFinfoArray, &$CommentsTargetArray)
fseek($bytes, $whence=SEEK_SET)
Definition: getid3.php:1711
$info
Definition: index.php:5
+ Here is the call graph for this function:

◆ WavPackMetablockNameLookup()

getid3_wavpack::WavPackMetablockNameLookup ( $id)

Definition at line 372 of file module.audio.wavpack.php.

References $id.

Referenced by Analyze().

372  {
373  static $WavPackMetablockNameLookup = array(
374  0x00 => 'Dummy',
375  0x01 => 'Encoder Info',
376  0x02 => 'Decorrelation Terms',
377  0x03 => 'Decorrelation Weights',
378  0x04 => 'Decorrelation Samples',
379  0x05 => 'Entropy Variables',
380  0x06 => 'Hybrid Profile',
381  0x07 => 'Shaping Weights',
382  0x08 => 'Float Info',
383  0x09 => 'Int32 Info',
384  0x0A => 'WV Bitstream',
385  0x0B => 'WVC Bitstream',
386  0x0C => 'WVX Bitstream',
387  0x0D => 'Channel Info',
388  0x21 => 'RIFF header',
389  0x22 => 'RIFF trailer',
390  0x23 => 'Replay Gain',
391  0x24 => 'Cuesheet',
392  0x25 => 'Config Block',
393  0x26 => 'MD5 Checksum',
394  );
395  return (isset($WavPackMetablockNameLookup[$id]) ? $WavPackMetablockNameLookup[$id] : '');
396  }
if(!array_key_exists('StateId', $_REQUEST)) $id
+ Here is the caller graph for this function:

The documentation for this class was generated from the following file: