module.audio.dsf.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.dsf.php //
  12. // module for analyzing dsf/DSF Audio files //
  13. // dependencies: module.tag.id3v2.php //
  14. // ///
  15. /////////////////////////////////////////////////////////////////
  16. getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.tag.id3v2.php', __FILE__, true);
  17. class getid3_dsf extends getid3_handler
  18. {
  19. public function Analyze() {
  20. $info = &$this->getid3->info;
  21. $info['fileformat'] = 'dsf';
  22. $info['audio']['dataformat'] = 'dsf';
  23. $info['audio']['lossless'] = true;
  24. $info['audio']['bitrate_mode'] = 'cbr';
  25. $this->fseek($info['avdataoffset']);
  26. $dsfheader = $this->fread(28 + 12);
  27. $headeroffset = 0;
  28. $info['dsf']['dsd']['magic'] = substr($dsfheader, $headeroffset, 4);
  29. $headeroffset += 4;
  30. $magic = 'DSD ';
  31. if ($info['dsf']['dsd']['magic'] != $magic) {
  32. $this->error('Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($info['dsf']['dsd']['magic']).'"');
  33. unset($info['fileformat']);
  34. unset($info['audio']);
  35. unset($info['dsf']);
  36. return false;
  37. }
  38. $info['dsf']['dsd']['dsd_chunk_size'] = getid3_lib::LittleEndian2Int(substr($dsfheader, $headeroffset, 8)); // should be 28
  39. $headeroffset += 8;
  40. $info['dsf']['dsd']['dsf_file_size'] = getid3_lib::LittleEndian2Int(substr($dsfheader, $headeroffset, 8));
  41. $headeroffset += 8;
  42. $info['dsf']['dsd']['meta_chunk_offset'] = getid3_lib::LittleEndian2Int(substr($dsfheader, $headeroffset, 8));
  43. $headeroffset += 8;
  44. $info['dsf']['fmt']['magic'] = substr($dsfheader, $headeroffset, 4);
  45. $headeroffset += 4;
  46. $magic = 'fmt ';
  47. if ($info['dsf']['fmt']['magic'] != $magic) {
  48. $this->error('Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$headeroffset.', found "'.getid3_lib::PrintHexBytes($info['dsf']['fmt']['magic']).'"');
  49. return false;
  50. }
  51. $info['dsf']['fmt']['fmt_chunk_size'] = getid3_lib::LittleEndian2Int(substr($dsfheader, $headeroffset, 8)); // usually 52 bytes
  52. $headeroffset += 8;
  53. $dsfheader .= $this->fread($info['dsf']['fmt']['fmt_chunk_size'] - 12 + 12); // we have already read the entire DSD chunk, plus 12 bytes of FMT. We now want to read the size of FMT, plus 12 bytes into the next chunk to get magic and size.
  54. if (strlen($dsfheader) != ($info['dsf']['dsd']['dsd_chunk_size'] + $info['dsf']['fmt']['fmt_chunk_size'] + 12)) {
  55. $this->error('Expecting '.($info['dsf']['dsd']['dsd_chunk_size'] + $info['dsf']['fmt']['fmt_chunk_size']).' bytes header, found '.strlen($dsfheader).' bytes');
  56. return false;
  57. }
  58. $info['dsf']['fmt']['format_version'] = getid3_lib::LittleEndian2Int(substr($dsfheader, $headeroffset, 4)); // usually "1"
  59. $headeroffset += 4;
  60. $info['dsf']['fmt']['format_id'] = getid3_lib::LittleEndian2Int(substr($dsfheader, $headeroffset, 4)); // usually "0" = "DSD Raw"
  61. $headeroffset += 4;
  62. $info['dsf']['fmt']['channel_type_id'] = getid3_lib::LittleEndian2Int(substr($dsfheader, $headeroffset, 4));
  63. $headeroffset += 4;
  64. $info['dsf']['fmt']['channels'] = getid3_lib::LittleEndian2Int(substr($dsfheader, $headeroffset, 4));
  65. $headeroffset += 4;
  66. $info['dsf']['fmt']['sample_rate'] = getid3_lib::LittleEndian2Int(substr($dsfheader, $headeroffset, 4));
  67. $headeroffset += 4;
  68. $info['dsf']['fmt']['bits_per_sample'] = getid3_lib::LittleEndian2Int(substr($dsfheader, $headeroffset, 4));
  69. $headeroffset += 4;
  70. $info['dsf']['fmt']['sample_count'] = getid3_lib::LittleEndian2Int(substr($dsfheader, $headeroffset, 8));
  71. $headeroffset += 8;
  72. $info['dsf']['fmt']['channel_block_size'] = getid3_lib::LittleEndian2Int(substr($dsfheader, $headeroffset, 4));
  73. $headeroffset += 4;
  74. $info['dsf']['fmt']['reserved'] = getid3_lib::LittleEndian2Int(substr($dsfheader, $headeroffset, 4)); // zero-filled
  75. $headeroffset += 4;
  76. $info['dsf']['data']['magic'] = substr($dsfheader, $headeroffset, 4);
  77. $headeroffset += 4;
  78. $magic = 'data';
  79. if ($info['dsf']['data']['magic'] != $magic) {
  80. $this->error('Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$headeroffset.', found "'.getid3_lib::PrintHexBytes($info['dsf']['data']['magic']).'"');
  81. return false;
  82. }
  83. $info['dsf']['data']['data_chunk_size'] = getid3_lib::LittleEndian2Int(substr($dsfheader, $headeroffset, 8));
  84. $headeroffset += 8;
  85. $info['avdataoffset'] = $headeroffset;
  86. $info['avdataend'] = $info['avdataoffset'] + $info['dsf']['data']['data_chunk_size'];
  87. if ($info['dsf']['dsd']['meta_chunk_offset'] > 0) {
  88. $getid3_id3v2 = new getid3_id3v2($this->getid3);
  89. $getid3_id3v2->StartingOffset = $info['dsf']['dsd']['meta_chunk_offset'];
  90. $getid3_id3v2->Analyze();
  91. unset($getid3_id3v2);
  92. }
  93. $info['dsf']['fmt']['channel_type'] = $this->DSFchannelTypeLookup($info['dsf']['fmt']['channel_type_id']);
  94. $info['audio']['channelmode'] = $info['dsf']['fmt']['channel_type'];
  95. $info['audio']['bits_per_sample'] = $info['dsf']['fmt']['bits_per_sample'];
  96. $info['audio']['sample_rate'] = $info['dsf']['fmt']['sample_rate'];
  97. $info['audio']['channels'] = $info['dsf']['fmt']['channels'];
  98. $info['audio']['bitrate'] = $info['audio']['bits_per_sample'] * $info['audio']['sample_rate'] * $info['audio']['channels'];
  99. $info['playtime_seconds'] = ($info['dsf']['data']['data_chunk_size'] * 8) / $info['audio']['bitrate'];
  100. return true;
  101. }
  102. public static function DSFchannelTypeLookup($channel_type_id) {
  103. static $DSFchannelTypeLookup = array(
  104. // interleaving order:
  105. 1 => 'mono', // 1: Mono
  106. 2 => 'stereo', // 1: Front-Left; 2: Front-Right
  107. 3 => '3-channel', // 1: Front-Left; 2: Front-Right; 3: Center
  108. 4 => 'quad', // 1: Front-Left; 2: Front-Right; 3: Back-Left; 4: Back-Right
  109. 5 => '4-channel', // 1: Front-Left; 2: Front-Right; 3: Center; 4: Low-Frequency
  110. 6 => '5-channel', // 1: Front-Left; 2: Front-Right; 3: Center; 4: Back-Left 5: Back-Right
  111. 7 => '5.1', // 1: Front-Left; 2: Front-Right; 3: Center; 4: Low-Frequency; 5: Back-Left; 6: Back-Right
  112. );
  113. return (isset($DSFchannelTypeLookup[$channel_type_id]) ? $DSFchannelTypeLookup[$channel_type_id] : '');
  114. }
  115. }