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