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