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 /////////////////////////////////////////////////////////////////
11 // module.audio.voc.php //
12 // module for analyzing Creative VOC Audio files //
13 // dependencies: NONE //
15 /////////////////////////////////////////////////////////////////
18 class getid3_voc
extends getid3_handler
21 public function Analyze() {
22 $info = &$this->getid3
->info
;
24 $OriginalAVdataOffset = $info['avdataoffset'];
25 $this->fseek($info['avdataoffset']);
26 $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)).'"');
35 $thisfile_audio = &$info['audio'];
36 $info['voc'] = array();
37 $thisfile_voc = &$info['voc'];
39 $info['fileformat'] = 'voc';
40 $thisfile_audio['dataformat'] = 'voc';
41 $thisfile_audio['bitrate_mode'] = 'cbr';
42 $thisfile_audio['lossless'] = true;
43 $thisfile_audio['channels'] = 1; // might be overriden below
44 $thisfile_audio['bits_per_sample'] = 8; // might be overriden below
47 // ------ ------------------------------------------
48 // 00-12 'Creative Voice File'
49 // 13 1A (eof to abort printing of file)
50 // 14-15 Offset of first datablock in .voc file (std 1A 00 in Intel Notation)
51 // 16-17 Version number (minor,major) (VOC-HDR puts 0A 01)
52 // 18-19 2's Comp of Ver. # + 1234h (VOC-HDR puts 29 11)
54 $thisfile_voc['header']['datablock_offset'] = getid3_lib
::LittleEndian2Int(substr($VOCheader, 20, 2));
55 $thisfile_voc['header']['minor_version'] = getid3_lib
::LittleEndian2Int(substr($VOCheader, 22, 1));
56 $thisfile_voc['header']['major_version'] = getid3_lib
::LittleEndian2Int(substr($VOCheader, 23, 1));
60 $BlockOffset = $this->ftell();
61 $BlockData = $this->fread(4);
62 $BlockType = ord($BlockData{0});
63 $BlockSize = getid3_lib
::LittleEndian2Int(substr($BlockData, 1, 3));
66 getid3_lib
::safe_inc($thisfile_voc['blocktypes'][$BlockType], 1);
69 // do nothing, we'll break out of the loop down below
73 $BlockData .= $this->fread(2);
74 if ($info['avdataoffset'] <= $OriginalAVdataOffset) {
75 $info['avdataoffset'] = $this->ftell();
77 $this->fseek($BlockSize - 2, SEEK_CUR
);
79 $ThisBlock['sample_rate_id'] = getid3_lib
::LittleEndian2Int(substr($BlockData, 4, 1));
80 $ThisBlock['compression_type'] = getid3_lib
::LittleEndian2Int(substr($BlockData, 5, 1));
82 $ThisBlock['compression_name'] = $this->VOCcompressionTypeLookup($ThisBlock['compression_type']);
83 if ($ThisBlock['compression_type'] <= 3) {
84 $thisfile_voc['compressed_bits_per_sample'] = getid3_lib
::CastAsInt(str_replace('-bit', '', $ThisBlock['compression_name']));
87 // Less accurate sample_rate calculation than the Extended block (#8) data (but better than nothing if Extended Block is not available)
88 if (empty($thisfile_audio['sample_rate'])) {
89 // SR byte = 256 - (1000000 / sample_rate)
90 $thisfile_audio['sample_rate'] = getid3_lib
::trunc((1000000 / (256 - $ThisBlock['sample_rate_id'])) / $thisfile_audio['channels']);
94 case 2: // Sound continue
99 // nothing useful, just skip
100 $this->fseek($BlockSize, SEEK_CUR
);
104 $BlockData .= $this->fread(4);
106 //00-01 Time Constant:
107 // Mono: 65536 - (256000000 / sample_rate)
108 // Stereo: 65536 - (256000000 / (sample_rate * 2))
109 $ThisBlock['time_constant'] = getid3_lib
::LittleEndian2Int(substr($BlockData, 4, 2));
110 $ThisBlock['pack_method'] = getid3_lib
::LittleEndian2Int(substr($BlockData, 6, 1));
111 $ThisBlock['stereo'] = (bool) getid3_lib
::LittleEndian2Int(substr($BlockData, 7, 1));
113 $thisfile_audio['channels'] = ($ThisBlock['stereo'] ?
2 : 1);
114 $thisfile_audio['sample_rate'] = getid3_lib
::trunc((256000000 / (65536 - $ThisBlock['time_constant'])) / $thisfile_audio['channels']);
117 case 9: // data block that supersedes blocks 1 and 8. Used for stereo, 16 bit
118 $BlockData .= $this->fread(12);
119 if ($info['avdataoffset'] <= $OriginalAVdataOffset) {
120 $info['avdataoffset'] = $this->ftell();
122 $this->fseek($BlockSize - 12, SEEK_CUR
);
124 $ThisBlock['sample_rate'] = getid3_lib
::LittleEndian2Int(substr($BlockData, 4, 4));
125 $ThisBlock['bits_per_sample'] = getid3_lib
::LittleEndian2Int(substr($BlockData, 8, 1));
126 $ThisBlock['channels'] = getid3_lib
::LittleEndian2Int(substr($BlockData, 9, 1));
127 $ThisBlock['wFormat'] = getid3_lib
::LittleEndian2Int(substr($BlockData, 10, 2));
129 $ThisBlock['compression_name'] = $this->VOCwFormatLookup($ThisBlock['wFormat']);
130 if ($this->VOCwFormatActualBitsPerSampleLookup($ThisBlock['wFormat'])) {
131 $thisfile_voc['compressed_bits_per_sample'] = $this->VOCwFormatActualBitsPerSampleLookup($ThisBlock['wFormat']);
134 $thisfile_audio['sample_rate'] = $ThisBlock['sample_rate'];
135 $thisfile_audio['bits_per_sample'] = $ThisBlock['bits_per_sample'];
136 $thisfile_audio['channels'] = $ThisBlock['channels'];
140 $this->warning('Unhandled block type "'.$BlockType.'" at offset '.$BlockOffset);
141 $this->fseek($BlockSize, SEEK_CUR
);
145 if (!empty($ThisBlock)) {
146 $ThisBlock['block_offset'] = $BlockOffset;
147 $ThisBlock['block_size'] = $BlockSize;
148 $ThisBlock['block_type_id'] = $BlockType;
149 $thisfile_voc['blocks'][] = $ThisBlock;
152 } while (!feof($this->getid3
->fp
) && ($BlockType != 0));
154 // Terminator block doesn't have size field, so seek back 3 spaces
155 $this->fseek(-3, SEEK_CUR
);
157 ksort($thisfile_voc['blocktypes']);
159 if (!empty($thisfile_voc['compressed_bits_per_sample'])) {
160 $info['playtime_seconds'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / ($thisfile_voc['compressed_bits_per_sample'] * $thisfile_audio['channels'] * $thisfile_audio['sample_rate']);
161 $thisfile_audio['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];
167 public function VOCcompressionTypeLookup($index) {
168 static $VOCcompressionTypeLookup = array(
174 return (isset($VOCcompressionTypeLookup[$index]) ?
$VOCcompressionTypeLookup[$index] : 'Multi DAC ('.($index - 3).') channels');
177 public function VOCwFormatLookup($index) {
178 static $VOCwFormatLookup = array(
179 0x0000 => '8-bit unsigned PCM',
180 0x0001 => 'Creative 8-bit to 4-bit ADPCM',
181 0x0002 => 'Creative 8-bit to 3-bit ADPCM',
182 0x0003 => 'Creative 8-bit to 2-bit ADPCM',
183 0x0004 => '16-bit signed PCM',
184 0x0006 => 'CCITT a-Law',
185 0x0007 => 'CCITT u-Law',
186 0x2000 => 'Creative 16-bit to 4-bit ADPCM'
188 return (isset($VOCwFormatLookup[$index]) ?
$VOCwFormatLookup[$index] : false);
191 public function VOCwFormatActualBitsPerSampleLookup($index) {
192 static $VOCwFormatLookup = array(
202 return (isset($VOCwFormatLookup[$index]) ?
$VOCwFormatLookup[$index] : false);