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