[SPIP] v3.2.1-->v3.2.3
[lhc/web/www.git] / www / plugins-dist / medias / lib / getid3 / module.audio.vqf.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.vqf.php //
12 // module for analyzing VQF audio files //
13 // dependencies: NONE //
14 // ///
15 /////////////////////////////////////////////////////////////////
16
17
18 class getid3_vqf extends getid3_handler
19 {
20 /**
21 * @return bool
22 */
23 public function Analyze() {
24 $info = &$this->getid3->info;
25
26 // based loosely on code from TTwinVQ by Jurgen Faul <jfaulØgmx*de>
27 // http://jfaul.de/atl or http://j-faul.virtualave.net/atl/atl.html
28
29 $info['fileformat'] = 'vqf';
30 $info['audio']['dataformat'] = 'vqf';
31 $info['audio']['bitrate_mode'] = 'cbr';
32 $info['audio']['lossless'] = false;
33
34 // shortcut
35 $info['vqf']['raw'] = array();
36 $thisfile_vqf = &$info['vqf'];
37 $thisfile_vqf_raw = &$thisfile_vqf['raw'];
38
39 $this->fseek($info['avdataoffset']);
40 $VQFheaderData = $this->fread(16);
41
42 $offset = 0;
43 $thisfile_vqf_raw['header_tag'] = substr($VQFheaderData, $offset, 4);
44 $magic = 'TWIN';
45 if ($thisfile_vqf_raw['header_tag'] != $magic) {
46 $this->error('Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($thisfile_vqf_raw['header_tag']).'"');
47 unset($info['vqf']);
48 unset($info['fileformat']);
49 return false;
50 }
51 $offset += 4;
52 $thisfile_vqf_raw['version'] = substr($VQFheaderData, $offset, 8);
53 $offset += 8;
54 $thisfile_vqf_raw['size'] = getid3_lib::BigEndian2Int(substr($VQFheaderData, $offset, 4));
55 $offset += 4;
56
57 while ($this->ftell() < $info['avdataend']) {
58
59 $ChunkBaseOffset = $this->ftell();
60 $chunkoffset = 0;
61 $ChunkData = $this->fread(8);
62 $ChunkName = substr($ChunkData, $chunkoffset, 4);
63 if ($ChunkName == 'DATA') {
64 $info['avdataoffset'] = $ChunkBaseOffset;
65 break;
66 }
67 $chunkoffset += 4;
68 $ChunkSize = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
69 $chunkoffset += 4;
70 if ($ChunkSize > ($info['avdataend'] - $this->ftell())) {
71 $this->error('Invalid chunk size ('.$ChunkSize.') for chunk "'.$ChunkName.'" at offset '.$ChunkBaseOffset);
72 break;
73 }
74 if ($ChunkSize > 0) {
75 $ChunkData .= $this->fread($ChunkSize);
76 }
77
78 switch ($ChunkName) {
79 case 'COMM':
80 // shortcut
81 $thisfile_vqf['COMM'] = array();
82 $thisfile_vqf_COMM = &$thisfile_vqf['COMM'];
83
84 $thisfile_vqf_COMM['channel_mode'] = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
85 $chunkoffset += 4;
86 $thisfile_vqf_COMM['bitrate'] = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
87 $chunkoffset += 4;
88 $thisfile_vqf_COMM['sample_rate'] = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
89 $chunkoffset += 4;
90 $thisfile_vqf_COMM['security_level'] = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
91 $chunkoffset += 4;
92
93 $info['audio']['channels'] = $thisfile_vqf_COMM['channel_mode'] + 1;
94 $info['audio']['sample_rate'] = $this->VQFchannelFrequencyLookup($thisfile_vqf_COMM['sample_rate']);
95 $info['audio']['bitrate'] = $thisfile_vqf_COMM['bitrate'] * 1000;
96 $info['audio']['encoder_options'] = 'CBR' . ceil($info['audio']['bitrate']/1000);
97
98 if ($info['audio']['bitrate'] == 0) {
99 $this->error('Corrupt VQF file: bitrate_audio == zero');
100 return false;
101 }
102 break;
103
104 case 'NAME':
105 case 'AUTH':
106 case '(c) ':
107 case 'FILE':
108 case 'COMT':
109 case 'ALBM':
110 $thisfile_vqf['comments'][$this->VQFcommentNiceNameLookup($ChunkName)][] = trim(substr($ChunkData, 8));
111 break;
112
113 case 'DSIZ':
114 $thisfile_vqf['DSIZ'] = getid3_lib::BigEndian2Int(substr($ChunkData, 8, 4));
115 break;
116
117 default:
118 $this->warning('Unhandled chunk type "'.$ChunkName.'" at offset '.$ChunkBaseOffset);
119 break;
120 }
121 }
122
123 $info['playtime_seconds'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['audio']['bitrate'];
124
125 if (isset($thisfile_vqf['DSIZ']) && (($thisfile_vqf['DSIZ'] != ($info['avdataend'] - $info['avdataoffset'] - strlen('DATA'))))) {
126 switch ($thisfile_vqf['DSIZ']) {
127 case 0:
128 case 1:
129 $this->warning('Invalid DSIZ value "'.$thisfile_vqf['DSIZ'].'". This is known to happen with VQF files encoded by Ahead Nero, and seems to be its way of saying this is TwinVQF v'.($thisfile_vqf['DSIZ'] + 1).'.0');
130 $info['audio']['encoder'] = 'Ahead Nero';
131 break;
132
133 default:
134 $this->warning('Probable corrupted file - should be '.$thisfile_vqf['DSIZ'].' bytes, actually '.($info['avdataend'] - $info['avdataoffset'] - strlen('DATA')));
135 break;
136 }
137 }
138
139 return true;
140 }
141
142 /**
143 * @param int $frequencyid
144 *
145 * @return int
146 */
147 public function VQFchannelFrequencyLookup($frequencyid) {
148 static $VQFchannelFrequencyLookup = array(
149 11 => 11025,
150 22 => 22050,
151 44 => 44100
152 );
153 return (isset($VQFchannelFrequencyLookup[$frequencyid]) ? $VQFchannelFrequencyLookup[$frequencyid] : $frequencyid * 1000);
154 }
155
156 /**
157 * @param string $shortname
158 *
159 * @return string
160 */
161 public function VQFcommentNiceNameLookup($shortname) {
162 static $VQFcommentNiceNameLookup = array(
163 'NAME' => 'title',
164 'AUTH' => 'artist',
165 '(c) ' => 'copyright',
166 'FILE' => 'filename',
167 'COMT' => 'comment',
168 'ALBM' => 'album'
169 );
170 return (isset($VQFcommentNiceNameLookup[$shortname]) ? $VQFcommentNiceNameLookup[$shortname] : $shortname);
171 }
172
173 }