module.audio.lpac.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. /////////////////////////////////////////////////////////////////
  7. // See readme.txt for more details //
  8. /////////////////////////////////////////////////////////////////
  9. // //
  10. // module.audio.lpac.php //
  11. // module for analyzing LPAC Audio files //
  12. // dependencies: module.audio-video.riff.php //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio-video.riff.php', __FILE__, true);
  16. class getid3_lpac extends getid3_handler
  17. {
  18. function Analyze() {
  19. $info = &$this->getid3->info;
  20. fseek($this->getid3->fp, $info['avdataoffset'], SEEK_SET);
  21. $LPACheader = fread($this->getid3->fp, 14);
  22. if (substr($LPACheader, 0, 4) != 'LPAC') {
  23. $info['error'][] = 'Expected "LPAC" at offset '.$info['avdataoffset'].', found "'.$StreamMarker.'"';
  24. return false;
  25. }
  26. $info['avdataoffset'] += 14;
  27. $info['fileformat'] = 'lpac';
  28. $info['audio']['dataformat'] = 'lpac';
  29. $info['audio']['lossless'] = true;
  30. $info['audio']['bitrate_mode'] = 'vbr';
  31. $info['lpac']['file_version'] = getid3_lib::BigEndian2Int(substr($LPACheader, 4, 1));
  32. $flags['audio_type'] = getid3_lib::BigEndian2Int(substr($LPACheader, 5, 1));
  33. $info['lpac']['total_samples']= getid3_lib::BigEndian2Int(substr($LPACheader, 6, 4));
  34. $flags['parameters'] = getid3_lib::BigEndian2Int(substr($LPACheader, 10, 4));
  35. $info['lpac']['flags']['is_wave'] = (bool) ($flags['audio_type'] & 0x40);
  36. $info['lpac']['flags']['stereo'] = (bool) ($flags['audio_type'] & 0x04);
  37. $info['lpac']['flags']['24_bit'] = (bool) ($flags['audio_type'] & 0x02);
  38. $info['lpac']['flags']['16_bit'] = (bool) ($flags['audio_type'] & 0x01);
  39. if ($info['lpac']['flags']['24_bit'] && $info['lpac']['flags']['16_bit']) {
  40. $info['warning'][] = '24-bit and 16-bit flags cannot both be set';
  41. }
  42. $info['lpac']['flags']['fast_compress'] = (bool) ($flags['parameters'] & 0x40000000);
  43. $info['lpac']['flags']['random_access'] = (bool) ($flags['parameters'] & 0x08000000);
  44. $info['lpac']['block_length'] = pow(2, (($flags['parameters'] & 0x07000000) >> 24)) * 256;
  45. $info['lpac']['flags']['adaptive_prediction_order'] = (bool) ($flags['parameters'] & 0x00800000);
  46. $info['lpac']['flags']['adaptive_quantization'] = (bool) ($flags['parameters'] & 0x00400000);
  47. $info['lpac']['flags']['joint_stereo'] = (bool) ($flags['parameters'] & 0x00040000);
  48. $info['lpac']['quantization'] = ($flags['parameters'] & 0x00001F00) >> 8;
  49. $info['lpac']['max_prediction_order'] = ($flags['parameters'] & 0x0000003F);
  50. if ($info['lpac']['flags']['fast_compress'] && ($info['lpac']['max_prediction_order'] != 3)) {
  51. $info['warning'][] = 'max_prediction_order expected to be "3" if fast_compress is true, actual value is "'.$info['lpac']['max_prediction_order'].'"';
  52. }
  53. switch ($info['lpac']['file_version']) {
  54. case 6:
  55. if ($info['lpac']['flags']['adaptive_quantization']) {
  56. $info['warning'][] = 'adaptive_quantization expected to be false in LPAC file stucture v6, actually true';
  57. }
  58. if ($info['lpac']['quantization'] != 20) {
  59. $info['warning'][] = 'Quantization expected to be 20 in LPAC file stucture v6, actually '.$info['lpac']['flags']['Q'];
  60. }
  61. break;
  62. default:
  63. //$info['warning'][] = 'This version of getID3() ['.$this->getid3->version().'] only supports LPAC file format version 6, this file is version '.$info['lpac']['file_version'].' - please report to info@getid3.org';
  64. break;
  65. }
  66. $getid3_temp = new getID3();
  67. $getid3_temp->openfile($this->getid3->filename);
  68. $getid3_temp->info = $info;
  69. $getid3_riff = new getid3_riff($getid3_temp);
  70. $getid3_riff->Analyze();
  71. $info['avdataoffset'] = $getid3_temp->info['avdataoffset'];
  72. $info['riff'] = $getid3_temp->info['riff'];
  73. $info['error'] = $getid3_temp->info['error'];
  74. $info['warning'] = $getid3_temp->info['warning'];
  75. $info['lpac']['comments']['comment'] = $getid3_temp->info['comments'];
  76. $info['audio']['sample_rate'] = $getid3_temp->info['audio']['sample_rate'];
  77. unset($getid3_temp, $getid3_riff);
  78. $info['audio']['channels'] = ($info['lpac']['flags']['stereo'] ? 2 : 1);
  79. if ($info['lpac']['flags']['24_bit']) {
  80. $info['audio']['bits_per_sample'] = $info['riff']['audio'][0]['bits_per_sample'];
  81. } elseif ($info['lpac']['flags']['16_bit']) {
  82. $info['audio']['bits_per_sample'] = 16;
  83. } else {
  84. $info['audio']['bits_per_sample'] = 8;
  85. }
  86. if ($info['lpac']['flags']['fast_compress']) {
  87. // fast
  88. $info['audio']['encoder_options'] = '-1';
  89. } else {
  90. switch ($info['lpac']['max_prediction_order']) {
  91. case 20: // simple
  92. $info['audio']['encoder_options'] = '-2';
  93. break;
  94. case 30: // medium
  95. $info['audio']['encoder_options'] = '-3';
  96. break;
  97. case 40: // high
  98. $info['audio']['encoder_options'] = '-4';
  99. break;
  100. case 60: // extrahigh
  101. $info['audio']['encoder_options'] = '-5';
  102. break;
  103. }
  104. }
  105. $info['playtime_seconds'] = $info['lpac']['total_samples'] / $info['audio']['sample_rate'];
  106. $info['audio']['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];
  107. return true;
  108. }
  109. }
  110. ?>