module.audio.rkau.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at https://github.com/JamesHeinrich/getID3 //
  5. // or https://www.getid3.org //
  6. // or http://getid3.sourceforge.net //
  7. // see readme.txt for more details //
  8. /////////////////////////////////////////////////////////////////
  9. // //
  10. // module.audio.shorten.php //
  11. // module for analyzing Shorten Audio files //
  12. // dependencies: NONE //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
  16. exit;
  17. }
  18. class getid3_rkau extends getid3_handler
  19. {
  20. /**
  21. * @return bool
  22. */
  23. public function Analyze() {
  24. $info = &$this->getid3->info;
  25. $this->fseek($info['avdataoffset']);
  26. $RKAUHeader = $this->fread(20);
  27. $magic = 'RKA';
  28. if (substr($RKAUHeader, 0, 3) != $magic) {
  29. $this->error('Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes(substr($RKAUHeader, 0, 3)).'"');
  30. return false;
  31. }
  32. $info['fileformat'] = 'rkau';
  33. $info['audio']['dataformat'] = 'rkau';
  34. $info['audio']['bitrate_mode'] = 'vbr';
  35. $info['rkau']['raw']['version'] = getid3_lib::LittleEndian2Int(substr($RKAUHeader, 3, 1));
  36. $info['rkau']['version'] = '1.'.str_pad($info['rkau']['raw']['version'] & 0x0F, 2, '0', STR_PAD_LEFT);
  37. if (($info['rkau']['version'] > 1.07) || ($info['rkau']['version'] < 1.06)) {
  38. $this->error('This version of getID3() ['.$this->getid3->version().'] can only parse RKAU files v1.06 and 1.07 (this file is v'.$info['rkau']['version'].')');
  39. unset($info['rkau']);
  40. return false;
  41. }
  42. $info['rkau']['source_bytes'] = getid3_lib::LittleEndian2Int(substr($RKAUHeader, 4, 4));
  43. $info['rkau']['sample_rate'] = getid3_lib::LittleEndian2Int(substr($RKAUHeader, 8, 4));
  44. $info['rkau']['channels'] = getid3_lib::LittleEndian2Int(substr($RKAUHeader, 12, 1));
  45. $info['rkau']['bits_per_sample'] = getid3_lib::LittleEndian2Int(substr($RKAUHeader, 13, 1));
  46. $info['rkau']['raw']['quality'] = getid3_lib::LittleEndian2Int(substr($RKAUHeader, 14, 1));
  47. $this->RKAUqualityLookup($info['rkau']);
  48. $info['rkau']['raw']['flags'] = getid3_lib::LittleEndian2Int(substr($RKAUHeader, 15, 1));
  49. $info['rkau']['flags']['joint_stereo'] = !($info['rkau']['raw']['flags'] & 0x01);
  50. $info['rkau']['flags']['streaming'] = (bool) ($info['rkau']['raw']['flags'] & 0x02);
  51. $info['rkau']['flags']['vrq_lossy_mode'] = (bool) ($info['rkau']['raw']['flags'] & 0x04);
  52. if ($info['rkau']['flags']['streaming']) {
  53. $info['avdataoffset'] += 20;
  54. $info['rkau']['compressed_bytes'] = getid3_lib::LittleEndian2Int(substr($RKAUHeader, 16, 4));
  55. } else {
  56. $info['avdataoffset'] += 16;
  57. $info['rkau']['compressed_bytes'] = $info['avdataend'] - $info['avdataoffset'] - 1;
  58. }
  59. // Note: compressed_bytes does not always equal what appears to be the actual number of compressed bytes,
  60. // sometimes it's more, sometimes less. No idea why(?)
  61. $info['audio']['lossless'] = $info['rkau']['lossless'];
  62. $info['audio']['channels'] = $info['rkau']['channels'];
  63. $info['audio']['bits_per_sample'] = $info['rkau']['bits_per_sample'];
  64. $info['audio']['sample_rate'] = $info['rkau']['sample_rate'];
  65. $info['playtime_seconds'] = $info['rkau']['source_bytes'] / ($info['rkau']['sample_rate'] * $info['rkau']['channels'] * ($info['rkau']['bits_per_sample'] / 8));
  66. $info['audio']['bitrate'] = ($info['rkau']['compressed_bytes'] * 8) / $info['playtime_seconds'];
  67. return true;
  68. }
  69. /**
  70. * @param array $RKAUdata
  71. *
  72. * @return bool
  73. */
  74. public function RKAUqualityLookup(&$RKAUdata) {
  75. $level = ($RKAUdata['raw']['quality'] & 0xF0) >> 4;
  76. $quality = $RKAUdata['raw']['quality'] & 0x0F;
  77. $RKAUdata['lossless'] = (($quality == 0) ? true : false);
  78. $RKAUdata['compression_level'] = $level + 1;
  79. if (!$RKAUdata['lossless']) {
  80. $RKAUdata['quality_setting'] = $quality;
  81. }
  82. return true;
  83. }
  84. }