module.audio.lpac.php 5.8 KB

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