PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/getid3/module.audio.voc.php

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