module.audio.lpac.php 6.0 KB

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