[SPIP] v3.2.1-->v3.2.3
[lhc/web/www.git] / www / plugins-dist / medias / lib / getid3 / module.misc.cue.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.misc.cue.php //
12 // module for analyzing CUEsheet files //
13 // dependencies: NONE //
14 // //
15 /////////////////////////////////////////////////////////////////
16 // //
17 // Module originally written [2009-Mar-25] by //
18 // Nigel Barnes <ngbarnesØhotmail*com> //
19 // Minor reformatting and similar small changes to integrate //
20 // into getID3 by James Heinrich <info@getid3.org> //
21 // ///
22 /////////////////////////////////////////////////////////////////
23
24 /*
25 * CueSheet parser by Nigel Barnes.
26 *
27 * This is a PHP conversion of CueSharp 0.5 by Wyatt O'Day (wyday.com/cuesharp)
28 */
29
30 /**
31 * A CueSheet class used to open and parse cuesheets.
32 *
33 */
34 class getid3_cue extends getid3_handler
35 {
36 public $cuesheet = array();
37
38 /**
39 * @return bool
40 */
41 public function Analyze() {
42 $info = &$this->getid3->info;
43
44 $info['fileformat'] = 'cue';
45 $this->readCueSheetFilename($info['filenamepath']);
46 $info['cue'] = $this->cuesheet;
47 return true;
48 }
49
50 /**
51 * @param string $filename
52 *
53 * @return array
54 */
55 public function readCueSheetFilename($filename)
56 {
57 $filedata = file_get_contents($filename);
58 return $this->readCueSheet($filedata);
59 }
60
61 /**
62 * Parses a cue sheet file.
63 *
64 * @param string $filedata
65 *
66 * @return array
67 */
68 public function readCueSheet(&$filedata)
69 {
70 $cue_lines = array();
71 foreach (explode("\n", str_replace("\r", null, $filedata)) as $line)
72 {
73 if ( (strlen($line) > 0) && ($line[0] != '#'))
74 {
75 $cue_lines[] = trim($line);
76 }
77 }
78 $this->parseCueSheet($cue_lines);
79
80 return $this->cuesheet;
81 }
82
83 /**
84 * Parses the cue sheet array.
85 *
86 * @param array $file - The cuesheet as an array of each line.
87 */
88 public function parseCueSheet($file)
89 {
90 //-1 means still global, all others are track specific
91 $track_on = -1;
92
93 for ($i=0; $i < count($file); $i++)
94 {
95 list($key) = explode(' ', strtolower($file[$i]), 2);
96 switch ($key)
97 {
98 case 'catalog':
99 case 'cdtextfile':
100 case 'isrc':
101 case 'performer':
102 case 'songwriter':
103 case 'title':
104 $this->parseString($file[$i], $track_on);
105 break;
106 case 'file':
107 $currentFile = $this->parseFile($file[$i]);
108 break;
109 case 'flags':
110 $this->parseFlags($file[$i], $track_on);
111 break;
112 case 'index':
113 case 'postgap':
114 case 'pregap':
115 $this->parseIndex($file[$i], $track_on);
116 break;
117 case 'rem':
118 $this->parseComment($file[$i], $track_on);
119 break;
120 case 'track':
121 $track_on++;
122 $this->parseTrack($file[$i], $track_on);
123 if (isset($currentFile)) // if there's a file
124 {
125 $this->cuesheet['tracks'][$track_on]['datafile'] = $currentFile;
126 }
127 break;
128 default:
129 //save discarded junk and place string[] with track it was found in
130 $this->parseGarbage($file[$i], $track_on);
131 break;
132 }
133 }
134 }
135
136 /**
137 * Parses the REM command.
138 *
139 * @param string $line - The line in the cue file that contains the TRACK command.
140 * @param integer $track_on - The track currently processing.
141 */
142 public function parseComment($line, $track_on)
143 {
144 $explodedline = explode(' ', $line, 3);
145 $comment_REM = (isset($explodedline[0]) ? $explodedline[0] : '');
146 $comment_type = (isset($explodedline[1]) ? $explodedline[1] : '');
147 $comment_data = (isset($explodedline[2]) ? $explodedline[2] : '');
148 if (($comment_REM == 'REM') && $comment_type) {
149 $comment_type = strtolower($comment_type);
150 $commment_data = trim($comment_data, ' "');
151 if ($track_on != -1) {
152 $this->cuesheet['tracks'][$track_on]['comments'][$comment_type][] = $comment_data;
153 } else {
154 $this->cuesheet['comments'][$comment_type][] = $comment_data;
155 }
156 }
157 }
158
159 /**
160 * Parses the FILE command.
161 *
162 * @param string $line - The line in the cue file that contains the FILE command.
163 *
164 * @return array - Array of FILENAME and TYPE of file..
165 */
166 public function parseFile($line)
167 {
168 $line = substr($line, strpos($line, ' ') + 1);
169 $type = strtolower(substr($line, strrpos($line, ' ')));
170
171 //remove type
172 $line = substr($line, 0, strrpos($line, ' ') - 1);
173
174 //if quotes around it, remove them.
175 $line = trim($line, '"');
176
177 return array('filename'=>$line, 'type'=>$type);
178 }
179
180 /**
181 * Parses the FLAG command.
182 *
183 * @param string $line - The line in the cue file that contains the TRACK command.
184 * @param integer $track_on - The track currently processing.
185 */
186 public function parseFlags($line, $track_on)
187 {
188 if ($track_on != -1)
189 {
190 foreach (explode(' ', strtolower($line)) as $type)
191 {
192 switch ($type)
193 {
194 case 'flags':
195 // first entry in this line
196 $this->cuesheet['tracks'][$track_on]['flags'] = array(
197 '4ch' => false,
198 'data' => false,
199 'dcp' => false,
200 'pre' => false,
201 'scms' => false,
202 );
203 break;
204 case 'data':
205 case 'dcp':
206 case '4ch':
207 case 'pre':
208 case 'scms':
209 $this->cuesheet['tracks'][$track_on]['flags'][$type] = true;
210 break;
211 default:
212 break;
213 }
214 }
215 }
216 }
217
218 /**
219 * Collect any unidentified data.
220 *
221 * @param string $line - The line in the cue file that contains the TRACK command.
222 * @param integer $track_on - The track currently processing.
223 */
224 public function parseGarbage($line, $track_on)
225 {
226 if ( strlen($line) > 0 )
227 {
228 if ($track_on == -1)
229 {
230 $this->cuesheet['garbage'][] = $line;
231 }
232 else
233 {
234 $this->cuesheet['tracks'][$track_on]['garbage'][] = $line;
235 }
236 }
237 }
238
239 /**
240 * Parses the INDEX command of a TRACK.
241 *
242 * @param string $line - The line in the cue file that contains the TRACK command.
243 * @param integer $track_on - The track currently processing.
244 */
245 public function parseIndex($line, $track_on)
246 {
247 $type = strtolower(substr($line, 0, strpos($line, ' ')));
248 $line = substr($line, strpos($line, ' ') + 1);
249 $number = 0;
250
251 if ($type == 'index')
252 {
253 //read the index number
254 $number = intval(substr($line, 0, strpos($line, ' ')));
255 $line = substr($line, strpos($line, ' ') + 1);
256 }
257
258 //extract the minutes, seconds, and frames
259 $explodedline = explode(':', $line);
260 $minutes = (isset($explodedline[0]) ? $explodedline[0] : '');
261 $seconds = (isset($explodedline[1]) ? $explodedline[1] : '');
262 $frames = (isset($explodedline[2]) ? $explodedline[2] : '');
263
264 switch ($type) {
265 case 'index':
266 $this->cuesheet['tracks'][$track_on][$type][$number] = array('minutes'=>intval($minutes), 'seconds'=>intval($seconds), 'frames'=>intval($frames));
267 break;
268 case 'pregap':
269 case 'postgap':
270 $this->cuesheet['tracks'][$track_on][$type] = array('minutes'=>intval($minutes), 'seconds'=>intval($seconds), 'frames'=>intval($frames));
271 break;
272 }
273 }
274
275 /**
276 * @param string $line
277 * @param int $track_on
278 */
279 public function parseString($line, $track_on)
280 {
281 $category = strtolower(substr($line, 0, strpos($line, ' ')));
282 $line = substr($line, strpos($line, ' ') + 1);
283
284 //get rid of the quotes
285 $line = trim($line, '"');
286
287 switch ($category)
288 {
289 case 'catalog':
290 case 'cdtextfile':
291 case 'isrc':
292 case 'performer':
293 case 'songwriter':
294 case 'title':
295 if ($track_on == -1)
296 {
297 $this->cuesheet[$category] = $line;
298 }
299 else
300 {
301 $this->cuesheet['tracks'][$track_on][$category] = $line;
302 }
303 break;
304 default:
305 break;
306 }
307 }
308
309 /**
310 * Parses the TRACK command.
311 *
312 * @param string $line - The line in the cue file that contains the TRACK command.
313 * @param integer $track_on - The track currently processing.
314 */
315 public function parseTrack($line, $track_on)
316 {
317 $line = substr($line, strpos($line, ' ') + 1);
318 $track = ltrim(substr($line, 0, strpos($line, ' ')), '0');
319
320 //find the data type.
321 $datatype = strtolower(substr($line, strpos($line, ' ') + 1));
322
323 $this->cuesheet['tracks'][$track_on] = array('track_number'=>$track, 'datatype'=>$datatype);
324 }
325
326 }
327