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