module.audio.voc.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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.voc.php //
  12. // module for analyzing Creative VOC Audio files //
  13. // dependencies: NONE //
  14. // ///
  15. /////////////////////////////////////////////////////////////////
  16. class getid3_voc extends getid3_handler
  17. {
  18. public function Analyze() {
  19. $info = &$this->getid3->info;
  20. $OriginalAVdataOffset = $info['avdataoffset'];
  21. $this->fseek($info['avdataoffset']);
  22. $VOCheader = $this->fread(26);
  23. $magic = 'Creative Voice File';
  24. if (substr($VOCheader, 0, 19) != $magic) {
  25. $info['error'][] = 'Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes(substr($VOCheader, 0, 19)).'"';
  26. return false;
  27. }
  28. // shortcuts
  29. $thisfile_audio = &$info['audio'];
  30. $info['voc'] = array();
  31. $thisfile_voc = &$info['voc'];
  32. $info['fileformat'] = 'voc';
  33. $thisfile_audio['dataformat'] = 'voc';
  34. $thisfile_audio['bitrate_mode'] = 'cbr';
  35. $thisfile_audio['lossless'] = true;
  36. $thisfile_audio['channels'] = 1; // might be overriden below
  37. $thisfile_audio['bits_per_sample'] = 8; // might be overriden below
  38. // byte # Description
  39. // ------ ------------------------------------------
  40. // 00-12 'Creative Voice File'
  41. // 13 1A (eof to abort printing of file)
  42. // 14-15 Offset of first datablock in .voc file (std 1A 00 in Intel Notation)
  43. // 16-17 Version number (minor,major) (VOC-HDR puts 0A 01)
  44. // 18-19 2's Comp of Ver. # + 1234h (VOC-HDR puts 29 11)
  45. $thisfile_voc['header']['datablock_offset'] = getid3_lib::LittleEndian2Int(substr($VOCheader, 20, 2));
  46. $thisfile_voc['header']['minor_version'] = getid3_lib::LittleEndian2Int(substr($VOCheader, 22, 1));
  47. $thisfile_voc['header']['major_version'] = getid3_lib::LittleEndian2Int(substr($VOCheader, 23, 1));
  48. do {
  49. $BlockOffset = $this->ftell();
  50. $BlockData = $this->fread(4);
  51. $BlockType = ord($BlockData{0});
  52. $BlockSize = getid3_lib::LittleEndian2Int(substr($BlockData, 1, 3));
  53. $ThisBlock = array();
  54. getid3_lib::safe_inc($thisfile_voc['blocktypes'][$BlockType], 1);
  55. switch ($BlockType) {
  56. case 0: // Terminator
  57. // do nothing, we'll break out of the loop down below
  58. break;
  59. case 1: // Sound data
  60. $BlockData .= $this->fread(2);
  61. if ($info['avdataoffset'] <= $OriginalAVdataOffset) {
  62. $info['avdataoffset'] = $this->ftell();
  63. }
  64. $this->fseek($BlockSize - 2, SEEK_CUR);
  65. $ThisBlock['sample_rate_id'] = getid3_lib::LittleEndian2Int(substr($BlockData, 4, 1));
  66. $ThisBlock['compression_type'] = getid3_lib::LittleEndian2Int(substr($BlockData, 5, 1));
  67. $ThisBlock['compression_name'] = $this->VOCcompressionTypeLookup($ThisBlock['compression_type']);
  68. if ($ThisBlock['compression_type'] <= 3) {
  69. $thisfile_voc['compressed_bits_per_sample'] = getid3_lib::CastAsInt(str_replace('-bit', '', $ThisBlock['compression_name']));
  70. }
  71. // Less accurate sample_rate calculation than the Extended block (#8) data (but better than nothing if Extended Block is not available)
  72. if (empty($thisfile_audio['sample_rate'])) {
  73. // SR byte = 256 - (1000000 / sample_rate)
  74. $thisfile_audio['sample_rate'] = getid3_lib::trunc((1000000 / (256 - $ThisBlock['sample_rate_id'])) / $thisfile_audio['channels']);
  75. }
  76. break;
  77. case 2: // Sound continue
  78. case 3: // Silence
  79. case 4: // Marker
  80. case 6: // Repeat
  81. case 7: // End repeat
  82. // nothing useful, just skip
  83. $this->fseek($BlockSize, SEEK_CUR);
  84. break;
  85. case 8: // Extended
  86. $BlockData .= $this->fread(4);
  87. //00-01 Time Constant:
  88. // Mono: 65536 - (256000000 / sample_rate)
  89. // Stereo: 65536 - (256000000 / (sample_rate * 2))
  90. $ThisBlock['time_constant'] = getid3_lib::LittleEndian2Int(substr($BlockData, 4, 2));
  91. $ThisBlock['pack_method'] = getid3_lib::LittleEndian2Int(substr($BlockData, 6, 1));
  92. $ThisBlock['stereo'] = (bool) getid3_lib::LittleEndian2Int(substr($BlockData, 7, 1));
  93. $thisfile_audio['channels'] = ($ThisBlock['stereo'] ? 2 : 1);
  94. $thisfile_audio['sample_rate'] = getid3_lib::trunc((256000000 / (65536 - $ThisBlock['time_constant'])) / $thisfile_audio['channels']);
  95. break;
  96. case 9: // data block that supersedes blocks 1 and 8. Used for stereo, 16 bit
  97. $BlockData .= $this->fread(12);
  98. if ($info['avdataoffset'] <= $OriginalAVdataOffset) {
  99. $info['avdataoffset'] = $this->ftell();
  100. }
  101. $this->fseek($BlockSize - 12, SEEK_CUR);
  102. $ThisBlock['sample_rate'] = getid3_lib::LittleEndian2Int(substr($BlockData, 4, 4));
  103. $ThisBlock['bits_per_sample'] = getid3_lib::LittleEndian2Int(substr($BlockData, 8, 1));
  104. $ThisBlock['channels'] = getid3_lib::LittleEndian2Int(substr($BlockData, 9, 1));
  105. $ThisBlock['wFormat'] = getid3_lib::LittleEndian2Int(substr($BlockData, 10, 2));
  106. $ThisBlock['compression_name'] = $this->VOCwFormatLookup($ThisBlock['wFormat']);
  107. if ($this->VOCwFormatActualBitsPerSampleLookup($ThisBlock['wFormat'])) {
  108. $thisfile_voc['compressed_bits_per_sample'] = $this->VOCwFormatActualBitsPerSampleLookup($ThisBlock['wFormat']);
  109. }
  110. $thisfile_audio['sample_rate'] = $ThisBlock['sample_rate'];
  111. $thisfile_audio['bits_per_sample'] = $ThisBlock['bits_per_sample'];
  112. $thisfile_audio['channels'] = $ThisBlock['channels'];
  113. break;
  114. default:
  115. $info['warning'][] = 'Unhandled block type "'.$BlockType.'" at offset '.$BlockOffset;
  116. $this->fseek($BlockSize, SEEK_CUR);
  117. break;
  118. }
  119. if (!empty($ThisBlock)) {
  120. $ThisBlock['block_offset'] = $BlockOffset;
  121. $ThisBlock['block_size'] = $BlockSize;
  122. $ThisBlock['block_type_id'] = $BlockType;
  123. $thisfile_voc['blocks'][] = $ThisBlock;
  124. }
  125. } while (!feof($this->getid3->fp) && ($BlockType != 0));
  126. // Terminator block doesn't have size field, so seek back 3 spaces
  127. $this->fseek(-3, SEEK_CUR);
  128. ksort($thisfile_voc['blocktypes']);
  129. if (!empty($thisfile_voc['compressed_bits_per_sample'])) {
  130. $info['playtime_seconds'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / ($thisfile_voc['compressed_bits_per_sample'] * $thisfile_audio['channels'] * $thisfile_audio['sample_rate']);
  131. $thisfile_audio['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];
  132. }
  133. return true;
  134. }
  135. public function VOCcompressionTypeLookup($index) {
  136. static $VOCcompressionTypeLookup = array(
  137. 0 => '8-bit',
  138. 1 => '4-bit',
  139. 2 => '2.6-bit',
  140. 3 => '2-bit'
  141. );
  142. return (isset($VOCcompressionTypeLookup[$index]) ? $VOCcompressionTypeLookup[$index] : 'Multi DAC ('.($index - 3).') channels');
  143. }
  144. public function VOCwFormatLookup($index) {
  145. static $VOCwFormatLookup = array(
  146. 0x0000 => '8-bit unsigned PCM',
  147. 0x0001 => 'Creative 8-bit to 4-bit ADPCM',
  148. 0x0002 => 'Creative 8-bit to 3-bit ADPCM',
  149. 0x0003 => 'Creative 8-bit to 2-bit ADPCM',
  150. 0x0004 => '16-bit signed PCM',
  151. 0x0006 => 'CCITT a-Law',
  152. 0x0007 => 'CCITT u-Law',
  153. 0x2000 => 'Creative 16-bit to 4-bit ADPCM'
  154. );
  155. return (isset($VOCwFormatLookup[$index]) ? $VOCwFormatLookup[$index] : false);
  156. }
  157. public function VOCwFormatActualBitsPerSampleLookup($index) {
  158. static $VOCwFormatLookup = array(
  159. 0x0000 => 8,
  160. 0x0001 => 4,
  161. 0x0002 => 3,
  162. 0x0003 => 2,
  163. 0x0004 => 16,
  164. 0x0006 => 8,
  165. 0x0007 => 8,
  166. 0x2000 => 4
  167. );
  168. return (isset($VOCwFormatLookup[$index]) ? $VOCwFormatLookup[$index] : false);
  169. }
  170. }