module.audio.voc.php 8.4 KB

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