module.audio.dss.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.dss.php //
  11. // module for analyzing Digital Speech Standard (DSS) 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_dss extends getid3_handler
  19. {
  20. /**
  21. * @return bool
  22. */
  23. public function Analyze() {
  24. $info = &$this->getid3->info;
  25. $this->fseek($info['avdataoffset']);
  26. $DSSheader = $this->fread(1540);
  27. if (!preg_match('#^[\\x02-\\x08]ds[s2]#', $DSSheader)) {
  28. $this->error('Expecting "[02-08] 64 73 [73|32]" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes(substr($DSSheader, 0, 4)).'"');
  29. return false;
  30. }
  31. // some structure information taken from http://cpansearch.perl.org/src/RGIBSON/Audio-DSS-0.02/lib/Audio/DSS.pm
  32. $info['encoding'] = 'ISO-8859-1'; // not certain, but assumed
  33. $info['dss'] = array();
  34. $info['fileformat'] = 'dss';
  35. $info['mime_type'] = 'audio/x-'.substr($DSSheader, 1, 3); // "audio/x-dss" or "audio/x-ds2"
  36. $info['audio']['dataformat'] = substr($DSSheader, 1, 3); // "dss" or "ds2"
  37. $info['audio']['bitrate_mode'] = 'cbr';
  38. $info['dss']['version'] = ord(substr($DSSheader, 0, 1));
  39. $info['dss']['hardware'] = trim(substr($DSSheader, 12, 16)); // identification string for hardware used to create the file, e.g. "DPM 9600", "DS2400"
  40. $info['dss']['unknown1'] = getid3_lib::LittleEndian2Int(substr($DSSheader, 28, 4));
  41. // 32-37 = "FE FF FE FF F7 FF" in all the sample files I've seen
  42. $info['dss']['date_create_unix'] = $this->DSSdateStringToUnixDate(substr($DSSheader, 38, 12));
  43. $info['dss']['date_complete_unix'] = $this->DSSdateStringToUnixDate(substr($DSSheader, 50, 12));
  44. $info['dss']['playtime_sec'] = ((int) substr($DSSheader, 62, 2) * 3600) + ((int) substr($DSSheader, 64, 2) * 60) + (int) substr($DSSheader, 66, 2); // approximate file playtime in HHMMSS
  45. if ($info['dss']['version'] <= 3) {
  46. $info['dss']['playtime_ms'] = getid3_lib::LittleEndian2Int(substr($DSSheader, 512, 4)); // exact file playtime in milliseconds. Has also been observed at offset 530 in one sample file, with something else (unknown) at offset 512
  47. $info['dss']['priority'] = ord(substr($DSSheader, 793, 1));
  48. $info['dss']['comments'] = trim(substr($DSSheader, 798, 100));
  49. $info['dss']['sample_rate_index'] = ord(substr($DSSheader, 1538, 1)); // this isn't certain, this may or may not be where the sample rate info is stored, but it seems consistent on my small selection of sample files
  50. $info['audio']['sample_rate'] = $this->DSSsampleRateLookup($info['dss']['sample_rate_index']);
  51. } else {
  52. $this->getid3->warning('DSS above version 3 not fully supported in this version of getID3. Any additional documentation or format specifications would be welcome. This file is version '.$info['dss']['version']);
  53. }
  54. $info['audio']['bits_per_sample'] = 16; // maybe, maybe not -- most compressed audio formats don't have a fixed bits-per-sample value, but this is a reasonable approximation
  55. $info['audio']['channels'] = 1;
  56. if (!empty($info['dss']['playtime_ms']) && (floor($info['dss']['playtime_ms'] / 1000) == $info['dss']['playtime_sec'])) { // *should* just be playtime_ms / 1000 but at least one sample file has playtime_ms at offset 530 instead of offset 512, so safety check
  57. $info['playtime_seconds'] = $info['dss']['playtime_ms'] / 1000;
  58. } else {
  59. $info['playtime_seconds'] = $info['dss']['playtime_sec'];
  60. if (!empty($info['dss']['playtime_ms'])) {
  61. $this->getid3->warning('playtime_ms ('.number_format($info['dss']['playtime_ms'] / 1000, 3).') does not match playtime_sec ('.number_format($info['dss']['playtime_sec']).') - using playtime_sec value');
  62. }
  63. }
  64. $info['audio']['bitrate'] = ($info['filesize'] * 8) / $info['playtime_seconds'];
  65. return true;
  66. }
  67. /**
  68. * @param string $datestring
  69. *
  70. * @return int|false
  71. */
  72. public function DSSdateStringToUnixDate($datestring) {
  73. $y = (int) substr($datestring, 0, 2);
  74. $m = substr($datestring, 2, 2);
  75. $d = substr($datestring, 4, 2);
  76. $h = substr($datestring, 6, 2);
  77. $i = substr($datestring, 8, 2);
  78. $s = substr($datestring, 10, 2);
  79. $y += (($y < 95) ? 2000 : 1900);
  80. return mktime($h, $i, $s, $m, $d, $y);
  81. }
  82. /**
  83. * @param int $sample_rate_index
  84. *
  85. * @return int|false
  86. */
  87. public function DSSsampleRateLookup($sample_rate_index) {
  88. static $dssSampleRateLookup = array(
  89. 0x0A => 16000,
  90. 0x0C => 11025,
  91. 0x0D => 12000,
  92. 0x15 => 8000,
  93. );
  94. if (!array_key_exists($sample_rate_index, $dssSampleRateLookup)) {
  95. $this->getid3->warning('unknown sample_rate_index: 0x'.strtoupper(dechex($sample_rate_index)));
  96. return false;
  97. }
  98. return $dssSampleRateLookup[$sample_rate_index];
  99. }
  100. }