Blame view

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