PageRenderTime 23ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/getid3/module.audio.voc.php

http://pumukit.googlecode.com/
PHP | 240 lines | 153 code | 50 blank | 37 comment | 11 complexity | 209d66b1defcb2396b83243162390429 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP version 5 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 2002-2006 James Heinrich, Allan Hansen |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2 of the GPL license, |
  8. // | that is bundled with this package in the file license.txt and is |
  9. // | available through the world-wide-web at the following url: |
  10. // | http://www.gnu.org/copyleft/gpl.html |
  11. // +----------------------------------------------------------------------+
  12. // | getID3() - http://getid3.sourceforge.net or http://www.getid3.org |
  13. // +----------------------------------------------------------------------+
  14. // | Authors: James Heinrich <info?getid3*org> |
  15. // | Allan Hansen <ah?artemis*dk> |
  16. // +----------------------------------------------------------------------+
  17. // | module.audio.voc.php |
  18. // | Module for analyzing Creative VOC Audio files. |
  19. // | dependencies: NONE |
  20. // +----------------------------------------------------------------------+
  21. //
  22. // $Id: module.audio.voc.php,v 1.3 2006/11/02 10:48:02 ah Exp $
  23. class getid3_voc extends getid3_handler
  24. {
  25. public function Analyze() {
  26. $getid3 = $this->getid3;
  27. $original_av_data_offset = $getid3->info['avdataoffset'];
  28. fseek($getid3->fp, $getid3->info['avdataoffset'], SEEK_SET);
  29. $voc_header= fread($getid3->fp, 26);
  30. // Magic bytes: 'Creative Voice File'
  31. $info_audio = &$getid3->info['audio'];
  32. $getid3->info['voc'] = array ();
  33. $info_voc = &$getid3->info['voc'];
  34. $getid3->info['fileformat'] = 'voc';
  35. $info_audio['dataformat'] = 'voc';
  36. $info_audio['bitrate_mode'] = 'cbr';
  37. $info_audio['lossless'] = true;
  38. $info_audio['channels'] = 1; // might be overriden below
  39. $info_audio['bits_per_sample'] = 8; // might be overriden below
  40. // byte # Description
  41. // ------ ------------------------------------------
  42. // 00-12 'Creative Voice File'
  43. // 13 1A (eof to abort printing of file)
  44. // 14-15 Offset of first datablock in .voc file (std 1A 00 in Intel Notation)
  45. // 16-17 Version number (minor,major) (VOC-HDR puts 0A 01)
  46. // 18-19 2's Comp of Ver. # + 1234h (VOC-HDR puts 29 11)
  47. getid3_lib::ReadSequence('LittleEndian2Int', $info_voc['header'], $voc_header, 20,
  48. array (
  49. 'datablock_offset' => 2,
  50. 'minor_version' => 1,
  51. 'major_version' => 1
  52. )
  53. );
  54. do {
  55. $block_offset = ftell($getid3->fp);
  56. $block_data = fread($getid3->fp, 4);
  57. $block_type = ord($block_data{0});
  58. $block_size = getid3_lib::LittleEndian2Int(substr($block_data, 1, 3));
  59. $this_block = array ();
  60. @$info_voc['blocktypes'][$block_type]++;
  61. switch ($block_type) {
  62. case 0: // Terminator
  63. // do nothing, we'll break out of the loop down below
  64. break;
  65. case 1: // Sound data
  66. $block_data .= fread($getid3->fp, 2);
  67. if ($getid3->info['avdataoffset'] <= $original_av_data_offset) {
  68. $getid3->info['avdataoffset'] = ftell($getid3->fp);
  69. }
  70. fseek($getid3->fp, $block_size - 2, SEEK_CUR);
  71. getid3_lib::ReadSequence('LittleEndian2Int', $this_block, $block_data, 4,
  72. array (
  73. 'sample_rate_id' => 1,
  74. 'compression_type' => 1
  75. )
  76. );
  77. $this_block['compression_name'] = getid3_voc::VOCcompressionTypeLookup($this_block['compression_type']);
  78. if ($this_block['compression_type'] <= 3) {
  79. $info_voc['compressed_bits_per_sample'] = (int)(str_replace('-bit', '', $this_block['compression_name']));
  80. }
  81. // Less accurate sample_rate calculation than the Extended block (#8) data (but better than nothing if Extended Block is not available)
  82. if (empty($info_audio['sample_rate'])) {
  83. // SR byte = 256 - (1000000 / sample_rate)
  84. $info_audio['sample_rate'] = (int)floor((1000000 / (256 - $this_block['sample_rate_id'])) / $info_audio['channels']);
  85. }
  86. break;
  87. case 2: // Sound continue
  88. case 3: // Silence
  89. case 4: // Marker
  90. case 6: // Repeat
  91. case 7: // End repeat
  92. // nothing useful, just skip
  93. fseek($getid3->fp, $block_size, SEEK_CUR);
  94. break;
  95. case 8: // Extended
  96. $block_data .= fread($getid3->fp, 4);
  97. //00-01 Time Constant:
  98. // Mono: 65536 - (256000000 / sample_rate)
  99. // Stereo: 65536 - (256000000 / (sample_rate * 2))
  100. getid3_lib::ReadSequence('LittleEndian2Int', $this_block, $block_data, 4,
  101. array (
  102. 'time_constant' => 2,
  103. 'pack_method' => 1,
  104. 'stereo' => 1
  105. )
  106. );
  107. $this_block['stereo'] = (bool)$this_block['stereo'];
  108. $info_audio['channels'] = ($this_block['stereo'] ? 2 : 1);
  109. $info_audio['sample_rate'] = (int)floor((256000000 / (65536 - $this_block['time_constant'])) / $info_audio['channels']);
  110. break;
  111. case 9: // data block that supersedes blocks 1 and 8. Used for stereo, 16 bit
  112. $block_data .= fread($getid3->fp, 12);
  113. if ($getid3->info['avdataoffset'] <= $original_av_data_offset) {
  114. $getid3->info['avdataoffset'] = ftell($getid3->fp);
  115. }
  116. fseek($getid3->fp, $block_size - 12, SEEK_CUR);
  117. getid3_lib::ReadSequence('LittleEndian2Int', $this_block, $block_data, 4,
  118. array (
  119. 'sample_rate' => 4,
  120. 'bits_per_sample' => 1,
  121. 'channels' => 1,
  122. 'wFormat' => 2
  123. )
  124. );
  125. $this_block['compression_name'] = getid3_voc::VOCwFormatLookup($this_block['wFormat']);
  126. if (getid3_voc::VOCwFormatActualBitsPerSampleLookup($this_block['wFormat'])) {
  127. $info_voc['compressed_bits_per_sample'] = getid3_voc::VOCwFormatActualBitsPerSampleLookup($this_block['wFormat']);
  128. }
  129. $info_audio['sample_rate'] = $this_block['sample_rate'];
  130. $info_audio['bits_per_sample'] = $this_block['bits_per_sample'];
  131. $info_audio['channels'] = $this_block['channels'];
  132. break;
  133. default:
  134. $getid3->warning('Unhandled block type "'.$block_type.'" at offset '.$block_offset);
  135. fseek($getid3->fp, $block_size, SEEK_CUR);
  136. break;
  137. }
  138. if (!empty($this_block)) {
  139. $this_block['block_offset'] = $block_offset;
  140. $this_block['block_size'] = $block_size;
  141. $this_block['block_type_id'] = $block_type;
  142. $info_voc['blocks'][] = $this_block;
  143. }
  144. } while (!feof($getid3->fp) && ($block_type != 0));
  145. // Terminator block doesn't have size field, so seek back 3 spaces
  146. fseek($getid3->fp, -3, SEEK_CUR);
  147. ksort($info_voc['blocktypes']);
  148. if (!empty($info_voc['compressed_bits_per_sample'])) {
  149. $getid3->info['playtime_seconds'] = (($getid3->info['avdataend'] - $getid3->info['avdataoffset']) * 8) / ($info_voc['compressed_bits_per_sample'] * $info_audio['channels'] * $info_audio['sample_rate']);
  150. $info_audio['bitrate'] = (($getid3->info['avdataend'] - $getid3->info['avdataoffset']) * 8) / $getid3->info['playtime_seconds'];
  151. }
  152. return true;
  153. }
  154. public static function VOCcompressionTypeLookup($index) {
  155. static $lookup = array (
  156. 0 => '8-bit',
  157. 1 => '4-bit',
  158. 2 => '2.6-bit',
  159. 3 => '2-bit'
  160. );
  161. return (isset($lookup[$index]) ? $lookup[$index] : 'Multi DAC ('.($index - 3).') channels');
  162. }
  163. public static function VOCwFormatLookup($index) {
  164. static $lookup = array (
  165. 0x0000 => '8-bit unsigned PCM',
  166. 0x0001 => 'Creative 8-bit to 4-bit ADPCM',
  167. 0x0002 => 'Creative 8-bit to 3-bit ADPCM',
  168. 0x0003 => 'Creative 8-bit to 2-bit ADPCM',
  169. 0x0004 => '16-bit signed PCM',
  170. 0x0006 => 'CCITT a-Law',
  171. 0x0007 => 'CCITT u-Law',
  172. 0x2000 => 'Creative 16-bit to 4-bit ADPCM'
  173. );
  174. return (isset($lookup[$index]) ? $lookup[$index] : false);
  175. }
  176. public static function VOCwFormatActualBitsPerSampleLookup($index) {
  177. static $lookup = array (
  178. 0x0000 => 8,
  179. 0x0001 => 4,
  180. 0x0002 => 3,
  181. 0x0003 => 2,
  182. 0x0004 => 16,
  183. 0x0006 => 8,
  184. 0x0007 => 8,
  185. 0x2000 => 4
  186. );
  187. return (isset($lookup[$index]) ? $lookup[$index] : false);
  188. }
  189. }
  190. ?>